blob: 1d6a48d04652dff046fd033ed6e553d47f5665b5 [file] [log] [blame]
Amith Yamasanid7993472010-08-18 13:59:28 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -070019import android.app.Activity;
Amith Yamasanid7993472010-08-18 13:59:28 -070020import android.app.Dialog;
Amith Yamasanid7993472010-08-18 13:59:28 -070021import android.content.ContentResolver;
Amith Yamasani350938e2013-04-09 10:22:47 -070022import android.content.Context;
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +080023import android.content.DialogInterface;
Jason Monkb7e43802016-06-06 16:01:58 -040024import android.content.Intent;
Amith Yamasanid7993472010-08-18 13:59:28 -070025import android.content.pm.PackageManager;
Amith Yamasanid7993472010-08-18 13:59:28 -070026import android.os.Bundle;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070027import android.text.TextUtils;
Jason Monk2071eda2016-02-25 13:55:48 -050028import android.util.ArrayMap;
Amith Yamasanid7993472010-08-18 13:59:28 -070029import android.util.Log;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070030import android.view.LayoutInflater;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070031import android.view.View;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070032import android.view.ViewGroup;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070033import android.widget.Button;
Jason Monkb7e43802016-06-06 16:01:58 -040034
Fan Zhang23f8d592018-08-28 15:11:40 -070035import androidx.annotation.VisibleForTesting;
36import androidx.annotation.XmlRes;
37import androidx.fragment.app.DialogFragment;
38import androidx.fragment.app.Fragment;
39import androidx.preference.Preference;
40import androidx.preference.PreferenceGroup;
41import androidx.preference.PreferenceScreen;
42import androidx.recyclerview.widget.LinearLayoutManager;
43import androidx.recyclerview.widget.RecyclerView;
44
Fan Zhang2d0b3442016-12-05 17:02:33 -080045import com.android.settings.core.InstrumentedPreferenceFragment;
Fan Zhangd65184f2016-09-19 17:45:24 -070046import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
Fan Zhange0b0e9f2017-11-29 14:55:59 -080047import com.android.settings.support.actionbar.HelpResourceProvider;
Fan Zhang3d516e72018-01-31 14:14:41 -080048import com.android.settings.widget.HighlightablePreferenceGroupAdapter;
Fan Zhang896f1b32017-06-26 14:22:45 -070049import com.android.settings.widget.LoadingViewController;
tmfang27c84de2018-06-28 11:39:05 +080050import com.android.settingslib.CustomDialogPreferenceCompat;
51import com.android.settingslib.CustomEditTextPreferenceCompat;
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -080052import com.android.settingslib.core.instrumentation.Instrumentable;
Raff Tsai966fa012019-09-25 11:19:06 +080053import com.android.settingslib.search.Indexable;
tmfangdce94bb2018-11-26 18:41:01 +080054import com.android.settingslib.widget.LayoutPreference;
John Spurlockb8e02b82015-04-15 21:15:55 -040055
Tsung-Mao Fangef877552021-02-24 16:37:39 +080056import com.google.android.material.appbar.AppBarLayout;
57
Jason Monk39b46742015-09-10 15:52:51 -040058import java.util.UUID;
59
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070060/**
Amith Yamasanid7993472010-08-18 13:59:28 -070061 * Base class for Settings fragments, with some helper functions and dialog management.
62 */
Fan Zhang2d0b3442016-12-05 17:02:33 -080063public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceFragment
Fan Zhang78ea7da2018-07-02 13:44:57 -070064 implements DialogCreatable, HelpResourceProvider, Indexable {
Anna Galusza0285c802016-01-29 17:32:19 -080065
Jason Monk65bb0972015-12-17 10:39:44 -050066 private static final String TAG = "SettingsPreference";
Amith Yamasanid7993472010-08-18 13:59:28 -070067
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070068 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070069
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000070 private static final int ORDER_FIRST = -1;
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000071
Fan Zhange0b0e9f2017-11-29 14:55:59 -080072 private SettingsDialogFragment mDialogFragment;
Amith Yamasani350938e2013-04-09 10:22:47 -070073 // Cache the content resolver for async callbacks
74 private ContentResolver mContentResolver;
75
Jason Monk39b46742015-09-10 15:52:51 -040076 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070077 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040078 private RecyclerView.AdapterDataObserver mDataSetObserver =
79 new RecyclerView.AdapterDataObserver() {
Tony Mantler0b825f52016-09-27 14:48:16 -070080 @Override
81 public void onChanged() {
82 onDataSetChanged();
83 }
84
85 @Override
86 public void onItemRangeChanged(int positionStart, int itemCount) {
87 onDataSetChanged();
88 }
89
90 @Override
91 public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
92 onDataSetChanged();
93 }
94
95 @Override
96 public void onItemRangeInserted(int positionStart, int itemCount) {
97 onDataSetChanged();
98 }
99
100 @Override
101 public void onItemRangeRemoved(int positionStart, int itemCount) {
102 onDataSetChanged();
103 }
104
105 @Override
106 public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
107 onDataSetChanged();
108 }
109 };
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700110
Jason Chiucfa36db2019-06-05 17:31:31 +0800111 @VisibleForTesting
112 ViewGroup mPinnedHeaderFrameLayout;
Tsung-Mao Fangef877552021-02-24 16:37:39 +0800113 private AppBarLayout mAppBarLayout;
Jason Monk39b46742015-09-10 15:52:51 -0400114 private LayoutPreference mHeader;
Jason Monk39b46742015-09-10 15:52:51 -0400115 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -0500116 private LinearLayoutManager mLayoutManager;
Jason Monk2071eda2016-02-25 13:55:48 -0500117 private ArrayMap<String, Preference> mPreferenceCache;
Jason Monkf38fb382016-03-18 14:23:01 -0400118 private boolean mAnimationAllowed;
Jason Monk39b46742015-09-10 15:52:51 -0400119
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700120 @VisibleForTesting
121 public HighlightablePreferenceGroupAdapter mAdapter;
Jason Chiu29a09e52021-10-12 17:24:34 +0800122 private boolean mPreferenceHighlighted = false;
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700123
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700124 @Override
125 public void onCreate(Bundle icicle) {
126 super.onCreate(icicle);
127
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700128 if (icicle != null) {
129 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
130 }
Fan Zhang72456a92018-02-20 11:25:56 -0800131 HighlightablePreferenceGroupAdapter.adjustInitialExpandedChildCount(this /* host */);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700132 }
133
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700134 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700135 public View onCreateView(LayoutInflater inflater, ViewGroup container,
136 Bundle savedInstanceState) {
137 final View root = super.onCreateView(inflater, container, savedInstanceState);
Fan Zhange0b0e9f2017-11-29 14:55:59 -0800138 mPinnedHeaderFrameLayout = root.findViewById(R.id.pinned_header);
Tsung-Mao Fangef877552021-02-24 16:37:39 +0800139 mAppBarLayout = getActivity().findViewById(R.id.app_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700140 return root;
141 }
142
Jason Monk39b46742015-09-10 15:52:51 -0400143 @Override
Jason Monk91e2f892016-02-23 15:31:09 -0500144 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
145 super.addPreferencesFromResource(preferencesResId);
146 checkAvailablePrefs(getPreferenceScreen());
147 }
148
Doris Lingfe525942018-11-27 14:58:55 -0800149 @VisibleForTesting
150 void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
Jason Monk91e2f892016-02-23 15:31:09 -0500151 if (preferenceGroup == null) return;
152 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
153 Preference pref = preferenceGroup.getPreference(i);
154 if (pref instanceof SelfAvailablePreference
155 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
Doris Lingfe525942018-11-27 14:58:55 -0800156 pref.setVisible(false);
Jason Monk91e2f892016-02-23 15:31:09 -0500157 } else if (pref instanceof PreferenceGroup) {
158 checkAvailablePrefs((PreferenceGroup) pref);
159 }
160 }
161 }
162
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700163 public View setPinnedHeaderView(int layoutResId) {
164 final LayoutInflater inflater = getActivity().getLayoutInflater();
165 final View pinnedHeader =
166 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
167 setPinnedHeaderView(pinnedHeader);
168 return pinnedHeader;
169 }
170
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700171 public void setPinnedHeaderView(View pinnedHeader) {
172 mPinnedHeaderFrameLayout.addView(pinnedHeader);
173 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
174 }
175
Jason Chiucfa36db2019-06-05 17:31:31 +0800176 public void showPinnedHeader(boolean show) {
177 mPinnedHeaderFrameLayout.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
178 }
179
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700180 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700181 public void onSaveInstanceState(Bundle outState) {
182 super.onSaveInstanceState(outState);
183
Fan Zhang3d516e72018-01-31 14:14:41 -0800184 if (mAdapter != null) {
185 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mAdapter.isHighlightRequested());
186 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700187 }
188
189 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700190 public void onActivityCreated(Bundle savedInstanceState) {
191 super.onActivityCreated(savedInstanceState);
Johan Redestig76218e52016-04-19 08:29:30 +0200192 setHasOptionsMenu(true);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700193 }
194
195 @Override
196 public void onResume() {
197 super.onResume();
Fan Zhang3d516e72018-01-31 14:14:41 -0800198 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700199 }
200
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700201 @Override
202 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700203 registerObserverIfNeeded();
204 }
205
206 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700207 protected void onUnbindPreferences() {
208 unregisterObserverIfNeeded();
209 }
210
Jason Monkb37e2882016-01-11 14:27:20 -0500211 public void setLoading(boolean loading, boolean animate) {
Fan Zhang896f1b32017-06-26 14:22:45 -0700212 View loadingContainer = getView().findViewById(R.id.loading_container);
213 LoadingViewController.handleLoadingContainer(loadingContainer, getListView(),
214 !loading /* done */,
215 animate);
Jason Monkb37e2882016-01-11 14:27:20 -0500216 }
217
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700218 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700219 if (!mIsDataSetObserverRegistered) {
220 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400221 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700222 }
Jason Monk39b46742015-09-10 15:52:51 -0400223 mCurrentRootAdapter = getListView().getAdapter();
224 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700225 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500226 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700227 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700228 }
229
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700230 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700231 if (mIsDataSetObserverRegistered) {
232 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400233 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700234 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700235 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700236 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700237 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700238 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700239
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700240 public void highlightPreferenceIfNeeded() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800241 if (!isAdded()) {
242 return;
243 }
244 if (mAdapter != null) {
Tsung-Mao Fangef877552021-02-24 16:37:39 +0800245 mAdapter.requestHighlight(getView(), getListView(), mAppBarLayout);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700246 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700247 }
248
Fan Zhang72456a92018-02-20 11:25:56 -0800249 /**
250 * Returns initial expanded child count.
251 * <p/>
252 * Only override this method if the initial expanded child must be determined at run time.
253 */
254 public int getInitialExpandedChildCount() {
255 return 0;
256 }
257
Bonian Chen1b9bda32020-02-04 10:27:50 +0800258 /**
259 * Whether preference is allowing to be displayed to the user.
260 *
261 * @param preference to check if it can be displayed to the user (not hidding in expand area).
262 * @return {@code true} when preference is allowing to be displayed to the user.
263 * {@code false} when preference is hidden in expand area and not been displayed to the user.
264 */
265 protected boolean isPreferenceExpanded(Preference preference) {
266 return ((mAdapter == null)
267 || (mAdapter.getPreferenceAdapterPosition(preference) != RecyclerView.NO_POSITION));
268 }
269
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000270 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400271 highlightPreferenceIfNeeded();
272 updateEmptyView();
273 }
274
Jason Monk39b46742015-09-10 15:52:51 -0400275 public LayoutPreference getHeaderView() {
276 return mHeader;
277 }
278
Jason Monk39b46742015-09-10 15:52:51 -0400279 protected void setHeaderView(int resource) {
280 mHeader = new LayoutPreference(getPrefContext(), resource);
Yanting Yang70a8e312019-05-21 21:05:32 +0800281 mHeader.setSelectable(false);
Udam Sainid553abc2016-02-16 17:54:13 -0800282 addPreferenceToTop(mHeader);
283 }
284
285 protected void setHeaderView(View view) {
286 mHeader = new LayoutPreference(getPrefContext(), view);
Yanting Yang70a8e312019-05-21 21:05:32 +0800287 mHeader.setSelectable(false);
Udam Sainid553abc2016-02-16 17:54:13 -0800288 addPreferenceToTop(mHeader);
289 }
290
291 private void addPreferenceToTop(LayoutPreference preference) {
292 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400293 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800294 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400295 }
296 }
297
Jason Monk39b46742015-09-10 15:52:51 -0400298 @Override
299 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400300 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400301 // Without ids generated, the RecyclerView won't animate changes to the preferences.
302 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
303 }
Jason Monk39b46742015-09-10 15:52:51 -0400304 super.setPreferenceScreen(preferenceScreen);
305 if (preferenceScreen != null) {
306 if (mHeader != null) {
307 preferenceScreen.addPreference(mHeader);
308 }
Jason Monk39b46742015-09-10 15:52:51 -0400309 }
310 }
311
jackqdyulei2b2abac2017-05-26 10:47:55 -0700312 @VisibleForTesting
313 void updateEmptyView() {
Jason Monk39b46742015-09-10 15:52:51 -0400314 if (mEmptyView == null) return;
315 if (getPreferenceScreen() != null) {
jackqdyulei2b2abac2017-05-26 10:47:55 -0700316 final View listContainer = getActivity().findViewById(android.R.id.list_container);
Jason Monk39b46742015-09-10 15:52:51 -0400317 boolean show = (getPreferenceScreen().getPreferenceCount()
Sunny Shaobf434972019-08-29 14:28:13 +0800318 - (mHeader != null ? 1 : 0)) <= 0
jackqdyulei2b2abac2017-05-26 10:47:55 -0700319 || (listContainer != null && listContainer.getVisibility() != View.VISIBLE);
Jason Monk39b46742015-09-10 15:52:51 -0400320 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
321 } else {
322 mEmptyView.setVisibility(View.VISIBLE);
323 }
324 }
325
326 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000327 if (mEmptyView != null) {
328 mEmptyView.setVisibility(View.GONE);
329 }
Jason Monk39b46742015-09-10 15:52:51 -0400330 mEmptyView = v;
331 updateEmptyView();
332 }
333
334 public View getEmptyView() {
335 return mEmptyView;
336 }
337
Jason Monk65bb0972015-12-17 10:39:44 -0500338 @Override
339 public RecyclerView.LayoutManager onCreateLayoutManager() {
340 mLayoutManager = new LinearLayoutManager(getContext());
341 return mLayoutManager;
342 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700343
Jason Monk65bb0972015-12-17 10:39:44 -0500344 @Override
345 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
Fan Zhanga1000462018-02-02 12:15:37 -0800346 final Bundle arguments = getArguments();
Fan Zhang3d516e72018-01-31 14:14:41 -0800347 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen,
Fan Zhanga1000462018-02-02 12:15:37 -0800348 arguments == null
349 ? null : arguments.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY),
Fan Zhang3d516e72018-01-31 14:14:41 -0800350 mPreferenceHighlighted);
Jason Monk65bb0972015-12-17 10:39:44 -0500351 return mAdapter;
352 }
353
Jason Monkf38fb382016-03-18 14:23:01 -0400354 protected void setAnimationAllowed(boolean animationAllowed) {
355 mAnimationAllowed = animationAllowed;
356 }
357
Jason Monk2071eda2016-02-25 13:55:48 -0500358 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
Fan Zhang3d516e72018-01-31 14:14:41 -0800359 mPreferenceCache = new ArrayMap<>();
Jason Monk2071eda2016-02-25 13:55:48 -0500360 final int N = group.getPreferenceCount();
361 for (int i = 0; i < N; i++) {
362 Preference p = group.getPreference(i);
363 if (TextUtils.isEmpty(p.getKey())) {
364 continue;
365 }
366 mPreferenceCache.put(p.getKey(), p);
367 }
368 }
369
370 protected Preference getCachedPreference(String key) {
371 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
372 }
373
374 protected void removeCachedPrefs(PreferenceGroup group) {
375 for (Preference p : mPreferenceCache.values()) {
376 group.removePreference(p);
377 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400378 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500379 }
380
Jason Monka6278442016-04-21 10:12:30 -0400381 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400382 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400383 }
384
Jan Nordqvist9eb43dd2018-03-26 15:29:44 -0700385 @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
386 public boolean removePreference(String key) {
Fan Zhange84407f2017-05-24 11:19:52 -0700387 return removePreference(getPreferenceScreen(), key);
388 }
389
390 @VisibleForTesting
391 boolean removePreference(PreferenceGroup group, String key) {
392 final int preferenceCount = group.getPreferenceCount();
393 for (int i = 0; i < preferenceCount; i++) {
394 final Preference preference = group.getPreference(i);
395 final String curKey = preference.getKey();
396
397 if (TextUtils.equals(curKey, key)) {
398 return group.removePreference(preference);
399 }
400
401 if (preference instanceof PreferenceGroup) {
402 if (removePreference((PreferenceGroup) preference, key)) {
403 return true;
404 }
405 }
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700406 }
Fan Zhange84407f2017-05-24 11:19:52 -0700407 return false;
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700408 }
409
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700410 /*
411 * The name is intentionally made different from Activity#finish(), so that
412 * users won't misunderstand its meaning.
413 */
414 public final void finishFragment() {
415 getActivity().onBackPressed();
416 }
417
Amith Yamasanid7993472010-08-18 13:59:28 -0700418 // Some helpers for functions used by the settings fragments when they were activities
419
420 /**
421 * Returns the ContentResolver from the owning Activity.
422 */
423 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700424 Context context = getActivity();
425 if (context != null) {
426 mContentResolver = context.getContentResolver();
427 }
428 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700429 }
430
431 /**
432 * Returns the specified system service from the owning Activity.
433 */
434 protected Object getSystemService(final String name) {
435 return getActivity().getSystemService(name);
436 }
437
438 /**
Bonian Chen49cdb602021-03-17 13:32:32 +0800439 * Returns the specified system service from the owning Activity.
440 */
441 protected <T> T getSystemService(final Class<T> serviceClass) {
442 return getActivity().getSystemService(serviceClass);
443 }
444
445 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700446 * Returns the PackageManager from the owning Activity.
447 */
448 protected PackageManager getPackageManager() {
449 return getActivity().getPackageManager();
450 }
451
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800452 @Override
453 public void onDetach() {
454 if (isRemoving()) {
455 if (mDialogFragment != null) {
456 mDialogFragment.dismiss();
457 mDialogFragment = null;
458 }
459 }
460 super.onDetach();
461 }
462
Amith Yamasanid7993472010-08-18 13:59:28 -0700463 // Dialog management
464
465 protected void showDialog(int dialogId) {
466 if (mDialogFragment != null) {
467 Log.e(TAG, "Old dialog fragment not null!");
468 }
tmfangd5405cf2018-10-05 18:45:07 +0800469 mDialogFragment = SettingsDialogFragment.newInstance(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800470 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700471 }
472
Fan Zhangd65184f2016-09-19 17:45:24 -0700473 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700474 public Dialog onCreateDialog(int dialogId) {
475 return null;
476 }
477
Fan Zhangd65184f2016-09-19 17:45:24 -0700478 @Override
479 public int getDialogMetricsCategory(int dialogId) {
480 return 0;
481 }
482
Amith Yamasanid7993472010-08-18 13:59:28 -0700483 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800484 // mDialogFragment may not be visible yet in parent fragment's onResume().
485 // To be able to dismiss dialog at that time, don't check
486 // mDialogFragment.isVisible().
487 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400488 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700489 }
490 mDialogFragment = null;
491 }
492
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800493 /**
494 * Sets the OnCancelListener of the dialog shown. This method can only be
495 * called after showDialog(int) and before removeDialog(int). The method
496 * does nothing otherwise.
497 */
498 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
499 if (mDialogFragment != null) {
500 mDialogFragment.mOnCancelListener = listener;
501 }
502 }
503
504 /**
505 * Sets the OnDismissListener of the dialog shown. This method can only be
506 * called after showDialog(int) and before removeDialog(int). The method
507 * does nothing otherwise.
508 */
509 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
510 if (mDialogFragment != null) {
511 mDialogFragment.mOnDismissListener = listener;
512 }
513 }
514
Amith Yamasanic861cf82012-10-02 14:51:46 -0700515 public void onDialogShowing() {
516 // override in subclass to attach a dismiss listener, for instance
517 }
518
Jason Monk39b46742015-09-10 15:52:51 -0400519 @Override
520 public void onDisplayPreferenceDialog(Preference preference) {
521 if (preference.getKey() == null) {
522 // Auto-key preferences that don't have a key, so the dialog can find them.
523 preference.setKey(UUID.randomUUID().toString());
524 }
525 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000526 if (preference instanceof RestrictedListPreference) {
527 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
528 .newInstance(preference.getKey());
529 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400530 f = CustomListPreference.CustomListPreferenceDialogFragment
531 .newInstance(preference.getKey());
tmfang27c84de2018-06-28 11:39:05 +0800532 } else if (preference instanceof CustomDialogPreferenceCompat) {
533 f = CustomDialogPreferenceCompat.CustomPreferenceDialogFragment
Jason Monk39b46742015-09-10 15:52:51 -0400534 .newInstance(preference.getKey());
tmfang27c84de2018-06-28 11:39:05 +0800535 } else if (preference instanceof CustomEditTextPreferenceCompat) {
536 f = CustomEditTextPreferenceCompat.CustomPreferenceDialogFragment
Jason Monk39b46742015-09-10 15:52:51 -0400537 .newInstance(preference.getKey());
538 } else {
539 super.onDisplayPreferenceDialog(preference);
540 return;
541 }
542 f.setTargetFragment(this, 0);
543 f.show(getFragmentManager(), "dialog_preference");
544 onDialogShowing();
545 }
546
Fan Zhangd65184f2016-09-19 17:45:24 -0700547 public static class SettingsDialogFragment extends InstrumentedDialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800548 private static final String KEY_DIALOG_ID = "key_dialog_id";
549 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
550
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800551 private Fragment mParentFragment;
552
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800553 private DialogInterface.OnCancelListener mOnCancelListener;
554 private DialogInterface.OnDismissListener mOnDismissListener;
555
tmfangd5405cf2018-10-05 18:45:07 +0800556 public static SettingsDialogFragment newInstance(DialogCreatable fragment, int dialogId) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800557 if (!(fragment instanceof Fragment)) {
558 throw new IllegalArgumentException("fragment argument must be an instance of "
559 + Fragment.class.getName());
560 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800561
tmfangd5405cf2018-10-05 18:45:07 +0800562 final SettingsDialogFragment settingsDialogFragment = new SettingsDialogFragment();
563 settingsDialogFragment.setParentFragment(fragment);
564 settingsDialogFragment.setDialogId(dialogId);
565
566 return settingsDialogFragment;
567 }
Fan Zhangd65184f2016-09-19 17:45:24 -0700568
569 @Override
570 public int getMetricsCategory() {
tmfangd5405cf2018-10-05 18:45:07 +0800571 if (mParentFragment == null) {
Fan Zhang4fe7c082016-10-03 13:48:55 -0700572 return Instrumentable.METRICS_CATEGORY_UNKNOWN;
573 }
tmfangd5405cf2018-10-05 18:45:07 +0800574 final int metricsCategory =
575 ((DialogCreatable) mParentFragment).getDialogMetricsCategory(mDialogId);
Fan Zhangd65184f2016-09-19 17:45:24 -0700576 if (metricsCategory <= 0) {
577 throw new IllegalStateException("Dialog must provide a metrics category");
578 }
579 return metricsCategory;
580 }
581
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800582 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800583 public void onSaveInstanceState(Bundle outState) {
584 super.onSaveInstanceState(outState);
585 if (mParentFragment != null) {
586 outState.putInt(KEY_DIALOG_ID, mDialogId);
587 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
588 }
589 }
590
591 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700592 public void onStart() {
593 super.onStart();
594
595 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
596 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
597 }
598 }
599
600 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800601 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800602 if (savedInstanceState != null) {
603 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800604 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800605 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700606 if (mParentFragment == null) {
607 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
608 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800609 if (!(mParentFragment instanceof DialogCreatable)) {
610 throw new IllegalArgumentException(
611 (mParentFragment != null
612 ? mParentFragment.getClass().getName()
613 : mParentFragmentId)
614 + " must implement "
615 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800616 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800617 // This dialog fragment could be created from non-SettingsPreferenceFragment
618 if (mParentFragment instanceof SettingsPreferenceFragment) {
619 // restore mDialogFragment in mParentFragment
620 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
621 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800622 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800623 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700624 }
625
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800626 @Override
627 public void onCancel(DialogInterface dialog) {
628 super.onCancel(dialog);
629 if (mOnCancelListener != null) {
630 mOnCancelListener.onCancel(dialog);
631 }
632 }
633
634 @Override
635 public void onDismiss(DialogInterface dialog) {
636 super.onDismiss(dialog);
637 if (mOnDismissListener != null) {
638 mOnDismissListener.onDismiss(dialog);
639 }
640 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800641
Amith Yamasanid7993472010-08-18 13:59:28 -0700642 public int getDialogId() {
643 return mDialogId;
644 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800645
646 @Override
647 public void onDetach() {
648 super.onDetach();
649
Amith Yamasani8875ede2011-01-31 12:46:57 -0800650 // This dialog fragment could be created from non-SettingsPreferenceFragment
651 if (mParentFragment instanceof SettingsPreferenceFragment) {
652 // in case the dialog is not explicitly removed by removeDialog()
653 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
654 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
655 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800656 }
657 }
tmfangd5405cf2018-10-05 18:45:07 +0800658
659 private void setParentFragment(DialogCreatable fragment) {
660 mParentFragment = (Fragment) fragment;
661 }
662
663 private void setDialogId(int dialogId) {
664 mDialogId = dialogId;
665 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700666 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700667
668 protected boolean hasNextButton() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800669 return ((ButtonBarHandler) getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700670 }
671
672 protected Button getNextButton() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800673 return ((ButtonBarHandler) getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700674 }
675
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700676 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700677 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400678 if (activity == null) return;
679 if (getFragmentManager().getBackStackEntryCount() > 0) {
680 getFragmentManager().popBackStack();
681 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800682 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700683 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700684 }
685
Jason Monkb7e43802016-06-06 16:01:58 -0400686 protected Intent getIntent() {
687 if (getActivity() == null) {
688 return null;
689 }
690 return getActivity().getIntent();
691 }
692
693 protected void setResult(int result, Intent intent) {
694 if (getActivity() == null) {
695 return;
696 }
697 getActivity().setResult(result, intent);
698 }
699
700 protected void setResult(int result) {
701 if (getActivity() == null) {
702 return;
703 }
704 getActivity().setResult(result);
705 }
Arc Wang04854f82020-03-20 13:34:33 +0800706
707 protected boolean isFinishingOrDestroyed() {
708 final Activity activity = getActivity();
709 return activity == null || activity.isFinishing() || activity.isDestroyed();
710 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700711}