blob: 07ba28f0418db52169b99781ac0e3fc83dd1b6c3 [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
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000261 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400262 highlightPreferenceIfNeeded();
263 updateEmptyView();
264 }
265
Jason Monk39b46742015-09-10 15:52:51 -0400266 public LayoutPreference getHeaderView() {
267 return mHeader;
268 }
269
Jason Monk39b46742015-09-10 15:52:51 -0400270 protected void setHeaderView(int resource) {
271 mHeader = new LayoutPreference(getPrefContext(), resource);
Yanting Yang70a8e312019-05-21 21:05:32 +0800272 mHeader.setSelectable(false);
Udam Sainid553abc2016-02-16 17:54:13 -0800273 addPreferenceToTop(mHeader);
274 }
275
276 protected void setHeaderView(View view) {
277 mHeader = new LayoutPreference(getPrefContext(), view);
Yanting Yang70a8e312019-05-21 21:05:32 +0800278 mHeader.setSelectable(false);
Udam Sainid553abc2016-02-16 17:54:13 -0800279 addPreferenceToTop(mHeader);
280 }
281
282 private void addPreferenceToTop(LayoutPreference preference) {
283 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400284 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800285 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400286 }
287 }
288
Jason Monk39b46742015-09-10 15:52:51 -0400289 @Override
290 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400291 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400292 // Without ids generated, the RecyclerView won't animate changes to the preferences.
293 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
294 }
Jason Monk39b46742015-09-10 15:52:51 -0400295 super.setPreferenceScreen(preferenceScreen);
296 if (preferenceScreen != null) {
297 if (mHeader != null) {
298 preferenceScreen.addPreference(mHeader);
299 }
Jason Monk39b46742015-09-10 15:52:51 -0400300 }
301 }
302
jackqdyulei2b2abac2017-05-26 10:47:55 -0700303 @VisibleForTesting
304 void updateEmptyView() {
Jason Monk39b46742015-09-10 15:52:51 -0400305 if (mEmptyView == null) return;
306 if (getPreferenceScreen() != null) {
jackqdyulei2b2abac2017-05-26 10:47:55 -0700307 final View listContainer = getActivity().findViewById(android.R.id.list_container);
Jason Monk39b46742015-09-10 15:52:51 -0400308 boolean show = (getPreferenceScreen().getPreferenceCount()
Sunny Shaobf434972019-08-29 14:28:13 +0800309 - (mHeader != null ? 1 : 0)) <= 0
jackqdyulei2b2abac2017-05-26 10:47:55 -0700310 || (listContainer != null && listContainer.getVisibility() != View.VISIBLE);
Jason Monk39b46742015-09-10 15:52:51 -0400311 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
312 } else {
313 mEmptyView.setVisibility(View.VISIBLE);
314 }
315 }
316
317 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000318 if (mEmptyView != null) {
319 mEmptyView.setVisibility(View.GONE);
320 }
Jason Monk39b46742015-09-10 15:52:51 -0400321 mEmptyView = v;
322 updateEmptyView();
323 }
324
325 public View getEmptyView() {
326 return mEmptyView;
327 }
328
Jason Monk65bb0972015-12-17 10:39:44 -0500329 @Override
330 public RecyclerView.LayoutManager onCreateLayoutManager() {
331 mLayoutManager = new LinearLayoutManager(getContext());
332 return mLayoutManager;
333 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700334
Jason Monk65bb0972015-12-17 10:39:44 -0500335 @Override
336 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
Fan Zhanga1000462018-02-02 12:15:37 -0800337 final Bundle arguments = getArguments();
Fan Zhang3d516e72018-01-31 14:14:41 -0800338 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen,
Fan Zhanga1000462018-02-02 12:15:37 -0800339 arguments == null
340 ? null : arguments.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY),
Fan Zhang3d516e72018-01-31 14:14:41 -0800341 mPreferenceHighlighted);
Jason Monk65bb0972015-12-17 10:39:44 -0500342 return mAdapter;
343 }
344
Jason Monkf38fb382016-03-18 14:23:01 -0400345 protected void setAnimationAllowed(boolean animationAllowed) {
346 mAnimationAllowed = animationAllowed;
347 }
348
Jason Monk2071eda2016-02-25 13:55:48 -0500349 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
Fan Zhang3d516e72018-01-31 14:14:41 -0800350 mPreferenceCache = new ArrayMap<>();
Jason Monk2071eda2016-02-25 13:55:48 -0500351 final int N = group.getPreferenceCount();
352 for (int i = 0; i < N; i++) {
353 Preference p = group.getPreference(i);
354 if (TextUtils.isEmpty(p.getKey())) {
355 continue;
356 }
357 mPreferenceCache.put(p.getKey(), p);
358 }
359 }
360
361 protected Preference getCachedPreference(String key) {
362 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
363 }
364
365 protected void removeCachedPrefs(PreferenceGroup group) {
366 for (Preference p : mPreferenceCache.values()) {
367 group.removePreference(p);
368 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400369 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500370 }
371
Jason Monka6278442016-04-21 10:12:30 -0400372 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400373 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400374 }
375
Jan Nordqvist9eb43dd2018-03-26 15:29:44 -0700376 @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
377 public boolean removePreference(String key) {
Fan Zhange84407f2017-05-24 11:19:52 -0700378 return removePreference(getPreferenceScreen(), key);
379 }
380
381 @VisibleForTesting
382 boolean removePreference(PreferenceGroup group, String key) {
383 final int preferenceCount = group.getPreferenceCount();
384 for (int i = 0; i < preferenceCount; i++) {
385 final Preference preference = group.getPreference(i);
386 final String curKey = preference.getKey();
387
388 if (TextUtils.equals(curKey, key)) {
389 return group.removePreference(preference);
390 }
391
392 if (preference instanceof PreferenceGroup) {
393 if (removePreference((PreferenceGroup) preference, key)) {
394 return true;
395 }
396 }
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700397 }
Fan Zhange84407f2017-05-24 11:19:52 -0700398 return false;
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700399 }
400
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700401 /*
402 * The name is intentionally made different from Activity#finish(), so that
403 * users won't misunderstand its meaning.
404 */
405 public final void finishFragment() {
406 getActivity().onBackPressed();
407 }
408
Amith Yamasanid7993472010-08-18 13:59:28 -0700409 // Some helpers for functions used by the settings fragments when they were activities
410
411 /**
412 * Returns the ContentResolver from the owning Activity.
413 */
414 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700415 Context context = getActivity();
416 if (context != null) {
417 mContentResolver = context.getContentResolver();
418 }
419 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700420 }
421
422 /**
423 * Returns the specified system service from the owning Activity.
424 */
425 protected Object getSystemService(final String name) {
426 return getActivity().getSystemService(name);
427 }
428
429 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700430 * Returns the PackageManager from the owning Activity.
431 */
432 protected PackageManager getPackageManager() {
433 return getActivity().getPackageManager();
434 }
435
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800436 @Override
437 public void onDetach() {
438 if (isRemoving()) {
439 if (mDialogFragment != null) {
440 mDialogFragment.dismiss();
441 mDialogFragment = null;
442 }
443 }
444 super.onDetach();
445 }
446
Amith Yamasanid7993472010-08-18 13:59:28 -0700447 // Dialog management
448
449 protected void showDialog(int dialogId) {
450 if (mDialogFragment != null) {
451 Log.e(TAG, "Old dialog fragment not null!");
452 }
tmfangd5405cf2018-10-05 18:45:07 +0800453 mDialogFragment = SettingsDialogFragment.newInstance(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800454 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700455 }
456
Fan Zhangd65184f2016-09-19 17:45:24 -0700457 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700458 public Dialog onCreateDialog(int dialogId) {
459 return null;
460 }
461
Fan Zhangd65184f2016-09-19 17:45:24 -0700462 @Override
463 public int getDialogMetricsCategory(int dialogId) {
464 return 0;
465 }
466
Amith Yamasanid7993472010-08-18 13:59:28 -0700467 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800468 // mDialogFragment may not be visible yet in parent fragment's onResume().
469 // To be able to dismiss dialog at that time, don't check
470 // mDialogFragment.isVisible().
471 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400472 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700473 }
474 mDialogFragment = null;
475 }
476
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800477 /**
478 * Sets the OnCancelListener of the dialog shown. This method can only be
479 * called after showDialog(int) and before removeDialog(int). The method
480 * does nothing otherwise.
481 */
482 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
483 if (mDialogFragment != null) {
484 mDialogFragment.mOnCancelListener = listener;
485 }
486 }
487
488 /**
489 * Sets the OnDismissListener of the dialog shown. This method can only be
490 * called after showDialog(int) and before removeDialog(int). The method
491 * does nothing otherwise.
492 */
493 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
494 if (mDialogFragment != null) {
495 mDialogFragment.mOnDismissListener = listener;
496 }
497 }
498
Amith Yamasanic861cf82012-10-02 14:51:46 -0700499 public void onDialogShowing() {
500 // override in subclass to attach a dismiss listener, for instance
501 }
502
Jason Monk39b46742015-09-10 15:52:51 -0400503 @Override
504 public void onDisplayPreferenceDialog(Preference preference) {
505 if (preference.getKey() == null) {
506 // Auto-key preferences that don't have a key, so the dialog can find them.
507 preference.setKey(UUID.randomUUID().toString());
508 }
509 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000510 if (preference instanceof RestrictedListPreference) {
511 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
512 .newInstance(preference.getKey());
513 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400514 f = CustomListPreference.CustomListPreferenceDialogFragment
515 .newInstance(preference.getKey());
tmfang27c84de2018-06-28 11:39:05 +0800516 } else if (preference instanceof CustomDialogPreferenceCompat) {
517 f = CustomDialogPreferenceCompat.CustomPreferenceDialogFragment
Jason Monk39b46742015-09-10 15:52:51 -0400518 .newInstance(preference.getKey());
tmfang27c84de2018-06-28 11:39:05 +0800519 } else if (preference instanceof CustomEditTextPreferenceCompat) {
520 f = CustomEditTextPreferenceCompat.CustomPreferenceDialogFragment
Jason Monk39b46742015-09-10 15:52:51 -0400521 .newInstance(preference.getKey());
522 } else {
523 super.onDisplayPreferenceDialog(preference);
524 return;
525 }
526 f.setTargetFragment(this, 0);
527 f.show(getFragmentManager(), "dialog_preference");
528 onDialogShowing();
529 }
530
Fan Zhangd65184f2016-09-19 17:45:24 -0700531 public static class SettingsDialogFragment extends InstrumentedDialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800532 private static final String KEY_DIALOG_ID = "key_dialog_id";
533 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
534
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800535 private Fragment mParentFragment;
536
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800537 private DialogInterface.OnCancelListener mOnCancelListener;
538 private DialogInterface.OnDismissListener mOnDismissListener;
539
tmfangd5405cf2018-10-05 18:45:07 +0800540 public static SettingsDialogFragment newInstance(DialogCreatable fragment, int dialogId) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800541 if (!(fragment instanceof Fragment)) {
542 throw new IllegalArgumentException("fragment argument must be an instance of "
543 + Fragment.class.getName());
544 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800545
tmfangd5405cf2018-10-05 18:45:07 +0800546 final SettingsDialogFragment settingsDialogFragment = new SettingsDialogFragment();
547 settingsDialogFragment.setParentFragment(fragment);
548 settingsDialogFragment.setDialogId(dialogId);
549
550 return settingsDialogFragment;
551 }
Fan Zhangd65184f2016-09-19 17:45:24 -0700552
553 @Override
554 public int getMetricsCategory() {
tmfangd5405cf2018-10-05 18:45:07 +0800555 if (mParentFragment == null) {
Fan Zhang4fe7c082016-10-03 13:48:55 -0700556 return Instrumentable.METRICS_CATEGORY_UNKNOWN;
557 }
tmfangd5405cf2018-10-05 18:45:07 +0800558 final int metricsCategory =
559 ((DialogCreatable) mParentFragment).getDialogMetricsCategory(mDialogId);
Fan Zhangd65184f2016-09-19 17:45:24 -0700560 if (metricsCategory <= 0) {
561 throw new IllegalStateException("Dialog must provide a metrics category");
562 }
563 return metricsCategory;
564 }
565
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800566 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800567 public void onSaveInstanceState(Bundle outState) {
568 super.onSaveInstanceState(outState);
569 if (mParentFragment != null) {
570 outState.putInt(KEY_DIALOG_ID, mDialogId);
571 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
572 }
573 }
574
575 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700576 public void onStart() {
577 super.onStart();
578
579 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
580 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
581 }
582 }
583
584 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800585 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800586 if (savedInstanceState != null) {
587 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800588 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800589 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700590 if (mParentFragment == null) {
591 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
592 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800593 if (!(mParentFragment instanceof DialogCreatable)) {
594 throw new IllegalArgumentException(
595 (mParentFragment != null
596 ? mParentFragment.getClass().getName()
597 : mParentFragmentId)
598 + " must implement "
599 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800600 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800601 // This dialog fragment could be created from non-SettingsPreferenceFragment
602 if (mParentFragment instanceof SettingsPreferenceFragment) {
603 // restore mDialogFragment in mParentFragment
604 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
605 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800606 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800607 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700608 }
609
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800610 @Override
611 public void onCancel(DialogInterface dialog) {
612 super.onCancel(dialog);
613 if (mOnCancelListener != null) {
614 mOnCancelListener.onCancel(dialog);
615 }
616 }
617
618 @Override
619 public void onDismiss(DialogInterface dialog) {
620 super.onDismiss(dialog);
621 if (mOnDismissListener != null) {
622 mOnDismissListener.onDismiss(dialog);
623 }
624 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800625
Amith Yamasanid7993472010-08-18 13:59:28 -0700626 public int getDialogId() {
627 return mDialogId;
628 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800629
630 @Override
631 public void onDetach() {
632 super.onDetach();
633
Amith Yamasani8875ede2011-01-31 12:46:57 -0800634 // This dialog fragment could be created from non-SettingsPreferenceFragment
635 if (mParentFragment instanceof SettingsPreferenceFragment) {
636 // in case the dialog is not explicitly removed by removeDialog()
637 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
638 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
639 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800640 }
641 }
tmfangd5405cf2018-10-05 18:45:07 +0800642
643 private void setParentFragment(DialogCreatable fragment) {
644 mParentFragment = (Fragment) fragment;
645 }
646
647 private void setDialogId(int dialogId) {
648 mDialogId = dialogId;
649 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700650 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700651
652 protected boolean hasNextButton() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800653 return ((ButtonBarHandler) getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700654 }
655
656 protected Button getNextButton() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800657 return ((ButtonBarHandler) getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700658 }
659
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700660 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700661 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400662 if (activity == null) return;
663 if (getFragmentManager().getBackStackEntryCount() > 0) {
664 getFragmentManager().popBackStack();
665 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800666 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700667 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700668 }
669
Jason Monkb7e43802016-06-06 16:01:58 -0400670 protected Intent getIntent() {
671 if (getActivity() == null) {
672 return null;
673 }
674 return getActivity().getIntent();
675 }
676
677 protected void setResult(int result, Intent intent) {
678 if (getActivity() == null) {
679 return;
680 }
681 getActivity().setResult(result, intent);
682 }
683
684 protected void setResult(int result) {
685 if (getActivity() == null) {
686 return;
687 }
688 getActivity().setResult(result);
689 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700690}