blob: 7875aa6fe6771240412485b95173026c6b66ec8a [file] [log] [blame]
Amith Yamasanid7993472010-08-18 13:59:28 -07001/*
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
17package com.android.settings;
18
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -070019import android.app.Activity;
Amith Yamasanid7993472010-08-18 13:59:28 -070020import android.app.Dialog;
Amith Yamasanid7993472010-08-18 13:59:28 -070021import android.content.ContentResolver;
Amith Yamasani350938e2013-04-09 10:22:47 -070022import android.content.Context;
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +080023import android.content.DialogInterface;
Jason Monkb7e43802016-06-06 16:01:58 -040024import android.content.Intent;
Amith Yamasanid7993472010-08-18 13:59:28 -070025import android.content.pm.PackageManager;
Amith Yamasanid7993472010-08-18 13:59:28 -070026import android.os.Bundle;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070027import android.text.TextUtils;
Jason Monk2071eda2016-02-25 13:55:48 -050028import android.util.ArrayMap;
Amith Yamasanid7993472010-08-18 13:59:28 -070029import android.util.Log;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070030import android.view.LayoutInflater;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070031import android.view.View;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070032import android.view.ViewGroup;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070033import android.widget.Button;
Jason Monkb7e43802016-06-06 16:01:58 -040034
Fan Zhang23f8d592018-08-28 15:11:40 -070035import androidx.annotation.VisibleForTesting;
36import androidx.annotation.XmlRes;
37import androidx.fragment.app.DialogFragment;
38import androidx.fragment.app.Fragment;
39import androidx.preference.Preference;
40import androidx.preference.PreferenceGroup;
41import androidx.preference.PreferenceScreen;
42import androidx.recyclerview.widget.LinearLayoutManager;
43import androidx.recyclerview.widget.RecyclerView;
44
Fan Zhang2d0b3442016-12-05 17:02:33 -080045import com.android.settings.core.InstrumentedPreferenceFragment;
Fan Zhangd65184f2016-09-19 17:45:24 -070046import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
Fan Zhang681a4cd2017-11-29 16:57:19 -080047import com.android.settings.search.actionbar.SearchMenuController;
Fan Zhange0b0e9f2017-11-29 14:55:59 -080048import com.android.settings.support.actionbar.HelpMenuController;
49import com.android.settings.support.actionbar.HelpResourceProvider;
Fan Zhang3d516e72018-01-31 14:14:41 -080050import com.android.settings.widget.HighlightablePreferenceGroupAdapter;
Fan Zhang896f1b32017-06-26 14:22:45 -070051import com.android.settings.widget.LoadingViewController;
tmfang27c84de2018-06-28 11:39:05 +080052import com.android.settingslib.CustomDialogPreferenceCompat;
53import com.android.settingslib.CustomEditTextPreferenceCompat;
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -080054import com.android.settingslib.core.instrumentation.Instrumentable;
Raff Tsai966fa012019-09-25 11:19:06 +080055import com.android.settingslib.search.Indexable;
tmfangdce94bb2018-11-26 18:41:01 +080056import com.android.settingslib.widget.LayoutPreference;
John Spurlockb8e02b82015-04-15 21:15:55 -040057
Jason Monk39b46742015-09-10 15:52:51 -040058import java.util.UUID;
59
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070060/**
Amith Yamasanid7993472010-08-18 13:59:28 -070061 * Base class for Settings fragments, with some helper functions and dialog management.
62 */
Fan Zhang2d0b3442016-12-05 17:02:33 -080063public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceFragment
Fan Zhang78ea7da2018-07-02 13:44:57 -070064 implements DialogCreatable, HelpResourceProvider, Indexable {
Anna Galusza0285c802016-01-29 17:32:19 -080065
Jason Monk65bb0972015-12-17 10:39:44 -050066 private static final String TAG = "SettingsPreference";
Amith Yamasanid7993472010-08-18 13:59:28 -070067
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070068 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070069
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000070 private static final int ORDER_FIRST = -1;
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000071
Fan Zhange0b0e9f2017-11-29 14:55:59 -080072 private SettingsDialogFragment mDialogFragment;
Amith Yamasani350938e2013-04-09 10:22:47 -070073 // Cache the content resolver for async callbacks
74 private ContentResolver mContentResolver;
75
Jason Monk39b46742015-09-10 15:52:51 -040076 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070077 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040078 private RecyclerView.AdapterDataObserver mDataSetObserver =
79 new RecyclerView.AdapterDataObserver() {
Tony Mantler0b825f52016-09-27 14:48:16 -070080 @Override
81 public void onChanged() {
82 onDataSetChanged();
83 }
84
85 @Override
86 public void onItemRangeChanged(int positionStart, int itemCount) {
87 onDataSetChanged();
88 }
89
90 @Override
91 public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
92 onDataSetChanged();
93 }
94
95 @Override
96 public void onItemRangeInserted(int positionStart, int itemCount) {
97 onDataSetChanged();
98 }
99
100 @Override
101 public void onItemRangeRemoved(int positionStart, int itemCount) {
102 onDataSetChanged();
103 }
104
105 @Override
106 public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
107 onDataSetChanged();
108 }
109 };
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700110
Jason Chiucfa36db2019-06-05 17:31:31 +0800111 @VisibleForTesting
112 ViewGroup mPinnedHeaderFrameLayout;
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900113 private ViewGroup mButtonBar;
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700114
Jason Monk39b46742015-09-10 15:52:51 -0400115 private LayoutPreference mHeader;
116
Jason Monk39b46742015-09-10 15:52:51 -0400117 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -0500118 private LinearLayoutManager mLayoutManager;
Jason Monk2071eda2016-02-25 13:55:48 -0500119 private ArrayMap<String, Preference> mPreferenceCache;
Jason Monkf38fb382016-03-18 14:23:01 -0400120 private boolean mAnimationAllowed;
Jason Monk39b46742015-09-10 15:52:51 -0400121
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700122 @VisibleForTesting
123 public HighlightablePreferenceGroupAdapter mAdapter;
124 @VisibleForTesting
125 public boolean mPreferenceHighlighted = false;
126
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700127 @Override
128 public void onCreate(Bundle icicle) {
129 super.onCreate(icicle);
Fan Zhang681a4cd2017-11-29 16:57:19 -0800130 SearchMenuController.init(this /* host */);
Fan Zhange0b0e9f2017-11-29 14:55:59 -0800131 HelpMenuController.init(this /* host */);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700132
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700133 if (icicle != null) {
134 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
135 }
Fan Zhang72456a92018-02-20 11:25:56 -0800136 HighlightablePreferenceGroupAdapter.adjustInitialExpandedChildCount(this /* host */);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700137 }
138
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700139 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700140 public View onCreateView(LayoutInflater inflater, ViewGroup container,
141 Bundle savedInstanceState) {
142 final View root = super.onCreateView(inflater, container, savedInstanceState);
Fan Zhange0b0e9f2017-11-29 14:55:59 -0800143 mPinnedHeaderFrameLayout = root.findViewById(R.id.pinned_header);
144 mButtonBar = root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700145 return root;
146 }
147
Jason Monk39b46742015-09-10 15:52:51 -0400148 @Override
Jason Monk91e2f892016-02-23 15:31:09 -0500149 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
150 super.addPreferencesFromResource(preferencesResId);
151 checkAvailablePrefs(getPreferenceScreen());
152 }
153
Doris Lingfe525942018-11-27 14:58:55 -0800154 @VisibleForTesting
155 void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
Jason Monk91e2f892016-02-23 15:31:09 -0500156 if (preferenceGroup == null) return;
157 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
158 Preference pref = preferenceGroup.getPreference(i);
159 if (pref instanceof SelfAvailablePreference
160 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
Doris Lingfe525942018-11-27 14:58:55 -0800161 pref.setVisible(false);
Jason Monk91e2f892016-02-23 15:31:09 -0500162 } else if (pref instanceof PreferenceGroup) {
163 checkAvailablePrefs((PreferenceGroup) pref);
164 }
165 }
166 }
167
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900168 public ViewGroup getButtonBar() {
169 return mButtonBar;
170 }
171
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700172 public View setPinnedHeaderView(int layoutResId) {
173 final LayoutInflater inflater = getActivity().getLayoutInflater();
174 final View pinnedHeader =
175 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
176 setPinnedHeaderView(pinnedHeader);
177 return pinnedHeader;
178 }
179
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700180 public void setPinnedHeaderView(View pinnedHeader) {
181 mPinnedHeaderFrameLayout.addView(pinnedHeader);
182 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
183 }
184
Jason Chiucfa36db2019-06-05 17:31:31 +0800185 public void showPinnedHeader(boolean show) {
186 mPinnedHeaderFrameLayout.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
187 }
188
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700189 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700190 public void onSaveInstanceState(Bundle outState) {
191 super.onSaveInstanceState(outState);
192
Fan Zhang3d516e72018-01-31 14:14:41 -0800193 if (mAdapter != null) {
194 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mAdapter.isHighlightRequested());
195 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700196 }
197
198 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700199 public void onActivityCreated(Bundle savedInstanceState) {
200 super.onActivityCreated(savedInstanceState);
Johan Redestig76218e52016-04-19 08:29:30 +0200201 setHasOptionsMenu(true);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700202 }
203
204 @Override
205 public void onResume() {
206 super.onResume();
Fan Zhang3d516e72018-01-31 14:14:41 -0800207 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700208 }
209
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700210 @Override
211 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700212 registerObserverIfNeeded();
213 }
214
215 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700216 protected void onUnbindPreferences() {
217 unregisterObserverIfNeeded();
218 }
219
Jason Monkb37e2882016-01-11 14:27:20 -0500220 public void setLoading(boolean loading, boolean animate) {
Fan Zhang896f1b32017-06-26 14:22:45 -0700221 View loadingContainer = getView().findViewById(R.id.loading_container);
222 LoadingViewController.handleLoadingContainer(loadingContainer, getListView(),
223 !loading /* done */,
224 animate);
Jason Monkb37e2882016-01-11 14:27:20 -0500225 }
226
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700227 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700228 if (!mIsDataSetObserverRegistered) {
229 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400230 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700231 }
Jason Monk39b46742015-09-10 15:52:51 -0400232 mCurrentRootAdapter = getListView().getAdapter();
233 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700234 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500235 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700236 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700237 }
238
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700239 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700240 if (mIsDataSetObserverRegistered) {
241 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400242 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700243 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700244 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700245 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700246 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700247 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700248
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700249 public void highlightPreferenceIfNeeded() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800250 if (!isAdded()) {
251 return;
252 }
253 if (mAdapter != null) {
254 mAdapter.requestHighlight(getView(), getListView());
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700255 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700256 }
257
Fan Zhang72456a92018-02-20 11:25:56 -0800258 /**
259 * Returns initial expanded child count.
260 * <p/>
261 * Only override this method if the initial expanded child must be determined at run time.
262 */
263 public int getInitialExpandedChildCount() {
264 return 0;
265 }
266
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000267 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400268 highlightPreferenceIfNeeded();
269 updateEmptyView();
270 }
271
Jason Monk39b46742015-09-10 15:52:51 -0400272 public LayoutPreference getHeaderView() {
273 return mHeader;
274 }
275
Jason Monk39b46742015-09-10 15:52:51 -0400276 protected void setHeaderView(int resource) {
277 mHeader = new LayoutPreference(getPrefContext(), resource);
Yanting Yang70a8e312019-05-21 21:05:32 +0800278 mHeader.setSelectable(false);
Udam Sainid553abc2016-02-16 17:54:13 -0800279 addPreferenceToTop(mHeader);
280 }
281
282 protected void setHeaderView(View view) {
283 mHeader = new LayoutPreference(getPrefContext(), view);
Yanting Yang70a8e312019-05-21 21:05:32 +0800284 mHeader.setSelectable(false);
Udam Sainid553abc2016-02-16 17:54:13 -0800285 addPreferenceToTop(mHeader);
286 }
287
288 private void addPreferenceToTop(LayoutPreference preference) {
289 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400290 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800291 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400292 }
293 }
294
Jason Monk39b46742015-09-10 15:52:51 -0400295 @Override
296 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400297 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400298 // Without ids generated, the RecyclerView won't animate changes to the preferences.
299 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
300 }
Jason Monk39b46742015-09-10 15:52:51 -0400301 super.setPreferenceScreen(preferenceScreen);
302 if (preferenceScreen != null) {
303 if (mHeader != null) {
304 preferenceScreen.addPreference(mHeader);
305 }
Jason Monk39b46742015-09-10 15:52:51 -0400306 }
307 }
308
jackqdyulei2b2abac2017-05-26 10:47:55 -0700309 @VisibleForTesting
310 void updateEmptyView() {
Jason Monk39b46742015-09-10 15:52:51 -0400311 if (mEmptyView == null) return;
312 if (getPreferenceScreen() != null) {
jackqdyulei2b2abac2017-05-26 10:47:55 -0700313 final View listContainer = getActivity().findViewById(android.R.id.list_container);
Jason Monk39b46742015-09-10 15:52:51 -0400314 boolean show = (getPreferenceScreen().getPreferenceCount()
Sunny Shaobf434972019-08-29 14:28:13 +0800315 - (mHeader != null ? 1 : 0)) <= 0
jackqdyulei2b2abac2017-05-26 10:47:55 -0700316 || (listContainer != null && listContainer.getVisibility() != View.VISIBLE);
Jason Monk39b46742015-09-10 15:52:51 -0400317 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
318 } else {
319 mEmptyView.setVisibility(View.VISIBLE);
320 }
321 }
322
323 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000324 if (mEmptyView != null) {
325 mEmptyView.setVisibility(View.GONE);
326 }
Jason Monk39b46742015-09-10 15:52:51 -0400327 mEmptyView = v;
328 updateEmptyView();
329 }
330
331 public View getEmptyView() {
332 return mEmptyView;
333 }
334
Jason Monk65bb0972015-12-17 10:39:44 -0500335 @Override
336 public RecyclerView.LayoutManager onCreateLayoutManager() {
337 mLayoutManager = new LinearLayoutManager(getContext());
338 return mLayoutManager;
339 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700340
Jason Monk65bb0972015-12-17 10:39:44 -0500341 @Override
342 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
Fan Zhanga1000462018-02-02 12:15:37 -0800343 final Bundle arguments = getArguments();
Fan Zhang3d516e72018-01-31 14:14:41 -0800344 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen,
Fan Zhanga1000462018-02-02 12:15:37 -0800345 arguments == null
346 ? null : arguments.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY),
Fan Zhang3d516e72018-01-31 14:14:41 -0800347 mPreferenceHighlighted);
Jason Monk65bb0972015-12-17 10:39:44 -0500348 return mAdapter;
349 }
350
Jason Monkf38fb382016-03-18 14:23:01 -0400351 protected void setAnimationAllowed(boolean animationAllowed) {
352 mAnimationAllowed = animationAllowed;
353 }
354
Jason Monk2071eda2016-02-25 13:55:48 -0500355 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
Fan Zhang3d516e72018-01-31 14:14:41 -0800356 mPreferenceCache = new ArrayMap<>();
Jason Monk2071eda2016-02-25 13:55:48 -0500357 final int N = group.getPreferenceCount();
358 for (int i = 0; i < N; i++) {
359 Preference p = group.getPreference(i);
360 if (TextUtils.isEmpty(p.getKey())) {
361 continue;
362 }
363 mPreferenceCache.put(p.getKey(), p);
364 }
365 }
366
367 protected Preference getCachedPreference(String key) {
368 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
369 }
370
371 protected void removeCachedPrefs(PreferenceGroup group) {
372 for (Preference p : mPreferenceCache.values()) {
373 group.removePreference(p);
374 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400375 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500376 }
377
Jason Monka6278442016-04-21 10:12:30 -0400378 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400379 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400380 }
381
Jan Nordqvist9eb43dd2018-03-26 15:29:44 -0700382 @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
383 public boolean removePreference(String key) {
Fan Zhange84407f2017-05-24 11:19:52 -0700384 return removePreference(getPreferenceScreen(), key);
385 }
386
387 @VisibleForTesting
388 boolean removePreference(PreferenceGroup group, String key) {
389 final int preferenceCount = group.getPreferenceCount();
390 for (int i = 0; i < preferenceCount; i++) {
391 final Preference preference = group.getPreference(i);
392 final String curKey = preference.getKey();
393
394 if (TextUtils.equals(curKey, key)) {
395 return group.removePreference(preference);
396 }
397
398 if (preference instanceof PreferenceGroup) {
399 if (removePreference((PreferenceGroup) preference, key)) {
400 return true;
401 }
402 }
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700403 }
Fan Zhange84407f2017-05-24 11:19:52 -0700404 return false;
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700405 }
406
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700407 /*
408 * The name is intentionally made different from Activity#finish(), so that
409 * users won't misunderstand its meaning.
410 */
411 public final void finishFragment() {
412 getActivity().onBackPressed();
413 }
414
Amith Yamasanid7993472010-08-18 13:59:28 -0700415 // Some helpers for functions used by the settings fragments when they were activities
416
417 /**
418 * Returns the ContentResolver from the owning Activity.
419 */
420 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700421 Context context = getActivity();
422 if (context != null) {
423 mContentResolver = context.getContentResolver();
424 }
425 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700426 }
427
428 /**
429 * Returns the specified system service from the owning Activity.
430 */
431 protected Object getSystemService(final String name) {
432 return getActivity().getSystemService(name);
433 }
434
435 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700436 * Returns the PackageManager from the owning Activity.
437 */
438 protected PackageManager getPackageManager() {
439 return getActivity().getPackageManager();
440 }
441
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800442 @Override
443 public void onDetach() {
444 if (isRemoving()) {
445 if (mDialogFragment != null) {
446 mDialogFragment.dismiss();
447 mDialogFragment = null;
448 }
449 }
450 super.onDetach();
451 }
452
Amith Yamasanid7993472010-08-18 13:59:28 -0700453 // Dialog management
454
455 protected void showDialog(int dialogId) {
456 if (mDialogFragment != null) {
457 Log.e(TAG, "Old dialog fragment not null!");
458 }
tmfangd5405cf2018-10-05 18:45:07 +0800459 mDialogFragment = SettingsDialogFragment.newInstance(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800460 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700461 }
462
Fan Zhangd65184f2016-09-19 17:45:24 -0700463 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700464 public Dialog onCreateDialog(int dialogId) {
465 return null;
466 }
467
Fan Zhangd65184f2016-09-19 17:45:24 -0700468 @Override
469 public int getDialogMetricsCategory(int dialogId) {
470 return 0;
471 }
472
Amith Yamasanid7993472010-08-18 13:59:28 -0700473 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800474 // mDialogFragment may not be visible yet in parent fragment's onResume().
475 // To be able to dismiss dialog at that time, don't check
476 // mDialogFragment.isVisible().
477 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400478 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700479 }
480 mDialogFragment = null;
481 }
482
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800483 /**
484 * Sets the OnCancelListener of the dialog shown. This method can only be
485 * called after showDialog(int) and before removeDialog(int). The method
486 * does nothing otherwise.
487 */
488 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
489 if (mDialogFragment != null) {
490 mDialogFragment.mOnCancelListener = listener;
491 }
492 }
493
494 /**
495 * Sets the OnDismissListener of the dialog shown. This method can only be
496 * called after showDialog(int) and before removeDialog(int). The method
497 * does nothing otherwise.
498 */
499 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
500 if (mDialogFragment != null) {
501 mDialogFragment.mOnDismissListener = listener;
502 }
503 }
504
Amith Yamasanic861cf82012-10-02 14:51:46 -0700505 public void onDialogShowing() {
506 // override in subclass to attach a dismiss listener, for instance
507 }
508
Jason Monk39b46742015-09-10 15:52:51 -0400509 @Override
510 public void onDisplayPreferenceDialog(Preference preference) {
511 if (preference.getKey() == null) {
512 // Auto-key preferences that don't have a key, so the dialog can find them.
513 preference.setKey(UUID.randomUUID().toString());
514 }
515 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000516 if (preference instanceof RestrictedListPreference) {
517 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
518 .newInstance(preference.getKey());
519 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400520 f = CustomListPreference.CustomListPreferenceDialogFragment
521 .newInstance(preference.getKey());
tmfang27c84de2018-06-28 11:39:05 +0800522 } else if (preference instanceof CustomDialogPreferenceCompat) {
523 f = CustomDialogPreferenceCompat.CustomPreferenceDialogFragment
Jason Monk39b46742015-09-10 15:52:51 -0400524 .newInstance(preference.getKey());
tmfang27c84de2018-06-28 11:39:05 +0800525 } else if (preference instanceof CustomEditTextPreferenceCompat) {
526 f = CustomEditTextPreferenceCompat.CustomPreferenceDialogFragment
Jason Monk39b46742015-09-10 15:52:51 -0400527 .newInstance(preference.getKey());
528 } else {
529 super.onDisplayPreferenceDialog(preference);
530 return;
531 }
532 f.setTargetFragment(this, 0);
533 f.show(getFragmentManager(), "dialog_preference");
534 onDialogShowing();
535 }
536
Fan Zhangd65184f2016-09-19 17:45:24 -0700537 public static class SettingsDialogFragment extends InstrumentedDialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800538 private static final String KEY_DIALOG_ID = "key_dialog_id";
539 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
540
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800541 private Fragment mParentFragment;
542
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800543 private DialogInterface.OnCancelListener mOnCancelListener;
544 private DialogInterface.OnDismissListener mOnDismissListener;
545
tmfangd5405cf2018-10-05 18:45:07 +0800546 public static SettingsDialogFragment newInstance(DialogCreatable fragment, int dialogId) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800547 if (!(fragment instanceof Fragment)) {
548 throw new IllegalArgumentException("fragment argument must be an instance of "
549 + Fragment.class.getName());
550 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800551
tmfangd5405cf2018-10-05 18:45:07 +0800552 final SettingsDialogFragment settingsDialogFragment = new SettingsDialogFragment();
553 settingsDialogFragment.setParentFragment(fragment);
554 settingsDialogFragment.setDialogId(dialogId);
555
556 return settingsDialogFragment;
557 }
Fan Zhangd65184f2016-09-19 17:45:24 -0700558
559 @Override
560 public int getMetricsCategory() {
tmfangd5405cf2018-10-05 18:45:07 +0800561 if (mParentFragment == null) {
Fan Zhang4fe7c082016-10-03 13:48:55 -0700562 return Instrumentable.METRICS_CATEGORY_UNKNOWN;
563 }
tmfangd5405cf2018-10-05 18:45:07 +0800564 final int metricsCategory =
565 ((DialogCreatable) mParentFragment).getDialogMetricsCategory(mDialogId);
Fan Zhangd65184f2016-09-19 17:45:24 -0700566 if (metricsCategory <= 0) {
567 throw new IllegalStateException("Dialog must provide a metrics category");
568 }
569 return metricsCategory;
570 }
571
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800572 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800573 public void onSaveInstanceState(Bundle outState) {
574 super.onSaveInstanceState(outState);
575 if (mParentFragment != null) {
576 outState.putInt(KEY_DIALOG_ID, mDialogId);
577 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
578 }
579 }
580
581 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700582 public void onStart() {
583 super.onStart();
584
585 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
586 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
587 }
588 }
589
590 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800591 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800592 if (savedInstanceState != null) {
593 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800594 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800595 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700596 if (mParentFragment == null) {
597 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
598 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800599 if (!(mParentFragment instanceof DialogCreatable)) {
600 throw new IllegalArgumentException(
601 (mParentFragment != null
602 ? mParentFragment.getClass().getName()
603 : mParentFragmentId)
604 + " must implement "
605 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800606 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800607 // This dialog fragment could be created from non-SettingsPreferenceFragment
608 if (mParentFragment instanceof SettingsPreferenceFragment) {
609 // restore mDialogFragment in mParentFragment
610 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
611 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800612 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800613 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700614 }
615
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800616 @Override
617 public void onCancel(DialogInterface dialog) {
618 super.onCancel(dialog);
619 if (mOnCancelListener != null) {
620 mOnCancelListener.onCancel(dialog);
621 }
622 }
623
624 @Override
625 public void onDismiss(DialogInterface dialog) {
626 super.onDismiss(dialog);
627 if (mOnDismissListener != null) {
628 mOnDismissListener.onDismiss(dialog);
629 }
630 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800631
Amith Yamasanid7993472010-08-18 13:59:28 -0700632 public int getDialogId() {
633 return mDialogId;
634 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800635
636 @Override
637 public void onDetach() {
638 super.onDetach();
639
Amith Yamasani8875ede2011-01-31 12:46:57 -0800640 // This dialog fragment could be created from non-SettingsPreferenceFragment
641 if (mParentFragment instanceof SettingsPreferenceFragment) {
642 // in case the dialog is not explicitly removed by removeDialog()
643 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
644 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
645 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800646 }
647 }
tmfangd5405cf2018-10-05 18:45:07 +0800648
649 private void setParentFragment(DialogCreatable fragment) {
650 mParentFragment = (Fragment) fragment;
651 }
652
653 private void setDialogId(int dialogId) {
654 mDialogId = dialogId;
655 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700656 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700657
658 protected boolean hasNextButton() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800659 return ((ButtonBarHandler) getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700660 }
661
662 protected Button getNextButton() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800663 return ((ButtonBarHandler) getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700664 }
665
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700666 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700667 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400668 if (activity == null) return;
669 if (getFragmentManager().getBackStackEntryCount() > 0) {
670 getFragmentManager().popBackStack();
671 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800672 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700673 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700674 }
675
Jason Monkb7e43802016-06-06 16:01:58 -0400676 protected Intent getIntent() {
677 if (getActivity() == null) {
678 return null;
679 }
680 return getActivity().getIntent();
681 }
682
683 protected void setResult(int result, Intent intent) {
684 if (getActivity() == null) {
685 return;
686 }
687 getActivity().setResult(result, intent);
688 }
689
690 protected void setResult(int result) {
691 if (getActivity() == null) {
692 return;
693 }
694 getActivity().setResult(result);
695 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700696}