blob: 5cc4011ddbcbb75ed9636cfaeb70f55f69a1b726 [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 Zhang65076132016-08-08 10:25:13 -070048import com.android.settings.core.InstrumentedFragment;
Fan Zhangd65184f2016-09-19 17:45:24 -070049import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
Suprabh Shuklab84720c2016-04-05 14:37:20 -070050import com.android.settingslib.HelpUtils;
John Spurlockb8e02b82015-04-15 21:15:55 -040051
Jason Monk39b46742015-09-10 15:52:51 -040052import java.util.UUID;
53
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070054/**
Amith Yamasanid7993472010-08-18 13:59:28 -070055 * Base class for Settings fragments, with some helper functions and dialog management.
56 */
Fan Zhangbf4be772016-08-04 16:05:55 -070057public abstract class SettingsPreferenceFragment extends InstrumentedFragment
Chris Wren8a963ba2015-03-20 10:29:14 -040058 implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070059
Anna Galusza0285c802016-01-29 17:32:19 -080060 /**
61 * The Help Uri Resource key. This can be passed as an extra argument when creating the
62 * Fragment.
63 **/
64 public static final String HELP_URI_RESOURCE_KEY = "help_uri_resource";
65
Jason Monk65bb0972015-12-17 10:39:44 -050066 private static final String TAG = "SettingsPreference";
Amith Yamasanid7993472010-08-18 13:59:28 -070067
Fabrice Di Meglioeced7802014-09-04 13:01:55 -070068 private 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
Amith Yamasanid7993472010-08-18 13:59:28 -070072 private SettingsDialogFragment mDialogFragment;
73
Jason Monk23acc2b2015-04-14 15:06:39 -040074 private String mHelpUri;
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
Amith Yamasani350938e2013-04-09 10:22:47 -070079 // Cache the content resolver for async callbacks
80 private ContentResolver mContentResolver;
81
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070082 private String mPreferenceKey;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070083 private boolean mPreferenceHighlighted = false;
84
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() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070089 @Override
90 public void onChanged() {
Jason Monk39b46742015-09-10 15:52:51 -040091 onDataSetChanged();
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070092 }
93 };
94
Fabrice Di Meglio86159282014-07-21 16:02:27 -070095 private ViewGroup mPinnedHeaderFrameLayout;
Daichi Hirono5e76cdc2015-07-08 11:38:55 +090096 private ViewGroup mButtonBar;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070097
Jason Monk39b46742015-09-10 15:52:51 -040098 private LayoutPreference mHeader;
99
100 private LayoutPreference mFooter;
101 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -0500102 private LinearLayoutManager mLayoutManager;
103 private HighlightablePreferenceGroupAdapter mAdapter;
Jason Monk2071eda2016-02-25 13:55:48 -0500104 private ArrayMap<String, Preference> mPreferenceCache;
Jason Monkf38fb382016-03-18 14:23:01 -0400105 private boolean mAnimationAllowed;
Jason Monk39b46742015-09-10 15:52:51 -0400106
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700107 @Override
108 public void onCreate(Bundle icicle) {
109 super.onCreate(icicle);
110
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700111 if (icicle != null) {
112 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
113 }
114
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700115 // Prepare help url and enable menu if necessary
Anna Galusza0285c802016-01-29 17:32:19 -0800116 Bundle arguments = getArguments();
117 int helpResource;
118 if (arguments != null && arguments.containsKey(HELP_URI_RESOURCE_KEY)) {
119 helpResource = arguments.getInt(HELP_URI_RESOURCE_KEY);
120 } else {
121 helpResource = getHelpResource();
122 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700123 if (helpResource != 0) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400124 mHelpUri = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700125 }
126 }
127
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700128 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700129 public View onCreateView(LayoutInflater inflater, ViewGroup container,
130 Bundle savedInstanceState) {
131 final View root = super.onCreateView(inflater, container, savedInstanceState);
132 mPinnedHeaderFrameLayout = (ViewGroup) root.findViewById(R.id.pinned_header);
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900133 mButtonBar = (ViewGroup) root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700134 return root;
135 }
136
Jason Monk39b46742015-09-10 15:52:51 -0400137 @Override
Jason Monk91e2f892016-02-23 15:31:09 -0500138 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
139 super.addPreferencesFromResource(preferencesResId);
140 checkAvailablePrefs(getPreferenceScreen());
141 }
142
143 private void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
144 if (preferenceGroup == null) return;
145 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
146 Preference pref = preferenceGroup.getPreference(i);
147 if (pref instanceof SelfAvailablePreference
148 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
149 preferenceGroup.removePreference(pref);
150 } else if (pref instanceof PreferenceGroup) {
151 checkAvailablePrefs((PreferenceGroup) pref);
152 }
153 }
154 }
155
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900156 public ViewGroup getButtonBar() {
157 return mButtonBar;
158 }
159
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700160 public View setPinnedHeaderView(int layoutResId) {
161 final LayoutInflater inflater = getActivity().getLayoutInflater();
162 final View pinnedHeader =
163 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
164 setPinnedHeaderView(pinnedHeader);
165 return pinnedHeader;
166 }
167
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700168 public void setPinnedHeaderView(View pinnedHeader) {
169 mPinnedHeaderFrameLayout.addView(pinnedHeader);
170 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
171 }
172
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700173 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700174 public void onSaveInstanceState(Bundle outState) {
175 super.onSaveInstanceState(outState);
176
177 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
178 }
179
180 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700181 public void onActivityCreated(Bundle savedInstanceState) {
182 super.onActivityCreated(savedInstanceState);
Johan Redestig76218e52016-04-19 08:29:30 +0200183 setHasOptionsMenu(true);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700184 }
185
186 @Override
187 public void onResume() {
188 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700189
190 final Bundle args = getArguments();
191 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700192 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
193 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700194 }
195 }
196
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700197 @Override
198 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700199 registerObserverIfNeeded();
200 }
201
202 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700203 protected void onUnbindPreferences() {
204 unregisterObserverIfNeeded();
205 }
206
Jason Monkb5aa73f2015-03-31 12:59:33 -0400207 public void showLoadingWhenEmpty() {
208 View loading = getView().findViewById(R.id.loading_container);
Jason Monk39b46742015-09-10 15:52:51 -0400209 setEmptyView(loading);
Jason Monkb5aa73f2015-03-31 12:59:33 -0400210 }
211
Jason Monkb37e2882016-01-11 14:27:20 -0500212 public void setLoading(boolean loading, boolean animate) {
213 View loading_container = getView().findViewById(R.id.loading_container);
214 Utils.handleLoadingContainer(loading_container, getListView(), !loading, animate);
215 }
216
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700217 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700218 if (!mIsDataSetObserverRegistered) {
219 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400220 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700221 }
Jason Monk39b46742015-09-10 15:52:51 -0400222 mCurrentRootAdapter = getListView().getAdapter();
223 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700224 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500225 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700226 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700227 }
228
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700229 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700230 if (mIsDataSetObserverRegistered) {
231 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400232 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700233 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700234 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700235 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700236 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700237 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700238
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700239 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700240 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700241 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700242 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700243 }
244
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000245 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400246 highlightPreferenceIfNeeded();
247 updateEmptyView();
248 }
249
Jason Monk39b46742015-09-10 15:52:51 -0400250 public LayoutPreference getHeaderView() {
251 return mHeader;
252 }
253
254 public LayoutPreference getFooterView() {
255 return mFooter;
256 }
257
258 protected void setHeaderView(int resource) {
259 mHeader = new LayoutPreference(getPrefContext(), resource);
Udam Sainid553abc2016-02-16 17:54:13 -0800260 addPreferenceToTop(mHeader);
261 }
262
263 protected void setHeaderView(View view) {
264 mHeader = new LayoutPreference(getPrefContext(), view);
265 addPreferenceToTop(mHeader);
266 }
267
268 private void addPreferenceToTop(LayoutPreference preference) {
269 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400270 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800271 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400272 }
273 }
274
275 protected void setFooterView(int resource) {
276 setFooterView(resource != 0 ? new LayoutPreference(getPrefContext(), resource) : null);
277 }
278
279 protected void setFooterView(View v) {
280 setFooterView(v != null ? new LayoutPreference(getPrefContext(), v) : null);
281 }
282
283 private void setFooterView(LayoutPreference footer) {
284 if (getPreferenceScreen() != null && mFooter != null) {
285 getPreferenceScreen().removePreference(mFooter);
286 }
287 if (footer != null) {
288 mFooter = footer;
Sudheer Shanka5590e2e2016-01-22 20:40:56 +0000289 mFooter.setOrder(ORDER_LAST);
Jason Monk39b46742015-09-10 15:52:51 -0400290 if (getPreferenceScreen() != null) {
291 getPreferenceScreen().addPreference(mFooter);
292 }
293 } else {
294 mFooter = null;
295 }
296 }
297
298 @Override
299 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400300 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400301 // Without ids generated, the RecyclerView won't animate changes to the preferences.
302 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
303 }
Jason Monk39b46742015-09-10 15:52:51 -0400304 super.setPreferenceScreen(preferenceScreen);
305 if (preferenceScreen != null) {
306 if (mHeader != null) {
307 preferenceScreen.addPreference(mHeader);
308 }
309 if (mFooter != null) {
310 preferenceScreen.addPreference(mFooter);
311 }
312 }
313 }
314
315 private void updateEmptyView() {
316 if (mEmptyView == null) return;
317 if (getPreferenceScreen() != null) {
318 boolean show = (getPreferenceScreen().getPreferenceCount()
319 - (mHeader != null ? 1 : 0)
320 - (mFooter != null ? 1 : 0)) <= 0;
321 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
322 } else {
323 mEmptyView.setVisibility(View.VISIBLE);
324 }
325 }
326
327 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000328 if (mEmptyView != null) {
329 mEmptyView.setVisibility(View.GONE);
330 }
Jason Monk39b46742015-09-10 15:52:51 -0400331 mEmptyView = v;
332 updateEmptyView();
333 }
334
335 public View getEmptyView() {
336 return mEmptyView;
337 }
338
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700339 /**
340 * Return a valid ListView position or -1 if none is found
341 */
342 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400343 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700344 return -1;
345 }
346
Jason Monk39b46742015-09-10 15:52:51 -0400347 RecyclerView listView = getListView();
348 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700349
350 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400351 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700352 }
353
354 return -1;
355 }
356
Jason Monk65bb0972015-12-17 10:39:44 -0500357 @Override
358 public RecyclerView.LayoutManager onCreateLayoutManager() {
359 mLayoutManager = new LinearLayoutManager(getContext());
360 return mLayoutManager;
361 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700362
Jason Monk65bb0972015-12-17 10:39:44 -0500363 @Override
364 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
365 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
366 return mAdapter;
367 }
368
Jason Monkf38fb382016-03-18 14:23:01 -0400369 protected void setAnimationAllowed(boolean animationAllowed) {
370 mAnimationAllowed = animationAllowed;
371 }
372
Jason Monk2071eda2016-02-25 13:55:48 -0500373 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
374 mPreferenceCache = new ArrayMap<String, Preference>();
375 final int N = group.getPreferenceCount();
376 for (int i = 0; i < N; i++) {
377 Preference p = group.getPreference(i);
378 if (TextUtils.isEmpty(p.getKey())) {
379 continue;
380 }
381 mPreferenceCache.put(p.getKey(), p);
382 }
383 }
384
385 protected Preference getCachedPreference(String key) {
386 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
387 }
388
389 protected void removeCachedPrefs(PreferenceGroup group) {
390 for (Preference p : mPreferenceCache.values()) {
391 group.removePreference(p);
392 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400393 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500394 }
395
Jason Monka6278442016-04-21 10:12:30 -0400396 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400397 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400398 }
399
Jason Monk65bb0972015-12-17 10:39:44 -0500400 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700401 final int position = canUseListViewForHighLighting(key);
402 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700403 mPreferenceHighlighted = true;
Jason Monk65bb0972015-12-17 10:39:44 -0500404 mLayoutManager.scrollToPosition(position);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700405
Jason Monk65bb0972015-12-17 10:39:44 -0500406 getView().postDelayed(new Runnable() {
407 @Override
408 public void run() {
409 mAdapter.highlight(position);
410 }
411 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700412 }
413 }
414
Jason Monk39b46742015-09-10 15:52:51 -0400415 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
416 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700417 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400418 final Preference preference = adapter.getItem(n);
419 final String preferenceKey = preference.getKey();
420 if (preferenceKey != null && preferenceKey.equals(key)) {
421 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700422 }
423 }
424 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700425 }
426
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700427 protected void removePreference(String key) {
428 Preference pref = findPreference(key);
429 if (pref != null) {
430 getPreferenceScreen().removePreference(pref);
431 }
432 }
433
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700434 /**
435 * Override this if you want to show a help item in the menu, by returning the resource id.
436 * @return the resource id for the help url
437 */
438 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400439 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700440 }
441
442 @Override
443 public void onCreateOptionsMenu(Menu menu, MenuInflater 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() {
604 final int metricsCategory = mDialogCreatable.getDialogMetricsCategory(mDialogId);
605 if (metricsCategory <= 0) {
606 throw new IllegalStateException("Dialog must provide a metrics category");
607 }
608 return metricsCategory;
609 }
610
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800611 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800612 public void onSaveInstanceState(Bundle outState) {
613 super.onSaveInstanceState(outState);
614 if (mParentFragment != null) {
615 outState.putInt(KEY_DIALOG_ID, mDialogId);
616 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
617 }
618 }
619
620 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700621 public void onStart() {
622 super.onStart();
623
624 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
625 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
626 }
627 }
628
629 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800630 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800631 if (savedInstanceState != null) {
632 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800633 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800634 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700635 if (mParentFragment == null) {
636 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
637 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800638 if (!(mParentFragment instanceof DialogCreatable)) {
639 throw new IllegalArgumentException(
640 (mParentFragment != null
641 ? mParentFragment.getClass().getName()
642 : mParentFragmentId)
643 + " must implement "
644 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800645 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800646 // This dialog fragment could be created from non-SettingsPreferenceFragment
647 if (mParentFragment instanceof SettingsPreferenceFragment) {
648 // restore mDialogFragment in mParentFragment
649 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
650 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800651 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800652 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700653 }
654
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800655 @Override
656 public void onCancel(DialogInterface dialog) {
657 super.onCancel(dialog);
658 if (mOnCancelListener != null) {
659 mOnCancelListener.onCancel(dialog);
660 }
661 }
662
663 @Override
664 public void onDismiss(DialogInterface dialog) {
665 super.onDismiss(dialog);
666 if (mOnDismissListener != null) {
667 mOnDismissListener.onDismiss(dialog);
668 }
669 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800670
Amith Yamasanid7993472010-08-18 13:59:28 -0700671 public int getDialogId() {
672 return mDialogId;
673 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800674
675 @Override
676 public void onDetach() {
677 super.onDetach();
678
Amith Yamasani8875ede2011-01-31 12:46:57 -0800679 // This dialog fragment could be created from non-SettingsPreferenceFragment
680 if (mParentFragment instanceof SettingsPreferenceFragment) {
681 // in case the dialog is not explicitly removed by removeDialog()
682 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
683 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
684 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800685 }
686 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700687 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700688
689 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800690 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700691 }
692
693 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800694 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700695 }
696
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700697 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700698 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400699 if (activity == null) return;
700 if (getFragmentManager().getBackStackEntryCount() > 0) {
701 getFragmentManager().popBackStack();
702 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800703 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700704 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700705 }
706
Jason Monkb7e43802016-06-06 16:01:58 -0400707 protected Intent getIntent() {
708 if (getActivity() == null) {
709 return null;
710 }
711 return getActivity().getIntent();
712 }
713
714 protected void setResult(int result, Intent intent) {
715 if (getActivity() == null) {
716 return;
717 }
718 getActivity().setResult(result, intent);
719 }
720
721 protected void setResult(int result) {
722 if (getActivity() == null) {
723 return;
724 }
725 getActivity().setResult(result);
726 }
727
Jason Monk39b46742015-09-10 15:52:51 -0400728 protected final Context getPrefContext() {
729 return getPreferenceManager().getContext();
730 }
731
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700732 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
733 int requestCode, Bundle extras) {
734 final Activity activity = getActivity();
735 if (activity instanceof SettingsActivity) {
736 SettingsActivity sa = (SettingsActivity) activity;
737 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
738 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;
766 if (v.getBackground() != null) {
767 final int centerX = v.getWidth() / 2;
768 final int centerY = v.getHeight() / 2;
769 v.getBackground().setHotspot(centerX, centerY);
770 }
771 v.setPressed(true);
772 v.setPressed(false);
773 mHighlightPosition = -1;
774 }
775 }
776 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700777}