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