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