blob: a3d26af8eb74b6f82e752a3e45535937e0382ace [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
Anna Galusza0285c802016-01-29 17:32:19 -0800153 Bundle arguments = getArguments();
154 int helpResource;
155 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 }
163 }
164
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700165 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700166 public View onCreateView(LayoutInflater inflater, ViewGroup container,
167 Bundle savedInstanceState) {
168 final View root = super.onCreateView(inflater, container, savedInstanceState);
169 mPinnedHeaderFrameLayout = (ViewGroup) root.findViewById(R.id.pinned_header);
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900170 mButtonBar = (ViewGroup) root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700171 return root;
172 }
173
Jason Monk39b46742015-09-10 15:52:51 -0400174 @Override
Jason Monk91e2f892016-02-23 15:31:09 -0500175 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
176 super.addPreferencesFromResource(preferencesResId);
177 checkAvailablePrefs(getPreferenceScreen());
178 }
179
180 private void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
181 if (preferenceGroup == null) return;
182 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
183 Preference pref = preferenceGroup.getPreference(i);
184 if (pref instanceof SelfAvailablePreference
185 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
186 preferenceGroup.removePreference(pref);
187 } else if (pref instanceof PreferenceGroup) {
188 checkAvailablePrefs((PreferenceGroup) pref);
189 }
190 }
191 }
192
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900193 public ViewGroup getButtonBar() {
194 return mButtonBar;
195 }
196
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700197 public View setPinnedHeaderView(int layoutResId) {
198 final LayoutInflater inflater = getActivity().getLayoutInflater();
199 final View pinnedHeader =
200 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
201 setPinnedHeaderView(pinnedHeader);
202 return pinnedHeader;
203 }
204
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700205 public void setPinnedHeaderView(View pinnedHeader) {
206 mPinnedHeaderFrameLayout.addView(pinnedHeader);
207 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
208 }
209
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700210 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700211 public void onSaveInstanceState(Bundle outState) {
212 super.onSaveInstanceState(outState);
213
214 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
215 }
216
217 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700218 public void onActivityCreated(Bundle savedInstanceState) {
219 super.onActivityCreated(savedInstanceState);
Johan Redestig76218e52016-04-19 08:29:30 +0200220 setHasOptionsMenu(true);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700221 }
222
223 @Override
224 public void onResume() {
225 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700226
227 final Bundle args = getArguments();
228 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700229 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
230 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700231 }
232 }
233
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700234 @Override
235 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700236 registerObserverIfNeeded();
237 }
238
239 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700240 protected void onUnbindPreferences() {
241 unregisterObserverIfNeeded();
242 }
243
Jason Monkb37e2882016-01-11 14:27:20 -0500244 public void setLoading(boolean loading, boolean animate) {
Fan Zhang896f1b32017-06-26 14:22:45 -0700245 View loadingContainer = getView().findViewById(R.id.loading_container);
246 LoadingViewController.handleLoadingContainer(loadingContainer, getListView(),
247 !loading /* done */,
248 animate);
Jason Monkb37e2882016-01-11 14:27:20 -0500249 }
250
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700251 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700252 if (!mIsDataSetObserverRegistered) {
253 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400254 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700255 }
Jason Monk39b46742015-09-10 15:52:51 -0400256 mCurrentRootAdapter = getListView().getAdapter();
257 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700258 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500259 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700260 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700261 }
262
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700263 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700264 if (mIsDataSetObserverRegistered) {
265 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400266 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700267 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700268 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700269 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700270 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700271 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700272
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700273 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700274 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700275 getView().postDelayed(new Runnable() {
276 @Override
277 public void run() {
278 highlightPreference(mPreferenceKey);
279 }
280 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700281 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700282 }
283
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000284 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400285 highlightPreferenceIfNeeded();
286 updateEmptyView();
287 }
288
Jason Monk39b46742015-09-10 15:52:51 -0400289 public LayoutPreference getHeaderView() {
290 return mHeader;
291 }
292
Jason Monk39b46742015-09-10 15:52:51 -0400293 protected void setHeaderView(int resource) {
294 mHeader = new LayoutPreference(getPrefContext(), resource);
Udam Sainid553abc2016-02-16 17:54:13 -0800295 addPreferenceToTop(mHeader);
296 }
297
298 protected void setHeaderView(View view) {
299 mHeader = new LayoutPreference(getPrefContext(), view);
300 addPreferenceToTop(mHeader);
301 }
302
303 private void addPreferenceToTop(LayoutPreference preference) {
304 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400305 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800306 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400307 }
308 }
309
Jason Monk39b46742015-09-10 15:52:51 -0400310 @Override
311 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400312 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400313 // Without ids generated, the RecyclerView won't animate changes to the preferences.
314 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
315 }
Jason Monk39b46742015-09-10 15:52:51 -0400316 super.setPreferenceScreen(preferenceScreen);
317 if (preferenceScreen != null) {
318 if (mHeader != null) {
319 preferenceScreen.addPreference(mHeader);
320 }
Jason Monk39b46742015-09-10 15:52:51 -0400321 }
322 }
323
jackqdyulei2b2abac2017-05-26 10:47:55 -0700324 @VisibleForTesting
325 void updateEmptyView() {
Jason Monk39b46742015-09-10 15:52:51 -0400326 if (mEmptyView == null) return;
327 if (getPreferenceScreen() != null) {
jackqdyulei2b2abac2017-05-26 10:47:55 -0700328 final View listContainer = getActivity().findViewById(android.R.id.list_container);
Jason Monk39b46742015-09-10 15:52:51 -0400329 boolean show = (getPreferenceScreen().getPreferenceCount()
330 - (mHeader != null ? 1 : 0)
jackqdyulei2b2abac2017-05-26 10:47:55 -0700331 - (mFooterPreferenceMixin.hasFooter() ? 1 : 0)) <= 0
332 || (listContainer != null && listContainer.getVisibility() != View.VISIBLE);
Jason Monk39b46742015-09-10 15:52:51 -0400333 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
334 } else {
335 mEmptyView.setVisibility(View.VISIBLE);
336 }
337 }
338
339 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000340 if (mEmptyView != null) {
341 mEmptyView.setVisibility(View.GONE);
342 }
Jason Monk39b46742015-09-10 15:52:51 -0400343 mEmptyView = v;
344 updateEmptyView();
345 }
346
347 public View getEmptyView() {
348 return mEmptyView;
349 }
350
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700351 /**
352 * Return a valid ListView position or -1 if none is found
353 */
354 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400355 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700356 return -1;
357 }
358
Jason Monk39b46742015-09-10 15:52:51 -0400359 RecyclerView listView = getListView();
360 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700361
362 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400363 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700364 }
365
366 return -1;
367 }
368
Jason Monk65bb0972015-12-17 10:39:44 -0500369 @Override
370 public RecyclerView.LayoutManager onCreateLayoutManager() {
371 mLayoutManager = new LinearLayoutManager(getContext());
372 return mLayoutManager;
373 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700374
Jason Monk65bb0972015-12-17 10:39:44 -0500375 @Override
376 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
377 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
378 return mAdapter;
379 }
380
Jason Monkf38fb382016-03-18 14:23:01 -0400381 protected void setAnimationAllowed(boolean animationAllowed) {
382 mAnimationAllowed = animationAllowed;
383 }
384
Jason Monk2071eda2016-02-25 13:55:48 -0500385 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
386 mPreferenceCache = new ArrayMap<String, Preference>();
387 final int N = group.getPreferenceCount();
388 for (int i = 0; i < N; i++) {
389 Preference p = group.getPreference(i);
390 if (TextUtils.isEmpty(p.getKey())) {
391 continue;
392 }
393 mPreferenceCache.put(p.getKey(), p);
394 }
395 }
396
397 protected Preference getCachedPreference(String key) {
398 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
399 }
400
401 protected void removeCachedPrefs(PreferenceGroup group) {
402 for (Preference p : mPreferenceCache.values()) {
403 group.removePreference(p);
404 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400405 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500406 }
407
Jason Monka6278442016-04-21 10:12:30 -0400408 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400409 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400410 }
411
Jason Monk65bb0972015-12-17 10:39:44 -0500412 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700413 final int position = canUseListViewForHighLighting(key);
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700414 if (position < 0) {
415 return;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700416 }
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700417
418 mPreferenceHighlighted = true;
419 mLayoutManager.scrollToPosition(position);
420 mAdapter.highlight(position);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700421 }
422
Jason Monk39b46742015-09-10 15:52:51 -0400423 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
424 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700425 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400426 final Preference preference = adapter.getItem(n);
427 final String preferenceKey = preference.getKey();
428 if (preferenceKey != null && preferenceKey.equals(key)) {
429 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700430 }
431 }
432 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700433 }
434
Fan Zhange84407f2017-05-24 11:19:52 -0700435 protected boolean removePreference(String key) {
436 return removePreference(getPreferenceScreen(), key);
437 }
438
439 @VisibleForTesting
440 boolean removePreference(PreferenceGroup group, String key) {
441 final int preferenceCount = group.getPreferenceCount();
442 for (int i = 0; i < preferenceCount; i++) {
443 final Preference preference = group.getPreference(i);
444 final String curKey = preference.getKey();
445
446 if (TextUtils.equals(curKey, key)) {
447 return group.removePreference(preference);
448 }
449
450 if (preference instanceof PreferenceGroup) {
451 if (removePreference((PreferenceGroup) preference, key)) {
452 return true;
453 }
454 }
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700455 }
Fan Zhange84407f2017-05-24 11:19:52 -0700456 return false;
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700457 }
458
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700459 /**
460 * Override this if you want to show a help item in the menu, by returning the resource id.
461 * @return the resource id for the help url
462 */
463 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400464 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700465 }
466
467 @Override
468 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Daniel Nishie6740f72017-04-14 14:12:35 -0700469 super.onCreateOptionsMenu(menu, inflater);
Jason Monk23acc2b2015-04-14 15:06:39 -0400470 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400471 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700472 }
473 }
474
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700475 /*
476 * The name is intentionally made different from Activity#finish(), so that
477 * users won't misunderstand its meaning.
478 */
479 public final void finishFragment() {
480 getActivity().onBackPressed();
481 }
482
Amith Yamasanid7993472010-08-18 13:59:28 -0700483 // Some helpers for functions used by the settings fragments when they were activities
484
485 /**
486 * Returns the ContentResolver from the owning Activity.
487 */
488 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700489 Context context = getActivity();
490 if (context != null) {
491 mContentResolver = context.getContentResolver();
492 }
493 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700494 }
495
496 /**
497 * Returns the specified system service from the owning Activity.
498 */
499 protected Object getSystemService(final String name) {
500 return getActivity().getSystemService(name);
501 }
502
503 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700504 * Returns the PackageManager from the owning Activity.
505 */
506 protected PackageManager getPackageManager() {
507 return getActivity().getPackageManager();
508 }
509
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800510 @Override
511 public void onDetach() {
512 if (isRemoving()) {
513 if (mDialogFragment != null) {
514 mDialogFragment.dismiss();
515 mDialogFragment = null;
516 }
517 }
518 super.onDetach();
519 }
520
Amith Yamasanid7993472010-08-18 13:59:28 -0700521 // Dialog management
522
523 protected void showDialog(int dialogId) {
524 if (mDialogFragment != null) {
525 Log.e(TAG, "Old dialog fragment not null!");
526 }
527 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800528 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700529 }
530
Fan Zhangd65184f2016-09-19 17:45:24 -0700531 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700532 public Dialog onCreateDialog(int dialogId) {
533 return null;
534 }
535
Fan Zhangd65184f2016-09-19 17:45:24 -0700536 @Override
537 public int getDialogMetricsCategory(int dialogId) {
538 return 0;
539 }
540
Amith Yamasanid7993472010-08-18 13:59:28 -0700541 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800542 // mDialogFragment may not be visible yet in parent fragment's onResume().
543 // To be able to dismiss dialog at that time, don't check
544 // mDialogFragment.isVisible().
545 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400546 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700547 }
548 mDialogFragment = null;
549 }
550
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800551 /**
552 * Sets the OnCancelListener of the dialog shown. This method can only be
553 * called after showDialog(int) and before removeDialog(int). The method
554 * does nothing otherwise.
555 */
556 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
557 if (mDialogFragment != null) {
558 mDialogFragment.mOnCancelListener = listener;
559 }
560 }
561
562 /**
563 * Sets the OnDismissListener of the dialog shown. This method can only be
564 * called after showDialog(int) and before removeDialog(int). The method
565 * does nothing otherwise.
566 */
567 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
568 if (mDialogFragment != null) {
569 mDialogFragment.mOnDismissListener = listener;
570 }
571 }
572
Amith Yamasanic861cf82012-10-02 14:51:46 -0700573 public void onDialogShowing() {
574 // override in subclass to attach a dismiss listener, for instance
575 }
576
Jason Monk39b46742015-09-10 15:52:51 -0400577 @Override
578 public void onDisplayPreferenceDialog(Preference preference) {
579 if (preference.getKey() == null) {
580 // Auto-key preferences that don't have a key, so the dialog can find them.
581 preference.setKey(UUID.randomUUID().toString());
582 }
583 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000584 if (preference instanceof RestrictedListPreference) {
585 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
586 .newInstance(preference.getKey());
587 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400588 f = CustomListPreference.CustomListPreferenceDialogFragment
589 .newInstance(preference.getKey());
590 } else if (preference instanceof CustomDialogPreference) {
591 f = CustomDialogPreference.CustomPreferenceDialogFragment
592 .newInstance(preference.getKey());
593 } else if (preference instanceof CustomEditTextPreference) {
594 f = CustomEditTextPreference.CustomPreferenceDialogFragment
595 .newInstance(preference.getKey());
596 } else {
597 super.onDisplayPreferenceDialog(preference);
598 return;
599 }
600 f.setTargetFragment(this, 0);
601 f.show(getFragmentManager(), "dialog_preference");
602 onDialogShowing();
603 }
604
Fan Zhangd65184f2016-09-19 17:45:24 -0700605 public static class SettingsDialogFragment extends InstrumentedDialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800606 private static final String KEY_DIALOG_ID = "key_dialog_id";
607 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
608
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800609 private Fragment mParentFragment;
610
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800611 private DialogInterface.OnCancelListener mOnCancelListener;
612 private DialogInterface.OnDismissListener mOnDismissListener;
613
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800614 public SettingsDialogFragment() {
615 /* do nothing */
616 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700617
Amith Yamasani43c69782010-12-01 09:04:36 -0800618 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Fan Zhangd65184f2016-09-19 17:45:24 -0700619 super(fragment, dialogId);
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800620 if (!(fragment instanceof Fragment)) {
621 throw new IllegalArgumentException("fragment argument must be an instance of "
622 + Fragment.class.getName());
623 }
624 mParentFragment = (Fragment) fragment;
625 }
626
Fan Zhangd65184f2016-09-19 17:45:24 -0700627
628 @Override
629 public int getMetricsCategory() {
Fan Zhang4fe7c082016-10-03 13:48:55 -0700630 if (mDialogCreatable == null) {
631 return Instrumentable.METRICS_CATEGORY_UNKNOWN;
632 }
Fan Zhangd65184f2016-09-19 17:45:24 -0700633 final int metricsCategory = mDialogCreatable.getDialogMetricsCategory(mDialogId);
634 if (metricsCategory <= 0) {
635 throw new IllegalStateException("Dialog must provide a metrics category");
636 }
637 return metricsCategory;
638 }
639
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800640 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800641 public void onSaveInstanceState(Bundle outState) {
642 super.onSaveInstanceState(outState);
643 if (mParentFragment != null) {
644 outState.putInt(KEY_DIALOG_ID, mDialogId);
645 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
646 }
647 }
648
649 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700650 public void onStart() {
651 super.onStart();
652
653 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
654 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
655 }
656 }
657
658 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800659 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800660 if (savedInstanceState != null) {
661 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800662 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800663 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700664 if (mParentFragment == null) {
665 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
666 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800667 if (!(mParentFragment instanceof DialogCreatable)) {
668 throw new IllegalArgumentException(
669 (mParentFragment != null
670 ? mParentFragment.getClass().getName()
671 : mParentFragmentId)
672 + " must implement "
673 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800674 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800675 // This dialog fragment could be created from non-SettingsPreferenceFragment
676 if (mParentFragment instanceof SettingsPreferenceFragment) {
677 // restore mDialogFragment in mParentFragment
678 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
679 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800680 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800681 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700682 }
683
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800684 @Override
685 public void onCancel(DialogInterface dialog) {
686 super.onCancel(dialog);
687 if (mOnCancelListener != null) {
688 mOnCancelListener.onCancel(dialog);
689 }
690 }
691
692 @Override
693 public void onDismiss(DialogInterface dialog) {
694 super.onDismiss(dialog);
695 if (mOnDismissListener != null) {
696 mOnDismissListener.onDismiss(dialog);
697 }
698 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800699
Amith Yamasanid7993472010-08-18 13:59:28 -0700700 public int getDialogId() {
701 return mDialogId;
702 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800703
704 @Override
705 public void onDetach() {
706 super.onDetach();
707
Amith Yamasani8875ede2011-01-31 12:46:57 -0800708 // This dialog fragment could be created from non-SettingsPreferenceFragment
709 if (mParentFragment instanceof SettingsPreferenceFragment) {
710 // in case the dialog is not explicitly removed by removeDialog()
711 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
712 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
713 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800714 }
715 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700716 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700717
718 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800719 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700720 }
721
722 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800723 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700724 }
725
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700726 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700727 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400728 if (activity == null) return;
729 if (getFragmentManager().getBackStackEntryCount() > 0) {
730 getFragmentManager().popBackStack();
731 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800732 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700733 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700734 }
735
Jason Monkb7e43802016-06-06 16:01:58 -0400736 protected Intent getIntent() {
737 if (getActivity() == null) {
738 return null;
739 }
740 return getActivity().getIntent();
741 }
742
743 protected void setResult(int result, Intent intent) {
744 if (getActivity() == null) {
745 return;
746 }
747 getActivity().setResult(result, intent);
748 }
749
750 protected void setResult(int result) {
751 if (getActivity() == null) {
752 return;
753 }
754 getActivity().setResult(result);
755 }
756
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700757 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
758 int requestCode, Bundle extras) {
759 final Activity activity = getActivity();
760 if (activity instanceof SettingsActivity) {
761 SettingsActivity sa = (SettingsActivity) activity;
Fan Zhangc6ca3142017-02-14 15:02:35 -0800762 sa.startPreferencePanel(
763 caller, fragmentClass, extras, titleRes, null, caller, requestCode);
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700764 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700765 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700766 Log.w(TAG,
767 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
768 + "launch the given Fragment (name: " + fragmentClass
769 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700770 return false;
771 }
772 }
Jason Monk65bb0972015-12-17 10:39:44 -0500773
774 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
775
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700776 @VisibleForTesting(otherwise=VisibleForTesting.NONE)
777 int initialHighlightedPosition = -1;
778
Jason Monk65bb0972015-12-17 10:39:44 -0500779 private int mHighlightPosition = -1;
780
781 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
782 super(preferenceGroup);
783 }
784
785 public void highlight(int position) {
786 mHighlightPosition = position;
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700787 initialHighlightedPosition = position;
Jason Monk65bb0972015-12-17 10:39:44 -0500788 notifyDataSetChanged();
789 }
790
791 @Override
792 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
793 super.onBindViewHolder(holder, position);
794 if (position == mHighlightPosition) {
795 View v = holder.itemView;
Qi Dingc4772632016-09-18 17:03:47 +0800796 v.post(() -> {
797 if (v.getBackground() != null) {
798 final int centerX = v.getWidth() / 2;
799 final int centerY = v.getHeight() / 2;
800 v.getBackground().setHotspot(centerX, centerY);
801 }
802 v.setPressed(true);
803 v.setPressed(false);
804 mHighlightPosition = -1;
805 });
Jason Monk65bb0972015-12-17 10:39:44 -0500806 }
807 }
808 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700809}