blob: c5d477aad0b5bc90afaee1f21ae57a77b4416d69 [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;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070042import android.view.View;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070043import android.view.ViewGroup;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070044import android.widget.Button;
Jason Monkb7e43802016-06-06 16:01:58 -040045
Jason Monk39b46742015-09-10 15:52:51 -040046import com.android.settings.applications.LayoutPreference;
Fan Zhang2d0b3442016-12-05 17:02:33 -080047import com.android.settings.core.InstrumentedPreferenceFragment;
Fan Zhangd65184f2016-09-19 17:45:24 -070048import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
Fan Zhang681a4cd2017-11-29 16:57:19 -080049import com.android.settings.search.actionbar.SearchMenuController;
Fan Zhange0b0e9f2017-11-29 14:55:59 -080050import com.android.settings.support.actionbar.HelpMenuController;
51import com.android.settings.support.actionbar.HelpResourceProvider;
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;
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -080055import com.android.settingslib.core.instrumentation.Instrumentable;
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
Fan Zhange0b0e9f2017-11-29 14:55:59 -080064 implements DialogCreatable, HelpResourceProvider {
Anna Galusza0285c802016-01-29 17:32:19 -080065
Jason Monk65bb0972015-12-17 10:39:44 -050066 private static final String TAG = "SettingsPreference";
Amith Yamasanid7993472010-08-18 13:59:28 -070067
Matthew Fritze33f3e3f2017-06-06 17:14:33 -070068 @VisibleForTesting
69 static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070070
71 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070072
Fan Zhangd5b48452016-12-13 12:42:50 -080073 protected final FooterPreferenceMixin mFooterPreferenceMixin =
74 new FooterPreferenceMixin(this, getLifecycle());
75
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070076
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000077 private static final int ORDER_FIRST = -1;
78 private static final int ORDER_LAST = Integer.MAX_VALUE -1;
79
Fan Zhange0b0e9f2017-11-29 14:55:59 -080080 private SettingsDialogFragment mDialogFragment;
Amith Yamasani350938e2013-04-09 10:22:47 -070081 // Cache the content resolver for async callbacks
82 private ContentResolver mContentResolver;
83
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070084 private String mPreferenceKey;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070085
Jason Monk39b46742015-09-10 15:52:51 -040086 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070087 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040088 private RecyclerView.AdapterDataObserver mDataSetObserver =
89 new RecyclerView.AdapterDataObserver() {
Tony Mantler0b825f52016-09-27 14:48:16 -070090 @Override
91 public void onChanged() {
92 onDataSetChanged();
93 }
94
95 @Override
96 public void onItemRangeChanged(int positionStart, int itemCount) {
97 onDataSetChanged();
98 }
99
100 @Override
101 public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
102 onDataSetChanged();
103 }
104
105 @Override
106 public void onItemRangeInserted(int positionStart, int itemCount) {
107 onDataSetChanged();
108 }
109
110 @Override
111 public void onItemRangeRemoved(int positionStart, int itemCount) {
112 onDataSetChanged();
113 }
114
115 @Override
116 public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
117 onDataSetChanged();
118 }
119 };
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700120
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700121 private ViewGroup mPinnedHeaderFrameLayout;
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900122 private ViewGroup mButtonBar;
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700123
Jason Monk39b46742015-09-10 15:52:51 -0400124 private LayoutPreference mHeader;
125
Jason Monk39b46742015-09-10 15:52:51 -0400126 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -0500127 private LinearLayoutManager mLayoutManager;
Jason Monk2071eda2016-02-25 13:55:48 -0500128 private ArrayMap<String, Preference> mPreferenceCache;
Jason Monkf38fb382016-03-18 14:23:01 -0400129 private boolean mAnimationAllowed;
Jason Monk39b46742015-09-10 15:52:51 -0400130
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700131 @VisibleForTesting
132 public HighlightablePreferenceGroupAdapter mAdapter;
133 @VisibleForTesting
134 public boolean mPreferenceHighlighted = false;
135
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700136 @Override
137 public void onCreate(Bundle icicle) {
138 super.onCreate(icicle);
Fan Zhang681a4cd2017-11-29 16:57:19 -0800139 SearchMenuController.init(this /* host */);
Fan Zhange0b0e9f2017-11-29 14:55:59 -0800140 HelpMenuController.init(this /* host */);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700141
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700142 if (icicle != null) {
143 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
144 }
Doris Ling9ff1d792017-11-13 13:43:17 -0800145 final Bundle arguments = getArguments();
Doris Ling9ff1d792017-11-13 13:43:17 -0800146
147 // Check if we should keep the preferences expanded.
148 if (arguments != null) {
149 mPreferenceKey = arguments.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
150 if (!TextUtils.isEmpty(mPreferenceKey)) {
151 final PreferenceScreen screen = getPreferenceScreen();
152 if (screen != null) {
153 screen.setInitialExpandedChildrenCount(Integer.MAX_VALUE);
154 }
155 }
156 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700157 }
158
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700159 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700160 public View onCreateView(LayoutInflater inflater, ViewGroup container,
161 Bundle savedInstanceState) {
162 final View root = super.onCreateView(inflater, container, savedInstanceState);
Fan Zhange0b0e9f2017-11-29 14:55:59 -0800163 mPinnedHeaderFrameLayout = root.findViewById(R.id.pinned_header);
164 mButtonBar = root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700165 return root;
166 }
167
Jason Monk39b46742015-09-10 15:52:51 -0400168 @Override
Jason Monk91e2f892016-02-23 15:31:09 -0500169 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
170 super.addPreferencesFromResource(preferencesResId);
171 checkAvailablePrefs(getPreferenceScreen());
172 }
173
174 private void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
175 if (preferenceGroup == null) return;
176 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
177 Preference pref = preferenceGroup.getPreference(i);
178 if (pref instanceof SelfAvailablePreference
179 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
180 preferenceGroup.removePreference(pref);
181 } else if (pref instanceof PreferenceGroup) {
182 checkAvailablePrefs((PreferenceGroup) pref);
183 }
184 }
185 }
186
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900187 public ViewGroup getButtonBar() {
188 return mButtonBar;
189 }
190
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700191 public View setPinnedHeaderView(int layoutResId) {
192 final LayoutInflater inflater = getActivity().getLayoutInflater();
193 final View pinnedHeader =
194 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
195 setPinnedHeaderView(pinnedHeader);
196 return pinnedHeader;
197 }
198
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700199 public void setPinnedHeaderView(View pinnedHeader) {
200 mPinnedHeaderFrameLayout.addView(pinnedHeader);
201 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
202 }
203
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700204 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700205 public void onSaveInstanceState(Bundle outState) {
206 super.onSaveInstanceState(outState);
207
208 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
209 }
210
211 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700212 public void onActivityCreated(Bundle savedInstanceState) {
213 super.onActivityCreated(savedInstanceState);
Johan Redestig76218e52016-04-19 08:29:30 +0200214 setHasOptionsMenu(true);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700215 }
216
217 @Override
218 public void onResume() {
219 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700220
Doris Ling9ff1d792017-11-13 13:43:17 -0800221 if (mPreferenceKey != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700222 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700223 }
224 }
225
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700226 @Override
227 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700228 registerObserverIfNeeded();
229 }
230
231 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700232 protected void onUnbindPreferences() {
233 unregisterObserverIfNeeded();
234 }
235
Jason Monkb37e2882016-01-11 14:27:20 -0500236 public void setLoading(boolean loading, boolean animate) {
Fan Zhang896f1b32017-06-26 14:22:45 -0700237 View loadingContainer = getView().findViewById(R.id.loading_container);
238 LoadingViewController.handleLoadingContainer(loadingContainer, getListView(),
239 !loading /* done */,
240 animate);
Jason Monkb37e2882016-01-11 14:27:20 -0500241 }
242
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700243 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700244 if (!mIsDataSetObserverRegistered) {
245 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400246 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700247 }
Jason Monk39b46742015-09-10 15:52:51 -0400248 mCurrentRootAdapter = getListView().getAdapter();
249 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700250 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500251 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700252 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700253 }
254
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700255 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700256 if (mIsDataSetObserverRegistered) {
257 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400258 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700259 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700260 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700261 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700262 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700263 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700264
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700265 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700266 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700267 getView().postDelayed(new Runnable() {
268 @Override
269 public void run() {
270 highlightPreference(mPreferenceKey);
271 }
272 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700273 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700274 }
275
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000276 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400277 highlightPreferenceIfNeeded();
278 updateEmptyView();
279 }
280
Jason Monk39b46742015-09-10 15:52:51 -0400281 public LayoutPreference getHeaderView() {
282 return mHeader;
283 }
284
Jason Monk39b46742015-09-10 15:52:51 -0400285 protected void setHeaderView(int resource) {
286 mHeader = new LayoutPreference(getPrefContext(), resource);
Udam Sainid553abc2016-02-16 17:54:13 -0800287 addPreferenceToTop(mHeader);
288 }
289
290 protected void setHeaderView(View view) {
291 mHeader = new LayoutPreference(getPrefContext(), view);
292 addPreferenceToTop(mHeader);
293 }
294
295 private void addPreferenceToTop(LayoutPreference preference) {
296 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400297 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800298 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400299 }
300 }
301
Jason Monk39b46742015-09-10 15:52:51 -0400302 @Override
303 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400304 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400305 // Without ids generated, the RecyclerView won't animate changes to the preferences.
306 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
307 }
Jason Monk39b46742015-09-10 15:52:51 -0400308 super.setPreferenceScreen(preferenceScreen);
309 if (preferenceScreen != null) {
310 if (mHeader != null) {
311 preferenceScreen.addPreference(mHeader);
312 }
Jason Monk39b46742015-09-10 15:52:51 -0400313 }
314 }
315
jackqdyulei2b2abac2017-05-26 10:47:55 -0700316 @VisibleForTesting
317 void updateEmptyView() {
Jason Monk39b46742015-09-10 15:52:51 -0400318 if (mEmptyView == null) return;
319 if (getPreferenceScreen() != null) {
jackqdyulei2b2abac2017-05-26 10:47:55 -0700320 final View listContainer = getActivity().findViewById(android.R.id.list_container);
Jason Monk39b46742015-09-10 15:52:51 -0400321 boolean show = (getPreferenceScreen().getPreferenceCount()
322 - (mHeader != null ? 1 : 0)
jackqdyulei2b2abac2017-05-26 10:47:55 -0700323 - (mFooterPreferenceMixin.hasFooter() ? 1 : 0)) <= 0
324 || (listContainer != null && listContainer.getVisibility() != View.VISIBLE);
Jason Monk39b46742015-09-10 15:52:51 -0400325 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
326 } else {
327 mEmptyView.setVisibility(View.VISIBLE);
328 }
329 }
330
331 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000332 if (mEmptyView != null) {
333 mEmptyView.setVisibility(View.GONE);
334 }
Jason Monk39b46742015-09-10 15:52:51 -0400335 mEmptyView = v;
336 updateEmptyView();
337 }
338
339 public View getEmptyView() {
340 return mEmptyView;
341 }
342
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700343 /**
344 * Return a valid ListView position or -1 if none is found
345 */
346 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400347 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700348 return -1;
349 }
350
Jason Monk39b46742015-09-10 15:52:51 -0400351 RecyclerView listView = getListView();
352 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700353
354 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400355 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700356 }
357
358 return -1;
359 }
360
Jason Monk65bb0972015-12-17 10:39:44 -0500361 @Override
362 public RecyclerView.LayoutManager onCreateLayoutManager() {
363 mLayoutManager = new LinearLayoutManager(getContext());
364 return mLayoutManager;
365 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700366
Jason Monk65bb0972015-12-17 10:39:44 -0500367 @Override
368 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
369 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
370 return mAdapter;
371 }
372
Jason Monkf38fb382016-03-18 14:23:01 -0400373 protected void setAnimationAllowed(boolean animationAllowed) {
374 mAnimationAllowed = animationAllowed;
375 }
376
Jason Monk2071eda2016-02-25 13:55:48 -0500377 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
378 mPreferenceCache = new ArrayMap<String, Preference>();
379 final int N = group.getPreferenceCount();
380 for (int i = 0; i < N; i++) {
381 Preference p = group.getPreference(i);
382 if (TextUtils.isEmpty(p.getKey())) {
383 continue;
384 }
385 mPreferenceCache.put(p.getKey(), p);
386 }
387 }
388
389 protected Preference getCachedPreference(String key) {
390 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
391 }
392
393 protected void removeCachedPrefs(PreferenceGroup group) {
394 for (Preference p : mPreferenceCache.values()) {
395 group.removePreference(p);
396 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400397 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500398 }
399
Jason Monka6278442016-04-21 10:12:30 -0400400 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400401 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400402 }
403
Jason Monk65bb0972015-12-17 10:39:44 -0500404 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700405 final int position = canUseListViewForHighLighting(key);
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700406 if (position < 0) {
407 return;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700408 }
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700409
410 mPreferenceHighlighted = true;
411 mLayoutManager.scrollToPosition(position);
412 mAdapter.highlight(position);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700413 }
414
Jason Monk39b46742015-09-10 15:52:51 -0400415 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
416 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700417 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400418 final Preference preference = adapter.getItem(n);
419 final String preferenceKey = preference.getKey();
420 if (preferenceKey != null && preferenceKey.equals(key)) {
421 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700422 }
423 }
424 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700425 }
426
Fan Zhange84407f2017-05-24 11:19:52 -0700427 protected boolean removePreference(String key) {
428 return removePreference(getPreferenceScreen(), key);
429 }
430
431 @VisibleForTesting
432 boolean removePreference(PreferenceGroup group, String key) {
433 final int preferenceCount = group.getPreferenceCount();
434 for (int i = 0; i < preferenceCount; i++) {
435 final Preference preference = group.getPreference(i);
436 final String curKey = preference.getKey();
437
438 if (TextUtils.equals(curKey, key)) {
439 return group.removePreference(preference);
440 }
441
442 if (preference instanceof PreferenceGroup) {
443 if (removePreference((PreferenceGroup) preference, key)) {
444 return true;
445 }
446 }
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700447 }
Fan Zhange84407f2017-05-24 11:19:52 -0700448 return false;
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700449 }
450
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700451 /*
452 * The name is intentionally made different from Activity#finish(), so that
453 * users won't misunderstand its meaning.
454 */
455 public final void finishFragment() {
456 getActivity().onBackPressed();
457 }
458
Amith Yamasanid7993472010-08-18 13:59:28 -0700459 // Some helpers for functions used by the settings fragments when they were activities
460
461 /**
462 * Returns the ContentResolver from the owning Activity.
463 */
464 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700465 Context context = getActivity();
466 if (context != null) {
467 mContentResolver = context.getContentResolver();
468 }
469 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700470 }
471
472 /**
473 * Returns the specified system service from the owning Activity.
474 */
475 protected Object getSystemService(final String name) {
476 return getActivity().getSystemService(name);
477 }
478
479 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700480 * Returns the PackageManager from the owning Activity.
481 */
482 protected PackageManager getPackageManager() {
483 return getActivity().getPackageManager();
484 }
485
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800486 @Override
487 public void onDetach() {
488 if (isRemoving()) {
489 if (mDialogFragment != null) {
490 mDialogFragment.dismiss();
491 mDialogFragment = null;
492 }
493 }
494 super.onDetach();
495 }
496
Amith Yamasanid7993472010-08-18 13:59:28 -0700497 // Dialog management
498
499 protected void showDialog(int dialogId) {
500 if (mDialogFragment != null) {
501 Log.e(TAG, "Old dialog fragment not null!");
502 }
503 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800504 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700505 }
506
Fan Zhangd65184f2016-09-19 17:45:24 -0700507 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700508 public Dialog onCreateDialog(int dialogId) {
509 return null;
510 }
511
Fan Zhangd65184f2016-09-19 17:45:24 -0700512 @Override
513 public int getDialogMetricsCategory(int dialogId) {
514 return 0;
515 }
516
Amith Yamasanid7993472010-08-18 13:59:28 -0700517 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800518 // mDialogFragment may not be visible yet in parent fragment's onResume().
519 // To be able to dismiss dialog at that time, don't check
520 // mDialogFragment.isVisible().
521 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400522 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700523 }
524 mDialogFragment = null;
525 }
526
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800527 /**
528 * Sets the OnCancelListener of the dialog shown. This method can only be
529 * called after showDialog(int) and before removeDialog(int). The method
530 * does nothing otherwise.
531 */
532 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
533 if (mDialogFragment != null) {
534 mDialogFragment.mOnCancelListener = listener;
535 }
536 }
537
538 /**
539 * Sets the OnDismissListener of the dialog shown. This method can only be
540 * called after showDialog(int) and before removeDialog(int). The method
541 * does nothing otherwise.
542 */
543 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
544 if (mDialogFragment != null) {
545 mDialogFragment.mOnDismissListener = listener;
546 }
547 }
548
Amith Yamasanic861cf82012-10-02 14:51:46 -0700549 public void onDialogShowing() {
550 // override in subclass to attach a dismiss listener, for instance
551 }
552
Jason Monk39b46742015-09-10 15:52:51 -0400553 @Override
554 public void onDisplayPreferenceDialog(Preference preference) {
555 if (preference.getKey() == null) {
556 // Auto-key preferences that don't have a key, so the dialog can find them.
557 preference.setKey(UUID.randomUUID().toString());
558 }
559 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000560 if (preference instanceof RestrictedListPreference) {
561 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
562 .newInstance(preference.getKey());
563 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400564 f = CustomListPreference.CustomListPreferenceDialogFragment
565 .newInstance(preference.getKey());
566 } else if (preference instanceof CustomDialogPreference) {
567 f = CustomDialogPreference.CustomPreferenceDialogFragment
568 .newInstance(preference.getKey());
569 } else if (preference instanceof CustomEditTextPreference) {
570 f = CustomEditTextPreference.CustomPreferenceDialogFragment
571 .newInstance(preference.getKey());
572 } else {
573 super.onDisplayPreferenceDialog(preference);
574 return;
575 }
576 f.setTargetFragment(this, 0);
577 f.show(getFragmentManager(), "dialog_preference");
578 onDialogShowing();
579 }
580
Fan Zhangd65184f2016-09-19 17:45:24 -0700581 public static class SettingsDialogFragment extends InstrumentedDialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800582 private static final String KEY_DIALOG_ID = "key_dialog_id";
583 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
584
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800585 private Fragment mParentFragment;
586
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800587 private DialogInterface.OnCancelListener mOnCancelListener;
588 private DialogInterface.OnDismissListener mOnDismissListener;
589
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800590 public SettingsDialogFragment() {
591 /* do nothing */
592 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700593
Amith Yamasani43c69782010-12-01 09:04:36 -0800594 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Fan Zhangd65184f2016-09-19 17:45:24 -0700595 super(fragment, dialogId);
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800596 if (!(fragment instanceof Fragment)) {
597 throw new IllegalArgumentException("fragment argument must be an instance of "
598 + Fragment.class.getName());
599 }
600 mParentFragment = (Fragment) fragment;
601 }
602
Fan Zhangd65184f2016-09-19 17:45:24 -0700603
604 @Override
605 public int getMetricsCategory() {
Fan Zhang4fe7c082016-10-03 13:48:55 -0700606 if (mDialogCreatable == null) {
607 return Instrumentable.METRICS_CATEGORY_UNKNOWN;
608 }
Fan Zhangd65184f2016-09-19 17:45:24 -0700609 final int metricsCategory = mDialogCreatable.getDialogMetricsCategory(mDialogId);
610 if (metricsCategory <= 0) {
611 throw new IllegalStateException("Dialog must provide a metrics category");
612 }
613 return metricsCategory;
614 }
615
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800616 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800617 public void onSaveInstanceState(Bundle outState) {
618 super.onSaveInstanceState(outState);
619 if (mParentFragment != null) {
620 outState.putInt(KEY_DIALOG_ID, mDialogId);
621 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
622 }
623 }
624
625 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700626 public void onStart() {
627 super.onStart();
628
629 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
630 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
631 }
632 }
633
634 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800635 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800636 if (savedInstanceState != null) {
637 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800638 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800639 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700640 if (mParentFragment == null) {
641 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
642 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800643 if (!(mParentFragment instanceof DialogCreatable)) {
644 throw new IllegalArgumentException(
645 (mParentFragment != null
646 ? mParentFragment.getClass().getName()
647 : mParentFragmentId)
648 + " must implement "
649 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800650 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800651 // This dialog fragment could be created from non-SettingsPreferenceFragment
652 if (mParentFragment instanceof SettingsPreferenceFragment) {
653 // restore mDialogFragment in mParentFragment
654 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
655 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800656 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800657 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700658 }
659
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800660 @Override
661 public void onCancel(DialogInterface dialog) {
662 super.onCancel(dialog);
663 if (mOnCancelListener != null) {
664 mOnCancelListener.onCancel(dialog);
665 }
666 }
667
668 @Override
669 public void onDismiss(DialogInterface dialog) {
670 super.onDismiss(dialog);
671 if (mOnDismissListener != null) {
672 mOnDismissListener.onDismiss(dialog);
673 }
674 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800675
Amith Yamasanid7993472010-08-18 13:59:28 -0700676 public int getDialogId() {
677 return mDialogId;
678 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800679
680 @Override
681 public void onDetach() {
682 super.onDetach();
683
Amith Yamasani8875ede2011-01-31 12:46:57 -0800684 // This dialog fragment could be created from non-SettingsPreferenceFragment
685 if (mParentFragment instanceof SettingsPreferenceFragment) {
686 // in case the dialog is not explicitly removed by removeDialog()
687 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
688 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
689 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800690 }
691 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700692 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700693
694 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800695 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700696 }
697
698 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800699 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700700 }
701
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700702 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700703 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400704 if (activity == null) return;
705 if (getFragmentManager().getBackStackEntryCount() > 0) {
706 getFragmentManager().popBackStack();
707 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800708 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700709 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700710 }
711
Jason Monkb7e43802016-06-06 16:01:58 -0400712 protected Intent getIntent() {
713 if (getActivity() == null) {
714 return null;
715 }
716 return getActivity().getIntent();
717 }
718
719 protected void setResult(int result, Intent intent) {
720 if (getActivity() == null) {
721 return;
722 }
723 getActivity().setResult(result, intent);
724 }
725
726 protected void setResult(int result) {
727 if (getActivity() == null) {
728 return;
729 }
730 getActivity().setResult(result);
731 }
732
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700733 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
734 int requestCode, Bundle extras) {
735 final Activity activity = getActivity();
736 if (activity instanceof SettingsActivity) {
737 SettingsActivity sa = (SettingsActivity) activity;
Fan Zhangc6ca3142017-02-14 15:02:35 -0800738 sa.startPreferencePanel(
739 caller, fragmentClass, extras, titleRes, null, caller, requestCode);
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700740 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700741 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700742 Log.w(TAG,
743 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
744 + "launch the given Fragment (name: " + fragmentClass
745 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700746 return false;
747 }
748 }
Jason Monk65bb0972015-12-17 10:39:44 -0500749
750 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
751
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700752 @VisibleForTesting(otherwise=VisibleForTesting.NONE)
753 int initialHighlightedPosition = -1;
754
Jason Monk65bb0972015-12-17 10:39:44 -0500755 private int mHighlightPosition = -1;
756
757 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
758 super(preferenceGroup);
759 }
760
761 public void highlight(int position) {
762 mHighlightPosition = position;
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700763 initialHighlightedPosition = position;
Jason Monk65bb0972015-12-17 10:39:44 -0500764 notifyDataSetChanged();
765 }
766
767 @Override
768 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
769 super.onBindViewHolder(holder, position);
770 if (position == mHighlightPosition) {
771 View v = holder.itemView;
Qi Dingc4772632016-09-18 17:03:47 +0800772 v.post(() -> {
773 if (v.getBackground() != null) {
774 final int centerX = v.getWidth() / 2;
775 final int centerY = v.getHeight() / 2;
776 v.getBackground().setHotspot(centerX, centerY);
777 }
778 v.setPressed(true);
779 v.setPressed(false);
780 mHighlightPosition = -1;
781 });
Jason Monk65bb0972015-12-17 10:39:44 -0500782 }
783 }
784 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700785}