blob: 2c628f6f375777de0d14fb7a0d256054737cfc59 [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;
Mill Chen14cfd2f2021-01-06 07:46:46 +080029import android.util.FeatureFlagUtils;
Amith Yamasanid7993472010-08-18 13:59:28 -070030import android.util.Log;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070031import android.view.LayoutInflater;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070032import android.view.View;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070033import android.view.ViewGroup;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070034import android.widget.Button;
Jason Monkb7e43802016-06-06 16:01:58 -040035
Fan Zhang23f8d592018-08-28 15:11:40 -070036import androidx.annotation.VisibleForTesting;
37import androidx.annotation.XmlRes;
38import androidx.fragment.app.DialogFragment;
39import androidx.fragment.app.Fragment;
40import androidx.preference.Preference;
41import androidx.preference.PreferenceGroup;
42import androidx.preference.PreferenceScreen;
43import androidx.recyclerview.widget.LinearLayoutManager;
44import androidx.recyclerview.widget.RecyclerView;
45
Mill Chen14cfd2f2021-01-06 07:46:46 +080046import com.android.settings.core.FeatureFlags;
Fan Zhang2d0b3442016-12-05 17:02:33 -080047import com.android.settings.core.InstrumentedPreferenceFragment;
Fan Zhangd65184f2016-09-19 17:45:24 -070048import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
Fan Zhang681a4cd2017-11-29 16:57:19 -080049import com.android.settings.search.actionbar.SearchMenuController;
Fan Zhange0b0e9f2017-11-29 14:55:59 -080050import com.android.settings.support.actionbar.HelpMenuController;
51import com.android.settings.support.actionbar.HelpResourceProvider;
Fan Zhang3d516e72018-01-31 14:14:41 -080052import com.android.settings.widget.HighlightablePreferenceGroupAdapter;
Fan Zhang896f1b32017-06-26 14:22:45 -070053import com.android.settings.widget.LoadingViewController;
tmfang27c84de2018-06-28 11:39:05 +080054import com.android.settingslib.CustomDialogPreferenceCompat;
55import com.android.settingslib.CustomEditTextPreferenceCompat;
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -080056import com.android.settingslib.core.instrumentation.Instrumentable;
Raff Tsai966fa012019-09-25 11:19:06 +080057import com.android.settingslib.search.Indexable;
tmfangdce94bb2018-11-26 18:41:01 +080058import com.android.settingslib.widget.LayoutPreference;
John Spurlockb8e02b82015-04-15 21:15:55 -040059
Jason Monk39b46742015-09-10 15:52:51 -040060import java.util.UUID;
61
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070062/**
Amith Yamasanid7993472010-08-18 13:59:28 -070063 * Base class for Settings fragments, with some helper functions and dialog management.
64 */
Fan Zhang2d0b3442016-12-05 17:02:33 -080065public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceFragment
Fan Zhang78ea7da2018-07-02 13:44:57 -070066 implements DialogCreatable, HelpResourceProvider, Indexable {
Anna Galusza0285c802016-01-29 17:32:19 -080067
Jason Monk65bb0972015-12-17 10:39:44 -050068 private static final String TAG = "SettingsPreference";
Amith Yamasanid7993472010-08-18 13:59:28 -070069
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070070 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070071
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000072 private static final int ORDER_FIRST = -1;
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000073
Fan Zhange0b0e9f2017-11-29 14:55:59 -080074 private SettingsDialogFragment mDialogFragment;
Amith Yamasani350938e2013-04-09 10:22:47 -070075 // Cache the content resolver for async callbacks
76 private ContentResolver mContentResolver;
77
Jason Monk39b46742015-09-10 15:52:51 -040078 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070079 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040080 private RecyclerView.AdapterDataObserver mDataSetObserver =
81 new RecyclerView.AdapterDataObserver() {
Tony Mantler0b825f52016-09-27 14:48:16 -070082 @Override
83 public void onChanged() {
84 onDataSetChanged();
85 }
86
87 @Override
88 public void onItemRangeChanged(int positionStart, int itemCount) {
89 onDataSetChanged();
90 }
91
92 @Override
93 public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
94 onDataSetChanged();
95 }
96
97 @Override
98 public void onItemRangeInserted(int positionStart, int itemCount) {
99 onDataSetChanged();
100 }
101
102 @Override
103 public void onItemRangeRemoved(int positionStart, int itemCount) {
104 onDataSetChanged();
105 }
106
107 @Override
108 public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
109 onDataSetChanged();
110 }
111 };
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700112
Jason Chiucfa36db2019-06-05 17:31:31 +0800113 @VisibleForTesting
114 ViewGroup mPinnedHeaderFrameLayout;
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700115
Jason Monk39b46742015-09-10 15:52:51 -0400116 private LayoutPreference mHeader;
117
Jason Monk39b46742015-09-10 15:52:51 -0400118 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -0500119 private LinearLayoutManager mLayoutManager;
Jason Monk2071eda2016-02-25 13:55:48 -0500120 private ArrayMap<String, Preference> mPreferenceCache;
Jason Monkf38fb382016-03-18 14:23:01 -0400121 private boolean mAnimationAllowed;
Jason Monk39b46742015-09-10 15:52:51 -0400122
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700123 @VisibleForTesting
124 public HighlightablePreferenceGroupAdapter mAdapter;
125 @VisibleForTesting
126 public boolean mPreferenceHighlighted = false;
127
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700128 @Override
129 public void onCreate(Bundle icicle) {
130 super.onCreate(icicle);
Mill Chen14cfd2f2021-01-06 07:46:46 +0800131 // TODO(b/176883483): Remove both search and help menu if this feature rolled out
132 if (!FeatureFlagUtils.isEnabled(getContext(), FeatureFlags.SILKY_HOME)) {
133 SearchMenuController.init(this /* host */);
134 HelpMenuController.init(this /* host */);
135 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700136
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700137 if (icicle != null) {
138 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
139 }
Fan Zhang72456a92018-02-20 11:25:56 -0800140 HighlightablePreferenceGroupAdapter.adjustInitialExpandedChildCount(this /* host */);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700141 }
142
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700143 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700144 public View onCreateView(LayoutInflater inflater, ViewGroup container,
145 Bundle savedInstanceState) {
146 final View root = super.onCreateView(inflater, container, savedInstanceState);
Fan Zhange0b0e9f2017-11-29 14:55:59 -0800147 mPinnedHeaderFrameLayout = root.findViewById(R.id.pinned_header);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700148 return root;
149 }
150
Jason Monk39b46742015-09-10 15:52:51 -0400151 @Override
Jason Monk91e2f892016-02-23 15:31:09 -0500152 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
153 super.addPreferencesFromResource(preferencesResId);
154 checkAvailablePrefs(getPreferenceScreen());
155 }
156
Doris Lingfe525942018-11-27 14:58:55 -0800157 @VisibleForTesting
158 void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
Jason Monk91e2f892016-02-23 15:31:09 -0500159 if (preferenceGroup == null) return;
160 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
161 Preference pref = preferenceGroup.getPreference(i);
162 if (pref instanceof SelfAvailablePreference
163 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
Doris Lingfe525942018-11-27 14:58:55 -0800164 pref.setVisible(false);
Jason Monk91e2f892016-02-23 15:31:09 -0500165 } else if (pref instanceof PreferenceGroup) {
166 checkAvailablePrefs((PreferenceGroup) pref);
167 }
168 }
169 }
170
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700171 public View setPinnedHeaderView(int layoutResId) {
172 final LayoutInflater inflater = getActivity().getLayoutInflater();
173 final View pinnedHeader =
174 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
175 setPinnedHeaderView(pinnedHeader);
176 return pinnedHeader;
177 }
178
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700179 public void setPinnedHeaderView(View pinnedHeader) {
180 mPinnedHeaderFrameLayout.addView(pinnedHeader);
181 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
182 }
183
Jason Chiucfa36db2019-06-05 17:31:31 +0800184 public void showPinnedHeader(boolean show) {
185 mPinnedHeaderFrameLayout.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
186 }
187
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700188 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700189 public void onSaveInstanceState(Bundle outState) {
190 super.onSaveInstanceState(outState);
191
Fan Zhang3d516e72018-01-31 14:14:41 -0800192 if (mAdapter != null) {
193 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mAdapter.isHighlightRequested());
194 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700195 }
196
197 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700198 public void onActivityCreated(Bundle savedInstanceState) {
199 super.onActivityCreated(savedInstanceState);
Johan Redestig76218e52016-04-19 08:29:30 +0200200 setHasOptionsMenu(true);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700201 }
202
203 @Override
204 public void onResume() {
205 super.onResume();
Fan Zhang3d516e72018-01-31 14:14:41 -0800206 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700207 }
208
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700209 @Override
210 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700211 registerObserverIfNeeded();
212 }
213
214 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700215 protected void onUnbindPreferences() {
216 unregisterObserverIfNeeded();
217 }
218
Jason Monkb37e2882016-01-11 14:27:20 -0500219 public void setLoading(boolean loading, boolean animate) {
Fan Zhang896f1b32017-06-26 14:22:45 -0700220 View loadingContainer = getView().findViewById(R.id.loading_container);
221 LoadingViewController.handleLoadingContainer(loadingContainer, getListView(),
222 !loading /* done */,
223 animate);
Jason Monkb37e2882016-01-11 14:27:20 -0500224 }
225
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700226 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700227 if (!mIsDataSetObserverRegistered) {
228 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400229 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700230 }
Jason Monk39b46742015-09-10 15:52:51 -0400231 mCurrentRootAdapter = getListView().getAdapter();
232 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700233 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500234 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700235 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700236 }
237
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700238 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700239 if (mIsDataSetObserverRegistered) {
240 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400241 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700242 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700243 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700244 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700245 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700246 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700247
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700248 public void highlightPreferenceIfNeeded() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800249 if (!isAdded()) {
250 return;
251 }
252 if (mAdapter != null) {
253 mAdapter.requestHighlight(getView(), getListView());
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700254 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700255 }
256
Fan Zhang72456a92018-02-20 11:25:56 -0800257 /**
258 * Returns initial expanded child count.
259 * <p/>
260 * Only override this method if the initial expanded child must be determined at run time.
261 */
262 public int getInitialExpandedChildCount() {
263 return 0;
264 }
265
Bonian Chen1b9bda32020-02-04 10:27:50 +0800266 /**
267 * Whether preference is allowing to be displayed to the user.
268 *
269 * @param preference to check if it can be displayed to the user (not hidding in expand area).
270 * @return {@code true} when preference is allowing to be displayed to the user.
271 * {@code false} when preference is hidden in expand area and not been displayed to the user.
272 */
273 protected boolean isPreferenceExpanded(Preference preference) {
274 return ((mAdapter == null)
275 || (mAdapter.getPreferenceAdapterPosition(preference) != RecyclerView.NO_POSITION));
276 }
277
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000278 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400279 highlightPreferenceIfNeeded();
280 updateEmptyView();
281 }
282
Jason Monk39b46742015-09-10 15:52:51 -0400283 public LayoutPreference getHeaderView() {
284 return mHeader;
285 }
286
Jason Monk39b46742015-09-10 15:52:51 -0400287 protected void setHeaderView(int resource) {
288 mHeader = new LayoutPreference(getPrefContext(), resource);
Yanting Yang70a8e312019-05-21 21:05:32 +0800289 mHeader.setSelectable(false);
Udam Sainid553abc2016-02-16 17:54:13 -0800290 addPreferenceToTop(mHeader);
291 }
292
293 protected void setHeaderView(View view) {
294 mHeader = new LayoutPreference(getPrefContext(), view);
Yanting Yang70a8e312019-05-21 21:05:32 +0800295 mHeader.setSelectable(false);
Udam Sainid553abc2016-02-16 17:54:13 -0800296 addPreferenceToTop(mHeader);
297 }
298
299 private void addPreferenceToTop(LayoutPreference preference) {
300 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400301 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800302 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400303 }
304 }
305
Jason Monk39b46742015-09-10 15:52:51 -0400306 @Override
307 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400308 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400309 // Without ids generated, the RecyclerView won't animate changes to the preferences.
310 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
311 }
Jason Monk39b46742015-09-10 15:52:51 -0400312 super.setPreferenceScreen(preferenceScreen);
313 if (preferenceScreen != null) {
314 if (mHeader != null) {
315 preferenceScreen.addPreference(mHeader);
316 }
Jason Monk39b46742015-09-10 15:52:51 -0400317 }
318 }
319
jackqdyulei2b2abac2017-05-26 10:47:55 -0700320 @VisibleForTesting
321 void updateEmptyView() {
Jason Monk39b46742015-09-10 15:52:51 -0400322 if (mEmptyView == null) return;
323 if (getPreferenceScreen() != null) {
jackqdyulei2b2abac2017-05-26 10:47:55 -0700324 final View listContainer = getActivity().findViewById(android.R.id.list_container);
Jason Monk39b46742015-09-10 15:52:51 -0400325 boolean show = (getPreferenceScreen().getPreferenceCount()
Sunny Shaobf434972019-08-29 14:28:13 +0800326 - (mHeader != null ? 1 : 0)) <= 0
jackqdyulei2b2abac2017-05-26 10:47:55 -0700327 || (listContainer != null && listContainer.getVisibility() != View.VISIBLE);
Jason Monk39b46742015-09-10 15:52:51 -0400328 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
329 } else {
330 mEmptyView.setVisibility(View.VISIBLE);
331 }
332 }
333
334 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000335 if (mEmptyView != null) {
336 mEmptyView.setVisibility(View.GONE);
337 }
Jason Monk39b46742015-09-10 15:52:51 -0400338 mEmptyView = v;
339 updateEmptyView();
340 }
341
342 public View getEmptyView() {
343 return mEmptyView;
344 }
345
Jason Monk65bb0972015-12-17 10:39:44 -0500346 @Override
347 public RecyclerView.LayoutManager onCreateLayoutManager() {
348 mLayoutManager = new LinearLayoutManager(getContext());
349 return mLayoutManager;
350 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700351
Jason Monk65bb0972015-12-17 10:39:44 -0500352 @Override
353 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
Fan Zhanga1000462018-02-02 12:15:37 -0800354 final Bundle arguments = getArguments();
Fan Zhang3d516e72018-01-31 14:14:41 -0800355 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen,
Fan Zhanga1000462018-02-02 12:15:37 -0800356 arguments == null
357 ? null : arguments.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY),
Fan Zhang3d516e72018-01-31 14:14:41 -0800358 mPreferenceHighlighted);
Jason Monk65bb0972015-12-17 10:39:44 -0500359 return mAdapter;
360 }
361
Jason Monkf38fb382016-03-18 14:23:01 -0400362 protected void setAnimationAllowed(boolean animationAllowed) {
363 mAnimationAllowed = animationAllowed;
364 }
365
Jason Monk2071eda2016-02-25 13:55:48 -0500366 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
Fan Zhang3d516e72018-01-31 14:14:41 -0800367 mPreferenceCache = new ArrayMap<>();
Jason Monk2071eda2016-02-25 13:55:48 -0500368 final int N = group.getPreferenceCount();
369 for (int i = 0; i < N; i++) {
370 Preference p = group.getPreference(i);
371 if (TextUtils.isEmpty(p.getKey())) {
372 continue;
373 }
374 mPreferenceCache.put(p.getKey(), p);
375 }
376 }
377
378 protected Preference getCachedPreference(String key) {
379 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
380 }
381
382 protected void removeCachedPrefs(PreferenceGroup group) {
383 for (Preference p : mPreferenceCache.values()) {
384 group.removePreference(p);
385 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400386 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500387 }
388
Jason Monka6278442016-04-21 10:12:30 -0400389 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400390 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400391 }
392
Jan Nordqvist9eb43dd2018-03-26 15:29:44 -0700393 @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
394 public boolean removePreference(String key) {
Fan Zhange84407f2017-05-24 11:19:52 -0700395 return removePreference(getPreferenceScreen(), key);
396 }
397
398 @VisibleForTesting
399 boolean removePreference(PreferenceGroup group, String key) {
400 final int preferenceCount = group.getPreferenceCount();
401 for (int i = 0; i < preferenceCount; i++) {
402 final Preference preference = group.getPreference(i);
403 final String curKey = preference.getKey();
404
405 if (TextUtils.equals(curKey, key)) {
406 return group.removePreference(preference);
407 }
408
409 if (preference instanceof PreferenceGroup) {
410 if (removePreference((PreferenceGroup) preference, key)) {
411 return true;
412 }
413 }
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700414 }
Fan Zhange84407f2017-05-24 11:19:52 -0700415 return false;
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700416 }
417
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700418 /*
419 * The name is intentionally made different from Activity#finish(), so that
420 * users won't misunderstand its meaning.
421 */
422 public final void finishFragment() {
423 getActivity().onBackPressed();
424 }
425
Amith Yamasanid7993472010-08-18 13:59:28 -0700426 // Some helpers for functions used by the settings fragments when they were activities
427
428 /**
429 * Returns the ContentResolver from the owning Activity.
430 */
431 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700432 Context context = getActivity();
433 if (context != null) {
434 mContentResolver = context.getContentResolver();
435 }
436 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700437 }
438
439 /**
440 * Returns the specified system service from the owning Activity.
441 */
442 protected Object getSystemService(final String name) {
443 return getActivity().getSystemService(name);
444 }
445
446 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700447 * Returns the PackageManager from the owning Activity.
448 */
449 protected PackageManager getPackageManager() {
450 return getActivity().getPackageManager();
451 }
452
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800453 @Override
454 public void onDetach() {
455 if (isRemoving()) {
456 if (mDialogFragment != null) {
457 mDialogFragment.dismiss();
458 mDialogFragment = null;
459 }
460 }
461 super.onDetach();
462 }
463
Amith Yamasanid7993472010-08-18 13:59:28 -0700464 // Dialog management
465
466 protected void showDialog(int dialogId) {
467 if (mDialogFragment != null) {
468 Log.e(TAG, "Old dialog fragment not null!");
469 }
tmfangd5405cf2018-10-05 18:45:07 +0800470 mDialogFragment = SettingsDialogFragment.newInstance(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800471 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700472 }
473
Fan Zhangd65184f2016-09-19 17:45:24 -0700474 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700475 public Dialog onCreateDialog(int dialogId) {
476 return null;
477 }
478
Fan Zhangd65184f2016-09-19 17:45:24 -0700479 @Override
480 public int getDialogMetricsCategory(int dialogId) {
481 return 0;
482 }
483
Amith Yamasanid7993472010-08-18 13:59:28 -0700484 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800485 // mDialogFragment may not be visible yet in parent fragment's onResume().
486 // To be able to dismiss dialog at that time, don't check
487 // mDialogFragment.isVisible().
488 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400489 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700490 }
491 mDialogFragment = null;
492 }
493
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800494 /**
495 * Sets the OnCancelListener 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 setOnCancelListener(DialogInterface.OnCancelListener listener) {
500 if (mDialogFragment != null) {
501 mDialogFragment.mOnCancelListener = listener;
502 }
503 }
504
505 /**
506 * Sets the OnDismissListener of the dialog shown. This method can only be
507 * called after showDialog(int) and before removeDialog(int). The method
508 * does nothing otherwise.
509 */
510 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
511 if (mDialogFragment != null) {
512 mDialogFragment.mOnDismissListener = listener;
513 }
514 }
515
Amith Yamasanic861cf82012-10-02 14:51:46 -0700516 public void onDialogShowing() {
517 // override in subclass to attach a dismiss listener, for instance
518 }
519
Jason Monk39b46742015-09-10 15:52:51 -0400520 @Override
521 public void onDisplayPreferenceDialog(Preference preference) {
522 if (preference.getKey() == null) {
523 // Auto-key preferences that don't have a key, so the dialog can find them.
524 preference.setKey(UUID.randomUUID().toString());
525 }
526 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000527 if (preference instanceof RestrictedListPreference) {
528 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
529 .newInstance(preference.getKey());
530 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400531 f = CustomListPreference.CustomListPreferenceDialogFragment
532 .newInstance(preference.getKey());
tmfang27c84de2018-06-28 11:39:05 +0800533 } else if (preference instanceof CustomDialogPreferenceCompat) {
534 f = CustomDialogPreferenceCompat.CustomPreferenceDialogFragment
Jason Monk39b46742015-09-10 15:52:51 -0400535 .newInstance(preference.getKey());
tmfang27c84de2018-06-28 11:39:05 +0800536 } else if (preference instanceof CustomEditTextPreferenceCompat) {
537 f = CustomEditTextPreferenceCompat.CustomPreferenceDialogFragment
Jason Monk39b46742015-09-10 15:52:51 -0400538 .newInstance(preference.getKey());
539 } else {
540 super.onDisplayPreferenceDialog(preference);
541 return;
542 }
543 f.setTargetFragment(this, 0);
544 f.show(getFragmentManager(), "dialog_preference");
545 onDialogShowing();
546 }
547
Fan Zhangd65184f2016-09-19 17:45:24 -0700548 public static class SettingsDialogFragment extends InstrumentedDialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800549 private static final String KEY_DIALOG_ID = "key_dialog_id";
550 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
551
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800552 private Fragment mParentFragment;
553
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800554 private DialogInterface.OnCancelListener mOnCancelListener;
555 private DialogInterface.OnDismissListener mOnDismissListener;
556
tmfangd5405cf2018-10-05 18:45:07 +0800557 public static SettingsDialogFragment newInstance(DialogCreatable fragment, int dialogId) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800558 if (!(fragment instanceof Fragment)) {
559 throw new IllegalArgumentException("fragment argument must be an instance of "
560 + Fragment.class.getName());
561 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800562
tmfangd5405cf2018-10-05 18:45:07 +0800563 final SettingsDialogFragment settingsDialogFragment = new SettingsDialogFragment();
564 settingsDialogFragment.setParentFragment(fragment);
565 settingsDialogFragment.setDialogId(dialogId);
566
567 return settingsDialogFragment;
568 }
Fan Zhangd65184f2016-09-19 17:45:24 -0700569
570 @Override
571 public int getMetricsCategory() {
tmfangd5405cf2018-10-05 18:45:07 +0800572 if (mParentFragment == null) {
Fan Zhang4fe7c082016-10-03 13:48:55 -0700573 return Instrumentable.METRICS_CATEGORY_UNKNOWN;
574 }
tmfangd5405cf2018-10-05 18:45:07 +0800575 final int metricsCategory =
576 ((DialogCreatable) mParentFragment).getDialogMetricsCategory(mDialogId);
Fan Zhangd65184f2016-09-19 17:45:24 -0700577 if (metricsCategory <= 0) {
578 throw new IllegalStateException("Dialog must provide a metrics category");
579 }
580 return metricsCategory;
581 }
582
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800583 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800584 public void onSaveInstanceState(Bundle outState) {
585 super.onSaveInstanceState(outState);
586 if (mParentFragment != null) {
587 outState.putInt(KEY_DIALOG_ID, mDialogId);
588 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
589 }
590 }
591
592 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700593 public void onStart() {
594 super.onStart();
595
596 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
597 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
598 }
599 }
600
601 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800602 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800603 if (savedInstanceState != null) {
604 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800605 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800606 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700607 if (mParentFragment == null) {
608 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
609 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800610 if (!(mParentFragment instanceof DialogCreatable)) {
611 throw new IllegalArgumentException(
612 (mParentFragment != null
613 ? mParentFragment.getClass().getName()
614 : mParentFragmentId)
615 + " must implement "
616 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800617 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800618 // This dialog fragment could be created from non-SettingsPreferenceFragment
619 if (mParentFragment instanceof SettingsPreferenceFragment) {
620 // restore mDialogFragment in mParentFragment
621 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
622 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800623 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800624 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700625 }
626
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800627 @Override
628 public void onCancel(DialogInterface dialog) {
629 super.onCancel(dialog);
630 if (mOnCancelListener != null) {
631 mOnCancelListener.onCancel(dialog);
632 }
633 }
634
635 @Override
636 public void onDismiss(DialogInterface dialog) {
637 super.onDismiss(dialog);
638 if (mOnDismissListener != null) {
639 mOnDismissListener.onDismiss(dialog);
640 }
641 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800642
Amith Yamasanid7993472010-08-18 13:59:28 -0700643 public int getDialogId() {
644 return mDialogId;
645 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800646
647 @Override
648 public void onDetach() {
649 super.onDetach();
650
Amith Yamasani8875ede2011-01-31 12:46:57 -0800651 // This dialog fragment could be created from non-SettingsPreferenceFragment
652 if (mParentFragment instanceof SettingsPreferenceFragment) {
653 // in case the dialog is not explicitly removed by removeDialog()
654 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
655 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
656 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800657 }
658 }
tmfangd5405cf2018-10-05 18:45:07 +0800659
660 private void setParentFragment(DialogCreatable fragment) {
661 mParentFragment = (Fragment) fragment;
662 }
663
664 private void setDialogId(int dialogId) {
665 mDialogId = dialogId;
666 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700667 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700668
669 protected boolean hasNextButton() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800670 return ((ButtonBarHandler) getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700671 }
672
673 protected Button getNextButton() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800674 return ((ButtonBarHandler) getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700675 }
676
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700677 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700678 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400679 if (activity == null) return;
680 if (getFragmentManager().getBackStackEntryCount() > 0) {
681 getFragmentManager().popBackStack();
682 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800683 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700684 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700685 }
686
Jason Monkb7e43802016-06-06 16:01:58 -0400687 protected Intent getIntent() {
688 if (getActivity() == null) {
689 return null;
690 }
691 return getActivity().getIntent();
692 }
693
694 protected void setResult(int result, Intent intent) {
695 if (getActivity() == null) {
696 return;
697 }
698 getActivity().setResult(result, intent);
699 }
700
701 protected void setResult(int result) {
702 if (getActivity() == null) {
703 return;
704 }
705 getActivity().setResult(result);
706 }
Arc Wang04854f82020-03-20 13:34:33 +0800707
708 protected boolean isFinishingOrDestroyed() {
709 final Activity activity = getActivity();
710 return activity == null || activity.isFinishing() || activity.isDestroyed();
711 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700712}