blob: a79045a7e5bef76b38e187e3020cb2bc4aaaca0c [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;
Suprabh Shuklab84720c2016-04-05 14:37:20 -070048import com.android.settingslib.HelpUtils;
John Spurlockb8e02b82015-04-15 21:15:55 -040049
Jason Monk39b46742015-09-10 15:52:51 -040050import java.util.UUID;
51
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070052/**
Amith Yamasanid7993472010-08-18 13:59:28 -070053 * Base class for Settings fragments, with some helper functions and dialog management.
54 */
Fan Zhangbf4be772016-08-04 16:05:55 -070055public abstract class SettingsPreferenceFragment extends InstrumentedFragment
Chris Wren8a963ba2015-03-20 10:29:14 -040056 implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070057
Anna Galusza0285c802016-01-29 17:32:19 -080058 /**
59 * The Help Uri Resource key. This can be passed as an extra argument when creating the
60 * Fragment.
61 **/
62 public static final String HELP_URI_RESOURCE_KEY = "help_uri_resource";
63
Jason Monk65bb0972015-12-17 10:39:44 -050064 private static final String TAG = "SettingsPreference";
Amith Yamasanid7993472010-08-18 13:59:28 -070065
Fabrice Di Meglioeced7802014-09-04 13:01:55 -070066 private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070067
68 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070069
Amith Yamasanid7993472010-08-18 13:59:28 -070070 private SettingsDialogFragment mDialogFragment;
71
Jason Monk23acc2b2015-04-14 15:06:39 -040072 private String mHelpUri;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070073
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000074 private static final int ORDER_FIRST = -1;
75 private static final int ORDER_LAST = Integer.MAX_VALUE -1;
76
Amith Yamasani350938e2013-04-09 10:22:47 -070077 // Cache the content resolver for async callbacks
78 private ContentResolver mContentResolver;
79
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070080 private String mPreferenceKey;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070081 private boolean mPreferenceHighlighted = false;
82
Jason Monk39b46742015-09-10 15:52:51 -040083 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070084 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040085 private RecyclerView.AdapterDataObserver mDataSetObserver =
86 new RecyclerView.AdapterDataObserver() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070087 @Override
88 public void onChanged() {
Jason Monk39b46742015-09-10 15:52:51 -040089 onDataSetChanged();
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070090 }
91 };
92
Fabrice Di Meglio86159282014-07-21 16:02:27 -070093 private ViewGroup mPinnedHeaderFrameLayout;
Daichi Hirono5e76cdc2015-07-08 11:38:55 +090094 private ViewGroup mButtonBar;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070095
Jason Monk39b46742015-09-10 15:52:51 -040096 private LayoutPreference mHeader;
97
98 private LayoutPreference mFooter;
99 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -0500100 private LinearLayoutManager mLayoutManager;
101 private HighlightablePreferenceGroupAdapter mAdapter;
Jason Monk2071eda2016-02-25 13:55:48 -0500102 private ArrayMap<String, Preference> mPreferenceCache;
Jason Monkf38fb382016-03-18 14:23:01 -0400103 private boolean mAnimationAllowed;
Jason Monk39b46742015-09-10 15:52:51 -0400104
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700105 @Override
106 public void onCreate(Bundle icicle) {
107 super.onCreate(icicle);
108
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700109 if (icicle != null) {
110 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
111 }
112
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700113 // Prepare help url and enable menu if necessary
Anna Galusza0285c802016-01-29 17:32:19 -0800114 Bundle arguments = getArguments();
115 int helpResource;
116 if (arguments != null && arguments.containsKey(HELP_URI_RESOURCE_KEY)) {
117 helpResource = arguments.getInt(HELP_URI_RESOURCE_KEY);
118 } else {
119 helpResource = getHelpResource();
120 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700121 if (helpResource != 0) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400122 mHelpUri = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700123 }
124 }
125
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700126 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700127 public View onCreateView(LayoutInflater inflater, ViewGroup container,
128 Bundle savedInstanceState) {
129 final View root = super.onCreateView(inflater, container, savedInstanceState);
130 mPinnedHeaderFrameLayout = (ViewGroup) root.findViewById(R.id.pinned_header);
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900131 mButtonBar = (ViewGroup) root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700132 return root;
133 }
134
Jason Monk39b46742015-09-10 15:52:51 -0400135 @Override
Jason Monk91e2f892016-02-23 15:31:09 -0500136 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
137 super.addPreferencesFromResource(preferencesResId);
138 checkAvailablePrefs(getPreferenceScreen());
139 }
140
141 private void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
142 if (preferenceGroup == null) return;
143 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
144 Preference pref = preferenceGroup.getPreference(i);
145 if (pref instanceof SelfAvailablePreference
146 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
147 preferenceGroup.removePreference(pref);
148 } else if (pref instanceof PreferenceGroup) {
149 checkAvailablePrefs((PreferenceGroup) pref);
150 }
151 }
152 }
153
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900154 public ViewGroup getButtonBar() {
155 return mButtonBar;
156 }
157
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700158 public View setPinnedHeaderView(int layoutResId) {
159 final LayoutInflater inflater = getActivity().getLayoutInflater();
160 final View pinnedHeader =
161 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
162 setPinnedHeaderView(pinnedHeader);
163 return pinnedHeader;
164 }
165
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700166 public void setPinnedHeaderView(View pinnedHeader) {
167 mPinnedHeaderFrameLayout.addView(pinnedHeader);
168 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
169 }
170
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700171 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700172 public void onSaveInstanceState(Bundle outState) {
173 super.onSaveInstanceState(outState);
174
175 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
176 }
177
178 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700179 public void onActivityCreated(Bundle savedInstanceState) {
180 super.onActivityCreated(savedInstanceState);
Johan Redestig76218e52016-04-19 08:29:30 +0200181 setHasOptionsMenu(true);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700182 }
183
184 @Override
185 public void onResume() {
186 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700187
188 final Bundle args = getArguments();
189 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700190 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
191 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700192 }
193 }
194
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700195 @Override
196 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700197 registerObserverIfNeeded();
198 }
199
200 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700201 protected void onUnbindPreferences() {
202 unregisterObserverIfNeeded();
203 }
204
Jason Monkb5aa73f2015-03-31 12:59:33 -0400205 public void showLoadingWhenEmpty() {
206 View loading = getView().findViewById(R.id.loading_container);
Jason Monk39b46742015-09-10 15:52:51 -0400207 setEmptyView(loading);
Jason Monkb5aa73f2015-03-31 12:59:33 -0400208 }
209
Jason Monkb37e2882016-01-11 14:27:20 -0500210 public void setLoading(boolean loading, boolean animate) {
211 View loading_container = getView().findViewById(R.id.loading_container);
212 Utils.handleLoadingContainer(loading_container, getListView(), !loading, animate);
213 }
214
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700215 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700216 if (!mIsDataSetObserverRegistered) {
217 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400218 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700219 }
Jason Monk39b46742015-09-10 15:52:51 -0400220 mCurrentRootAdapter = getListView().getAdapter();
221 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700222 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500223 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700224 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700225 }
226
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700227 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700228 if (mIsDataSetObserverRegistered) {
229 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400230 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700231 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700232 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700233 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700234 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700235 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700236
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700237 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700238 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700239 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700240 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700241 }
242
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000243 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400244 highlightPreferenceIfNeeded();
245 updateEmptyView();
246 }
247
Jason Monk39b46742015-09-10 15:52:51 -0400248 public LayoutPreference getHeaderView() {
249 return mHeader;
250 }
251
252 public LayoutPreference getFooterView() {
253 return mFooter;
254 }
255
256 protected void setHeaderView(int resource) {
257 mHeader = new LayoutPreference(getPrefContext(), resource);
Udam Sainid553abc2016-02-16 17:54:13 -0800258 addPreferenceToTop(mHeader);
259 }
260
261 protected void setHeaderView(View view) {
262 mHeader = new LayoutPreference(getPrefContext(), view);
263 addPreferenceToTop(mHeader);
264 }
265
266 private void addPreferenceToTop(LayoutPreference preference) {
267 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400268 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800269 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400270 }
271 }
272
273 protected void setFooterView(int resource) {
274 setFooterView(resource != 0 ? new LayoutPreference(getPrefContext(), resource) : null);
275 }
276
277 protected void setFooterView(View v) {
278 setFooterView(v != null ? new LayoutPreference(getPrefContext(), v) : null);
279 }
280
281 private void setFooterView(LayoutPreference footer) {
282 if (getPreferenceScreen() != null && mFooter != null) {
283 getPreferenceScreen().removePreference(mFooter);
284 }
285 if (footer != null) {
286 mFooter = footer;
Sudheer Shanka5590e2e2016-01-22 20:40:56 +0000287 mFooter.setOrder(ORDER_LAST);
Jason Monk39b46742015-09-10 15:52:51 -0400288 if (getPreferenceScreen() != null) {
289 getPreferenceScreen().addPreference(mFooter);
290 }
291 } else {
292 mFooter = null;
293 }
294 }
295
296 @Override
297 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400298 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400299 // Without ids generated, the RecyclerView won't animate changes to the preferences.
300 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
301 }
Jason Monk39b46742015-09-10 15:52:51 -0400302 super.setPreferenceScreen(preferenceScreen);
303 if (preferenceScreen != null) {
304 if (mHeader != null) {
305 preferenceScreen.addPreference(mHeader);
306 }
307 if (mFooter != null) {
308 preferenceScreen.addPreference(mFooter);
309 }
310 }
311 }
312
313 private void updateEmptyView() {
314 if (mEmptyView == null) return;
315 if (getPreferenceScreen() != null) {
316 boolean show = (getPreferenceScreen().getPreferenceCount()
317 - (mHeader != null ? 1 : 0)
318 - (mFooter != null ? 1 : 0)) <= 0;
319 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
320 } else {
321 mEmptyView.setVisibility(View.VISIBLE);
322 }
323 }
324
325 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000326 if (mEmptyView != null) {
327 mEmptyView.setVisibility(View.GONE);
328 }
Jason Monk39b46742015-09-10 15:52:51 -0400329 mEmptyView = v;
330 updateEmptyView();
331 }
332
333 public View getEmptyView() {
334 return mEmptyView;
335 }
336
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700337 /**
338 * Return a valid ListView position or -1 if none is found
339 */
340 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400341 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700342 return -1;
343 }
344
Jason Monk39b46742015-09-10 15:52:51 -0400345 RecyclerView listView = getListView();
346 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700347
348 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400349 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700350 }
351
352 return -1;
353 }
354
Jason Monk65bb0972015-12-17 10:39:44 -0500355 @Override
356 public RecyclerView.LayoutManager onCreateLayoutManager() {
357 mLayoutManager = new LinearLayoutManager(getContext());
358 return mLayoutManager;
359 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700360
Jason Monk65bb0972015-12-17 10:39:44 -0500361 @Override
362 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
363 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
364 return mAdapter;
365 }
366
Jason Monkf38fb382016-03-18 14:23:01 -0400367 protected void setAnimationAllowed(boolean animationAllowed) {
368 mAnimationAllowed = animationAllowed;
369 }
370
Jason Monk2071eda2016-02-25 13:55:48 -0500371 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
372 mPreferenceCache = new ArrayMap<String, Preference>();
373 final int N = group.getPreferenceCount();
374 for (int i = 0; i < N; i++) {
375 Preference p = group.getPreference(i);
376 if (TextUtils.isEmpty(p.getKey())) {
377 continue;
378 }
379 mPreferenceCache.put(p.getKey(), p);
380 }
381 }
382
383 protected Preference getCachedPreference(String key) {
384 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
385 }
386
387 protected void removeCachedPrefs(PreferenceGroup group) {
388 for (Preference p : mPreferenceCache.values()) {
389 group.removePreference(p);
390 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400391 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500392 }
393
Jason Monka6278442016-04-21 10:12:30 -0400394 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400395 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400396 }
397
Jason Monk65bb0972015-12-17 10:39:44 -0500398 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700399 final int position = canUseListViewForHighLighting(key);
400 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700401 mPreferenceHighlighted = true;
Jason Monk65bb0972015-12-17 10:39:44 -0500402 mLayoutManager.scrollToPosition(position);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700403
Jason Monk65bb0972015-12-17 10:39:44 -0500404 getView().postDelayed(new Runnable() {
405 @Override
406 public void run() {
407 mAdapter.highlight(position);
408 }
409 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700410 }
411 }
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
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700425 protected void removePreference(String key) {
426 Preference pref = findPreference(key);
427 if (pref != null) {
428 getPreferenceScreen().removePreference(pref);
429 }
430 }
431
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700432 /**
433 * Override this if you want to show a help item in the menu, by returning the resource id.
434 * @return the resource id for the help url
435 */
436 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400437 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700438 }
439
440 @Override
441 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400442 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400443 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700444 }
445 }
446
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700447 /*
448 * The name is intentionally made different from Activity#finish(), so that
449 * users won't misunderstand its meaning.
450 */
451 public final void finishFragment() {
452 getActivity().onBackPressed();
453 }
454
Amith Yamasanid7993472010-08-18 13:59:28 -0700455 // Some helpers for functions used by the settings fragments when they were activities
456
457 /**
458 * Returns the ContentResolver from the owning Activity.
459 */
460 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700461 Context context = getActivity();
462 if (context != null) {
463 mContentResolver = context.getContentResolver();
464 }
465 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700466 }
467
468 /**
469 * Returns the specified system service from the owning Activity.
470 */
471 protected Object getSystemService(final String name) {
472 return getActivity().getSystemService(name);
473 }
474
475 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700476 * Returns the PackageManager from the owning Activity.
477 */
478 protected PackageManager getPackageManager() {
479 return getActivity().getPackageManager();
480 }
481
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800482 @Override
483 public void onDetach() {
484 if (isRemoving()) {
485 if (mDialogFragment != null) {
486 mDialogFragment.dismiss();
487 mDialogFragment = null;
488 }
489 }
490 super.onDetach();
491 }
492
Amith Yamasanid7993472010-08-18 13:59:28 -0700493 // Dialog management
494
495 protected void showDialog(int dialogId) {
496 if (mDialogFragment != null) {
497 Log.e(TAG, "Old dialog fragment not null!");
498 }
499 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800500 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700501 }
502
503 public Dialog onCreateDialog(int dialogId) {
504 return null;
505 }
506
507 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800508 // mDialogFragment may not be visible yet in parent fragment's onResume().
509 // To be able to dismiss dialog at that time, don't check
510 // mDialogFragment.isVisible().
511 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400512 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700513 }
514 mDialogFragment = null;
515 }
516
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800517 /**
518 * Sets the OnCancelListener of the dialog shown. This method can only be
519 * called after showDialog(int) and before removeDialog(int). The method
520 * does nothing otherwise.
521 */
522 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
523 if (mDialogFragment != null) {
524 mDialogFragment.mOnCancelListener = listener;
525 }
526 }
527
528 /**
529 * Sets the OnDismissListener of the dialog shown. This method can only be
530 * called after showDialog(int) and before removeDialog(int). The method
531 * does nothing otherwise.
532 */
533 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
534 if (mDialogFragment != null) {
535 mDialogFragment.mOnDismissListener = listener;
536 }
537 }
538
Amith Yamasanic861cf82012-10-02 14:51:46 -0700539 public void onDialogShowing() {
540 // override in subclass to attach a dismiss listener, for instance
541 }
542
Jason Monk39b46742015-09-10 15:52:51 -0400543 @Override
544 public void onDisplayPreferenceDialog(Preference preference) {
545 if (preference.getKey() == null) {
546 // Auto-key preferences that don't have a key, so the dialog can find them.
547 preference.setKey(UUID.randomUUID().toString());
548 }
549 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000550 if (preference instanceof RestrictedListPreference) {
551 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
552 .newInstance(preference.getKey());
553 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400554 f = CustomListPreference.CustomListPreferenceDialogFragment
555 .newInstance(preference.getKey());
556 } else if (preference instanceof CustomDialogPreference) {
557 f = CustomDialogPreference.CustomPreferenceDialogFragment
558 .newInstance(preference.getKey());
559 } else if (preference instanceof CustomEditTextPreference) {
560 f = CustomEditTextPreference.CustomPreferenceDialogFragment
561 .newInstance(preference.getKey());
562 } else {
563 super.onDisplayPreferenceDialog(preference);
564 return;
565 }
566 f.setTargetFragment(this, 0);
567 f.show(getFragmentManager(), "dialog_preference");
568 onDialogShowing();
569 }
570
Amith Yamasani43c69782010-12-01 09:04:36 -0800571 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800572 private static final String KEY_DIALOG_ID = "key_dialog_id";
573 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
574
Amith Yamasanid7993472010-08-18 13:59:28 -0700575 private int mDialogId;
576
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800577 private Fragment mParentFragment;
578
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800579 private DialogInterface.OnCancelListener mOnCancelListener;
580 private DialogInterface.OnDismissListener mOnDismissListener;
581
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800582 public SettingsDialogFragment() {
583 /* do nothing */
584 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700585
Amith Yamasani43c69782010-12-01 09:04:36 -0800586 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700587 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800588 if (!(fragment instanceof Fragment)) {
589 throw new IllegalArgumentException("fragment argument must be an instance of "
590 + Fragment.class.getName());
591 }
592 mParentFragment = (Fragment) fragment;
593 }
594
595 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800596 public void onSaveInstanceState(Bundle outState) {
597 super.onSaveInstanceState(outState);
598 if (mParentFragment != null) {
599 outState.putInt(KEY_DIALOG_ID, mDialogId);
600 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
601 }
602 }
603
604 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700605 public void onStart() {
606 super.onStart();
607
608 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
609 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
610 }
611 }
612
613 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800614 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800615 if (savedInstanceState != null) {
616 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800617 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800618 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700619 if (mParentFragment == null) {
620 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
621 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800622 if (!(mParentFragment instanceof DialogCreatable)) {
623 throw new IllegalArgumentException(
624 (mParentFragment != null
625 ? mParentFragment.getClass().getName()
626 : mParentFragmentId)
627 + " must implement "
628 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800629 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800630 // This dialog fragment could be created from non-SettingsPreferenceFragment
631 if (mParentFragment instanceof SettingsPreferenceFragment) {
632 // restore mDialogFragment in mParentFragment
633 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
634 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800635 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800636 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700637 }
638
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800639 @Override
640 public void onCancel(DialogInterface dialog) {
641 super.onCancel(dialog);
642 if (mOnCancelListener != null) {
643 mOnCancelListener.onCancel(dialog);
644 }
645 }
646
647 @Override
648 public void onDismiss(DialogInterface dialog) {
649 super.onDismiss(dialog);
650 if (mOnDismissListener != null) {
651 mOnDismissListener.onDismiss(dialog);
652 }
653 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800654
Amith Yamasanid7993472010-08-18 13:59:28 -0700655 public int getDialogId() {
656 return mDialogId;
657 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800658
659 @Override
660 public void onDetach() {
661 super.onDetach();
662
Amith Yamasani8875ede2011-01-31 12:46:57 -0800663 // This dialog fragment could be created from non-SettingsPreferenceFragment
664 if (mParentFragment instanceof SettingsPreferenceFragment) {
665 // in case the dialog is not explicitly removed by removeDialog()
666 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
667 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
668 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800669 }
670 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700671 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700672
673 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800674 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700675 }
676
677 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800678 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700679 }
680
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700681 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700682 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400683 if (activity == null) return;
684 if (getFragmentManager().getBackStackEntryCount() > 0) {
685 getFragmentManager().popBackStack();
686 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800687 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700688 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700689 }
690
Jason Monkb7e43802016-06-06 16:01:58 -0400691 protected Intent getIntent() {
692 if (getActivity() == null) {
693 return null;
694 }
695 return getActivity().getIntent();
696 }
697
698 protected void setResult(int result, Intent intent) {
699 if (getActivity() == null) {
700 return;
701 }
702 getActivity().setResult(result, intent);
703 }
704
705 protected void setResult(int result) {
706 if (getActivity() == null) {
707 return;
708 }
709 getActivity().setResult(result);
710 }
711
Jason Monk39b46742015-09-10 15:52:51 -0400712 protected final Context getPrefContext() {
713 return getPreferenceManager().getContext();
714 }
715
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700716 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
717 int requestCode, Bundle extras) {
718 final Activity activity = getActivity();
719 if (activity instanceof SettingsActivity) {
720 SettingsActivity sa = (SettingsActivity) activity;
721 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
722 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700723 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700724 Log.w(TAG,
725 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
726 + "launch the given Fragment (name: " + fragmentClass
727 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700728 return false;
729 }
730 }
Jason Monk65bb0972015-12-17 10:39:44 -0500731
732 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
733
734 private int mHighlightPosition = -1;
735
736 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
737 super(preferenceGroup);
738 }
739
740 public void highlight(int position) {
741 mHighlightPosition = position;
742 notifyDataSetChanged();
743 }
744
745 @Override
746 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
747 super.onBindViewHolder(holder, position);
748 if (position == mHighlightPosition) {
749 View v = holder.itemView;
750 if (v.getBackground() != null) {
751 final int centerX = v.getWidth() / 2;
752 final int centerY = v.getHeight() / 2;
753 v.getBackground().setHotspot(centerX, centerY);
754 }
755 v.setPressed(true);
756 v.setPressed(false);
757 mHighlightPosition = -1;
758 }
759 }
760 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700761}