blob: 35893ffaf0373f315aa5c699e2db742742cc0824 [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;
Amith Yamasani350938e2013-04-09 10:22:47 -070023import android.content.Context;
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +080024import android.content.DialogInterface;
Amith Yamasanid7993472010-08-18 13:59:28 -070025import android.content.pm.PackageManager;
Amith Yamasanid7993472010-08-18 13:59:28 -070026import android.os.Bundle;
Amith Yamasani9627a8e2012-09-23 12:54:14 -070027import android.preference.Preference;
Amith Yamasanid7993472010-08-18 13:59:28 -070028import android.preference.PreferenceFragment;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070029import android.text.TextUtils;
Amith Yamasanid7993472010-08-18 13:59:28 -070030import android.util.Log;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070031import android.view.Menu;
32import android.view.MenuInflater;
33import android.view.MenuItem;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070034import android.widget.Button;
Amith Yamasanid7993472010-08-18 13:59:28 -070035
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070036/**
Amith Yamasanid7993472010-08-18 13:59:28 -070037 * Base class for Settings fragments, with some helper functions and dialog management.
38 */
Gilles Debunne64650542011-08-23 11:01:35 -070039public class SettingsPreferenceFragment extends PreferenceFragment implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070040
41 private static final String TAG = "SettingsPreferenceFragment";
42
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070043 private static final int MENU_HELP = Menu.FIRST + 100;
44
Amith Yamasanid7993472010-08-18 13:59:28 -070045 private SettingsDialogFragment mDialogFragment;
46
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070047 private String mHelpUrl;
48
Amith Yamasani350938e2013-04-09 10:22:47 -070049 // Cache the content resolver for async callbacks
50 private ContentResolver mContentResolver;
51
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070052 @Override
53 public void onCreate(Bundle icicle) {
54 super.onCreate(icicle);
55
56 // Prepare help url and enable menu if necessary
57 int helpResource = getHelpResource();
58 if (helpResource != 0) {
59 mHelpUrl = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070060 }
61 }
62
Daisuke Miyakawab5647c52010-09-10 18:04:02 -070063 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -070064 public void onActivityCreated(Bundle savedInstanceState) {
65 super.onActivityCreated(savedInstanceState);
Amith Yamasanib3a593e2012-04-23 18:03:52 -070066 if (!TextUtils.isEmpty(mHelpUrl)) {
67 setHasOptionsMenu(true);
68 }
Amith Yamasanid7993472010-08-18 13:59:28 -070069 }
70
Amith Yamasani9627a8e2012-09-23 12:54:14 -070071 protected void removePreference(String key) {
72 Preference pref = findPreference(key);
73 if (pref != null) {
74 getPreferenceScreen().removePreference(pref);
75 }
76 }
77
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070078 /**
79 * Override this if you want to show a help item in the menu, by returning the resource id.
80 * @return the resource id for the help url
81 */
82 protected int getHelpResource() {
83 return 0;
84 }
85
86 @Override
87 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -080088 if (mHelpUrl != null && getActivity() != null) {
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070089 MenuItem helpItem = menu.add(0, MENU_HELP, 0, R.string.help_label);
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -080090 HelpUtils.prepareHelpMenuItem(getActivity(), helpItem, mHelpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070091 }
92 }
93
Daisuke Miyakawab5647c52010-09-10 18:04:02 -070094 /*
95 * The name is intentionally made different from Activity#finish(), so that
96 * users won't misunderstand its meaning.
97 */
98 public final void finishFragment() {
99 getActivity().onBackPressed();
100 }
101
Amith Yamasanid7993472010-08-18 13:59:28 -0700102 // Some helpers for functions used by the settings fragments when they were activities
103
104 /**
105 * Returns the ContentResolver from the owning Activity.
106 */
107 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700108 Context context = getActivity();
109 if (context != null) {
110 mContentResolver = context.getContentResolver();
111 }
112 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700113 }
114
115 /**
116 * Returns the specified system service from the owning Activity.
117 */
118 protected Object getSystemService(final String name) {
119 return getActivity().getSystemService(name);
120 }
121
122 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700123 * Returns the PackageManager from the owning Activity.
124 */
125 protected PackageManager getPackageManager() {
126 return getActivity().getPackageManager();
127 }
128
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800129 @Override
130 public void onDetach() {
131 if (isRemoving()) {
132 if (mDialogFragment != null) {
133 mDialogFragment.dismiss();
134 mDialogFragment = null;
135 }
136 }
137 super.onDetach();
138 }
139
Amith Yamasanid7993472010-08-18 13:59:28 -0700140 // Dialog management
141
142 protected void showDialog(int dialogId) {
143 if (mDialogFragment != null) {
144 Log.e(TAG, "Old dialog fragment not null!");
145 }
146 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800147 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700148 }
149
150 public Dialog onCreateDialog(int dialogId) {
151 return null;
152 }
153
154 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800155 // mDialogFragment may not be visible yet in parent fragment's onResume().
156 // To be able to dismiss dialog at that time, don't check
157 // mDialogFragment.isVisible().
158 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700159 mDialogFragment.dismiss();
160 }
161 mDialogFragment = null;
162 }
163
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800164 /**
165 * Sets the OnCancelListener of the dialog shown. This method can only be
166 * called after showDialog(int) and before removeDialog(int). The method
167 * does nothing otherwise.
168 */
169 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
170 if (mDialogFragment != null) {
171 mDialogFragment.mOnCancelListener = listener;
172 }
173 }
174
175 /**
176 * Sets the OnDismissListener of the dialog shown. This method can only be
177 * called after showDialog(int) and before removeDialog(int). The method
178 * does nothing otherwise.
179 */
180 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
181 if (mDialogFragment != null) {
182 mDialogFragment.mOnDismissListener = listener;
183 }
184 }
185
Amith Yamasanic861cf82012-10-02 14:51:46 -0700186 public void onDialogShowing() {
187 // override in subclass to attach a dismiss listener, for instance
188 }
189
Amith Yamasani43c69782010-12-01 09:04:36 -0800190 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800191 private static final String KEY_DIALOG_ID = "key_dialog_id";
192 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
193
Amith Yamasanid7993472010-08-18 13:59:28 -0700194 private int mDialogId;
195
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800196 private Fragment mParentFragment;
197
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800198 private DialogInterface.OnCancelListener mOnCancelListener;
199 private DialogInterface.OnDismissListener mOnDismissListener;
200
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800201 public SettingsDialogFragment() {
202 /* do nothing */
203 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700204
Amith Yamasani43c69782010-12-01 09:04:36 -0800205 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700206 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800207 if (!(fragment instanceof Fragment)) {
208 throw new IllegalArgumentException("fragment argument must be an instance of "
209 + Fragment.class.getName());
210 }
211 mParentFragment = (Fragment) fragment;
212 }
213
214 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800215 public void onSaveInstanceState(Bundle outState) {
216 super.onSaveInstanceState(outState);
217 if (mParentFragment != null) {
218 outState.putInt(KEY_DIALOG_ID, mDialogId);
219 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
220 }
221 }
222
223 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700224 public void onStart() {
225 super.onStart();
226
227 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
228 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
229 }
230 }
231
232 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800233 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800234 if (savedInstanceState != null) {
235 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800236 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800237 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800238 if (!(mParentFragment instanceof DialogCreatable)) {
239 throw new IllegalArgumentException(
240 (mParentFragment != null
241 ? mParentFragment.getClass().getName()
242 : mParentFragmentId)
243 + " must implement "
244 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800245 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800246 // This dialog fragment could be created from non-SettingsPreferenceFragment
247 if (mParentFragment instanceof SettingsPreferenceFragment) {
248 // restore mDialogFragment in mParentFragment
249 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
250 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800251 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800252 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700253 }
254
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800255 @Override
256 public void onCancel(DialogInterface dialog) {
257 super.onCancel(dialog);
258 if (mOnCancelListener != null) {
259 mOnCancelListener.onCancel(dialog);
260 }
261 }
262
263 @Override
264 public void onDismiss(DialogInterface dialog) {
265 super.onDismiss(dialog);
266 if (mOnDismissListener != null) {
267 mOnDismissListener.onDismiss(dialog);
268 }
269 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800270
Amith Yamasanid7993472010-08-18 13:59:28 -0700271 public int getDialogId() {
272 return mDialogId;
273 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800274
275 @Override
276 public void onDetach() {
277 super.onDetach();
278
Amith Yamasani8875ede2011-01-31 12:46:57 -0800279 // This dialog fragment could be created from non-SettingsPreferenceFragment
280 if (mParentFragment instanceof SettingsPreferenceFragment) {
281 // in case the dialog is not explicitly removed by removeDialog()
282 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
283 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
284 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800285 }
286 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700287 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700288
289 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800290 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700291 }
292
293 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800294 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700295 }
296
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700297 public void finish() {
298 getActivity().onBackPressed();
299 }
300
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700301 public boolean startFragment(
302 Fragment caller, String fragmentClass, int requestCode, Bundle extras) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800303 if (getActivity() instanceof SettingsActivity) {
304 SettingsActivity sa = (SettingsActivity) getActivity();
305 sa.startPreferencePanel(fragmentClass, extras,
Gilles Debunne64650542011-08-23 11:01:35 -0700306 R.string.lock_settings_picker_title, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700307 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700308 } else {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800309 Log.w(TAG, "Parent isn't Settings activity, thus there's no way to launch the "
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700310 + "given Fragment (name: " + fragmentClass + ", requestCode: " + requestCode
311 + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700312 return false;
313 }
314 }
315
Amith Yamasanid7993472010-08-18 13:59:28 -0700316}