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