blob: 0c1ace905c28e6c5c77fbe80e0d344e79843aebc [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
Tsung-Mao Fangef877552021-02-24 16:37:39 +080060import com.google.android.material.appbar.AppBarLayout;
61
Jason Monk39b46742015-09-10 15:52:51 -040062import java.util.UUID;
63
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070064/**
Amith Yamasanid7993472010-08-18 13:59:28 -070065 * Base class for Settings fragments, with some helper functions and dialog management.
66 */
Fan Zhang2d0b3442016-12-05 17:02:33 -080067public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceFragment
Fan Zhang78ea7da2018-07-02 13:44:57 -070068 implements DialogCreatable, HelpResourceProvider, Indexable {
Anna Galusza0285c802016-01-29 17:32:19 -080069
Jason Monk65bb0972015-12-17 10:39:44 -050070 private static final String TAG = "SettingsPreference";
Amith Yamasanid7993472010-08-18 13:59:28 -070071
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070072 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070073
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000074 private static final int ORDER_FIRST = -1;
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000075
Fan Zhange0b0e9f2017-11-29 14:55:59 -080076 private SettingsDialogFragment mDialogFragment;
Amith Yamasani350938e2013-04-09 10:22:47 -070077 // Cache the content resolver for async callbacks
78 private ContentResolver mContentResolver;
79
Jason Monk39b46742015-09-10 15:52:51 -040080 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070081 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040082 private RecyclerView.AdapterDataObserver mDataSetObserver =
83 new RecyclerView.AdapterDataObserver() {
Tony Mantler0b825f52016-09-27 14:48:16 -070084 @Override
85 public void onChanged() {
86 onDataSetChanged();
87 }
88
89 @Override
90 public void onItemRangeChanged(int positionStart, int itemCount) {
91 onDataSetChanged();
92 }
93
94 @Override
95 public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
96 onDataSetChanged();
97 }
98
99 @Override
100 public void onItemRangeInserted(int positionStart, int itemCount) {
101 onDataSetChanged();
102 }
103
104 @Override
105 public void onItemRangeRemoved(int positionStart, int itemCount) {
106 onDataSetChanged();
107 }
108
109 @Override
110 public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
111 onDataSetChanged();
112 }
113 };
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700114
Jason Chiucfa36db2019-06-05 17:31:31 +0800115 @VisibleForTesting
116 ViewGroup mPinnedHeaderFrameLayout;
Tsung-Mao Fangef877552021-02-24 16:37:39 +0800117 private AppBarLayout mAppBarLayout;
Jason Monk39b46742015-09-10 15:52:51 -0400118 private LayoutPreference mHeader;
Jason Monk39b46742015-09-10 15:52:51 -0400119 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -0500120 private LinearLayoutManager mLayoutManager;
Jason Monk2071eda2016-02-25 13:55:48 -0500121 private ArrayMap<String, Preference> mPreferenceCache;
Jason Monkf38fb382016-03-18 14:23:01 -0400122 private boolean mAnimationAllowed;
Jason Monk39b46742015-09-10 15:52:51 -0400123
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700124 @VisibleForTesting
125 public HighlightablePreferenceGroupAdapter mAdapter;
126 @VisibleForTesting
127 public boolean mPreferenceHighlighted = false;
128
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700129 @Override
130 public void onCreate(Bundle icicle) {
131 super.onCreate(icicle);
Mill Chen14cfd2f2021-01-06 07:46:46 +0800132 // TODO(b/176883483): Remove both search and help menu if this feature rolled out
133 if (!FeatureFlagUtils.isEnabled(getContext(), FeatureFlags.SILKY_HOME)) {
134 SearchMenuController.init(this /* host */);
135 HelpMenuController.init(this /* host */);
136 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700137
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700138 if (icicle != null) {
139 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
140 }
Fan Zhang72456a92018-02-20 11:25:56 -0800141 HighlightablePreferenceGroupAdapter.adjustInitialExpandedChildCount(this /* host */);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700142 }
143
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700144 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700145 public View onCreateView(LayoutInflater inflater, ViewGroup container,
146 Bundle savedInstanceState) {
147 final View root = super.onCreateView(inflater, container, savedInstanceState);
Fan Zhange0b0e9f2017-11-29 14:55:59 -0800148 mPinnedHeaderFrameLayout = root.findViewById(R.id.pinned_header);
Tsung-Mao Fangef877552021-02-24 16:37:39 +0800149 mAppBarLayout = getActivity().findViewById(R.id.app_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700150 return root;
151 }
152
Jason Monk39b46742015-09-10 15:52:51 -0400153 @Override
Jason Monk91e2f892016-02-23 15:31:09 -0500154 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
155 super.addPreferencesFromResource(preferencesResId);
156 checkAvailablePrefs(getPreferenceScreen());
157 }
158
Doris Lingfe525942018-11-27 14:58:55 -0800159 @VisibleForTesting
160 void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
Jason Monk91e2f892016-02-23 15:31:09 -0500161 if (preferenceGroup == null) return;
162 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
163 Preference pref = preferenceGroup.getPreference(i);
164 if (pref instanceof SelfAvailablePreference
165 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
Doris Lingfe525942018-11-27 14:58:55 -0800166 pref.setVisible(false);
Jason Monk91e2f892016-02-23 15:31:09 -0500167 } else if (pref instanceof PreferenceGroup) {
168 checkAvailablePrefs((PreferenceGroup) pref);
169 }
170 }
171 }
172
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700173 public View setPinnedHeaderView(int layoutResId) {
174 final LayoutInflater inflater = getActivity().getLayoutInflater();
175 final View pinnedHeader =
176 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
177 setPinnedHeaderView(pinnedHeader);
178 return pinnedHeader;
179 }
180
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700181 public void setPinnedHeaderView(View pinnedHeader) {
182 mPinnedHeaderFrameLayout.addView(pinnedHeader);
183 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
184 }
185
Jason Chiucfa36db2019-06-05 17:31:31 +0800186 public void showPinnedHeader(boolean show) {
187 mPinnedHeaderFrameLayout.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
188 }
189
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700190 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700191 public void onSaveInstanceState(Bundle outState) {
192 super.onSaveInstanceState(outState);
193
Fan Zhang3d516e72018-01-31 14:14:41 -0800194 if (mAdapter != null) {
195 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mAdapter.isHighlightRequested());
196 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700197 }
198
199 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700200 public void onActivityCreated(Bundle savedInstanceState) {
201 super.onActivityCreated(savedInstanceState);
Johan Redestig76218e52016-04-19 08:29:30 +0200202 setHasOptionsMenu(true);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700203 }
204
205 @Override
206 public void onResume() {
207 super.onResume();
Fan Zhang3d516e72018-01-31 14:14:41 -0800208 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700209 }
210
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700211 @Override
212 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700213 registerObserverIfNeeded();
214 }
215
216 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700217 protected void onUnbindPreferences() {
218 unregisterObserverIfNeeded();
219 }
220
Jason Monkb37e2882016-01-11 14:27:20 -0500221 public void setLoading(boolean loading, boolean animate) {
Fan Zhang896f1b32017-06-26 14:22:45 -0700222 View loadingContainer = getView().findViewById(R.id.loading_container);
223 LoadingViewController.handleLoadingContainer(loadingContainer, getListView(),
224 !loading /* done */,
225 animate);
Jason Monkb37e2882016-01-11 14:27:20 -0500226 }
227
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700228 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700229 if (!mIsDataSetObserverRegistered) {
230 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400231 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700232 }
Jason Monk39b46742015-09-10 15:52:51 -0400233 mCurrentRootAdapter = getListView().getAdapter();
234 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700235 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500236 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700237 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700238 }
239
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700240 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700241 if (mIsDataSetObserverRegistered) {
242 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400243 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700244 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700245 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700246 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700247 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700248 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700249
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700250 public void highlightPreferenceIfNeeded() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800251 if (!isAdded()) {
252 return;
253 }
254 if (mAdapter != null) {
Tsung-Mao Fangef877552021-02-24 16:37:39 +0800255 mAdapter.requestHighlight(getView(), getListView(), mAppBarLayout);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700256 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700257 }
258
Fan Zhang72456a92018-02-20 11:25:56 -0800259 /**
260 * Returns initial expanded child count.
261 * <p/>
262 * Only override this method if the initial expanded child must be determined at run time.
263 */
264 public int getInitialExpandedChildCount() {
265 return 0;
266 }
267
Bonian Chen1b9bda32020-02-04 10:27:50 +0800268 /**
269 * Whether preference is allowing to be displayed to the user.
270 *
271 * @param preference to check if it can be displayed to the user (not hidding in expand area).
272 * @return {@code true} when preference is allowing to be displayed to the user.
273 * {@code false} when preference is hidden in expand area and not been displayed to the user.
274 */
275 protected boolean isPreferenceExpanded(Preference preference) {
276 return ((mAdapter == null)
277 || (mAdapter.getPreferenceAdapterPosition(preference) != RecyclerView.NO_POSITION));
278 }
279
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000280 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400281 highlightPreferenceIfNeeded();
282 updateEmptyView();
283 }
284
Jason Monk39b46742015-09-10 15:52:51 -0400285 public LayoutPreference getHeaderView() {
286 return mHeader;
287 }
288
Jason Monk39b46742015-09-10 15:52:51 -0400289 protected void setHeaderView(int resource) {
290 mHeader = new LayoutPreference(getPrefContext(), resource);
Yanting Yang70a8e312019-05-21 21:05:32 +0800291 mHeader.setSelectable(false);
Udam Sainid553abc2016-02-16 17:54:13 -0800292 addPreferenceToTop(mHeader);
293 }
294
295 protected void setHeaderView(View view) {
296 mHeader = new LayoutPreference(getPrefContext(), view);
Yanting Yang70a8e312019-05-21 21:05:32 +0800297 mHeader.setSelectable(false);
Udam Sainid553abc2016-02-16 17:54:13 -0800298 addPreferenceToTop(mHeader);
299 }
300
301 private void addPreferenceToTop(LayoutPreference preference) {
302 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400303 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800304 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400305 }
306 }
307
Jason Monk39b46742015-09-10 15:52:51 -0400308 @Override
309 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400310 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400311 // Without ids generated, the RecyclerView won't animate changes to the preferences.
312 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
313 }
Jason Monk39b46742015-09-10 15:52:51 -0400314 super.setPreferenceScreen(preferenceScreen);
315 if (preferenceScreen != null) {
316 if (mHeader != null) {
317 preferenceScreen.addPreference(mHeader);
318 }
Jason Monk39b46742015-09-10 15:52:51 -0400319 }
320 }
321
jackqdyulei2b2abac2017-05-26 10:47:55 -0700322 @VisibleForTesting
323 void updateEmptyView() {
Jason Monk39b46742015-09-10 15:52:51 -0400324 if (mEmptyView == null) return;
325 if (getPreferenceScreen() != null) {
jackqdyulei2b2abac2017-05-26 10:47:55 -0700326 final View listContainer = getActivity().findViewById(android.R.id.list_container);
Jason Monk39b46742015-09-10 15:52:51 -0400327 boolean show = (getPreferenceScreen().getPreferenceCount()
Sunny Shaobf434972019-08-29 14:28:13 +0800328 - (mHeader != null ? 1 : 0)) <= 0
jackqdyulei2b2abac2017-05-26 10:47:55 -0700329 || (listContainer != null && listContainer.getVisibility() != View.VISIBLE);
Jason Monk39b46742015-09-10 15:52:51 -0400330 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
331 } else {
332 mEmptyView.setVisibility(View.VISIBLE);
333 }
334 }
335
336 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000337 if (mEmptyView != null) {
338 mEmptyView.setVisibility(View.GONE);
339 }
Jason Monk39b46742015-09-10 15:52:51 -0400340 mEmptyView = v;
341 updateEmptyView();
342 }
343
344 public View getEmptyView() {
345 return mEmptyView;
346 }
347
Jason Monk65bb0972015-12-17 10:39:44 -0500348 @Override
349 public RecyclerView.LayoutManager onCreateLayoutManager() {
350 mLayoutManager = new LinearLayoutManager(getContext());
351 return mLayoutManager;
352 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700353
Jason Monk65bb0972015-12-17 10:39:44 -0500354 @Override
355 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
Fan Zhanga1000462018-02-02 12:15:37 -0800356 final Bundle arguments = getArguments();
Fan Zhang3d516e72018-01-31 14:14:41 -0800357 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen,
Fan Zhanga1000462018-02-02 12:15:37 -0800358 arguments == null
359 ? null : arguments.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY),
Fan Zhang3d516e72018-01-31 14:14:41 -0800360 mPreferenceHighlighted);
Jason Monk65bb0972015-12-17 10:39:44 -0500361 return mAdapter;
362 }
363
Jason Monkf38fb382016-03-18 14:23:01 -0400364 protected void setAnimationAllowed(boolean animationAllowed) {
365 mAnimationAllowed = animationAllowed;
366 }
367
Jason Monk2071eda2016-02-25 13:55:48 -0500368 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
Fan Zhang3d516e72018-01-31 14:14:41 -0800369 mPreferenceCache = new ArrayMap<>();
Jason Monk2071eda2016-02-25 13:55:48 -0500370 final int N = group.getPreferenceCount();
371 for (int i = 0; i < N; i++) {
372 Preference p = group.getPreference(i);
373 if (TextUtils.isEmpty(p.getKey())) {
374 continue;
375 }
376 mPreferenceCache.put(p.getKey(), p);
377 }
378 }
379
380 protected Preference getCachedPreference(String key) {
381 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
382 }
383
384 protected void removeCachedPrefs(PreferenceGroup group) {
385 for (Preference p : mPreferenceCache.values()) {
386 group.removePreference(p);
387 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400388 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500389 }
390
Jason Monka6278442016-04-21 10:12:30 -0400391 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400392 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400393 }
394
Jan Nordqvist9eb43dd2018-03-26 15:29:44 -0700395 @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
396 public boolean removePreference(String key) {
Fan Zhange84407f2017-05-24 11:19:52 -0700397 return removePreference(getPreferenceScreen(), key);
398 }
399
400 @VisibleForTesting
401 boolean removePreference(PreferenceGroup group, String key) {
402 final int preferenceCount = group.getPreferenceCount();
403 for (int i = 0; i < preferenceCount; i++) {
404 final Preference preference = group.getPreference(i);
405 final String curKey = preference.getKey();
406
407 if (TextUtils.equals(curKey, key)) {
408 return group.removePreference(preference);
409 }
410
411 if (preference instanceof PreferenceGroup) {
412 if (removePreference((PreferenceGroup) preference, key)) {
413 return true;
414 }
415 }
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700416 }
Fan Zhange84407f2017-05-24 11:19:52 -0700417 return false;
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700418 }
419
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700420 /*
421 * The name is intentionally made different from Activity#finish(), so that
422 * users won't misunderstand its meaning.
423 */
424 public final void finishFragment() {
425 getActivity().onBackPressed();
426 }
427
Amith Yamasanid7993472010-08-18 13:59:28 -0700428 // Some helpers for functions used by the settings fragments when they were activities
429
430 /**
431 * Returns the ContentResolver from the owning Activity.
432 */
433 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700434 Context context = getActivity();
435 if (context != null) {
436 mContentResolver = context.getContentResolver();
437 }
438 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700439 }
440
441 /**
442 * Returns the specified system service from the owning Activity.
443 */
444 protected Object getSystemService(final String name) {
445 return getActivity().getSystemService(name);
446 }
447
448 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700449 * Returns the PackageManager from the owning Activity.
450 */
451 protected PackageManager getPackageManager() {
452 return getActivity().getPackageManager();
453 }
454
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800455 @Override
456 public void onDetach() {
457 if (isRemoving()) {
458 if (mDialogFragment != null) {
459 mDialogFragment.dismiss();
460 mDialogFragment = null;
461 }
462 }
463 super.onDetach();
464 }
465
Amith Yamasanid7993472010-08-18 13:59:28 -0700466 // Dialog management
467
468 protected void showDialog(int dialogId) {
469 if (mDialogFragment != null) {
470 Log.e(TAG, "Old dialog fragment not null!");
471 }
tmfangd5405cf2018-10-05 18:45:07 +0800472 mDialogFragment = SettingsDialogFragment.newInstance(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800473 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700474 }
475
Fan Zhangd65184f2016-09-19 17:45:24 -0700476 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700477 public Dialog onCreateDialog(int dialogId) {
478 return null;
479 }
480
Fan Zhangd65184f2016-09-19 17:45:24 -0700481 @Override
482 public int getDialogMetricsCategory(int dialogId) {
483 return 0;
484 }
485
Amith Yamasanid7993472010-08-18 13:59:28 -0700486 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800487 // mDialogFragment may not be visible yet in parent fragment's onResume().
488 // To be able to dismiss dialog at that time, don't check
489 // mDialogFragment.isVisible().
490 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400491 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700492 }
493 mDialogFragment = null;
494 }
495
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800496 /**
497 * Sets the OnCancelListener of the dialog shown. This method can only be
498 * called after showDialog(int) and before removeDialog(int). The method
499 * does nothing otherwise.
500 */
501 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
502 if (mDialogFragment != null) {
503 mDialogFragment.mOnCancelListener = listener;
504 }
505 }
506
507 /**
508 * Sets the OnDismissListener of the dialog shown. This method can only be
509 * called after showDialog(int) and before removeDialog(int). The method
510 * does nothing otherwise.
511 */
512 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
513 if (mDialogFragment != null) {
514 mDialogFragment.mOnDismissListener = listener;
515 }
516 }
517
Amith Yamasanic861cf82012-10-02 14:51:46 -0700518 public void onDialogShowing() {
519 // override in subclass to attach a dismiss listener, for instance
520 }
521
Jason Monk39b46742015-09-10 15:52:51 -0400522 @Override
523 public void onDisplayPreferenceDialog(Preference preference) {
524 if (preference.getKey() == null) {
525 // Auto-key preferences that don't have a key, so the dialog can find them.
526 preference.setKey(UUID.randomUUID().toString());
527 }
528 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000529 if (preference instanceof RestrictedListPreference) {
530 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
531 .newInstance(preference.getKey());
532 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400533 f = CustomListPreference.CustomListPreferenceDialogFragment
534 .newInstance(preference.getKey());
tmfang27c84de2018-06-28 11:39:05 +0800535 } else if (preference instanceof CustomDialogPreferenceCompat) {
536 f = CustomDialogPreferenceCompat.CustomPreferenceDialogFragment
Jason Monk39b46742015-09-10 15:52:51 -0400537 .newInstance(preference.getKey());
tmfang27c84de2018-06-28 11:39:05 +0800538 } else if (preference instanceof CustomEditTextPreferenceCompat) {
539 f = CustomEditTextPreferenceCompat.CustomPreferenceDialogFragment
Jason Monk39b46742015-09-10 15:52:51 -0400540 .newInstance(preference.getKey());
541 } else {
542 super.onDisplayPreferenceDialog(preference);
543 return;
544 }
545 f.setTargetFragment(this, 0);
546 f.show(getFragmentManager(), "dialog_preference");
547 onDialogShowing();
548 }
549
Fan Zhangd65184f2016-09-19 17:45:24 -0700550 public static class SettingsDialogFragment extends InstrumentedDialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800551 private static final String KEY_DIALOG_ID = "key_dialog_id";
552 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
553
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800554 private Fragment mParentFragment;
555
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800556 private DialogInterface.OnCancelListener mOnCancelListener;
557 private DialogInterface.OnDismissListener mOnDismissListener;
558
tmfangd5405cf2018-10-05 18:45:07 +0800559 public static SettingsDialogFragment newInstance(DialogCreatable fragment, int dialogId) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800560 if (!(fragment instanceof Fragment)) {
561 throw new IllegalArgumentException("fragment argument must be an instance of "
562 + Fragment.class.getName());
563 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800564
tmfangd5405cf2018-10-05 18:45:07 +0800565 final SettingsDialogFragment settingsDialogFragment = new SettingsDialogFragment();
566 settingsDialogFragment.setParentFragment(fragment);
567 settingsDialogFragment.setDialogId(dialogId);
568
569 return settingsDialogFragment;
570 }
Fan Zhangd65184f2016-09-19 17:45:24 -0700571
572 @Override
573 public int getMetricsCategory() {
tmfangd5405cf2018-10-05 18:45:07 +0800574 if (mParentFragment == null) {
Fan Zhang4fe7c082016-10-03 13:48:55 -0700575 return Instrumentable.METRICS_CATEGORY_UNKNOWN;
576 }
tmfangd5405cf2018-10-05 18:45:07 +0800577 final int metricsCategory =
578 ((DialogCreatable) mParentFragment).getDialogMetricsCategory(mDialogId);
Fan Zhangd65184f2016-09-19 17:45:24 -0700579 if (metricsCategory <= 0) {
580 throw new IllegalStateException("Dialog must provide a metrics category");
581 }
582 return metricsCategory;
583 }
584
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800585 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800586 public void onSaveInstanceState(Bundle outState) {
587 super.onSaveInstanceState(outState);
588 if (mParentFragment != null) {
589 outState.putInt(KEY_DIALOG_ID, mDialogId);
590 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
591 }
592 }
593
594 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700595 public void onStart() {
596 super.onStart();
597
598 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
599 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
600 }
601 }
602
603 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800604 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800605 if (savedInstanceState != null) {
606 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800607 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800608 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700609 if (mParentFragment == null) {
610 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
611 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800612 if (!(mParentFragment instanceof DialogCreatable)) {
613 throw new IllegalArgumentException(
614 (mParentFragment != null
615 ? mParentFragment.getClass().getName()
616 : mParentFragmentId)
617 + " must implement "
618 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800619 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800620 // This dialog fragment could be created from non-SettingsPreferenceFragment
621 if (mParentFragment instanceof SettingsPreferenceFragment) {
622 // restore mDialogFragment in mParentFragment
623 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
624 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800625 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800626 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700627 }
628
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800629 @Override
630 public void onCancel(DialogInterface dialog) {
631 super.onCancel(dialog);
632 if (mOnCancelListener != null) {
633 mOnCancelListener.onCancel(dialog);
634 }
635 }
636
637 @Override
638 public void onDismiss(DialogInterface dialog) {
639 super.onDismiss(dialog);
640 if (mOnDismissListener != null) {
641 mOnDismissListener.onDismiss(dialog);
642 }
643 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800644
Amith Yamasanid7993472010-08-18 13:59:28 -0700645 public int getDialogId() {
646 return mDialogId;
647 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800648
649 @Override
650 public void onDetach() {
651 super.onDetach();
652
Amith Yamasani8875ede2011-01-31 12:46:57 -0800653 // This dialog fragment could be created from non-SettingsPreferenceFragment
654 if (mParentFragment instanceof SettingsPreferenceFragment) {
655 // in case the dialog is not explicitly removed by removeDialog()
656 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
657 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
658 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800659 }
660 }
tmfangd5405cf2018-10-05 18:45:07 +0800661
662 private void setParentFragment(DialogCreatable fragment) {
663 mParentFragment = (Fragment) fragment;
664 }
665
666 private void setDialogId(int dialogId) {
667 mDialogId = dialogId;
668 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700669 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700670
671 protected boolean hasNextButton() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800672 return ((ButtonBarHandler) getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700673 }
674
675 protected Button getNextButton() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800676 return ((ButtonBarHandler) getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700677 }
678
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700679 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700680 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400681 if (activity == null) return;
682 if (getFragmentManager().getBackStackEntryCount() > 0) {
683 getFragmentManager().popBackStack();
684 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800685 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700686 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700687 }
688
Jason Monkb7e43802016-06-06 16:01:58 -0400689 protected Intent getIntent() {
690 if (getActivity() == null) {
691 return null;
692 }
693 return getActivity().getIntent();
694 }
695
696 protected void setResult(int result, Intent intent) {
697 if (getActivity() == null) {
698 return;
699 }
700 getActivity().setResult(result, intent);
701 }
702
703 protected void setResult(int result) {
704 if (getActivity() == null) {
705 return;
706 }
707 getActivity().setResult(result);
708 }
Arc Wang04854f82020-03-20 13:34:33 +0800709
710 protected boolean isFinishingOrDestroyed() {
711 final Activity activity = getActivity();
712 return activity == null || activity.isFinishing() || activity.isDestroyed();
713 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700714}