blob: 96096ef64d278f47310b9724d67aceb90a866327 [file] [log] [blame]
Amith Yamasanid7993472010-08-18 13:59:28 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -070019import android.app.Activity;
Amith Yamasanid7993472010-08-18 13:59:28 -070020import android.app.Dialog;
21import android.app.DialogFragment;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -070022import android.app.Fragment;
Amith Yamasanid7993472010-08-18 13:59:28 -070023import android.content.ContentResolver;
Amith Yamasani350938e2013-04-09 10:22:47 -070024import android.content.Context;
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +080025import android.content.DialogInterface;
Jason Monkb7e43802016-06-06 16:01:58 -040026import android.content.Intent;
Amith Yamasanid7993472010-08-18 13:59:28 -070027import android.content.pm.PackageManager;
Amith Yamasanid7993472010-08-18 13:59:28 -070028import android.os.Bundle;
Fan Zhange84407f2017-05-24 11:19:52 -070029import android.support.annotation.VisibleForTesting;
Jason Monk91e2f892016-02-23 15:31:09 -050030import android.support.annotation.XmlRes;
Jason Monk39b46742015-09-10 15:52:51 -040031import android.support.v7.preference.Preference;
Jason Monk65bb0972015-12-17 10:39:44 -050032import android.support.v7.preference.PreferenceGroup;
Jason Monk39b46742015-09-10 15:52:51 -040033import android.support.v7.preference.PreferenceGroupAdapter;
34import android.support.v7.preference.PreferenceScreen;
Jason Monk65bb0972015-12-17 10:39:44 -050035import android.support.v7.preference.PreferenceViewHolder;
36import android.support.v7.widget.LinearLayoutManager;
Jason Monk39b46742015-09-10 15:52:51 -040037import android.support.v7.widget.RecyclerView;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070038import android.text.TextUtils;
Jason Monk2071eda2016-02-25 13:55:48 -050039import android.util.ArrayMap;
Amith Yamasanid7993472010-08-18 13:59:28 -070040import android.util.Log;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070041import android.view.LayoutInflater;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070042import android.view.View;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070043import android.view.ViewGroup;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070044import android.widget.Button;
Jason Monkb7e43802016-06-06 16:01:58 -040045
Jason Monk39b46742015-09-10 15:52:51 -040046import com.android.settings.applications.LayoutPreference;
Fan Zhang2d0b3442016-12-05 17:02:33 -080047import com.android.settings.core.InstrumentedPreferenceFragment;
Fan Zhang4fe7c082016-10-03 13:48:55 -070048import com.android.settings.core.instrumentation.Instrumentable;
Fan Zhangd65184f2016-09-19 17:45:24 -070049import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
Fan Zhange0b0e9f2017-11-29 14:55:59 -080050import com.android.settings.support.actionbar.HelpMenuController;
51import com.android.settings.support.actionbar.HelpResourceProvider;
Fan Zhang896f1b32017-06-26 14:22:45 -070052import com.android.settings.widget.LoadingViewController;
Juan Lang44828122017-05-10 17:26:02 -070053import com.android.settingslib.CustomDialogPreference;
54import com.android.settingslib.CustomEditTextPreference;
Juan Lang777ed252017-05-09 15:42:36 -070055import com.android.settingslib.widget.FooterPreferenceMixin;
John Spurlockb8e02b82015-04-15 21:15:55 -040056
Jason Monk39b46742015-09-10 15:52:51 -040057import java.util.UUID;
58
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070059/**
Amith Yamasanid7993472010-08-18 13:59:28 -070060 * Base class for Settings fragments, with some helper functions and dialog management.
61 */
Fan Zhang2d0b3442016-12-05 17:02:33 -080062public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceFragment
Fan Zhange0b0e9f2017-11-29 14:55:59 -080063 implements DialogCreatable, HelpResourceProvider {
Anna Galusza0285c802016-01-29 17:32:19 -080064
Jason Monk65bb0972015-12-17 10:39:44 -050065 private static final String TAG = "SettingsPreference";
Amith Yamasanid7993472010-08-18 13:59:28 -070066
Matthew Fritze33f3e3f2017-06-06 17:14:33 -070067 @VisibleForTesting
68 static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070069
70 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070071
Fan Zhangd5b48452016-12-13 12:42:50 -080072 protected final FooterPreferenceMixin mFooterPreferenceMixin =
73 new FooterPreferenceMixin(this, getLifecycle());
74
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070075
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000076 private static final int ORDER_FIRST = -1;
77 private static final int ORDER_LAST = Integer.MAX_VALUE -1;
78
Fan Zhange0b0e9f2017-11-29 14:55:59 -080079 private SettingsDialogFragment mDialogFragment;
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
Jason Monk39b46742015-09-10 15:52:51 -040085 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070086 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040087 private RecyclerView.AdapterDataObserver mDataSetObserver =
88 new RecyclerView.AdapterDataObserver() {
Tony Mantler0b825f52016-09-27 14:48:16 -070089 @Override
90 public void onChanged() {
91 onDataSetChanged();
92 }
93
94 @Override
95 public void onItemRangeChanged(int positionStart, int itemCount) {
96 onDataSetChanged();
97 }
98
99 @Override
100 public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
101 onDataSetChanged();
102 }
103
104 @Override
105 public void onItemRangeInserted(int positionStart, int itemCount) {
106 onDataSetChanged();
107 }
108
109 @Override
110 public void onItemRangeRemoved(int positionStart, int itemCount) {
111 onDataSetChanged();
112 }
113
114 @Override
115 public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
116 onDataSetChanged();
117 }
118 };
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700119
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700120 private ViewGroup mPinnedHeaderFrameLayout;
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900121 private ViewGroup mButtonBar;
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700122
Jason Monk39b46742015-09-10 15:52:51 -0400123 private LayoutPreference mHeader;
124
Jason Monk39b46742015-09-10 15:52:51 -0400125 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -0500126 private LinearLayoutManager mLayoutManager;
Jason Monk2071eda2016-02-25 13:55:48 -0500127 private ArrayMap<String, Preference> mPreferenceCache;
Jason Monkf38fb382016-03-18 14:23:01 -0400128 private boolean mAnimationAllowed;
Jason Monk39b46742015-09-10 15:52:51 -0400129
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700130 @VisibleForTesting
131 public HighlightablePreferenceGroupAdapter mAdapter;
132 @VisibleForTesting
133 public boolean mPreferenceHighlighted = false;
134
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700135 @Override
136 public void onCreate(Bundle icicle) {
137 super.onCreate(icicle);
Fan Zhange0b0e9f2017-11-29 14:55:59 -0800138 HelpMenuController.init(this /* host */);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700139
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700140 if (icicle != null) {
141 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
142 }
Doris Ling9ff1d792017-11-13 13:43:17 -0800143 final Bundle arguments = getArguments();
Doris Ling9ff1d792017-11-13 13:43:17 -0800144
145 // Check if we should keep the preferences expanded.
146 if (arguments != null) {
147 mPreferenceKey = arguments.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
148 if (!TextUtils.isEmpty(mPreferenceKey)) {
149 final PreferenceScreen screen = getPreferenceScreen();
150 if (screen != null) {
151 screen.setInitialExpandedChildrenCount(Integer.MAX_VALUE);
152 }
153 }
154 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700155 }
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);
Fan Zhange0b0e9f2017-11-29 14:55:59 -0800161 mPinnedHeaderFrameLayout = root.findViewById(R.id.pinned_header);
162 mButtonBar = 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
Doris Ling9ff1d792017-11-13 13:43:17 -0800219 if (mPreferenceKey != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700220 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700221 }
222 }
223
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700224 @Override
225 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700226 registerObserverIfNeeded();
227 }
228
229 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700230 protected void onUnbindPreferences() {
231 unregisterObserverIfNeeded();
232 }
233
Jason Monkb37e2882016-01-11 14:27:20 -0500234 public void setLoading(boolean loading, boolean animate) {
Fan Zhang896f1b32017-06-26 14:22:45 -0700235 View loadingContainer = getView().findViewById(R.id.loading_container);
236 LoadingViewController.handleLoadingContainer(loadingContainer, getListView(),
237 !loading /* done */,
238 animate);
Jason Monkb37e2882016-01-11 14:27:20 -0500239 }
240
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700241 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700242 if (!mIsDataSetObserverRegistered) {
243 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400244 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700245 }
Jason Monk39b46742015-09-10 15:52:51 -0400246 mCurrentRootAdapter = getListView().getAdapter();
247 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700248 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500249 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700250 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700251 }
252
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700253 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700254 if (mIsDataSetObserverRegistered) {
255 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400256 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700257 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700258 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700259 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700260 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700261 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700262
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700263 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700264 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700265 getView().postDelayed(new Runnable() {
266 @Override
267 public void run() {
268 highlightPreference(mPreferenceKey);
269 }
270 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
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
jackqdyulei2b2abac2017-05-26 10:47:55 -0700314 @VisibleForTesting
315 void updateEmptyView() {
Jason Monk39b46742015-09-10 15:52:51 -0400316 if (mEmptyView == null) return;
317 if (getPreferenceScreen() != null) {
jackqdyulei2b2abac2017-05-26 10:47:55 -0700318 final View listContainer = getActivity().findViewById(android.R.id.list_container);
Jason Monk39b46742015-09-10 15:52:51 -0400319 boolean show = (getPreferenceScreen().getPreferenceCount()
320 - (mHeader != null ? 1 : 0)
jackqdyulei2b2abac2017-05-26 10:47:55 -0700321 - (mFooterPreferenceMixin.hasFooter() ? 1 : 0)) <= 0
322 || (listContainer != null && listContainer.getVisibility() != View.VISIBLE);
Jason Monk39b46742015-09-10 15:52:51 -0400323 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
324 } else {
325 mEmptyView.setVisibility(View.VISIBLE);
326 }
327 }
328
329 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000330 if (mEmptyView != null) {
331 mEmptyView.setVisibility(View.GONE);
332 }
Jason Monk39b46742015-09-10 15:52:51 -0400333 mEmptyView = v;
334 updateEmptyView();
335 }
336
337 public View getEmptyView() {
338 return mEmptyView;
339 }
340
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700341 /**
342 * Return a valid ListView position or -1 if none is found
343 */
344 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400345 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700346 return -1;
347 }
348
Jason Monk39b46742015-09-10 15:52:51 -0400349 RecyclerView listView = getListView();
350 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700351
352 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400353 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700354 }
355
356 return -1;
357 }
358
Jason Monk65bb0972015-12-17 10:39:44 -0500359 @Override
360 public RecyclerView.LayoutManager onCreateLayoutManager() {
361 mLayoutManager = new LinearLayoutManager(getContext());
362 return mLayoutManager;
363 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700364
Jason Monk65bb0972015-12-17 10:39:44 -0500365 @Override
366 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
367 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
368 return mAdapter;
369 }
370
Jason Monkf38fb382016-03-18 14:23:01 -0400371 protected void setAnimationAllowed(boolean animationAllowed) {
372 mAnimationAllowed = animationAllowed;
373 }
374
Jason Monk2071eda2016-02-25 13:55:48 -0500375 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
376 mPreferenceCache = new ArrayMap<String, Preference>();
377 final int N = group.getPreferenceCount();
378 for (int i = 0; i < N; i++) {
379 Preference p = group.getPreference(i);
380 if (TextUtils.isEmpty(p.getKey())) {
381 continue;
382 }
383 mPreferenceCache.put(p.getKey(), p);
384 }
385 }
386
387 protected Preference getCachedPreference(String key) {
388 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
389 }
390
391 protected void removeCachedPrefs(PreferenceGroup group) {
392 for (Preference p : mPreferenceCache.values()) {
393 group.removePreference(p);
394 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400395 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500396 }
397
Jason Monka6278442016-04-21 10:12:30 -0400398 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400399 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400400 }
401
Jason Monk65bb0972015-12-17 10:39:44 -0500402 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700403 final int position = canUseListViewForHighLighting(key);
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700404 if (position < 0) {
405 return;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700406 }
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700407
408 mPreferenceHighlighted = true;
409 mLayoutManager.scrollToPosition(position);
410 mAdapter.highlight(position);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700411 }
412
Jason Monk39b46742015-09-10 15:52:51 -0400413 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
414 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700415 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400416 final Preference preference = adapter.getItem(n);
417 final String preferenceKey = preference.getKey();
418 if (preferenceKey != null && preferenceKey.equals(key)) {
419 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700420 }
421 }
422 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700423 }
424
Fan Zhange84407f2017-05-24 11:19:52 -0700425 protected boolean removePreference(String key) {
426 return removePreference(getPreferenceScreen(), key);
427 }
428
429 @VisibleForTesting
430 boolean removePreference(PreferenceGroup group, String key) {
431 final int preferenceCount = group.getPreferenceCount();
432 for (int i = 0; i < preferenceCount; i++) {
433 final Preference preference = group.getPreference(i);
434 final String curKey = preference.getKey();
435
436 if (TextUtils.equals(curKey, key)) {
437 return group.removePreference(preference);
438 }
439
440 if (preference instanceof PreferenceGroup) {
441 if (removePreference((PreferenceGroup) preference, key)) {
442 return true;
443 }
444 }
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700445 }
Fan Zhange84407f2017-05-24 11:19:52 -0700446 return false;
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700447 }
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
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700750 @VisibleForTesting(otherwise=VisibleForTesting.NONE)
751 int initialHighlightedPosition = -1;
752
Jason Monk65bb0972015-12-17 10:39:44 -0500753 private int mHighlightPosition = -1;
754
755 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
756 super(preferenceGroup);
757 }
758
759 public void highlight(int position) {
760 mHighlightPosition = position;
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700761 initialHighlightedPosition = position;
Jason Monk65bb0972015-12-17 10:39:44 -0500762 notifyDataSetChanged();
763 }
764
765 @Override
766 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
767 super.onBindViewHolder(holder, position);
768 if (position == mHighlightPosition) {
769 View v = holder.itemView;
Qi Dingc4772632016-09-18 17:03:47 +0800770 v.post(() -> {
771 if (v.getBackground() != null) {
772 final int centerX = v.getWidth() / 2;
773 final int centerY = v.getHeight() / 2;
774 v.getBackground().setHotspot(centerX, centerY);
775 }
776 v.setPressed(true);
777 v.setPressed(false);
778 mHighlightPosition = -1;
779 });
Jason Monk65bb0972015-12-17 10:39:44 -0500780 }
781 }
782 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700783}