blob: 5a5db454b5614b0f2a509b189aa03265017b8357 [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;
21import android.app.DialogFragment;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -070022import android.app.Fragment;
Amith Yamasanid7993472010-08-18 13:59:28 -070023import android.content.ContentResolver;
Amith Yamasani350938e2013-04-09 10:22:47 -070024import android.content.Context;
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +080025import android.content.DialogInterface;
Jason Monkb7e43802016-06-06 16:01:58 -040026import android.content.Intent;
Amith Yamasanid7993472010-08-18 13:59:28 -070027import android.content.pm.PackageManager;
Amith Yamasanid7993472010-08-18 13:59:28 -070028import android.os.Bundle;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070029import android.text.TextUtils;
Jason Monk2071eda2016-02-25 13:55:48 -050030import android.util.ArrayMap;
Amith Yamasanid7993472010-08-18 13:59:28 -070031import android.util.Log;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070032import android.view.LayoutInflater;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070033import android.view.View;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070034import android.view.ViewGroup;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070035import android.widget.Button;
Jason Monkb7e43802016-06-06 16:01:58 -040036
Jason Monk39b46742015-09-10 15:52:51 -040037import com.android.settings.applications.LayoutPreference;
Fan Zhang2d0b3442016-12-05 17:02:33 -080038import com.android.settings.core.InstrumentedPreferenceFragment;
Fan Zhangd65184f2016-09-19 17:45:24 -070039import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
Fan Zhang681a4cd2017-11-29 16:57:19 -080040import com.android.settings.search.actionbar.SearchMenuController;
Fan Zhange0b0e9f2017-11-29 14:55:59 -080041import com.android.settings.support.actionbar.HelpMenuController;
42import com.android.settings.support.actionbar.HelpResourceProvider;
Fan Zhang3d516e72018-01-31 14:14:41 -080043import com.android.settings.widget.HighlightablePreferenceGroupAdapter;
Fan Zhang896f1b32017-06-26 14:22:45 -070044import com.android.settings.widget.LoadingViewController;
Juan Lang44828122017-05-10 17:26:02 -070045import com.android.settingslib.CustomDialogPreference;
46import com.android.settingslib.CustomEditTextPreference;
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -080047import com.android.settingslib.core.instrumentation.Instrumentable;
Juan Lang777ed252017-05-09 15:42:36 -070048import com.android.settingslib.widget.FooterPreferenceMixin;
John Spurlockb8e02b82015-04-15 21:15:55 -040049
Jason Monk39b46742015-09-10 15:52:51 -040050import java.util.UUID;
51
Fan Zhangc7162cd2018-06-18 15:21:41 -070052import androidx.annotation.VisibleForTesting;
53import androidx.annotation.XmlRes;
54import androidx.preference.Preference;
55import androidx.preference.PreferenceGroup;
56import androidx.preference.PreferenceScreen;
57import androidx.recyclerview.widget.LinearLayoutManager;
58import androidx.recyclerview.widget.RecyclerView;
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 Zhange0b0e9f2017-11-29 14:55:59 -080064 implements DialogCreatable, HelpResourceProvider {
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
Fan Zhangd5b48452016-12-13 12:42:50 -080070 protected final FooterPreferenceMixin mFooterPreferenceMixin =
71 new FooterPreferenceMixin(this, getLifecycle());
72
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070073
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000074 private static final int ORDER_FIRST = -1;
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000075
Fan Zhange0b0e9f2017-11-29 14:55:59 -080076 private SettingsDialogFragment mDialogFragment;
Amith Yamasani350938e2013-04-09 10:22:47 -070077 // Cache the content resolver for async callbacks
78 private ContentResolver mContentResolver;
79
Jason Monk39b46742015-09-10 15:52:51 -040080 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070081 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040082 private RecyclerView.AdapterDataObserver mDataSetObserver =
83 new RecyclerView.AdapterDataObserver() {
Tony Mantler0b825f52016-09-27 14:48:16 -070084 @Override
85 public void onChanged() {
86 onDataSetChanged();
87 }
88
89 @Override
90 public void onItemRangeChanged(int positionStart, int itemCount) {
91 onDataSetChanged();
92 }
93
94 @Override
95 public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
96 onDataSetChanged();
97 }
98
99 @Override
100 public void onItemRangeInserted(int positionStart, int itemCount) {
101 onDataSetChanged();
102 }
103
104 @Override
105 public void onItemRangeRemoved(int positionStart, int itemCount) {
106 onDataSetChanged();
107 }
108
109 @Override
110 public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
111 onDataSetChanged();
112 }
113 };
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700114
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700115 private ViewGroup mPinnedHeaderFrameLayout;
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900116 private ViewGroup mButtonBar;
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700117
Jason Monk39b46742015-09-10 15:52:51 -0400118 private LayoutPreference mHeader;
119
Jason Monk39b46742015-09-10 15:52:51 -0400120 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -0500121 private LinearLayoutManager mLayoutManager;
Jason Monk2071eda2016-02-25 13:55:48 -0500122 private ArrayMap<String, Preference> mPreferenceCache;
Jason Monkf38fb382016-03-18 14:23:01 -0400123 private boolean mAnimationAllowed;
Jason Monk39b46742015-09-10 15:52:51 -0400124
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700125 @VisibleForTesting
126 public HighlightablePreferenceGroupAdapter mAdapter;
127 @VisibleForTesting
128 public boolean mPreferenceHighlighted = false;
129
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700130 @Override
131 public void onCreate(Bundle icicle) {
132 super.onCreate(icicle);
Fan Zhang681a4cd2017-11-29 16:57:19 -0800133 SearchMenuController.init(this /* host */);
Fan Zhange0b0e9f2017-11-29 14:55:59 -0800134 HelpMenuController.init(this /* host */);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700135
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700136 if (icicle != null) {
137 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
138 }
Fan Zhang72456a92018-02-20 11:25:56 -0800139 HighlightablePreferenceGroupAdapter.adjustInitialExpandedChildCount(this /* host */);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700140 }
141
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700142 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700143 public View onCreateView(LayoutInflater inflater, ViewGroup container,
144 Bundle savedInstanceState) {
145 final View root = super.onCreateView(inflater, container, savedInstanceState);
Fan Zhange0b0e9f2017-11-29 14:55:59 -0800146 mPinnedHeaderFrameLayout = root.findViewById(R.id.pinned_header);
147 mButtonBar = root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700148 return root;
149 }
150
Jason Monk39b46742015-09-10 15:52:51 -0400151 @Override
Jason Monk91e2f892016-02-23 15:31:09 -0500152 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
153 super.addPreferencesFromResource(preferencesResId);
154 checkAvailablePrefs(getPreferenceScreen());
155 }
156
157 private void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
158 if (preferenceGroup == null) return;
159 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
160 Preference pref = preferenceGroup.getPreference(i);
161 if (pref instanceof SelfAvailablePreference
162 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
163 preferenceGroup.removePreference(pref);
164 } else if (pref instanceof PreferenceGroup) {
165 checkAvailablePrefs((PreferenceGroup) pref);
166 }
167 }
168 }
169
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900170 public ViewGroup getButtonBar() {
171 return mButtonBar;
172 }
173
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700174 public View setPinnedHeaderView(int layoutResId) {
175 final LayoutInflater inflater = getActivity().getLayoutInflater();
176 final View pinnedHeader =
177 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
178 setPinnedHeaderView(pinnedHeader);
179 return pinnedHeader;
180 }
181
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700182 public void setPinnedHeaderView(View pinnedHeader) {
183 mPinnedHeaderFrameLayout.addView(pinnedHeader);
184 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
185 }
186
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700187 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700188 public void onSaveInstanceState(Bundle outState) {
189 super.onSaveInstanceState(outState);
190
Fan Zhang3d516e72018-01-31 14:14:41 -0800191 if (mAdapter != null) {
192 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mAdapter.isHighlightRequested());
193 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700194 }
195
196 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700197 public void onActivityCreated(Bundle savedInstanceState) {
198 super.onActivityCreated(savedInstanceState);
Johan Redestig76218e52016-04-19 08:29:30 +0200199 setHasOptionsMenu(true);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700200 }
201
202 @Override
203 public void onResume() {
204 super.onResume();
Fan Zhang3d516e72018-01-31 14:14:41 -0800205 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700206 }
207
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700208 @Override
209 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700210 registerObserverIfNeeded();
211 }
212
213 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700214 protected void onUnbindPreferences() {
215 unregisterObserverIfNeeded();
216 }
217
Jason Monkb37e2882016-01-11 14:27:20 -0500218 public void setLoading(boolean loading, boolean animate) {
Fan Zhang896f1b32017-06-26 14:22:45 -0700219 View loadingContainer = getView().findViewById(R.id.loading_container);
220 LoadingViewController.handleLoadingContainer(loadingContainer, getListView(),
221 !loading /* done */,
222 animate);
Jason Monkb37e2882016-01-11 14:27:20 -0500223 }
224
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700225 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700226 if (!mIsDataSetObserverRegistered) {
227 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400228 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700229 }
Jason Monk39b46742015-09-10 15:52:51 -0400230 mCurrentRootAdapter = getListView().getAdapter();
231 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700232 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500233 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700234 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700235 }
236
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700237 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700238 if (mIsDataSetObserverRegistered) {
239 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400240 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700241 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700242 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700243 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700244 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700245 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700246
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700247 public void highlightPreferenceIfNeeded() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800248 if (!isAdded()) {
249 return;
250 }
251 if (mAdapter != null) {
252 mAdapter.requestHighlight(getView(), getListView());
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700253 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700254 }
255
Fan Zhang72456a92018-02-20 11:25:56 -0800256 /**
257 * Returns initial expanded child count.
258 * <p/>
259 * Only override this method if the initial expanded child must be determined at run time.
260 */
261 public int getInitialExpandedChildCount() {
262 return 0;
263 }
264
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000265 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400266 highlightPreferenceIfNeeded();
267 updateEmptyView();
268 }
269
Jason Monk39b46742015-09-10 15:52:51 -0400270 public LayoutPreference getHeaderView() {
271 return mHeader;
272 }
273
Jason Monk39b46742015-09-10 15:52:51 -0400274 protected void setHeaderView(int resource) {
275 mHeader = new LayoutPreference(getPrefContext(), resource);
Udam Sainid553abc2016-02-16 17:54:13 -0800276 addPreferenceToTop(mHeader);
277 }
278
279 protected void setHeaderView(View view) {
280 mHeader = new LayoutPreference(getPrefContext(), view);
281 addPreferenceToTop(mHeader);
282 }
283
284 private void addPreferenceToTop(LayoutPreference preference) {
285 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400286 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800287 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400288 }
289 }
290
Jason Monk39b46742015-09-10 15:52:51 -0400291 @Override
292 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400293 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400294 // Without ids generated, the RecyclerView won't animate changes to the preferences.
295 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
296 }
Jason Monk39b46742015-09-10 15:52:51 -0400297 super.setPreferenceScreen(preferenceScreen);
298 if (preferenceScreen != null) {
299 if (mHeader != null) {
300 preferenceScreen.addPreference(mHeader);
301 }
Jason Monk39b46742015-09-10 15:52:51 -0400302 }
303 }
304
jackqdyulei2b2abac2017-05-26 10:47:55 -0700305 @VisibleForTesting
306 void updateEmptyView() {
Jason Monk39b46742015-09-10 15:52:51 -0400307 if (mEmptyView == null) return;
308 if (getPreferenceScreen() != null) {
jackqdyulei2b2abac2017-05-26 10:47:55 -0700309 final View listContainer = getActivity().findViewById(android.R.id.list_container);
Jason Monk39b46742015-09-10 15:52:51 -0400310 boolean show = (getPreferenceScreen().getPreferenceCount()
311 - (mHeader != null ? 1 : 0)
jackqdyulei2b2abac2017-05-26 10:47:55 -0700312 - (mFooterPreferenceMixin.hasFooter() ? 1 : 0)) <= 0
313 || (listContainer != null && listContainer.getVisibility() != View.VISIBLE);
Jason Monk39b46742015-09-10 15:52:51 -0400314 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
315 } else {
316 mEmptyView.setVisibility(View.VISIBLE);
317 }
318 }
319
320 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000321 if (mEmptyView != null) {
322 mEmptyView.setVisibility(View.GONE);
323 }
Jason Monk39b46742015-09-10 15:52:51 -0400324 mEmptyView = v;
325 updateEmptyView();
326 }
327
328 public View getEmptyView() {
329 return mEmptyView;
330 }
331
Jason Monk65bb0972015-12-17 10:39:44 -0500332 @Override
333 public RecyclerView.LayoutManager onCreateLayoutManager() {
334 mLayoutManager = new LinearLayoutManager(getContext());
335 return mLayoutManager;
336 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700337
Jason Monk65bb0972015-12-17 10:39:44 -0500338 @Override
339 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
Fan Zhanga1000462018-02-02 12:15:37 -0800340 final Bundle arguments = getArguments();
Fan Zhang3d516e72018-01-31 14:14:41 -0800341 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen,
Fan Zhanga1000462018-02-02 12:15:37 -0800342 arguments == null
343 ? null : arguments.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY),
Fan Zhang3d516e72018-01-31 14:14:41 -0800344 mPreferenceHighlighted);
Jason Monk65bb0972015-12-17 10:39:44 -0500345 return mAdapter;
346 }
347
Jason Monkf38fb382016-03-18 14:23:01 -0400348 protected void setAnimationAllowed(boolean animationAllowed) {
349 mAnimationAllowed = animationAllowed;
350 }
351
Jason Monk2071eda2016-02-25 13:55:48 -0500352 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
Fan Zhang3d516e72018-01-31 14:14:41 -0800353 mPreferenceCache = new ArrayMap<>();
Jason Monk2071eda2016-02-25 13:55:48 -0500354 final int N = group.getPreferenceCount();
355 for (int i = 0; i < N; i++) {
356 Preference p = group.getPreference(i);
357 if (TextUtils.isEmpty(p.getKey())) {
358 continue;
359 }
360 mPreferenceCache.put(p.getKey(), p);
361 }
362 }
363
364 protected Preference getCachedPreference(String key) {
365 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
366 }
367
368 protected void removeCachedPrefs(PreferenceGroup group) {
369 for (Preference p : mPreferenceCache.values()) {
370 group.removePreference(p);
371 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400372 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500373 }
374
Jason Monka6278442016-04-21 10:12:30 -0400375 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400376 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400377 }
378
Jan Nordqvist9eb43dd2018-03-26 15:29:44 -0700379 @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
380 public boolean removePreference(String key) {
Fan Zhange84407f2017-05-24 11:19:52 -0700381 return removePreference(getPreferenceScreen(), key);
382 }
383
384 @VisibleForTesting
385 boolean removePreference(PreferenceGroup group, String key) {
386 final int preferenceCount = group.getPreferenceCount();
387 for (int i = 0; i < preferenceCount; i++) {
388 final Preference preference = group.getPreference(i);
389 final String curKey = preference.getKey();
390
391 if (TextUtils.equals(curKey, key)) {
392 return group.removePreference(preference);
393 }
394
395 if (preference instanceof PreferenceGroup) {
396 if (removePreference((PreferenceGroup) preference, key)) {
397 return true;
398 }
399 }
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700400 }
Fan Zhange84407f2017-05-24 11:19:52 -0700401 return false;
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700402 }
403
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700404 /*
405 * The name is intentionally made different from Activity#finish(), so that
406 * users won't misunderstand its meaning.
407 */
408 public final void finishFragment() {
409 getActivity().onBackPressed();
410 }
411
Amith Yamasanid7993472010-08-18 13:59:28 -0700412 // Some helpers for functions used by the settings fragments when they were activities
413
414 /**
415 * Returns the ContentResolver from the owning Activity.
416 */
417 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700418 Context context = getActivity();
419 if (context != null) {
420 mContentResolver = context.getContentResolver();
421 }
422 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700423 }
424
425 /**
426 * Returns the specified system service from the owning Activity.
427 */
428 protected Object getSystemService(final String name) {
429 return getActivity().getSystemService(name);
430 }
431
432 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700433 * Returns the PackageManager from the owning Activity.
434 */
435 protected PackageManager getPackageManager() {
436 return getActivity().getPackageManager();
437 }
438
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800439 @Override
440 public void onDetach() {
441 if (isRemoving()) {
442 if (mDialogFragment != null) {
443 mDialogFragment.dismiss();
444 mDialogFragment = null;
445 }
446 }
447 super.onDetach();
448 }
449
Amith Yamasanid7993472010-08-18 13:59:28 -0700450 // Dialog management
451
452 protected void showDialog(int dialogId) {
453 if (mDialogFragment != null) {
454 Log.e(TAG, "Old dialog fragment not null!");
455 }
456 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800457 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700458 }
459
Fan Zhangd65184f2016-09-19 17:45:24 -0700460 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700461 public Dialog onCreateDialog(int dialogId) {
462 return null;
463 }
464
Fan Zhangd65184f2016-09-19 17:45:24 -0700465 @Override
466 public int getDialogMetricsCategory(int dialogId) {
467 return 0;
468 }
469
Amith Yamasanid7993472010-08-18 13:59:28 -0700470 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800471 // mDialogFragment may not be visible yet in parent fragment's onResume().
472 // To be able to dismiss dialog at that time, don't check
473 // mDialogFragment.isVisible().
474 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400475 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700476 }
477 mDialogFragment = null;
478 }
479
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800480 /**
481 * Sets the OnCancelListener of the dialog shown. This method can only be
482 * called after showDialog(int) and before removeDialog(int). The method
483 * does nothing otherwise.
484 */
485 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
486 if (mDialogFragment != null) {
487 mDialogFragment.mOnCancelListener = listener;
488 }
489 }
490
491 /**
492 * Sets the OnDismissListener of the dialog shown. This method can only be
493 * called after showDialog(int) and before removeDialog(int). The method
494 * does nothing otherwise.
495 */
496 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
497 if (mDialogFragment != null) {
498 mDialogFragment.mOnDismissListener = listener;
499 }
500 }
501
Amith Yamasanic861cf82012-10-02 14:51:46 -0700502 public void onDialogShowing() {
503 // override in subclass to attach a dismiss listener, for instance
504 }
505
Jason Monk39b46742015-09-10 15:52:51 -0400506 @Override
507 public void onDisplayPreferenceDialog(Preference preference) {
508 if (preference.getKey() == null) {
509 // Auto-key preferences that don't have a key, so the dialog can find them.
510 preference.setKey(UUID.randomUUID().toString());
511 }
512 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000513 if (preference instanceof RestrictedListPreference) {
514 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
515 .newInstance(preference.getKey());
516 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400517 f = CustomListPreference.CustomListPreferenceDialogFragment
518 .newInstance(preference.getKey());
519 } else if (preference instanceof CustomDialogPreference) {
520 f = CustomDialogPreference.CustomPreferenceDialogFragment
521 .newInstance(preference.getKey());
522 } else if (preference instanceof CustomEditTextPreference) {
523 f = CustomEditTextPreference.CustomPreferenceDialogFragment
524 .newInstance(preference.getKey());
525 } else {
526 super.onDisplayPreferenceDialog(preference);
527 return;
528 }
529 f.setTargetFragment(this, 0);
530 f.show(getFragmentManager(), "dialog_preference");
531 onDialogShowing();
532 }
533
Fan Zhangd65184f2016-09-19 17:45:24 -0700534 public static class SettingsDialogFragment extends InstrumentedDialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800535 private static final String KEY_DIALOG_ID = "key_dialog_id";
536 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
537
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800538 private Fragment mParentFragment;
539
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800540 private DialogInterface.OnCancelListener mOnCancelListener;
541 private DialogInterface.OnDismissListener mOnDismissListener;
542
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800543 public SettingsDialogFragment() {
544 /* do nothing */
545 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700546
Amith Yamasani43c69782010-12-01 09:04:36 -0800547 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Fan Zhangd65184f2016-09-19 17:45:24 -0700548 super(fragment, dialogId);
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800549 if (!(fragment instanceof Fragment)) {
550 throw new IllegalArgumentException("fragment argument must be an instance of "
551 + Fragment.class.getName());
552 }
553 mParentFragment = (Fragment) fragment;
554 }
555
Fan Zhangd65184f2016-09-19 17:45:24 -0700556
557 @Override
558 public int getMetricsCategory() {
Fan Zhang4fe7c082016-10-03 13:48:55 -0700559 if (mDialogCreatable == null) {
560 return Instrumentable.METRICS_CATEGORY_UNKNOWN;
561 }
Fan Zhangd65184f2016-09-19 17:45:24 -0700562 final int metricsCategory = mDialogCreatable.getDialogMetricsCategory(mDialogId);
563 if (metricsCategory <= 0) {
564 throw new IllegalStateException("Dialog must provide a metrics category");
565 }
566 return metricsCategory;
567 }
568
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800569 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800570 public void onSaveInstanceState(Bundle outState) {
571 super.onSaveInstanceState(outState);
572 if (mParentFragment != null) {
573 outState.putInt(KEY_DIALOG_ID, mDialogId);
574 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
575 }
576 }
577
578 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700579 public void onStart() {
580 super.onStart();
581
582 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
583 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
584 }
585 }
586
587 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800588 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800589 if (savedInstanceState != null) {
590 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800591 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800592 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700593 if (mParentFragment == null) {
594 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
595 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800596 if (!(mParentFragment instanceof DialogCreatable)) {
597 throw new IllegalArgumentException(
598 (mParentFragment != null
599 ? mParentFragment.getClass().getName()
600 : mParentFragmentId)
601 + " must implement "
602 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800603 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800604 // This dialog fragment could be created from non-SettingsPreferenceFragment
605 if (mParentFragment instanceof SettingsPreferenceFragment) {
606 // restore mDialogFragment in mParentFragment
607 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
608 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800609 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800610 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700611 }
612
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800613 @Override
614 public void onCancel(DialogInterface dialog) {
615 super.onCancel(dialog);
616 if (mOnCancelListener != null) {
617 mOnCancelListener.onCancel(dialog);
618 }
619 }
620
621 @Override
622 public void onDismiss(DialogInterface dialog) {
623 super.onDismiss(dialog);
624 if (mOnDismissListener != null) {
625 mOnDismissListener.onDismiss(dialog);
626 }
627 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800628
Amith Yamasanid7993472010-08-18 13:59:28 -0700629 public int getDialogId() {
630 return mDialogId;
631 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800632
633 @Override
634 public void onDetach() {
635 super.onDetach();
636
Amith Yamasani8875ede2011-01-31 12:46:57 -0800637 // This dialog fragment could be created from non-SettingsPreferenceFragment
638 if (mParentFragment instanceof SettingsPreferenceFragment) {
639 // in case the dialog is not explicitly removed by removeDialog()
640 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
641 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
642 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800643 }
644 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700645 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700646
647 protected boolean hasNextButton() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800648 return ((ButtonBarHandler) getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700649 }
650
651 protected Button getNextButton() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800652 return ((ButtonBarHandler) getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700653 }
654
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700655 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700656 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400657 if (activity == null) return;
658 if (getFragmentManager().getBackStackEntryCount() > 0) {
659 getFragmentManager().popBackStack();
660 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800661 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700662 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700663 }
664
Jason Monkb7e43802016-06-06 16:01:58 -0400665 protected Intent getIntent() {
666 if (getActivity() == null) {
667 return null;
668 }
669 return getActivity().getIntent();
670 }
671
672 protected void setResult(int result, Intent intent) {
673 if (getActivity() == null) {
674 return;
675 }
676 getActivity().setResult(result, intent);
677 }
678
679 protected void setResult(int result) {
680 if (getActivity() == null) {
681 return;
682 }
683 getActivity().setResult(result);
684 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700685}