blob: 659ada4bd40a8d695138a81085f5032b965e92de [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 Zhang681a4cd2017-11-29 16:57:19 -080047import com.android.settings.search.actionbar.SearchMenuController;
Fan Zhange0b0e9f2017-11-29 14:55:59 -080048import com.android.settings.support.actionbar.HelpMenuController;
49import com.android.settings.support.actionbar.HelpResourceProvider;
Fan Zhang3d516e72018-01-31 14:14:41 -080050import com.android.settings.widget.HighlightablePreferenceGroupAdapter;
Fan Zhang896f1b32017-06-26 14:22:45 -070051import com.android.settings.widget.LoadingViewController;
tmfang27c84de2018-06-28 11:39:05 +080052import com.android.settingslib.CustomDialogPreferenceCompat;
53import com.android.settingslib.CustomEditTextPreferenceCompat;
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -080054import com.android.settingslib.core.instrumentation.Instrumentable;
Raff Tsai966fa012019-09-25 11:19:06 +080055import com.android.settingslib.search.Indexable;
tmfangdce94bb2018-11-26 18:41:01 +080056import com.android.settingslib.widget.LayoutPreference;
John Spurlockb8e02b82015-04-15 21:15:55 -040057
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;
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700113
Jason Monk39b46742015-09-10 15:52:51 -0400114 private LayoutPreference mHeader;
115
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;
123 @VisibleForTesting
124 public boolean mPreferenceHighlighted = false;
125
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700126 @Override
127 public void onCreate(Bundle icicle) {
128 super.onCreate(icicle);
Fan Zhang681a4cd2017-11-29 16:57:19 -0800129 SearchMenuController.init(this /* host */);
Fan Zhange0b0e9f2017-11-29 14:55:59 -0800130 HelpMenuController.init(this /* host */);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700131
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700132 if (icicle != null) {
133 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
134 }
Fan Zhang72456a92018-02-20 11:25:56 -0800135 HighlightablePreferenceGroupAdapter.adjustInitialExpandedChildCount(this /* host */);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700136 }
137
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700138 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700139 public View onCreateView(LayoutInflater inflater, ViewGroup container,
140 Bundle savedInstanceState) {
141 final View root = super.onCreateView(inflater, container, savedInstanceState);
Fan Zhange0b0e9f2017-11-29 14:55:59 -0800142 mPinnedHeaderFrameLayout = root.findViewById(R.id.pinned_header);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700143 return root;
144 }
145
Jason Monk39b46742015-09-10 15:52:51 -0400146 @Override
Jason Monk91e2f892016-02-23 15:31:09 -0500147 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
148 super.addPreferencesFromResource(preferencesResId);
149 checkAvailablePrefs(getPreferenceScreen());
150 }
151
Doris Lingfe525942018-11-27 14:58:55 -0800152 @VisibleForTesting
153 void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
Jason Monk91e2f892016-02-23 15:31:09 -0500154 if (preferenceGroup == null) return;
155 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
156 Preference pref = preferenceGroup.getPreference(i);
157 if (pref instanceof SelfAvailablePreference
158 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
Doris Lingfe525942018-11-27 14:58:55 -0800159 pref.setVisible(false);
Jason Monk91e2f892016-02-23 15:31:09 -0500160 } else if (pref instanceof PreferenceGroup) {
161 checkAvailablePrefs((PreferenceGroup) pref);
162 }
163 }
164 }
165
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700166 public View setPinnedHeaderView(int layoutResId) {
167 final LayoutInflater inflater = getActivity().getLayoutInflater();
168 final View pinnedHeader =
169 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
170 setPinnedHeaderView(pinnedHeader);
171 return pinnedHeader;
172 }
173
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700174 public void setPinnedHeaderView(View pinnedHeader) {
175 mPinnedHeaderFrameLayout.addView(pinnedHeader);
176 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
177 }
178
Jason Chiucfa36db2019-06-05 17:31:31 +0800179 public void showPinnedHeader(boolean show) {
180 mPinnedHeaderFrameLayout.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
181 }
182
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700183 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700184 public void onSaveInstanceState(Bundle outState) {
185 super.onSaveInstanceState(outState);
186
Fan Zhang3d516e72018-01-31 14:14:41 -0800187 if (mAdapter != null) {
188 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mAdapter.isHighlightRequested());
189 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700190 }
191
192 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700193 public void onActivityCreated(Bundle savedInstanceState) {
194 super.onActivityCreated(savedInstanceState);
Johan Redestig76218e52016-04-19 08:29:30 +0200195 setHasOptionsMenu(true);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700196 }
197
198 @Override
199 public void onResume() {
200 super.onResume();
Fan Zhang3d516e72018-01-31 14:14:41 -0800201 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700202 }
203
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700204 @Override
205 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700206 registerObserverIfNeeded();
207 }
208
209 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700210 protected void onUnbindPreferences() {
211 unregisterObserverIfNeeded();
212 }
213
Jason Monkb37e2882016-01-11 14:27:20 -0500214 public void setLoading(boolean loading, boolean animate) {
Fan Zhang896f1b32017-06-26 14:22:45 -0700215 View loadingContainer = getView().findViewById(R.id.loading_container);
216 LoadingViewController.handleLoadingContainer(loadingContainer, getListView(),
217 !loading /* done */,
218 animate);
Jason Monkb37e2882016-01-11 14:27:20 -0500219 }
220
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700221 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700222 if (!mIsDataSetObserverRegistered) {
223 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400224 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700225 }
Jason Monk39b46742015-09-10 15:52:51 -0400226 mCurrentRootAdapter = getListView().getAdapter();
227 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700228 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500229 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700230 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700231 }
232
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700233 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700234 if (mIsDataSetObserverRegistered) {
235 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400236 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700237 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700238 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700239 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700240 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700241 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700242
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700243 public void highlightPreferenceIfNeeded() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800244 if (!isAdded()) {
245 return;
246 }
247 if (mAdapter != null) {
248 mAdapter.requestHighlight(getView(), getListView());
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700249 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700250 }
251
Fan Zhang72456a92018-02-20 11:25:56 -0800252 /**
253 * Returns initial expanded child count.
254 * <p/>
255 * Only override this method if the initial expanded child must be determined at run time.
256 */
257 public int getInitialExpandedChildCount() {
258 return 0;
259 }
260
Bonian Chen1b9bda32020-02-04 10:27:50 +0800261 /**
262 * Whether preference is allowing to be displayed to the user.
263 *
264 * @param preference to check if it can be displayed to the user (not hidding in expand area).
265 * @return {@code true} when preference is allowing to be displayed to the user.
266 * {@code false} when preference is hidden in expand area and not been displayed to the user.
267 */
268 protected boolean isPreferenceExpanded(Preference preference) {
269 return ((mAdapter == null)
270 || (mAdapter.getPreferenceAdapterPosition(preference) != RecyclerView.NO_POSITION));
271 }
272
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000273 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400274 highlightPreferenceIfNeeded();
275 updateEmptyView();
276 }
277
Jason Monk39b46742015-09-10 15:52:51 -0400278 public LayoutPreference getHeaderView() {
279 return mHeader;
280 }
281
Jason Monk39b46742015-09-10 15:52:51 -0400282 protected void setHeaderView(int resource) {
283 mHeader = new LayoutPreference(getPrefContext(), resource);
Yanting Yang70a8e312019-05-21 21:05:32 +0800284 mHeader.setSelectable(false);
Udam Sainid553abc2016-02-16 17:54:13 -0800285 addPreferenceToTop(mHeader);
286 }
287
288 protected void setHeaderView(View view) {
289 mHeader = new LayoutPreference(getPrefContext(), view);
Yanting Yang70a8e312019-05-21 21:05:32 +0800290 mHeader.setSelectable(false);
Udam Sainid553abc2016-02-16 17:54:13 -0800291 addPreferenceToTop(mHeader);
292 }
293
294 private void addPreferenceToTop(LayoutPreference preference) {
295 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400296 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800297 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400298 }
299 }
300
Jason Monk39b46742015-09-10 15:52:51 -0400301 @Override
302 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400303 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400304 // Without ids generated, the RecyclerView won't animate changes to the preferences.
305 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
306 }
Jason Monk39b46742015-09-10 15:52:51 -0400307 super.setPreferenceScreen(preferenceScreen);
308 if (preferenceScreen != null) {
309 if (mHeader != null) {
310 preferenceScreen.addPreference(mHeader);
311 }
Jason Monk39b46742015-09-10 15:52:51 -0400312 }
313 }
314
jackqdyulei2b2abac2017-05-26 10:47:55 -0700315 @VisibleForTesting
316 void updateEmptyView() {
Jason Monk39b46742015-09-10 15:52:51 -0400317 if (mEmptyView == null) return;
318 if (getPreferenceScreen() != null) {
jackqdyulei2b2abac2017-05-26 10:47:55 -0700319 final View listContainer = getActivity().findViewById(android.R.id.list_container);
Jason Monk39b46742015-09-10 15:52:51 -0400320 boolean show = (getPreferenceScreen().getPreferenceCount()
Sunny Shaobf434972019-08-29 14:28:13 +0800321 - (mHeader != null ? 1 : 0)) <= 0
jackqdyulei2b2abac2017-05-26 10:47:55 -0700322 || (listContainer != null && listContainer.getVisibility() != View.VISIBLE);
Jason Monk39b46742015-09-10 15:52:51 -0400323 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
324 } else {
325 mEmptyView.setVisibility(View.VISIBLE);
326 }
327 }
328
329 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000330 if (mEmptyView != null) {
331 mEmptyView.setVisibility(View.GONE);
332 }
Jason Monk39b46742015-09-10 15:52:51 -0400333 mEmptyView = v;
334 updateEmptyView();
335 }
336
337 public View getEmptyView() {
338 return mEmptyView;
339 }
340
Jason Monk65bb0972015-12-17 10:39:44 -0500341 @Override
342 public RecyclerView.LayoutManager onCreateLayoutManager() {
343 mLayoutManager = new LinearLayoutManager(getContext());
344 return mLayoutManager;
345 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700346
Jason Monk65bb0972015-12-17 10:39:44 -0500347 @Override
348 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
Fan Zhanga1000462018-02-02 12:15:37 -0800349 final Bundle arguments = getArguments();
Fan Zhang3d516e72018-01-31 14:14:41 -0800350 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen,
Fan Zhanga1000462018-02-02 12:15:37 -0800351 arguments == null
352 ? null : arguments.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY),
Fan Zhang3d516e72018-01-31 14:14:41 -0800353 mPreferenceHighlighted);
Jason Monk65bb0972015-12-17 10:39:44 -0500354 return mAdapter;
355 }
356
Jason Monkf38fb382016-03-18 14:23:01 -0400357 protected void setAnimationAllowed(boolean animationAllowed) {
358 mAnimationAllowed = animationAllowed;
359 }
360
Jason Monk2071eda2016-02-25 13:55:48 -0500361 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
Fan Zhang3d516e72018-01-31 14:14:41 -0800362 mPreferenceCache = new ArrayMap<>();
Jason Monk2071eda2016-02-25 13:55:48 -0500363 final int N = group.getPreferenceCount();
364 for (int i = 0; i < N; i++) {
365 Preference p = group.getPreference(i);
366 if (TextUtils.isEmpty(p.getKey())) {
367 continue;
368 }
369 mPreferenceCache.put(p.getKey(), p);
370 }
371 }
372
373 protected Preference getCachedPreference(String key) {
374 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
375 }
376
377 protected void removeCachedPrefs(PreferenceGroup group) {
378 for (Preference p : mPreferenceCache.values()) {
379 group.removePreference(p);
380 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400381 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500382 }
383
Jason Monka6278442016-04-21 10:12:30 -0400384 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400385 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400386 }
387
Jan Nordqvist9eb43dd2018-03-26 15:29:44 -0700388 @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
389 public boolean removePreference(String key) {
Fan Zhange84407f2017-05-24 11:19:52 -0700390 return removePreference(getPreferenceScreen(), key);
391 }
392
393 @VisibleForTesting
394 boolean removePreference(PreferenceGroup group, String key) {
395 final int preferenceCount = group.getPreferenceCount();
396 for (int i = 0; i < preferenceCount; i++) {
397 final Preference preference = group.getPreference(i);
398 final String curKey = preference.getKey();
399
400 if (TextUtils.equals(curKey, key)) {
401 return group.removePreference(preference);
402 }
403
404 if (preference instanceof PreferenceGroup) {
405 if (removePreference((PreferenceGroup) preference, key)) {
406 return true;
407 }
408 }
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700409 }
Fan Zhange84407f2017-05-24 11:19:52 -0700410 return false;
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700411 }
412
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700413 /*
414 * The name is intentionally made different from Activity#finish(), so that
415 * users won't misunderstand its meaning.
416 */
417 public final void finishFragment() {
418 getActivity().onBackPressed();
419 }
420
Amith Yamasanid7993472010-08-18 13:59:28 -0700421 // Some helpers for functions used by the settings fragments when they were activities
422
423 /**
424 * Returns the ContentResolver from the owning Activity.
425 */
426 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700427 Context context = getActivity();
428 if (context != null) {
429 mContentResolver = context.getContentResolver();
430 }
431 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700432 }
433
434 /**
435 * Returns the specified system service from the owning Activity.
436 */
437 protected Object getSystemService(final String name) {
438 return getActivity().getSystemService(name);
439 }
440
441 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700442 * Returns the PackageManager from the owning Activity.
443 */
444 protected PackageManager getPackageManager() {
445 return getActivity().getPackageManager();
446 }
447
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800448 @Override
449 public void onDetach() {
450 if (isRemoving()) {
451 if (mDialogFragment != null) {
452 mDialogFragment.dismiss();
453 mDialogFragment = null;
454 }
455 }
456 super.onDetach();
457 }
458
Amith Yamasanid7993472010-08-18 13:59:28 -0700459 // Dialog management
460
461 protected void showDialog(int dialogId) {
462 if (mDialogFragment != null) {
463 Log.e(TAG, "Old dialog fragment not null!");
464 }
tmfangd5405cf2018-10-05 18:45:07 +0800465 mDialogFragment = SettingsDialogFragment.newInstance(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800466 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700467 }
468
Fan Zhangd65184f2016-09-19 17:45:24 -0700469 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700470 public Dialog onCreateDialog(int dialogId) {
471 return null;
472 }
473
Fan Zhangd65184f2016-09-19 17:45:24 -0700474 @Override
475 public int getDialogMetricsCategory(int dialogId) {
476 return 0;
477 }
478
Amith Yamasanid7993472010-08-18 13:59:28 -0700479 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800480 // mDialogFragment may not be visible yet in parent fragment's onResume().
481 // To be able to dismiss dialog at that time, don't check
482 // mDialogFragment.isVisible().
483 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400484 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700485 }
486 mDialogFragment = null;
487 }
488
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800489 /**
490 * Sets the OnCancelListener of the dialog shown. This method can only be
491 * called after showDialog(int) and before removeDialog(int). The method
492 * does nothing otherwise.
493 */
494 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
495 if (mDialogFragment != null) {
496 mDialogFragment.mOnCancelListener = listener;
497 }
498 }
499
500 /**
501 * Sets the OnDismissListener of the dialog shown. This method can only be
502 * called after showDialog(int) and before removeDialog(int). The method
503 * does nothing otherwise.
504 */
505 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
506 if (mDialogFragment != null) {
507 mDialogFragment.mOnDismissListener = listener;
508 }
509 }
510
Amith Yamasanic861cf82012-10-02 14:51:46 -0700511 public void onDialogShowing() {
512 // override in subclass to attach a dismiss listener, for instance
513 }
514
Jason Monk39b46742015-09-10 15:52:51 -0400515 @Override
516 public void onDisplayPreferenceDialog(Preference preference) {
517 if (preference.getKey() == null) {
518 // Auto-key preferences that don't have a key, so the dialog can find them.
519 preference.setKey(UUID.randomUUID().toString());
520 }
521 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000522 if (preference instanceof RestrictedListPreference) {
523 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
524 .newInstance(preference.getKey());
525 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400526 f = CustomListPreference.CustomListPreferenceDialogFragment
527 .newInstance(preference.getKey());
tmfang27c84de2018-06-28 11:39:05 +0800528 } else if (preference instanceof CustomDialogPreferenceCompat) {
529 f = CustomDialogPreferenceCompat.CustomPreferenceDialogFragment
Jason Monk39b46742015-09-10 15:52:51 -0400530 .newInstance(preference.getKey());
tmfang27c84de2018-06-28 11:39:05 +0800531 } else if (preference instanceof CustomEditTextPreferenceCompat) {
532 f = CustomEditTextPreferenceCompat.CustomPreferenceDialogFragment
Jason Monk39b46742015-09-10 15:52:51 -0400533 .newInstance(preference.getKey());
534 } else {
535 super.onDisplayPreferenceDialog(preference);
536 return;
537 }
538 f.setTargetFragment(this, 0);
539 f.show(getFragmentManager(), "dialog_preference");
540 onDialogShowing();
541 }
542
Fan Zhangd65184f2016-09-19 17:45:24 -0700543 public static class SettingsDialogFragment extends InstrumentedDialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800544 private static final String KEY_DIALOG_ID = "key_dialog_id";
545 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
546
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800547 private Fragment mParentFragment;
548
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800549 private DialogInterface.OnCancelListener mOnCancelListener;
550 private DialogInterface.OnDismissListener mOnDismissListener;
551
tmfangd5405cf2018-10-05 18:45:07 +0800552 public static SettingsDialogFragment newInstance(DialogCreatable fragment, int dialogId) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800553 if (!(fragment instanceof Fragment)) {
554 throw new IllegalArgumentException("fragment argument must be an instance of "
555 + Fragment.class.getName());
556 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800557
tmfangd5405cf2018-10-05 18:45:07 +0800558 final SettingsDialogFragment settingsDialogFragment = new SettingsDialogFragment();
559 settingsDialogFragment.setParentFragment(fragment);
560 settingsDialogFragment.setDialogId(dialogId);
561
562 return settingsDialogFragment;
563 }
Fan Zhangd65184f2016-09-19 17:45:24 -0700564
565 @Override
566 public int getMetricsCategory() {
tmfangd5405cf2018-10-05 18:45:07 +0800567 if (mParentFragment == null) {
Fan Zhang4fe7c082016-10-03 13:48:55 -0700568 return Instrumentable.METRICS_CATEGORY_UNKNOWN;
569 }
tmfangd5405cf2018-10-05 18:45:07 +0800570 final int metricsCategory =
571 ((DialogCreatable) mParentFragment).getDialogMetricsCategory(mDialogId);
Fan Zhangd65184f2016-09-19 17:45:24 -0700572 if (metricsCategory <= 0) {
573 throw new IllegalStateException("Dialog must provide a metrics category");
574 }
575 return metricsCategory;
576 }
577
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800578 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800579 public void onSaveInstanceState(Bundle outState) {
580 super.onSaveInstanceState(outState);
581 if (mParentFragment != null) {
582 outState.putInt(KEY_DIALOG_ID, mDialogId);
583 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
584 }
585 }
586
587 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700588 public void onStart() {
589 super.onStart();
590
591 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
592 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
593 }
594 }
595
596 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800597 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800598 if (savedInstanceState != null) {
599 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800600 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800601 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700602 if (mParentFragment == null) {
603 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
604 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800605 if (!(mParentFragment instanceof DialogCreatable)) {
606 throw new IllegalArgumentException(
607 (mParentFragment != null
608 ? mParentFragment.getClass().getName()
609 : mParentFragmentId)
610 + " must implement "
611 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800612 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800613 // This dialog fragment could be created from non-SettingsPreferenceFragment
614 if (mParentFragment instanceof SettingsPreferenceFragment) {
615 // restore mDialogFragment in mParentFragment
616 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
617 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800618 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800619 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700620 }
621
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800622 @Override
623 public void onCancel(DialogInterface dialog) {
624 super.onCancel(dialog);
625 if (mOnCancelListener != null) {
626 mOnCancelListener.onCancel(dialog);
627 }
628 }
629
630 @Override
631 public void onDismiss(DialogInterface dialog) {
632 super.onDismiss(dialog);
633 if (mOnDismissListener != null) {
634 mOnDismissListener.onDismiss(dialog);
635 }
636 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800637
Amith Yamasanid7993472010-08-18 13:59:28 -0700638 public int getDialogId() {
639 return mDialogId;
640 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800641
642 @Override
643 public void onDetach() {
644 super.onDetach();
645
Amith Yamasani8875ede2011-01-31 12:46:57 -0800646 // This dialog fragment could be created from non-SettingsPreferenceFragment
647 if (mParentFragment instanceof SettingsPreferenceFragment) {
648 // in case the dialog is not explicitly removed by removeDialog()
649 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
650 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
651 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800652 }
653 }
tmfangd5405cf2018-10-05 18:45:07 +0800654
655 private void setParentFragment(DialogCreatable fragment) {
656 mParentFragment = (Fragment) fragment;
657 }
658
659 private void setDialogId(int dialogId) {
660 mDialogId = dialogId;
661 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700662 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700663
664 protected boolean hasNextButton() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800665 return ((ButtonBarHandler) getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700666 }
667
668 protected Button getNextButton() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800669 return ((ButtonBarHandler) getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700670 }
671
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700672 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700673 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400674 if (activity == null) return;
675 if (getFragmentManager().getBackStackEntryCount() > 0) {
676 getFragmentManager().popBackStack();
677 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800678 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700679 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700680 }
681
Jason Monkb7e43802016-06-06 16:01:58 -0400682 protected Intent getIntent() {
683 if (getActivity() == null) {
684 return null;
685 }
686 return getActivity().getIntent();
687 }
688
689 protected void setResult(int result, Intent intent) {
690 if (getActivity() == null) {
691 return;
692 }
693 getActivity().setResult(result, intent);
694 }
695
696 protected void setResult(int result) {
697 if (getActivity() == null) {
698 return;
699 }
700 getActivity().setResult(result);
701 }
Arc Wang04854f82020-03-20 13:34:33 +0800702
703 protected boolean isFinishingOrDestroyed() {
704 final Activity activity = getActivity();
705 return activity == null || activity.isFinishing() || activity.isDestroyed();
706 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700707}