blob: 1104672d33797d52151dcc29c4b94368e3f522ce [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 Zhang78ea7da2018-07-02 13:44:57 -070047import com.android.settings.search.Indexable;
Fan Zhang681a4cd2017-11-29 16:57:19 -080048import com.android.settings.search.actionbar.SearchMenuController;
Fan Zhange0b0e9f2017-11-29 14:55:59 -080049import com.android.settings.support.actionbar.HelpMenuController;
50import com.android.settings.support.actionbar.HelpResourceProvider;
Fan Zhang3d516e72018-01-31 14:14:41 -080051import com.android.settings.widget.HighlightablePreferenceGroupAdapter;
Fan Zhang896f1b32017-06-26 14:22:45 -070052import com.android.settings.widget.LoadingViewController;
tmfang27c84de2018-06-28 11:39:05 +080053import com.android.settingslib.CustomDialogPreferenceCompat;
54import com.android.settingslib.CustomEditTextPreferenceCompat;
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -080055import com.android.settingslib.core.instrumentation.Instrumentable;
tmfang27c84de2018-06-28 11:39:05 +080056import com.android.settingslib.widget.FooterPreferenceMixinCompat;
tmfangdce94bb2018-11-26 18:41:01 +080057import com.android.settingslib.widget.LayoutPreference;
John Spurlockb8e02b82015-04-15 21:15:55 -040058
Jason Monk39b46742015-09-10 15:52:51 -040059import java.util.UUID;
60
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070061/**
Amith Yamasanid7993472010-08-18 13:59:28 -070062 * Base class for Settings fragments, with some helper functions and dialog management.
63 */
Fan Zhang2d0b3442016-12-05 17:02:33 -080064public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceFragment
Fan Zhang78ea7da2018-07-02 13:44:57 -070065 implements DialogCreatable, HelpResourceProvider, Indexable {
Anna Galusza0285c802016-01-29 17:32:19 -080066
Jason Monk65bb0972015-12-17 10:39:44 -050067 private static final String TAG = "SettingsPreference";
Amith Yamasanid7993472010-08-18 13:59:28 -070068
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070069 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070070
tmfang27c84de2018-06-28 11:39:05 +080071 protected final FooterPreferenceMixinCompat mFooterPreferenceMixin =
72 new FooterPreferenceMixinCompat(this, getSettingsLifecycle());
Fan Zhangd5b48452016-12-13 12:42:50 -080073
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070074
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000075 private static final int ORDER_FIRST = -1;
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000076
Fan Zhange0b0e9f2017-11-29 14:55:59 -080077 private SettingsDialogFragment mDialogFragment;
Amith Yamasani350938e2013-04-09 10:22:47 -070078 // Cache the content resolver for async callbacks
79 private ContentResolver mContentResolver;
80
Jason Monk39b46742015-09-10 15:52:51 -040081 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070082 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040083 private RecyclerView.AdapterDataObserver mDataSetObserver =
84 new RecyclerView.AdapterDataObserver() {
Tony Mantler0b825f52016-09-27 14:48:16 -070085 @Override
86 public void onChanged() {
87 onDataSetChanged();
88 }
89
90 @Override
91 public void onItemRangeChanged(int positionStart, int itemCount) {
92 onDataSetChanged();
93 }
94
95 @Override
96 public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
97 onDataSetChanged();
98 }
99
100 @Override
101 public void onItemRangeInserted(int positionStart, int itemCount) {
102 onDataSetChanged();
103 }
104
105 @Override
106 public void onItemRangeRemoved(int positionStart, int itemCount) {
107 onDataSetChanged();
108 }
109
110 @Override
111 public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
112 onDataSetChanged();
113 }
114 };
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700115
Jason Chiucfa36db2019-06-05 17:31:31 +0800116 @VisibleForTesting
117 ViewGroup mPinnedHeaderFrameLayout;
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900118 private ViewGroup mButtonBar;
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700119
Jason Monk39b46742015-09-10 15:52:51 -0400120 private LayoutPreference mHeader;
121
Jason Monk39b46742015-09-10 15:52:51 -0400122 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -0500123 private LinearLayoutManager mLayoutManager;
Jason Monk2071eda2016-02-25 13:55:48 -0500124 private ArrayMap<String, Preference> mPreferenceCache;
Jason Monkf38fb382016-03-18 14:23:01 -0400125 private boolean mAnimationAllowed;
Jason Monk39b46742015-09-10 15:52:51 -0400126
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700127 @VisibleForTesting
128 public HighlightablePreferenceGroupAdapter mAdapter;
129 @VisibleForTesting
130 public boolean mPreferenceHighlighted = false;
131
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700132 @Override
133 public void onCreate(Bundle icicle) {
134 super.onCreate(icicle);
Fan Zhang681a4cd2017-11-29 16:57:19 -0800135 SearchMenuController.init(this /* host */);
Fan Zhange0b0e9f2017-11-29 14:55:59 -0800136 HelpMenuController.init(this /* host */);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700137
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700138 if (icicle != null) {
139 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
140 }
Fan Zhang72456a92018-02-20 11:25:56 -0800141 HighlightablePreferenceGroupAdapter.adjustInitialExpandedChildCount(this /* host */);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700142 }
143
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700144 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700145 public View onCreateView(LayoutInflater inflater, ViewGroup container,
146 Bundle savedInstanceState) {
147 final View root = super.onCreateView(inflater, container, savedInstanceState);
Fan Zhange0b0e9f2017-11-29 14:55:59 -0800148 mPinnedHeaderFrameLayout = root.findViewById(R.id.pinned_header);
149 mButtonBar = root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700150 return root;
151 }
152
Jason Monk39b46742015-09-10 15:52:51 -0400153 @Override
Jason Monk91e2f892016-02-23 15:31:09 -0500154 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
155 super.addPreferencesFromResource(preferencesResId);
156 checkAvailablePrefs(getPreferenceScreen());
157 }
158
Doris Lingfe525942018-11-27 14:58:55 -0800159 @VisibleForTesting
160 void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
Jason Monk91e2f892016-02-23 15:31:09 -0500161 if (preferenceGroup == null) return;
162 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
163 Preference pref = preferenceGroup.getPreference(i);
164 if (pref instanceof SelfAvailablePreference
165 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
Doris Lingfe525942018-11-27 14:58:55 -0800166 pref.setVisible(false);
Jason Monk91e2f892016-02-23 15:31:09 -0500167 } else if (pref instanceof PreferenceGroup) {
168 checkAvailablePrefs((PreferenceGroup) pref);
169 }
170 }
171 }
172
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900173 public ViewGroup getButtonBar() {
174 return mButtonBar;
175 }
176
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700177 public View setPinnedHeaderView(int layoutResId) {
178 final LayoutInflater inflater = getActivity().getLayoutInflater();
179 final View pinnedHeader =
180 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
181 setPinnedHeaderView(pinnedHeader);
182 return pinnedHeader;
183 }
184
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700185 public void setPinnedHeaderView(View pinnedHeader) {
186 mPinnedHeaderFrameLayout.addView(pinnedHeader);
187 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
188 }
189
Jason Chiucfa36db2019-06-05 17:31:31 +0800190 public void showPinnedHeader(boolean show) {
191 mPinnedHeaderFrameLayout.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
192 }
193
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700194 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700195 public void onSaveInstanceState(Bundle outState) {
196 super.onSaveInstanceState(outState);
197
Fan Zhang3d516e72018-01-31 14:14:41 -0800198 if (mAdapter != null) {
199 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mAdapter.isHighlightRequested());
200 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700201 }
202
203 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700204 public void onActivityCreated(Bundle savedInstanceState) {
205 super.onActivityCreated(savedInstanceState);
Johan Redestig76218e52016-04-19 08:29:30 +0200206 setHasOptionsMenu(true);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700207 }
208
209 @Override
210 public void onResume() {
211 super.onResume();
Fan Zhang3d516e72018-01-31 14:14:41 -0800212 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700213 }
214
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700215 @Override
216 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700217 registerObserverIfNeeded();
218 }
219
220 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700221 protected void onUnbindPreferences() {
222 unregisterObserverIfNeeded();
223 }
224
Jason Monkb37e2882016-01-11 14:27:20 -0500225 public void setLoading(boolean loading, boolean animate) {
Fan Zhang896f1b32017-06-26 14:22:45 -0700226 View loadingContainer = getView().findViewById(R.id.loading_container);
227 LoadingViewController.handleLoadingContainer(loadingContainer, getListView(),
228 !loading /* done */,
229 animate);
Jason Monkb37e2882016-01-11 14:27:20 -0500230 }
231
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700232 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700233 if (!mIsDataSetObserverRegistered) {
234 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400235 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700236 }
Jason Monk39b46742015-09-10 15:52:51 -0400237 mCurrentRootAdapter = getListView().getAdapter();
238 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700239 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500240 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700241 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700242 }
243
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700244 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700245 if (mIsDataSetObserverRegistered) {
246 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400247 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700248 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700249 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700250 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700251 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700252 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700253
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700254 public void highlightPreferenceIfNeeded() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800255 if (!isAdded()) {
256 return;
257 }
258 if (mAdapter != null) {
259 mAdapter.requestHighlight(getView(), getListView());
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700260 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700261 }
262
Fan Zhang72456a92018-02-20 11:25:56 -0800263 /**
264 * Returns initial expanded child count.
265 * <p/>
266 * Only override this method if the initial expanded child must be determined at run time.
267 */
268 public int getInitialExpandedChildCount() {
269 return 0;
270 }
271
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000272 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400273 highlightPreferenceIfNeeded();
274 updateEmptyView();
275 }
276
Jason Monk39b46742015-09-10 15:52:51 -0400277 public LayoutPreference getHeaderView() {
278 return mHeader;
279 }
280
Jason Monk39b46742015-09-10 15:52:51 -0400281 protected void setHeaderView(int resource) {
282 mHeader = new LayoutPreference(getPrefContext(), resource);
Yanting Yang70a8e312019-05-21 21:05:32 +0800283 mHeader.setSelectable(false);
Udam Sainid553abc2016-02-16 17:54:13 -0800284 addPreferenceToTop(mHeader);
285 }
286
287 protected void setHeaderView(View view) {
288 mHeader = new LayoutPreference(getPrefContext(), view);
Yanting Yang70a8e312019-05-21 21:05:32 +0800289 mHeader.setSelectable(false);
Udam Sainid553abc2016-02-16 17:54:13 -0800290 addPreferenceToTop(mHeader);
291 }
292
293 private void addPreferenceToTop(LayoutPreference preference) {
294 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400295 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800296 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400297 }
298 }
299
Jason Monk39b46742015-09-10 15:52:51 -0400300 @Override
301 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400302 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400303 // Without ids generated, the RecyclerView won't animate changes to the preferences.
304 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
305 }
Jason Monk39b46742015-09-10 15:52:51 -0400306 super.setPreferenceScreen(preferenceScreen);
307 if (preferenceScreen != null) {
308 if (mHeader != null) {
309 preferenceScreen.addPreference(mHeader);
310 }
Jason Monk39b46742015-09-10 15:52:51 -0400311 }
312 }
313
jackqdyulei2b2abac2017-05-26 10:47:55 -0700314 @VisibleForTesting
315 void updateEmptyView() {
Jason Monk39b46742015-09-10 15:52:51 -0400316 if (mEmptyView == null) return;
317 if (getPreferenceScreen() != null) {
jackqdyulei2b2abac2017-05-26 10:47:55 -0700318 final View listContainer = getActivity().findViewById(android.R.id.list_container);
Jason Monk39b46742015-09-10 15:52:51 -0400319 boolean show = (getPreferenceScreen().getPreferenceCount()
320 - (mHeader != null ? 1 : 0)
jackqdyulei2b2abac2017-05-26 10:47:55 -0700321 - (mFooterPreferenceMixin.hasFooter() ? 1 : 0)) <= 0
322 || (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 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700702}