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