blob: d3e1c49c06f3e4b91969413b4e342e233fb67780 [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;
Tsung-Mao Fang07dd8332022-01-03 18:25:04 +080057import com.google.android.setupcompat.util.WizardManagerHelper;
Tsung-Mao Fangef877552021-02-24 16:37:39 +080058
Jason Monk39b46742015-09-10 15:52:51 -040059import java.util.UUID;
60
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070061/**
Amith Yamasanid7993472010-08-18 13:59:28 -070062 * Base class for Settings fragments, with some helper functions and dialog management.
63 */
Fan Zhang2d0b3442016-12-05 17:02:33 -080064public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceFragment
Fan Zhang78ea7da2018-07-02 13:44:57 -070065 implements DialogCreatable, HelpResourceProvider, Indexable {
Anna Galusza0285c802016-01-29 17:32:19 -080066
Tsung-Mao Fang07dd8332022-01-03 18:25:04 +080067 private static final String TAG = "SettingsPreferenceFragment";
Amith Yamasanid7993472010-08-18 13:59:28 -070068
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070069 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070070
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000071 private static final int ORDER_FIRST = -1;
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000072
Fan Zhange0b0e9f2017-11-29 14:55:59 -080073 private SettingsDialogFragment mDialogFragment;
Amith Yamasani350938e2013-04-09 10:22:47 -070074 // Cache the content resolver for async callbacks
75 private ContentResolver mContentResolver;
76
Jason Monk39b46742015-09-10 15:52:51 -040077 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070078 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040079 private RecyclerView.AdapterDataObserver mDataSetObserver =
80 new RecyclerView.AdapterDataObserver() {
Tony Mantler0b825f52016-09-27 14:48:16 -070081 @Override
82 public void onChanged() {
83 onDataSetChanged();
84 }
85
86 @Override
87 public void onItemRangeChanged(int positionStart, int itemCount) {
88 onDataSetChanged();
89 }
90
91 @Override
92 public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
93 onDataSetChanged();
94 }
95
96 @Override
97 public void onItemRangeInserted(int positionStart, int itemCount) {
98 onDataSetChanged();
99 }
100
101 @Override
102 public void onItemRangeRemoved(int positionStart, int itemCount) {
103 onDataSetChanged();
104 }
105
106 @Override
107 public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
108 onDataSetChanged();
109 }
110 };
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700111
Jason Chiucfa36db2019-06-05 17:31:31 +0800112 @VisibleForTesting
113 ViewGroup mPinnedHeaderFrameLayout;
Tsung-Mao Fangef877552021-02-24 16:37:39 +0800114 private AppBarLayout mAppBarLayout;
Jason Monk39b46742015-09-10 15:52:51 -0400115 private LayoutPreference mHeader;
Jason Monk39b46742015-09-10 15:52:51 -0400116 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -0500117 private LinearLayoutManager mLayoutManager;
Jason Monk2071eda2016-02-25 13:55:48 -0500118 private ArrayMap<String, Preference> mPreferenceCache;
Jason Monkf38fb382016-03-18 14:23:01 -0400119 private boolean mAnimationAllowed;
Jason Monk39b46742015-09-10 15:52:51 -0400120
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700121 @VisibleForTesting
122 public HighlightablePreferenceGroupAdapter mAdapter;
Jason Chiu29a09e52021-10-12 17:24:34 +0800123 private boolean mPreferenceHighlighted = false;
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700124
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700125 @Override
Tsung-Mao Fang07dd8332022-01-03 18:25:04 +0800126 public void onAttach(Context context) {
127 if (shouldSkipForInitialSUW() && !WizardManagerHelper.isDeviceProvisioned(getContext())) {
128 Log.w(TAG, "Skip " + getClass().getSimpleName() + " before SUW completed.");
129 finish();
130 }
131 super.onAttach(context);
132 }
133
134 @Override
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700135 public void onCreate(Bundle icicle) {
136 super.onCreate(icicle);
137
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
Tsung-Mao Fang07dd8332022-01-03 18:25:04 +0800280 /**
281 * Whether UI should be skipped in the initial SUW flow.
282 *
283 * @return {@code true} when UI should be skipped in the initial SUW flow.
284 * {@code false} when UI should not be skipped in the initial SUW flow.
285 */
286 protected boolean shouldSkipForInitialSUW() {
287 return false;
288 }
289
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000290 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400291 highlightPreferenceIfNeeded();
292 updateEmptyView();
293 }
294
Jason Monk39b46742015-09-10 15:52:51 -0400295 public LayoutPreference getHeaderView() {
296 return mHeader;
297 }
298
Jason Monk39b46742015-09-10 15:52:51 -0400299 protected void setHeaderView(int resource) {
300 mHeader = new LayoutPreference(getPrefContext(), resource);
Yanting Yang70a8e312019-05-21 21:05:32 +0800301 mHeader.setSelectable(false);
Udam Sainid553abc2016-02-16 17:54:13 -0800302 addPreferenceToTop(mHeader);
303 }
304
305 protected void setHeaderView(View view) {
306 mHeader = new LayoutPreference(getPrefContext(), view);
Yanting Yang70a8e312019-05-21 21:05:32 +0800307 mHeader.setSelectable(false);
Udam Sainid553abc2016-02-16 17:54:13 -0800308 addPreferenceToTop(mHeader);
309 }
310
311 private void addPreferenceToTop(LayoutPreference preference) {
312 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400313 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800314 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400315 }
316 }
317
Jason Monk39b46742015-09-10 15:52:51 -0400318 @Override
319 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400320 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400321 // Without ids generated, the RecyclerView won't animate changes to the preferences.
322 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
323 }
Jason Monk39b46742015-09-10 15:52:51 -0400324 super.setPreferenceScreen(preferenceScreen);
325 if (preferenceScreen != null) {
326 if (mHeader != null) {
327 preferenceScreen.addPreference(mHeader);
328 }
Jason Monk39b46742015-09-10 15:52:51 -0400329 }
330 }
331
jackqdyulei2b2abac2017-05-26 10:47:55 -0700332 @VisibleForTesting
333 void updateEmptyView() {
Jason Monk39b46742015-09-10 15:52:51 -0400334 if (mEmptyView == null) return;
335 if (getPreferenceScreen() != null) {
jackqdyulei2b2abac2017-05-26 10:47:55 -0700336 final View listContainer = getActivity().findViewById(android.R.id.list_container);
Jason Monk39b46742015-09-10 15:52:51 -0400337 boolean show = (getPreferenceScreen().getPreferenceCount()
Sunny Shaobf434972019-08-29 14:28:13 +0800338 - (mHeader != null ? 1 : 0)) <= 0
jackqdyulei2b2abac2017-05-26 10:47:55 -0700339 || (listContainer != null && listContainer.getVisibility() != View.VISIBLE);
Jason Monk39b46742015-09-10 15:52:51 -0400340 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
341 } else {
342 mEmptyView.setVisibility(View.VISIBLE);
343 }
344 }
345
346 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000347 if (mEmptyView != null) {
348 mEmptyView.setVisibility(View.GONE);
349 }
Jason Monk39b46742015-09-10 15:52:51 -0400350 mEmptyView = v;
351 updateEmptyView();
352 }
353
354 public View getEmptyView() {
355 return mEmptyView;
356 }
357
Jason Monk65bb0972015-12-17 10:39:44 -0500358 @Override
359 public RecyclerView.LayoutManager onCreateLayoutManager() {
360 mLayoutManager = new LinearLayoutManager(getContext());
361 return mLayoutManager;
362 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700363
Jason Monk65bb0972015-12-17 10:39:44 -0500364 @Override
365 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
Fan Zhanga1000462018-02-02 12:15:37 -0800366 final Bundle arguments = getArguments();
Fan Zhang3d516e72018-01-31 14:14:41 -0800367 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen,
Fan Zhanga1000462018-02-02 12:15:37 -0800368 arguments == null
369 ? null : arguments.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY),
Fan Zhang3d516e72018-01-31 14:14:41 -0800370 mPreferenceHighlighted);
Jason Monk65bb0972015-12-17 10:39:44 -0500371 return mAdapter;
372 }
373
Jason Monkf38fb382016-03-18 14:23:01 -0400374 protected void setAnimationAllowed(boolean animationAllowed) {
375 mAnimationAllowed = animationAllowed;
376 }
377
Jason Monk2071eda2016-02-25 13:55:48 -0500378 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
Fan Zhang3d516e72018-01-31 14:14:41 -0800379 mPreferenceCache = new ArrayMap<>();
Jason Monk2071eda2016-02-25 13:55:48 -0500380 final int N = group.getPreferenceCount();
381 for (int i = 0; i < N; i++) {
382 Preference p = group.getPreference(i);
383 if (TextUtils.isEmpty(p.getKey())) {
384 continue;
385 }
386 mPreferenceCache.put(p.getKey(), p);
387 }
388 }
389
390 protected Preference getCachedPreference(String key) {
391 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
392 }
393
394 protected void removeCachedPrefs(PreferenceGroup group) {
395 for (Preference p : mPreferenceCache.values()) {
396 group.removePreference(p);
397 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400398 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500399 }
400
Jason Monka6278442016-04-21 10:12:30 -0400401 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400402 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400403 }
404
Jan Nordqvist9eb43dd2018-03-26 15:29:44 -0700405 @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
406 public boolean removePreference(String key) {
Fan Zhange84407f2017-05-24 11:19:52 -0700407 return removePreference(getPreferenceScreen(), key);
408 }
409
410 @VisibleForTesting
411 boolean removePreference(PreferenceGroup group, String key) {
412 final int preferenceCount = group.getPreferenceCount();
413 for (int i = 0; i < preferenceCount; i++) {
414 final Preference preference = group.getPreference(i);
415 final String curKey = preference.getKey();
416
417 if (TextUtils.equals(curKey, key)) {
418 return group.removePreference(preference);
419 }
420
421 if (preference instanceof PreferenceGroup) {
422 if (removePreference((PreferenceGroup) preference, key)) {
423 return true;
424 }
425 }
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700426 }
Fan Zhange84407f2017-05-24 11:19:52 -0700427 return false;
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700428 }
429
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700430 /*
431 * The name is intentionally made different from Activity#finish(), so that
432 * users won't misunderstand its meaning.
433 */
434 public final void finishFragment() {
435 getActivity().onBackPressed();
436 }
437
Amith Yamasanid7993472010-08-18 13:59:28 -0700438 // Some helpers for functions used by the settings fragments when they were activities
439
440 /**
441 * Returns the ContentResolver from the owning Activity.
442 */
443 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700444 Context context = getActivity();
445 if (context != null) {
446 mContentResolver = context.getContentResolver();
447 }
448 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700449 }
450
451 /**
452 * Returns the specified system service from the owning Activity.
453 */
454 protected Object getSystemService(final String name) {
455 return getActivity().getSystemService(name);
456 }
457
458 /**
Bonian Chen49cdb602021-03-17 13:32:32 +0800459 * Returns the specified system service from the owning Activity.
460 */
461 protected <T> T getSystemService(final Class<T> serviceClass) {
462 return getActivity().getSystemService(serviceClass);
463 }
464
465 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700466 * Returns the PackageManager from the owning Activity.
467 */
468 protected PackageManager getPackageManager() {
469 return getActivity().getPackageManager();
470 }
471
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800472 @Override
473 public void onDetach() {
474 if (isRemoving()) {
475 if (mDialogFragment != null) {
476 mDialogFragment.dismiss();
477 mDialogFragment = null;
478 }
479 }
480 super.onDetach();
481 }
482
Amith Yamasanid7993472010-08-18 13:59:28 -0700483 // Dialog management
484
485 protected void showDialog(int dialogId) {
486 if (mDialogFragment != null) {
487 Log.e(TAG, "Old dialog fragment not null!");
488 }
tmfangd5405cf2018-10-05 18:45:07 +0800489 mDialogFragment = SettingsDialogFragment.newInstance(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800490 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700491 }
492
Fan Zhangd65184f2016-09-19 17:45:24 -0700493 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700494 public Dialog onCreateDialog(int dialogId) {
495 return null;
496 }
497
Fan Zhangd65184f2016-09-19 17:45:24 -0700498 @Override
499 public int getDialogMetricsCategory(int dialogId) {
500 return 0;
501 }
502
Amith Yamasanid7993472010-08-18 13:59:28 -0700503 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800504 // mDialogFragment may not be visible yet in parent fragment's onResume().
505 // To be able to dismiss dialog at that time, don't check
506 // mDialogFragment.isVisible().
507 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400508 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700509 }
510 mDialogFragment = null;
511 }
512
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800513 /**
514 * Sets the OnCancelListener of the dialog shown. This method can only be
515 * called after showDialog(int) and before removeDialog(int). The method
516 * does nothing otherwise.
517 */
518 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
519 if (mDialogFragment != null) {
520 mDialogFragment.mOnCancelListener = listener;
521 }
522 }
523
524 /**
525 * Sets the OnDismissListener of the dialog shown. This method can only be
526 * called after showDialog(int) and before removeDialog(int). The method
527 * does nothing otherwise.
528 */
529 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
530 if (mDialogFragment != null) {
531 mDialogFragment.mOnDismissListener = listener;
532 }
533 }
534
Amith Yamasanic861cf82012-10-02 14:51:46 -0700535 public void onDialogShowing() {
536 // override in subclass to attach a dismiss listener, for instance
537 }
538
Jason Monk39b46742015-09-10 15:52:51 -0400539 @Override
540 public void onDisplayPreferenceDialog(Preference preference) {
541 if (preference.getKey() == null) {
542 // Auto-key preferences that don't have a key, so the dialog can find them.
543 preference.setKey(UUID.randomUUID().toString());
544 }
545 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000546 if (preference instanceof RestrictedListPreference) {
547 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
548 .newInstance(preference.getKey());
549 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400550 f = CustomListPreference.CustomListPreferenceDialogFragment
551 .newInstance(preference.getKey());
tmfang27c84de2018-06-28 11:39:05 +0800552 } else if (preference instanceof CustomDialogPreferenceCompat) {
553 f = CustomDialogPreferenceCompat.CustomPreferenceDialogFragment
Jason Monk39b46742015-09-10 15:52:51 -0400554 .newInstance(preference.getKey());
tmfang27c84de2018-06-28 11:39:05 +0800555 } else if (preference instanceof CustomEditTextPreferenceCompat) {
556 f = CustomEditTextPreferenceCompat.CustomPreferenceDialogFragment
Jason Monk39b46742015-09-10 15:52:51 -0400557 .newInstance(preference.getKey());
558 } else {
559 super.onDisplayPreferenceDialog(preference);
560 return;
561 }
562 f.setTargetFragment(this, 0);
563 f.show(getFragmentManager(), "dialog_preference");
564 onDialogShowing();
565 }
566
Fan Zhangd65184f2016-09-19 17:45:24 -0700567 public static class SettingsDialogFragment extends InstrumentedDialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800568 private static final String KEY_DIALOG_ID = "key_dialog_id";
569 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
570
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800571 private Fragment mParentFragment;
572
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800573 private DialogInterface.OnCancelListener mOnCancelListener;
574 private DialogInterface.OnDismissListener mOnDismissListener;
575
tmfangd5405cf2018-10-05 18:45:07 +0800576 public static SettingsDialogFragment newInstance(DialogCreatable fragment, int dialogId) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800577 if (!(fragment instanceof Fragment)) {
578 throw new IllegalArgumentException("fragment argument must be an instance of "
579 + Fragment.class.getName());
580 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800581
tmfangd5405cf2018-10-05 18:45:07 +0800582 final SettingsDialogFragment settingsDialogFragment = new SettingsDialogFragment();
583 settingsDialogFragment.setParentFragment(fragment);
584 settingsDialogFragment.setDialogId(dialogId);
585
586 return settingsDialogFragment;
587 }
Fan Zhangd65184f2016-09-19 17:45:24 -0700588
589 @Override
590 public int getMetricsCategory() {
tmfangd5405cf2018-10-05 18:45:07 +0800591 if (mParentFragment == null) {
Fan Zhang4fe7c082016-10-03 13:48:55 -0700592 return Instrumentable.METRICS_CATEGORY_UNKNOWN;
593 }
tmfangd5405cf2018-10-05 18:45:07 +0800594 final int metricsCategory =
595 ((DialogCreatable) mParentFragment).getDialogMetricsCategory(mDialogId);
Fan Zhangd65184f2016-09-19 17:45:24 -0700596 if (metricsCategory <= 0) {
597 throw new IllegalStateException("Dialog must provide a metrics category");
598 }
599 return metricsCategory;
600 }
601
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800602 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800603 public void onSaveInstanceState(Bundle outState) {
604 super.onSaveInstanceState(outState);
605 if (mParentFragment != null) {
606 outState.putInt(KEY_DIALOG_ID, mDialogId);
607 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
608 }
609 }
610
611 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700612 public void onStart() {
613 super.onStart();
614
615 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
616 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
617 }
618 }
619
620 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800621 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800622 if (savedInstanceState != null) {
623 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800624 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800625 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700626 if (mParentFragment == null) {
627 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
628 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800629 if (!(mParentFragment instanceof DialogCreatable)) {
630 throw new IllegalArgumentException(
631 (mParentFragment != null
632 ? mParentFragment.getClass().getName()
633 : mParentFragmentId)
634 + " must implement "
635 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800636 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800637 // This dialog fragment could be created from non-SettingsPreferenceFragment
638 if (mParentFragment instanceof SettingsPreferenceFragment) {
639 // restore mDialogFragment in mParentFragment
640 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
641 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800642 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800643 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700644 }
645
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800646 @Override
647 public void onCancel(DialogInterface dialog) {
648 super.onCancel(dialog);
649 if (mOnCancelListener != null) {
650 mOnCancelListener.onCancel(dialog);
651 }
652 }
653
654 @Override
655 public void onDismiss(DialogInterface dialog) {
656 super.onDismiss(dialog);
657 if (mOnDismissListener != null) {
658 mOnDismissListener.onDismiss(dialog);
659 }
660 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800661
Amith Yamasanid7993472010-08-18 13:59:28 -0700662 public int getDialogId() {
663 return mDialogId;
664 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800665
666 @Override
667 public void onDetach() {
668 super.onDetach();
669
Amith Yamasani8875ede2011-01-31 12:46:57 -0800670 // This dialog fragment could be created from non-SettingsPreferenceFragment
671 if (mParentFragment instanceof SettingsPreferenceFragment) {
672 // in case the dialog is not explicitly removed by removeDialog()
673 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
674 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
675 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800676 }
677 }
tmfangd5405cf2018-10-05 18:45:07 +0800678
679 private void setParentFragment(DialogCreatable fragment) {
680 mParentFragment = (Fragment) fragment;
681 }
682
683 private void setDialogId(int dialogId) {
684 mDialogId = dialogId;
685 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700686 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700687
688 protected boolean hasNextButton() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800689 return ((ButtonBarHandler) getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700690 }
691
692 protected Button getNextButton() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800693 return ((ButtonBarHandler) getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700694 }
695
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700696 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700697 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400698 if (activity == null) return;
699 if (getFragmentManager().getBackStackEntryCount() > 0) {
700 getFragmentManager().popBackStack();
701 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800702 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700703 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700704 }
705
Jason Monkb7e43802016-06-06 16:01:58 -0400706 protected Intent getIntent() {
707 if (getActivity() == null) {
708 return null;
709 }
710 return getActivity().getIntent();
711 }
712
713 protected void setResult(int result, Intent intent) {
714 if (getActivity() == null) {
715 return;
716 }
717 getActivity().setResult(result, intent);
718 }
719
720 protected void setResult(int result) {
721 if (getActivity() == null) {
722 return;
723 }
724 getActivity().setResult(result);
725 }
Arc Wang04854f82020-03-20 13:34:33 +0800726
727 protected boolean isFinishingOrDestroyed() {
728 final Activity activity = getActivity();
729 return activity == null || activity.isFinishing() || activity.isDestroyed();
730 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700731}