blob: 0c5bc063b1541ef4dce28d4927ff14d996aa2859 [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;
Suprabh Shuklab84720c2016-04-05 14:37:20 -070052import com.android.settingslib.HelpUtils;
Juan Lang777ed252017-05-09 15:42:36 -070053import com.android.settingslib.widget.FooterPreferenceMixin;
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
Fabrice Di Meglioeced7802014-09-04 13:01:55 -070071 private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070072
73 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070074
Fan Zhangd5b48452016-12-13 12:42:50 -080075 protected final FooterPreferenceMixin mFooterPreferenceMixin =
76 new FooterPreferenceMixin(this, getLifecycle());
77
Amith Yamasanid7993472010-08-18 13:59:28 -070078 private SettingsDialogFragment mDialogFragment;
79
Jason Monk23acc2b2015-04-14 15:06:39 -040080 private String mHelpUri;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070081
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000082 private static final int ORDER_FIRST = -1;
83 private static final int ORDER_LAST = Integer.MAX_VALUE -1;
84
Amith Yamasani350938e2013-04-09 10:22:47 -070085 // Cache the content resolver for async callbacks
86 private ContentResolver mContentResolver;
87
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070088 private String mPreferenceKey;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070089 private boolean mPreferenceHighlighted = false;
90
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;
133 private HighlightablePreferenceGroupAdapter mAdapter;
Jason Monk2071eda2016-02-25 13:55:48 -0500134 private ArrayMap<String, Preference> mPreferenceCache;
Jason Monkf38fb382016-03-18 14:23:01 -0400135 private boolean mAnimationAllowed;
Jason Monk39b46742015-09-10 15:52:51 -0400136
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700137 @Override
138 public void onCreate(Bundle icicle) {
139 super.onCreate(icicle);
140
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700141 if (icicle != null) {
142 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
143 }
144
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700145 // Prepare help url and enable menu if necessary
Anna Galusza0285c802016-01-29 17:32:19 -0800146 Bundle arguments = getArguments();
147 int helpResource;
148 if (arguments != null && arguments.containsKey(HELP_URI_RESOURCE_KEY)) {
149 helpResource = arguments.getInt(HELP_URI_RESOURCE_KEY);
150 } else {
151 helpResource = getHelpResource();
152 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700153 if (helpResource != 0) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400154 mHelpUri = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700155 }
156 }
157
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700158 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700159 public View onCreateView(LayoutInflater inflater, ViewGroup container,
160 Bundle savedInstanceState) {
161 final View root = super.onCreateView(inflater, container, savedInstanceState);
162 mPinnedHeaderFrameLayout = (ViewGroup) root.findViewById(R.id.pinned_header);
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900163 mButtonBar = (ViewGroup) root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700164 return root;
165 }
166
Jason Monk39b46742015-09-10 15:52:51 -0400167 @Override
Jason Monk91e2f892016-02-23 15:31:09 -0500168 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
169 super.addPreferencesFromResource(preferencesResId);
170 checkAvailablePrefs(getPreferenceScreen());
171 }
172
173 private void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
174 if (preferenceGroup == null) return;
175 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
176 Preference pref = preferenceGroup.getPreference(i);
177 if (pref instanceof SelfAvailablePreference
178 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
179 preferenceGroup.removePreference(pref);
180 } else if (pref instanceof PreferenceGroup) {
181 checkAvailablePrefs((PreferenceGroup) pref);
182 }
183 }
184 }
185
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900186 public ViewGroup getButtonBar() {
187 return mButtonBar;
188 }
189
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700190 public View setPinnedHeaderView(int layoutResId) {
191 final LayoutInflater inflater = getActivity().getLayoutInflater();
192 final View pinnedHeader =
193 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
194 setPinnedHeaderView(pinnedHeader);
195 return pinnedHeader;
196 }
197
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700198 public void setPinnedHeaderView(View pinnedHeader) {
199 mPinnedHeaderFrameLayout.addView(pinnedHeader);
200 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
201 }
202
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700203 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700204 public void onSaveInstanceState(Bundle outState) {
205 super.onSaveInstanceState(outState);
206
207 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
208 }
209
210 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700211 public void onActivityCreated(Bundle savedInstanceState) {
212 super.onActivityCreated(savedInstanceState);
Johan Redestig76218e52016-04-19 08:29:30 +0200213 setHasOptionsMenu(true);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700214 }
215
216 @Override
217 public void onResume() {
218 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700219
220 final Bundle args = getArguments();
221 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700222 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
223 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700224 }
225 }
226
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700227 @Override
228 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700229 registerObserverIfNeeded();
230 }
231
232 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700233 protected void onUnbindPreferences() {
234 unregisterObserverIfNeeded();
235 }
236
Jason Monkb5aa73f2015-03-31 12:59:33 -0400237 public void showLoadingWhenEmpty() {
238 View loading = getView().findViewById(R.id.loading_container);
Jason Monk39b46742015-09-10 15:52:51 -0400239 setEmptyView(loading);
Jason Monkb5aa73f2015-03-31 12:59:33 -0400240 }
241
Jason Monkb37e2882016-01-11 14:27:20 -0500242 public void setLoading(boolean loading, boolean animate) {
243 View loading_container = getView().findViewById(R.id.loading_container);
244 Utils.handleLoadingContainer(loading_container, getListView(), !loading, animate);
245 }
246
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700247 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700248 if (!mIsDataSetObserverRegistered) {
249 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400250 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700251 }
Jason Monk39b46742015-09-10 15:52:51 -0400252 mCurrentRootAdapter = getListView().getAdapter();
253 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700254 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500255 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700256 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700257 }
258
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700259 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700260 if (mIsDataSetObserverRegistered) {
261 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400262 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700263 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700264 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700265 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700266 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700267 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700268
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700269 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700270 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700271 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700272 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700273 }
274
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000275 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400276 highlightPreferenceIfNeeded();
277 updateEmptyView();
278 }
279
Jason Monk39b46742015-09-10 15:52:51 -0400280 public LayoutPreference getHeaderView() {
281 return mHeader;
282 }
283
Jason Monk39b46742015-09-10 15:52:51 -0400284 protected void setHeaderView(int resource) {
285 mHeader = new LayoutPreference(getPrefContext(), resource);
Udam Sainid553abc2016-02-16 17:54:13 -0800286 addPreferenceToTop(mHeader);
287 }
288
289 protected void setHeaderView(View view) {
290 mHeader = new LayoutPreference(getPrefContext(), view);
291 addPreferenceToTop(mHeader);
292 }
293
294 private void addPreferenceToTop(LayoutPreference preference) {
295 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400296 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800297 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400298 }
299 }
300
Jason Monk39b46742015-09-10 15:52:51 -0400301 @Override
302 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400303 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400304 // Without ids generated, the RecyclerView won't animate changes to the preferences.
305 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
306 }
Jason Monk39b46742015-09-10 15:52:51 -0400307 super.setPreferenceScreen(preferenceScreen);
308 if (preferenceScreen != null) {
309 if (mHeader != null) {
310 preferenceScreen.addPreference(mHeader);
311 }
Jason Monk39b46742015-09-10 15:52:51 -0400312 }
313 }
314
jackqdyulei2b2abac2017-05-26 10:47:55 -0700315 @VisibleForTesting
316 void updateEmptyView() {
Jason Monk39b46742015-09-10 15:52:51 -0400317 if (mEmptyView == null) return;
318 if (getPreferenceScreen() != null) {
jackqdyulei2b2abac2017-05-26 10:47:55 -0700319 final View listContainer = getActivity().findViewById(android.R.id.list_container);
Jason Monk39b46742015-09-10 15:52:51 -0400320 boolean show = (getPreferenceScreen().getPreferenceCount()
321 - (mHeader != null ? 1 : 0)
jackqdyulei2b2abac2017-05-26 10:47:55 -0700322 - (mFooterPreferenceMixin.hasFooter() ? 1 : 0)) <= 0
323 || (listContainer != null && listContainer.getVisibility() != View.VISIBLE);
Jason Monk39b46742015-09-10 15:52:51 -0400324 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
325 } else {
326 mEmptyView.setVisibility(View.VISIBLE);
327 }
328 }
329
330 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000331 if (mEmptyView != null) {
332 mEmptyView.setVisibility(View.GONE);
333 }
Jason Monk39b46742015-09-10 15:52:51 -0400334 mEmptyView = v;
335 updateEmptyView();
336 }
337
338 public View getEmptyView() {
339 return mEmptyView;
340 }
341
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700342 /**
343 * Return a valid ListView position or -1 if none is found
344 */
345 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400346 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700347 return -1;
348 }
349
Jason Monk39b46742015-09-10 15:52:51 -0400350 RecyclerView listView = getListView();
351 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700352
353 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400354 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700355 }
356
357 return -1;
358 }
359
Jason Monk65bb0972015-12-17 10:39:44 -0500360 @Override
361 public RecyclerView.LayoutManager onCreateLayoutManager() {
362 mLayoutManager = new LinearLayoutManager(getContext());
363 return mLayoutManager;
364 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700365
Jason Monk65bb0972015-12-17 10:39:44 -0500366 @Override
367 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
368 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
369 return mAdapter;
370 }
371
Jason Monkf38fb382016-03-18 14:23:01 -0400372 protected void setAnimationAllowed(boolean animationAllowed) {
373 mAnimationAllowed = animationAllowed;
374 }
375
Jason Monk2071eda2016-02-25 13:55:48 -0500376 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
377 mPreferenceCache = new ArrayMap<String, Preference>();
378 final int N = group.getPreferenceCount();
379 for (int i = 0; i < N; i++) {
380 Preference p = group.getPreference(i);
381 if (TextUtils.isEmpty(p.getKey())) {
382 continue;
383 }
384 mPreferenceCache.put(p.getKey(), p);
385 }
386 }
387
388 protected Preference getCachedPreference(String key) {
389 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
390 }
391
392 protected void removeCachedPrefs(PreferenceGroup group) {
393 for (Preference p : mPreferenceCache.values()) {
394 group.removePreference(p);
395 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400396 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500397 }
398
Jason Monka6278442016-04-21 10:12:30 -0400399 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400400 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400401 }
402
Jason Monk65bb0972015-12-17 10:39:44 -0500403 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700404 final int position = canUseListViewForHighLighting(key);
405 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700406 mPreferenceHighlighted = true;
Jason Monk65bb0972015-12-17 10:39:44 -0500407 mLayoutManager.scrollToPosition(position);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700408
Jason Monk65bb0972015-12-17 10:39:44 -0500409 getView().postDelayed(new Runnable() {
410 @Override
411 public void run() {
412 mAdapter.highlight(position);
413 }
414 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700415 }
416 }
417
Jason Monk39b46742015-09-10 15:52:51 -0400418 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
419 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700420 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400421 final Preference preference = adapter.getItem(n);
422 final String preferenceKey = preference.getKey();
423 if (preferenceKey != null && preferenceKey.equals(key)) {
424 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700425 }
426 }
427 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700428 }
429
Fan Zhange84407f2017-05-24 11:19:52 -0700430 protected boolean removePreference(String key) {
431 return removePreference(getPreferenceScreen(), key);
432 }
433
434 @VisibleForTesting
435 boolean removePreference(PreferenceGroup group, String key) {
436 final int preferenceCount = group.getPreferenceCount();
437 for (int i = 0; i < preferenceCount; i++) {
438 final Preference preference = group.getPreference(i);
439 final String curKey = preference.getKey();
440
441 if (TextUtils.equals(curKey, key)) {
442 return group.removePreference(preference);
443 }
444
445 if (preference instanceof PreferenceGroup) {
446 if (removePreference((PreferenceGroup) preference, key)) {
447 return true;
448 }
449 }
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700450 }
Fan Zhange84407f2017-05-24 11:19:52 -0700451 return false;
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700452 }
453
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700454 /**
455 * Override this if you want to show a help item in the menu, by returning the resource id.
456 * @return the resource id for the help url
457 */
458 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400459 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700460 }
461
462 @Override
463 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Daniel Nishie6740f72017-04-14 14:12:35 -0700464 super.onCreateOptionsMenu(menu, inflater);
Jason Monk23acc2b2015-04-14 15:06:39 -0400465 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400466 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700467 }
468 }
469
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700470 /*
471 * The name is intentionally made different from Activity#finish(), so that
472 * users won't misunderstand its meaning.
473 */
474 public final void finishFragment() {
475 getActivity().onBackPressed();
476 }
477
Amith Yamasanid7993472010-08-18 13:59:28 -0700478 // Some helpers for functions used by the settings fragments when they were activities
479
480 /**
481 * Returns the ContentResolver from the owning Activity.
482 */
483 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700484 Context context = getActivity();
485 if (context != null) {
486 mContentResolver = context.getContentResolver();
487 }
488 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700489 }
490
491 /**
492 * Returns the specified system service from the owning Activity.
493 */
494 protected Object getSystemService(final String name) {
495 return getActivity().getSystemService(name);
496 }
497
498 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700499 * Returns the PackageManager from the owning Activity.
500 */
501 protected PackageManager getPackageManager() {
502 return getActivity().getPackageManager();
503 }
504
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800505 @Override
506 public void onDetach() {
507 if (isRemoving()) {
508 if (mDialogFragment != null) {
509 mDialogFragment.dismiss();
510 mDialogFragment = null;
511 }
512 }
513 super.onDetach();
514 }
515
Amith Yamasanid7993472010-08-18 13:59:28 -0700516 // Dialog management
517
518 protected void showDialog(int dialogId) {
519 if (mDialogFragment != null) {
520 Log.e(TAG, "Old dialog fragment not null!");
521 }
522 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800523 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700524 }
525
Fan Zhangd65184f2016-09-19 17:45:24 -0700526 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700527 public Dialog onCreateDialog(int dialogId) {
528 return null;
529 }
530
Fan Zhangd65184f2016-09-19 17:45:24 -0700531 @Override
532 public int getDialogMetricsCategory(int dialogId) {
533 return 0;
534 }
535
Amith Yamasanid7993472010-08-18 13:59:28 -0700536 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800537 // mDialogFragment may not be visible yet in parent fragment's onResume().
538 // To be able to dismiss dialog at that time, don't check
539 // mDialogFragment.isVisible().
540 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400541 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700542 }
543 mDialogFragment = null;
544 }
545
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800546 /**
547 * Sets the OnCancelListener of the dialog shown. This method can only be
548 * called after showDialog(int) and before removeDialog(int). The method
549 * does nothing otherwise.
550 */
551 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
552 if (mDialogFragment != null) {
553 mDialogFragment.mOnCancelListener = listener;
554 }
555 }
556
557 /**
558 * Sets the OnDismissListener of the dialog shown. This method can only be
559 * called after showDialog(int) and before removeDialog(int). The method
560 * does nothing otherwise.
561 */
562 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
563 if (mDialogFragment != null) {
564 mDialogFragment.mOnDismissListener = listener;
565 }
566 }
567
Amith Yamasanic861cf82012-10-02 14:51:46 -0700568 public void onDialogShowing() {
569 // override in subclass to attach a dismiss listener, for instance
570 }
571
Jason Monk39b46742015-09-10 15:52:51 -0400572 @Override
573 public void onDisplayPreferenceDialog(Preference preference) {
574 if (preference.getKey() == null) {
575 // Auto-key preferences that don't have a key, so the dialog can find them.
576 preference.setKey(UUID.randomUUID().toString());
577 }
578 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000579 if (preference instanceof RestrictedListPreference) {
580 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
581 .newInstance(preference.getKey());
582 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400583 f = CustomListPreference.CustomListPreferenceDialogFragment
584 .newInstance(preference.getKey());
585 } else if (preference instanceof CustomDialogPreference) {
586 f = CustomDialogPreference.CustomPreferenceDialogFragment
587 .newInstance(preference.getKey());
588 } else if (preference instanceof CustomEditTextPreference) {
589 f = CustomEditTextPreference.CustomPreferenceDialogFragment
590 .newInstance(preference.getKey());
591 } else {
592 super.onDisplayPreferenceDialog(preference);
593 return;
594 }
595 f.setTargetFragment(this, 0);
596 f.show(getFragmentManager(), "dialog_preference");
597 onDialogShowing();
598 }
599
Fan Zhangd65184f2016-09-19 17:45:24 -0700600 public static class SettingsDialogFragment extends InstrumentedDialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800601 private static final String KEY_DIALOG_ID = "key_dialog_id";
602 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
603
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800604 private Fragment mParentFragment;
605
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800606 private DialogInterface.OnCancelListener mOnCancelListener;
607 private DialogInterface.OnDismissListener mOnDismissListener;
608
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800609 public SettingsDialogFragment() {
610 /* do nothing */
611 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700612
Amith Yamasani43c69782010-12-01 09:04:36 -0800613 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Fan Zhangd65184f2016-09-19 17:45:24 -0700614 super(fragment, dialogId);
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800615 if (!(fragment instanceof Fragment)) {
616 throw new IllegalArgumentException("fragment argument must be an instance of "
617 + Fragment.class.getName());
618 }
619 mParentFragment = (Fragment) fragment;
620 }
621
Fan Zhangd65184f2016-09-19 17:45:24 -0700622
623 @Override
624 public int getMetricsCategory() {
Fan Zhang4fe7c082016-10-03 13:48:55 -0700625 if (mDialogCreatable == null) {
626 return Instrumentable.METRICS_CATEGORY_UNKNOWN;
627 }
Fan Zhangd65184f2016-09-19 17:45:24 -0700628 final int metricsCategory = mDialogCreatable.getDialogMetricsCategory(mDialogId);
629 if (metricsCategory <= 0) {
630 throw new IllegalStateException("Dialog must provide a metrics category");
631 }
632 return metricsCategory;
633 }
634
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800635 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800636 public void onSaveInstanceState(Bundle outState) {
637 super.onSaveInstanceState(outState);
638 if (mParentFragment != null) {
639 outState.putInt(KEY_DIALOG_ID, mDialogId);
640 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
641 }
642 }
643
644 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700645 public void onStart() {
646 super.onStart();
647
648 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
649 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
650 }
651 }
652
653 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800654 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800655 if (savedInstanceState != null) {
656 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800657 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800658 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700659 if (mParentFragment == null) {
660 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
661 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800662 if (!(mParentFragment instanceof DialogCreatable)) {
663 throw new IllegalArgumentException(
664 (mParentFragment != null
665 ? mParentFragment.getClass().getName()
666 : mParentFragmentId)
667 + " must implement "
668 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800669 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800670 // This dialog fragment could be created from non-SettingsPreferenceFragment
671 if (mParentFragment instanceof SettingsPreferenceFragment) {
672 // restore mDialogFragment in mParentFragment
673 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
674 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800675 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800676 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700677 }
678
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800679 @Override
680 public void onCancel(DialogInterface dialog) {
681 super.onCancel(dialog);
682 if (mOnCancelListener != null) {
683 mOnCancelListener.onCancel(dialog);
684 }
685 }
686
687 @Override
688 public void onDismiss(DialogInterface dialog) {
689 super.onDismiss(dialog);
690 if (mOnDismissListener != null) {
691 mOnDismissListener.onDismiss(dialog);
692 }
693 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800694
Amith Yamasanid7993472010-08-18 13:59:28 -0700695 public int getDialogId() {
696 return mDialogId;
697 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800698
699 @Override
700 public void onDetach() {
701 super.onDetach();
702
Amith Yamasani8875ede2011-01-31 12:46:57 -0800703 // This dialog fragment could be created from non-SettingsPreferenceFragment
704 if (mParentFragment instanceof SettingsPreferenceFragment) {
705 // in case the dialog is not explicitly removed by removeDialog()
706 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
707 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
708 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800709 }
710 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700711 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700712
713 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800714 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700715 }
716
717 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800718 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700719 }
720
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700721 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700722 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400723 if (activity == null) return;
724 if (getFragmentManager().getBackStackEntryCount() > 0) {
725 getFragmentManager().popBackStack();
726 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800727 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700728 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700729 }
730
Jason Monkb7e43802016-06-06 16:01:58 -0400731 protected Intent getIntent() {
732 if (getActivity() == null) {
733 return null;
734 }
735 return getActivity().getIntent();
736 }
737
738 protected void setResult(int result, Intent intent) {
739 if (getActivity() == null) {
740 return;
741 }
742 getActivity().setResult(result, intent);
743 }
744
745 protected void setResult(int result) {
746 if (getActivity() == null) {
747 return;
748 }
749 getActivity().setResult(result);
750 }
751
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700752 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
753 int requestCode, Bundle extras) {
754 final Activity activity = getActivity();
755 if (activity instanceof SettingsActivity) {
756 SettingsActivity sa = (SettingsActivity) activity;
Fan Zhangc6ca3142017-02-14 15:02:35 -0800757 sa.startPreferencePanel(
758 caller, fragmentClass, extras, titleRes, null, caller, requestCode);
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700759 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700760 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700761 Log.w(TAG,
762 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
763 + "launch the given Fragment (name: " + fragmentClass
764 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700765 return false;
766 }
767 }
Jason Monk65bb0972015-12-17 10:39:44 -0500768
769 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
770
771 private int mHighlightPosition = -1;
772
773 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
774 super(preferenceGroup);
775 }
776
777 public void highlight(int position) {
778 mHighlightPosition = position;
779 notifyDataSetChanged();
780 }
781
782 @Override
783 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
784 super.onBindViewHolder(holder, position);
785 if (position == mHighlightPosition) {
786 View v = holder.itemView;
Qi Dingc4772632016-09-18 17:03:47 +0800787 v.post(() -> {
788 if (v.getBackground() != null) {
789 final int centerX = v.getWidth() / 2;
790 final int centerY = v.getHeight() / 2;
791 v.getBackground().setHotspot(centerX, centerY);
792 }
793 v.setPressed(true);
794 v.setPressed(false);
795 mHighlightPosition = -1;
796 });
Jason Monk65bb0972015-12-17 10:39:44 -0500797 }
798 }
799 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700800}