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