blob: 5f42579775687efda1eb6d9edec4cf44bf76a439 [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;
Jason Monk91e2f892016-02-23 15:31:09 -050029import android.support.annotation.XmlRes;
Jason Monk39b46742015-09-10 15:52:51 -040030import android.support.v7.preference.Preference;
Jason Monk65bb0972015-12-17 10:39:44 -050031import android.support.v7.preference.PreferenceGroup;
Jason Monk39b46742015-09-10 15:52:51 -040032import android.support.v7.preference.PreferenceGroupAdapter;
33import android.support.v7.preference.PreferenceScreen;
Jason Monk65bb0972015-12-17 10:39:44 -050034import android.support.v7.preference.PreferenceViewHolder;
35import android.support.v7.widget.LinearLayoutManager;
Jason Monk39b46742015-09-10 15:52:51 -040036import android.support.v7.widget.RecyclerView;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070037import android.text.TextUtils;
Jason Monk2071eda2016-02-25 13:55:48 -050038import android.util.ArrayMap;
Amith Yamasanid7993472010-08-18 13:59:28 -070039import android.util.Log;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070040import android.view.LayoutInflater;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070041import android.view.Menu;
42import android.view.MenuInflater;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070043import android.view.View;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070044import android.view.ViewGroup;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070045import android.widget.Button;
Jason Monkb7e43802016-06-06 16:01:58 -040046
Jason Monk39b46742015-09-10 15:52:51 -040047import com.android.settings.applications.LayoutPreference;
Fan Zhang2d0b3442016-12-05 17:02:33 -080048import com.android.settings.core.InstrumentedPreferenceFragment;
Fan Zhang4fe7c082016-10-03 13:48:55 -070049import com.android.settings.core.instrumentation.Instrumentable;
Fan Zhangd65184f2016-09-19 17:45:24 -070050import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
Suprabh Shuklab84720c2016-04-05 14:37:20 -070051import com.android.settingslib.HelpUtils;
John Spurlockb8e02b82015-04-15 21:15:55 -040052
Jason Monk39b46742015-09-10 15:52:51 -040053import java.util.UUID;
54
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070055/**
Amith Yamasanid7993472010-08-18 13:59:28 -070056 * Base class for Settings fragments, with some helper functions and dialog management.
57 */
Fan Zhang2d0b3442016-12-05 17:02:33 -080058public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceFragment
Chris Wren8a963ba2015-03-20 10:29:14 -040059 implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070060
Anna Galusza0285c802016-01-29 17:32:19 -080061 /**
62 * The Help Uri Resource key. This can be passed as an extra argument when creating the
63 * Fragment.
64 **/
65 public static final String HELP_URI_RESOURCE_KEY = "help_uri_resource";
66
Jason Monk65bb0972015-12-17 10:39:44 -050067 private static final String TAG = "SettingsPreference";
Amith Yamasanid7993472010-08-18 13:59:28 -070068
Fabrice Di Meglioeced7802014-09-04 13:01:55 -070069 private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070070
71 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070072
Amith Yamasanid7993472010-08-18 13:59:28 -070073 private SettingsDialogFragment mDialogFragment;
74
Jason Monk23acc2b2015-04-14 15:06:39 -040075 private String mHelpUri;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070076
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000077 private static final int ORDER_FIRST = -1;
78 private static final int ORDER_LAST = Integer.MAX_VALUE -1;
79
Amith Yamasani350938e2013-04-09 10:22:47 -070080 // Cache the content resolver for async callbacks
81 private ContentResolver mContentResolver;
82
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070083 private String mPreferenceKey;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070084 private boolean mPreferenceHighlighted = false;
85
Jason Monk39b46742015-09-10 15:52:51 -040086 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070087 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040088 private RecyclerView.AdapterDataObserver mDataSetObserver =
89 new RecyclerView.AdapterDataObserver() {
Tony Mantler0b825f52016-09-27 14:48:16 -070090 @Override
91 public void onChanged() {
92 onDataSetChanged();
93 }
94
95 @Override
96 public void onItemRangeChanged(int positionStart, int itemCount) {
97 onDataSetChanged();
98 }
99
100 @Override
101 public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
102 onDataSetChanged();
103 }
104
105 @Override
106 public void onItemRangeInserted(int positionStart, int itemCount) {
107 onDataSetChanged();
108 }
109
110 @Override
111 public void onItemRangeRemoved(int positionStart, int itemCount) {
112 onDataSetChanged();
113 }
114
115 @Override
116 public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
117 onDataSetChanged();
118 }
119 };
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700120
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700121 private ViewGroup mPinnedHeaderFrameLayout;
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900122 private ViewGroup mButtonBar;
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700123
Jason Monk39b46742015-09-10 15:52:51 -0400124 private LayoutPreference mHeader;
125
126 private LayoutPreference mFooter;
127 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -0500128 private LinearLayoutManager mLayoutManager;
129 private HighlightablePreferenceGroupAdapter mAdapter;
Jason Monk2071eda2016-02-25 13:55:48 -0500130 private ArrayMap<String, Preference> mPreferenceCache;
Jason Monkf38fb382016-03-18 14:23:01 -0400131 private boolean mAnimationAllowed;
Jason Monk39b46742015-09-10 15:52:51 -0400132
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700133 @Override
134 public void onCreate(Bundle icicle) {
135 super.onCreate(icicle);
136
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700137 if (icicle != null) {
138 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
139 }
140
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700141 // Prepare help url and enable menu if necessary
Anna Galusza0285c802016-01-29 17:32:19 -0800142 Bundle arguments = getArguments();
143 int helpResource;
144 if (arguments != null && arguments.containsKey(HELP_URI_RESOURCE_KEY)) {
145 helpResource = arguments.getInt(HELP_URI_RESOURCE_KEY);
146 } else {
147 helpResource = getHelpResource();
148 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700149 if (helpResource != 0) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400150 mHelpUri = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700151 }
152 }
153
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700154 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700155 public View onCreateView(LayoutInflater inflater, ViewGroup container,
156 Bundle savedInstanceState) {
157 final View root = super.onCreateView(inflater, container, savedInstanceState);
158 mPinnedHeaderFrameLayout = (ViewGroup) root.findViewById(R.id.pinned_header);
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900159 mButtonBar = (ViewGroup) root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700160 return root;
161 }
162
Jason Monk39b46742015-09-10 15:52:51 -0400163 @Override
Jason Monk91e2f892016-02-23 15:31:09 -0500164 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
165 super.addPreferencesFromResource(preferencesResId);
166 checkAvailablePrefs(getPreferenceScreen());
167 }
168
169 private void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
170 if (preferenceGroup == null) return;
171 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
172 Preference pref = preferenceGroup.getPreference(i);
173 if (pref instanceof SelfAvailablePreference
174 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
175 preferenceGroup.removePreference(pref);
176 } else if (pref instanceof PreferenceGroup) {
177 checkAvailablePrefs((PreferenceGroup) pref);
178 }
179 }
180 }
181
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900182 public ViewGroup getButtonBar() {
183 return mButtonBar;
184 }
185
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700186 public View setPinnedHeaderView(int layoutResId) {
187 final LayoutInflater inflater = getActivity().getLayoutInflater();
188 final View pinnedHeader =
189 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
190 setPinnedHeaderView(pinnedHeader);
191 return pinnedHeader;
192 }
193
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700194 public void setPinnedHeaderView(View pinnedHeader) {
195 mPinnedHeaderFrameLayout.addView(pinnedHeader);
196 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
197 }
198
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700199 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700200 public void onSaveInstanceState(Bundle outState) {
201 super.onSaveInstanceState(outState);
202
203 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
204 }
205
206 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700207 public void onActivityCreated(Bundle savedInstanceState) {
208 super.onActivityCreated(savedInstanceState);
Johan Redestig76218e52016-04-19 08:29:30 +0200209 setHasOptionsMenu(true);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700210 }
211
212 @Override
213 public void onResume() {
214 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700215
216 final Bundle args = getArguments();
217 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700218 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
219 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700220 }
221 }
222
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700223 @Override
224 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700225 registerObserverIfNeeded();
226 }
227
228 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700229 protected void onUnbindPreferences() {
230 unregisterObserverIfNeeded();
231 }
232
Jason Monkb5aa73f2015-03-31 12:59:33 -0400233 public void showLoadingWhenEmpty() {
234 View loading = getView().findViewById(R.id.loading_container);
Jason Monk39b46742015-09-10 15:52:51 -0400235 setEmptyView(loading);
Jason Monkb5aa73f2015-03-31 12:59:33 -0400236 }
237
Jason Monkb37e2882016-01-11 14:27:20 -0500238 public void setLoading(boolean loading, boolean animate) {
239 View loading_container = getView().findViewById(R.id.loading_container);
240 Utils.handleLoadingContainer(loading_container, getListView(), !loading, animate);
241 }
242
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700243 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700244 if (!mIsDataSetObserverRegistered) {
245 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400246 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700247 }
Jason Monk39b46742015-09-10 15:52:51 -0400248 mCurrentRootAdapter = getListView().getAdapter();
249 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700250 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500251 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700252 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700253 }
254
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700255 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700256 if (mIsDataSetObserverRegistered) {
257 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400258 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700259 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700260 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700261 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700262 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700263 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700264
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700265 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700266 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700267 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700268 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700269 }
270
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000271 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400272 highlightPreferenceIfNeeded();
273 updateEmptyView();
274 }
275
Jason Monk39b46742015-09-10 15:52:51 -0400276 public LayoutPreference getHeaderView() {
277 return mHeader;
278 }
279
280 public LayoutPreference getFooterView() {
281 return mFooter;
282 }
283
284 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
301 protected void setFooterView(int resource) {
302 setFooterView(resource != 0 ? new LayoutPreference(getPrefContext(), resource) : null);
303 }
304
305 protected void setFooterView(View v) {
306 setFooterView(v != null ? new LayoutPreference(getPrefContext(), v) : null);
307 }
308
309 private void setFooterView(LayoutPreference footer) {
310 if (getPreferenceScreen() != null && mFooter != null) {
311 getPreferenceScreen().removePreference(mFooter);
312 }
313 if (footer != null) {
314 mFooter = footer;
Sudheer Shanka5590e2e2016-01-22 20:40:56 +0000315 mFooter.setOrder(ORDER_LAST);
Jason Monk39b46742015-09-10 15:52:51 -0400316 if (getPreferenceScreen() != null) {
317 getPreferenceScreen().addPreference(mFooter);
318 }
319 } else {
320 mFooter = null;
321 }
322 }
323
324 @Override
325 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400326 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400327 // Without ids generated, the RecyclerView won't animate changes to the preferences.
328 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
329 }
Jason Monk39b46742015-09-10 15:52:51 -0400330 super.setPreferenceScreen(preferenceScreen);
331 if (preferenceScreen != null) {
332 if (mHeader != null) {
333 preferenceScreen.addPreference(mHeader);
334 }
335 if (mFooter != null) {
336 preferenceScreen.addPreference(mFooter);
337 }
338 }
339 }
340
341 private void updateEmptyView() {
342 if (mEmptyView == null) return;
343 if (getPreferenceScreen() != null) {
344 boolean show = (getPreferenceScreen().getPreferenceCount()
345 - (mHeader != null ? 1 : 0)
346 - (mFooter != null ? 1 : 0)) <= 0;
347 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
348 } else {
349 mEmptyView.setVisibility(View.VISIBLE);
350 }
351 }
352
353 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000354 if (mEmptyView != null) {
355 mEmptyView.setVisibility(View.GONE);
356 }
Jason Monk39b46742015-09-10 15:52:51 -0400357 mEmptyView = v;
358 updateEmptyView();
359 }
360
361 public View getEmptyView() {
362 return mEmptyView;
363 }
364
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700365 /**
366 * Return a valid ListView position or -1 if none is found
367 */
368 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400369 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700370 return -1;
371 }
372
Jason Monk39b46742015-09-10 15:52:51 -0400373 RecyclerView listView = getListView();
374 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700375
376 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400377 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700378 }
379
380 return -1;
381 }
382
Jason Monk65bb0972015-12-17 10:39:44 -0500383 @Override
384 public RecyclerView.LayoutManager onCreateLayoutManager() {
385 mLayoutManager = new LinearLayoutManager(getContext());
386 return mLayoutManager;
387 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700388
Jason Monk65bb0972015-12-17 10:39:44 -0500389 @Override
390 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
391 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
392 return mAdapter;
393 }
394
Jason Monkf38fb382016-03-18 14:23:01 -0400395 protected void setAnimationAllowed(boolean animationAllowed) {
396 mAnimationAllowed = animationAllowed;
397 }
398
Jason Monk2071eda2016-02-25 13:55:48 -0500399 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
400 mPreferenceCache = new ArrayMap<String, Preference>();
401 final int N = group.getPreferenceCount();
402 for (int i = 0; i < N; i++) {
403 Preference p = group.getPreference(i);
404 if (TextUtils.isEmpty(p.getKey())) {
405 continue;
406 }
407 mPreferenceCache.put(p.getKey(), p);
408 }
409 }
410
411 protected Preference getCachedPreference(String key) {
412 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
413 }
414
415 protected void removeCachedPrefs(PreferenceGroup group) {
416 for (Preference p : mPreferenceCache.values()) {
417 group.removePreference(p);
418 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400419 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500420 }
421
Jason Monka6278442016-04-21 10:12:30 -0400422 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400423 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400424 }
425
Jason Monk65bb0972015-12-17 10:39:44 -0500426 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700427 final int position = canUseListViewForHighLighting(key);
428 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700429 mPreferenceHighlighted = true;
Jason Monk65bb0972015-12-17 10:39:44 -0500430 mLayoutManager.scrollToPosition(position);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700431
Jason Monk65bb0972015-12-17 10:39:44 -0500432 getView().postDelayed(new Runnable() {
433 @Override
434 public void run() {
435 mAdapter.highlight(position);
436 }
437 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700438 }
439 }
440
Jason Monk39b46742015-09-10 15:52:51 -0400441 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
442 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700443 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400444 final Preference preference = adapter.getItem(n);
445 final String preferenceKey = preference.getKey();
446 if (preferenceKey != null && preferenceKey.equals(key)) {
447 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700448 }
449 }
450 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700451 }
452
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700453 protected void removePreference(String key) {
454 Preference pref = findPreference(key);
455 if (pref != null) {
456 getPreferenceScreen().removePreference(pref);
457 }
458 }
459
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700460 /**
461 * Override this if you want to show a help item in the menu, by returning the resource id.
462 * @return the resource id for the help url
463 */
464 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400465 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700466 }
467
468 @Override
469 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400470 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400471 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700472 }
473 }
474
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700475 /*
476 * The name is intentionally made different from Activity#finish(), so that
477 * users won't misunderstand its meaning.
478 */
479 public final void finishFragment() {
480 getActivity().onBackPressed();
481 }
482
Amith Yamasanid7993472010-08-18 13:59:28 -0700483 // Some helpers for functions used by the settings fragments when they were activities
484
485 /**
486 * Returns the ContentResolver from the owning Activity.
487 */
488 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700489 Context context = getActivity();
490 if (context != null) {
491 mContentResolver = context.getContentResolver();
492 }
493 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700494 }
495
496 /**
497 * Returns the specified system service from the owning Activity.
498 */
499 protected Object getSystemService(final String name) {
500 return getActivity().getSystemService(name);
501 }
502
503 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700504 * Returns the PackageManager from the owning Activity.
505 */
506 protected PackageManager getPackageManager() {
507 return getActivity().getPackageManager();
508 }
509
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800510 @Override
511 public void onDetach() {
512 if (isRemoving()) {
513 if (mDialogFragment != null) {
514 mDialogFragment.dismiss();
515 mDialogFragment = null;
516 }
517 }
518 super.onDetach();
519 }
520
Amith Yamasanid7993472010-08-18 13:59:28 -0700521 // Dialog management
522
523 protected void showDialog(int dialogId) {
524 if (mDialogFragment != null) {
525 Log.e(TAG, "Old dialog fragment not null!");
526 }
527 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800528 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700529 }
530
Fan Zhangd65184f2016-09-19 17:45:24 -0700531 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700532 public Dialog onCreateDialog(int dialogId) {
533 return null;
534 }
535
Fan Zhangd65184f2016-09-19 17:45:24 -0700536 @Override
537 public int getDialogMetricsCategory(int dialogId) {
538 return 0;
539 }
540
Amith Yamasanid7993472010-08-18 13:59:28 -0700541 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800542 // mDialogFragment may not be visible yet in parent fragment's onResume().
543 // To be able to dismiss dialog at that time, don't check
544 // mDialogFragment.isVisible().
545 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400546 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700547 }
548 mDialogFragment = null;
549 }
550
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800551 /**
552 * Sets the OnCancelListener of the dialog shown. This method can only be
553 * called after showDialog(int) and before removeDialog(int). The method
554 * does nothing otherwise.
555 */
556 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
557 if (mDialogFragment != null) {
558 mDialogFragment.mOnCancelListener = listener;
559 }
560 }
561
562 /**
563 * Sets the OnDismissListener of the dialog shown. This method can only be
564 * called after showDialog(int) and before removeDialog(int). The method
565 * does nothing otherwise.
566 */
567 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
568 if (mDialogFragment != null) {
569 mDialogFragment.mOnDismissListener = listener;
570 }
571 }
572
Amith Yamasanic861cf82012-10-02 14:51:46 -0700573 public void onDialogShowing() {
574 // override in subclass to attach a dismiss listener, for instance
575 }
576
Jason Monk39b46742015-09-10 15:52:51 -0400577 @Override
578 public void onDisplayPreferenceDialog(Preference preference) {
579 if (preference.getKey() == null) {
580 // Auto-key preferences that don't have a key, so the dialog can find them.
581 preference.setKey(UUID.randomUUID().toString());
582 }
583 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000584 if (preference instanceof RestrictedListPreference) {
585 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
586 .newInstance(preference.getKey());
587 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400588 f = CustomListPreference.CustomListPreferenceDialogFragment
589 .newInstance(preference.getKey());
590 } else if (preference instanceof CustomDialogPreference) {
591 f = CustomDialogPreference.CustomPreferenceDialogFragment
592 .newInstance(preference.getKey());
593 } else if (preference instanceof CustomEditTextPreference) {
594 f = CustomEditTextPreference.CustomPreferenceDialogFragment
595 .newInstance(preference.getKey());
596 } else {
597 super.onDisplayPreferenceDialog(preference);
598 return;
599 }
600 f.setTargetFragment(this, 0);
601 f.show(getFragmentManager(), "dialog_preference");
602 onDialogShowing();
603 }
604
Fan Zhangd65184f2016-09-19 17:45:24 -0700605 public static class SettingsDialogFragment extends InstrumentedDialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800606 private static final String KEY_DIALOG_ID = "key_dialog_id";
607 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
608
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800609 private Fragment mParentFragment;
610
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800611 private DialogInterface.OnCancelListener mOnCancelListener;
612 private DialogInterface.OnDismissListener mOnDismissListener;
613
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800614 public SettingsDialogFragment() {
615 /* do nothing */
616 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700617
Amith Yamasani43c69782010-12-01 09:04:36 -0800618 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Fan Zhangd65184f2016-09-19 17:45:24 -0700619 super(fragment, dialogId);
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800620 if (!(fragment instanceof Fragment)) {
621 throw new IllegalArgumentException("fragment argument must be an instance of "
622 + Fragment.class.getName());
623 }
624 mParentFragment = (Fragment) fragment;
625 }
626
Fan Zhangd65184f2016-09-19 17:45:24 -0700627
628 @Override
629 public int getMetricsCategory() {
Fan Zhang4fe7c082016-10-03 13:48:55 -0700630 if (mDialogCreatable == null) {
631 return Instrumentable.METRICS_CATEGORY_UNKNOWN;
632 }
Fan Zhangd65184f2016-09-19 17:45:24 -0700633 final int metricsCategory = mDialogCreatable.getDialogMetricsCategory(mDialogId);
634 if (metricsCategory <= 0) {
635 throw new IllegalStateException("Dialog must provide a metrics category");
636 }
637 return metricsCategory;
638 }
639
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800640 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800641 public void onSaveInstanceState(Bundle outState) {
642 super.onSaveInstanceState(outState);
643 if (mParentFragment != null) {
644 outState.putInt(KEY_DIALOG_ID, mDialogId);
645 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
646 }
647 }
648
649 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700650 public void onStart() {
651 super.onStart();
652
653 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
654 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
655 }
656 }
657
658 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800659 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800660 if (savedInstanceState != null) {
661 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800662 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800663 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700664 if (mParentFragment == null) {
665 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
666 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800667 if (!(mParentFragment instanceof DialogCreatable)) {
668 throw new IllegalArgumentException(
669 (mParentFragment != null
670 ? mParentFragment.getClass().getName()
671 : mParentFragmentId)
672 + " must implement "
673 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800674 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800675 // This dialog fragment could be created from non-SettingsPreferenceFragment
676 if (mParentFragment instanceof SettingsPreferenceFragment) {
677 // restore mDialogFragment in mParentFragment
678 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
679 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800680 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800681 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700682 }
683
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800684 @Override
685 public void onCancel(DialogInterface dialog) {
686 super.onCancel(dialog);
687 if (mOnCancelListener != null) {
688 mOnCancelListener.onCancel(dialog);
689 }
690 }
691
692 @Override
693 public void onDismiss(DialogInterface dialog) {
694 super.onDismiss(dialog);
695 if (mOnDismissListener != null) {
696 mOnDismissListener.onDismiss(dialog);
697 }
698 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800699
Amith Yamasanid7993472010-08-18 13:59:28 -0700700 public int getDialogId() {
701 return mDialogId;
702 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800703
704 @Override
705 public void onDetach() {
706 super.onDetach();
707
Amith Yamasani8875ede2011-01-31 12:46:57 -0800708 // This dialog fragment could be created from non-SettingsPreferenceFragment
709 if (mParentFragment instanceof SettingsPreferenceFragment) {
710 // in case the dialog is not explicitly removed by removeDialog()
711 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
712 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
713 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800714 }
715 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700716 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700717
718 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800719 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700720 }
721
722 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800723 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700724 }
725
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700726 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700727 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400728 if (activity == null) return;
729 if (getFragmentManager().getBackStackEntryCount() > 0) {
730 getFragmentManager().popBackStack();
731 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800732 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700733 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700734 }
735
Jason Monkb7e43802016-06-06 16:01:58 -0400736 protected Intent getIntent() {
737 if (getActivity() == null) {
738 return null;
739 }
740 return getActivity().getIntent();
741 }
742
743 protected void setResult(int result, Intent intent) {
744 if (getActivity() == null) {
745 return;
746 }
747 getActivity().setResult(result, intent);
748 }
749
750 protected void setResult(int result) {
751 if (getActivity() == null) {
752 return;
753 }
754 getActivity().setResult(result);
755 }
756
Jason Monk39b46742015-09-10 15:52:51 -0400757 protected final Context getPrefContext() {
758 return getPreferenceManager().getContext();
759 }
760
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700761 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
762 int requestCode, Bundle extras) {
763 final Activity activity = getActivity();
764 if (activity instanceof SettingsActivity) {
765 SettingsActivity sa = (SettingsActivity) activity;
766 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
767 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700768 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700769 Log.w(TAG,
770 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
771 + "launch the given Fragment (name: " + fragmentClass
772 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700773 return false;
774 }
775 }
Jason Monk65bb0972015-12-17 10:39:44 -0500776
777 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
778
779 private int mHighlightPosition = -1;
780
781 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
782 super(preferenceGroup);
783 }
784
785 public void highlight(int position) {
786 mHighlightPosition = position;
787 notifyDataSetChanged();
788 }
789
790 @Override
791 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
792 super.onBindViewHolder(holder, position);
793 if (position == mHighlightPosition) {
794 View v = holder.itemView;
Qi Dingc4772632016-09-18 17:03:47 +0800795 v.post(() -> {
796 if (v.getBackground() != null) {
797 final int centerX = v.getWidth() / 2;
798 final int centerY = v.getHeight() / 2;
799 v.getBackground().setHotspot(centerX, centerY);
800 }
801 v.setPressed(true);
802 v.setPressed(false);
803 mHighlightPosition = -1;
804 });
Jason Monk65bb0972015-12-17 10:39:44 -0500805 }
806 }
807 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700808}