Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settings; |
| 18 | |
| 19 | import android.app.Dialog; |
| 20 | import android.app.DialogFragment; |
Daisuke Miyakawa | b5647c5 | 2010-09-10 18:04:02 -0700 | [diff] [blame] | 21 | import android.app.Fragment; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 22 | import android.content.ContentResolver; |
Amith Yamasani | 350938e | 2013-04-09 10:22:47 -0700 | [diff] [blame^] | 23 | import android.content.Context; |
Hung-ying Tyan | 0ee51e0 | 2011-01-25 16:42:14 +0800 | [diff] [blame] | 24 | import android.content.DialogInterface; |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 25 | import android.content.Intent; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 26 | import android.content.pm.PackageManager; |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 27 | import android.net.Uri; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 28 | import android.os.Bundle; |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 29 | import android.preference.Preference; |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 30 | import android.preference.PreferenceActivity; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 31 | import android.preference.PreferenceFragment; |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 32 | import android.text.TextUtils; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 33 | import android.util.Log; |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 34 | import android.view.Menu; |
| 35 | import android.view.MenuInflater; |
| 36 | import android.view.MenuItem; |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 37 | import android.widget.Button; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 38 | |
Daisuke Miyakawa | f58090d | 2010-09-12 17:27:33 -0700 | [diff] [blame] | 39 | /** |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 40 | * Base class for Settings fragments, with some helper functions and dialog management. |
| 41 | */ |
Gilles Debunne | 6465054 | 2011-08-23 11:01:35 -0700 | [diff] [blame] | 42 | public class SettingsPreferenceFragment extends PreferenceFragment implements DialogCreatable { |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 43 | |
| 44 | private static final String TAG = "SettingsPreferenceFragment"; |
| 45 | |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 46 | private static final int MENU_HELP = Menu.FIRST + 100; |
| 47 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 48 | private SettingsDialogFragment mDialogFragment; |
| 49 | |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 50 | private String mHelpUrl; |
| 51 | |
Amith Yamasani | 350938e | 2013-04-09 10:22:47 -0700 | [diff] [blame^] | 52 | // Cache the content resolver for async callbacks |
| 53 | private ContentResolver mContentResolver; |
| 54 | |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 55 | @Override |
| 56 | public void onCreate(Bundle icicle) { |
| 57 | super.onCreate(icicle); |
| 58 | |
| 59 | // Prepare help url and enable menu if necessary |
| 60 | int helpResource = getHelpResource(); |
| 61 | if (helpResource != 0) { |
| 62 | mHelpUrl = getResources().getString(helpResource); |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
Daisuke Miyakawa | b5647c5 | 2010-09-10 18:04:02 -0700 | [diff] [blame] | 66 | @Override |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 67 | public void onActivityCreated(Bundle savedInstanceState) { |
| 68 | super.onActivityCreated(savedInstanceState); |
Amith Yamasani | b3a593e | 2012-04-23 18:03:52 -0700 | [diff] [blame] | 69 | if (!TextUtils.isEmpty(mHelpUrl)) { |
| 70 | setHasOptionsMenu(true); |
| 71 | } |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 74 | protected void removePreference(String key) { |
| 75 | Preference pref = findPreference(key); |
| 76 | if (pref != null) { |
| 77 | getPreferenceScreen().removePreference(pref); |
| 78 | } |
| 79 | } |
| 80 | |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 81 | /** |
| 82 | * Override this if you want to show a help item in the menu, by returning the resource id. |
| 83 | * @return the resource id for the help url |
| 84 | */ |
| 85 | protected int getHelpResource() { |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
Amith Yamasani | aeb57ed | 2012-12-06 14:40:51 -0800 | [diff] [blame] | 91 | if (mHelpUrl != null && getActivity() != null) { |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 92 | MenuItem helpItem = menu.add(0, MENU_HELP, 0, R.string.help_label); |
Amith Yamasani | aeb57ed | 2012-12-06 14:40:51 -0800 | [diff] [blame] | 93 | HelpUtils.prepareHelpMenuItem(getActivity(), helpItem, mHelpUrl); |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
Daisuke Miyakawa | b5647c5 | 2010-09-10 18:04:02 -0700 | [diff] [blame] | 97 | /* |
| 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 Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 105 | // 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() { |
Amith Yamasani | 350938e | 2013-04-09 10:22:47 -0700 | [diff] [blame^] | 111 | Context context = getActivity(); |
| 112 | if (context != null) { |
| 113 | mContentResolver = context.getContentResolver(); |
| 114 | } |
| 115 | return mContentResolver; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Returns the specified system service from the owning Activity. |
| 120 | */ |
| 121 | protected Object getSystemService(final String name) { |
| 122 | return getActivity().getSystemService(name); |
| 123 | } |
| 124 | |
| 125 | /** |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 126 | * Returns the PackageManager from the owning Activity. |
| 127 | */ |
| 128 | protected PackageManager getPackageManager() { |
| 129 | return getActivity().getPackageManager(); |
| 130 | } |
| 131 | |
Dianne Hackborn | 0385cf1 | 2011-01-24 16:22:13 -0800 | [diff] [blame] | 132 | @Override |
| 133 | public void onDetach() { |
| 134 | if (isRemoving()) { |
| 135 | if (mDialogFragment != null) { |
| 136 | mDialogFragment.dismiss(); |
| 137 | mDialogFragment = null; |
| 138 | } |
| 139 | } |
| 140 | super.onDetach(); |
| 141 | } |
| 142 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 143 | // Dialog management |
| 144 | |
| 145 | protected void showDialog(int dialogId) { |
| 146 | if (mDialogFragment != null) { |
| 147 | Log.e(TAG, "Old dialog fragment not null!"); |
| 148 | } |
| 149 | mDialogFragment = new SettingsDialogFragment(this, dialogId); |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 150 | mDialogFragment.show(getActivity().getFragmentManager(), Integer.toString(dialogId)); |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | public Dialog onCreateDialog(int dialogId) { |
| 154 | return null; |
| 155 | } |
| 156 | |
| 157 | protected void removeDialog(int dialogId) { |
Hung-ying Tyan | adc83d8 | 2011-01-24 15:05:27 +0800 | [diff] [blame] | 158 | // mDialogFragment may not be visible yet in parent fragment's onResume(). |
| 159 | // To be able to dismiss dialog at that time, don't check |
| 160 | // mDialogFragment.isVisible(). |
| 161 | if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) { |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 162 | mDialogFragment.dismiss(); |
| 163 | } |
| 164 | mDialogFragment = null; |
| 165 | } |
| 166 | |
Hung-ying Tyan | 0ee51e0 | 2011-01-25 16:42:14 +0800 | [diff] [blame] | 167 | /** |
| 168 | * Sets the OnCancelListener of the dialog shown. This method can only be |
| 169 | * called after showDialog(int) and before removeDialog(int). The method |
| 170 | * does nothing otherwise. |
| 171 | */ |
| 172 | protected void setOnCancelListener(DialogInterface.OnCancelListener listener) { |
| 173 | if (mDialogFragment != null) { |
| 174 | mDialogFragment.mOnCancelListener = listener; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Sets the OnDismissListener of the dialog shown. This method can only be |
| 180 | * called after showDialog(int) and before removeDialog(int). The method |
| 181 | * does nothing otherwise. |
| 182 | */ |
| 183 | protected void setOnDismissListener(DialogInterface.OnDismissListener listener) { |
| 184 | if (mDialogFragment != null) { |
| 185 | mDialogFragment.mOnDismissListener = listener; |
| 186 | } |
| 187 | } |
| 188 | |
Amith Yamasani | c861cf8 | 2012-10-02 14:51:46 -0700 | [diff] [blame] | 189 | public void onDialogShowing() { |
| 190 | // override in subclass to attach a dismiss listener, for instance |
| 191 | } |
| 192 | |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 193 | public static class SettingsDialogFragment extends DialogFragment { |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 194 | private static final String KEY_DIALOG_ID = "key_dialog_id"; |
| 195 | private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id"; |
| 196 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 197 | private int mDialogId; |
| 198 | |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 199 | private Fragment mParentFragment; |
| 200 | |
Hung-ying Tyan | 0ee51e0 | 2011-01-25 16:42:14 +0800 | [diff] [blame] | 201 | private DialogInterface.OnCancelListener mOnCancelListener; |
| 202 | private DialogInterface.OnDismissListener mOnDismissListener; |
| 203 | |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 204 | public SettingsDialogFragment() { |
| 205 | /* do nothing */ |
| 206 | } |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 207 | |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 208 | public SettingsDialogFragment(DialogCreatable fragment, int dialogId) { |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 209 | mDialogId = dialogId; |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 210 | if (!(fragment instanceof Fragment)) { |
| 211 | throw new IllegalArgumentException("fragment argument must be an instance of " |
| 212 | + Fragment.class.getName()); |
| 213 | } |
| 214 | mParentFragment = (Fragment) fragment; |
| 215 | } |
| 216 | |
| 217 | @Override |
Dianne Hackborn | 300768f | 2011-01-27 20:39:21 -0800 | [diff] [blame] | 218 | public void onSaveInstanceState(Bundle outState) { |
| 219 | super.onSaveInstanceState(outState); |
| 220 | if (mParentFragment != null) { |
| 221 | outState.putInt(KEY_DIALOG_ID, mDialogId); |
| 222 | outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId()); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | @Override |
Amith Yamasani | c861cf8 | 2012-10-02 14:51:46 -0700 | [diff] [blame] | 227 | public void onStart() { |
| 228 | super.onStart(); |
| 229 | |
| 230 | if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) { |
| 231 | ((SettingsPreferenceFragment) mParentFragment).onDialogShowing(); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | @Override |
Dianne Hackborn | 300768f | 2011-01-27 20:39:21 -0800 | [diff] [blame] | 236 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 237 | if (savedInstanceState != null) { |
| 238 | mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0); |
| 239 | int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1); |
| 240 | if (mParentFragmentId > -1) { |
| 241 | mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId); |
| 242 | if (!(mParentFragment instanceof DialogCreatable)) { |
| 243 | throw new IllegalArgumentException( |
Amith Yamasani | 258f7c7 | 2013-02-05 10:56:55 -0800 | [diff] [blame] | 244 | (mParentFragment != null |
| 245 | ? mParentFragment.getClass().getName() |
| 246 | : mParentFragmentId) |
| 247 | + " must implement " |
| 248 | + DialogCreatable.class.getName()); |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 249 | } |
| 250 | } |
Amith Yamasani | 8875ede | 2011-01-31 12:46:57 -0800 | [diff] [blame] | 251 | // This dialog fragment could be created from non-SettingsPreferenceFragment |
| 252 | if (mParentFragment instanceof SettingsPreferenceFragment) { |
| 253 | // restore mDialogFragment in mParentFragment |
| 254 | ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this; |
| 255 | } |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 256 | } |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 257 | return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId); |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Hung-ying Tyan | 0ee51e0 | 2011-01-25 16:42:14 +0800 | [diff] [blame] | 260 | @Override |
| 261 | public void onCancel(DialogInterface dialog) { |
| 262 | super.onCancel(dialog); |
| 263 | if (mOnCancelListener != null) { |
| 264 | mOnCancelListener.onCancel(dialog); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | @Override |
| 269 | public void onDismiss(DialogInterface dialog) { |
| 270 | super.onDismiss(dialog); |
| 271 | if (mOnDismissListener != null) { |
| 272 | mOnDismissListener.onDismiss(dialog); |
| 273 | } |
| 274 | } |
Amith Yamasani | 8875ede | 2011-01-31 12:46:57 -0800 | [diff] [blame] | 275 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 276 | public int getDialogId() { |
| 277 | return mDialogId; |
| 278 | } |
Hung-ying Tyan | 18eb39d | 2011-01-28 16:17:27 +0800 | [diff] [blame] | 279 | |
| 280 | @Override |
| 281 | public void onDetach() { |
| 282 | super.onDetach(); |
| 283 | |
Amith Yamasani | 8875ede | 2011-01-31 12:46:57 -0800 | [diff] [blame] | 284 | // This dialog fragment could be created from non-SettingsPreferenceFragment |
| 285 | if (mParentFragment instanceof SettingsPreferenceFragment) { |
| 286 | // in case the dialog is not explicitly removed by removeDialog() |
| 287 | if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) { |
| 288 | ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null; |
| 289 | } |
Hung-ying Tyan | 18eb39d | 2011-01-28 16:17:27 +0800 | [diff] [blame] | 290 | } |
| 291 | } |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 292 | } |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 293 | |
| 294 | protected boolean hasNextButton() { |
Daisuke Miyakawa | 79c5fd9 | 2011-01-15 14:58:00 -0800 | [diff] [blame] | 295 | return ((ButtonBarHandler)getActivity()).hasNextButton(); |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | protected Button getNextButton() { |
Daisuke Miyakawa | 79c5fd9 | 2011-01-15 14:58:00 -0800 | [diff] [blame] | 299 | return ((ButtonBarHandler)getActivity()).getNextButton(); |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Daisuke Miyakawa | 6ebf861 | 2010-09-10 09:48:51 -0700 | [diff] [blame] | 302 | public void finish() { |
| 303 | getActivity().onBackPressed(); |
| 304 | } |
| 305 | |
Daisuke Miyakawa | b5647c5 | 2010-09-10 18:04:02 -0700 | [diff] [blame] | 306 | public boolean startFragment( |
| 307 | Fragment caller, String fragmentClass, int requestCode, Bundle extras) { |
Daisuke Miyakawa | 25af150 | 2010-09-24 11:29:31 -0700 | [diff] [blame] | 308 | if (getActivity() instanceof PreferenceActivity) { |
| 309 | PreferenceActivity preferenceActivity = (PreferenceActivity)getActivity(); |
Gilles Debunne | 6465054 | 2011-08-23 11:01:35 -0700 | [diff] [blame] | 310 | preferenceActivity.startPreferencePanel(fragmentClass, extras, |
| 311 | R.string.lock_settings_picker_title, null, caller, requestCode); |
Daisuke Miyakawa | 25af150 | 2010-09-24 11:29:31 -0700 | [diff] [blame] | 312 | return true; |
Daisuke Miyakawa | b5647c5 | 2010-09-10 18:04:02 -0700 | [diff] [blame] | 313 | } else { |
Daisuke Miyakawa | 25af150 | 2010-09-24 11:29:31 -0700 | [diff] [blame] | 314 | Log.w(TAG, "Parent isn't PreferenceActivity, thus there's no way to launch the " |
| 315 | + "given Fragment (name: " + fragmentClass + ", requestCode: " + requestCode |
| 316 | + ")"); |
Daisuke Miyakawa | b5647c5 | 2010-09-10 18:04:02 -0700 | [diff] [blame] | 317 | return false; |
| 318 | } |
| 319 | } |
| 320 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 321 | } |