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; |
Hung-ying Tyan | 0ee51e0 | 2011-01-25 16:42:14 +0800 | [diff] [blame] | 23 | import android.content.DialogInterface; |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 24 | import android.content.Intent; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 25 | import android.content.pm.PackageManager; |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 26 | import android.net.Uri; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 27 | import android.os.Bundle; |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 28 | import android.preference.Preference; |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 29 | import android.preference.PreferenceActivity; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 30 | import android.preference.PreferenceFragment; |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 31 | import android.text.TextUtils; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 32 | import android.util.Log; |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 33 | import android.view.Menu; |
| 34 | import android.view.MenuInflater; |
| 35 | import android.view.MenuItem; |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 36 | import android.widget.Button; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 37 | |
Daisuke Miyakawa | f58090d | 2010-09-12 17:27:33 -0700 | [diff] [blame] | 38 | /** |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 39 | * Base class for Settings fragments, with some helper functions and dialog management. |
| 40 | */ |
Gilles Debunne | 6465054 | 2011-08-23 11:01:35 -0700 | [diff] [blame] | 41 | public class SettingsPreferenceFragment extends PreferenceFragment implements DialogCreatable { |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 42 | |
| 43 | private static final String TAG = "SettingsPreferenceFragment"; |
| 44 | |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 45 | private static final int MENU_HELP = Menu.FIRST + 100; |
| 46 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 47 | private SettingsDialogFragment mDialogFragment; |
| 48 | |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 49 | 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 Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
Daisuke Miyakawa | b5647c5 | 2010-09-10 18:04:02 -0700 | [diff] [blame] | 62 | @Override |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 63 | public void onActivityCreated(Bundle savedInstanceState) { |
| 64 | super.onActivityCreated(savedInstanceState); |
Amith Yamasani | b3a593e | 2012-04-23 18:03:52 -0700 | [diff] [blame] | 65 | if (!TextUtils.isEmpty(mHelpUrl)) { |
| 66 | setHasOptionsMenu(true); |
| 67 | } |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 70 | protected void removePreference(String key) { |
| 71 | Preference pref = findPreference(key); |
| 72 | if (pref != null) { |
| 73 | getPreferenceScreen().removePreference(pref); |
| 74 | } |
| 75 | } |
| 76 | |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 77 | /** |
| 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 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() { |
| 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 Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 122 | * Returns the PackageManager from the owning Activity. |
| 123 | */ |
| 124 | protected PackageManager getPackageManager() { |
| 125 | return getActivity().getPackageManager(); |
| 126 | } |
| 127 | |
Dianne Hackborn | 0385cf1 | 2011-01-24 16:22:13 -0800 | [diff] [blame] | 128 | @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 Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 139 | // 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 Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 146 | mDialogFragment.show(getActivity().getFragmentManager(), Integer.toString(dialogId)); |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | public Dialog onCreateDialog(int dialogId) { |
| 150 | return null; |
| 151 | } |
| 152 | |
| 153 | protected void removeDialog(int dialogId) { |
Hung-ying Tyan | adc83d8 | 2011-01-24 15:05:27 +0800 | [diff] [blame] | 154 | // 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 Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 158 | mDialogFragment.dismiss(); |
| 159 | } |
| 160 | mDialogFragment = null; |
| 161 | } |
| 162 | |
Hung-ying Tyan | 0ee51e0 | 2011-01-25 16:42:14 +0800 | [diff] [blame] | 163 | /** |
| 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 Yamasani | c861cf8 | 2012-10-02 14:51:46 -0700 | [diff] [blame^] | 185 | public void onDialogShowing() { |
| 186 | // override in subclass to attach a dismiss listener, for instance |
| 187 | } |
| 188 | |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 189 | public static class SettingsDialogFragment extends DialogFragment { |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 190 | private static final String KEY_DIALOG_ID = "key_dialog_id"; |
| 191 | private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id"; |
| 192 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 193 | private int mDialogId; |
| 194 | |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 195 | private Fragment mParentFragment; |
| 196 | |
Hung-ying Tyan | 0ee51e0 | 2011-01-25 16:42:14 +0800 | [diff] [blame] | 197 | private DialogInterface.OnCancelListener mOnCancelListener; |
| 198 | private DialogInterface.OnDismissListener mOnDismissListener; |
| 199 | |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 200 | public SettingsDialogFragment() { |
| 201 | /* do nothing */ |
| 202 | } |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 203 | |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 204 | public SettingsDialogFragment(DialogCreatable fragment, int dialogId) { |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 205 | mDialogId = dialogId; |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 206 | if (!(fragment instanceof Fragment)) { |
| 207 | throw new IllegalArgumentException("fragment argument must be an instance of " |
| 208 | + Fragment.class.getName()); |
| 209 | } |
| 210 | mParentFragment = (Fragment) fragment; |
| 211 | } |
| 212 | |
| 213 | @Override |
Dianne Hackborn | 300768f | 2011-01-27 20:39:21 -0800 | [diff] [blame] | 214 | public void onSaveInstanceState(Bundle outState) { |
| 215 | super.onSaveInstanceState(outState); |
| 216 | if (mParentFragment != null) { |
| 217 | outState.putInt(KEY_DIALOG_ID, mDialogId); |
| 218 | outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId()); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | @Override |
Amith Yamasani | c861cf8 | 2012-10-02 14:51:46 -0700 | [diff] [blame^] | 223 | public void onStart() { |
| 224 | super.onStart(); |
| 225 | |
| 226 | if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) { |
| 227 | ((SettingsPreferenceFragment) mParentFragment).onDialogShowing(); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | @Override |
Dianne Hackborn | 300768f | 2011-01-27 20:39:21 -0800 | [diff] [blame] | 232 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 233 | if (savedInstanceState != null) { |
| 234 | mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0); |
| 235 | int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1); |
| 236 | if (mParentFragmentId > -1) { |
| 237 | mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId); |
| 238 | if (!(mParentFragment instanceof DialogCreatable)) { |
| 239 | throw new IllegalArgumentException( |
| 240 | KEY_PARENT_FRAGMENT_ID + " must implement " |
| 241 | + DialogCreatable.class.getName()); |
| 242 | } |
| 243 | } |
Amith Yamasani | 8875ede | 2011-01-31 12:46:57 -0800 | [diff] [blame] | 244 | // This dialog fragment could be created from non-SettingsPreferenceFragment |
| 245 | if (mParentFragment instanceof SettingsPreferenceFragment) { |
| 246 | // restore mDialogFragment in mParentFragment |
| 247 | ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this; |
| 248 | } |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 249 | } |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 250 | return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId); |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Hung-ying Tyan | 0ee51e0 | 2011-01-25 16:42:14 +0800 | [diff] [blame] | 253 | @Override |
| 254 | public void onCancel(DialogInterface dialog) { |
| 255 | super.onCancel(dialog); |
| 256 | if (mOnCancelListener != null) { |
| 257 | mOnCancelListener.onCancel(dialog); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | public void onDismiss(DialogInterface dialog) { |
| 263 | super.onDismiss(dialog); |
| 264 | if (mOnDismissListener != null) { |
| 265 | mOnDismissListener.onDismiss(dialog); |
| 266 | } |
| 267 | } |
Amith Yamasani | 8875ede | 2011-01-31 12:46:57 -0800 | [diff] [blame] | 268 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 269 | public int getDialogId() { |
| 270 | return mDialogId; |
| 271 | } |
Hung-ying Tyan | 18eb39d | 2011-01-28 16:17:27 +0800 | [diff] [blame] | 272 | |
| 273 | @Override |
| 274 | public void onDetach() { |
| 275 | super.onDetach(); |
| 276 | |
Amith Yamasani | 8875ede | 2011-01-31 12:46:57 -0800 | [diff] [blame] | 277 | // This dialog fragment could be created from non-SettingsPreferenceFragment |
| 278 | if (mParentFragment instanceof SettingsPreferenceFragment) { |
| 279 | // in case the dialog is not explicitly removed by removeDialog() |
| 280 | if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) { |
| 281 | ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null; |
| 282 | } |
Hung-ying Tyan | 18eb39d | 2011-01-28 16:17:27 +0800 | [diff] [blame] | 283 | } |
| 284 | } |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 285 | } |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 286 | |
| 287 | protected boolean hasNextButton() { |
Daisuke Miyakawa | 79c5fd9 | 2011-01-15 14:58:00 -0800 | [diff] [blame] | 288 | return ((ButtonBarHandler)getActivity()).hasNextButton(); |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | protected Button getNextButton() { |
Daisuke Miyakawa | 79c5fd9 | 2011-01-15 14:58:00 -0800 | [diff] [blame] | 292 | return ((ButtonBarHandler)getActivity()).getNextButton(); |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Daisuke Miyakawa | 6ebf861 | 2010-09-10 09:48:51 -0700 | [diff] [blame] | 295 | public void finish() { |
| 296 | getActivity().onBackPressed(); |
| 297 | } |
| 298 | |
Daisuke Miyakawa | b5647c5 | 2010-09-10 18:04:02 -0700 | [diff] [blame] | 299 | public boolean startFragment( |
| 300 | Fragment caller, String fragmentClass, int requestCode, Bundle extras) { |
Daisuke Miyakawa | 25af150 | 2010-09-24 11:29:31 -0700 | [diff] [blame] | 301 | if (getActivity() instanceof PreferenceActivity) { |
| 302 | PreferenceActivity preferenceActivity = (PreferenceActivity)getActivity(); |
Gilles Debunne | 6465054 | 2011-08-23 11:01:35 -0700 | [diff] [blame] | 303 | preferenceActivity.startPreferencePanel(fragmentClass, extras, |
| 304 | R.string.lock_settings_picker_title, null, caller, requestCode); |
Daisuke Miyakawa | 25af150 | 2010-09-24 11:29:31 -0700 | [diff] [blame] | 305 | return true; |
Daisuke Miyakawa | b5647c5 | 2010-09-10 18:04:02 -0700 | [diff] [blame] | 306 | } else { |
Daisuke Miyakawa | 25af150 | 2010-09-24 11:29:31 -0700 | [diff] [blame] | 307 | Log.w(TAG, "Parent isn't PreferenceActivity, thus there's no way to launch the " |
| 308 | + "given Fragment (name: " + fragmentClass + ", requestCode: " + requestCode |
| 309 | + ")"); |
Daisuke Miyakawa | b5647c5 | 2010-09-10 18:04:02 -0700 | [diff] [blame] | 310 | return false; |
| 311 | } |
| 312 | } |
| 313 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 314 | } |