blob: b4ca6a6572e6881a5ba46e5cb36d2b4fb514a835 [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
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
315 private void updateEmptyView() {
316 if (mEmptyView == null) return;
317 if (getPreferenceScreen() != null) {
318 boolean show = (getPreferenceScreen().getPreferenceCount()
319 - (mHeader != null ? 1 : 0)
Fan Zhangd5b48452016-12-13 12:42:50 -0800320 - (mFooterPreferenceMixin.hasFooter() ? 1 : 0)) <= 0;
Jason Monk39b46742015-09-10 15:52:51 -0400321 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
322 } else {
323 mEmptyView.setVisibility(View.VISIBLE);
324 }
325 }
326
327 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000328 if (mEmptyView != null) {
329 mEmptyView.setVisibility(View.GONE);
330 }
Jason Monk39b46742015-09-10 15:52:51 -0400331 mEmptyView = v;
332 updateEmptyView();
333 }
334
335 public View getEmptyView() {
336 return mEmptyView;
337 }
338
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700339 /**
340 * Return a valid ListView position or -1 if none is found
341 */
342 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400343 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700344 return -1;
345 }
346
Jason Monk39b46742015-09-10 15:52:51 -0400347 RecyclerView listView = getListView();
348 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700349
350 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400351 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700352 }
353
354 return -1;
355 }
356
Jason Monk65bb0972015-12-17 10:39:44 -0500357 @Override
358 public RecyclerView.LayoutManager onCreateLayoutManager() {
359 mLayoutManager = new LinearLayoutManager(getContext());
360 return mLayoutManager;
361 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700362
Jason Monk65bb0972015-12-17 10:39:44 -0500363 @Override
364 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
365 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
366 return mAdapter;
367 }
368
Jason Monkf38fb382016-03-18 14:23:01 -0400369 protected void setAnimationAllowed(boolean animationAllowed) {
370 mAnimationAllowed = animationAllowed;
371 }
372
Jason Monk2071eda2016-02-25 13:55:48 -0500373 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
374 mPreferenceCache = new ArrayMap<String, Preference>();
375 final int N = group.getPreferenceCount();
376 for (int i = 0; i < N; i++) {
377 Preference p = group.getPreference(i);
378 if (TextUtils.isEmpty(p.getKey())) {
379 continue;
380 }
381 mPreferenceCache.put(p.getKey(), p);
382 }
383 }
384
385 protected Preference getCachedPreference(String key) {
386 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
387 }
388
389 protected void removeCachedPrefs(PreferenceGroup group) {
390 for (Preference p : mPreferenceCache.values()) {
391 group.removePreference(p);
392 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400393 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500394 }
395
Jason Monka6278442016-04-21 10:12:30 -0400396 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400397 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400398 }
399
Jason Monk65bb0972015-12-17 10:39:44 -0500400 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700401 final int position = canUseListViewForHighLighting(key);
402 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700403 mPreferenceHighlighted = true;
Jason Monk65bb0972015-12-17 10:39:44 -0500404 mLayoutManager.scrollToPosition(position);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700405
Jason Monk65bb0972015-12-17 10:39:44 -0500406 getView().postDelayed(new Runnable() {
407 @Override
408 public void run() {
409 mAdapter.highlight(position);
410 }
411 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700412 }
413 }
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
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700451 /**
452 * Override this if you want to show a help item in the menu, by returning the resource id.
453 * @return the resource id for the help url
454 */
455 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400456 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700457 }
458
459 @Override
460 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Daniel Nishie6740f72017-04-14 14:12:35 -0700461 super.onCreateOptionsMenu(menu, inflater);
Jason Monk23acc2b2015-04-14 15:06:39 -0400462 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400463 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700464 }
465 }
466
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700467 /*
468 * The name is intentionally made different from Activity#finish(), so that
469 * users won't misunderstand its meaning.
470 */
471 public final void finishFragment() {
472 getActivity().onBackPressed();
473 }
474
Amith Yamasanid7993472010-08-18 13:59:28 -0700475 // Some helpers for functions used by the settings fragments when they were activities
476
477 /**
478 * Returns the ContentResolver from the owning Activity.
479 */
480 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700481 Context context = getActivity();
482 if (context != null) {
483 mContentResolver = context.getContentResolver();
484 }
485 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700486 }
487
488 /**
489 * Returns the specified system service from the owning Activity.
490 */
491 protected Object getSystemService(final String name) {
492 return getActivity().getSystemService(name);
493 }
494
495 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700496 * Returns the PackageManager from the owning Activity.
497 */
498 protected PackageManager getPackageManager() {
499 return getActivity().getPackageManager();
500 }
501
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800502 @Override
503 public void onDetach() {
504 if (isRemoving()) {
505 if (mDialogFragment != null) {
506 mDialogFragment.dismiss();
507 mDialogFragment = null;
508 }
509 }
510 super.onDetach();
511 }
512
Amith Yamasanid7993472010-08-18 13:59:28 -0700513 // Dialog management
514
515 protected void showDialog(int dialogId) {
516 if (mDialogFragment != null) {
517 Log.e(TAG, "Old dialog fragment not null!");
518 }
519 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800520 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700521 }
522
Fan Zhangd65184f2016-09-19 17:45:24 -0700523 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700524 public Dialog onCreateDialog(int dialogId) {
525 return null;
526 }
527
Fan Zhangd65184f2016-09-19 17:45:24 -0700528 @Override
529 public int getDialogMetricsCategory(int dialogId) {
530 return 0;
531 }
532
Amith Yamasanid7993472010-08-18 13:59:28 -0700533 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800534 // mDialogFragment may not be visible yet in parent fragment's onResume().
535 // To be able to dismiss dialog at that time, don't check
536 // mDialogFragment.isVisible().
537 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400538 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700539 }
540 mDialogFragment = null;
541 }
542
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800543 /**
544 * Sets the OnCancelListener of the dialog shown. This method can only be
545 * called after showDialog(int) and before removeDialog(int). The method
546 * does nothing otherwise.
547 */
548 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
549 if (mDialogFragment != null) {
550 mDialogFragment.mOnCancelListener = listener;
551 }
552 }
553
554 /**
555 * Sets the OnDismissListener of the dialog shown. This method can only be
556 * called after showDialog(int) and before removeDialog(int). The method
557 * does nothing otherwise.
558 */
559 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
560 if (mDialogFragment != null) {
561 mDialogFragment.mOnDismissListener = listener;
562 }
563 }
564
Amith Yamasanic861cf82012-10-02 14:51:46 -0700565 public void onDialogShowing() {
566 // override in subclass to attach a dismiss listener, for instance
567 }
568
Jason Monk39b46742015-09-10 15:52:51 -0400569 @Override
570 public void onDisplayPreferenceDialog(Preference preference) {
571 if (preference.getKey() == null) {
572 // Auto-key preferences that don't have a key, so the dialog can find them.
573 preference.setKey(UUID.randomUUID().toString());
574 }
575 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000576 if (preference instanceof RestrictedListPreference) {
577 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
578 .newInstance(preference.getKey());
579 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400580 f = CustomListPreference.CustomListPreferenceDialogFragment
581 .newInstance(preference.getKey());
582 } else if (preference instanceof CustomDialogPreference) {
583 f = CustomDialogPreference.CustomPreferenceDialogFragment
584 .newInstance(preference.getKey());
585 } else if (preference instanceof CustomEditTextPreference) {
586 f = CustomEditTextPreference.CustomPreferenceDialogFragment
587 .newInstance(preference.getKey());
588 } else {
589 super.onDisplayPreferenceDialog(preference);
590 return;
591 }
592 f.setTargetFragment(this, 0);
593 f.show(getFragmentManager(), "dialog_preference");
594 onDialogShowing();
595 }
596
Fan Zhangd65184f2016-09-19 17:45:24 -0700597 public static class SettingsDialogFragment extends InstrumentedDialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800598 private static final String KEY_DIALOG_ID = "key_dialog_id";
599 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
600
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800601 private Fragment mParentFragment;
602
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800603 private DialogInterface.OnCancelListener mOnCancelListener;
604 private DialogInterface.OnDismissListener mOnDismissListener;
605
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800606 public SettingsDialogFragment() {
607 /* do nothing */
608 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700609
Amith Yamasani43c69782010-12-01 09:04:36 -0800610 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Fan Zhangd65184f2016-09-19 17:45:24 -0700611 super(fragment, dialogId);
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800612 if (!(fragment instanceof Fragment)) {
613 throw new IllegalArgumentException("fragment argument must be an instance of "
614 + Fragment.class.getName());
615 }
616 mParentFragment = (Fragment) fragment;
617 }
618
Fan Zhangd65184f2016-09-19 17:45:24 -0700619
620 @Override
621 public int getMetricsCategory() {
Fan Zhang4fe7c082016-10-03 13:48:55 -0700622 if (mDialogCreatable == null) {
623 return Instrumentable.METRICS_CATEGORY_UNKNOWN;
624 }
Fan Zhangd65184f2016-09-19 17:45:24 -0700625 final int metricsCategory = mDialogCreatable.getDialogMetricsCategory(mDialogId);
626 if (metricsCategory <= 0) {
627 throw new IllegalStateException("Dialog must provide a metrics category");
628 }
629 return metricsCategory;
630 }
631
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800632 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800633 public void onSaveInstanceState(Bundle outState) {
634 super.onSaveInstanceState(outState);
635 if (mParentFragment != null) {
636 outState.putInt(KEY_DIALOG_ID, mDialogId);
637 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
638 }
639 }
640
641 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700642 public void onStart() {
643 super.onStart();
644
645 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
646 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
647 }
648 }
649
650 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800651 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800652 if (savedInstanceState != null) {
653 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800654 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800655 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700656 if (mParentFragment == null) {
657 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
658 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800659 if (!(mParentFragment instanceof DialogCreatable)) {
660 throw new IllegalArgumentException(
661 (mParentFragment != null
662 ? mParentFragment.getClass().getName()
663 : mParentFragmentId)
664 + " must implement "
665 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800666 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800667 // This dialog fragment could be created from non-SettingsPreferenceFragment
668 if (mParentFragment instanceof SettingsPreferenceFragment) {
669 // restore mDialogFragment in mParentFragment
670 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
671 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800672 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800673 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700674 }
675
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800676 @Override
677 public void onCancel(DialogInterface dialog) {
678 super.onCancel(dialog);
679 if (mOnCancelListener != null) {
680 mOnCancelListener.onCancel(dialog);
681 }
682 }
683
684 @Override
685 public void onDismiss(DialogInterface dialog) {
686 super.onDismiss(dialog);
687 if (mOnDismissListener != null) {
688 mOnDismissListener.onDismiss(dialog);
689 }
690 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800691
Amith Yamasanid7993472010-08-18 13:59:28 -0700692 public int getDialogId() {
693 return mDialogId;
694 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800695
696 @Override
697 public void onDetach() {
698 super.onDetach();
699
Amith Yamasani8875ede2011-01-31 12:46:57 -0800700 // This dialog fragment could be created from non-SettingsPreferenceFragment
701 if (mParentFragment instanceof SettingsPreferenceFragment) {
702 // in case the dialog is not explicitly removed by removeDialog()
703 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
704 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
705 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800706 }
707 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700708 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700709
710 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800711 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700712 }
713
714 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800715 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700716 }
717
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700718 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700719 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400720 if (activity == null) return;
721 if (getFragmentManager().getBackStackEntryCount() > 0) {
722 getFragmentManager().popBackStack();
723 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800724 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700725 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700726 }
727
Jason Monkb7e43802016-06-06 16:01:58 -0400728 protected Intent getIntent() {
729 if (getActivity() == null) {
730 return null;
731 }
732 return getActivity().getIntent();
733 }
734
735 protected void setResult(int result, Intent intent) {
736 if (getActivity() == null) {
737 return;
738 }
739 getActivity().setResult(result, intent);
740 }
741
742 protected void setResult(int result) {
743 if (getActivity() == null) {
744 return;
745 }
746 getActivity().setResult(result);
747 }
748
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700749 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
750 int requestCode, Bundle extras) {
751 final Activity activity = getActivity();
752 if (activity instanceof SettingsActivity) {
753 SettingsActivity sa = (SettingsActivity) activity;
Fan Zhangc6ca3142017-02-14 15:02:35 -0800754 sa.startPreferencePanel(
755 caller, fragmentClass, extras, titleRes, null, caller, requestCode);
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700756 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700757 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700758 Log.w(TAG,
759 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
760 + "launch the given Fragment (name: " + fragmentClass
761 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700762 return false;
763 }
764 }
Jason Monk65bb0972015-12-17 10:39:44 -0500765
766 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
767
768 private int mHighlightPosition = -1;
769
770 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
771 super(preferenceGroup);
772 }
773
774 public void highlight(int position) {
775 mHighlightPosition = position;
776 notifyDataSetChanged();
777 }
778
779 @Override
780 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
781 super.onBindViewHolder(holder, position);
782 if (position == mHighlightPosition) {
783 View v = holder.itemView;
Qi Dingc4772632016-09-18 17:03:47 +0800784 v.post(() -> {
785 if (v.getBackground() != null) {
786 final int centerX = v.getWidth() / 2;
787 final int centerY = v.getHeight() / 2;
788 v.getBackground().setHotspot(centerX, centerY);
789 }
790 v.setPressed(true);
791 v.setPressed(false);
792 mHighlightPosition = -1;
793 });
Jason Monk65bb0972015-12-17 10:39:44 -0500794 }
795 }
796 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700797}