blob: b8ff60c7c147d02ba6c89dda87431e8d538e4e4e [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 Zhange0b0e9f2017-11-29 14:55:59 -080047import com.android.settings.support.actionbar.HelpResourceProvider;
Fan Zhang3d516e72018-01-31 14:14:41 -080048import com.android.settings.widget.HighlightablePreferenceGroupAdapter;
Fan Zhang896f1b32017-06-26 14:22:45 -070049import com.android.settings.widget.LoadingViewController;
tmfang27c84de2018-06-28 11:39:05 +080050import com.android.settingslib.CustomDialogPreferenceCompat;
51import com.android.settingslib.CustomEditTextPreferenceCompat;
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -080052import com.android.settingslib.core.instrumentation.Instrumentable;
Raff Tsai966fa012019-09-25 11:19:06 +080053import com.android.settingslib.search.Indexable;
tmfangdce94bb2018-11-26 18:41:01 +080054import com.android.settingslib.widget.LayoutPreference;
John Spurlockb8e02b82015-04-15 21:15:55 -040055
Tsung-Mao Fangef877552021-02-24 16:37:39 +080056import com.google.android.material.appbar.AppBarLayout;
57
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;
Tsung-Mao Fangef877552021-02-24 16:37:39 +0800113 private AppBarLayout mAppBarLayout;
Jason Monk39b46742015-09-10 15:52:51 -0400114 private LayoutPreference mHeader;
Jason Monk39b46742015-09-10 15:52:51 -0400115 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -0500116 private LinearLayoutManager mLayoutManager;
Jason Monk2071eda2016-02-25 13:55:48 -0500117 private ArrayMap<String, Preference> mPreferenceCache;
Jason Monkf38fb382016-03-18 14:23:01 -0400118 private boolean mAnimationAllowed;
Jason Monk39b46742015-09-10 15:52:51 -0400119
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700120 @VisibleForTesting
121 public HighlightablePreferenceGroupAdapter mAdapter;
Greg Kaiser096c0902021-10-08 14:29:06 +0000122 @VisibleForTesting
123 public boolean mPreferenceHighlighted = false;
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700124
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700125 @Override
126 public void onCreate(Bundle icicle) {
127 super.onCreate(icicle);
128
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700129 if (icicle != null) {
130 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
131 }
Fan Zhang72456a92018-02-20 11:25:56 -0800132 HighlightablePreferenceGroupAdapter.adjustInitialExpandedChildCount(this /* host */);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700133 }
134
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700135 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700136 public View onCreateView(LayoutInflater inflater, ViewGroup container,
137 Bundle savedInstanceState) {
138 final View root = super.onCreateView(inflater, container, savedInstanceState);
Fan Zhange0b0e9f2017-11-29 14:55:59 -0800139 mPinnedHeaderFrameLayout = root.findViewById(R.id.pinned_header);
Tsung-Mao Fangef877552021-02-24 16:37:39 +0800140 mAppBarLayout = getActivity().findViewById(R.id.app_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700141 return root;
142 }
143
Jason Monk39b46742015-09-10 15:52:51 -0400144 @Override
Jason Monk91e2f892016-02-23 15:31:09 -0500145 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
146 super.addPreferencesFromResource(preferencesResId);
147 checkAvailablePrefs(getPreferenceScreen());
148 }
149
Doris Lingfe525942018-11-27 14:58:55 -0800150 @VisibleForTesting
151 void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
Jason Monk91e2f892016-02-23 15:31:09 -0500152 if (preferenceGroup == null) return;
153 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
154 Preference pref = preferenceGroup.getPreference(i);
155 if (pref instanceof SelfAvailablePreference
156 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
Doris Lingfe525942018-11-27 14:58:55 -0800157 pref.setVisible(false);
Jason Monk91e2f892016-02-23 15:31:09 -0500158 } else if (pref instanceof PreferenceGroup) {
159 checkAvailablePrefs((PreferenceGroup) pref);
160 }
161 }
162 }
163
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700164 public View setPinnedHeaderView(int layoutResId) {
165 final LayoutInflater inflater = getActivity().getLayoutInflater();
166 final View pinnedHeader =
167 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
168 setPinnedHeaderView(pinnedHeader);
169 return pinnedHeader;
170 }
171
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700172 public void setPinnedHeaderView(View pinnedHeader) {
173 mPinnedHeaderFrameLayout.addView(pinnedHeader);
174 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
175 }
176
Jason Chiucfa36db2019-06-05 17:31:31 +0800177 public void showPinnedHeader(boolean show) {
178 mPinnedHeaderFrameLayout.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
179 }
180
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700181 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700182 public void onSaveInstanceState(Bundle outState) {
183 super.onSaveInstanceState(outState);
184
Fan Zhang3d516e72018-01-31 14:14:41 -0800185 if (mAdapter != null) {
186 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mAdapter.isHighlightRequested());
187 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700188 }
189
190 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700191 public void onActivityCreated(Bundle savedInstanceState) {
192 super.onActivityCreated(savedInstanceState);
Johan Redestig76218e52016-04-19 08:29:30 +0200193 setHasOptionsMenu(true);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700194 }
195
196 @Override
197 public void onResume() {
198 super.onResume();
Fan Zhang3d516e72018-01-31 14:14:41 -0800199 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700200 }
201
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700202 @Override
203 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700204 registerObserverIfNeeded();
205 }
206
207 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700208 protected void onUnbindPreferences() {
209 unregisterObserverIfNeeded();
210 }
211
Jason Monkb37e2882016-01-11 14:27:20 -0500212 public void setLoading(boolean loading, boolean animate) {
Fan Zhang896f1b32017-06-26 14:22:45 -0700213 View loadingContainer = getView().findViewById(R.id.loading_container);
214 LoadingViewController.handleLoadingContainer(loadingContainer, getListView(),
215 !loading /* done */,
216 animate);
Jason Monkb37e2882016-01-11 14:27:20 -0500217 }
218
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700219 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700220 if (!mIsDataSetObserverRegistered) {
221 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400222 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700223 }
Jason Monk39b46742015-09-10 15:52:51 -0400224 mCurrentRootAdapter = getListView().getAdapter();
225 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700226 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500227 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700228 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700229 }
230
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700231 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700232 if (mIsDataSetObserverRegistered) {
233 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400234 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700235 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700236 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700237 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700238 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700239 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700240
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700241 public void highlightPreferenceIfNeeded() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800242 if (!isAdded()) {
243 return;
244 }
245 if (mAdapter != null) {
Tsung-Mao Fangef877552021-02-24 16:37:39 +0800246 mAdapter.requestHighlight(getView(), getListView(), mAppBarLayout);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700247 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700248 }
249
Fan Zhang72456a92018-02-20 11:25:56 -0800250 /**
251 * Returns initial expanded child count.
252 * <p/>
253 * Only override this method if the initial expanded child must be determined at run time.
254 */
255 public int getInitialExpandedChildCount() {
256 return 0;
257 }
258
Bonian Chen1b9bda32020-02-04 10:27:50 +0800259 /**
260 * Whether preference is allowing to be displayed to the user.
261 *
262 * @param preference to check if it can be displayed to the user (not hidding in expand area).
263 * @return {@code true} when preference is allowing to be displayed to the user.
264 * {@code false} when preference is hidden in expand area and not been displayed to the user.
265 */
266 protected boolean isPreferenceExpanded(Preference preference) {
267 return ((mAdapter == null)
268 || (mAdapter.getPreferenceAdapterPosition(preference) != RecyclerView.NO_POSITION));
269 }
270
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000271 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400272 highlightPreferenceIfNeeded();
273 updateEmptyView();
274 }
275
Jason Monk39b46742015-09-10 15:52:51 -0400276 public LayoutPreference getHeaderView() {
277 return mHeader;
278 }
279
Jason Monk39b46742015-09-10 15:52:51 -0400280 protected void setHeaderView(int resource) {
281 mHeader = new LayoutPreference(getPrefContext(), resource);
Yanting Yang70a8e312019-05-21 21:05:32 +0800282 mHeader.setSelectable(false);
Udam Sainid553abc2016-02-16 17:54:13 -0800283 addPreferenceToTop(mHeader);
284 }
285
286 protected void setHeaderView(View view) {
287 mHeader = new LayoutPreference(getPrefContext(), view);
Yanting Yang70a8e312019-05-21 21:05:32 +0800288 mHeader.setSelectable(false);
Udam Sainid553abc2016-02-16 17:54:13 -0800289 addPreferenceToTop(mHeader);
290 }
291
292 private void addPreferenceToTop(LayoutPreference preference) {
293 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400294 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800295 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400296 }
297 }
298
Jason Monk39b46742015-09-10 15:52:51 -0400299 @Override
300 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400301 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400302 // Without ids generated, the RecyclerView won't animate changes to the preferences.
303 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
304 }
Jason Monk39b46742015-09-10 15:52:51 -0400305 super.setPreferenceScreen(preferenceScreen);
306 if (preferenceScreen != null) {
307 if (mHeader != null) {
308 preferenceScreen.addPreference(mHeader);
309 }
Jason Monk39b46742015-09-10 15:52:51 -0400310 }
311 }
312
jackqdyulei2b2abac2017-05-26 10:47:55 -0700313 @VisibleForTesting
314 void updateEmptyView() {
Jason Monk39b46742015-09-10 15:52:51 -0400315 if (mEmptyView == null) return;
316 if (getPreferenceScreen() != null) {
jackqdyulei2b2abac2017-05-26 10:47:55 -0700317 final View listContainer = getActivity().findViewById(android.R.id.list_container);
Jason Monk39b46742015-09-10 15:52:51 -0400318 boolean show = (getPreferenceScreen().getPreferenceCount()
Sunny Shaobf434972019-08-29 14:28:13 +0800319 - (mHeader != null ? 1 : 0)) <= 0
jackqdyulei2b2abac2017-05-26 10:47:55 -0700320 || (listContainer != null && listContainer.getVisibility() != View.VISIBLE);
Jason Monk39b46742015-09-10 15:52:51 -0400321 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
322 } else {
323 mEmptyView.setVisibility(View.VISIBLE);
324 }
325 }
326
327 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000328 if (mEmptyView != null) {
329 mEmptyView.setVisibility(View.GONE);
330 }
Jason Monk39b46742015-09-10 15:52:51 -0400331 mEmptyView = v;
332 updateEmptyView();
333 }
334
335 public View getEmptyView() {
336 return mEmptyView;
337 }
338
Jason Monk65bb0972015-12-17 10:39:44 -0500339 @Override
340 public RecyclerView.LayoutManager onCreateLayoutManager() {
341 mLayoutManager = new LinearLayoutManager(getContext());
342 return mLayoutManager;
343 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700344
Jason Monk65bb0972015-12-17 10:39:44 -0500345 @Override
346 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
Fan Zhanga1000462018-02-02 12:15:37 -0800347 final Bundle arguments = getArguments();
Fan Zhang3d516e72018-01-31 14:14:41 -0800348 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen,
Fan Zhanga1000462018-02-02 12:15:37 -0800349 arguments == null
350 ? null : arguments.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY),
Fan Zhang3d516e72018-01-31 14:14:41 -0800351 mPreferenceHighlighted);
Jason Monk65bb0972015-12-17 10:39:44 -0500352 return mAdapter;
353 }
354
Jason Monkf38fb382016-03-18 14:23:01 -0400355 protected void setAnimationAllowed(boolean animationAllowed) {
356 mAnimationAllowed = animationAllowed;
357 }
358
Jason Monk2071eda2016-02-25 13:55:48 -0500359 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
Fan Zhang3d516e72018-01-31 14:14:41 -0800360 mPreferenceCache = new ArrayMap<>();
Jason Monk2071eda2016-02-25 13:55:48 -0500361 final int N = group.getPreferenceCount();
362 for (int i = 0; i < N; i++) {
363 Preference p = group.getPreference(i);
364 if (TextUtils.isEmpty(p.getKey())) {
365 continue;
366 }
367 mPreferenceCache.put(p.getKey(), p);
368 }
369 }
370
371 protected Preference getCachedPreference(String key) {
372 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
373 }
374
375 protected void removeCachedPrefs(PreferenceGroup group) {
376 for (Preference p : mPreferenceCache.values()) {
377 group.removePreference(p);
378 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400379 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500380 }
381
Jason Monka6278442016-04-21 10:12:30 -0400382 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400383 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400384 }
385
Jan Nordqvist9eb43dd2018-03-26 15:29:44 -0700386 @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
387 public boolean removePreference(String key) {
Fan Zhange84407f2017-05-24 11:19:52 -0700388 return removePreference(getPreferenceScreen(), key);
389 }
390
391 @VisibleForTesting
392 boolean removePreference(PreferenceGroup group, String key) {
393 final int preferenceCount = group.getPreferenceCount();
394 for (int i = 0; i < preferenceCount; i++) {
395 final Preference preference = group.getPreference(i);
396 final String curKey = preference.getKey();
397
398 if (TextUtils.equals(curKey, key)) {
399 return group.removePreference(preference);
400 }
401
402 if (preference instanceof PreferenceGroup) {
403 if (removePreference((PreferenceGroup) preference, key)) {
404 return true;
405 }
406 }
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700407 }
Fan Zhange84407f2017-05-24 11:19:52 -0700408 return false;
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700409 }
410
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700411 /*
412 * The name is intentionally made different from Activity#finish(), so that
413 * users won't misunderstand its meaning.
414 */
415 public final void finishFragment() {
416 getActivity().onBackPressed();
417 }
418
Amith Yamasanid7993472010-08-18 13:59:28 -0700419 // Some helpers for functions used by the settings fragments when they were activities
420
421 /**
422 * Returns the ContentResolver from the owning Activity.
423 */
424 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700425 Context context = getActivity();
426 if (context != null) {
427 mContentResolver = context.getContentResolver();
428 }
429 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700430 }
431
432 /**
433 * Returns the specified system service from the owning Activity.
434 */
435 protected Object getSystemService(final String name) {
436 return getActivity().getSystemService(name);
437 }
438
439 /**
Bonian Chen49cdb602021-03-17 13:32:32 +0800440 * Returns the specified system service from the owning Activity.
441 */
442 protected <T> T getSystemService(final Class<T> serviceClass) {
443 return getActivity().getSystemService(serviceClass);
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}