blob: 6b29b2e1e482d98537eeb5634568bd1abdba19e6 [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);
Udam Sainid553abc2016-02-16 17:54:13 -0800278 addPreferenceToTop(mHeader);
279 }
280
281 protected void setHeaderView(View view) {
282 mHeader = new LayoutPreference(getPrefContext(), view);
283 addPreferenceToTop(mHeader);
284 }
285
286 private void addPreferenceToTop(LayoutPreference preference) {
287 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400288 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800289 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400290 }
291 }
292
Jason Monk39b46742015-09-10 15:52:51 -0400293 @Override
294 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400295 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400296 // Without ids generated, the RecyclerView won't animate changes to the preferences.
297 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
298 }
Jason Monk39b46742015-09-10 15:52:51 -0400299 super.setPreferenceScreen(preferenceScreen);
300 if (preferenceScreen != null) {
301 if (mHeader != null) {
302 preferenceScreen.addPreference(mHeader);
303 }
Jason Monk39b46742015-09-10 15:52:51 -0400304 }
305 }
306
jackqdyulei2b2abac2017-05-26 10:47:55 -0700307 @VisibleForTesting
308 void updateEmptyView() {
Jason Monk39b46742015-09-10 15:52:51 -0400309 if (mEmptyView == null) return;
310 if (getPreferenceScreen() != null) {
jackqdyulei2b2abac2017-05-26 10:47:55 -0700311 final View listContainer = getActivity().findViewById(android.R.id.list_container);
Jason Monk39b46742015-09-10 15:52:51 -0400312 boolean show = (getPreferenceScreen().getPreferenceCount()
313 - (mHeader != null ? 1 : 0)
jackqdyulei2b2abac2017-05-26 10:47:55 -0700314 - (mFooterPreferenceMixin.hasFooter() ? 1 : 0)) <= 0
315 || (listContainer != null && listContainer.getVisibility() != View.VISIBLE);
Jason Monk39b46742015-09-10 15:52:51 -0400316 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
317 } else {
318 mEmptyView.setVisibility(View.VISIBLE);
319 }
320 }
321
322 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000323 if (mEmptyView != null) {
324 mEmptyView.setVisibility(View.GONE);
325 }
Jason Monk39b46742015-09-10 15:52:51 -0400326 mEmptyView = v;
327 updateEmptyView();
328 }
329
330 public View getEmptyView() {
331 return mEmptyView;
332 }
333
Jason Monk65bb0972015-12-17 10:39:44 -0500334 @Override
335 public RecyclerView.LayoutManager onCreateLayoutManager() {
336 mLayoutManager = new LinearLayoutManager(getContext());
337 return mLayoutManager;
338 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700339
Jason Monk65bb0972015-12-17 10:39:44 -0500340 @Override
341 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
Fan Zhanga1000462018-02-02 12:15:37 -0800342 final Bundle arguments = getArguments();
Fan Zhang3d516e72018-01-31 14:14:41 -0800343 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen,
Fan Zhanga1000462018-02-02 12:15:37 -0800344 arguments == null
345 ? null : arguments.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY),
Fan Zhang3d516e72018-01-31 14:14:41 -0800346 mPreferenceHighlighted);
Jason Monk65bb0972015-12-17 10:39:44 -0500347 return mAdapter;
348 }
349
Jason Monkf38fb382016-03-18 14:23:01 -0400350 protected void setAnimationAllowed(boolean animationAllowed) {
351 mAnimationAllowed = animationAllowed;
352 }
353
Jason Monk2071eda2016-02-25 13:55:48 -0500354 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
Fan Zhang3d516e72018-01-31 14:14:41 -0800355 mPreferenceCache = new ArrayMap<>();
Jason Monk2071eda2016-02-25 13:55:48 -0500356 final int N = group.getPreferenceCount();
357 for (int i = 0; i < N; i++) {
358 Preference p = group.getPreference(i);
359 if (TextUtils.isEmpty(p.getKey())) {
360 continue;
361 }
362 mPreferenceCache.put(p.getKey(), p);
363 }
364 }
365
366 protected Preference getCachedPreference(String key) {
367 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
368 }
369
370 protected void removeCachedPrefs(PreferenceGroup group) {
371 for (Preference p : mPreferenceCache.values()) {
372 group.removePreference(p);
373 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400374 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500375 }
376
Jason Monka6278442016-04-21 10:12:30 -0400377 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400378 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400379 }
380
Jan Nordqvist9eb43dd2018-03-26 15:29:44 -0700381 @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
382 public boolean removePreference(String key) {
Fan Zhange84407f2017-05-24 11:19:52 -0700383 return removePreference(getPreferenceScreen(), key);
384 }
385
386 @VisibleForTesting
387 boolean removePreference(PreferenceGroup group, String key) {
388 final int preferenceCount = group.getPreferenceCount();
389 for (int i = 0; i < preferenceCount; i++) {
390 final Preference preference = group.getPreference(i);
391 final String curKey = preference.getKey();
392
393 if (TextUtils.equals(curKey, key)) {
394 return group.removePreference(preference);
395 }
396
397 if (preference instanceof PreferenceGroup) {
398 if (removePreference((PreferenceGroup) preference, key)) {
399 return true;
400 }
401 }
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700402 }
Fan Zhange84407f2017-05-24 11:19:52 -0700403 return false;
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700404 }
405
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700406 /*
407 * The name is intentionally made different from Activity#finish(), so that
408 * users won't misunderstand its meaning.
409 */
410 public final void finishFragment() {
411 getActivity().onBackPressed();
412 }
413
Amith Yamasanid7993472010-08-18 13:59:28 -0700414 // Some helpers for functions used by the settings fragments when they were activities
415
416 /**
417 * Returns the ContentResolver from the owning Activity.
418 */
419 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700420 Context context = getActivity();
421 if (context != null) {
422 mContentResolver = context.getContentResolver();
423 }
424 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700425 }
426
427 /**
428 * Returns the specified system service from the owning Activity.
429 */
430 protected Object getSystemService(final String name) {
431 return getActivity().getSystemService(name);
432 }
433
434 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700435 * Returns the PackageManager from the owning Activity.
436 */
437 protected PackageManager getPackageManager() {
438 return getActivity().getPackageManager();
439 }
440
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800441 @Override
442 public void onDetach() {
443 if (isRemoving()) {
444 if (mDialogFragment != null) {
445 mDialogFragment.dismiss();
446 mDialogFragment = null;
447 }
448 }
449 super.onDetach();
450 }
451
Amith Yamasanid7993472010-08-18 13:59:28 -0700452 // Dialog management
453
454 protected void showDialog(int dialogId) {
455 if (mDialogFragment != null) {
456 Log.e(TAG, "Old dialog fragment not null!");
457 }
tmfangd5405cf2018-10-05 18:45:07 +0800458 mDialogFragment = SettingsDialogFragment.newInstance(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800459 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700460 }
461
Fan Zhangd65184f2016-09-19 17:45:24 -0700462 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700463 public Dialog onCreateDialog(int dialogId) {
464 return null;
465 }
466
Fan Zhangd65184f2016-09-19 17:45:24 -0700467 @Override
468 public int getDialogMetricsCategory(int dialogId) {
469 return 0;
470 }
471
Amith Yamasanid7993472010-08-18 13:59:28 -0700472 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800473 // mDialogFragment may not be visible yet in parent fragment's onResume().
474 // To be able to dismiss dialog at that time, don't check
475 // mDialogFragment.isVisible().
476 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400477 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700478 }
479 mDialogFragment = null;
480 }
481
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800482 /**
483 * Sets the OnCancelListener of the dialog shown. This method can only be
484 * called after showDialog(int) and before removeDialog(int). The method
485 * does nothing otherwise.
486 */
487 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
488 if (mDialogFragment != null) {
489 mDialogFragment.mOnCancelListener = listener;
490 }
491 }
492
493 /**
494 * Sets the OnDismissListener of the dialog shown. This method can only be
495 * called after showDialog(int) and before removeDialog(int). The method
496 * does nothing otherwise.
497 */
498 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
499 if (mDialogFragment != null) {
500 mDialogFragment.mOnDismissListener = listener;
501 }
502 }
503
Amith Yamasanic861cf82012-10-02 14:51:46 -0700504 public void onDialogShowing() {
505 // override in subclass to attach a dismiss listener, for instance
506 }
507
Jason Monk39b46742015-09-10 15:52:51 -0400508 @Override
509 public void onDisplayPreferenceDialog(Preference preference) {
510 if (preference.getKey() == null) {
511 // Auto-key preferences that don't have a key, so the dialog can find them.
512 preference.setKey(UUID.randomUUID().toString());
513 }
514 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000515 if (preference instanceof RestrictedListPreference) {
516 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
517 .newInstance(preference.getKey());
518 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400519 f = CustomListPreference.CustomListPreferenceDialogFragment
520 .newInstance(preference.getKey());
tmfang27c84de2018-06-28 11:39:05 +0800521 } else if (preference instanceof CustomDialogPreferenceCompat) {
522 f = CustomDialogPreferenceCompat.CustomPreferenceDialogFragment
Jason Monk39b46742015-09-10 15:52:51 -0400523 .newInstance(preference.getKey());
tmfang27c84de2018-06-28 11:39:05 +0800524 } else if (preference instanceof CustomEditTextPreferenceCompat) {
525 f = CustomEditTextPreferenceCompat.CustomPreferenceDialogFragment
Jason Monk39b46742015-09-10 15:52:51 -0400526 .newInstance(preference.getKey());
527 } else {
528 super.onDisplayPreferenceDialog(preference);
529 return;
530 }
531 f.setTargetFragment(this, 0);
532 f.show(getFragmentManager(), "dialog_preference");
533 onDialogShowing();
534 }
535
Fan Zhangd65184f2016-09-19 17:45:24 -0700536 public static class SettingsDialogFragment extends InstrumentedDialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800537 private static final String KEY_DIALOG_ID = "key_dialog_id";
538 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
539
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800540 private Fragment mParentFragment;
541
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800542 private DialogInterface.OnCancelListener mOnCancelListener;
543 private DialogInterface.OnDismissListener mOnDismissListener;
544
tmfangd5405cf2018-10-05 18:45:07 +0800545 public static SettingsDialogFragment newInstance(DialogCreatable fragment, int dialogId) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800546 if (!(fragment instanceof Fragment)) {
547 throw new IllegalArgumentException("fragment argument must be an instance of "
548 + Fragment.class.getName());
549 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800550
tmfangd5405cf2018-10-05 18:45:07 +0800551 final SettingsDialogFragment settingsDialogFragment = new SettingsDialogFragment();
552 settingsDialogFragment.setParentFragment(fragment);
553 settingsDialogFragment.setDialogId(dialogId);
554
555 return settingsDialogFragment;
556 }
Fan Zhangd65184f2016-09-19 17:45:24 -0700557
558 @Override
559 public int getMetricsCategory() {
tmfangd5405cf2018-10-05 18:45:07 +0800560 if (mParentFragment == null) {
Fan Zhang4fe7c082016-10-03 13:48:55 -0700561 return Instrumentable.METRICS_CATEGORY_UNKNOWN;
562 }
tmfangd5405cf2018-10-05 18:45:07 +0800563 final int metricsCategory =
564 ((DialogCreatable) mParentFragment).getDialogMetricsCategory(mDialogId);
Fan Zhangd65184f2016-09-19 17:45:24 -0700565 if (metricsCategory <= 0) {
566 throw new IllegalStateException("Dialog must provide a metrics category");
567 }
568 return metricsCategory;
569 }
570
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800571 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800572 public void onSaveInstanceState(Bundle outState) {
573 super.onSaveInstanceState(outState);
574 if (mParentFragment != null) {
575 outState.putInt(KEY_DIALOG_ID, mDialogId);
576 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
577 }
578 }
579
580 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700581 public void onStart() {
582 super.onStart();
583
584 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
585 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
586 }
587 }
588
589 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800590 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800591 if (savedInstanceState != null) {
592 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800593 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800594 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700595 if (mParentFragment == null) {
596 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
597 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800598 if (!(mParentFragment instanceof DialogCreatable)) {
599 throw new IllegalArgumentException(
600 (mParentFragment != null
601 ? mParentFragment.getClass().getName()
602 : mParentFragmentId)
603 + " must implement "
604 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800605 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800606 // This dialog fragment could be created from non-SettingsPreferenceFragment
607 if (mParentFragment instanceof SettingsPreferenceFragment) {
608 // restore mDialogFragment in mParentFragment
609 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
610 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800611 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800612 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700613 }
614
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800615 @Override
616 public void onCancel(DialogInterface dialog) {
617 super.onCancel(dialog);
618 if (mOnCancelListener != null) {
619 mOnCancelListener.onCancel(dialog);
620 }
621 }
622
623 @Override
624 public void onDismiss(DialogInterface dialog) {
625 super.onDismiss(dialog);
626 if (mOnDismissListener != null) {
627 mOnDismissListener.onDismiss(dialog);
628 }
629 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800630
Amith Yamasanid7993472010-08-18 13:59:28 -0700631 public int getDialogId() {
632 return mDialogId;
633 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800634
635 @Override
636 public void onDetach() {
637 super.onDetach();
638
Amith Yamasani8875ede2011-01-31 12:46:57 -0800639 // This dialog fragment could be created from non-SettingsPreferenceFragment
640 if (mParentFragment instanceof SettingsPreferenceFragment) {
641 // in case the dialog is not explicitly removed by removeDialog()
642 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
643 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
644 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800645 }
646 }
tmfangd5405cf2018-10-05 18:45:07 +0800647
648 private void setParentFragment(DialogCreatable fragment) {
649 mParentFragment = (Fragment) fragment;
650 }
651
652 private void setDialogId(int dialogId) {
653 mDialogId = dialogId;
654 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700655 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700656
657 protected boolean hasNextButton() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800658 return ((ButtonBarHandler) getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700659 }
660
661 protected Button getNextButton() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800662 return ((ButtonBarHandler) getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700663 }
664
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700665 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700666 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400667 if (activity == null) return;
668 if (getFragmentManager().getBackStackEntryCount() > 0) {
669 getFragmentManager().popBackStack();
670 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800671 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700672 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700673 }
674
Jason Monkb7e43802016-06-06 16:01:58 -0400675 protected Intent getIntent() {
676 if (getActivity() == null) {
677 return null;
678 }
679 return getActivity().getIntent();
680 }
681
682 protected void setResult(int result, Intent intent) {
683 if (getActivity() == null) {
684 return;
685 }
686 getActivity().setResult(result, intent);
687 }
688
689 protected void setResult(int result) {
690 if (getActivity() == null) {
691 return;
692 }
693 getActivity().setResult(result);
694 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700695}