blob: 5f22545ca92caaf5d1d9a2fb05ebc262f9976a1a [file] [log] [blame]
Amith Yamasanid7993472010-08-18 13:59:28 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -070019import android.app.Activity;
Amith Yamasanid7993472010-08-18 13:59:28 -070020import android.app.Dialog;
21import android.app.DialogFragment;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -070022import android.app.Fragment;
Amith Yamasanid7993472010-08-18 13:59:28 -070023import android.content.ContentResolver;
Amith Yamasani350938e2013-04-09 10:22:47 -070024import android.content.Context;
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +080025import android.content.DialogInterface;
Jason Monkb7e43802016-06-06 16:01:58 -040026import android.content.Intent;
Amith Yamasanid7993472010-08-18 13:59:28 -070027import android.content.pm.PackageManager;
Amith Yamasanid7993472010-08-18 13:59:28 -070028import android.os.Bundle;
Fan Zhange84407f2017-05-24 11:19:52 -070029import android.support.annotation.VisibleForTesting;
Jason Monk91e2f892016-02-23 15:31:09 -050030import android.support.annotation.XmlRes;
Jason Monk39b46742015-09-10 15:52:51 -040031import android.support.v7.preference.Preference;
Jason Monk65bb0972015-12-17 10:39:44 -050032import android.support.v7.preference.PreferenceGroup;
Jason Monk39b46742015-09-10 15:52:51 -040033import android.support.v7.preference.PreferenceGroupAdapter;
34import android.support.v7.preference.PreferenceScreen;
Jason Monk65bb0972015-12-17 10:39:44 -050035import android.support.v7.preference.PreferenceViewHolder;
36import android.support.v7.widget.LinearLayoutManager;
Jason Monk39b46742015-09-10 15:52:51 -040037import android.support.v7.widget.RecyclerView;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070038import android.text.TextUtils;
Jason Monk2071eda2016-02-25 13:55:48 -050039import android.util.ArrayMap;
Amith Yamasanid7993472010-08-18 13:59:28 -070040import android.util.Log;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070041import android.view.LayoutInflater;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070042import android.view.Menu;
43import android.view.MenuInflater;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070044import android.view.View;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070045import android.view.ViewGroup;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070046import android.widget.Button;
Jason Monkb7e43802016-06-06 16:01:58 -040047
Jason Monk39b46742015-09-10 15:52:51 -040048import com.android.settings.applications.LayoutPreference;
Fan Zhang2d0b3442016-12-05 17:02:33 -080049import com.android.settings.core.InstrumentedPreferenceFragment;
Fan Zhang4fe7c082016-10-03 13:48:55 -070050import com.android.settings.core.instrumentation.Instrumentable;
Fan Zhangd65184f2016-09-19 17:45:24 -070051import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
Juan Lang44828122017-05-10 17:26:02 -070052import com.android.settingslib.CustomDialogPreference;
53import com.android.settingslib.CustomEditTextPreference;
Suprabh Shuklab84720c2016-04-05 14:37:20 -070054import com.android.settingslib.HelpUtils;
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
Chris Wren8a963ba2015-03-20 10:29:14 -040063 implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070064
Anna Galusza0285c802016-01-29 17:32:19 -080065 /**
66 * The Help Uri Resource key. This can be passed as an extra argument when creating the
67 * Fragment.
68 **/
69 public static final String HELP_URI_RESOURCE_KEY = "help_uri_resource";
70
Jason Monk65bb0972015-12-17 10:39:44 -050071 private static final String TAG = "SettingsPreference";
Amith Yamasanid7993472010-08-18 13:59:28 -070072
Matthew Fritze33f3e3f2017-06-06 17:14:33 -070073 @VisibleForTesting
74 static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070075
76 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070077
Fan Zhangd5b48452016-12-13 12:42:50 -080078 protected final FooterPreferenceMixin mFooterPreferenceMixin =
79 new FooterPreferenceMixin(this, getLifecycle());
80
Amith Yamasanid7993472010-08-18 13:59:28 -070081 private SettingsDialogFragment mDialogFragment;
82
Jason Monk23acc2b2015-04-14 15:06:39 -040083 private String mHelpUri;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070084
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000085 private static final int ORDER_FIRST = -1;
86 private static final int ORDER_LAST = Integer.MAX_VALUE -1;
87
Amith Yamasani350938e2013-04-09 10:22:47 -070088 // Cache the content resolver for async callbacks
89 private ContentResolver mContentResolver;
90
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070091 private String mPreferenceKey;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070092
Jason Monk39b46742015-09-10 15:52:51 -040093 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070094 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040095 private RecyclerView.AdapterDataObserver mDataSetObserver =
96 new RecyclerView.AdapterDataObserver() {
Tony Mantler0b825f52016-09-27 14:48:16 -070097 @Override
98 public void onChanged() {
99 onDataSetChanged();
100 }
101
102 @Override
103 public void onItemRangeChanged(int positionStart, int itemCount) {
104 onDataSetChanged();
105 }
106
107 @Override
108 public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
109 onDataSetChanged();
110 }
111
112 @Override
113 public void onItemRangeInserted(int positionStart, int itemCount) {
114 onDataSetChanged();
115 }
116
117 @Override
118 public void onItemRangeRemoved(int positionStart, int itemCount) {
119 onDataSetChanged();
120 }
121
122 @Override
123 public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
124 onDataSetChanged();
125 }
126 };
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700127
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700128 private ViewGroup mPinnedHeaderFrameLayout;
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900129 private ViewGroup mButtonBar;
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700130
Jason Monk39b46742015-09-10 15:52:51 -0400131 private LayoutPreference mHeader;
132
Jason Monk39b46742015-09-10 15:52:51 -0400133 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -0500134 private LinearLayoutManager mLayoutManager;
Jason Monk2071eda2016-02-25 13:55:48 -0500135 private ArrayMap<String, Preference> mPreferenceCache;
Jason Monkf38fb382016-03-18 14:23:01 -0400136 private boolean mAnimationAllowed;
Jason Monk39b46742015-09-10 15:52:51 -0400137
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700138 @VisibleForTesting
139 public HighlightablePreferenceGroupAdapter mAdapter;
140 @VisibleForTesting
141 public boolean mPreferenceHighlighted = false;
142
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700143 @Override
144 public void onCreate(Bundle icicle) {
145 super.onCreate(icicle);
146
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700147 if (icicle != null) {
148 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
149 }
150
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700151 // Prepare help url and enable menu if necessary
Anna Galusza0285c802016-01-29 17:32:19 -0800152 Bundle arguments = getArguments();
153 int helpResource;
154 if (arguments != null && arguments.containsKey(HELP_URI_RESOURCE_KEY)) {
155 helpResource = arguments.getInt(HELP_URI_RESOURCE_KEY);
156 } else {
157 helpResource = getHelpResource();
158 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700159 if (helpResource != 0) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400160 mHelpUri = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700161 }
162 }
163
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700164 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700165 public View onCreateView(LayoutInflater inflater, ViewGroup container,
166 Bundle savedInstanceState) {
167 final View root = super.onCreateView(inflater, container, savedInstanceState);
168 mPinnedHeaderFrameLayout = (ViewGroup) root.findViewById(R.id.pinned_header);
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900169 mButtonBar = (ViewGroup) root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700170 return root;
171 }
172
Jason Monk39b46742015-09-10 15:52:51 -0400173 @Override
Jason Monk91e2f892016-02-23 15:31:09 -0500174 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
175 super.addPreferencesFromResource(preferencesResId);
176 checkAvailablePrefs(getPreferenceScreen());
177 }
178
179 private void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
180 if (preferenceGroup == null) return;
181 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
182 Preference pref = preferenceGroup.getPreference(i);
183 if (pref instanceof SelfAvailablePreference
184 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
185 preferenceGroup.removePreference(pref);
186 } else if (pref instanceof PreferenceGroup) {
187 checkAvailablePrefs((PreferenceGroup) pref);
188 }
189 }
190 }
191
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900192 public ViewGroup getButtonBar() {
193 return mButtonBar;
194 }
195
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700196 public View setPinnedHeaderView(int layoutResId) {
197 final LayoutInflater inflater = getActivity().getLayoutInflater();
198 final View pinnedHeader =
199 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
200 setPinnedHeaderView(pinnedHeader);
201 return pinnedHeader;
202 }
203
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700204 public void setPinnedHeaderView(View pinnedHeader) {
205 mPinnedHeaderFrameLayout.addView(pinnedHeader);
206 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
207 }
208
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700209 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700210 public void onSaveInstanceState(Bundle outState) {
211 super.onSaveInstanceState(outState);
212
213 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
214 }
215
216 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700217 public void onActivityCreated(Bundle savedInstanceState) {
218 super.onActivityCreated(savedInstanceState);
Johan Redestig76218e52016-04-19 08:29:30 +0200219 setHasOptionsMenu(true);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700220 }
221
222 @Override
223 public void onResume() {
224 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700225
226 final Bundle args = getArguments();
227 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700228 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
229 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700230 }
231 }
232
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700233 @Override
234 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700235 registerObserverIfNeeded();
236 }
237
238 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700239 protected void onUnbindPreferences() {
240 unregisterObserverIfNeeded();
241 }
242
Jason Monkb5aa73f2015-03-31 12:59:33 -0400243 public void showLoadingWhenEmpty() {
244 View loading = getView().findViewById(R.id.loading_container);
Jason Monk39b46742015-09-10 15:52:51 -0400245 setEmptyView(loading);
Jason Monkb5aa73f2015-03-31 12:59:33 -0400246 }
247
Jason Monkb37e2882016-01-11 14:27:20 -0500248 public void setLoading(boolean loading, boolean animate) {
249 View loading_container = getView().findViewById(R.id.loading_container);
250 Utils.handleLoadingContainer(loading_container, getListView(), !loading, animate);
251 }
252
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700253 public void registerObserverIfNeeded() {
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 Meglio7c435f62014-07-29 16:02:22 -0700257 }
Jason Monk39b46742015-09-10 15:52:51 -0400258 mCurrentRootAdapter = getListView().getAdapter();
259 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700260 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500261 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700262 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700263 }
264
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700265 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700266 if (mIsDataSetObserverRegistered) {
267 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400268 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700269 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700270 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700271 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700272 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700273 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700274
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700275 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700276 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700277 getView().postDelayed(new Runnable() {
278 @Override
279 public void run() {
280 highlightPreference(mPreferenceKey);
281 }
282 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700283 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700284 }
285
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000286 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400287 highlightPreferenceIfNeeded();
288 updateEmptyView();
289 }
290
Jason Monk39b46742015-09-10 15:52:51 -0400291 public LayoutPreference getHeaderView() {
292 return mHeader;
293 }
294
Jason Monk39b46742015-09-10 15:52:51 -0400295 protected void setHeaderView(int resource) {
296 mHeader = new LayoutPreference(getPrefContext(), resource);
Udam Sainid553abc2016-02-16 17:54:13 -0800297 addPreferenceToTop(mHeader);
298 }
299
300 protected void setHeaderView(View view) {
301 mHeader = new LayoutPreference(getPrefContext(), view);
302 addPreferenceToTop(mHeader);
303 }
304
305 private void addPreferenceToTop(LayoutPreference preference) {
306 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400307 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800308 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400309 }
310 }
311
Jason Monk39b46742015-09-10 15:52:51 -0400312 @Override
313 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400314 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400315 // Without ids generated, the RecyclerView won't animate changes to the preferences.
316 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
317 }
Jason Monk39b46742015-09-10 15:52:51 -0400318 super.setPreferenceScreen(preferenceScreen);
319 if (preferenceScreen != null) {
320 if (mHeader != null) {
321 preferenceScreen.addPreference(mHeader);
322 }
Jason Monk39b46742015-09-10 15:52:51 -0400323 }
324 }
325
jackqdyulei2b2abac2017-05-26 10:47:55 -0700326 @VisibleForTesting
327 void updateEmptyView() {
Jason Monk39b46742015-09-10 15:52:51 -0400328 if (mEmptyView == null) return;
329 if (getPreferenceScreen() != null) {
jackqdyulei2b2abac2017-05-26 10:47:55 -0700330 final View listContainer = getActivity().findViewById(android.R.id.list_container);
Jason Monk39b46742015-09-10 15:52:51 -0400331 boolean show = (getPreferenceScreen().getPreferenceCount()
332 - (mHeader != null ? 1 : 0)
jackqdyulei2b2abac2017-05-26 10:47:55 -0700333 - (mFooterPreferenceMixin.hasFooter() ? 1 : 0)) <= 0
334 || (listContainer != null && listContainer.getVisibility() != View.VISIBLE);
Jason Monk39b46742015-09-10 15:52:51 -0400335 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
336 } else {
337 mEmptyView.setVisibility(View.VISIBLE);
338 }
339 }
340
341 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000342 if (mEmptyView != null) {
343 mEmptyView.setVisibility(View.GONE);
344 }
Jason Monk39b46742015-09-10 15:52:51 -0400345 mEmptyView = v;
346 updateEmptyView();
347 }
348
349 public View getEmptyView() {
350 return mEmptyView;
351 }
352
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700353 /**
354 * Return a valid ListView position or -1 if none is found
355 */
356 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400357 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700358 return -1;
359 }
360
Jason Monk39b46742015-09-10 15:52:51 -0400361 RecyclerView listView = getListView();
362 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700363
364 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400365 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700366 }
367
368 return -1;
369 }
370
Jason Monk65bb0972015-12-17 10:39:44 -0500371 @Override
372 public RecyclerView.LayoutManager onCreateLayoutManager() {
373 mLayoutManager = new LinearLayoutManager(getContext());
374 return mLayoutManager;
375 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700376
Jason Monk65bb0972015-12-17 10:39:44 -0500377 @Override
378 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
379 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
380 return mAdapter;
381 }
382
Jason Monkf38fb382016-03-18 14:23:01 -0400383 protected void setAnimationAllowed(boolean animationAllowed) {
384 mAnimationAllowed = animationAllowed;
385 }
386
Jason Monk2071eda2016-02-25 13:55:48 -0500387 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
388 mPreferenceCache = new ArrayMap<String, Preference>();
389 final int N = group.getPreferenceCount();
390 for (int i = 0; i < N; i++) {
391 Preference p = group.getPreference(i);
392 if (TextUtils.isEmpty(p.getKey())) {
393 continue;
394 }
395 mPreferenceCache.put(p.getKey(), p);
396 }
397 }
398
399 protected Preference getCachedPreference(String key) {
400 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
401 }
402
403 protected void removeCachedPrefs(PreferenceGroup group) {
404 for (Preference p : mPreferenceCache.values()) {
405 group.removePreference(p);
406 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400407 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500408 }
409
Jason Monka6278442016-04-21 10:12:30 -0400410 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400411 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400412 }
413
Jason Monk65bb0972015-12-17 10:39:44 -0500414 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700415 final int position = canUseListViewForHighLighting(key);
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700416 if (position < 0) {
417 return;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700418 }
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700419
420 mPreferenceHighlighted = true;
421 mLayoutManager.scrollToPosition(position);
422 mAdapter.highlight(position);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700423 }
424
Jason Monk39b46742015-09-10 15:52:51 -0400425 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
426 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700427 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400428 final Preference preference = adapter.getItem(n);
429 final String preferenceKey = preference.getKey();
430 if (preferenceKey != null && preferenceKey.equals(key)) {
431 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700432 }
433 }
434 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700435 }
436
Fan Zhange84407f2017-05-24 11:19:52 -0700437 protected boolean removePreference(String key) {
438 return removePreference(getPreferenceScreen(), key);
439 }
440
441 @VisibleForTesting
442 boolean removePreference(PreferenceGroup group, String key) {
443 final int preferenceCount = group.getPreferenceCount();
444 for (int i = 0; i < preferenceCount; i++) {
445 final Preference preference = group.getPreference(i);
446 final String curKey = preference.getKey();
447
448 if (TextUtils.equals(curKey, key)) {
449 return group.removePreference(preference);
450 }
451
452 if (preference instanceof PreferenceGroup) {
453 if (removePreference((PreferenceGroup) preference, key)) {
454 return true;
455 }
456 }
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700457 }
Fan Zhange84407f2017-05-24 11:19:52 -0700458 return false;
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700459 }
460
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700461 /**
462 * Override this if you want to show a help item in the menu, by returning the resource id.
463 * @return the resource id for the help url
464 */
465 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400466 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700467 }
468
469 @Override
470 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Daniel Nishie6740f72017-04-14 14:12:35 -0700471 super.onCreateOptionsMenu(menu, inflater);
Jason Monk23acc2b2015-04-14 15:06:39 -0400472 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400473 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700474 }
475 }
476
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700477 /*
478 * The name is intentionally made different from Activity#finish(), so that
479 * users won't misunderstand its meaning.
480 */
481 public final void finishFragment() {
482 getActivity().onBackPressed();
483 }
484
Amith Yamasanid7993472010-08-18 13:59:28 -0700485 // Some helpers for functions used by the settings fragments when they were activities
486
487 /**
488 * Returns the ContentResolver from the owning Activity.
489 */
490 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700491 Context context = getActivity();
492 if (context != null) {
493 mContentResolver = context.getContentResolver();
494 }
495 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700496 }
497
498 /**
499 * Returns the specified system service from the owning Activity.
500 */
501 protected Object getSystemService(final String name) {
502 return getActivity().getSystemService(name);
503 }
504
505 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700506 * Returns the PackageManager from the owning Activity.
507 */
508 protected PackageManager getPackageManager() {
509 return getActivity().getPackageManager();
510 }
511
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800512 @Override
513 public void onDetach() {
514 if (isRemoving()) {
515 if (mDialogFragment != null) {
516 mDialogFragment.dismiss();
517 mDialogFragment = null;
518 }
519 }
520 super.onDetach();
521 }
522
Amith Yamasanid7993472010-08-18 13:59:28 -0700523 // Dialog management
524
525 protected void showDialog(int dialogId) {
526 if (mDialogFragment != null) {
527 Log.e(TAG, "Old dialog fragment not null!");
528 }
529 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800530 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700531 }
532
Fan Zhangd65184f2016-09-19 17:45:24 -0700533 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700534 public Dialog onCreateDialog(int dialogId) {
535 return null;
536 }
537
Fan Zhangd65184f2016-09-19 17:45:24 -0700538 @Override
539 public int getDialogMetricsCategory(int dialogId) {
540 return 0;
541 }
542
Amith Yamasanid7993472010-08-18 13:59:28 -0700543 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800544 // mDialogFragment may not be visible yet in parent fragment's onResume().
545 // To be able to dismiss dialog at that time, don't check
546 // mDialogFragment.isVisible().
547 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400548 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700549 }
550 mDialogFragment = null;
551 }
552
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800553 /**
554 * Sets the OnCancelListener of the dialog shown. This method can only be
555 * called after showDialog(int) and before removeDialog(int). The method
556 * does nothing otherwise.
557 */
558 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
559 if (mDialogFragment != null) {
560 mDialogFragment.mOnCancelListener = listener;
561 }
562 }
563
564 /**
565 * Sets the OnDismissListener of the dialog shown. This method can only be
566 * called after showDialog(int) and before removeDialog(int). The method
567 * does nothing otherwise.
568 */
569 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
570 if (mDialogFragment != null) {
571 mDialogFragment.mOnDismissListener = listener;
572 }
573 }
574
Amith Yamasanic861cf82012-10-02 14:51:46 -0700575 public void onDialogShowing() {
576 // override in subclass to attach a dismiss listener, for instance
577 }
578
Jason Monk39b46742015-09-10 15:52:51 -0400579 @Override
580 public void onDisplayPreferenceDialog(Preference preference) {
581 if (preference.getKey() == null) {
582 // Auto-key preferences that don't have a key, so the dialog can find them.
583 preference.setKey(UUID.randomUUID().toString());
584 }
585 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000586 if (preference instanceof RestrictedListPreference) {
587 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
588 .newInstance(preference.getKey());
589 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400590 f = CustomListPreference.CustomListPreferenceDialogFragment
591 .newInstance(preference.getKey());
592 } else if (preference instanceof CustomDialogPreference) {
593 f = CustomDialogPreference.CustomPreferenceDialogFragment
594 .newInstance(preference.getKey());
595 } else if (preference instanceof CustomEditTextPreference) {
596 f = CustomEditTextPreference.CustomPreferenceDialogFragment
597 .newInstance(preference.getKey());
598 } else {
599 super.onDisplayPreferenceDialog(preference);
600 return;
601 }
602 f.setTargetFragment(this, 0);
603 f.show(getFragmentManager(), "dialog_preference");
604 onDialogShowing();
605 }
606
Fan Zhangd65184f2016-09-19 17:45:24 -0700607 public static class SettingsDialogFragment extends InstrumentedDialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800608 private static final String KEY_DIALOG_ID = "key_dialog_id";
609 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
610
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800611 private Fragment mParentFragment;
612
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800613 private DialogInterface.OnCancelListener mOnCancelListener;
614 private DialogInterface.OnDismissListener mOnDismissListener;
615
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800616 public SettingsDialogFragment() {
617 /* do nothing */
618 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700619
Amith Yamasani43c69782010-12-01 09:04:36 -0800620 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Fan Zhangd65184f2016-09-19 17:45:24 -0700621 super(fragment, dialogId);
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800622 if (!(fragment instanceof Fragment)) {
623 throw new IllegalArgumentException("fragment argument must be an instance of "
624 + Fragment.class.getName());
625 }
626 mParentFragment = (Fragment) fragment;
627 }
628
Fan Zhangd65184f2016-09-19 17:45:24 -0700629
630 @Override
631 public int getMetricsCategory() {
Fan Zhang4fe7c082016-10-03 13:48:55 -0700632 if (mDialogCreatable == null) {
633 return Instrumentable.METRICS_CATEGORY_UNKNOWN;
634 }
Fan Zhangd65184f2016-09-19 17:45:24 -0700635 final int metricsCategory = mDialogCreatable.getDialogMetricsCategory(mDialogId);
636 if (metricsCategory <= 0) {
637 throw new IllegalStateException("Dialog must provide a metrics category");
638 }
639 return metricsCategory;
640 }
641
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800642 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800643 public void onSaveInstanceState(Bundle outState) {
644 super.onSaveInstanceState(outState);
645 if (mParentFragment != null) {
646 outState.putInt(KEY_DIALOG_ID, mDialogId);
647 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
648 }
649 }
650
651 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700652 public void onStart() {
653 super.onStart();
654
655 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
656 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
657 }
658 }
659
660 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800661 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800662 if (savedInstanceState != null) {
663 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800664 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800665 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700666 if (mParentFragment == null) {
667 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
668 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800669 if (!(mParentFragment instanceof DialogCreatable)) {
670 throw new IllegalArgumentException(
671 (mParentFragment != null
672 ? mParentFragment.getClass().getName()
673 : mParentFragmentId)
674 + " must implement "
675 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800676 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800677 // This dialog fragment could be created from non-SettingsPreferenceFragment
678 if (mParentFragment instanceof SettingsPreferenceFragment) {
679 // restore mDialogFragment in mParentFragment
680 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
681 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800682 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800683 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700684 }
685
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800686 @Override
687 public void onCancel(DialogInterface dialog) {
688 super.onCancel(dialog);
689 if (mOnCancelListener != null) {
690 mOnCancelListener.onCancel(dialog);
691 }
692 }
693
694 @Override
695 public void onDismiss(DialogInterface dialog) {
696 super.onDismiss(dialog);
697 if (mOnDismissListener != null) {
698 mOnDismissListener.onDismiss(dialog);
699 }
700 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800701
Amith Yamasanid7993472010-08-18 13:59:28 -0700702 public int getDialogId() {
703 return mDialogId;
704 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800705
706 @Override
707 public void onDetach() {
708 super.onDetach();
709
Amith Yamasani8875ede2011-01-31 12:46:57 -0800710 // This dialog fragment could be created from non-SettingsPreferenceFragment
711 if (mParentFragment instanceof SettingsPreferenceFragment) {
712 // in case the dialog is not explicitly removed by removeDialog()
713 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
714 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
715 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800716 }
717 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700718 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700719
720 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800721 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700722 }
723
724 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800725 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700726 }
727
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700728 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700729 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400730 if (activity == null) return;
731 if (getFragmentManager().getBackStackEntryCount() > 0) {
732 getFragmentManager().popBackStack();
733 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800734 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700735 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700736 }
737
Jason Monkb7e43802016-06-06 16:01:58 -0400738 protected Intent getIntent() {
739 if (getActivity() == null) {
740 return null;
741 }
742 return getActivity().getIntent();
743 }
744
745 protected void setResult(int result, Intent intent) {
746 if (getActivity() == null) {
747 return;
748 }
749 getActivity().setResult(result, intent);
750 }
751
752 protected void setResult(int result) {
753 if (getActivity() == null) {
754 return;
755 }
756 getActivity().setResult(result);
757 }
758
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700759 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
760 int requestCode, Bundle extras) {
761 final Activity activity = getActivity();
762 if (activity instanceof SettingsActivity) {
763 SettingsActivity sa = (SettingsActivity) activity;
Fan Zhangc6ca3142017-02-14 15:02:35 -0800764 sa.startPreferencePanel(
765 caller, fragmentClass, extras, titleRes, null, caller, requestCode);
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700766 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700767 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700768 Log.w(TAG,
769 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
770 + "launch the given Fragment (name: " + fragmentClass
771 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700772 return false;
773 }
774 }
Jason Monk65bb0972015-12-17 10:39:44 -0500775
776 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
777
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700778 @VisibleForTesting(otherwise=VisibleForTesting.NONE)
779 int initialHighlightedPosition = -1;
780
Jason Monk65bb0972015-12-17 10:39:44 -0500781 private int mHighlightPosition = -1;
782
783 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
784 super(preferenceGroup);
785 }
786
787 public void highlight(int position) {
788 mHighlightPosition = position;
Matthew Fritze33f3e3f2017-06-06 17:14:33 -0700789 initialHighlightedPosition = position;
Jason Monk65bb0972015-12-17 10:39:44 -0500790 notifyDataSetChanged();
791 }
792
793 @Override
794 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
795 super.onBindViewHolder(holder, position);
796 if (position == mHighlightPosition) {
797 View v = holder.itemView;
Qi Dingc4772632016-09-18 17:03:47 +0800798 v.post(() -> {
799 if (v.getBackground() != null) {
800 final int centerX = v.getWidth() / 2;
801 final int centerY = v.getHeight() / 2;
802 v.getBackground().setHotspot(centerX, centerY);
803 }
804 v.setPressed(true);
805 v.setPressed(false);
806 mHighlightPosition = -1;
807 });
Jason Monk65bb0972015-12-17 10:39:44 -0500808 }
809 }
810 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700811}