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