blob: 3fc7964a05112e2b08ceadf52562c2c1a8874072 [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;
Juan Lang777ed252017-05-09 15:42:36 -070052import com.android.settingslib.widget.FooterPreferenceMixin;
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) {
Daniel Nishie6740f72017-04-14 14:12:35 -0700443 super.onCreateOptionsMenu(menu, inflater);
Jason Monk23acc2b2015-04-14 15:06:39 -0400444 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400445 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700446 }
447 }
448
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700449 /*
450 * The name is intentionally made different from Activity#finish(), so that
451 * users won't misunderstand its meaning.
452 */
453 public final void finishFragment() {
454 getActivity().onBackPressed();
455 }
456
Amith Yamasanid7993472010-08-18 13:59:28 -0700457 // Some helpers for functions used by the settings fragments when they were activities
458
459 /**
460 * Returns the ContentResolver from the owning Activity.
461 */
462 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700463 Context context = getActivity();
464 if (context != null) {
465 mContentResolver = context.getContentResolver();
466 }
467 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700468 }
469
470 /**
471 * Returns the specified system service from the owning Activity.
472 */
473 protected Object getSystemService(final String name) {
474 return getActivity().getSystemService(name);
475 }
476
477 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700478 * Returns the PackageManager from the owning Activity.
479 */
480 protected PackageManager getPackageManager() {
481 return getActivity().getPackageManager();
482 }
483
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800484 @Override
485 public void onDetach() {
486 if (isRemoving()) {
487 if (mDialogFragment != null) {
488 mDialogFragment.dismiss();
489 mDialogFragment = null;
490 }
491 }
492 super.onDetach();
493 }
494
Amith Yamasanid7993472010-08-18 13:59:28 -0700495 // Dialog management
496
497 protected void showDialog(int dialogId) {
498 if (mDialogFragment != null) {
499 Log.e(TAG, "Old dialog fragment not null!");
500 }
501 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800502 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700503 }
504
Fan Zhangd65184f2016-09-19 17:45:24 -0700505 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700506 public Dialog onCreateDialog(int dialogId) {
507 return null;
508 }
509
Fan Zhangd65184f2016-09-19 17:45:24 -0700510 @Override
511 public int getDialogMetricsCategory(int dialogId) {
512 return 0;
513 }
514
Amith Yamasanid7993472010-08-18 13:59:28 -0700515 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800516 // mDialogFragment may not be visible yet in parent fragment's onResume().
517 // To be able to dismiss dialog at that time, don't check
518 // mDialogFragment.isVisible().
519 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400520 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700521 }
522 mDialogFragment = null;
523 }
524
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800525 /**
526 * Sets the OnCancelListener of the dialog shown. This method can only be
527 * called after showDialog(int) and before removeDialog(int). The method
528 * does nothing otherwise.
529 */
530 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
531 if (mDialogFragment != null) {
532 mDialogFragment.mOnCancelListener = listener;
533 }
534 }
535
536 /**
537 * Sets the OnDismissListener of the dialog shown. This method can only be
538 * called after showDialog(int) and before removeDialog(int). The method
539 * does nothing otherwise.
540 */
541 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
542 if (mDialogFragment != null) {
543 mDialogFragment.mOnDismissListener = listener;
544 }
545 }
546
Amith Yamasanic861cf82012-10-02 14:51:46 -0700547 public void onDialogShowing() {
548 // override in subclass to attach a dismiss listener, for instance
549 }
550
Jason Monk39b46742015-09-10 15:52:51 -0400551 @Override
552 public void onDisplayPreferenceDialog(Preference preference) {
553 if (preference.getKey() == null) {
554 // Auto-key preferences that don't have a key, so the dialog can find them.
555 preference.setKey(UUID.randomUUID().toString());
556 }
557 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000558 if (preference instanceof RestrictedListPreference) {
559 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
560 .newInstance(preference.getKey());
561 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400562 f = CustomListPreference.CustomListPreferenceDialogFragment
563 .newInstance(preference.getKey());
564 } else if (preference instanceof CustomDialogPreference) {
565 f = CustomDialogPreference.CustomPreferenceDialogFragment
566 .newInstance(preference.getKey());
567 } else if (preference instanceof CustomEditTextPreference) {
568 f = CustomEditTextPreference.CustomPreferenceDialogFragment
569 .newInstance(preference.getKey());
570 } else {
571 super.onDisplayPreferenceDialog(preference);
572 return;
573 }
574 f.setTargetFragment(this, 0);
575 f.show(getFragmentManager(), "dialog_preference");
576 onDialogShowing();
577 }
578
Fan Zhangd65184f2016-09-19 17:45:24 -0700579 public static class SettingsDialogFragment extends InstrumentedDialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800580 private static final String KEY_DIALOG_ID = "key_dialog_id";
581 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
582
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800583 private Fragment mParentFragment;
584
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800585 private DialogInterface.OnCancelListener mOnCancelListener;
586 private DialogInterface.OnDismissListener mOnDismissListener;
587
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800588 public SettingsDialogFragment() {
589 /* do nothing */
590 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700591
Amith Yamasani43c69782010-12-01 09:04:36 -0800592 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Fan Zhangd65184f2016-09-19 17:45:24 -0700593 super(fragment, dialogId);
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800594 if (!(fragment instanceof Fragment)) {
595 throw new IllegalArgumentException("fragment argument must be an instance of "
596 + Fragment.class.getName());
597 }
598 mParentFragment = (Fragment) fragment;
599 }
600
Fan Zhangd65184f2016-09-19 17:45:24 -0700601
602 @Override
603 public int getMetricsCategory() {
Fan Zhang4fe7c082016-10-03 13:48:55 -0700604 if (mDialogCreatable == null) {
605 return Instrumentable.METRICS_CATEGORY_UNKNOWN;
606 }
Fan Zhangd65184f2016-09-19 17:45:24 -0700607 final int metricsCategory = mDialogCreatable.getDialogMetricsCategory(mDialogId);
608 if (metricsCategory <= 0) {
609 throw new IllegalStateException("Dialog must provide a metrics category");
610 }
611 return metricsCategory;
612 }
613
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800614 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800615 public void onSaveInstanceState(Bundle outState) {
616 super.onSaveInstanceState(outState);
617 if (mParentFragment != null) {
618 outState.putInt(KEY_DIALOG_ID, mDialogId);
619 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
620 }
621 }
622
623 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700624 public void onStart() {
625 super.onStart();
626
627 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
628 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
629 }
630 }
631
632 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800633 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800634 if (savedInstanceState != null) {
635 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800636 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800637 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700638 if (mParentFragment == null) {
639 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
640 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800641 if (!(mParentFragment instanceof DialogCreatable)) {
642 throw new IllegalArgumentException(
643 (mParentFragment != null
644 ? mParentFragment.getClass().getName()
645 : mParentFragmentId)
646 + " must implement "
647 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800648 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800649 // This dialog fragment could be created from non-SettingsPreferenceFragment
650 if (mParentFragment instanceof SettingsPreferenceFragment) {
651 // restore mDialogFragment in mParentFragment
652 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
653 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800654 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800655 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700656 }
657
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800658 @Override
659 public void onCancel(DialogInterface dialog) {
660 super.onCancel(dialog);
661 if (mOnCancelListener != null) {
662 mOnCancelListener.onCancel(dialog);
663 }
664 }
665
666 @Override
667 public void onDismiss(DialogInterface dialog) {
668 super.onDismiss(dialog);
669 if (mOnDismissListener != null) {
670 mOnDismissListener.onDismiss(dialog);
671 }
672 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800673
Amith Yamasanid7993472010-08-18 13:59:28 -0700674 public int getDialogId() {
675 return mDialogId;
676 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800677
678 @Override
679 public void onDetach() {
680 super.onDetach();
681
Amith Yamasani8875ede2011-01-31 12:46:57 -0800682 // This dialog fragment could be created from non-SettingsPreferenceFragment
683 if (mParentFragment instanceof SettingsPreferenceFragment) {
684 // in case the dialog is not explicitly removed by removeDialog()
685 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
686 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
687 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800688 }
689 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700690 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700691
692 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800693 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700694 }
695
696 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800697 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700698 }
699
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700700 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700701 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400702 if (activity == null) return;
703 if (getFragmentManager().getBackStackEntryCount() > 0) {
704 getFragmentManager().popBackStack();
705 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800706 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700707 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700708 }
709
Jason Monkb7e43802016-06-06 16:01:58 -0400710 protected Intent getIntent() {
711 if (getActivity() == null) {
712 return null;
713 }
714 return getActivity().getIntent();
715 }
716
717 protected void setResult(int result, Intent intent) {
718 if (getActivity() == null) {
719 return;
720 }
721 getActivity().setResult(result, intent);
722 }
723
724 protected void setResult(int result) {
725 if (getActivity() == null) {
726 return;
727 }
728 getActivity().setResult(result);
729 }
730
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700731 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
732 int requestCode, Bundle extras) {
733 final Activity activity = getActivity();
734 if (activity instanceof SettingsActivity) {
735 SettingsActivity sa = (SettingsActivity) activity;
Fan Zhangc6ca3142017-02-14 15:02:35 -0800736 sa.startPreferencePanel(
737 caller, fragmentClass, extras, titleRes, null, caller, requestCode);
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700738 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700739 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700740 Log.w(TAG,
741 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
742 + "launch the given Fragment (name: " + fragmentClass
743 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700744 return false;
745 }
746 }
Jason Monk65bb0972015-12-17 10:39:44 -0500747
748 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
749
750 private int mHighlightPosition = -1;
751
752 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
753 super(preferenceGroup);
754 }
755
756 public void highlight(int position) {
757 mHighlightPosition = position;
758 notifyDataSetChanged();
759 }
760
761 @Override
762 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
763 super.onBindViewHolder(holder, position);
764 if (position == mHighlightPosition) {
765 View v = holder.itemView;
Qi Dingc4772632016-09-18 17:03:47 +0800766 v.post(() -> {
767 if (v.getBackground() != null) {
768 final int centerX = v.getWidth() / 2;
769 final int centerY = v.getHeight() / 2;
770 v.getBackground().setHotspot(centerX, centerY);
771 }
772 v.setPressed(true);
773 v.setPressed(false);
774 mHighlightPosition = -1;
775 });
Jason Monk65bb0972015-12-17 10:39:44 -0500776 }
777 }
778 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700779}