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