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 | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 25 | import android.content.pm.PackageManager; |
Fabrice Di Meglio | 6602d02 | 2014-04-15 16:45:20 -0700 | [diff] [blame^] | 26 | import android.content.res.TypedArray; |
| 27 | import android.graphics.drawable.Drawable; |
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; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 30 | import android.preference.PreferenceFragment; |
Fabrice Di Meglio | c145732 | 2014-04-04 19:07:50 -0700 | [diff] [blame] | 31 | import android.preference.PreferenceGroupAdapter; |
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; |
Fabrice Di Meglio | c145732 | 2014-04-04 19:07:50 -0700 | [diff] [blame] | 38 | import android.widget.ListAdapter; |
Fabrice Di Meglio | 6602d02 | 2014-04-15 16:45:20 -0700 | [diff] [blame^] | 39 | import android.widget.ListView; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 40 | |
Daisuke Miyakawa | f58090d | 2010-09-12 17:27:33 -0700 | [diff] [blame] | 41 | /** |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 42 | * Base class for Settings fragments, with some helper functions and dialog management. |
| 43 | */ |
Gilles Debunne | 6465054 | 2011-08-23 11:01:35 -0700 | [diff] [blame] | 44 | public class SettingsPreferenceFragment extends PreferenceFragment implements DialogCreatable { |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 45 | |
| 46 | private static final String TAG = "SettingsPreferenceFragment"; |
| 47 | |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 48 | private static final int MENU_HELP = Menu.FIRST + 100; |
Fabrice Di Meglio | 6602d02 | 2014-04-15 16:45:20 -0700 | [diff] [blame^] | 49 | private static final int HIGHLIGHT_DURATION_MILLIS = 300; |
| 50 | |
| 51 | private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted"; |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 52 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 53 | private SettingsDialogFragment mDialogFragment; |
| 54 | |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 55 | private String mHelpUrl; |
| 56 | |
Amith Yamasani | 350938e | 2013-04-09 10:22:47 -0700 | [diff] [blame] | 57 | // Cache the content resolver for async callbacks |
| 58 | private ContentResolver mContentResolver; |
| 59 | |
Fabrice Di Meglio | 6602d02 | 2014-04-15 16:45:20 -0700 | [diff] [blame^] | 60 | private boolean mPreferenceHighlighted = false; |
| 61 | |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 62 | @Override |
| 63 | public void onCreate(Bundle icicle) { |
| 64 | super.onCreate(icicle); |
| 65 | |
Fabrice Di Meglio | 6602d02 | 2014-04-15 16:45:20 -0700 | [diff] [blame^] | 66 | if (icicle != null) { |
| 67 | mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY); |
| 68 | } |
| 69 | |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 70 | // Prepare help url and enable menu if necessary |
| 71 | int helpResource = getHelpResource(); |
| 72 | if (helpResource != 0) { |
| 73 | mHelpUrl = getResources().getString(helpResource); |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
Daisuke Miyakawa | b5647c5 | 2010-09-10 18:04:02 -0700 | [diff] [blame] | 77 | @Override |
Fabrice Di Meglio | 6602d02 | 2014-04-15 16:45:20 -0700 | [diff] [blame^] | 78 | public void onSaveInstanceState(Bundle outState) { |
| 79 | super.onSaveInstanceState(outState); |
| 80 | |
| 81 | outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted); |
| 82 | } |
| 83 | |
| 84 | @Override |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 85 | public void onActivityCreated(Bundle savedInstanceState) { |
| 86 | super.onActivityCreated(savedInstanceState); |
Amith Yamasani | b3a593e | 2012-04-23 18:03:52 -0700 | [diff] [blame] | 87 | if (!TextUtils.isEmpty(mHelpUrl)) { |
| 88 | setHasOptionsMenu(true); |
| 89 | } |
Fabrice Di Meglio | c145732 | 2014-04-04 19:07:50 -0700 | [diff] [blame] | 90 | |
| 91 | final Bundle args = getArguments(); |
| 92 | if (args != null) { |
| 93 | final String key = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY); |
Fabrice Di Meglio | 6602d02 | 2014-04-15 16:45:20 -0700 | [diff] [blame^] | 94 | if (hasListView() && !TextUtils.isEmpty(key)) { |
| 95 | highlightPreference(key); |
Fabrice Di Meglio | c145732 | 2014-04-04 19:07:50 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
Fabrice Di Meglio | 6602d02 | 2014-04-15 16:45:20 -0700 | [diff] [blame^] | 100 | private void highlightPreference(String key) { |
| 101 | final int position = findPositionFromKey(key); |
| 102 | if (position >= 0) { |
| 103 | final ListView listView = getListView(); |
| 104 | final ListAdapter adapter = listView.getAdapter(); |
| 105 | |
| 106 | if (adapter instanceof PreferenceGroupAdapter) { |
| 107 | final Drawable drawable = getHighlightDrawable(); |
| 108 | ((PreferenceGroupAdapter) adapter).setHighlightedDrawable(drawable); |
| 109 | ((PreferenceGroupAdapter) adapter).setHighlighted(position); |
| 110 | |
| 111 | listView.post(new Runnable() { |
| 112 | @Override |
| 113 | public void run() { |
| 114 | listView.setSelection(position); |
| 115 | if (!mPreferenceHighlighted) { |
| 116 | listView.postDelayed(new Runnable() { |
| 117 | @Override |
| 118 | public void run() { |
| 119 | final int centerX = listView.getWidth() / 2; |
| 120 | final int centerY = listView.getChildAt(0).getHeight() / 2; |
| 121 | drawable.setHotspot(0, centerX, centerY); |
| 122 | drawable.clearHotspots(); |
| 123 | ((PreferenceGroupAdapter) adapter).setHighlighted(-1); |
| 124 | } |
| 125 | }, HIGHLIGHT_DURATION_MILLIS); |
| 126 | |
| 127 | mPreferenceHighlighted = true; |
| 128 | } |
| 129 | } |
| 130 | }); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | } |
| 135 | |
| 136 | private Drawable getHighlightDrawable() { |
| 137 | final int[] attrs = new int[] { android.R.attr.selectableItemBackground }; |
| 138 | TypedArray ta = getActivity().getTheme().obtainStyledAttributes(attrs); |
| 139 | return ta.getDrawable(0); |
| 140 | } |
| 141 | |
| 142 | private int findPositionFromKey(String key) { |
| 143 | final ListAdapter adapter = getListView().getAdapter(); |
| 144 | final int count = adapter.getCount(); |
| 145 | for (int n = 0; n < count; n++) { |
| 146 | Object item = adapter.getItem(n); |
| 147 | if (item instanceof Preference) { |
| 148 | Preference preference = (Preference) item; |
Fabrice Di Meglio | c145732 | 2014-04-04 19:07:50 -0700 | [diff] [blame] | 149 | final String preferenceKey = preference.getKey(); |
| 150 | if (preferenceKey != null && preferenceKey.equals(key)) { |
| 151 | return n; |
| 152 | } |
Fabrice Di Meglio | c145732 | 2014-04-04 19:07:50 -0700 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | return -1; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 158 | protected void removePreference(String key) { |
| 159 | Preference pref = findPreference(key); |
| 160 | if (pref != null) { |
| 161 | getPreferenceScreen().removePreference(pref); |
| 162 | } |
| 163 | } |
| 164 | |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 165 | /** |
| 166 | * Override this if you want to show a help item in the menu, by returning the resource id. |
| 167 | * @return the resource id for the help url |
| 168 | */ |
| 169 | protected int getHelpResource() { |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
Amith Yamasani | aeb57ed | 2012-12-06 14:40:51 -0800 | [diff] [blame] | 175 | if (mHelpUrl != null && getActivity() != null) { |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 176 | MenuItem helpItem = menu.add(0, MENU_HELP, 0, R.string.help_label); |
Amith Yamasani | aeb57ed | 2012-12-06 14:40:51 -0800 | [diff] [blame] | 177 | HelpUtils.prepareHelpMenuItem(getActivity(), helpItem, mHelpUrl); |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
Daisuke Miyakawa | b5647c5 | 2010-09-10 18:04:02 -0700 | [diff] [blame] | 181 | /* |
| 182 | * The name is intentionally made different from Activity#finish(), so that |
| 183 | * users won't misunderstand its meaning. |
| 184 | */ |
| 185 | public final void finishFragment() { |
| 186 | getActivity().onBackPressed(); |
| 187 | } |
| 188 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 189 | // Some helpers for functions used by the settings fragments when they were activities |
| 190 | |
| 191 | /** |
| 192 | * Returns the ContentResolver from the owning Activity. |
| 193 | */ |
| 194 | protected ContentResolver getContentResolver() { |
Amith Yamasani | 350938e | 2013-04-09 10:22:47 -0700 | [diff] [blame] | 195 | Context context = getActivity(); |
| 196 | if (context != null) { |
| 197 | mContentResolver = context.getContentResolver(); |
| 198 | } |
| 199 | return mContentResolver; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Returns the specified system service from the owning Activity. |
| 204 | */ |
| 205 | protected Object getSystemService(final String name) { |
| 206 | return getActivity().getSystemService(name); |
| 207 | } |
| 208 | |
| 209 | /** |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 210 | * Returns the PackageManager from the owning Activity. |
| 211 | */ |
| 212 | protected PackageManager getPackageManager() { |
| 213 | return getActivity().getPackageManager(); |
| 214 | } |
| 215 | |
Dianne Hackborn | 0385cf1 | 2011-01-24 16:22:13 -0800 | [diff] [blame] | 216 | @Override |
| 217 | public void onDetach() { |
| 218 | if (isRemoving()) { |
| 219 | if (mDialogFragment != null) { |
| 220 | mDialogFragment.dismiss(); |
| 221 | mDialogFragment = null; |
| 222 | } |
| 223 | } |
| 224 | super.onDetach(); |
| 225 | } |
| 226 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 227 | // Dialog management |
| 228 | |
| 229 | protected void showDialog(int dialogId) { |
| 230 | if (mDialogFragment != null) { |
| 231 | Log.e(TAG, "Old dialog fragment not null!"); |
| 232 | } |
| 233 | mDialogFragment = new SettingsDialogFragment(this, dialogId); |
Fabrice Di Meglio | 377dd62 | 2014-02-12 20:05:57 -0800 | [diff] [blame] | 234 | mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId)); |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | public Dialog onCreateDialog(int dialogId) { |
| 238 | return null; |
| 239 | } |
| 240 | |
| 241 | protected void removeDialog(int dialogId) { |
Hung-ying Tyan | adc83d8 | 2011-01-24 15:05:27 +0800 | [diff] [blame] | 242 | // mDialogFragment may not be visible yet in parent fragment's onResume(). |
| 243 | // To be able to dismiss dialog at that time, don't check |
| 244 | // mDialogFragment.isVisible(). |
| 245 | if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) { |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 246 | mDialogFragment.dismiss(); |
| 247 | } |
| 248 | mDialogFragment = null; |
| 249 | } |
| 250 | |
Hung-ying Tyan | 0ee51e0 | 2011-01-25 16:42:14 +0800 | [diff] [blame] | 251 | /** |
| 252 | * Sets the OnCancelListener of the dialog shown. This method can only be |
| 253 | * called after showDialog(int) and before removeDialog(int). The method |
| 254 | * does nothing otherwise. |
| 255 | */ |
| 256 | protected void setOnCancelListener(DialogInterface.OnCancelListener listener) { |
| 257 | if (mDialogFragment != null) { |
| 258 | mDialogFragment.mOnCancelListener = listener; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Sets the OnDismissListener of the dialog shown. This method can only be |
| 264 | * called after showDialog(int) and before removeDialog(int). The method |
| 265 | * does nothing otherwise. |
| 266 | */ |
| 267 | protected void setOnDismissListener(DialogInterface.OnDismissListener listener) { |
| 268 | if (mDialogFragment != null) { |
| 269 | mDialogFragment.mOnDismissListener = listener; |
| 270 | } |
| 271 | } |
| 272 | |
Amith Yamasani | c861cf8 | 2012-10-02 14:51:46 -0700 | [diff] [blame] | 273 | public void onDialogShowing() { |
| 274 | // override in subclass to attach a dismiss listener, for instance |
| 275 | } |
| 276 | |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 277 | public static class SettingsDialogFragment extends DialogFragment { |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 278 | private static final String KEY_DIALOG_ID = "key_dialog_id"; |
| 279 | private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id"; |
| 280 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 281 | private int mDialogId; |
| 282 | |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 283 | private Fragment mParentFragment; |
| 284 | |
Hung-ying Tyan | 0ee51e0 | 2011-01-25 16:42:14 +0800 | [diff] [blame] | 285 | private DialogInterface.OnCancelListener mOnCancelListener; |
| 286 | private DialogInterface.OnDismissListener mOnDismissListener; |
| 287 | |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 288 | public SettingsDialogFragment() { |
| 289 | /* do nothing */ |
| 290 | } |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 291 | |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 292 | public SettingsDialogFragment(DialogCreatable fragment, int dialogId) { |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 293 | mDialogId = dialogId; |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 294 | if (!(fragment instanceof Fragment)) { |
| 295 | throw new IllegalArgumentException("fragment argument must be an instance of " |
| 296 | + Fragment.class.getName()); |
| 297 | } |
| 298 | mParentFragment = (Fragment) fragment; |
| 299 | } |
| 300 | |
| 301 | @Override |
Dianne Hackborn | 300768f | 2011-01-27 20:39:21 -0800 | [diff] [blame] | 302 | public void onSaveInstanceState(Bundle outState) { |
| 303 | super.onSaveInstanceState(outState); |
| 304 | if (mParentFragment != null) { |
| 305 | outState.putInt(KEY_DIALOG_ID, mDialogId); |
| 306 | outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId()); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | @Override |
Amith Yamasani | c861cf8 | 2012-10-02 14:51:46 -0700 | [diff] [blame] | 311 | public void onStart() { |
| 312 | super.onStart(); |
| 313 | |
| 314 | if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) { |
| 315 | ((SettingsPreferenceFragment) mParentFragment).onDialogShowing(); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | @Override |
Dianne Hackborn | 300768f | 2011-01-27 20:39:21 -0800 | [diff] [blame] | 320 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 321 | if (savedInstanceState != null) { |
| 322 | mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0); |
Fabrice Di Meglio | 377dd62 | 2014-02-12 20:05:57 -0800 | [diff] [blame] | 323 | mParentFragment = getParentFragment(); |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 324 | int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1); |
Fabrice Di Meglio | 377dd62 | 2014-02-12 20:05:57 -0800 | [diff] [blame] | 325 | if (!(mParentFragment instanceof DialogCreatable)) { |
| 326 | throw new IllegalArgumentException( |
| 327 | (mParentFragment != null |
| 328 | ? mParentFragment.getClass().getName() |
| 329 | : mParentFragmentId) |
| 330 | + " must implement " |
| 331 | + DialogCreatable.class.getName()); |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 332 | } |
Amith Yamasani | 8875ede | 2011-01-31 12:46:57 -0800 | [diff] [blame] | 333 | // This dialog fragment could be created from non-SettingsPreferenceFragment |
| 334 | if (mParentFragment instanceof SettingsPreferenceFragment) { |
| 335 | // restore mDialogFragment in mParentFragment |
| 336 | ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this; |
| 337 | } |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 338 | } |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 339 | return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId); |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Hung-ying Tyan | 0ee51e0 | 2011-01-25 16:42:14 +0800 | [diff] [blame] | 342 | @Override |
| 343 | public void onCancel(DialogInterface dialog) { |
| 344 | super.onCancel(dialog); |
| 345 | if (mOnCancelListener != null) { |
| 346 | mOnCancelListener.onCancel(dialog); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | @Override |
| 351 | public void onDismiss(DialogInterface dialog) { |
| 352 | super.onDismiss(dialog); |
| 353 | if (mOnDismissListener != null) { |
| 354 | mOnDismissListener.onDismiss(dialog); |
| 355 | } |
| 356 | } |
Amith Yamasani | 8875ede | 2011-01-31 12:46:57 -0800 | [diff] [blame] | 357 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 358 | public int getDialogId() { |
| 359 | return mDialogId; |
| 360 | } |
Hung-ying Tyan | 18eb39d | 2011-01-28 16:17:27 +0800 | [diff] [blame] | 361 | |
| 362 | @Override |
| 363 | public void onDetach() { |
| 364 | super.onDetach(); |
| 365 | |
Amith Yamasani | 8875ede | 2011-01-31 12:46:57 -0800 | [diff] [blame] | 366 | // This dialog fragment could be created from non-SettingsPreferenceFragment |
| 367 | if (mParentFragment instanceof SettingsPreferenceFragment) { |
| 368 | // in case the dialog is not explicitly removed by removeDialog() |
| 369 | if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) { |
| 370 | ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null; |
| 371 | } |
Hung-ying Tyan | 18eb39d | 2011-01-28 16:17:27 +0800 | [diff] [blame] | 372 | } |
| 373 | } |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 374 | } |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 375 | |
| 376 | protected boolean hasNextButton() { |
Daisuke Miyakawa | 79c5fd9 | 2011-01-15 14:58:00 -0800 | [diff] [blame] | 377 | return ((ButtonBarHandler)getActivity()).hasNextButton(); |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | protected Button getNextButton() { |
Daisuke Miyakawa | 79c5fd9 | 2011-01-15 14:58:00 -0800 | [diff] [blame] | 381 | return ((ButtonBarHandler)getActivity()).getNextButton(); |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Daisuke Miyakawa | 6ebf861 | 2010-09-10 09:48:51 -0700 | [diff] [blame] | 384 | public void finish() { |
| 385 | getActivity().onBackPressed(); |
| 386 | } |
| 387 | |
Daisuke Miyakawa | b5647c5 | 2010-09-10 18:04:02 -0700 | [diff] [blame] | 388 | public boolean startFragment( |
| 389 | Fragment caller, String fragmentClass, int requestCode, Bundle extras) { |
Fabrice Di Meglio | 263bcc8 | 2014-01-17 19:17:58 -0800 | [diff] [blame] | 390 | if (getActivity() instanceof SettingsActivity) { |
| 391 | SettingsActivity sa = (SettingsActivity) getActivity(); |
| 392 | sa.startPreferencePanel(fragmentClass, extras, |
Gilles Debunne | 6465054 | 2011-08-23 11:01:35 -0700 | [diff] [blame] | 393 | R.string.lock_settings_picker_title, null, caller, requestCode); |
Daisuke Miyakawa | 25af150 | 2010-09-24 11:29:31 -0700 | [diff] [blame] | 394 | return true; |
Daisuke Miyakawa | b5647c5 | 2010-09-10 18:04:02 -0700 | [diff] [blame] | 395 | } else { |
Fabrice Di Meglio | 263bcc8 | 2014-01-17 19:17:58 -0800 | [diff] [blame] | 396 | Log.w(TAG, "Parent isn't Settings activity, thus there's no way to launch the " |
Daisuke Miyakawa | 25af150 | 2010-09-24 11:29:31 -0700 | [diff] [blame] | 397 | + "given Fragment (name: " + fragmentClass + ", requestCode: " + requestCode |
| 398 | + ")"); |
Daisuke Miyakawa | b5647c5 | 2010-09-10 18:04:02 -0700 | [diff] [blame] | 399 | return false; |
| 400 | } |
| 401 | } |
| 402 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 403 | } |