blob: 2a593c277d9b3960cfc7af1d550d4c673c513fa0 [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 }
Doris Ling9ff1d792017-11-13 13:43:17 -0800138 final Bundle arguments = getArguments();
Doris Ling9ff1d792017-11-13 13:43:17 -0800139
140 // Check if we should keep the preferences expanded.
141 if (arguments != null) {
Fan Zhang3d516e72018-01-31 14:14:41 -0800142 final String highlightKey =
143 arguments.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
144 if (!TextUtils.isEmpty(highlightKey)) {
Doris Ling9ff1d792017-11-13 13:43:17 -0800145 final PreferenceScreen screen = getPreferenceScreen();
146 if (screen != null) {
147 screen.setInitialExpandedChildrenCount(Integer.MAX_VALUE);
148 }
149 }
150 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700151 }
152
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700153 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700154 public View onCreateView(LayoutInflater inflater, ViewGroup container,
155 Bundle savedInstanceState) {
156 final View root = super.onCreateView(inflater, container, savedInstanceState);
Fan Zhange0b0e9f2017-11-29 14:55:59 -0800157 mPinnedHeaderFrameLayout = root.findViewById(R.id.pinned_header);
158 mButtonBar = root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700159 return root;
160 }
161
Jason Monk39b46742015-09-10 15:52:51 -0400162 @Override
Jason Monk91e2f892016-02-23 15:31:09 -0500163 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
164 super.addPreferencesFromResource(preferencesResId);
165 checkAvailablePrefs(getPreferenceScreen());
166 }
167
168 private void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
169 if (preferenceGroup == null) return;
170 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
171 Preference pref = preferenceGroup.getPreference(i);
172 if (pref instanceof SelfAvailablePreference
173 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
174 preferenceGroup.removePreference(pref);
175 } else if (pref instanceof PreferenceGroup) {
176 checkAvailablePrefs((PreferenceGroup) pref);
177 }
178 }
179 }
180
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900181 public ViewGroup getButtonBar() {
182 return mButtonBar;
183 }
184
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700185 public View setPinnedHeaderView(int layoutResId) {
186 final LayoutInflater inflater = getActivity().getLayoutInflater();
187 final View pinnedHeader =
188 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
189 setPinnedHeaderView(pinnedHeader);
190 return pinnedHeader;
191 }
192
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700193 public void setPinnedHeaderView(View pinnedHeader) {
194 mPinnedHeaderFrameLayout.addView(pinnedHeader);
195 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
196 }
197
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700198 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700199 public void onSaveInstanceState(Bundle outState) {
200 super.onSaveInstanceState(outState);
201
Fan Zhang3d516e72018-01-31 14:14:41 -0800202 if (mAdapter != null) {
203 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mAdapter.isHighlightRequested());
204 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700205 }
206
207 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700208 public void onActivityCreated(Bundle savedInstanceState) {
209 super.onActivityCreated(savedInstanceState);
Johan Redestig76218e52016-04-19 08:29:30 +0200210 setHasOptionsMenu(true);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700211 }
212
213 @Override
214 public void onResume() {
215 super.onResume();
Fan Zhang3d516e72018-01-31 14:14:41 -0800216 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700217 }
218
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700219 @Override
220 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700221 registerObserverIfNeeded();
222 }
223
224 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700225 protected void onUnbindPreferences() {
226 unregisterObserverIfNeeded();
227 }
228
Jason Monkb37e2882016-01-11 14:27:20 -0500229 public void setLoading(boolean loading, boolean animate) {
Fan Zhang896f1b32017-06-26 14:22:45 -0700230 View loadingContainer = getView().findViewById(R.id.loading_container);
231 LoadingViewController.handleLoadingContainer(loadingContainer, getListView(),
232 !loading /* done */,
233 animate);
Jason Monkb37e2882016-01-11 14:27:20 -0500234 }
235
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700236 public void registerObserverIfNeeded() {
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 Meglio7c435f62014-07-29 16:02:22 -0700240 }
Jason Monk39b46742015-09-10 15:52:51 -0400241 mCurrentRootAdapter = getListView().getAdapter();
242 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700243 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500244 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700245 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700246 }
247
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700248 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700249 if (mIsDataSetObserverRegistered) {
250 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400251 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700252 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700253 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700254 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700255 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700256 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700257
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700258 public void highlightPreferenceIfNeeded() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800259 if (!isAdded()) {
260 return;
261 }
262 if (mAdapter != null) {
263 mAdapter.requestHighlight(getView(), getListView());
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700264 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700265 }
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 Zhang3d516e72018-01-31 14:14:41 -0800342 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen,
343 getArguments().getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY),
344 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
Fan Zhange84407f2017-05-24 11:19:52 -0700379 protected boolean removePreference(String key) {
380 return removePreference(getPreferenceScreen(), key);
381 }
382
383 @VisibleForTesting
384 boolean removePreference(PreferenceGroup group, String key) {
385 final int preferenceCount = group.getPreferenceCount();
386 for (int i = 0; i < preferenceCount; i++) {
387 final Preference preference = group.getPreference(i);
388 final String curKey = preference.getKey();
389
390 if (TextUtils.equals(curKey, key)) {
391 return group.removePreference(preference);
392 }
393
394 if (preference instanceof PreferenceGroup) {
395 if (removePreference((PreferenceGroup) preference, key)) {
396 return true;
397 }
398 }
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700399 }
Fan Zhange84407f2017-05-24 11:19:52 -0700400 return false;
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700401 }
402
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700403 /*
404 * The name is intentionally made different from Activity#finish(), so that
405 * users won't misunderstand its meaning.
406 */
407 public final void finishFragment() {
408 getActivity().onBackPressed();
409 }
410
Amith Yamasanid7993472010-08-18 13:59:28 -0700411 // Some helpers for functions used by the settings fragments when they were activities
412
413 /**
414 * Returns the ContentResolver from the owning Activity.
415 */
416 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700417 Context context = getActivity();
418 if (context != null) {
419 mContentResolver = context.getContentResolver();
420 }
421 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700422 }
423
424 /**
425 * Returns the specified system service from the owning Activity.
426 */
427 protected Object getSystemService(final String name) {
428 return getActivity().getSystemService(name);
429 }
430
431 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700432 * Returns the PackageManager from the owning Activity.
433 */
434 protected PackageManager getPackageManager() {
435 return getActivity().getPackageManager();
436 }
437
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800438 @Override
439 public void onDetach() {
440 if (isRemoving()) {
441 if (mDialogFragment != null) {
442 mDialogFragment.dismiss();
443 mDialogFragment = null;
444 }
445 }
446 super.onDetach();
447 }
448
Amith Yamasanid7993472010-08-18 13:59:28 -0700449 // Dialog management
450
451 protected void showDialog(int dialogId) {
452 if (mDialogFragment != null) {
453 Log.e(TAG, "Old dialog fragment not null!");
454 }
455 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800456 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700457 }
458
Fan Zhangd65184f2016-09-19 17:45:24 -0700459 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700460 public Dialog onCreateDialog(int dialogId) {
461 return null;
462 }
463
Fan Zhangd65184f2016-09-19 17:45:24 -0700464 @Override
465 public int getDialogMetricsCategory(int dialogId) {
466 return 0;
467 }
468
Amith Yamasanid7993472010-08-18 13:59:28 -0700469 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800470 // mDialogFragment may not be visible yet in parent fragment's onResume().
471 // To be able to dismiss dialog at that time, don't check
472 // mDialogFragment.isVisible().
473 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400474 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700475 }
476 mDialogFragment = null;
477 }
478
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800479 /**
480 * Sets the OnCancelListener of the dialog shown. This method can only be
481 * called after showDialog(int) and before removeDialog(int). The method
482 * does nothing otherwise.
483 */
484 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
485 if (mDialogFragment != null) {
486 mDialogFragment.mOnCancelListener = listener;
487 }
488 }
489
490 /**
491 * Sets the OnDismissListener of the dialog shown. This method can only be
492 * called after showDialog(int) and before removeDialog(int). The method
493 * does nothing otherwise.
494 */
495 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
496 if (mDialogFragment != null) {
497 mDialogFragment.mOnDismissListener = listener;
498 }
499 }
500
Amith Yamasanic861cf82012-10-02 14:51:46 -0700501 public void onDialogShowing() {
502 // override in subclass to attach a dismiss listener, for instance
503 }
504
Jason Monk39b46742015-09-10 15:52:51 -0400505 @Override
506 public void onDisplayPreferenceDialog(Preference preference) {
507 if (preference.getKey() == null) {
508 // Auto-key preferences that don't have a key, so the dialog can find them.
509 preference.setKey(UUID.randomUUID().toString());
510 }
511 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000512 if (preference instanceof RestrictedListPreference) {
513 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
514 .newInstance(preference.getKey());
515 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400516 f = CustomListPreference.CustomListPreferenceDialogFragment
517 .newInstance(preference.getKey());
518 } else if (preference instanceof CustomDialogPreference) {
519 f = CustomDialogPreference.CustomPreferenceDialogFragment
520 .newInstance(preference.getKey());
521 } else if (preference instanceof CustomEditTextPreference) {
522 f = CustomEditTextPreference.CustomPreferenceDialogFragment
523 .newInstance(preference.getKey());
524 } else {
525 super.onDisplayPreferenceDialog(preference);
526 return;
527 }
528 f.setTargetFragment(this, 0);
529 f.show(getFragmentManager(), "dialog_preference");
530 onDialogShowing();
531 }
532
Fan Zhangd65184f2016-09-19 17:45:24 -0700533 public static class SettingsDialogFragment extends InstrumentedDialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800534 private static final String KEY_DIALOG_ID = "key_dialog_id";
535 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
536
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800537 private Fragment mParentFragment;
538
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800539 private DialogInterface.OnCancelListener mOnCancelListener;
540 private DialogInterface.OnDismissListener mOnDismissListener;
541
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800542 public SettingsDialogFragment() {
543 /* do nothing */
544 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700545
Amith Yamasani43c69782010-12-01 09:04:36 -0800546 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Fan Zhangd65184f2016-09-19 17:45:24 -0700547 super(fragment, dialogId);
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800548 if (!(fragment instanceof Fragment)) {
549 throw new IllegalArgumentException("fragment argument must be an instance of "
550 + Fragment.class.getName());
551 }
552 mParentFragment = (Fragment) fragment;
553 }
554
Fan Zhangd65184f2016-09-19 17:45:24 -0700555
556 @Override
557 public int getMetricsCategory() {
Fan Zhang4fe7c082016-10-03 13:48:55 -0700558 if (mDialogCreatable == null) {
559 return Instrumentable.METRICS_CATEGORY_UNKNOWN;
560 }
Fan Zhangd65184f2016-09-19 17:45:24 -0700561 final int metricsCategory = mDialogCreatable.getDialogMetricsCategory(mDialogId);
562 if (metricsCategory <= 0) {
563 throw new IllegalStateException("Dialog must provide a metrics category");
564 }
565 return metricsCategory;
566 }
567
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800568 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800569 public void onSaveInstanceState(Bundle outState) {
570 super.onSaveInstanceState(outState);
571 if (mParentFragment != null) {
572 outState.putInt(KEY_DIALOG_ID, mDialogId);
573 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
574 }
575 }
576
577 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700578 public void onStart() {
579 super.onStart();
580
581 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
582 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
583 }
584 }
585
586 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800587 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800588 if (savedInstanceState != null) {
589 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800590 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800591 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700592 if (mParentFragment == null) {
593 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
594 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800595 if (!(mParentFragment instanceof DialogCreatable)) {
596 throw new IllegalArgumentException(
597 (mParentFragment != null
598 ? mParentFragment.getClass().getName()
599 : mParentFragmentId)
600 + " must implement "
601 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800602 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800603 // This dialog fragment could be created from non-SettingsPreferenceFragment
604 if (mParentFragment instanceof SettingsPreferenceFragment) {
605 // restore mDialogFragment in mParentFragment
606 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
607 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800608 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800609 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700610 }
611
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800612 @Override
613 public void onCancel(DialogInterface dialog) {
614 super.onCancel(dialog);
615 if (mOnCancelListener != null) {
616 mOnCancelListener.onCancel(dialog);
617 }
618 }
619
620 @Override
621 public void onDismiss(DialogInterface dialog) {
622 super.onDismiss(dialog);
623 if (mOnDismissListener != null) {
624 mOnDismissListener.onDismiss(dialog);
625 }
626 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800627
Amith Yamasanid7993472010-08-18 13:59:28 -0700628 public int getDialogId() {
629 return mDialogId;
630 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800631
632 @Override
633 public void onDetach() {
634 super.onDetach();
635
Amith Yamasani8875ede2011-01-31 12:46:57 -0800636 // This dialog fragment could be created from non-SettingsPreferenceFragment
637 if (mParentFragment instanceof SettingsPreferenceFragment) {
638 // in case the dialog is not explicitly removed by removeDialog()
639 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
640 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
641 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800642 }
643 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700644 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700645
646 protected boolean hasNextButton() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800647 return ((ButtonBarHandler) getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700648 }
649
650 protected Button getNextButton() {
Fan Zhang3d516e72018-01-31 14:14:41 -0800651 return ((ButtonBarHandler) getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700652 }
653
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700654 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700655 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400656 if (activity == null) return;
657 if (getFragmentManager().getBackStackEntryCount() > 0) {
658 getFragmentManager().popBackStack();
659 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800660 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700661 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700662 }
663
Jason Monkb7e43802016-06-06 16:01:58 -0400664 protected Intent getIntent() {
665 if (getActivity() == null) {
666 return null;
667 }
668 return getActivity().getIntent();
669 }
670
671 protected void setResult(int result, Intent intent) {
672 if (getActivity() == null) {
673 return;
674 }
675 getActivity().setResult(result, intent);
676 }
677
678 protected void setResult(int result) {
679 if (getActivity() == null) {
680 return;
681 }
682 getActivity().setResult(result);
683 }
684
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700685 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
686 int requestCode, Bundle extras) {
687 final Activity activity = getActivity();
688 if (activity instanceof SettingsActivity) {
689 SettingsActivity sa = (SettingsActivity) activity;
Fan Zhangc6ca3142017-02-14 15:02:35 -0800690 sa.startPreferencePanel(
691 caller, fragmentClass, extras, titleRes, null, caller, requestCode);
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700692 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700693 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700694 Log.w(TAG,
695 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
Fan Zhang3d516e72018-01-31 14:14:41 -0800696 + "launch the given Fragment (name: " + fragmentClass
697 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700698 return false;
699 }
700 }
Jason Monk65bb0972015-12-17 10:39:44 -0500701
Amith Yamasanid7993472010-08-18 13:59:28 -0700702}