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 | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 24 | import android.content.pm.PackageManager; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 25 | import android.os.Bundle; |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 26 | import android.preference.PreferenceActivity; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 27 | import android.preference.PreferenceFragment; |
| 28 | import android.util.Log; |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 29 | import android.widget.Button; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 30 | |
Daisuke Miyakawa | f58090d | 2010-09-12 17:27:33 -0700 | [diff] [blame] | 31 | /** |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 32 | * Base class for Settings fragments, with some helper functions and dialog management. |
| 33 | */ |
Gilles Debunne | 6465054 | 2011-08-23 11:01:35 -0700 | [diff] [blame] | 34 | public class SettingsPreferenceFragment extends PreferenceFragment implements DialogCreatable { |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 35 | |
| 36 | private static final String TAG = "SettingsPreferenceFragment"; |
| 37 | |
| 38 | private SettingsDialogFragment mDialogFragment; |
| 39 | |
Daisuke Miyakawa | b5647c5 | 2010-09-10 18:04:02 -0700 | [diff] [blame] | 40 | @Override |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 41 | public void onActivityCreated(Bundle savedInstanceState) { |
| 42 | super.onActivityCreated(savedInstanceState); |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Daisuke Miyakawa | b5647c5 | 2010-09-10 18:04:02 -0700 | [diff] [blame] | 45 | /* |
| 46 | * The name is intentionally made different from Activity#finish(), so that |
| 47 | * users won't misunderstand its meaning. |
| 48 | */ |
| 49 | public final void finishFragment() { |
| 50 | getActivity().onBackPressed(); |
| 51 | } |
| 52 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 53 | // Some helpers for functions used by the settings fragments when they were activities |
| 54 | |
| 55 | /** |
| 56 | * Returns the ContentResolver from the owning Activity. |
| 57 | */ |
| 58 | protected ContentResolver getContentResolver() { |
| 59 | return getActivity().getContentResolver(); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Returns the specified system service from the owning Activity. |
| 64 | */ |
| 65 | protected Object getSystemService(final String name) { |
| 66 | return getActivity().getSystemService(name); |
| 67 | } |
| 68 | |
| 69 | /** |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 70 | * Returns the PackageManager from the owning Activity. |
| 71 | */ |
| 72 | protected PackageManager getPackageManager() { |
| 73 | return getActivity().getPackageManager(); |
| 74 | } |
| 75 | |
Dianne Hackborn | 0385cf1 | 2011-01-24 16:22:13 -0800 | [diff] [blame] | 76 | @Override |
| 77 | public void onDetach() { |
| 78 | if (isRemoving()) { |
| 79 | if (mDialogFragment != null) { |
| 80 | mDialogFragment.dismiss(); |
| 81 | mDialogFragment = null; |
| 82 | } |
| 83 | } |
| 84 | super.onDetach(); |
| 85 | } |
| 86 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 87 | // Dialog management |
| 88 | |
| 89 | protected void showDialog(int dialogId) { |
| 90 | if (mDialogFragment != null) { |
| 91 | Log.e(TAG, "Old dialog fragment not null!"); |
| 92 | } |
| 93 | mDialogFragment = new SettingsDialogFragment(this, dialogId); |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 94 | mDialogFragment.show(getActivity().getFragmentManager(), Integer.toString(dialogId)); |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | public Dialog onCreateDialog(int dialogId) { |
| 98 | return null; |
| 99 | } |
| 100 | |
| 101 | protected void removeDialog(int dialogId) { |
Hung-ying Tyan | adc83d8 | 2011-01-24 15:05:27 +0800 | [diff] [blame] | 102 | // mDialogFragment may not be visible yet in parent fragment's onResume(). |
| 103 | // To be able to dismiss dialog at that time, don't check |
| 104 | // mDialogFragment.isVisible(). |
| 105 | if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) { |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 106 | mDialogFragment.dismiss(); |
| 107 | } |
| 108 | mDialogFragment = null; |
| 109 | } |
| 110 | |
Hung-ying Tyan | 0ee51e0 | 2011-01-25 16:42:14 +0800 | [diff] [blame] | 111 | /** |
| 112 | * Sets the OnCancelListener of the dialog shown. This method can only be |
| 113 | * called after showDialog(int) and before removeDialog(int). The method |
| 114 | * does nothing otherwise. |
| 115 | */ |
| 116 | protected void setOnCancelListener(DialogInterface.OnCancelListener listener) { |
| 117 | if (mDialogFragment != null) { |
| 118 | mDialogFragment.mOnCancelListener = listener; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Sets the OnDismissListener of the dialog shown. This method can only be |
| 124 | * called after showDialog(int) and before removeDialog(int). The method |
| 125 | * does nothing otherwise. |
| 126 | */ |
| 127 | protected void setOnDismissListener(DialogInterface.OnDismissListener listener) { |
| 128 | if (mDialogFragment != null) { |
| 129 | mDialogFragment.mOnDismissListener = listener; |
| 130 | } |
| 131 | } |
| 132 | |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 133 | public static class SettingsDialogFragment extends DialogFragment { |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 134 | private static final String KEY_DIALOG_ID = "key_dialog_id"; |
| 135 | private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id"; |
| 136 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 137 | private int mDialogId; |
| 138 | |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 139 | private Fragment mParentFragment; |
| 140 | |
Hung-ying Tyan | 0ee51e0 | 2011-01-25 16:42:14 +0800 | [diff] [blame] | 141 | private DialogInterface.OnCancelListener mOnCancelListener; |
| 142 | private DialogInterface.OnDismissListener mOnDismissListener; |
| 143 | |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 144 | public SettingsDialogFragment() { |
| 145 | /* do nothing */ |
| 146 | } |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 147 | |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 148 | public SettingsDialogFragment(DialogCreatable fragment, int dialogId) { |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 149 | mDialogId = dialogId; |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 150 | if (!(fragment instanceof Fragment)) { |
| 151 | throw new IllegalArgumentException("fragment argument must be an instance of " |
| 152 | + Fragment.class.getName()); |
| 153 | } |
| 154 | mParentFragment = (Fragment) fragment; |
| 155 | } |
| 156 | |
| 157 | @Override |
Dianne Hackborn | 300768f | 2011-01-27 20:39:21 -0800 | [diff] [blame] | 158 | public void onSaveInstanceState(Bundle outState) { |
| 159 | super.onSaveInstanceState(outState); |
| 160 | if (mParentFragment != null) { |
| 161 | outState.putInt(KEY_DIALOG_ID, mDialogId); |
| 162 | outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId()); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 168 | if (savedInstanceState != null) { |
| 169 | mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0); |
| 170 | int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1); |
| 171 | if (mParentFragmentId > -1) { |
| 172 | mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId); |
| 173 | if (!(mParentFragment instanceof DialogCreatable)) { |
| 174 | throw new IllegalArgumentException( |
| 175 | KEY_PARENT_FRAGMENT_ID + " must implement " |
| 176 | + DialogCreatable.class.getName()); |
| 177 | } |
| 178 | } |
Amith Yamasani | 8875ede | 2011-01-31 12:46:57 -0800 | [diff] [blame] | 179 | // This dialog fragment could be created from non-SettingsPreferenceFragment |
| 180 | if (mParentFragment instanceof SettingsPreferenceFragment) { |
| 181 | // restore mDialogFragment in mParentFragment |
| 182 | ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this; |
| 183 | } |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 184 | } |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 185 | return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId); |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Hung-ying Tyan | 0ee51e0 | 2011-01-25 16:42:14 +0800 | [diff] [blame] | 188 | @Override |
| 189 | public void onCancel(DialogInterface dialog) { |
| 190 | super.onCancel(dialog); |
| 191 | if (mOnCancelListener != null) { |
| 192 | mOnCancelListener.onCancel(dialog); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public void onDismiss(DialogInterface dialog) { |
| 198 | super.onDismiss(dialog); |
| 199 | if (mOnDismissListener != null) { |
| 200 | mOnDismissListener.onDismiss(dialog); |
| 201 | } |
| 202 | } |
Amith Yamasani | 8875ede | 2011-01-31 12:46:57 -0800 | [diff] [blame] | 203 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 204 | public int getDialogId() { |
| 205 | return mDialogId; |
| 206 | } |
Hung-ying Tyan | 18eb39d | 2011-01-28 16:17:27 +0800 | [diff] [blame] | 207 | |
| 208 | @Override |
| 209 | public void onDetach() { |
| 210 | super.onDetach(); |
| 211 | |
Amith Yamasani | 8875ede | 2011-01-31 12:46:57 -0800 | [diff] [blame] | 212 | // This dialog fragment could be created from non-SettingsPreferenceFragment |
| 213 | if (mParentFragment instanceof SettingsPreferenceFragment) { |
| 214 | // in case the dialog is not explicitly removed by removeDialog() |
| 215 | if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) { |
| 216 | ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null; |
| 217 | } |
Hung-ying Tyan | 18eb39d | 2011-01-28 16:17:27 +0800 | [diff] [blame] | 218 | } |
| 219 | } |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 220 | } |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 221 | |
| 222 | protected boolean hasNextButton() { |
Daisuke Miyakawa | 79c5fd9 | 2011-01-15 14:58:00 -0800 | [diff] [blame] | 223 | return ((ButtonBarHandler)getActivity()).hasNextButton(); |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | protected Button getNextButton() { |
Daisuke Miyakawa | 79c5fd9 | 2011-01-15 14:58:00 -0800 | [diff] [blame] | 227 | return ((ButtonBarHandler)getActivity()).getNextButton(); |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Daisuke Miyakawa | 6ebf861 | 2010-09-10 09:48:51 -0700 | [diff] [blame] | 230 | public void finish() { |
| 231 | getActivity().onBackPressed(); |
| 232 | } |
| 233 | |
Daisuke Miyakawa | b5647c5 | 2010-09-10 18:04:02 -0700 | [diff] [blame] | 234 | public boolean startFragment( |
| 235 | Fragment caller, String fragmentClass, int requestCode, Bundle extras) { |
Daisuke Miyakawa | 25af150 | 2010-09-24 11:29:31 -0700 | [diff] [blame] | 236 | if (getActivity() instanceof PreferenceActivity) { |
| 237 | PreferenceActivity preferenceActivity = (PreferenceActivity)getActivity(); |
Gilles Debunne | 6465054 | 2011-08-23 11:01:35 -0700 | [diff] [blame] | 238 | preferenceActivity.startPreferencePanel(fragmentClass, extras, |
| 239 | R.string.lock_settings_picker_title, null, caller, requestCode); |
Daisuke Miyakawa | 25af150 | 2010-09-24 11:29:31 -0700 | [diff] [blame] | 240 | return true; |
Daisuke Miyakawa | b5647c5 | 2010-09-10 18:04:02 -0700 | [diff] [blame] | 241 | } else { |
Daisuke Miyakawa | 25af150 | 2010-09-24 11:29:31 -0700 | [diff] [blame] | 242 | Log.w(TAG, "Parent isn't PreferenceActivity, thus there's no way to launch the " |
| 243 | + "given Fragment (name: " + fragmentClass + ", requestCode: " + requestCode |
| 244 | + ")"); |
Daisuke Miyakawa | b5647c5 | 2010-09-10 18:04:02 -0700 | [diff] [blame] | 245 | return false; |
| 246 | } |
| 247 | } |
| 248 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 249 | } |