blob: 5d3bf0027605e467609f6bd101e577ce074b02e9 [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.PreferenceGroupAdapter;
34import android.support.v7.preference.PreferenceScreen;
Jason Monk65bb0972015-12-17 10:39:44 -050035import android.support.v7.preference.PreferenceViewHolder;
36import android.support.v7.widget.LinearLayoutManager;
Jason Monk39b46742015-09-10 15:52:51 -040037import android.support.v7.widget.RecyclerView;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070038import android.text.TextUtils;
Jason Monk2071eda2016-02-25 13:55:48 -050039import android.util.ArrayMap;
Amith Yamasanid7993472010-08-18 13:59:28 -070040import android.util.Log;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070041import android.view.LayoutInflater;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070042import android.view.Menu;
43import android.view.MenuInflater;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070044import android.view.View;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070045import android.view.ViewGroup;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070046import android.widget.Button;
Jason Monkb7e43802016-06-06 16:01:58 -040047
Jason Monk39b46742015-09-10 15:52:51 -040048import com.android.settings.applications.LayoutPreference;
Fan Zhang2d0b3442016-12-05 17:02:33 -080049import com.android.settings.core.InstrumentedPreferenceFragment;
Fan Zhang4fe7c082016-10-03 13:48:55 -070050import com.android.settings.core.instrumentation.Instrumentable;
Fan Zhangd65184f2016-09-19 17:45:24 -070051import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
Fan Zhang896f1b32017-06-26 14:22:45 -070052import com.android.settings.widget.LoadingViewController;
Juan Lang44828122017-05-10 17:26:02 -070053import com.android.settingslib.CustomDialogPreference;
54import com.android.settingslib.CustomEditTextPreference;
Suprabh Shuklab84720c2016-04-05 14:37:20 -070055import com.android.settingslib.HelpUtils;
Juan Lang777ed252017-05-09 15:42:36 -070056import com.android.settingslib.widget.FooterPreferenceMixin;
John Spurlockb8e02b82015-04-15 21:15:55 -040057
Jason Monk39b46742015-09-10 15:52:51 -040058import java.util.UUID;
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
Chris Wren8a963ba2015-03-20 10:29:14 -040064 implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070065
Anna Galusza0285c802016-01-29 17:32:19 -080066 /**
67 * The Help Uri Resource key. This can be passed as an extra argument when creating the
68 * Fragment.
69 **/
70 public static final String HELP_URI_RESOURCE_KEY = "help_uri_resource";
71
Jason Monk65bb0972015-12-17 10:39:44 -050072 private static final String TAG = "SettingsPreference";
Amith Yamasanid7993472010-08-18 13:59:28 -070073
Matthew Fritze33f3e3f2017-06-06 17:14:33 -070074 @VisibleForTesting
75 static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070076
77 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070078
Fan Zhangd5b48452016-12-13 12:42:50 -080079 protected final FooterPreferenceMixin mFooterPreferenceMixin =
80 new FooterPreferenceMixin(this, getLifecycle());
81
Amith Yamasanid7993472010-08-18 13:59:28 -070082 private SettingsDialogFragment mDialogFragment;
83
Jason Monk23acc2b2015-04-14 15:06:39 -040084 private String mHelpUri;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070085
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000086 private static final int ORDER_FIRST = -1;
87 private static final int ORDER_LAST = Integer.MAX_VALUE -1;
88
Amith Yamasani350938e2013-04-09 10:22:47 -070089 // Cache the content resolver for async callbacks
90 private ContentResolver mContentResolver;
91
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070092 private String mPreferenceKey;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070093
Jason Monk39b46742015-09-10 15:52:51 -040094 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070095 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040096 private RecyclerView.AdapterDataObserver mDataSetObserver =
97 new RecyclerView.AdapterDataObserver() {
Tony Mantler0b825f52016-09-27 14:48:16 -070098 @Override
99 public void onChanged() {
100 onDataSetChanged();
101 }
102
103 @Override
104 public void onItemRangeChanged(int positionStart, int itemCount) {
105 onDataSetChanged();
106 }
107
108 @Override
109 public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
110 onDataSetChanged();
111 }
112
113 @Override
114 public void onItemRangeInserted(int positionStart, int itemCount) {
115 onDataSetChanged();
116 }
117
118 @Override
119 public void onItemRangeRemoved(int positionStart, int itemCount) {
120 onDataSetChanged();
121 }
122
123 @Override
124 public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
125 onDataSetChanged();
126 }
127 };
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700128
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700129 private ViewGroup mPinnedHeaderFrameLayout;
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900130 private ViewGroup mButtonBar;
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700131
Jason Monk39b46742015-09-10 15:52:51 -0400132 private LayoutPreference mHeader;
133
Jason Monk39b46742015-09-10 15:52:51 -0400134 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -0500135 private LinearLayoutManager mLayoutManager;
Jason Monk2071eda2016-02-25 13:55:48 -0500136 private ArrayMap<String, Preference> mPreferenceCache;
Jason Monkf38fb382016-03-18 14:23:01 -0400137 private boolean mAnimationAllowed;
Jason Monk39b46742015-09-10 15:52:51 -0400138
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700139 @VisibleForTesting
140 public HighlightablePreferenceGroupAdapter mAdapter;
141 @VisibleForTesting
142 public boolean mPreferenceHighlighted = false;
143
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700144 @Override
145 public void onCreate(Bundle icicle) {
146 super.onCreate(icicle);
147
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700148 if (icicle != null) {
149 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
150 }
151
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700152 // Prepare help url and enable menu if necessary
Doris Ling9ff1d792017-11-13 13:43:17 -0800153 final Bundle arguments = getArguments();
154 final int helpResource;
Anna Galusza0285c802016-01-29 17:32:19 -0800155 if (arguments != null && arguments.containsKey(HELP_URI_RESOURCE_KEY)) {
156 helpResource = arguments.getInt(HELP_URI_RESOURCE_KEY);
157 } else {
158 helpResource = getHelpResource();
159 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700160 if (helpResource != 0) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400161 mHelpUri = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700162 }
Doris Ling9ff1d792017-11-13 13:43:17 -0800163
164 // Check if we should keep the preferences expanded.
165 if (arguments != null) {
166 mPreferenceKey = arguments.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
167 if (!TextUtils.isEmpty(mPreferenceKey)) {
168 final PreferenceScreen screen = getPreferenceScreen();
169 if (screen != null) {
170 screen.setInitialExpandedChildrenCount(Integer.MAX_VALUE);
171 }
172 }
173 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700174 }
175
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700176 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700177 public View onCreateView(LayoutInflater inflater, ViewGroup container,
178 Bundle savedInstanceState) {
179 final View root = super.onCreateView(inflater, container, savedInstanceState);
180 mPinnedHeaderFrameLayout = (ViewGroup) root.findViewById(R.id.pinned_header);
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900181 mButtonBar = (ViewGroup) root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700182 return root;
183 }
184
Jason Monk39b46742015-09-10 15:52:51 -0400185 @Override
Jason Monk91e2f892016-02-23 15:31:09 -0500186 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
187 super.addPreferencesFromResource(preferencesResId);
188 checkAvailablePrefs(getPreferenceScreen());
189 }
190
191 private void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
192 if (preferenceGroup == null) return;
193 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
194 Preference pref = preferenceGroup.getPreference(i);
195 if (pref instanceof SelfAvailablePreference
196 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
197 preferenceGroup.removePreference(pref);
198 } else if (pref instanceof PreferenceGroup) {
199 checkAvailablePrefs((PreferenceGroup) pref);
200 }
201 }
202 }
203
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900204 public ViewGroup getButtonBar() {
205 return mButtonBar;
206 }
207
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700208 public View setPinnedHeaderView(int layoutResId) {
209 final LayoutInflater inflater = getActivity().getLayoutInflater();
210 final View pinnedHeader =
211 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
212 setPinnedHeaderView(pinnedHeader);
213 return pinnedHeader;
214 }
215
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700216 public void setPinnedHeaderView(View pinnedHeader) {
217 mPinnedHeaderFrameLayout.addView(pinnedHeader);
218 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
219 }
220
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700221 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700222 public void onSaveInstanceState(Bundle outState) {
223 super.onSaveInstanceState(outState);
224
225 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
226 }
227
228 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700229 public void onActivityCreated(Bundle savedInstanceState) {
230 super.onActivityCreated(savedInstanceState);
Johan Redestig76218e52016-04-19 08:29:30 +0200231 setHasOptionsMenu(true);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700232 }
233
234 @Override
235 public void onResume() {
236 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700237
Doris Ling9ff1d792017-11-13 13:43:17 -0800238 if (mPreferenceKey != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700239 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700240 }
241 }
242
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700243 @Override
244 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700245 registerObserverIfNeeded();
246 }
247
248 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700249 protected void onUnbindPreferences() {
250 unregisterObserverIfNeeded();
251 }
252
Jason Monkb37e2882016-01-11 14:27:20 -0500253 public void setLoading(boolean loading, boolean animate) {
Fan Zhang896f1b32017-06-26 14:22:45 -0700254 View loadingContainer = getView().findViewById(R.id.loading_container);
255 LoadingViewController.handleLoadingContainer(loadingContainer, getListView(),
256 !loading /* done */,
257 animate);
Jason Monkb37e2882016-01-11 14:27:20 -0500258 }
259
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700260 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700261 if (!mIsDataSetObserverRegistered) {
262 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400263 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700264 }
Jason Monk39b46742015-09-10 15:52:51 -0400265 mCurrentRootAdapter = getListView().getAdapter();
266 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700267 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500268 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700269 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700270 }
271
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700272 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700273 if (mIsDataSetObserverRegistered) {
274 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400275 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700276 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700277 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700278 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700279 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700280 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700281
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700282 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700283 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700284 getView().postDelayed(new Runnable() {
285 @Override
286 public void run() {
287 highlightPreference(mPreferenceKey);
288 }
289 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700290 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700291 }
292
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000293 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400294 highlightPreferenceIfNeeded();
295 updateEmptyView();
296 }
297
Jason Monk39b46742015-09-10 15:52:51 -0400298 public LayoutPreference getHeaderView() {
299 return mHeader;
300 }
301
Jason Monk39b46742015-09-10 15:52:51 -0400302 protected void setHeaderView(int resource) {
303 mHeader = new LayoutPreference(getPrefContext(), resource);
Udam Sainid553abc2016-02-16 17:54:13 -0800304 addPreferenceToTop(mHeader);
305 }
306
307 protected void setHeaderView(View view) {
308 mHeader = new LayoutPreference(getPrefContext(), view);
309 addPreferenceToTop(mHeader);
310 }
311
312 private void addPreferenceToTop(LayoutPreference preference) {
313 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400314 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800315 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400316 }
317 }
318
Jason Monk39b46742015-09-10 15:52:51 -0400319 @Override
320 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400321 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400322 // Without ids generated, the RecyclerView won't animate changes to the preferences.
323 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
324 }
Jason Monk39b46742015-09-10 15:52:51 -0400325 super.setPreferenceScreen(preferenceScreen);
326 if (preferenceScreen != null) {
327 if (mHeader != null) {
328 preferenceScreen.addPreference(mHeader);
329 }
Jason Monk39b46742015-09-10 15:52:51 -0400330 }
331 }
332
jackqdyulei2b2abac2017-05-26 10:47:55 -0700333 @VisibleForTesting
334 void updateEmptyView() {
Jason Monk39b46742015-09-10 15:52:51 -0400335 if (mEmptyView == null) return;
336 if (getPreferenceScreen() != null) {
jackqdyulei2b2abac2017-05-26 10:47:55 -0700337 final View listContainer = getActivity().findViewById(android.R.id.list_container);
Jason Monk39b46742015-09-10 15:52:51 -0400338 boolean show = (getPreferenceScreen().getPreferenceCount()
339 - (mHeader != null ? 1 : 0)
jackqdyulei2b2abac2017-05-26 10:47:55 -0700340 - (mFooterPreferenceMixin.hasFooter() ? 1 : 0)) <= 0
341 || (listContainer != null && listContainer.getVisibility() != View.VISIBLE);
Jason Monk39b46742015-09-10 15:52:51 -0400342 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
343 } else {
344 mEmptyView.setVisibility(View.VISIBLE);
345 }
346 }
347
348 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000349 if (mEmptyView != null) {
350 mEmptyView.setVisibility(View.GONE);
351 }
Jason Monk39b46742015-09-10 15:52:51 -0400352 mEmptyView = v;
353 updateEmptyView();
354 }
355
356 public View getEmptyView() {
357 return mEmptyView;
358 }
359
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700360 /**
361 * Return a valid ListView position or -1 if none is found
362 */
363 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400364 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700365 return -1;
366 }
367
Jason Monk39b46742015-09-10 15:52:51 -0400368 RecyclerView listView = getListView();
369 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700370
371 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400372 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700373 }
374
375 return -1;
376 }
377
Jason Monk65bb0972015-12-17 10:39:44 -0500378 @Override
379 public RecyclerView.LayoutManager onCreateLayoutManager() {
380 mLayoutManager = new LinearLayoutManager(getContext());
381 return mLayoutManager;
382 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700383
Jason Monk65bb0972015-12-17 10:39:44 -0500384 @Override
385 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
386 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
387 return mAdapter;
388 }
389
Jason Monkf38fb382016-03-18 14:23:01 -0400390 protected void setAnimationAllowed(boolean animationAllowed) {
391 mAnimationAllowed = animationAllowed;
392 }
393
Jason Monk2071eda2016-02-25 13:55:48 -0500394 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
395 mPreferenceCache = new ArrayMap<String, Preference>();
396 final int N = group.getPreferenceCount();
397 for (int i = 0; i < N; i++) {
398 Preference p = group.getPreference(i);
399 if (TextUtils.isEmpty(p.getKey())) {
400 continue;
401 }
402 mPreferenceCache.put(p.getKey(), p);
403 }
404 }
405
406 protected Preference getCachedPreference(String key) {
407 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
408 }
409
410 protected void removeCachedPrefs(PreferenceGroup group) {
411 for (Preference p : mPreferenceCache.values()) {
412 group.removePreference(p);
413 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400414 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500415 }
416
Jason Monka6278442016-04-21 10:12:30 -0400417 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400418 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400419 }
420
Jason Monk65bb0972015-12-17 10:39:44 -0500421 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700422 final int position = canUseListViewForHighLighting(key);
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700423 if (position < 0) {
424 return;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700425 }
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700426
427 mPreferenceHighlighted = true;
428 mLayoutManager.scrollToPosition(position);
429 mAdapter.highlight(position);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700430 }
431
Jason Monk39b46742015-09-10 15:52:51 -0400432 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
433 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700434 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400435 final Preference preference = adapter.getItem(n);
436 final String preferenceKey = preference.getKey();
437 if (preferenceKey != null && preferenceKey.equals(key)) {
438 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700439 }
440 }
441 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700442 }
443
Fan Zhange84407f2017-05-24 11:19:52 -0700444 protected boolean removePreference(String key) {
445 return removePreference(getPreferenceScreen(), key);
446 }
447
448 @VisibleForTesting
449 boolean removePreference(PreferenceGroup group, String key) {
450 final int preferenceCount = group.getPreferenceCount();
451 for (int i = 0; i < preferenceCount; i++) {
452 final Preference preference = group.getPreference(i);
453 final String curKey = preference.getKey();
454
455 if (TextUtils.equals(curKey, key)) {
456 return group.removePreference(preference);
457 }
458
459 if (preference instanceof PreferenceGroup) {
460 if (removePreference((PreferenceGroup) preference, key)) {
461 return true;
462 }
463 }
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700464 }
Fan Zhange84407f2017-05-24 11:19:52 -0700465 return false;
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700466 }
467
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700468 /**
469 * Override this if you want to show a help item in the menu, by returning the resource id.
470 * @return the resource id for the help url
471 */
472 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400473 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700474 }
475
476 @Override
477 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Daniel Nishie6740f72017-04-14 14:12:35 -0700478 super.onCreateOptionsMenu(menu, inflater);
Jason Monk23acc2b2015-04-14 15:06:39 -0400479 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400480 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700481 }
482 }
483
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700484 /*
485 * The name is intentionally made different from Activity#finish(), so that
486 * users won't misunderstand its meaning.
487 */
488 public final void finishFragment() {
489 getActivity().onBackPressed();
490 }
491
Amith Yamasanid7993472010-08-18 13:59:28 -0700492 // Some helpers for functions used by the settings fragments when they were activities
493
494 /**
495 * Returns the ContentResolver from the owning Activity.
496 */
497 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700498 Context context = getActivity();
499 if (context != null) {
500 mContentResolver = context.getContentResolver();
501 }
502 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700503 }
504
505 /**
506 * Returns the specified system service from the owning Activity.
507 */
508 protected Object getSystemService(final String name) {
509 return getActivity().getSystemService(name);
510 }
511
512 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700513 * Returns the PackageManager from the owning Activity.
514 */
515 protected PackageManager getPackageManager() {
516 return getActivity().getPackageManager();
517 }
518
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800519 @Override
520 public void onDetach() {
521 if (isRemoving()) {
522 if (mDialogFragment != null) {
523 mDialogFragment.dismiss();
524 mDialogFragment = null;
525 }
526 }
527 super.onDetach();
528 }
529
Amith Yamasanid7993472010-08-18 13:59:28 -0700530 // Dialog management
531
532 protected void showDialog(int dialogId) {
533 if (mDialogFragment != null) {
534 Log.e(TAG, "Old dialog fragment not null!");
535 }
536 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800537 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700538 }
539
Fan Zhangd65184f2016-09-19 17:45:24 -0700540 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700541 public Dialog onCreateDialog(int dialogId) {
542 return null;
543 }
544
Fan Zhangd65184f2016-09-19 17:45:24 -0700545 @Override
546 public int getDialogMetricsCategory(int dialogId) {
547 return 0;
548 }
549
Amith Yamasanid7993472010-08-18 13:59:28 -0700550 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800551 // mDialogFragment may not be visible yet in parent fragment's onResume().
552 // To be able to dismiss dialog at that time, don't check
553 // mDialogFragment.isVisible().
554 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400555 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700556 }
557 mDialogFragment = null;
558 }
559
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800560 /**
561 * Sets the OnCancelListener of the dialog shown. This method can only be
562 * called after showDialog(int) and before removeDialog(int). The method
563 * does nothing otherwise.
564 */
565 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
566 if (mDialogFragment != null) {
567 mDialogFragment.mOnCancelListener = listener;
568 }
569 }
570
571 /**
572 * Sets the OnDismissListener of the dialog shown. This method can only be
573 * called after showDialog(int) and before removeDialog(int). The method
574 * does nothing otherwise.
575 */
576 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
577 if (mDialogFragment != null) {
578 mDialogFragment.mOnDismissListener = listener;
579 }
580 }
581
Amith Yamasanic861cf82012-10-02 14:51:46 -0700582 public void onDialogShowing() {
583 // override in subclass to attach a dismiss listener, for instance
584 }
585
Jason Monk39b46742015-09-10 15:52:51 -0400586 @Override
587 public void onDisplayPreferenceDialog(Preference preference) {
588 if (preference.getKey() == null) {
589 // Auto-key preferences that don't have a key, so the dialog can find them.
590 preference.setKey(UUID.randomUUID().toString());
591 }
592 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000593 if (preference instanceof RestrictedListPreference) {
594 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
595 .newInstance(preference.getKey());
596 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400597 f = CustomListPreference.CustomListPreferenceDialogFragment
598 .newInstance(preference.getKey());
599 } else if (preference instanceof CustomDialogPreference) {
600 f = CustomDialogPreference.CustomPreferenceDialogFragment
601 .newInstance(preference.getKey());
602 } else if (preference instanceof CustomEditTextPreference) {
603 f = CustomEditTextPreference.CustomPreferenceDialogFragment
604 .newInstance(preference.getKey());
605 } else {
606 super.onDisplayPreferenceDialog(preference);
607 return;
608 }
609 f.setTargetFragment(this, 0);
610 f.show(getFragmentManager(), "dialog_preference");
611 onDialogShowing();
612 }
613
Fan Zhangd65184f2016-09-19 17:45:24 -0700614 public static class SettingsDialogFragment extends InstrumentedDialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800615 private static final String KEY_DIALOG_ID = "key_dialog_id";
616 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
617
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800618 private Fragment mParentFragment;
619
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800620 private DialogInterface.OnCancelListener mOnCancelListener;
621 private DialogInterface.OnDismissListener mOnDismissListener;
622
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800623 public SettingsDialogFragment() {
624 /* do nothing */
625 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700626
Amith Yamasani43c69782010-12-01 09:04:36 -0800627 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Fan Zhangd65184f2016-09-19 17:45:24 -0700628 super(fragment, dialogId);
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800629 if (!(fragment instanceof Fragment)) {
630 throw new IllegalArgumentException("fragment argument must be an instance of "
631 + Fragment.class.getName());
632 }
633 mParentFragment = (Fragment) fragment;
634 }
635
Fan Zhangd65184f2016-09-19 17:45:24 -0700636
637 @Override
638 public int getMetricsCategory() {
Fan Zhang4fe7c082016-10-03 13:48:55 -0700639 if (mDialogCreatable == null) {
640 return Instrumentable.METRICS_CATEGORY_UNKNOWN;
641 }
Fan Zhangd65184f2016-09-19 17:45:24 -0700642 final int metricsCategory = mDialogCreatable.getDialogMetricsCategory(mDialogId);
643 if (metricsCategory <= 0) {
644 throw new IllegalStateException("Dialog must provide a metrics category");
645 }
646 return metricsCategory;
647 }
648
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800649 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800650 public void onSaveInstanceState(Bundle outState) {
651 super.onSaveInstanceState(outState);
652 if (mParentFragment != null) {
653 outState.putInt(KEY_DIALOG_ID, mDialogId);
654 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
655 }
656 }
657
658 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700659 public void onStart() {
660 super.onStart();
661
662 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
663 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
664 }
665 }
666
667 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800668 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800669 if (savedInstanceState != null) {
670 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800671 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800672 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700673 if (mParentFragment == null) {
674 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
675 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800676 if (!(mParentFragment instanceof DialogCreatable)) {
677 throw new IllegalArgumentException(
678 (mParentFragment != null
679 ? mParentFragment.getClass().getName()
680 : mParentFragmentId)
681 + " must implement "
682 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800683 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800684 // This dialog fragment could be created from non-SettingsPreferenceFragment
685 if (mParentFragment instanceof SettingsPreferenceFragment) {
686 // restore mDialogFragment in mParentFragment
687 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
688 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800689 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800690 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700691 }
692
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800693 @Override
694 public void onCancel(DialogInterface dialog) {
695 super.onCancel(dialog);
696 if (mOnCancelListener != null) {
697 mOnCancelListener.onCancel(dialog);
698 }
699 }
700
701 @Override
702 public void onDismiss(DialogInterface dialog) {
703 super.onDismiss(dialog);
704 if (mOnDismissListener != null) {
705 mOnDismissListener.onDismiss(dialog);
706 }
707 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800708
Amith Yamasanid7993472010-08-18 13:59:28 -0700709 public int getDialogId() {
710 return mDialogId;
711 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800712
713 @Override
714 public void onDetach() {
715 super.onDetach();
716
Amith Yamasani8875ede2011-01-31 12:46:57 -0800717 // This dialog fragment could be created from non-SettingsPreferenceFragment
718 if (mParentFragment instanceof SettingsPreferenceFragment) {
719 // in case the dialog is not explicitly removed by removeDialog()
720 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
721 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
722 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800723 }
724 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700725 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700726
727 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800728 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700729 }
730
731 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800732 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700733 }
734
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700735 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700736 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400737 if (activity == null) return;
738 if (getFragmentManager().getBackStackEntryCount() > 0) {
739 getFragmentManager().popBackStack();
740 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800741 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700742 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700743 }
744
Jason Monkb7e43802016-06-06 16:01:58 -0400745 protected Intent getIntent() {
746 if (getActivity() == null) {
747 return null;
748 }
749 return getActivity().getIntent();
750 }
751
752 protected void setResult(int result, Intent intent) {
753 if (getActivity() == null) {
754 return;
755 }
756 getActivity().setResult(result, intent);
757 }
758
759 protected void setResult(int result) {
760 if (getActivity() == null) {
761 return;
762 }
763 getActivity().setResult(result);
764 }
765
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700766 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
767 int requestCode, Bundle extras) {
768 final Activity activity = getActivity();
769 if (activity instanceof SettingsActivity) {
770 SettingsActivity sa = (SettingsActivity) activity;
Fan Zhangc6ca3142017-02-14 15:02:35 -0800771 sa.startPreferencePanel(
772 caller, fragmentClass, extras, titleRes, null, caller, requestCode);
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700773 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700774 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700775 Log.w(TAG,
776 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
777 + "launch the given Fragment (name: " + fragmentClass
778 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700779 return false;
780 }
781 }
Jason Monk65bb0972015-12-17 10:39:44 -0500782
783 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
784
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700785 @VisibleForTesting(otherwise=VisibleForTesting.NONE)
786 int initialHighlightedPosition = -1;
787
Jason Monk65bb0972015-12-17 10:39:44 -0500788 private int mHighlightPosition = -1;
789
790 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
791 super(preferenceGroup);
792 }
793
794 public void highlight(int position) {
795 mHighlightPosition = position;
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700796 initialHighlightedPosition = position;
Jason Monk65bb0972015-12-17 10:39:44 -0500797 notifyDataSetChanged();
798 }
799
800 @Override
801 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
802 super.onBindViewHolder(holder, position);
803 if (position == mHighlightPosition) {
804 View v = holder.itemView;
Qi Dingc4772632016-09-18 17:03:47 +0800805 v.post(() -> {
806 if (v.getBackground() != null) {
807 final int centerX = v.getWidth() / 2;
808 final int centerY = v.getHeight() / 2;
809 v.getBackground().setHotspot(centerX, centerY);
810 }
811 v.setPressed(true);
812 v.setPressed(false);
813 mHighlightPosition = -1;
814 });
Jason Monk65bb0972015-12-17 10:39:44 -0500815 }
816 }
817 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700818}