blob: 8c97f0244a7d2052f14f1307cadfa22437127c90 [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;
Jonathan Scottb7f4f562022-04-11 09:49:17 +000021import android.app.admin.DevicePolicyManager;
Amith Yamasanid7993472010-08-18 13:59:28 -070022import android.content.ContentResolver;
Amith Yamasani350938e2013-04-09 10:22:47 -070023import android.content.Context;
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +080024import android.content.DialogInterface;
Jason Monkb7e43802016-06-06 16:01:58 -040025import android.content.Intent;
Amith Yamasanid7993472010-08-18 13:59:28 -070026import android.content.pm.PackageManager;
Amith Yamasanid7993472010-08-18 13:59:28 -070027import android.os.Bundle;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070028import android.text.TextUtils;
Jason Monk2071eda2016-02-25 13:55:48 -050029import android.util.ArrayMap;
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
Fan Zhang2d0b3442016-12-05 17:02:33 -080046import com.android.settings.core.InstrumentedPreferenceFragment;
Fan Zhangd65184f2016-09-19 17:45:24 -070047import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
Fan Zhange0b0e9f2017-11-29 14:55:59 -080048import com.android.settings.support.actionbar.HelpResourceProvider;
Fan Zhang3d516e72018-01-31 14:14:41 -080049import com.android.settings.widget.HighlightablePreferenceGroupAdapter;
Fan Zhang896f1b32017-06-26 14:22:45 -070050import com.android.settings.widget.LoadingViewController;
tmfang27c84de2018-06-28 11:39:05 +080051import com.android.settingslib.CustomDialogPreferenceCompat;
52import com.android.settingslib.CustomEditTextPreferenceCompat;
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -080053import com.android.settingslib.core.instrumentation.Instrumentable;
Raff Tsai966fa012019-09-25 11:19:06 +080054import com.android.settingslib.search.Indexable;
tmfangdce94bb2018-11-26 18:41:01 +080055import com.android.settingslib.widget.LayoutPreference;
John Spurlockb8e02b82015-04-15 21:15:55 -040056
Tsung-Mao Fangef877552021-02-24 16:37:39 +080057import com.google.android.material.appbar.AppBarLayout;
Tsung-Mao Fang07dd8332022-01-03 18:25:04 +080058import com.google.android.setupcompat.util.WizardManagerHelper;
Tsung-Mao Fangef877552021-02-24 16:37:39 +080059
Jason Monk39b46742015-09-10 15:52:51 -040060import java.util.UUID;
61
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070062/**
Amith Yamasanid7993472010-08-18 13:59:28 -070063 * Base class for Settings fragments, with some helper functions and dialog management.
64 */
Fan Zhang2d0b3442016-12-05 17:02:33 -080065public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceFragment
Fan Zhang78ea7da2018-07-02 13:44:57 -070066 implements DialogCreatable, HelpResourceProvider, Indexable {
Anna Galusza0285c802016-01-29 17:32:19 -080067
Tsung-Mao Fang07dd8332022-01-03 18:25:04 +080068 private static final String TAG = "SettingsPreferenceFragment";
Amith Yamasanid7993472010-08-18 13:59:28 -070069
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070070 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070071
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000072 private static final int ORDER_FIRST = -1;
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000073
Jonathan Scottb7f4f562022-04-11 09:49:17 +000074 protected DevicePolicyManager mDevicePolicyManager;
Fan Zhange0b0e9f2017-11-29 14:55:59 -080075 private SettingsDialogFragment mDialogFragment;
Amith Yamasani350938e2013-04-09 10:22:47 -070076 // Cache the content resolver for async callbacks
77 private ContentResolver mContentResolver;
78
Jason Monk39b46742015-09-10 15:52:51 -040079 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070080 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040081 private RecyclerView.AdapterDataObserver mDataSetObserver =
82 new RecyclerView.AdapterDataObserver() {
Tony Mantler0b825f52016-09-27 14:48:16 -070083 @Override
84 public void onChanged() {
85 onDataSetChanged();
86 }
87
88 @Override
89 public void onItemRangeChanged(int positionStart, int itemCount) {
90 onDataSetChanged();
91 }
92
93 @Override
94 public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
95 onDataSetChanged();
96 }
97
98 @Override
99 public void onItemRangeInserted(int positionStart, int itemCount) {
100 onDataSetChanged();
101 }
102
103 @Override
104 public void onItemRangeRemoved(int positionStart, int itemCount) {
105 onDataSetChanged();
106 }
107
108 @Override
109 public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
110 onDataSetChanged();
111 }
112 };
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700113
Jason Chiucfa36db2019-06-05 17:31:31 +0800114 @VisibleForTesting
115 ViewGroup mPinnedHeaderFrameLayout;
Tsung-Mao Fangef877552021-02-24 16:37:39 +0800116 private AppBarLayout mAppBarLayout;
Jason Monk39b46742015-09-10 15:52:51 -0400117 private LayoutPreference mHeader;
Jason Monk39b46742015-09-10 15:52:51 -0400118 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -0500119 private LinearLayoutManager mLayoutManager;
Jason Monk2071eda2016-02-25 13:55:48 -0500120 private ArrayMap<String, Preference> mPreferenceCache;
Jason Monkf38fb382016-03-18 14:23:01 -0400121 private boolean mAnimationAllowed;
Jason Monk39b46742015-09-10 15:52:51 -0400122
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700123 @VisibleForTesting
124 public HighlightablePreferenceGroupAdapter mAdapter;
Jason Chiu29a09e52021-10-12 17:24:34 +0800125 private boolean mPreferenceHighlighted = false;
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700126
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700127 @Override
Tsung-Mao Fang07dd8332022-01-03 18:25:04 +0800128 public void onAttach(Context context) {
129 if (shouldSkipForInitialSUW() && !WizardManagerHelper.isDeviceProvisioned(getContext())) {
130 Log.w(TAG, "Skip " + getClass().getSimpleName() + " before SUW completed.");
131 finish();
132 }
133 super.onAttach(context);
134 }
135
136 @Override
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700137 public void onCreate(Bundle icicle) {
138 super.onCreate(icicle);
139
Jonathan Scottb7f4f562022-04-11 09:49:17 +0000140 mDevicePolicyManager = getContext().getSystemService(DevicePolicyManager.class);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700141 if (icicle != null) {
142 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
143 }
Fan Zhang72456a92018-02-20 11:25:56 -0800144 HighlightablePreferenceGroupAdapter.adjustInitialExpandedChildCount(this /* host */);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700145 }
146
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700147 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700148 public View onCreateView(LayoutInflater inflater, ViewGroup container,
149 Bundle savedInstanceState) {
150 final View root = super.onCreateView(inflater, container, savedInstanceState);
Fan Zhange0b0e9f2017-11-29 14:55:59 -0800151 mPinnedHeaderFrameLayout = root.findViewById(R.id.pinned_header);
Tsung-Mao Fangef877552021-02-24 16:37:39 +0800152 mAppBarLayout = getActivity().findViewById(R.id.app_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700153 return root;
154 }
155
Jason Monk39b46742015-09-10 15:52:51 -0400156 @Override
Jason Monk91e2f892016-02-23 15:31:09 -0500157 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
158 super.addPreferencesFromResource(preferencesResId);
159 checkAvailablePrefs(getPreferenceScreen());
160 }
161
Doris Lingfe525942018-11-27 14:58:55 -0800162 @VisibleForTesting
163 void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
Jason Monk91e2f892016-02-23 15:31:09 -0500164 if (preferenceGroup == null) return;
165 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
166 Preference pref = preferenceGroup.getPreference(i);
167 if (pref instanceof SelfAvailablePreference
168 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
Doris Lingfe525942018-11-27 14:58:55 -0800169 pref.setVisible(false);
Jason Monk91e2f892016-02-23 15:31:09 -0500170 } else if (pref instanceof PreferenceGroup) {
171 checkAvailablePrefs((PreferenceGroup) pref);
172 }
173 }
174 }
175
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700176 public View setPinnedHeaderView(int layoutResId) {
177 final LayoutInflater inflater = getActivity().getLayoutInflater();
178 final View pinnedHeader =
179 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
180 setPinnedHeaderView(pinnedHeader);
181 return pinnedHeader;
182 }
183
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700184 public void setPinnedHeaderView(View pinnedHeader) {
185 mPinnedHeaderFrameLayout.addView(pinnedHeader);
186 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
187 }
188
Jason Chiucfa36db2019-06-05 17:31:31 +0800189 public void showPinnedHeader(boolean show) {
190 mPinnedHeaderFrameLayout.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
191 }
192
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700193 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700194 public void onSaveInstanceState(Bundle outState) {
195 super.onSaveInstanceState(outState);
196
Fan Zhang3d516e72018-01-31 14:14:41 -0800197 if (mAdapter != null) {
198 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mAdapter.isHighlightRequested());
199 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700200 }
201
202 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700203 public void onActivityCreated(Bundle savedInstanceState) {
204 super.onActivityCreated(savedInstanceState);
Johan Redestig76218e52016-04-19 08:29:30 +0200205 setHasOptionsMenu(true);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700206 }
207
208 @Override
209 public void onResume() {
210 super.onResume();
Fan Zhang3d516e72018-01-31 14:14:41 -0800211 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700212 }
213
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700214 @Override
215 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700216 registerObserverIfNeeded();
217 }
218
219 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700220 protected void onUnbindPreferences() {
221 unregisterObserverIfNeeded();
222 }
223
Jason Monkb37e2882016-01-11 14:27:20 -0500224 public void setLoading(boolean loading, boolean animate) {
Fan Zhang896f1b32017-06-26 14:22:45 -0700225 View loadingContainer = getView().findViewById(R.id.loading_container);
226 LoadingViewController.handleLoadingContainer(loadingContainer, getListView(),
227 !loading /* done */,
228 animate);
Jason Monkb37e2882016-01-11 14:27:20 -0500229 }
230
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700231 public void registerObserverIfNeeded() {
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 Meglio7c435f62014-07-29 16:02:22 -0700235 }
Jason Monk39b46742015-09-10 15:52:51 -0400236 mCurrentRootAdapter = getListView().getAdapter();
237 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700238 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500239 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700240 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700241 }
242
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700243 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700244 if (mIsDataSetObserverRegistered) {
245 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400246 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700247 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700248 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700249 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700250 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700251 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700252
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700253 public void highlightPreferenceIfNeeded() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800254 if (!isAdded()) {
255 return;
256 }
257 if (mAdapter != null) {
Tsung-Mao Fangef877552021-02-24 16:37:39 +0800258 mAdapter.requestHighlight(getView(), getListView(), mAppBarLayout);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700259 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700260 }
261
Fan Zhang72456a92018-02-20 11:25:56 -0800262 /**
263 * Returns initial expanded child count.
264 * <p/>
265 * Only override this method if the initial expanded child must be determined at run time.
266 */
267 public int getInitialExpandedChildCount() {
268 return 0;
269 }
270
Bonian Chen1b9bda32020-02-04 10:27:50 +0800271 /**
272 * Whether preference is allowing to be displayed to the user.
273 *
274 * @param preference to check if it can be displayed to the user (not hidding in expand area).
275 * @return {@code true} when preference is allowing to be displayed to the user.
276 * {@code false} when preference is hidden in expand area and not been displayed to the user.
277 */
278 protected boolean isPreferenceExpanded(Preference preference) {
279 return ((mAdapter == null)
280 || (mAdapter.getPreferenceAdapterPosition(preference) != RecyclerView.NO_POSITION));
281 }
282
Tsung-Mao Fang07dd8332022-01-03 18:25:04 +0800283 /**
284 * Whether UI should be skipped in the initial SUW flow.
285 *
286 * @return {@code true} when UI should be skipped in the initial SUW flow.
287 * {@code false} when UI should not be skipped in the initial SUW flow.
288 */
289 protected boolean shouldSkipForInitialSUW() {
290 return false;
291 }
292
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000293 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400294 highlightPreferenceIfNeeded();
295 updateEmptyView();
296 }
297
Jason Monk39b46742015-09-10 15:52:51 -0400298 public LayoutPreference getHeaderView() {
299 return mHeader;
300 }
301
Jason Monk39b46742015-09-10 15:52:51 -0400302 protected void setHeaderView(int resource) {
303 mHeader = new LayoutPreference(getPrefContext(), resource);
Yanting Yang70a8e312019-05-21 21:05:32 +0800304 mHeader.setSelectable(false);
Udam Sainid553abc2016-02-16 17:54:13 -0800305 addPreferenceToTop(mHeader);
306 }
307
308 protected void setHeaderView(View view) {
309 mHeader = new LayoutPreference(getPrefContext(), view);
Yanting Yang70a8e312019-05-21 21:05:32 +0800310 mHeader.setSelectable(false);
Udam Sainid553abc2016-02-16 17:54:13 -0800311 addPreferenceToTop(mHeader);
312 }
313
314 private void addPreferenceToTop(LayoutPreference preference) {
315 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400316 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800317 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400318 }
319 }
320
Jason Monk39b46742015-09-10 15:52:51 -0400321 @Override
322 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400323 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400324 // Without ids generated, the RecyclerView won't animate changes to the preferences.
325 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
326 }
Jason Monk39b46742015-09-10 15:52:51 -0400327 super.setPreferenceScreen(preferenceScreen);
328 if (preferenceScreen != null) {
329 if (mHeader != null) {
330 preferenceScreen.addPreference(mHeader);
331 }
Jason Monk39b46742015-09-10 15:52:51 -0400332 }
333 }
334
jackqdyulei2b2abac2017-05-26 10:47:55 -0700335 @VisibleForTesting
336 void updateEmptyView() {
Jason Monk39b46742015-09-10 15:52:51 -0400337 if (mEmptyView == null) return;
338 if (getPreferenceScreen() != null) {
jackqdyulei2b2abac2017-05-26 10:47:55 -0700339 final View listContainer = getActivity().findViewById(android.R.id.list_container);
Jason Monk39b46742015-09-10 15:52:51 -0400340 boolean show = (getPreferenceScreen().getPreferenceCount()
Sunny Shaobf434972019-08-29 14:28:13 +0800341 - (mHeader != null ? 1 : 0)) <= 0
jackqdyulei2b2abac2017-05-26 10:47:55 -0700342 || (listContainer != null && listContainer.getVisibility() != View.VISIBLE);
Jason Monk39b46742015-09-10 15:52:51 -0400343 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
344 } else {
345 mEmptyView.setVisibility(View.VISIBLE);
346 }
347 }
348
349 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000350 if (mEmptyView != null) {
351 mEmptyView.setVisibility(View.GONE);
352 }
Jason Monk39b46742015-09-10 15:52:51 -0400353 mEmptyView = v;
354 updateEmptyView();
355 }
356
357 public View getEmptyView() {
358 return mEmptyView;
359 }
360
Jason Monk65bb0972015-12-17 10:39:44 -0500361 @Override
362 public RecyclerView.LayoutManager onCreateLayoutManager() {
363 mLayoutManager = new LinearLayoutManager(getContext());
364 return mLayoutManager;
365 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700366
Jason Monk65bb0972015-12-17 10:39:44 -0500367 @Override
368 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
Fan Zhanga1000462018-02-02 12:15:37 -0800369 final Bundle arguments = getArguments();
Fan Zhang3d516e72018-01-31 14:14:41 -0800370 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen,
Fan Zhanga1000462018-02-02 12:15:37 -0800371 arguments == null
372 ? null : arguments.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY),
Fan Zhang3d516e72018-01-31 14:14:41 -0800373 mPreferenceHighlighted);
Jason Monk65bb0972015-12-17 10:39:44 -0500374 return mAdapter;
375 }
376
Jason Monkf38fb382016-03-18 14:23:01 -0400377 protected void setAnimationAllowed(boolean animationAllowed) {
378 mAnimationAllowed = animationAllowed;
379 }
380
Jason Monk2071eda2016-02-25 13:55:48 -0500381 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
Fan Zhang3d516e72018-01-31 14:14:41 -0800382 mPreferenceCache = new ArrayMap<>();
Jason Monk2071eda2016-02-25 13:55:48 -0500383 final int N = group.getPreferenceCount();
384 for (int i = 0; i < N; i++) {
385 Preference p = group.getPreference(i);
386 if (TextUtils.isEmpty(p.getKey())) {
387 continue;
388 }
389 mPreferenceCache.put(p.getKey(), p);
390 }
391 }
392
393 protected Preference getCachedPreference(String key) {
394 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
395 }
396
397 protected void removeCachedPrefs(PreferenceGroup group) {
398 for (Preference p : mPreferenceCache.values()) {
399 group.removePreference(p);
400 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400401 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500402 }
403
Jason Monka6278442016-04-21 10:12:30 -0400404 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400405 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400406 }
407
Jan Nordqvist9eb43dd2018-03-26 15:29:44 -0700408 @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
409 public boolean removePreference(String key) {
Fan Zhange84407f2017-05-24 11:19:52 -0700410 return removePreference(getPreferenceScreen(), key);
411 }
412
413 @VisibleForTesting
414 boolean removePreference(PreferenceGroup group, String key) {
415 final int preferenceCount = group.getPreferenceCount();
416 for (int i = 0; i < preferenceCount; i++) {
417 final Preference preference = group.getPreference(i);
418 final String curKey = preference.getKey();
419
420 if (TextUtils.equals(curKey, key)) {
421 return group.removePreference(preference);
422 }
423
424 if (preference instanceof PreferenceGroup) {
425 if (removePreference((PreferenceGroup) preference, key)) {
426 return true;
427 }
428 }
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700429 }
Fan Zhange84407f2017-05-24 11:19:52 -0700430 return false;
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700431 }
432
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700433 /*
434 * The name is intentionally made different from Activity#finish(), so that
435 * users won't misunderstand its meaning.
436 */
437 public final void finishFragment() {
438 getActivity().onBackPressed();
439 }
440
Amith Yamasanid7993472010-08-18 13:59:28 -0700441 // Some helpers for functions used by the settings fragments when they were activities
442
443 /**
444 * Returns the ContentResolver from the owning Activity.
445 */
446 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700447 Context context = getActivity();
448 if (context != null) {
449 mContentResolver = context.getContentResolver();
450 }
451 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700452 }
453
454 /**
455 * Returns the specified system service from the owning Activity.
456 */
457 protected Object getSystemService(final String name) {
458 return getActivity().getSystemService(name);
459 }
460
461 /**
Bonian Chen49cdb602021-03-17 13:32:32 +0800462 * Returns the specified system service from the owning Activity.
463 */
464 protected <T> T getSystemService(final Class<T> serviceClass) {
465 return getActivity().getSystemService(serviceClass);
466 }
467
468 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700469 * Returns the PackageManager from the owning Activity.
470 */
471 protected PackageManager getPackageManager() {
472 return getActivity().getPackageManager();
473 }
474
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800475 @Override
476 public void onDetach() {
477 if (isRemoving()) {
478 if (mDialogFragment != null) {
479 mDialogFragment.dismiss();
480 mDialogFragment = null;
481 }
482 }
483 super.onDetach();
484 }
485
Amith Yamasanid7993472010-08-18 13:59:28 -0700486 // Dialog management
487
488 protected void showDialog(int dialogId) {
489 if (mDialogFragment != null) {
490 Log.e(TAG, "Old dialog fragment not null!");
491 }
tmfangd5405cf2018-10-05 18:45:07 +0800492 mDialogFragment = SettingsDialogFragment.newInstance(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800493 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700494 }
495
Fan Zhangd65184f2016-09-19 17:45:24 -0700496 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700497 public Dialog onCreateDialog(int dialogId) {
498 return null;
499 }
500
Fan Zhangd65184f2016-09-19 17:45:24 -0700501 @Override
502 public int getDialogMetricsCategory(int dialogId) {
503 return 0;
504 }
505
Amith Yamasanid7993472010-08-18 13:59:28 -0700506 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800507 // mDialogFragment may not be visible yet in parent fragment's onResume().
508 // To be able to dismiss dialog at that time, don't check
509 // mDialogFragment.isVisible().
510 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400511 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700512 }
513 mDialogFragment = null;
514 }
515
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800516 /**
517 * Sets the OnCancelListener of the dialog shown. This method can only be
518 * called after showDialog(int) and before removeDialog(int). The method
519 * does nothing otherwise.
520 */
521 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
522 if (mDialogFragment != null) {
523 mDialogFragment.mOnCancelListener = listener;
524 }
525 }
526
527 /**
528 * Sets the OnDismissListener of the dialog shown. This method can only be
529 * called after showDialog(int) and before removeDialog(int). The method
530 * does nothing otherwise.
531 */
532 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
533 if (mDialogFragment != null) {
534 mDialogFragment.mOnDismissListener = listener;
535 }
536 }
537
Amith Yamasanic861cf82012-10-02 14:51:46 -0700538 public void onDialogShowing() {
539 // override in subclass to attach a dismiss listener, for instance
540 }
541
Jason Monk39b46742015-09-10 15:52:51 -0400542 @Override
543 public void onDisplayPreferenceDialog(Preference preference) {
544 if (preference.getKey() == null) {
545 // Auto-key preferences that don't have a key, so the dialog can find them.
546 preference.setKey(UUID.randomUUID().toString());
547 }
548 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000549 if (preference instanceof RestrictedListPreference) {
550 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
551 .newInstance(preference.getKey());
552 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400553 f = CustomListPreference.CustomListPreferenceDialogFragment
554 .newInstance(preference.getKey());
tmfang27c84de2018-06-28 11:39:05 +0800555 } else if (preference instanceof CustomDialogPreferenceCompat) {
556 f = CustomDialogPreferenceCompat.CustomPreferenceDialogFragment
Jason Monk39b46742015-09-10 15:52:51 -0400557 .newInstance(preference.getKey());
tmfang27c84de2018-06-28 11:39:05 +0800558 } else if (preference instanceof CustomEditTextPreferenceCompat) {
559 f = CustomEditTextPreferenceCompat.CustomPreferenceDialogFragment
Jason Monk39b46742015-09-10 15:52:51 -0400560 .newInstance(preference.getKey());
561 } else {
562 super.onDisplayPreferenceDialog(preference);
563 return;
564 }
565 f.setTargetFragment(this, 0);
566 f.show(getFragmentManager(), "dialog_preference");
567 onDialogShowing();
568 }
569
Fan Zhangd65184f2016-09-19 17:45:24 -0700570 public static class SettingsDialogFragment extends InstrumentedDialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800571 private static final String KEY_DIALOG_ID = "key_dialog_id";
572 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
573
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800574 private Fragment mParentFragment;
575
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800576 private DialogInterface.OnCancelListener mOnCancelListener;
577 private DialogInterface.OnDismissListener mOnDismissListener;
578
tmfangd5405cf2018-10-05 18:45:07 +0800579 public static SettingsDialogFragment newInstance(DialogCreatable fragment, int dialogId) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800580 if (!(fragment instanceof Fragment)) {
581 throw new IllegalArgumentException("fragment argument must be an instance of "
582 + Fragment.class.getName());
583 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800584
tmfangd5405cf2018-10-05 18:45:07 +0800585 final SettingsDialogFragment settingsDialogFragment = new SettingsDialogFragment();
586 settingsDialogFragment.setParentFragment(fragment);
587 settingsDialogFragment.setDialogId(dialogId);
588
589 return settingsDialogFragment;
590 }
Fan Zhangd65184f2016-09-19 17:45:24 -0700591
592 @Override
593 public int getMetricsCategory() {
tmfangd5405cf2018-10-05 18:45:07 +0800594 if (mParentFragment == null) {
Fan Zhang4fe7c082016-10-03 13:48:55 -0700595 return Instrumentable.METRICS_CATEGORY_UNKNOWN;
596 }
tmfangd5405cf2018-10-05 18:45:07 +0800597 final int metricsCategory =
598 ((DialogCreatable) mParentFragment).getDialogMetricsCategory(mDialogId);
Fan Zhangd65184f2016-09-19 17:45:24 -0700599 if (metricsCategory <= 0) {
600 throw new IllegalStateException("Dialog must provide a metrics category");
601 }
602 return metricsCategory;
603 }
604
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800605 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800606 public void onSaveInstanceState(Bundle outState) {
607 super.onSaveInstanceState(outState);
608 if (mParentFragment != null) {
609 outState.putInt(KEY_DIALOG_ID, mDialogId);
610 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
611 }
612 }
613
614 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700615 public void onStart() {
616 super.onStart();
617
618 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
619 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
620 }
621 }
622
623 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800624 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800625 if (savedInstanceState != null) {
626 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800627 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800628 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700629 if (mParentFragment == null) {
630 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
631 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800632 if (!(mParentFragment instanceof DialogCreatable)) {
633 throw new IllegalArgumentException(
634 (mParentFragment != null
635 ? mParentFragment.getClass().getName()
636 : mParentFragmentId)
637 + " must implement "
638 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800639 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800640 // This dialog fragment could be created from non-SettingsPreferenceFragment
641 if (mParentFragment instanceof SettingsPreferenceFragment) {
642 // restore mDialogFragment in mParentFragment
643 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
644 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800645 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800646 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700647 }
648
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800649 @Override
650 public void onCancel(DialogInterface dialog) {
651 super.onCancel(dialog);
652 if (mOnCancelListener != null) {
653 mOnCancelListener.onCancel(dialog);
654 }
655 }
656
657 @Override
658 public void onDismiss(DialogInterface dialog) {
659 super.onDismiss(dialog);
660 if (mOnDismissListener != null) {
661 mOnDismissListener.onDismiss(dialog);
662 }
663 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800664
Amith Yamasanid7993472010-08-18 13:59:28 -0700665 public int getDialogId() {
666 return mDialogId;
667 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800668
669 @Override
670 public void onDetach() {
671 super.onDetach();
672
Amith Yamasani8875ede2011-01-31 12:46:57 -0800673 // This dialog fragment could be created from non-SettingsPreferenceFragment
674 if (mParentFragment instanceof SettingsPreferenceFragment) {
675 // in case the dialog is not explicitly removed by removeDialog()
676 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
677 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
678 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800679 }
680 }
tmfangd5405cf2018-10-05 18:45:07 +0800681
682 private void setParentFragment(DialogCreatable fragment) {
683 mParentFragment = (Fragment) fragment;
684 }
685
686 private void setDialogId(int dialogId) {
687 mDialogId = dialogId;
688 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700689 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700690
691 protected boolean hasNextButton() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800692 return ((ButtonBarHandler) getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700693 }
694
695 protected Button getNextButton() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800696 return ((ButtonBarHandler) getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700697 }
698
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700699 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700700 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400701 if (activity == null) return;
702 if (getFragmentManager().getBackStackEntryCount() > 0) {
703 getFragmentManager().popBackStack();
704 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800705 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700706 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700707 }
708
Jason Monkb7e43802016-06-06 16:01:58 -0400709 protected Intent getIntent() {
710 if (getActivity() == null) {
711 return null;
712 }
713 return getActivity().getIntent();
714 }
715
716 protected void setResult(int result, Intent intent) {
717 if (getActivity() == null) {
718 return;
719 }
720 getActivity().setResult(result, intent);
721 }
722
723 protected void setResult(int result) {
724 if (getActivity() == null) {
725 return;
726 }
727 getActivity().setResult(result);
728 }
Arc Wang04854f82020-03-20 13:34:33 +0800729
730 protected boolean isFinishingOrDestroyed() {
731 final Activity activity = getActivity();
732 return activity == null || activity.isFinishing() || activity.isDestroyed();
733 }
Jonathan Scottb7f4f562022-04-11 09:49:17 +0000734
735 protected void replaceEnterprisePreferenceScreenTitle(String overrideKey, int resource) {
736 getActivity().setTitle(mDevicePolicyManager.getResources().getString(
737 overrideKey, () -> getString(resource)));
738 }
739
740 protected void replaceEnterpriseStringSummary(
741 String preferenceKey, String overrideKey, int resource) {
742 Preference preference = findPreference(preferenceKey);
743 if (preference == null) {
744 Log.d(TAG, "Could not find enterprise preference " + preferenceKey);
745 return;
746 }
747
748 preference.setSummary(
749 mDevicePolicyManager.getResources().getString(overrideKey,
750 () -> getString(resource)));
751 }
752
753 protected void replaceEnterpriseStringTitle(
754 String preferenceKey, String overrideKey, int resource) {
755 Preference preference = findPreference(preferenceKey);
756 if (preference == null) {
757 Log.d(TAG, "Could not find enterprise preference " + preferenceKey);
758 return;
759 }
760
761 preference.setTitle(
762 mDevicePolicyManager.getResources().getString(overrideKey,
763 () -> getString(resource)));
764 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700765}