blob: a5f021fddf05fa04597ce5bee7dd9c93e50180e9 [file] [log] [blame]
Amith Yamasanid7993472010-08-18 13:59:28 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
19import android.app.Dialog;
20import android.app.DialogFragment;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -070021import android.app.Fragment;
Amith Yamasanid7993472010-08-18 13:59:28 -070022import android.content.ContentResolver;
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +080023import android.content.DialogInterface;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070024import android.content.Intent;
Amith Yamasanid7993472010-08-18 13:59:28 -070025import android.content.pm.PackageManager;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070026import android.net.Uri;
Amith Yamasanid7993472010-08-18 13:59:28 -070027import android.os.Bundle;
Amith Yamasani9627a8e2012-09-23 12:54:14 -070028import android.preference.Preference;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070029import android.preference.PreferenceActivity;
Amith Yamasanid7993472010-08-18 13:59:28 -070030import android.preference.PreferenceFragment;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070031import android.text.TextUtils;
Amith Yamasanid7993472010-08-18 13:59:28 -070032import android.util.Log;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070033import android.view.Menu;
34import android.view.MenuInflater;
35import android.view.MenuItem;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070036import android.widget.Button;
Amith Yamasanid7993472010-08-18 13:59:28 -070037
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070038/**
Amith Yamasanid7993472010-08-18 13:59:28 -070039 * Base class for Settings fragments, with some helper functions and dialog management.
40 */
Gilles Debunne64650542011-08-23 11:01:35 -070041public class SettingsPreferenceFragment extends PreferenceFragment implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070042
43 private static final String TAG = "SettingsPreferenceFragment";
44
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070045 private static final int MENU_HELP = Menu.FIRST + 100;
46
Amith Yamasanid7993472010-08-18 13:59:28 -070047 private SettingsDialogFragment mDialogFragment;
48
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070049 private String mHelpUrl;
50
51 @Override
52 public void onCreate(Bundle icicle) {
53 super.onCreate(icicle);
54
55 // Prepare help url and enable menu if necessary
56 int helpResource = getHelpResource();
57 if (helpResource != 0) {
58 mHelpUrl = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070059 }
60 }
61
Daisuke Miyakawab5647c52010-09-10 18:04:02 -070062 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -070063 public void onActivityCreated(Bundle savedInstanceState) {
64 super.onActivityCreated(savedInstanceState);
Amith Yamasanib3a593e2012-04-23 18:03:52 -070065 if (!TextUtils.isEmpty(mHelpUrl)) {
66 setHasOptionsMenu(true);
67 }
Amith Yamasanid7993472010-08-18 13:59:28 -070068 }
69
Amith Yamasani9627a8e2012-09-23 12:54:14 -070070 protected void removePreference(String key) {
71 Preference pref = findPreference(key);
72 if (pref != null) {
73 getPreferenceScreen().removePreference(pref);
74 }
75 }
76
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070077 /**
78 * Override this if you want to show a help item in the menu, by returning the resource id.
79 * @return the resource id for the help url
80 */
81 protected int getHelpResource() {
82 return 0;
83 }
84
85 @Override
86 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
87 if (mHelpUrl != null) {
88 Intent helpIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(mHelpUrl));
89 helpIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
90 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
91 MenuItem helpItem = menu.add(0, MENU_HELP, 0, R.string.help_label);
92 helpItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
93 helpItem.setIntent(helpIntent);
94 }
95 }
96
Daisuke Miyakawab5647c52010-09-10 18:04:02 -070097 /*
98 * The name is intentionally made different from Activity#finish(), so that
99 * users won't misunderstand its meaning.
100 */
101 public final void finishFragment() {
102 getActivity().onBackPressed();
103 }
104
Amith Yamasanid7993472010-08-18 13:59:28 -0700105 // Some helpers for functions used by the settings fragments when they were activities
106
107 /**
108 * Returns the ContentResolver from the owning Activity.
109 */
110 protected ContentResolver getContentResolver() {
111 return getActivity().getContentResolver();
112 }
113
114 /**
115 * Returns the specified system service from the owning Activity.
116 */
117 protected Object getSystemService(final String name) {
118 return getActivity().getSystemService(name);
119 }
120
121 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700122 * Returns the PackageManager from the owning Activity.
123 */
124 protected PackageManager getPackageManager() {
125 return getActivity().getPackageManager();
126 }
127
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800128 @Override
129 public void onDetach() {
130 if (isRemoving()) {
131 if (mDialogFragment != null) {
132 mDialogFragment.dismiss();
133 mDialogFragment = null;
134 }
135 }
136 super.onDetach();
137 }
138
Amith Yamasanid7993472010-08-18 13:59:28 -0700139 // Dialog management
140
141 protected void showDialog(int dialogId) {
142 if (mDialogFragment != null) {
143 Log.e(TAG, "Old dialog fragment not null!");
144 }
145 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -0700146 mDialogFragment.show(getActivity().getFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700147 }
148
149 public Dialog onCreateDialog(int dialogId) {
150 return null;
151 }
152
153 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800154 // mDialogFragment may not be visible yet in parent fragment's onResume().
155 // To be able to dismiss dialog at that time, don't check
156 // mDialogFragment.isVisible().
157 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700158 mDialogFragment.dismiss();
159 }
160 mDialogFragment = null;
161 }
162
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800163 /**
164 * Sets the OnCancelListener of the dialog shown. This method can only be
165 * called after showDialog(int) and before removeDialog(int). The method
166 * does nothing otherwise.
167 */
168 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
169 if (mDialogFragment != null) {
170 mDialogFragment.mOnCancelListener = listener;
171 }
172 }
173
174 /**
175 * Sets the OnDismissListener of the dialog shown. This method can only be
176 * called after showDialog(int) and before removeDialog(int). The method
177 * does nothing otherwise.
178 */
179 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
180 if (mDialogFragment != null) {
181 mDialogFragment.mOnDismissListener = listener;
182 }
183 }
184
Amith Yamasani43c69782010-12-01 09:04:36 -0800185 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800186 private static final String KEY_DIALOG_ID = "key_dialog_id";
187 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
188
Amith Yamasanid7993472010-08-18 13:59:28 -0700189 private int mDialogId;
190
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800191 private Fragment mParentFragment;
192
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800193 private DialogInterface.OnCancelListener mOnCancelListener;
194 private DialogInterface.OnDismissListener mOnDismissListener;
195
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800196 public SettingsDialogFragment() {
197 /* do nothing */
198 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700199
Amith Yamasani43c69782010-12-01 09:04:36 -0800200 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700201 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800202 if (!(fragment instanceof Fragment)) {
203 throw new IllegalArgumentException("fragment argument must be an instance of "
204 + Fragment.class.getName());
205 }
206 mParentFragment = (Fragment) fragment;
207 }
208
209 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800210 public void onSaveInstanceState(Bundle outState) {
211 super.onSaveInstanceState(outState);
212 if (mParentFragment != null) {
213 outState.putInt(KEY_DIALOG_ID, mDialogId);
214 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
215 }
216 }
217
218 @Override
219 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800220 if (savedInstanceState != null) {
221 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
222 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
223 if (mParentFragmentId > -1) {
224 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
225 if (!(mParentFragment instanceof DialogCreatable)) {
226 throw new IllegalArgumentException(
227 KEY_PARENT_FRAGMENT_ID + " must implement "
228 + DialogCreatable.class.getName());
229 }
230 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800231 // This dialog fragment could be created from non-SettingsPreferenceFragment
232 if (mParentFragment instanceof SettingsPreferenceFragment) {
233 // restore mDialogFragment in mParentFragment
234 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
235 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800236 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800237 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700238 }
239
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800240 @Override
241 public void onCancel(DialogInterface dialog) {
242 super.onCancel(dialog);
243 if (mOnCancelListener != null) {
244 mOnCancelListener.onCancel(dialog);
245 }
246 }
247
248 @Override
249 public void onDismiss(DialogInterface dialog) {
250 super.onDismiss(dialog);
251 if (mOnDismissListener != null) {
252 mOnDismissListener.onDismiss(dialog);
253 }
254 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800255
Amith Yamasanid7993472010-08-18 13:59:28 -0700256 public int getDialogId() {
257 return mDialogId;
258 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800259
260 @Override
261 public void onDetach() {
262 super.onDetach();
263
Amith Yamasani8875ede2011-01-31 12:46:57 -0800264 // This dialog fragment could be created from non-SettingsPreferenceFragment
265 if (mParentFragment instanceof SettingsPreferenceFragment) {
266 // in case the dialog is not explicitly removed by removeDialog()
267 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
268 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
269 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800270 }
271 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700272 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700273
274 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800275 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700276 }
277
278 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800279 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700280 }
281
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700282 public void finish() {
283 getActivity().onBackPressed();
284 }
285
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700286 public boolean startFragment(
287 Fragment caller, String fragmentClass, int requestCode, Bundle extras) {
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700288 if (getActivity() instanceof PreferenceActivity) {
289 PreferenceActivity preferenceActivity = (PreferenceActivity)getActivity();
Gilles Debunne64650542011-08-23 11:01:35 -0700290 preferenceActivity.startPreferencePanel(fragmentClass, extras,
291 R.string.lock_settings_picker_title, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700292 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700293 } else {
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700294 Log.w(TAG, "Parent isn't PreferenceActivity, thus there's no way to launch the "
295 + "given Fragment (name: " + fragmentClass + ", requestCode: " + requestCode
296 + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700297 return false;
298 }
299 }
300
Amith Yamasanid7993472010-08-18 13:59:28 -0700301}