blob: ee500a97ec352d674045d45f59f7f5b3c5080245 [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;
Fan Zhangd5b48452016-12-13 12:42:50 -080051import com.android.settings.widget.FooterPreferenceMixin;
Suprabh Shuklab84720c2016-04-05 14:37:20 -070052import com.android.settingslib.HelpUtils;
John Spurlockb8e02b82015-04-15 21:15:55 -040053
Jason Monk39b46742015-09-10 15:52:51 -040054import java.util.UUID;
55
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070056/**
Amith Yamasanid7993472010-08-18 13:59:28 -070057 * Base class for Settings fragments, with some helper functions and dialog management.
58 */
Fan Zhang2d0b3442016-12-05 17:02:33 -080059public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceFragment
Chris Wren8a963ba2015-03-20 10:29:14 -040060 implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070061
Anna Galusza0285c802016-01-29 17:32:19 -080062 /**
63 * The Help Uri Resource key. This can be passed as an extra argument when creating the
64 * Fragment.
65 **/
66 public static final String HELP_URI_RESOURCE_KEY = "help_uri_resource";
67
Jason Monk65bb0972015-12-17 10:39:44 -050068 private static final String TAG = "SettingsPreference";
Amith Yamasanid7993472010-08-18 13:59:28 -070069
Fabrice Di Meglioeced7802014-09-04 13:01:55 -070070 private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070071
72 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070073
Fan Zhangd5b48452016-12-13 12:42:50 -080074 protected final FooterPreferenceMixin mFooterPreferenceMixin =
75 new FooterPreferenceMixin(this, getLifecycle());
76
Amith Yamasanid7993472010-08-18 13:59:28 -070077 private SettingsDialogFragment mDialogFragment;
78
Jason Monk23acc2b2015-04-14 15:06:39 -040079 private String mHelpUri;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070080
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000081 private static final int ORDER_FIRST = -1;
82 private static final int ORDER_LAST = Integer.MAX_VALUE -1;
83
Amith Yamasani350938e2013-04-09 10:22:47 -070084 // Cache the content resolver for async callbacks
85 private ContentResolver mContentResolver;
86
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070087 private String mPreferenceKey;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070088 private boolean mPreferenceHighlighted = false;
89
Jason Monk39b46742015-09-10 15:52:51 -040090 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070091 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040092 private RecyclerView.AdapterDataObserver mDataSetObserver =
93 new RecyclerView.AdapterDataObserver() {
Tony Mantler0b825f52016-09-27 14:48:16 -070094 @Override
95 public void onChanged() {
96 onDataSetChanged();
97 }
98
99 @Override
100 public void onItemRangeChanged(int positionStart, int itemCount) {
101 onDataSetChanged();
102 }
103
104 @Override
105 public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
106 onDataSetChanged();
107 }
108
109 @Override
110 public void onItemRangeInserted(int positionStart, int itemCount) {
111 onDataSetChanged();
112 }
113
114 @Override
115 public void onItemRangeRemoved(int positionStart, int itemCount) {
116 onDataSetChanged();
117 }
118
119 @Override
120 public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
121 onDataSetChanged();
122 }
123 };
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700124
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700125 private ViewGroup mPinnedHeaderFrameLayout;
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900126 private ViewGroup mButtonBar;
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700127
Jason Monk39b46742015-09-10 15:52:51 -0400128 private LayoutPreference mHeader;
129
Jason Monk39b46742015-09-10 15:52:51 -0400130 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -0500131 private LinearLayoutManager mLayoutManager;
132 private HighlightablePreferenceGroupAdapter mAdapter;
Jason Monk2071eda2016-02-25 13:55:48 -0500133 private ArrayMap<String, Preference> mPreferenceCache;
Jason Monkf38fb382016-03-18 14:23:01 -0400134 private boolean mAnimationAllowed;
Jason Monk39b46742015-09-10 15:52:51 -0400135
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700136 @Override
137 public void onCreate(Bundle icicle) {
138 super.onCreate(icicle);
139
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700140 if (icicle != null) {
141 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
142 }
143
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700144 // Prepare help url and enable menu if necessary
Anna Galusza0285c802016-01-29 17:32:19 -0800145 Bundle arguments = getArguments();
146 int helpResource;
147 if (arguments != null && arguments.containsKey(HELP_URI_RESOURCE_KEY)) {
148 helpResource = arguments.getInt(HELP_URI_RESOURCE_KEY);
149 } else {
150 helpResource = getHelpResource();
151 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700152 if (helpResource != 0) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400153 mHelpUri = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700154 }
155 }
156
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700157 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700158 public View onCreateView(LayoutInflater inflater, ViewGroup container,
159 Bundle savedInstanceState) {
160 final View root = super.onCreateView(inflater, container, savedInstanceState);
161 mPinnedHeaderFrameLayout = (ViewGroup) root.findViewById(R.id.pinned_header);
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900162 mButtonBar = (ViewGroup) root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700163 return root;
164 }
165
Jason Monk39b46742015-09-10 15:52:51 -0400166 @Override
Jason Monk91e2f892016-02-23 15:31:09 -0500167 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
168 super.addPreferencesFromResource(preferencesResId);
169 checkAvailablePrefs(getPreferenceScreen());
170 }
171
172 private void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
173 if (preferenceGroup == null) return;
174 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
175 Preference pref = preferenceGroup.getPreference(i);
176 if (pref instanceof SelfAvailablePreference
177 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
178 preferenceGroup.removePreference(pref);
179 } else if (pref instanceof PreferenceGroup) {
180 checkAvailablePrefs((PreferenceGroup) pref);
181 }
182 }
183 }
184
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900185 public ViewGroup getButtonBar() {
186 return mButtonBar;
187 }
188
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700189 public View setPinnedHeaderView(int layoutResId) {
190 final LayoutInflater inflater = getActivity().getLayoutInflater();
191 final View pinnedHeader =
192 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
193 setPinnedHeaderView(pinnedHeader);
194 return pinnedHeader;
195 }
196
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700197 public void setPinnedHeaderView(View pinnedHeader) {
198 mPinnedHeaderFrameLayout.addView(pinnedHeader);
199 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
200 }
201
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700202 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700203 public void onSaveInstanceState(Bundle outState) {
204 super.onSaveInstanceState(outState);
205
206 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
207 }
208
209 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700210 public void onActivityCreated(Bundle savedInstanceState) {
211 super.onActivityCreated(savedInstanceState);
Johan Redestig76218e52016-04-19 08:29:30 +0200212 setHasOptionsMenu(true);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700213 }
214
215 @Override
216 public void onResume() {
217 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700218
219 final Bundle args = getArguments();
220 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700221 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
222 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700223 }
224 }
225
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700226 @Override
227 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700228 registerObserverIfNeeded();
229 }
230
231 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700232 protected void onUnbindPreferences() {
233 unregisterObserverIfNeeded();
234 }
235
Jason Monkb5aa73f2015-03-31 12:59:33 -0400236 public void showLoadingWhenEmpty() {
237 View loading = getView().findViewById(R.id.loading_container);
Jason Monk39b46742015-09-10 15:52:51 -0400238 setEmptyView(loading);
Jason Monkb5aa73f2015-03-31 12:59:33 -0400239 }
240
Jason Monkb37e2882016-01-11 14:27:20 -0500241 public void setLoading(boolean loading, boolean animate) {
242 View loading_container = getView().findViewById(R.id.loading_container);
243 Utils.handleLoadingContainer(loading_container, getListView(), !loading, animate);
244 }
245
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700246 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700247 if (!mIsDataSetObserverRegistered) {
248 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400249 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700250 }
Jason Monk39b46742015-09-10 15:52:51 -0400251 mCurrentRootAdapter = getListView().getAdapter();
252 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700253 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500254 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700255 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700256 }
257
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700258 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700259 if (mIsDataSetObserverRegistered) {
260 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400261 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700262 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700263 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700264 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700265 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700266 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700267
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700268 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700269 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700270 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700271 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700272 }
273
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000274 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400275 highlightPreferenceIfNeeded();
276 updateEmptyView();
277 }
278
Jason Monk39b46742015-09-10 15:52:51 -0400279 public LayoutPreference getHeaderView() {
280 return mHeader;
281 }
282
Jason Monk39b46742015-09-10 15:52:51 -0400283 protected void setHeaderView(int resource) {
284 mHeader = new LayoutPreference(getPrefContext(), resource);
Udam Sainid553abc2016-02-16 17:54:13 -0800285 addPreferenceToTop(mHeader);
286 }
287
288 protected void setHeaderView(View view) {
289 mHeader = new LayoutPreference(getPrefContext(), view);
290 addPreferenceToTop(mHeader);
291 }
292
293 private void addPreferenceToTop(LayoutPreference preference) {
294 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400295 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800296 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400297 }
298 }
299
Jason Monk39b46742015-09-10 15:52:51 -0400300 @Override
301 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400302 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400303 // Without ids generated, the RecyclerView won't animate changes to the preferences.
304 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
305 }
Jason Monk39b46742015-09-10 15:52:51 -0400306 super.setPreferenceScreen(preferenceScreen);
307 if (preferenceScreen != null) {
308 if (mHeader != null) {
309 preferenceScreen.addPreference(mHeader);
310 }
Jason Monk39b46742015-09-10 15:52:51 -0400311 }
312 }
313
314 private void updateEmptyView() {
315 if (mEmptyView == null) return;
316 if (getPreferenceScreen() != null) {
317 boolean show = (getPreferenceScreen().getPreferenceCount()
318 - (mHeader != null ? 1 : 0)
Fan Zhangd5b48452016-12-13 12:42:50 -0800319 - (mFooterPreferenceMixin.hasFooter() ? 1 : 0)) <= 0;
Jason Monk39b46742015-09-10 15:52:51 -0400320 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
321 } else {
322 mEmptyView.setVisibility(View.VISIBLE);
323 }
324 }
325
326 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000327 if (mEmptyView != null) {
328 mEmptyView.setVisibility(View.GONE);
329 }
Jason Monk39b46742015-09-10 15:52:51 -0400330 mEmptyView = v;
331 updateEmptyView();
332 }
333
334 public View getEmptyView() {
335 return mEmptyView;
336 }
337
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700338 /**
339 * Return a valid ListView position or -1 if none is found
340 */
341 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400342 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700343 return -1;
344 }
345
Jason Monk39b46742015-09-10 15:52:51 -0400346 RecyclerView listView = getListView();
347 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700348
349 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400350 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700351 }
352
353 return -1;
354 }
355
Jason Monk65bb0972015-12-17 10:39:44 -0500356 @Override
357 public RecyclerView.LayoutManager onCreateLayoutManager() {
358 mLayoutManager = new LinearLayoutManager(getContext());
359 return mLayoutManager;
360 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700361
Jason Monk65bb0972015-12-17 10:39:44 -0500362 @Override
363 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
364 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
365 return mAdapter;
366 }
367
Jason Monkf38fb382016-03-18 14:23:01 -0400368 protected void setAnimationAllowed(boolean animationAllowed) {
369 mAnimationAllowed = animationAllowed;
370 }
371
Jason Monk2071eda2016-02-25 13:55:48 -0500372 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
373 mPreferenceCache = new ArrayMap<String, Preference>();
374 final int N = group.getPreferenceCount();
375 for (int i = 0; i < N; i++) {
376 Preference p = group.getPreference(i);
377 if (TextUtils.isEmpty(p.getKey())) {
378 continue;
379 }
380 mPreferenceCache.put(p.getKey(), p);
381 }
382 }
383
384 protected Preference getCachedPreference(String key) {
385 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
386 }
387
388 protected void removeCachedPrefs(PreferenceGroup group) {
389 for (Preference p : mPreferenceCache.values()) {
390 group.removePreference(p);
391 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400392 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500393 }
394
Jason Monka6278442016-04-21 10:12:30 -0400395 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400396 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400397 }
398
Jason Monk65bb0972015-12-17 10:39:44 -0500399 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700400 final int position = canUseListViewForHighLighting(key);
401 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700402 mPreferenceHighlighted = true;
Jason Monk65bb0972015-12-17 10:39:44 -0500403 mLayoutManager.scrollToPosition(position);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700404
Jason Monk65bb0972015-12-17 10:39:44 -0500405 getView().postDelayed(new Runnable() {
406 @Override
407 public void run() {
408 mAdapter.highlight(position);
409 }
410 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700411 }
412 }
413
Jason Monk39b46742015-09-10 15:52:51 -0400414 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
415 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700416 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400417 final Preference preference = adapter.getItem(n);
418 final String preferenceKey = preference.getKey();
419 if (preferenceKey != null && preferenceKey.equals(key)) {
420 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700421 }
422 }
423 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700424 }
425
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700426 protected void removePreference(String key) {
427 Preference pref = findPreference(key);
428 if (pref != null) {
429 getPreferenceScreen().removePreference(pref);
430 }
431 }
432
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700433 /**
434 * Override this if you want to show a help item in the menu, by returning the resource id.
435 * @return the resource id for the help url
436 */
437 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400438 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700439 }
440
441 @Override
442 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400443 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400444 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700445 }
446 }
447
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700448 /*
449 * The name is intentionally made different from Activity#finish(), so that
450 * users won't misunderstand its meaning.
451 */
452 public final void finishFragment() {
453 getActivity().onBackPressed();
454 }
455
Amith Yamasanid7993472010-08-18 13:59:28 -0700456 // Some helpers for functions used by the settings fragments when they were activities
457
458 /**
459 * Returns the ContentResolver from the owning Activity.
460 */
461 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700462 Context context = getActivity();
463 if (context != null) {
464 mContentResolver = context.getContentResolver();
465 }
466 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700467 }
468
469 /**
470 * Returns the specified system service from the owning Activity.
471 */
472 protected Object getSystemService(final String name) {
473 return getActivity().getSystemService(name);
474 }
475
476 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700477 * Returns the PackageManager from the owning Activity.
478 */
479 protected PackageManager getPackageManager() {
480 return getActivity().getPackageManager();
481 }
482
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800483 @Override
484 public void onDetach() {
485 if (isRemoving()) {
486 if (mDialogFragment != null) {
487 mDialogFragment.dismiss();
488 mDialogFragment = null;
489 }
490 }
491 super.onDetach();
492 }
493
Amith Yamasanid7993472010-08-18 13:59:28 -0700494 // Dialog management
495
496 protected void showDialog(int dialogId) {
497 if (mDialogFragment != null) {
498 Log.e(TAG, "Old dialog fragment not null!");
499 }
500 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800501 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700502 }
503
Fan Zhangd65184f2016-09-19 17:45:24 -0700504 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700505 public Dialog onCreateDialog(int dialogId) {
506 return null;
507 }
508
Fan Zhangd65184f2016-09-19 17:45:24 -0700509 @Override
510 public int getDialogMetricsCategory(int dialogId) {
511 return 0;
512 }
513
Amith Yamasanid7993472010-08-18 13:59:28 -0700514 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800515 // mDialogFragment may not be visible yet in parent fragment's onResume().
516 // To be able to dismiss dialog at that time, don't check
517 // mDialogFragment.isVisible().
518 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400519 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700520 }
521 mDialogFragment = null;
522 }
523
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800524 /**
525 * Sets the OnCancelListener of the dialog shown. This method can only be
526 * called after showDialog(int) and before removeDialog(int). The method
527 * does nothing otherwise.
528 */
529 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
530 if (mDialogFragment != null) {
531 mDialogFragment.mOnCancelListener = listener;
532 }
533 }
534
535 /**
536 * Sets the OnDismissListener of the dialog shown. This method can only be
537 * called after showDialog(int) and before removeDialog(int). The method
538 * does nothing otherwise.
539 */
540 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
541 if (mDialogFragment != null) {
542 mDialogFragment.mOnDismissListener = listener;
543 }
544 }
545
Amith Yamasanic861cf82012-10-02 14:51:46 -0700546 public void onDialogShowing() {
547 // override in subclass to attach a dismiss listener, for instance
548 }
549
Jason Monk39b46742015-09-10 15:52:51 -0400550 @Override
551 public void onDisplayPreferenceDialog(Preference preference) {
552 if (preference.getKey() == null) {
553 // Auto-key preferences that don't have a key, so the dialog can find them.
554 preference.setKey(UUID.randomUUID().toString());
555 }
556 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000557 if (preference instanceof RestrictedListPreference) {
558 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
559 .newInstance(preference.getKey());
560 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400561 f = CustomListPreference.CustomListPreferenceDialogFragment
562 .newInstance(preference.getKey());
563 } else if (preference instanceof CustomDialogPreference) {
564 f = CustomDialogPreference.CustomPreferenceDialogFragment
565 .newInstance(preference.getKey());
566 } else if (preference instanceof CustomEditTextPreference) {
567 f = CustomEditTextPreference.CustomPreferenceDialogFragment
568 .newInstance(preference.getKey());
569 } else {
570 super.onDisplayPreferenceDialog(preference);
571 return;
572 }
573 f.setTargetFragment(this, 0);
574 f.show(getFragmentManager(), "dialog_preference");
575 onDialogShowing();
576 }
577
Fan Zhangd65184f2016-09-19 17:45:24 -0700578 public static class SettingsDialogFragment extends InstrumentedDialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800579 private static final String KEY_DIALOG_ID = "key_dialog_id";
580 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
581
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800582 private Fragment mParentFragment;
583
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800584 private DialogInterface.OnCancelListener mOnCancelListener;
585 private DialogInterface.OnDismissListener mOnDismissListener;
586
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800587 public SettingsDialogFragment() {
588 /* do nothing */
589 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700590
Amith Yamasani43c69782010-12-01 09:04:36 -0800591 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Fan Zhangd65184f2016-09-19 17:45:24 -0700592 super(fragment, dialogId);
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800593 if (!(fragment instanceof Fragment)) {
594 throw new IllegalArgumentException("fragment argument must be an instance of "
595 + Fragment.class.getName());
596 }
597 mParentFragment = (Fragment) fragment;
598 }
599
Fan Zhangd65184f2016-09-19 17:45:24 -0700600
601 @Override
602 public int getMetricsCategory() {
Fan Zhang4fe7c082016-10-03 13:48:55 -0700603 if (mDialogCreatable == null) {
604 return Instrumentable.METRICS_CATEGORY_UNKNOWN;
605 }
Fan Zhangd65184f2016-09-19 17:45:24 -0700606 final int metricsCategory = mDialogCreatable.getDialogMetricsCategory(mDialogId);
607 if (metricsCategory <= 0) {
608 throw new IllegalStateException("Dialog must provide a metrics category");
609 }
610 return metricsCategory;
611 }
612
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800613 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800614 public void onSaveInstanceState(Bundle outState) {
615 super.onSaveInstanceState(outState);
616 if (mParentFragment != null) {
617 outState.putInt(KEY_DIALOG_ID, mDialogId);
618 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
619 }
620 }
621
622 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700623 public void onStart() {
624 super.onStart();
625
626 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
627 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
628 }
629 }
630
631 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800632 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800633 if (savedInstanceState != null) {
634 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800635 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800636 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700637 if (mParentFragment == null) {
638 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
639 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800640 if (!(mParentFragment instanceof DialogCreatable)) {
641 throw new IllegalArgumentException(
642 (mParentFragment != null
643 ? mParentFragment.getClass().getName()
644 : mParentFragmentId)
645 + " must implement "
646 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800647 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800648 // This dialog fragment could be created from non-SettingsPreferenceFragment
649 if (mParentFragment instanceof SettingsPreferenceFragment) {
650 // restore mDialogFragment in mParentFragment
651 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
652 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800653 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800654 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700655 }
656
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800657 @Override
658 public void onCancel(DialogInterface dialog) {
659 super.onCancel(dialog);
660 if (mOnCancelListener != null) {
661 mOnCancelListener.onCancel(dialog);
662 }
663 }
664
665 @Override
666 public void onDismiss(DialogInterface dialog) {
667 super.onDismiss(dialog);
668 if (mOnDismissListener != null) {
669 mOnDismissListener.onDismiss(dialog);
670 }
671 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800672
Amith Yamasanid7993472010-08-18 13:59:28 -0700673 public int getDialogId() {
674 return mDialogId;
675 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800676
677 @Override
678 public void onDetach() {
679 super.onDetach();
680
Amith Yamasani8875ede2011-01-31 12:46:57 -0800681 // This dialog fragment could be created from non-SettingsPreferenceFragment
682 if (mParentFragment instanceof SettingsPreferenceFragment) {
683 // in case the dialog is not explicitly removed by removeDialog()
684 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
685 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
686 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800687 }
688 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700689 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700690
691 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800692 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700693 }
694
695 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800696 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700697 }
698
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700699 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700700 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400701 if (activity == null) return;
702 if (getFragmentManager().getBackStackEntryCount() > 0) {
703 getFragmentManager().popBackStack();
704 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800705 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700706 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700707 }
708
Jason Monkb7e43802016-06-06 16:01:58 -0400709 protected Intent getIntent() {
710 if (getActivity() == null) {
711 return null;
712 }
713 return getActivity().getIntent();
714 }
715
716 protected void setResult(int result, Intent intent) {
717 if (getActivity() == null) {
718 return;
719 }
720 getActivity().setResult(result, intent);
721 }
722
723 protected void setResult(int result) {
724 if (getActivity() == null) {
725 return;
726 }
727 getActivity().setResult(result);
728 }
729
Jason Monk39b46742015-09-10 15:52:51 -0400730 protected final Context getPrefContext() {
731 return getPreferenceManager().getContext();
732 }
733
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700734 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
735 int requestCode, Bundle extras) {
736 final Activity activity = getActivity();
737 if (activity instanceof SettingsActivity) {
738 SettingsActivity sa = (SettingsActivity) activity;
739 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
740 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700741 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700742 Log.w(TAG,
743 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
744 + "launch the given Fragment (name: " + fragmentClass
745 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700746 return false;
747 }
748 }
Jason Monk65bb0972015-12-17 10:39:44 -0500749
750 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
751
752 private int mHighlightPosition = -1;
753
754 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
755 super(preferenceGroup);
756 }
757
758 public void highlight(int position) {
759 mHighlightPosition = position;
760 notifyDataSetChanged();
761 }
762
763 @Override
764 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
765 super.onBindViewHolder(holder, position);
766 if (position == mHighlightPosition) {
767 View v = holder.itemView;
Qi Dingc4772632016-09-18 17:03:47 +0800768 v.post(() -> {
769 if (v.getBackground() != null) {
770 final int centerX = v.getWidth() / 2;
771 final int centerY = v.getHeight() / 2;
772 v.getBackground().setHotspot(centerX, centerY);
773 }
774 v.setPressed(true);
775 v.setPressed(false);
776 mHighlightPosition = -1;
777 });
Jason Monk65bb0972015-12-17 10:39:44 -0500778 }
779 }
780 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700781}