blob: 9a60876ca4909c18ca3a8c858e11c0e10a81fdc5 [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
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700116 private ViewGroup mPinnedHeaderFrameLayout;
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900117 private ViewGroup mButtonBar;
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700118
Jason Monk39b46742015-09-10 15:52:51 -0400119 private LayoutPreference mHeader;
120
Jason Monk39b46742015-09-10 15:52:51 -0400121 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -0500122 private LinearLayoutManager mLayoutManager;
Jason Monk2071eda2016-02-25 13:55:48 -0500123 private ArrayMap<String, Preference> mPreferenceCache;
Jason Monkf38fb382016-03-18 14:23:01 -0400124 private boolean mAnimationAllowed;
Jason Monk39b46742015-09-10 15:52:51 -0400125
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700126 @VisibleForTesting
127 public HighlightablePreferenceGroupAdapter mAdapter;
128 @VisibleForTesting
129 public boolean mPreferenceHighlighted = false;
130
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700131 @Override
132 public void onCreate(Bundle icicle) {
133 super.onCreate(icicle);
Fan Zhang681a4cd2017-11-29 16:57:19 -0800134 SearchMenuController.init(this /* host */);
Fan Zhange0b0e9f2017-11-29 14:55:59 -0800135 HelpMenuController.init(this /* host */);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700136
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700137 if (icicle != null) {
138 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
139 }
Fan Zhang72456a92018-02-20 11:25:56 -0800140 HighlightablePreferenceGroupAdapter.adjustInitialExpandedChildCount(this /* host */);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700141 }
142
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700143 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700144 public View onCreateView(LayoutInflater inflater, ViewGroup container,
145 Bundle savedInstanceState) {
146 final View root = super.onCreateView(inflater, container, savedInstanceState);
Fan Zhange0b0e9f2017-11-29 14:55:59 -0800147 mPinnedHeaderFrameLayout = root.findViewById(R.id.pinned_header);
148 mButtonBar = root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700149 return root;
150 }
151
Jason Monk39b46742015-09-10 15:52:51 -0400152 @Override
Jason Monk91e2f892016-02-23 15:31:09 -0500153 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
154 super.addPreferencesFromResource(preferencesResId);
155 checkAvailablePrefs(getPreferenceScreen());
156 }
157
Doris Lingfe525942018-11-27 14:58:55 -0800158 @VisibleForTesting
159 void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
Jason Monk91e2f892016-02-23 15:31:09 -0500160 if (preferenceGroup == null) return;
161 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
162 Preference pref = preferenceGroup.getPreference(i);
163 if (pref instanceof SelfAvailablePreference
164 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
Doris Lingfe525942018-11-27 14:58:55 -0800165 pref.setVisible(false);
Jason Monk91e2f892016-02-23 15:31:09 -0500166 } else if (pref instanceof PreferenceGroup) {
167 checkAvailablePrefs((PreferenceGroup) pref);
168 }
169 }
170 }
171
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900172 public ViewGroup getButtonBar() {
173 return mButtonBar;
174 }
175
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700176 public View setPinnedHeaderView(int layoutResId) {
177 final LayoutInflater inflater = getActivity().getLayoutInflater();
178 final View pinnedHeader =
179 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
180 setPinnedHeaderView(pinnedHeader);
181 return pinnedHeader;
182 }
183
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700184 public void setPinnedHeaderView(View pinnedHeader) {
185 mPinnedHeaderFrameLayout.addView(pinnedHeader);
186 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
187 }
188
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700189 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700190 public void onSaveInstanceState(Bundle outState) {
191 super.onSaveInstanceState(outState);
192
Fan Zhang3d516e72018-01-31 14:14:41 -0800193 if (mAdapter != null) {
194 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mAdapter.isHighlightRequested());
195 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700196 }
197
198 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700199 public void onActivityCreated(Bundle savedInstanceState) {
200 super.onActivityCreated(savedInstanceState);
Johan Redestig76218e52016-04-19 08:29:30 +0200201 setHasOptionsMenu(true);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700202 }
203
204 @Override
205 public void onResume() {
206 super.onResume();
Fan Zhang3d516e72018-01-31 14:14:41 -0800207 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700208 }
209
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700210 @Override
211 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700212 registerObserverIfNeeded();
213 }
214
215 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700216 protected void onUnbindPreferences() {
217 unregisterObserverIfNeeded();
218 }
219
Jason Monkb37e2882016-01-11 14:27:20 -0500220 public void setLoading(boolean loading, boolean animate) {
Fan Zhang896f1b32017-06-26 14:22:45 -0700221 View loadingContainer = getView().findViewById(R.id.loading_container);
222 LoadingViewController.handleLoadingContainer(loadingContainer, getListView(),
223 !loading /* done */,
224 animate);
Jason Monkb37e2882016-01-11 14:27:20 -0500225 }
226
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700227 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700228 if (!mIsDataSetObserverRegistered) {
229 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400230 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700231 }
Jason Monk39b46742015-09-10 15:52:51 -0400232 mCurrentRootAdapter = getListView().getAdapter();
233 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700234 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500235 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700236 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700237 }
238
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700239 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700240 if (mIsDataSetObserverRegistered) {
241 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400242 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700243 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700244 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700245 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700246 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700247 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700248
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700249 public void highlightPreferenceIfNeeded() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800250 if (!isAdded()) {
251 return;
252 }
253 if (mAdapter != null) {
254 mAdapter.requestHighlight(getView(), getListView());
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700255 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700256 }
257
Fan Zhang72456a92018-02-20 11:25:56 -0800258 /**
259 * Returns initial expanded child count.
260 * <p/>
261 * Only override this method if the initial expanded child must be determined at run time.
262 */
263 public int getInitialExpandedChildCount() {
264 return 0;
265 }
266
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000267 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400268 highlightPreferenceIfNeeded();
269 updateEmptyView();
270 }
271
Jason Monk39b46742015-09-10 15:52:51 -0400272 public LayoutPreference getHeaderView() {
273 return mHeader;
274 }
275
Jason Monk39b46742015-09-10 15:52:51 -0400276 protected void setHeaderView(int resource) {
277 mHeader = new LayoutPreference(getPrefContext(), resource);
Yanting Yang70a8e312019-05-21 21:05:32 +0800278 mHeader.setSelectable(false);
Udam Sainid553abc2016-02-16 17:54:13 -0800279 addPreferenceToTop(mHeader);
280 }
281
282 protected void setHeaderView(View view) {
283 mHeader = new LayoutPreference(getPrefContext(), view);
Yanting Yang70a8e312019-05-21 21:05:32 +0800284 mHeader.setSelectable(false);
Udam Sainid553abc2016-02-16 17:54:13 -0800285 addPreferenceToTop(mHeader);
286 }
287
288 private void addPreferenceToTop(LayoutPreference preference) {
289 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400290 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800291 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400292 }
293 }
294
Jason Monk39b46742015-09-10 15:52:51 -0400295 @Override
296 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400297 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400298 // Without ids generated, the RecyclerView won't animate changes to the preferences.
299 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
300 }
Jason Monk39b46742015-09-10 15:52:51 -0400301 super.setPreferenceScreen(preferenceScreen);
302 if (preferenceScreen != null) {
303 if (mHeader != null) {
304 preferenceScreen.addPreference(mHeader);
305 }
Jason Monk39b46742015-09-10 15:52:51 -0400306 }
307 }
308
jackqdyulei2b2abac2017-05-26 10:47:55 -0700309 @VisibleForTesting
310 void updateEmptyView() {
Jason Monk39b46742015-09-10 15:52:51 -0400311 if (mEmptyView == null) return;
312 if (getPreferenceScreen() != null) {
jackqdyulei2b2abac2017-05-26 10:47:55 -0700313 final View listContainer = getActivity().findViewById(android.R.id.list_container);
Jason Monk39b46742015-09-10 15:52:51 -0400314 boolean show = (getPreferenceScreen().getPreferenceCount()
315 - (mHeader != null ? 1 : 0)
jackqdyulei2b2abac2017-05-26 10:47:55 -0700316 - (mFooterPreferenceMixin.hasFooter() ? 1 : 0)) <= 0
317 || (listContainer != null && listContainer.getVisibility() != View.VISIBLE);
Jason Monk39b46742015-09-10 15:52:51 -0400318 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
319 } else {
320 mEmptyView.setVisibility(View.VISIBLE);
321 }
322 }
323
324 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000325 if (mEmptyView != null) {
326 mEmptyView.setVisibility(View.GONE);
327 }
Jason Monk39b46742015-09-10 15:52:51 -0400328 mEmptyView = v;
329 updateEmptyView();
330 }
331
332 public View getEmptyView() {
333 return mEmptyView;
334 }
335
Jason Monk65bb0972015-12-17 10:39:44 -0500336 @Override
337 public RecyclerView.LayoutManager onCreateLayoutManager() {
338 mLayoutManager = new LinearLayoutManager(getContext());
339 return mLayoutManager;
340 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700341
Jason Monk65bb0972015-12-17 10:39:44 -0500342 @Override
343 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
Fan Zhanga1000462018-02-02 12:15:37 -0800344 final Bundle arguments = getArguments();
Fan Zhang3d516e72018-01-31 14:14:41 -0800345 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen,
Fan Zhanga1000462018-02-02 12:15:37 -0800346 arguments == null
347 ? null : arguments.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY),
Fan Zhang3d516e72018-01-31 14:14:41 -0800348 mPreferenceHighlighted);
Jason Monk65bb0972015-12-17 10:39:44 -0500349 return mAdapter;
350 }
351
Jason Monkf38fb382016-03-18 14:23:01 -0400352 protected void setAnimationAllowed(boolean animationAllowed) {
353 mAnimationAllowed = animationAllowed;
354 }
355
Jason Monk2071eda2016-02-25 13:55:48 -0500356 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
Fan Zhang3d516e72018-01-31 14:14:41 -0800357 mPreferenceCache = new ArrayMap<>();
Jason Monk2071eda2016-02-25 13:55:48 -0500358 final int N = group.getPreferenceCount();
359 for (int i = 0; i < N; i++) {
360 Preference p = group.getPreference(i);
361 if (TextUtils.isEmpty(p.getKey())) {
362 continue;
363 }
364 mPreferenceCache.put(p.getKey(), p);
365 }
366 }
367
368 protected Preference getCachedPreference(String key) {
369 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
370 }
371
372 protected void removeCachedPrefs(PreferenceGroup group) {
373 for (Preference p : mPreferenceCache.values()) {
374 group.removePreference(p);
375 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400376 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500377 }
378
Jason Monka6278442016-04-21 10:12:30 -0400379 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400380 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400381 }
382
Jan Nordqvist9eb43dd2018-03-26 15:29:44 -0700383 @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
384 public boolean removePreference(String key) {
Fan Zhange84407f2017-05-24 11:19:52 -0700385 return removePreference(getPreferenceScreen(), key);
386 }
387
388 @VisibleForTesting
389 boolean removePreference(PreferenceGroup group, String key) {
390 final int preferenceCount = group.getPreferenceCount();
391 for (int i = 0; i < preferenceCount; i++) {
392 final Preference preference = group.getPreference(i);
393 final String curKey = preference.getKey();
394
395 if (TextUtils.equals(curKey, key)) {
396 return group.removePreference(preference);
397 }
398
399 if (preference instanceof PreferenceGroup) {
400 if (removePreference((PreferenceGroup) preference, key)) {
401 return true;
402 }
403 }
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700404 }
Fan Zhange84407f2017-05-24 11:19:52 -0700405 return false;
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700406 }
407
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700408 /*
409 * The name is intentionally made different from Activity#finish(), so that
410 * users won't misunderstand its meaning.
411 */
412 public final void finishFragment() {
413 getActivity().onBackPressed();
414 }
415
Amith Yamasanid7993472010-08-18 13:59:28 -0700416 // Some helpers for functions used by the settings fragments when they were activities
417
418 /**
419 * Returns the ContentResolver from the owning Activity.
420 */
421 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700422 Context context = getActivity();
423 if (context != null) {
424 mContentResolver = context.getContentResolver();
425 }
426 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700427 }
428
429 /**
430 * Returns the specified system service from the owning Activity.
431 */
432 protected Object getSystemService(final String name) {
433 return getActivity().getSystemService(name);
434 }
435
436 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700437 * Returns the PackageManager from the owning Activity.
438 */
439 protected PackageManager getPackageManager() {
440 return getActivity().getPackageManager();
441 }
442
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800443 @Override
444 public void onDetach() {
445 if (isRemoving()) {
446 if (mDialogFragment != null) {
447 mDialogFragment.dismiss();
448 mDialogFragment = null;
449 }
450 }
451 super.onDetach();
452 }
453
Amith Yamasanid7993472010-08-18 13:59:28 -0700454 // Dialog management
455
456 protected void showDialog(int dialogId) {
457 if (mDialogFragment != null) {
458 Log.e(TAG, "Old dialog fragment not null!");
459 }
tmfangd5405cf2018-10-05 18:45:07 +0800460 mDialogFragment = SettingsDialogFragment.newInstance(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800461 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700462 }
463
Fan Zhangd65184f2016-09-19 17:45:24 -0700464 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700465 public Dialog onCreateDialog(int dialogId) {
466 return null;
467 }
468
Fan Zhangd65184f2016-09-19 17:45:24 -0700469 @Override
470 public int getDialogMetricsCategory(int dialogId) {
471 return 0;
472 }
473
Amith Yamasanid7993472010-08-18 13:59:28 -0700474 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800475 // mDialogFragment may not be visible yet in parent fragment's onResume().
476 // To be able to dismiss dialog at that time, don't check
477 // mDialogFragment.isVisible().
478 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400479 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700480 }
481 mDialogFragment = null;
482 }
483
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800484 /**
485 * Sets the OnCancelListener of the dialog shown. This method can only be
486 * called after showDialog(int) and before removeDialog(int). The method
487 * does nothing otherwise.
488 */
489 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
490 if (mDialogFragment != null) {
491 mDialogFragment.mOnCancelListener = listener;
492 }
493 }
494
495 /**
496 * Sets the OnDismissListener of the dialog shown. This method can only be
497 * called after showDialog(int) and before removeDialog(int). The method
498 * does nothing otherwise.
499 */
500 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
501 if (mDialogFragment != null) {
502 mDialogFragment.mOnDismissListener = listener;
503 }
504 }
505
Amith Yamasanic861cf82012-10-02 14:51:46 -0700506 public void onDialogShowing() {
507 // override in subclass to attach a dismiss listener, for instance
508 }
509
Jason Monk39b46742015-09-10 15:52:51 -0400510 @Override
511 public void onDisplayPreferenceDialog(Preference preference) {
512 if (preference.getKey() == null) {
513 // Auto-key preferences that don't have a key, so the dialog can find them.
514 preference.setKey(UUID.randomUUID().toString());
515 }
516 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000517 if (preference instanceof RestrictedListPreference) {
518 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
519 .newInstance(preference.getKey());
520 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400521 f = CustomListPreference.CustomListPreferenceDialogFragment
522 .newInstance(preference.getKey());
tmfang27c84de2018-06-28 11:39:05 +0800523 } else if (preference instanceof CustomDialogPreferenceCompat) {
524 f = CustomDialogPreferenceCompat.CustomPreferenceDialogFragment
Jason Monk39b46742015-09-10 15:52:51 -0400525 .newInstance(preference.getKey());
tmfang27c84de2018-06-28 11:39:05 +0800526 } else if (preference instanceof CustomEditTextPreferenceCompat) {
527 f = CustomEditTextPreferenceCompat.CustomPreferenceDialogFragment
Jason Monk39b46742015-09-10 15:52:51 -0400528 .newInstance(preference.getKey());
529 } else {
530 super.onDisplayPreferenceDialog(preference);
531 return;
532 }
533 f.setTargetFragment(this, 0);
534 f.show(getFragmentManager(), "dialog_preference");
535 onDialogShowing();
536 }
537
Fan Zhangd65184f2016-09-19 17:45:24 -0700538 public static class SettingsDialogFragment extends InstrumentedDialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800539 private static final String KEY_DIALOG_ID = "key_dialog_id";
540 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
541
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800542 private Fragment mParentFragment;
543
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800544 private DialogInterface.OnCancelListener mOnCancelListener;
545 private DialogInterface.OnDismissListener mOnDismissListener;
546
tmfangd5405cf2018-10-05 18:45:07 +0800547 public static SettingsDialogFragment newInstance(DialogCreatable fragment, int dialogId) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800548 if (!(fragment instanceof Fragment)) {
549 throw new IllegalArgumentException("fragment argument must be an instance of "
550 + Fragment.class.getName());
551 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800552
tmfangd5405cf2018-10-05 18:45:07 +0800553 final SettingsDialogFragment settingsDialogFragment = new SettingsDialogFragment();
554 settingsDialogFragment.setParentFragment(fragment);
555 settingsDialogFragment.setDialogId(dialogId);
556
557 return settingsDialogFragment;
558 }
Fan Zhangd65184f2016-09-19 17:45:24 -0700559
560 @Override
561 public int getMetricsCategory() {
tmfangd5405cf2018-10-05 18:45:07 +0800562 if (mParentFragment == null) {
Fan Zhang4fe7c082016-10-03 13:48:55 -0700563 return Instrumentable.METRICS_CATEGORY_UNKNOWN;
564 }
tmfangd5405cf2018-10-05 18:45:07 +0800565 final int metricsCategory =
566 ((DialogCreatable) mParentFragment).getDialogMetricsCategory(mDialogId);
Fan Zhangd65184f2016-09-19 17:45:24 -0700567 if (metricsCategory <= 0) {
568 throw new IllegalStateException("Dialog must provide a metrics category");
569 }
570 return metricsCategory;
571 }
572
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800573 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800574 public void onSaveInstanceState(Bundle outState) {
575 super.onSaveInstanceState(outState);
576 if (mParentFragment != null) {
577 outState.putInt(KEY_DIALOG_ID, mDialogId);
578 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
579 }
580 }
581
582 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700583 public void onStart() {
584 super.onStart();
585
586 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
587 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
588 }
589 }
590
591 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800592 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800593 if (savedInstanceState != null) {
594 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800595 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800596 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700597 if (mParentFragment == null) {
598 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
599 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800600 if (!(mParentFragment instanceof DialogCreatable)) {
601 throw new IllegalArgumentException(
602 (mParentFragment != null
603 ? mParentFragment.getClass().getName()
604 : mParentFragmentId)
605 + " must implement "
606 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800607 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800608 // This dialog fragment could be created from non-SettingsPreferenceFragment
609 if (mParentFragment instanceof SettingsPreferenceFragment) {
610 // restore mDialogFragment in mParentFragment
611 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
612 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800613 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800614 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700615 }
616
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800617 @Override
618 public void onCancel(DialogInterface dialog) {
619 super.onCancel(dialog);
620 if (mOnCancelListener != null) {
621 mOnCancelListener.onCancel(dialog);
622 }
623 }
624
625 @Override
626 public void onDismiss(DialogInterface dialog) {
627 super.onDismiss(dialog);
628 if (mOnDismissListener != null) {
629 mOnDismissListener.onDismiss(dialog);
630 }
631 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800632
Amith Yamasanid7993472010-08-18 13:59:28 -0700633 public int getDialogId() {
634 return mDialogId;
635 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800636
637 @Override
638 public void onDetach() {
639 super.onDetach();
640
Amith Yamasani8875ede2011-01-31 12:46:57 -0800641 // This dialog fragment could be created from non-SettingsPreferenceFragment
642 if (mParentFragment instanceof SettingsPreferenceFragment) {
643 // in case the dialog is not explicitly removed by removeDialog()
644 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
645 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
646 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800647 }
648 }
tmfangd5405cf2018-10-05 18:45:07 +0800649
650 private void setParentFragment(DialogCreatable fragment) {
651 mParentFragment = (Fragment) fragment;
652 }
653
654 private void setDialogId(int dialogId) {
655 mDialogId = dialogId;
656 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700657 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700658
659 protected boolean hasNextButton() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800660 return ((ButtonBarHandler) getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700661 }
662
663 protected Button getNextButton() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800664 return ((ButtonBarHandler) getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700665 }
666
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700667 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700668 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400669 if (activity == null) return;
670 if (getFragmentManager().getBackStackEntryCount() > 0) {
671 getFragmentManager().popBackStack();
672 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800673 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700674 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700675 }
676
Jason Monkb7e43802016-06-06 16:01:58 -0400677 protected Intent getIntent() {
678 if (getActivity() == null) {
679 return null;
680 }
681 return getActivity().getIntent();
682 }
683
684 protected void setResult(int result, Intent intent) {
685 if (getActivity() == null) {
686 return;
687 }
688 getActivity().setResult(result, intent);
689 }
690
691 protected void setResult(int result) {
692 if (getActivity() == null) {
693 return;
694 }
695 getActivity().setResult(result);
696 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700697}