blob: 9d5e343f4163115173f868387503d78013f3332b [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 Zhangd5b48452016-12-13 12:42:50 -080052import com.android.settings.widget.FooterPreferenceMixin;
Suprabh Shuklab84720c2016-04-05 14:37:20 -070053import com.android.settingslib.HelpUtils;
John Spurlockb8e02b82015-04-15 21:15:55 -040054
Jason Monk39b46742015-09-10 15:52:51 -040055import java.util.UUID;
56
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070057/**
Amith Yamasanid7993472010-08-18 13:59:28 -070058 * Base class for Settings fragments, with some helper functions and dialog management.
59 */
Fan Zhang2d0b3442016-12-05 17:02:33 -080060public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceFragment
Chris Wren8a963ba2015-03-20 10:29:14 -040061 implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070062
Anna Galusza0285c802016-01-29 17:32:19 -080063 /**
64 * The Help Uri Resource key. This can be passed as an extra argument when creating the
65 * Fragment.
66 **/
67 public static final String HELP_URI_RESOURCE_KEY = "help_uri_resource";
68
Jason Monk65bb0972015-12-17 10:39:44 -050069 private static final String TAG = "SettingsPreference";
Amith Yamasanid7993472010-08-18 13:59:28 -070070
Matthew Fritze33f3e3f2017-06-06 17:14:33 -070071 @VisibleForTesting
72 static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070073
74 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070075
Fan Zhangd5b48452016-12-13 12:42:50 -080076 protected final FooterPreferenceMixin mFooterPreferenceMixin =
77 new FooterPreferenceMixin(this, getLifecycle());
78
Amith Yamasanid7993472010-08-18 13:59:28 -070079 private SettingsDialogFragment mDialogFragment;
80
Jason Monk23acc2b2015-04-14 15:06:39 -040081 private String mHelpUri;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070082
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000083 private static final int ORDER_FIRST = -1;
84 private static final int ORDER_LAST = Integer.MAX_VALUE -1;
85
Amith Yamasani350938e2013-04-09 10:22:47 -070086 // Cache the content resolver for async callbacks
87 private ContentResolver mContentResolver;
88
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070089 private String mPreferenceKey;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070090
Jason Monk39b46742015-09-10 15:52:51 -040091 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070092 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040093 private RecyclerView.AdapterDataObserver mDataSetObserver =
94 new RecyclerView.AdapterDataObserver() {
Tony Mantler0b825f52016-09-27 14:48:16 -070095 @Override
96 public void onChanged() {
97 onDataSetChanged();
98 }
99
100 @Override
101 public void onItemRangeChanged(int positionStart, int itemCount) {
102 onDataSetChanged();
103 }
104
105 @Override
106 public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
107 onDataSetChanged();
108 }
109
110 @Override
111 public void onItemRangeInserted(int positionStart, int itemCount) {
112 onDataSetChanged();
113 }
114
115 @Override
116 public void onItemRangeRemoved(int positionStart, int itemCount) {
117 onDataSetChanged();
118 }
119
120 @Override
121 public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
122 onDataSetChanged();
123 }
124 };
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700125
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700126 private ViewGroup mPinnedHeaderFrameLayout;
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900127 private ViewGroup mButtonBar;
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700128
Jason Monk39b46742015-09-10 15:52:51 -0400129 private LayoutPreference mHeader;
130
Jason Monk39b46742015-09-10 15:52:51 -0400131 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -0500132 private LinearLayoutManager mLayoutManager;
Jason Monk2071eda2016-02-25 13:55:48 -0500133 private ArrayMap<String, Preference> mPreferenceCache;
Jason Monkf38fb382016-03-18 14:23:01 -0400134 private boolean mAnimationAllowed;
Jason Monk39b46742015-09-10 15:52:51 -0400135
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700136 @VisibleForTesting
137 public HighlightablePreferenceGroupAdapter mAdapter;
138 @VisibleForTesting
139 public boolean mPreferenceHighlighted = false;
140
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700141 @Override
142 public void onCreate(Bundle icicle) {
143 super.onCreate(icicle);
144
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700145 if (icicle != null) {
146 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
147 }
148
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700149 // Prepare help url and enable menu if necessary
Anna Galusza0285c802016-01-29 17:32:19 -0800150 Bundle arguments = getArguments();
151 int helpResource;
152 if (arguments != null && arguments.containsKey(HELP_URI_RESOURCE_KEY)) {
153 helpResource = arguments.getInt(HELP_URI_RESOURCE_KEY);
154 } else {
155 helpResource = getHelpResource();
156 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700157 if (helpResource != 0) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400158 mHelpUri = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700159 }
160 }
161
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700162 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700163 public View onCreateView(LayoutInflater inflater, ViewGroup container,
164 Bundle savedInstanceState) {
165 final View root = super.onCreateView(inflater, container, savedInstanceState);
166 mPinnedHeaderFrameLayout = (ViewGroup) root.findViewById(R.id.pinned_header);
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900167 mButtonBar = (ViewGroup) root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700168 return root;
169 }
170
Jason Monk39b46742015-09-10 15:52:51 -0400171 @Override
Jason Monk91e2f892016-02-23 15:31:09 -0500172 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
173 super.addPreferencesFromResource(preferencesResId);
174 checkAvailablePrefs(getPreferenceScreen());
175 }
176
177 private void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
178 if (preferenceGroup == null) return;
179 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
180 Preference pref = preferenceGroup.getPreference(i);
181 if (pref instanceof SelfAvailablePreference
182 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
183 preferenceGroup.removePreference(pref);
184 } else if (pref instanceof PreferenceGroup) {
185 checkAvailablePrefs((PreferenceGroup) pref);
186 }
187 }
188 }
189
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900190 public ViewGroup getButtonBar() {
191 return mButtonBar;
192 }
193
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700194 public View setPinnedHeaderView(int layoutResId) {
195 final LayoutInflater inflater = getActivity().getLayoutInflater();
196 final View pinnedHeader =
197 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
198 setPinnedHeaderView(pinnedHeader);
199 return pinnedHeader;
200 }
201
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700202 public void setPinnedHeaderView(View pinnedHeader) {
203 mPinnedHeaderFrameLayout.addView(pinnedHeader);
204 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
205 }
206
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700207 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700208 public void onSaveInstanceState(Bundle outState) {
209 super.onSaveInstanceState(outState);
210
211 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
212 }
213
214 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700215 public void onActivityCreated(Bundle savedInstanceState) {
216 super.onActivityCreated(savedInstanceState);
Johan Redestig76218e52016-04-19 08:29:30 +0200217 setHasOptionsMenu(true);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700218 }
219
220 @Override
221 public void onResume() {
222 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700223
224 final Bundle args = getArguments();
225 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700226 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
227 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700228 }
229 }
230
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700231 @Override
232 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700233 registerObserverIfNeeded();
234 }
235
236 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700237 protected void onUnbindPreferences() {
238 unregisterObserverIfNeeded();
239 }
240
Jason Monkb5aa73f2015-03-31 12:59:33 -0400241 public void showLoadingWhenEmpty() {
242 View loading = getView().findViewById(R.id.loading_container);
Jason Monk39b46742015-09-10 15:52:51 -0400243 setEmptyView(loading);
Jason Monkb5aa73f2015-03-31 12:59:33 -0400244 }
245
Jason Monkb37e2882016-01-11 14:27:20 -0500246 public void setLoading(boolean loading, boolean animate) {
247 View loading_container = getView().findViewById(R.id.loading_container);
248 Utils.handleLoadingContainer(loading_container, getListView(), !loading, animate);
249 }
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
324 private void updateEmptyView() {
325 if (mEmptyView == null) return;
326 if (getPreferenceScreen() != null) {
327 boolean show = (getPreferenceScreen().getPreferenceCount()
328 - (mHeader != null ? 1 : 0)
Fan Zhangd5b48452016-12-13 12:42:50 -0800329 - (mFooterPreferenceMixin.hasFooter() ? 1 : 0)) <= 0;
Jason Monk39b46742015-09-10 15:52:51 -0400330 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
331 } else {
332 mEmptyView.setVisibility(View.VISIBLE);
333 }
334 }
335
336 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000337 if (mEmptyView != null) {
338 mEmptyView.setVisibility(View.GONE);
339 }
Jason Monk39b46742015-09-10 15:52:51 -0400340 mEmptyView = v;
341 updateEmptyView();
342 }
343
344 public View getEmptyView() {
345 return mEmptyView;
346 }
347
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700348 /**
349 * Return a valid ListView position or -1 if none is found
350 */
351 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400352 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700353 return -1;
354 }
355
Jason Monk39b46742015-09-10 15:52:51 -0400356 RecyclerView listView = getListView();
357 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700358
359 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400360 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700361 }
362
363 return -1;
364 }
365
Jason Monk65bb0972015-12-17 10:39:44 -0500366 @Override
367 public RecyclerView.LayoutManager onCreateLayoutManager() {
368 mLayoutManager = new LinearLayoutManager(getContext());
369 return mLayoutManager;
370 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700371
Jason Monk65bb0972015-12-17 10:39:44 -0500372 @Override
373 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
374 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
375 return mAdapter;
376 }
377
Jason Monkf38fb382016-03-18 14:23:01 -0400378 protected void setAnimationAllowed(boolean animationAllowed) {
379 mAnimationAllowed = animationAllowed;
380 }
381
Jason Monk2071eda2016-02-25 13:55:48 -0500382 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
383 mPreferenceCache = new ArrayMap<String, Preference>();
384 final int N = group.getPreferenceCount();
385 for (int i = 0; i < N; i++) {
386 Preference p = group.getPreference(i);
387 if (TextUtils.isEmpty(p.getKey())) {
388 continue;
389 }
390 mPreferenceCache.put(p.getKey(), p);
391 }
392 }
393
394 protected Preference getCachedPreference(String key) {
395 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
396 }
397
398 protected void removeCachedPrefs(PreferenceGroup group) {
399 for (Preference p : mPreferenceCache.values()) {
400 group.removePreference(p);
401 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400402 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500403 }
404
Jason Monka6278442016-04-21 10:12:30 -0400405 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400406 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400407 }
408
Jason Monk65bb0972015-12-17 10:39:44 -0500409 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700410 final int position = canUseListViewForHighLighting(key);
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700411 if (position < 0) {
412 return;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700413 }
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700414
415 mPreferenceHighlighted = true;
416 mLayoutManager.scrollToPosition(position);
417 mAdapter.highlight(position);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700418 }
419
Jason Monk39b46742015-09-10 15:52:51 -0400420 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
421 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700422 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400423 final Preference preference = adapter.getItem(n);
424 final String preferenceKey = preference.getKey();
425 if (preferenceKey != null && preferenceKey.equals(key)) {
426 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700427 }
428 }
429 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700430 }
431
Fan Zhange84407f2017-05-24 11:19:52 -0700432 protected boolean removePreference(String key) {
433 return removePreference(getPreferenceScreen(), key);
434 }
435
436 @VisibleForTesting
437 boolean removePreference(PreferenceGroup group, String key) {
438 final int preferenceCount = group.getPreferenceCount();
439 for (int i = 0; i < preferenceCount; i++) {
440 final Preference preference = group.getPreference(i);
441 final String curKey = preference.getKey();
442
443 if (TextUtils.equals(curKey, key)) {
444 return group.removePreference(preference);
445 }
446
447 if (preference instanceof PreferenceGroup) {
448 if (removePreference((PreferenceGroup) preference, key)) {
449 return true;
450 }
451 }
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700452 }
Fan Zhange84407f2017-05-24 11:19:52 -0700453 return false;
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700454 }
455
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700456 /**
457 * Override this if you want to show a help item in the menu, by returning the resource id.
458 * @return the resource id for the help url
459 */
460 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400461 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700462 }
463
464 @Override
465 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Daniel Nishie6740f72017-04-14 14:12:35 -0700466 super.onCreateOptionsMenu(menu, inflater);
Jason Monk23acc2b2015-04-14 15:06:39 -0400467 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400468 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700469 }
470 }
471
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700472 /*
473 * The name is intentionally made different from Activity#finish(), so that
474 * users won't misunderstand its meaning.
475 */
476 public final void finishFragment() {
477 getActivity().onBackPressed();
478 }
479
Amith Yamasanid7993472010-08-18 13:59:28 -0700480 // Some helpers for functions used by the settings fragments when they were activities
481
482 /**
483 * Returns the ContentResolver from the owning Activity.
484 */
485 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700486 Context context = getActivity();
487 if (context != null) {
488 mContentResolver = context.getContentResolver();
489 }
490 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700491 }
492
493 /**
494 * Returns the specified system service from the owning Activity.
495 */
496 protected Object getSystemService(final String name) {
497 return getActivity().getSystemService(name);
498 }
499
500 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700501 * Returns the PackageManager from the owning Activity.
502 */
503 protected PackageManager getPackageManager() {
504 return getActivity().getPackageManager();
505 }
506
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800507 @Override
508 public void onDetach() {
509 if (isRemoving()) {
510 if (mDialogFragment != null) {
511 mDialogFragment.dismiss();
512 mDialogFragment = null;
513 }
514 }
515 super.onDetach();
516 }
517
Amith Yamasanid7993472010-08-18 13:59:28 -0700518 // Dialog management
519
520 protected void showDialog(int dialogId) {
521 if (mDialogFragment != null) {
522 Log.e(TAG, "Old dialog fragment not null!");
523 }
524 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800525 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700526 }
527
Fan Zhangd65184f2016-09-19 17:45:24 -0700528 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700529 public Dialog onCreateDialog(int dialogId) {
530 return null;
531 }
532
Fan Zhangd65184f2016-09-19 17:45:24 -0700533 @Override
534 public int getDialogMetricsCategory(int dialogId) {
535 return 0;
536 }
537
Amith Yamasanid7993472010-08-18 13:59:28 -0700538 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800539 // mDialogFragment may not be visible yet in parent fragment's onResume().
540 // To be able to dismiss dialog at that time, don't check
541 // mDialogFragment.isVisible().
542 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400543 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700544 }
545 mDialogFragment = null;
546 }
547
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800548 /**
549 * Sets the OnCancelListener of the dialog shown. This method can only be
550 * called after showDialog(int) and before removeDialog(int). The method
551 * does nothing otherwise.
552 */
553 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
554 if (mDialogFragment != null) {
555 mDialogFragment.mOnCancelListener = listener;
556 }
557 }
558
559 /**
560 * Sets the OnDismissListener of the dialog shown. This method can only be
561 * called after showDialog(int) and before removeDialog(int). The method
562 * does nothing otherwise.
563 */
564 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
565 if (mDialogFragment != null) {
566 mDialogFragment.mOnDismissListener = listener;
567 }
568 }
569
Amith Yamasanic861cf82012-10-02 14:51:46 -0700570 public void onDialogShowing() {
571 // override in subclass to attach a dismiss listener, for instance
572 }
573
Jason Monk39b46742015-09-10 15:52:51 -0400574 @Override
575 public void onDisplayPreferenceDialog(Preference preference) {
576 if (preference.getKey() == null) {
577 // Auto-key preferences that don't have a key, so the dialog can find them.
578 preference.setKey(UUID.randomUUID().toString());
579 }
580 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000581 if (preference instanceof RestrictedListPreference) {
582 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
583 .newInstance(preference.getKey());
584 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400585 f = CustomListPreference.CustomListPreferenceDialogFragment
586 .newInstance(preference.getKey());
587 } else if (preference instanceof CustomDialogPreference) {
588 f = CustomDialogPreference.CustomPreferenceDialogFragment
589 .newInstance(preference.getKey());
590 } else if (preference instanceof CustomEditTextPreference) {
591 f = CustomEditTextPreference.CustomPreferenceDialogFragment
592 .newInstance(preference.getKey());
593 } else {
594 super.onDisplayPreferenceDialog(preference);
595 return;
596 }
597 f.setTargetFragment(this, 0);
598 f.show(getFragmentManager(), "dialog_preference");
599 onDialogShowing();
600 }
601
Fan Zhangd65184f2016-09-19 17:45:24 -0700602 public static class SettingsDialogFragment extends InstrumentedDialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800603 private static final String KEY_DIALOG_ID = "key_dialog_id";
604 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
605
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800606 private Fragment mParentFragment;
607
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800608 private DialogInterface.OnCancelListener mOnCancelListener;
609 private DialogInterface.OnDismissListener mOnDismissListener;
610
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800611 public SettingsDialogFragment() {
612 /* do nothing */
613 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700614
Amith Yamasani43c69782010-12-01 09:04:36 -0800615 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Fan Zhangd65184f2016-09-19 17:45:24 -0700616 super(fragment, dialogId);
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800617 if (!(fragment instanceof Fragment)) {
618 throw new IllegalArgumentException("fragment argument must be an instance of "
619 + Fragment.class.getName());
620 }
621 mParentFragment = (Fragment) fragment;
622 }
623
Fan Zhangd65184f2016-09-19 17:45:24 -0700624
625 @Override
626 public int getMetricsCategory() {
Fan Zhang4fe7c082016-10-03 13:48:55 -0700627 if (mDialogCreatable == null) {
628 return Instrumentable.METRICS_CATEGORY_UNKNOWN;
629 }
Fan Zhangd65184f2016-09-19 17:45:24 -0700630 final int metricsCategory = mDialogCreatable.getDialogMetricsCategory(mDialogId);
631 if (metricsCategory <= 0) {
632 throw new IllegalStateException("Dialog must provide a metrics category");
633 }
634 return metricsCategory;
635 }
636
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800637 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800638 public void onSaveInstanceState(Bundle outState) {
639 super.onSaveInstanceState(outState);
640 if (mParentFragment != null) {
641 outState.putInt(KEY_DIALOG_ID, mDialogId);
642 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
643 }
644 }
645
646 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700647 public void onStart() {
648 super.onStart();
649
650 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
651 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
652 }
653 }
654
655 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800656 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800657 if (savedInstanceState != null) {
658 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800659 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800660 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700661 if (mParentFragment == null) {
662 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
663 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800664 if (!(mParentFragment instanceof DialogCreatable)) {
665 throw new IllegalArgumentException(
666 (mParentFragment != null
667 ? mParentFragment.getClass().getName()
668 : mParentFragmentId)
669 + " must implement "
670 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800671 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800672 // This dialog fragment could be created from non-SettingsPreferenceFragment
673 if (mParentFragment instanceof SettingsPreferenceFragment) {
674 // restore mDialogFragment in mParentFragment
675 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
676 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800677 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800678 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700679 }
680
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800681 @Override
682 public void onCancel(DialogInterface dialog) {
683 super.onCancel(dialog);
684 if (mOnCancelListener != null) {
685 mOnCancelListener.onCancel(dialog);
686 }
687 }
688
689 @Override
690 public void onDismiss(DialogInterface dialog) {
691 super.onDismiss(dialog);
692 if (mOnDismissListener != null) {
693 mOnDismissListener.onDismiss(dialog);
694 }
695 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800696
Amith Yamasanid7993472010-08-18 13:59:28 -0700697 public int getDialogId() {
698 return mDialogId;
699 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800700
701 @Override
702 public void onDetach() {
703 super.onDetach();
704
Amith Yamasani8875ede2011-01-31 12:46:57 -0800705 // This dialog fragment could be created from non-SettingsPreferenceFragment
706 if (mParentFragment instanceof SettingsPreferenceFragment) {
707 // in case the dialog is not explicitly removed by removeDialog()
708 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
709 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
710 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800711 }
712 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700713 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700714
715 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800716 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700717 }
718
719 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800720 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700721 }
722
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700723 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700724 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400725 if (activity == null) return;
726 if (getFragmentManager().getBackStackEntryCount() > 0) {
727 getFragmentManager().popBackStack();
728 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800729 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700730 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700731 }
732
Jason Monkb7e43802016-06-06 16:01:58 -0400733 protected Intent getIntent() {
734 if (getActivity() == null) {
735 return null;
736 }
737 return getActivity().getIntent();
738 }
739
740 protected void setResult(int result, Intent intent) {
741 if (getActivity() == null) {
742 return;
743 }
744 getActivity().setResult(result, intent);
745 }
746
747 protected void setResult(int result) {
748 if (getActivity() == null) {
749 return;
750 }
751 getActivity().setResult(result);
752 }
753
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700754 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
755 int requestCode, Bundle extras) {
756 final Activity activity = getActivity();
757 if (activity instanceof SettingsActivity) {
758 SettingsActivity sa = (SettingsActivity) activity;
Fan Zhangc6ca3142017-02-14 15:02:35 -0800759 sa.startPreferencePanel(
760 caller, fragmentClass, extras, titleRes, null, caller, requestCode);
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700761 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700762 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700763 Log.w(TAG,
764 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
765 + "launch the given Fragment (name: " + fragmentClass
766 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700767 return false;
768 }
769 }
Jason Monk65bb0972015-12-17 10:39:44 -0500770
771 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
772
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700773 @VisibleForTesting(otherwise=VisibleForTesting.NONE)
774 int initialHighlightedPosition = -1;
775
Jason Monk65bb0972015-12-17 10:39:44 -0500776 private int mHighlightPosition = -1;
777
778 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
779 super(preferenceGroup);
780 }
781
782 public void highlight(int position) {
783 mHighlightPosition = position;
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700784 initialHighlightedPosition = position;
Jason Monk65bb0972015-12-17 10:39:44 -0500785 notifyDataSetChanged();
786 }
787
788 @Override
789 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
790 super.onBindViewHolder(holder, position);
791 if (position == mHighlightPosition) {
792 View v = holder.itemView;
Qi Dingc4772632016-09-18 17:03:47 +0800793 v.post(() -> {
794 if (v.getBackground() != null) {
795 final int centerX = v.getWidth() / 2;
796 final int centerY = v.getHeight() / 2;
797 v.getBackground().setHotspot(centerX, centerY);
798 }
799 v.setPressed(true);
800 v.setPressed(false);
801 mHighlightPosition = -1;
802 });
Jason Monk65bb0972015-12-17 10:39:44 -0500803 }
804 }
805 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700806}