blob: bf4da663590538ba43a1bd6ef22837b03eaa06f4 [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;
Fan Zhange84407f2017-05-24 11:19:52 -070029import android.support.annotation.VisibleForTesting;
Jason Monk91e2f892016-02-23 15:31:09 -050030import android.support.annotation.XmlRes;
Jason Monk39b46742015-09-10 15:52:51 -040031import android.support.v7.preference.Preference;
Jason Monk65bb0972015-12-17 10:39:44 -050032import android.support.v7.preference.PreferenceGroup;
Jason Monk39b46742015-09-10 15:52:51 -040033import android.support.v7.preference.PreferenceScreen;
Jason Monk65bb0972015-12-17 10:39:44 -050034import android.support.v7.widget.LinearLayoutManager;
Jason Monk39b46742015-09-10 15:52:51 -040035import android.support.v7.widget.RecyclerView;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070036import android.text.TextUtils;
Jason Monk2071eda2016-02-25 13:55:48 -050037import android.util.ArrayMap;
Amith Yamasanid7993472010-08-18 13:59:28 -070038import android.util.Log;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070039import android.view.LayoutInflater;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070040import android.view.View;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070041import android.view.ViewGroup;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070042import android.widget.Button;
Jason Monkb7e43802016-06-06 16:01:58 -040043
Jason Monk39b46742015-09-10 15:52:51 -040044import com.android.settings.applications.LayoutPreference;
Fan Zhang2d0b3442016-12-05 17:02:33 -080045import com.android.settings.core.InstrumentedPreferenceFragment;
Fan Zhangd65184f2016-09-19 17:45:24 -070046import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
Fan Zhang681a4cd2017-11-29 16:57:19 -080047import com.android.settings.search.actionbar.SearchMenuController;
Fan Zhange0b0e9f2017-11-29 14:55:59 -080048import com.android.settings.support.actionbar.HelpMenuController;
49import com.android.settings.support.actionbar.HelpResourceProvider;
Fan Zhang3d516e72018-01-31 14:14:41 -080050import com.android.settings.widget.HighlightablePreferenceGroupAdapter;
Fan Zhang896f1b32017-06-26 14:22:45 -070051import com.android.settings.widget.LoadingViewController;
Juan Lang44828122017-05-10 17:26:02 -070052import com.android.settingslib.CustomDialogPreference;
53import com.android.settingslib.CustomEditTextPreference;
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -080054import com.android.settingslib.core.instrumentation.Instrumentable;
Juan Lang777ed252017-05-09 15:42:36 -070055import com.android.settingslib.widget.FooterPreferenceMixin;
John Spurlockb8e02b82015-04-15 21:15:55 -040056
Jason Monk39b46742015-09-10 15:52:51 -040057import java.util.UUID;
58
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070059/**
Amith Yamasanid7993472010-08-18 13:59:28 -070060 * Base class for Settings fragments, with some helper functions and dialog management.
61 */
Fan Zhang2d0b3442016-12-05 17:02:33 -080062public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceFragment
Fan Zhange0b0e9f2017-11-29 14:55:59 -080063 implements DialogCreatable, HelpResourceProvider {
Anna Galusza0285c802016-01-29 17:32:19 -080064
Jason Monk65bb0972015-12-17 10:39:44 -050065 private static final String TAG = "SettingsPreference";
Amith Yamasanid7993472010-08-18 13:59:28 -070066
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070067 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070068
Fan Zhangd5b48452016-12-13 12:42:50 -080069 protected final FooterPreferenceMixin mFooterPreferenceMixin =
70 new FooterPreferenceMixin(this, getLifecycle());
71
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070072
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000073 private static final int ORDER_FIRST = -1;
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000074
Fan Zhange0b0e9f2017-11-29 14:55:59 -080075 private SettingsDialogFragment mDialogFragment;
Amith Yamasani350938e2013-04-09 10:22:47 -070076 // Cache the content resolver for async callbacks
77 private ContentResolver mContentResolver;
78
Jason Monk39b46742015-09-10 15:52:51 -040079 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070080 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040081 private RecyclerView.AdapterDataObserver mDataSetObserver =
82 new RecyclerView.AdapterDataObserver() {
Tony Mantler0b825f52016-09-27 14:48:16 -070083 @Override
84 public void onChanged() {
85 onDataSetChanged();
86 }
87
88 @Override
89 public void onItemRangeChanged(int positionStart, int itemCount) {
90 onDataSetChanged();
91 }
92
93 @Override
94 public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
95 onDataSetChanged();
96 }
97
98 @Override
99 public void onItemRangeInserted(int positionStart, int itemCount) {
100 onDataSetChanged();
101 }
102
103 @Override
104 public void onItemRangeRemoved(int positionStart, int itemCount) {
105 onDataSetChanged();
106 }
107
108 @Override
109 public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
110 onDataSetChanged();
111 }
112 };
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700113
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700114 private ViewGroup mPinnedHeaderFrameLayout;
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900115 private ViewGroup mButtonBar;
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700116
Jason Monk39b46742015-09-10 15:52:51 -0400117 private LayoutPreference mHeader;
118
Jason Monk39b46742015-09-10 15:52:51 -0400119 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -0500120 private LinearLayoutManager mLayoutManager;
Jason Monk2071eda2016-02-25 13:55:48 -0500121 private ArrayMap<String, Preference> mPreferenceCache;
Jason Monkf38fb382016-03-18 14:23:01 -0400122 private boolean mAnimationAllowed;
Jason Monk39b46742015-09-10 15:52:51 -0400123
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700124 @VisibleForTesting
125 public HighlightablePreferenceGroupAdapter mAdapter;
126 @VisibleForTesting
127 public boolean mPreferenceHighlighted = false;
128
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700129 @Override
130 public void onCreate(Bundle icicle) {
131 super.onCreate(icicle);
Fan Zhang681a4cd2017-11-29 16:57:19 -0800132 SearchMenuController.init(this /* host */);
Fan Zhange0b0e9f2017-11-29 14:55:59 -0800133 HelpMenuController.init(this /* host */);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700134
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700135 if (icicle != null) {
136 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
137 }
Fan Zhang72456a92018-02-20 11:25:56 -0800138 HighlightablePreferenceGroupAdapter.adjustInitialExpandedChildCount(this /* host */);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700139 }
140
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700141 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700142 public View onCreateView(LayoutInflater inflater, ViewGroup container,
143 Bundle savedInstanceState) {
144 final View root = super.onCreateView(inflater, container, savedInstanceState);
Fan Zhange0b0e9f2017-11-29 14:55:59 -0800145 mPinnedHeaderFrameLayout = root.findViewById(R.id.pinned_header);
146 mButtonBar = root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700147 return root;
148 }
149
Jason Monk39b46742015-09-10 15:52:51 -0400150 @Override
Jason Monk91e2f892016-02-23 15:31:09 -0500151 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
152 super.addPreferencesFromResource(preferencesResId);
153 checkAvailablePrefs(getPreferenceScreen());
154 }
155
156 private void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
157 if (preferenceGroup == null) return;
158 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
159 Preference pref = preferenceGroup.getPreference(i);
160 if (pref instanceof SelfAvailablePreference
161 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
162 preferenceGroup.removePreference(pref);
163 } else if (pref instanceof PreferenceGroup) {
164 checkAvailablePrefs((PreferenceGroup) pref);
165 }
166 }
167 }
168
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900169 public ViewGroup getButtonBar() {
170 return mButtonBar;
171 }
172
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700173 public View setPinnedHeaderView(int layoutResId) {
174 final LayoutInflater inflater = getActivity().getLayoutInflater();
175 final View pinnedHeader =
176 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
177 setPinnedHeaderView(pinnedHeader);
178 return pinnedHeader;
179 }
180
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700181 public void setPinnedHeaderView(View pinnedHeader) {
182 mPinnedHeaderFrameLayout.addView(pinnedHeader);
183 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
184 }
185
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700186 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700187 public void onSaveInstanceState(Bundle outState) {
188 super.onSaveInstanceState(outState);
189
Fan Zhang3d516e72018-01-31 14:14:41 -0800190 if (mAdapter != null) {
191 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mAdapter.isHighlightRequested());
192 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700193 }
194
195 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700196 public void onActivityCreated(Bundle savedInstanceState) {
197 super.onActivityCreated(savedInstanceState);
Johan Redestig76218e52016-04-19 08:29:30 +0200198 setHasOptionsMenu(true);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700199 }
200
201 @Override
202 public void onResume() {
203 super.onResume();
Fan Zhang3d516e72018-01-31 14:14:41 -0800204 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700205 }
206
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700207 @Override
208 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700209 registerObserverIfNeeded();
210 }
211
212 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700213 protected void onUnbindPreferences() {
214 unregisterObserverIfNeeded();
215 }
216
Jason Monkb37e2882016-01-11 14:27:20 -0500217 public void setLoading(boolean loading, boolean animate) {
Fan Zhang896f1b32017-06-26 14:22:45 -0700218 View loadingContainer = getView().findViewById(R.id.loading_container);
219 LoadingViewController.handleLoadingContainer(loadingContainer, getListView(),
220 !loading /* done */,
221 animate);
Jason Monkb37e2882016-01-11 14:27:20 -0500222 }
223
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700224 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700225 if (!mIsDataSetObserverRegistered) {
226 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400227 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700228 }
Jason Monk39b46742015-09-10 15:52:51 -0400229 mCurrentRootAdapter = getListView().getAdapter();
230 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700231 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500232 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700233 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700234 }
235
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700236 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700237 if (mIsDataSetObserverRegistered) {
238 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400239 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700240 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700241 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700242 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700243 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700244 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700245
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700246 public void highlightPreferenceIfNeeded() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800247 if (!isAdded()) {
248 return;
249 }
250 if (mAdapter != null) {
251 mAdapter.requestHighlight(getView(), getListView());
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700252 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700253 }
254
Fan Zhang72456a92018-02-20 11:25:56 -0800255 /**
256 * Returns initial expanded child count.
257 * <p/>
258 * Only override this method if the initial expanded child must be determined at run time.
259 */
260 public int getInitialExpandedChildCount() {
261 return 0;
262 }
263
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000264 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400265 highlightPreferenceIfNeeded();
266 updateEmptyView();
267 }
268
Jason Monk39b46742015-09-10 15:52:51 -0400269 public LayoutPreference getHeaderView() {
270 return mHeader;
271 }
272
Jason Monk39b46742015-09-10 15:52:51 -0400273 protected void setHeaderView(int resource) {
274 mHeader = new LayoutPreference(getPrefContext(), resource);
Udam Sainid553abc2016-02-16 17:54:13 -0800275 addPreferenceToTop(mHeader);
276 }
277
278 protected void setHeaderView(View view) {
279 mHeader = new LayoutPreference(getPrefContext(), view);
280 addPreferenceToTop(mHeader);
281 }
282
283 private void addPreferenceToTop(LayoutPreference preference) {
284 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400285 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800286 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400287 }
288 }
289
Jason Monk39b46742015-09-10 15:52:51 -0400290 @Override
291 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400292 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400293 // Without ids generated, the RecyclerView won't animate changes to the preferences.
294 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
295 }
Jason Monk39b46742015-09-10 15:52:51 -0400296 super.setPreferenceScreen(preferenceScreen);
297 if (preferenceScreen != null) {
298 if (mHeader != null) {
299 preferenceScreen.addPreference(mHeader);
300 }
Jason Monk39b46742015-09-10 15:52:51 -0400301 }
302 }
303
jackqdyulei2b2abac2017-05-26 10:47:55 -0700304 @VisibleForTesting
305 void updateEmptyView() {
Jason Monk39b46742015-09-10 15:52:51 -0400306 if (mEmptyView == null) return;
307 if (getPreferenceScreen() != null) {
jackqdyulei2b2abac2017-05-26 10:47:55 -0700308 final View listContainer = getActivity().findViewById(android.R.id.list_container);
Jason Monk39b46742015-09-10 15:52:51 -0400309 boolean show = (getPreferenceScreen().getPreferenceCount()
310 - (mHeader != null ? 1 : 0)
jackqdyulei2b2abac2017-05-26 10:47:55 -0700311 - (mFooterPreferenceMixin.hasFooter() ? 1 : 0)) <= 0
312 || (listContainer != null && listContainer.getVisibility() != View.VISIBLE);
Jason Monk39b46742015-09-10 15:52:51 -0400313 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
314 } else {
315 mEmptyView.setVisibility(View.VISIBLE);
316 }
317 }
318
319 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000320 if (mEmptyView != null) {
321 mEmptyView.setVisibility(View.GONE);
322 }
Jason Monk39b46742015-09-10 15:52:51 -0400323 mEmptyView = v;
324 updateEmptyView();
325 }
326
327 public View getEmptyView() {
328 return mEmptyView;
329 }
330
Jason Monk65bb0972015-12-17 10:39:44 -0500331 @Override
332 public RecyclerView.LayoutManager onCreateLayoutManager() {
333 mLayoutManager = new LinearLayoutManager(getContext());
334 return mLayoutManager;
335 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700336
Jason Monk65bb0972015-12-17 10:39:44 -0500337 @Override
338 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
Fan Zhanga1000462018-02-02 12:15:37 -0800339 final Bundle arguments = getArguments();
Fan Zhang3d516e72018-01-31 14:14:41 -0800340 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen,
Fan Zhanga1000462018-02-02 12:15:37 -0800341 arguments == null
342 ? null : arguments.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY),
Fan Zhang3d516e72018-01-31 14:14:41 -0800343 mPreferenceHighlighted);
Jason Monk65bb0972015-12-17 10:39:44 -0500344 return mAdapter;
345 }
346
Jason Monkf38fb382016-03-18 14:23:01 -0400347 protected void setAnimationAllowed(boolean animationAllowed) {
348 mAnimationAllowed = animationAllowed;
349 }
350
Jason Monk2071eda2016-02-25 13:55:48 -0500351 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
Fan Zhang3d516e72018-01-31 14:14:41 -0800352 mPreferenceCache = new ArrayMap<>();
Jason Monk2071eda2016-02-25 13:55:48 -0500353 final int N = group.getPreferenceCount();
354 for (int i = 0; i < N; i++) {
355 Preference p = group.getPreference(i);
356 if (TextUtils.isEmpty(p.getKey())) {
357 continue;
358 }
359 mPreferenceCache.put(p.getKey(), p);
360 }
361 }
362
363 protected Preference getCachedPreference(String key) {
364 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
365 }
366
367 protected void removeCachedPrefs(PreferenceGroup group) {
368 for (Preference p : mPreferenceCache.values()) {
369 group.removePreference(p);
370 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400371 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500372 }
373
Jason Monka6278442016-04-21 10:12:30 -0400374 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400375 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400376 }
377
Fan Zhange84407f2017-05-24 11:19:52 -0700378 protected boolean removePreference(String key) {
379 return removePreference(getPreferenceScreen(), key);
380 }
381
382 @VisibleForTesting
383 boolean removePreference(PreferenceGroup group, String key) {
384 final int preferenceCount = group.getPreferenceCount();
385 for (int i = 0; i < preferenceCount; i++) {
386 final Preference preference = group.getPreference(i);
387 final String curKey = preference.getKey();
388
389 if (TextUtils.equals(curKey, key)) {
390 return group.removePreference(preference);
391 }
392
393 if (preference instanceof PreferenceGroup) {
394 if (removePreference((PreferenceGroup) preference, key)) {
395 return true;
396 }
397 }
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700398 }
Fan Zhange84407f2017-05-24 11:19:52 -0700399 return false;
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700400 }
401
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700402 /*
403 * The name is intentionally made different from Activity#finish(), so that
404 * users won't misunderstand its meaning.
405 */
406 public final void finishFragment() {
407 getActivity().onBackPressed();
408 }
409
Amith Yamasanid7993472010-08-18 13:59:28 -0700410 // Some helpers for functions used by the settings fragments when they were activities
411
412 /**
413 * Returns the ContentResolver from the owning Activity.
414 */
415 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700416 Context context = getActivity();
417 if (context != null) {
418 mContentResolver = context.getContentResolver();
419 }
420 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700421 }
422
423 /**
424 * Returns the specified system service from the owning Activity.
425 */
426 protected Object getSystemService(final String name) {
427 return getActivity().getSystemService(name);
428 }
429
430 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700431 * Returns the PackageManager from the owning Activity.
432 */
433 protected PackageManager getPackageManager() {
434 return getActivity().getPackageManager();
435 }
436
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800437 @Override
438 public void onDetach() {
439 if (isRemoving()) {
440 if (mDialogFragment != null) {
441 mDialogFragment.dismiss();
442 mDialogFragment = null;
443 }
444 }
445 super.onDetach();
446 }
447
Amith Yamasanid7993472010-08-18 13:59:28 -0700448 // Dialog management
449
450 protected void showDialog(int dialogId) {
451 if (mDialogFragment != null) {
452 Log.e(TAG, "Old dialog fragment not null!");
453 }
454 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800455 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700456 }
457
Fan Zhangd65184f2016-09-19 17:45:24 -0700458 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700459 public Dialog onCreateDialog(int dialogId) {
460 return null;
461 }
462
Fan Zhangd65184f2016-09-19 17:45:24 -0700463 @Override
464 public int getDialogMetricsCategory(int dialogId) {
465 return 0;
466 }
467
Amith Yamasanid7993472010-08-18 13:59:28 -0700468 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800469 // mDialogFragment may not be visible yet in parent fragment's onResume().
470 // To be able to dismiss dialog at that time, don't check
471 // mDialogFragment.isVisible().
472 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400473 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700474 }
475 mDialogFragment = null;
476 }
477
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800478 /**
479 * Sets the OnCancelListener of the dialog shown. This method can only be
480 * called after showDialog(int) and before removeDialog(int). The method
481 * does nothing otherwise.
482 */
483 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
484 if (mDialogFragment != null) {
485 mDialogFragment.mOnCancelListener = listener;
486 }
487 }
488
489 /**
490 * Sets the OnDismissListener of the dialog shown. This method can only be
491 * called after showDialog(int) and before removeDialog(int). The method
492 * does nothing otherwise.
493 */
494 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
495 if (mDialogFragment != null) {
496 mDialogFragment.mOnDismissListener = listener;
497 }
498 }
499
Amith Yamasanic861cf82012-10-02 14:51:46 -0700500 public void onDialogShowing() {
501 // override in subclass to attach a dismiss listener, for instance
502 }
503
Jason Monk39b46742015-09-10 15:52:51 -0400504 @Override
505 public void onDisplayPreferenceDialog(Preference preference) {
506 if (preference.getKey() == null) {
507 // Auto-key preferences that don't have a key, so the dialog can find them.
508 preference.setKey(UUID.randomUUID().toString());
509 }
510 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000511 if (preference instanceof RestrictedListPreference) {
512 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
513 .newInstance(preference.getKey());
514 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400515 f = CustomListPreference.CustomListPreferenceDialogFragment
516 .newInstance(preference.getKey());
517 } else if (preference instanceof CustomDialogPreference) {
518 f = CustomDialogPreference.CustomPreferenceDialogFragment
519 .newInstance(preference.getKey());
520 } else if (preference instanceof CustomEditTextPreference) {
521 f = CustomEditTextPreference.CustomPreferenceDialogFragment
522 .newInstance(preference.getKey());
523 } else {
524 super.onDisplayPreferenceDialog(preference);
525 return;
526 }
527 f.setTargetFragment(this, 0);
528 f.show(getFragmentManager(), "dialog_preference");
529 onDialogShowing();
530 }
531
Fan Zhangd65184f2016-09-19 17:45:24 -0700532 public static class SettingsDialogFragment extends InstrumentedDialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800533 private static final String KEY_DIALOG_ID = "key_dialog_id";
534 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
535
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800536 private Fragment mParentFragment;
537
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800538 private DialogInterface.OnCancelListener mOnCancelListener;
539 private DialogInterface.OnDismissListener mOnDismissListener;
540
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800541 public SettingsDialogFragment() {
542 /* do nothing */
543 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700544
Amith Yamasani43c69782010-12-01 09:04:36 -0800545 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Fan Zhangd65184f2016-09-19 17:45:24 -0700546 super(fragment, dialogId);
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800547 if (!(fragment instanceof Fragment)) {
548 throw new IllegalArgumentException("fragment argument must be an instance of "
549 + Fragment.class.getName());
550 }
551 mParentFragment = (Fragment) fragment;
552 }
553
Fan Zhangd65184f2016-09-19 17:45:24 -0700554
555 @Override
556 public int getMetricsCategory() {
Fan Zhang4fe7c082016-10-03 13:48:55 -0700557 if (mDialogCreatable == null) {
558 return Instrumentable.METRICS_CATEGORY_UNKNOWN;
559 }
Fan Zhangd65184f2016-09-19 17:45:24 -0700560 final int metricsCategory = mDialogCreatable.getDialogMetricsCategory(mDialogId);
561 if (metricsCategory <= 0) {
562 throw new IllegalStateException("Dialog must provide a metrics category");
563 }
564 return metricsCategory;
565 }
566
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800567 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800568 public void onSaveInstanceState(Bundle outState) {
569 super.onSaveInstanceState(outState);
570 if (mParentFragment != null) {
571 outState.putInt(KEY_DIALOG_ID, mDialogId);
572 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
573 }
574 }
575
576 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700577 public void onStart() {
578 super.onStart();
579
580 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
581 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
582 }
583 }
584
585 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800586 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800587 if (savedInstanceState != null) {
588 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800589 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800590 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700591 if (mParentFragment == null) {
592 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
593 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800594 if (!(mParentFragment instanceof DialogCreatable)) {
595 throw new IllegalArgumentException(
596 (mParentFragment != null
597 ? mParentFragment.getClass().getName()
598 : mParentFragmentId)
599 + " must implement "
600 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800601 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800602 // This dialog fragment could be created from non-SettingsPreferenceFragment
603 if (mParentFragment instanceof SettingsPreferenceFragment) {
604 // restore mDialogFragment in mParentFragment
605 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
606 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800607 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800608 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700609 }
610
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800611 @Override
612 public void onCancel(DialogInterface dialog) {
613 super.onCancel(dialog);
614 if (mOnCancelListener != null) {
615 mOnCancelListener.onCancel(dialog);
616 }
617 }
618
619 @Override
620 public void onDismiss(DialogInterface dialog) {
621 super.onDismiss(dialog);
622 if (mOnDismissListener != null) {
623 mOnDismissListener.onDismiss(dialog);
624 }
625 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800626
Amith Yamasanid7993472010-08-18 13:59:28 -0700627 public int getDialogId() {
628 return mDialogId;
629 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800630
631 @Override
632 public void onDetach() {
633 super.onDetach();
634
Amith Yamasani8875ede2011-01-31 12:46:57 -0800635 // This dialog fragment could be created from non-SettingsPreferenceFragment
636 if (mParentFragment instanceof SettingsPreferenceFragment) {
637 // in case the dialog is not explicitly removed by removeDialog()
638 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
639 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
640 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800641 }
642 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700643 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700644
645 protected boolean hasNextButton() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800646 return ((ButtonBarHandler) getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700647 }
648
649 protected Button getNextButton() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800650 return ((ButtonBarHandler) getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700651 }
652
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700653 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700654 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400655 if (activity == null) return;
656 if (getFragmentManager().getBackStackEntryCount() > 0) {
657 getFragmentManager().popBackStack();
658 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800659 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700660 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700661 }
662
Jason Monkb7e43802016-06-06 16:01:58 -0400663 protected Intent getIntent() {
664 if (getActivity() == null) {
665 return null;
666 }
667 return getActivity().getIntent();
668 }
669
670 protected void setResult(int result, Intent intent) {
671 if (getActivity() == null) {
672 return;
673 }
674 getActivity().setResult(result, intent);
675 }
676
677 protected void setResult(int result) {
678 if (getActivity() == null) {
679 return;
680 }
681 getActivity().setResult(result);
682 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700683}