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