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