blob: 6933cfd6653353f161324b2e295e52190491c040 [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
158 private void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
159 if (preferenceGroup == null) return;
160 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
161 Preference pref = preferenceGroup.getPreference(i);
162 if (pref instanceof SelfAvailablePreference
163 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
164 preferenceGroup.removePreference(pref);
165 } else if (pref instanceof PreferenceGroup) {
166 checkAvailablePrefs((PreferenceGroup) pref);
167 }
168 }
169 }
170
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900171 public ViewGroup getButtonBar() {
172 return mButtonBar;
173 }
174
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700175 public View setPinnedHeaderView(int layoutResId) {
176 final LayoutInflater inflater = getActivity().getLayoutInflater();
177 final View pinnedHeader =
178 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
179 setPinnedHeaderView(pinnedHeader);
180 return pinnedHeader;
181 }
182
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700183 public void setPinnedHeaderView(View pinnedHeader) {
184 mPinnedHeaderFrameLayout.addView(pinnedHeader);
185 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
186 }
187
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700188 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700189 public void onSaveInstanceState(Bundle outState) {
190 super.onSaveInstanceState(outState);
191
Fan Zhang3d516e72018-01-31 14:14:41 -0800192 if (mAdapter != null) {
193 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mAdapter.isHighlightRequested());
194 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700195 }
196
197 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700198 public void onActivityCreated(Bundle savedInstanceState) {
199 super.onActivityCreated(savedInstanceState);
Johan Redestig76218e52016-04-19 08:29:30 +0200200 setHasOptionsMenu(true);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700201 }
202
203 @Override
204 public void onResume() {
205 super.onResume();
Fan Zhang3d516e72018-01-31 14:14:41 -0800206 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700207 }
208
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700209 @Override
210 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700211 registerObserverIfNeeded();
212 }
213
214 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700215 protected void onUnbindPreferences() {
216 unregisterObserverIfNeeded();
217 }
218
Jason Monkb37e2882016-01-11 14:27:20 -0500219 public void setLoading(boolean loading, boolean animate) {
Fan Zhang896f1b32017-06-26 14:22:45 -0700220 View loadingContainer = getView().findViewById(R.id.loading_container);
221 LoadingViewController.handleLoadingContainer(loadingContainer, getListView(),
222 !loading /* done */,
223 animate);
Jason Monkb37e2882016-01-11 14:27:20 -0500224 }
225
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700226 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700227 if (!mIsDataSetObserverRegistered) {
228 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400229 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700230 }
Jason Monk39b46742015-09-10 15:52:51 -0400231 mCurrentRootAdapter = getListView().getAdapter();
232 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700233 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500234 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700235 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700236 }
237
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700238 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700239 if (mIsDataSetObserverRegistered) {
240 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400241 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700242 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700243 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700244 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700245 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700246 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700247
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700248 public void highlightPreferenceIfNeeded() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800249 if (!isAdded()) {
250 return;
251 }
252 if (mAdapter != null) {
253 mAdapter.requestHighlight(getView(), getListView());
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700254 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700255 }
256
Fan Zhang72456a92018-02-20 11:25:56 -0800257 /**
258 * Returns initial expanded child count.
259 * <p/>
260 * Only override this method if the initial expanded child must be determined at run time.
261 */
262 public int getInitialExpandedChildCount() {
263 return 0;
264 }
265
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000266 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400267 highlightPreferenceIfNeeded();
268 updateEmptyView();
269 }
270
Jason Monk39b46742015-09-10 15:52:51 -0400271 public LayoutPreference getHeaderView() {
272 return mHeader;
273 }
274
Jason Monk39b46742015-09-10 15:52:51 -0400275 protected void setHeaderView(int resource) {
276 mHeader = new LayoutPreference(getPrefContext(), resource);
Udam Sainid553abc2016-02-16 17:54:13 -0800277 addPreferenceToTop(mHeader);
278 }
279
280 protected void setHeaderView(View view) {
281 mHeader = new LayoutPreference(getPrefContext(), view);
282 addPreferenceToTop(mHeader);
283 }
284
285 private void addPreferenceToTop(LayoutPreference preference) {
286 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400287 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800288 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400289 }
290 }
291
Jason Monk39b46742015-09-10 15:52:51 -0400292 @Override
293 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400294 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400295 // Without ids generated, the RecyclerView won't animate changes to the preferences.
296 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
297 }
Jason Monk39b46742015-09-10 15:52:51 -0400298 super.setPreferenceScreen(preferenceScreen);
299 if (preferenceScreen != null) {
300 if (mHeader != null) {
301 preferenceScreen.addPreference(mHeader);
302 }
Jason Monk39b46742015-09-10 15:52:51 -0400303 }
304 }
305
jackqdyulei2b2abac2017-05-26 10:47:55 -0700306 @VisibleForTesting
307 void updateEmptyView() {
Jason Monk39b46742015-09-10 15:52:51 -0400308 if (mEmptyView == null) return;
309 if (getPreferenceScreen() != null) {
jackqdyulei2b2abac2017-05-26 10:47:55 -0700310 final View listContainer = getActivity().findViewById(android.R.id.list_container);
Jason Monk39b46742015-09-10 15:52:51 -0400311 boolean show = (getPreferenceScreen().getPreferenceCount()
312 - (mHeader != null ? 1 : 0)
jackqdyulei2b2abac2017-05-26 10:47:55 -0700313 - (mFooterPreferenceMixin.hasFooter() ? 1 : 0)) <= 0
314 || (listContainer != null && listContainer.getVisibility() != View.VISIBLE);
Jason Monk39b46742015-09-10 15:52:51 -0400315 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
316 } else {
317 mEmptyView.setVisibility(View.VISIBLE);
318 }
319 }
320
321 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000322 if (mEmptyView != null) {
323 mEmptyView.setVisibility(View.GONE);
324 }
Jason Monk39b46742015-09-10 15:52:51 -0400325 mEmptyView = v;
326 updateEmptyView();
327 }
328
329 public View getEmptyView() {
330 return mEmptyView;
331 }
332
Jason Monk65bb0972015-12-17 10:39:44 -0500333 @Override
334 public RecyclerView.LayoutManager onCreateLayoutManager() {
335 mLayoutManager = new LinearLayoutManager(getContext());
336 return mLayoutManager;
337 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700338
Jason Monk65bb0972015-12-17 10:39:44 -0500339 @Override
340 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
Fan Zhanga1000462018-02-02 12:15:37 -0800341 final Bundle arguments = getArguments();
Fan Zhang3d516e72018-01-31 14:14:41 -0800342 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen,
Fan Zhanga1000462018-02-02 12:15:37 -0800343 arguments == null
344 ? null : arguments.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY),
Fan Zhang3d516e72018-01-31 14:14:41 -0800345 mPreferenceHighlighted);
Jason Monk65bb0972015-12-17 10:39:44 -0500346 return mAdapter;
347 }
348
Jason Monkf38fb382016-03-18 14:23:01 -0400349 protected void setAnimationAllowed(boolean animationAllowed) {
350 mAnimationAllowed = animationAllowed;
351 }
352
Jason Monk2071eda2016-02-25 13:55:48 -0500353 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
Fan Zhang3d516e72018-01-31 14:14:41 -0800354 mPreferenceCache = new ArrayMap<>();
Jason Monk2071eda2016-02-25 13:55:48 -0500355 final int N = group.getPreferenceCount();
356 for (int i = 0; i < N; i++) {
357 Preference p = group.getPreference(i);
358 if (TextUtils.isEmpty(p.getKey())) {
359 continue;
360 }
361 mPreferenceCache.put(p.getKey(), p);
362 }
363 }
364
365 protected Preference getCachedPreference(String key) {
366 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
367 }
368
369 protected void removeCachedPrefs(PreferenceGroup group) {
370 for (Preference p : mPreferenceCache.values()) {
371 group.removePreference(p);
372 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400373 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500374 }
375
Jason Monka6278442016-04-21 10:12:30 -0400376 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400377 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400378 }
379
Jan Nordqvist9eb43dd2018-03-26 15:29:44 -0700380 @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
381 public boolean removePreference(String key) {
Fan Zhange84407f2017-05-24 11:19:52 -0700382 return removePreference(getPreferenceScreen(), key);
383 }
384
385 @VisibleForTesting
386 boolean removePreference(PreferenceGroup group, String key) {
387 final int preferenceCount = group.getPreferenceCount();
388 for (int i = 0; i < preferenceCount; i++) {
389 final Preference preference = group.getPreference(i);
390 final String curKey = preference.getKey();
391
392 if (TextUtils.equals(curKey, key)) {
393 return group.removePreference(preference);
394 }
395
396 if (preference instanceof PreferenceGroup) {
397 if (removePreference((PreferenceGroup) preference, key)) {
398 return true;
399 }
400 }
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700401 }
Fan Zhange84407f2017-05-24 11:19:52 -0700402 return false;
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700403 }
404
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700405 /*
406 * The name is intentionally made different from Activity#finish(), so that
407 * users won't misunderstand its meaning.
408 */
409 public final void finishFragment() {
410 getActivity().onBackPressed();
411 }
412
Amith Yamasanid7993472010-08-18 13:59:28 -0700413 // Some helpers for functions used by the settings fragments when they were activities
414
415 /**
416 * Returns the ContentResolver from the owning Activity.
417 */
418 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700419 Context context = getActivity();
420 if (context != null) {
421 mContentResolver = context.getContentResolver();
422 }
423 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700424 }
425
426 /**
427 * Returns the specified system service from the owning Activity.
428 */
429 protected Object getSystemService(final String name) {
430 return getActivity().getSystemService(name);
431 }
432
433 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700434 * Returns the PackageManager from the owning Activity.
435 */
436 protected PackageManager getPackageManager() {
437 return getActivity().getPackageManager();
438 }
439
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800440 @Override
441 public void onDetach() {
442 if (isRemoving()) {
443 if (mDialogFragment != null) {
444 mDialogFragment.dismiss();
445 mDialogFragment = null;
446 }
447 }
448 super.onDetach();
449 }
450
Amith Yamasanid7993472010-08-18 13:59:28 -0700451 // Dialog management
452
453 protected void showDialog(int dialogId) {
454 if (mDialogFragment != null) {
455 Log.e(TAG, "Old dialog fragment not null!");
456 }
tmfangd5405cf2018-10-05 18:45:07 +0800457 mDialogFragment = SettingsDialogFragment.newInstance(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800458 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700459 }
460
Fan Zhangd65184f2016-09-19 17:45:24 -0700461 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700462 public Dialog onCreateDialog(int dialogId) {
463 return null;
464 }
465
Fan Zhangd65184f2016-09-19 17:45:24 -0700466 @Override
467 public int getDialogMetricsCategory(int dialogId) {
468 return 0;
469 }
470
Amith Yamasanid7993472010-08-18 13:59:28 -0700471 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800472 // mDialogFragment may not be visible yet in parent fragment's onResume().
473 // To be able to dismiss dialog at that time, don't check
474 // mDialogFragment.isVisible().
475 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400476 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700477 }
478 mDialogFragment = null;
479 }
480
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800481 /**
482 * Sets the OnCancelListener of the dialog shown. This method can only be
483 * called after showDialog(int) and before removeDialog(int). The method
484 * does nothing otherwise.
485 */
486 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
487 if (mDialogFragment != null) {
488 mDialogFragment.mOnCancelListener = listener;
489 }
490 }
491
492 /**
493 * Sets the OnDismissListener of the dialog shown. This method can only be
494 * called after showDialog(int) and before removeDialog(int). The method
495 * does nothing otherwise.
496 */
497 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
498 if (mDialogFragment != null) {
499 mDialogFragment.mOnDismissListener = listener;
500 }
501 }
502
Amith Yamasanic861cf82012-10-02 14:51:46 -0700503 public void onDialogShowing() {
504 // override in subclass to attach a dismiss listener, for instance
505 }
506
Jason Monk39b46742015-09-10 15:52:51 -0400507 @Override
508 public void onDisplayPreferenceDialog(Preference preference) {
509 if (preference.getKey() == null) {
510 // Auto-key preferences that don't have a key, so the dialog can find them.
511 preference.setKey(UUID.randomUUID().toString());
512 }
513 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000514 if (preference instanceof RestrictedListPreference) {
515 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
516 .newInstance(preference.getKey());
517 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400518 f = CustomListPreference.CustomListPreferenceDialogFragment
519 .newInstance(preference.getKey());
tmfang27c84de2018-06-28 11:39:05 +0800520 } else if (preference instanceof CustomDialogPreferenceCompat) {
521 f = CustomDialogPreferenceCompat.CustomPreferenceDialogFragment
Jason Monk39b46742015-09-10 15:52:51 -0400522 .newInstance(preference.getKey());
tmfang27c84de2018-06-28 11:39:05 +0800523 } else if (preference instanceof CustomEditTextPreferenceCompat) {
524 f = CustomEditTextPreferenceCompat.CustomPreferenceDialogFragment
Jason Monk39b46742015-09-10 15:52:51 -0400525 .newInstance(preference.getKey());
526 } else {
527 super.onDisplayPreferenceDialog(preference);
528 return;
529 }
530 f.setTargetFragment(this, 0);
531 f.show(getFragmentManager(), "dialog_preference");
532 onDialogShowing();
533 }
534
Fan Zhangd65184f2016-09-19 17:45:24 -0700535 public static class SettingsDialogFragment extends InstrumentedDialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800536 private static final String KEY_DIALOG_ID = "key_dialog_id";
537 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
538
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800539 private Fragment mParentFragment;
540
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800541 private DialogInterface.OnCancelListener mOnCancelListener;
542 private DialogInterface.OnDismissListener mOnDismissListener;
543
tmfangd5405cf2018-10-05 18:45:07 +0800544 public static SettingsDialogFragment newInstance(DialogCreatable fragment, int dialogId) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800545 if (!(fragment instanceof Fragment)) {
546 throw new IllegalArgumentException("fragment argument must be an instance of "
547 + Fragment.class.getName());
548 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800549
tmfangd5405cf2018-10-05 18:45:07 +0800550 final SettingsDialogFragment settingsDialogFragment = new SettingsDialogFragment();
551 settingsDialogFragment.setParentFragment(fragment);
552 settingsDialogFragment.setDialogId(dialogId);
553
554 return settingsDialogFragment;
555 }
Fan Zhangd65184f2016-09-19 17:45:24 -0700556
557 @Override
558 public int getMetricsCategory() {
tmfangd5405cf2018-10-05 18:45:07 +0800559 if (mParentFragment == null) {
Fan Zhang4fe7c082016-10-03 13:48:55 -0700560 return Instrumentable.METRICS_CATEGORY_UNKNOWN;
561 }
tmfangd5405cf2018-10-05 18:45:07 +0800562 final int metricsCategory =
563 ((DialogCreatable) mParentFragment).getDialogMetricsCategory(mDialogId);
Fan Zhangd65184f2016-09-19 17:45:24 -0700564 if (metricsCategory <= 0) {
565 throw new IllegalStateException("Dialog must provide a metrics category");
566 }
567 return metricsCategory;
568 }
569
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800570 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800571 public void onSaveInstanceState(Bundle outState) {
572 super.onSaveInstanceState(outState);
573 if (mParentFragment != null) {
574 outState.putInt(KEY_DIALOG_ID, mDialogId);
575 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
576 }
577 }
578
579 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700580 public void onStart() {
581 super.onStart();
582
583 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
584 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
585 }
586 }
587
588 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800589 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800590 if (savedInstanceState != null) {
591 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800592 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800593 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700594 if (mParentFragment == null) {
595 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
596 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800597 if (!(mParentFragment instanceof DialogCreatable)) {
598 throw new IllegalArgumentException(
599 (mParentFragment != null
600 ? mParentFragment.getClass().getName()
601 : mParentFragmentId)
602 + " must implement "
603 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800604 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800605 // This dialog fragment could be created from non-SettingsPreferenceFragment
606 if (mParentFragment instanceof SettingsPreferenceFragment) {
607 // restore mDialogFragment in mParentFragment
608 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
609 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800610 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800611 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700612 }
613
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800614 @Override
615 public void onCancel(DialogInterface dialog) {
616 super.onCancel(dialog);
617 if (mOnCancelListener != null) {
618 mOnCancelListener.onCancel(dialog);
619 }
620 }
621
622 @Override
623 public void onDismiss(DialogInterface dialog) {
624 super.onDismiss(dialog);
625 if (mOnDismissListener != null) {
626 mOnDismissListener.onDismiss(dialog);
627 }
628 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800629
Amith Yamasanid7993472010-08-18 13:59:28 -0700630 public int getDialogId() {
631 return mDialogId;
632 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800633
634 @Override
635 public void onDetach() {
636 super.onDetach();
637
Amith Yamasani8875ede2011-01-31 12:46:57 -0800638 // This dialog fragment could be created from non-SettingsPreferenceFragment
639 if (mParentFragment instanceof SettingsPreferenceFragment) {
640 // in case the dialog is not explicitly removed by removeDialog()
641 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
642 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
643 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800644 }
645 }
tmfangd5405cf2018-10-05 18:45:07 +0800646
647 private void setParentFragment(DialogCreatable fragment) {
648 mParentFragment = (Fragment) fragment;
649 }
650
651 private void setDialogId(int dialogId) {
652 mDialogId = dialogId;
653 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700654 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700655
656 protected boolean hasNextButton() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800657 return ((ButtonBarHandler) getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700658 }
659
660 protected Button getNextButton() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800661 return ((ButtonBarHandler) getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700662 }
663
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700664 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700665 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400666 if (activity == null) return;
667 if (getFragmentManager().getBackStackEntryCount() > 0) {
668 getFragmentManager().popBackStack();
669 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800670 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700671 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700672 }
673
Jason Monkb7e43802016-06-06 16:01:58 -0400674 protected Intent getIntent() {
675 if (getActivity() == null) {
676 return null;
677 }
678 return getActivity().getIntent();
679 }
680
681 protected void setResult(int result, Intent intent) {
682 if (getActivity() == null) {
683 return;
684 }
685 getActivity().setResult(result, intent);
686 }
687
688 protected void setResult(int result) {
689 if (getActivity() == null) {
690 return;
691 }
692 getActivity().setResult(result);
693 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700694}