blob: a8bd3092731ce751d723b4c2f8d8ac5489e4918b [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 */
Chris Wren8a963ba2015-03-20 10:29:14 -040055public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceFragment
56 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
136 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
137 }
138
Jason Monk91e2f892016-02-23 15:31:09 -0500139 @Override
140 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
141 super.addPreferencesFromResource(preferencesResId);
142 checkAvailablePrefs(getPreferenceScreen());
143 }
144
145 private void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
146 if (preferenceGroup == null) return;
147 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
148 Preference pref = preferenceGroup.getPreference(i);
149 if (pref instanceof SelfAvailablePreference
150 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
151 preferenceGroup.removePreference(pref);
152 } else if (pref instanceof PreferenceGroup) {
153 checkAvailablePrefs((PreferenceGroup) pref);
154 }
155 }
156 }
157
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900158 public ViewGroup getButtonBar() {
159 return mButtonBar;
160 }
161
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700162 public View setPinnedHeaderView(int layoutResId) {
163 final LayoutInflater inflater = getActivity().getLayoutInflater();
164 final View pinnedHeader =
165 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
166 setPinnedHeaderView(pinnedHeader);
167 return pinnedHeader;
168 }
169
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700170 public void setPinnedHeaderView(View pinnedHeader) {
171 mPinnedHeaderFrameLayout.addView(pinnedHeader);
172 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
173 }
174
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700175 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700176 public void onSaveInstanceState(Bundle outState) {
177 super.onSaveInstanceState(outState);
178
179 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
180 }
181
182 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700183 public void onActivityCreated(Bundle savedInstanceState) {
184 super.onActivityCreated(savedInstanceState);
Johan Redestig76218e52016-04-19 08:29:30 +0200185 setHasOptionsMenu(true);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700186 }
187
188 @Override
189 public void onResume() {
190 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700191
192 final Bundle args = getArguments();
193 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700194 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
195 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700196 }
197 }
198
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700199 @Override
200 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700201 registerObserverIfNeeded();
202 }
203
204 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700205 protected void onUnbindPreferences() {
206 unregisterObserverIfNeeded();
207 }
208
Jason Monkb5aa73f2015-03-31 12:59:33 -0400209 public void showLoadingWhenEmpty() {
210 View loading = getView().findViewById(R.id.loading_container);
Jason Monk39b46742015-09-10 15:52:51 -0400211 setEmptyView(loading);
Jason Monkb5aa73f2015-03-31 12:59:33 -0400212 }
213
Jason Monkb37e2882016-01-11 14:27:20 -0500214 public void setLoading(boolean loading, boolean animate) {
215 View loading_container = getView().findViewById(R.id.loading_container);
216 Utils.handleLoadingContainer(loading_container, getListView(), !loading, animate);
217 }
218
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700219 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700220 if (!mIsDataSetObserverRegistered) {
221 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400222 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700223 }
Jason Monk39b46742015-09-10 15:52:51 -0400224 mCurrentRootAdapter = getListView().getAdapter();
225 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700226 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500227 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700228 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700229 }
230
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700231 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700232 if (mIsDataSetObserverRegistered) {
233 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400234 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700235 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700236 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700237 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700238 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700239 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700240
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700241 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700242 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700243 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700244 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700245 }
246
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000247 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400248 highlightPreferenceIfNeeded();
249 updateEmptyView();
250 }
251
Jason Monk39b46742015-09-10 15:52:51 -0400252 public LayoutPreference getHeaderView() {
253 return mHeader;
254 }
255
256 public LayoutPreference getFooterView() {
257 return mFooter;
258 }
259
260 protected void setHeaderView(int resource) {
261 mHeader = new LayoutPreference(getPrefContext(), resource);
Udam Sainid553abc2016-02-16 17:54:13 -0800262 addPreferenceToTop(mHeader);
263 }
264
265 protected void setHeaderView(View view) {
266 mHeader = new LayoutPreference(getPrefContext(), view);
267 addPreferenceToTop(mHeader);
268 }
269
270 private void addPreferenceToTop(LayoutPreference preference) {
271 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400272 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800273 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400274 }
275 }
276
277 protected void setFooterView(int resource) {
278 setFooterView(resource != 0 ? new LayoutPreference(getPrefContext(), resource) : null);
279 }
280
281 protected void setFooterView(View v) {
282 setFooterView(v != null ? new LayoutPreference(getPrefContext(), v) : null);
283 }
284
285 private void setFooterView(LayoutPreference footer) {
286 if (getPreferenceScreen() != null && mFooter != null) {
287 getPreferenceScreen().removePreference(mFooter);
288 }
289 if (footer != null) {
290 mFooter = footer;
Sudheer Shanka5590e2e2016-01-22 20:40:56 +0000291 mFooter.setOrder(ORDER_LAST);
Jason Monk39b46742015-09-10 15:52:51 -0400292 if (getPreferenceScreen() != null) {
293 getPreferenceScreen().addPreference(mFooter);
294 }
295 } else {
296 mFooter = null;
297 }
298 }
299
300 @Override
301 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400302 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400303 // Without ids generated, the RecyclerView won't animate changes to the preferences.
304 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
305 }
Jason Monk39b46742015-09-10 15:52:51 -0400306 super.setPreferenceScreen(preferenceScreen);
307 if (preferenceScreen != null) {
308 if (mHeader != null) {
309 preferenceScreen.addPreference(mHeader);
310 }
311 if (mFooter != null) {
312 preferenceScreen.addPreference(mFooter);
313 }
314 }
315 }
316
317 private void updateEmptyView() {
318 if (mEmptyView == null) return;
319 if (getPreferenceScreen() != null) {
320 boolean show = (getPreferenceScreen().getPreferenceCount()
321 - (mHeader != null ? 1 : 0)
322 - (mFooter != null ? 1 : 0)) <= 0;
323 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
324 } else {
325 mEmptyView.setVisibility(View.VISIBLE);
326 }
327 }
328
329 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000330 if (mEmptyView != null) {
331 mEmptyView.setVisibility(View.GONE);
332 }
Jason Monk39b46742015-09-10 15:52:51 -0400333 mEmptyView = v;
334 updateEmptyView();
335 }
336
337 public View getEmptyView() {
338 return mEmptyView;
339 }
340
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700341 /**
342 * Return a valid ListView position or -1 if none is found
343 */
344 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400345 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700346 return -1;
347 }
348
Jason Monk39b46742015-09-10 15:52:51 -0400349 RecyclerView listView = getListView();
350 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700351
352 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400353 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700354 }
355
356 return -1;
357 }
358
Jason Monk65bb0972015-12-17 10:39:44 -0500359 @Override
360 public RecyclerView.LayoutManager onCreateLayoutManager() {
361 mLayoutManager = new LinearLayoutManager(getContext());
362 return mLayoutManager;
363 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700364
Jason Monk65bb0972015-12-17 10:39:44 -0500365 @Override
366 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
367 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
368 return mAdapter;
369 }
370
Jason Monkf38fb382016-03-18 14:23:01 -0400371 protected void setAnimationAllowed(boolean animationAllowed) {
372 mAnimationAllowed = animationAllowed;
373 }
374
Jason Monk2071eda2016-02-25 13:55:48 -0500375 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
376 mPreferenceCache = new ArrayMap<String, Preference>();
377 final int N = group.getPreferenceCount();
378 for (int i = 0; i < N; i++) {
379 Preference p = group.getPreference(i);
380 if (TextUtils.isEmpty(p.getKey())) {
381 continue;
382 }
383 mPreferenceCache.put(p.getKey(), p);
384 }
385 }
386
387 protected Preference getCachedPreference(String key) {
388 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
389 }
390
391 protected void removeCachedPrefs(PreferenceGroup group) {
392 for (Preference p : mPreferenceCache.values()) {
393 group.removePreference(p);
394 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400395 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500396 }
397
Jason Monka6278442016-04-21 10:12:30 -0400398 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400399 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400400 }
401
Jason Monk65bb0972015-12-17 10:39:44 -0500402 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700403 final int position = canUseListViewForHighLighting(key);
404 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700405 mPreferenceHighlighted = true;
Jason Monk65bb0972015-12-17 10:39:44 -0500406 mLayoutManager.scrollToPosition(position);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700407
Jason Monk65bb0972015-12-17 10:39:44 -0500408 getView().postDelayed(new Runnable() {
409 @Override
410 public void run() {
411 mAdapter.highlight(position);
412 }
413 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700414 }
415 }
416
Jason Monk39b46742015-09-10 15:52:51 -0400417 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
418 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700419 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400420 final Preference preference = adapter.getItem(n);
421 final String preferenceKey = preference.getKey();
422 if (preferenceKey != null && preferenceKey.equals(key)) {
423 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700424 }
425 }
426 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700427 }
428
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700429 protected void removePreference(String key) {
430 Preference pref = findPreference(key);
431 if (pref != null) {
432 getPreferenceScreen().removePreference(pref);
433 }
434 }
435
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700436 /**
437 * Override this if you want to show a help item in the menu, by returning the resource id.
438 * @return the resource id for the help url
439 */
440 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400441 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700442 }
443
444 @Override
445 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400446 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400447 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700448 }
449 }
450
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700451 /*
452 * The name is intentionally made different from Activity#finish(), so that
453 * users won't misunderstand its meaning.
454 */
455 public final void finishFragment() {
456 getActivity().onBackPressed();
457 }
458
Amith Yamasanid7993472010-08-18 13:59:28 -0700459 // Some helpers for functions used by the settings fragments when they were activities
460
461 /**
462 * Returns the ContentResolver from the owning Activity.
463 */
464 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700465 Context context = getActivity();
466 if (context != null) {
467 mContentResolver = context.getContentResolver();
468 }
469 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700470 }
471
472 /**
473 * Returns the specified system service from the owning Activity.
474 */
475 protected Object getSystemService(final String name) {
476 return getActivity().getSystemService(name);
477 }
478
479 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700480 * Returns the PackageManager from the owning Activity.
481 */
482 protected PackageManager getPackageManager() {
483 return getActivity().getPackageManager();
484 }
485
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800486 @Override
487 public void onDetach() {
488 if (isRemoving()) {
489 if (mDialogFragment != null) {
490 mDialogFragment.dismiss();
491 mDialogFragment = null;
492 }
493 }
494 super.onDetach();
495 }
496
Amith Yamasanid7993472010-08-18 13:59:28 -0700497 // Dialog management
498
499 protected void showDialog(int dialogId) {
500 if (mDialogFragment != null) {
501 Log.e(TAG, "Old dialog fragment not null!");
502 }
503 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800504 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700505 }
506
507 public Dialog onCreateDialog(int dialogId) {
508 return null;
509 }
510
511 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800512 // mDialogFragment may not be visible yet in parent fragment's onResume().
513 // To be able to dismiss dialog at that time, don't check
514 // mDialogFragment.isVisible().
515 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400516 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700517 }
518 mDialogFragment = null;
519 }
520
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800521 /**
522 * Sets the OnCancelListener of the dialog shown. This method can only be
523 * called after showDialog(int) and before removeDialog(int). The method
524 * does nothing otherwise.
525 */
526 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
527 if (mDialogFragment != null) {
528 mDialogFragment.mOnCancelListener = listener;
529 }
530 }
531
532 /**
533 * Sets the OnDismissListener of the dialog shown. This method can only be
534 * called after showDialog(int) and before removeDialog(int). The method
535 * does nothing otherwise.
536 */
537 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
538 if (mDialogFragment != null) {
539 mDialogFragment.mOnDismissListener = listener;
540 }
541 }
542
Amith Yamasanic861cf82012-10-02 14:51:46 -0700543 public void onDialogShowing() {
544 // override in subclass to attach a dismiss listener, for instance
545 }
546
Jason Monk39b46742015-09-10 15:52:51 -0400547 @Override
548 public void onDisplayPreferenceDialog(Preference preference) {
549 if (preference.getKey() == null) {
550 // Auto-key preferences that don't have a key, so the dialog can find them.
551 preference.setKey(UUID.randomUUID().toString());
552 }
553 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000554 if (preference instanceof RestrictedListPreference) {
555 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
556 .newInstance(preference.getKey());
557 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400558 f = CustomListPreference.CustomListPreferenceDialogFragment
559 .newInstance(preference.getKey());
560 } else if (preference instanceof CustomDialogPreference) {
561 f = CustomDialogPreference.CustomPreferenceDialogFragment
562 .newInstance(preference.getKey());
563 } else if (preference instanceof CustomEditTextPreference) {
564 f = CustomEditTextPreference.CustomPreferenceDialogFragment
565 .newInstance(preference.getKey());
566 } else {
567 super.onDisplayPreferenceDialog(preference);
568 return;
569 }
570 f.setTargetFragment(this, 0);
571 f.show(getFragmentManager(), "dialog_preference");
572 onDialogShowing();
573 }
574
Amith Yamasani43c69782010-12-01 09:04:36 -0800575 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800576 private static final String KEY_DIALOG_ID = "key_dialog_id";
577 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
578
Amith Yamasanid7993472010-08-18 13:59:28 -0700579 private int mDialogId;
580
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800581 private Fragment mParentFragment;
582
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800583 private DialogInterface.OnCancelListener mOnCancelListener;
584 private DialogInterface.OnDismissListener mOnDismissListener;
585
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800586 public SettingsDialogFragment() {
587 /* do nothing */
588 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700589
Amith Yamasani43c69782010-12-01 09:04:36 -0800590 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700591 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800592 if (!(fragment instanceof Fragment)) {
593 throw new IllegalArgumentException("fragment argument must be an instance of "
594 + Fragment.class.getName());
595 }
596 mParentFragment = (Fragment) fragment;
597 }
598
599 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800600 public void onSaveInstanceState(Bundle outState) {
601 super.onSaveInstanceState(outState);
602 if (mParentFragment != null) {
603 outState.putInt(KEY_DIALOG_ID, mDialogId);
604 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
605 }
606 }
607
608 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700609 public void onStart() {
610 super.onStart();
611
612 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
613 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
614 }
615 }
616
617 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800618 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800619 if (savedInstanceState != null) {
620 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800621 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800622 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700623 if (mParentFragment == null) {
624 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
625 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800626 if (!(mParentFragment instanceof DialogCreatable)) {
627 throw new IllegalArgumentException(
628 (mParentFragment != null
629 ? mParentFragment.getClass().getName()
630 : mParentFragmentId)
631 + " must implement "
632 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800633 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800634 // This dialog fragment could be created from non-SettingsPreferenceFragment
635 if (mParentFragment instanceof SettingsPreferenceFragment) {
636 // restore mDialogFragment in mParentFragment
637 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
638 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800639 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800640 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700641 }
642
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800643 @Override
644 public void onCancel(DialogInterface dialog) {
645 super.onCancel(dialog);
646 if (mOnCancelListener != null) {
647 mOnCancelListener.onCancel(dialog);
648 }
649 }
650
651 @Override
652 public void onDismiss(DialogInterface dialog) {
653 super.onDismiss(dialog);
654 if (mOnDismissListener != null) {
655 mOnDismissListener.onDismiss(dialog);
656 }
657 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800658
Amith Yamasanid7993472010-08-18 13:59:28 -0700659 public int getDialogId() {
660 return mDialogId;
661 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800662
663 @Override
664 public void onDetach() {
665 super.onDetach();
666
Amith Yamasani8875ede2011-01-31 12:46:57 -0800667 // This dialog fragment could be created from non-SettingsPreferenceFragment
668 if (mParentFragment instanceof SettingsPreferenceFragment) {
669 // in case the dialog is not explicitly removed by removeDialog()
670 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
671 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
672 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800673 }
674 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700675 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700676
677 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800678 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700679 }
680
681 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800682 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700683 }
684
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700685 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700686 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400687 if (activity == null) return;
688 if (getFragmentManager().getBackStackEntryCount() > 0) {
689 getFragmentManager().popBackStack();
690 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800691 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700692 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700693 }
694
Jason Monkb7e43802016-06-06 16:01:58 -0400695 protected Intent getIntent() {
696 if (getActivity() == null) {
697 return null;
698 }
699 return getActivity().getIntent();
700 }
701
702 protected void setResult(int result, Intent intent) {
703 if (getActivity() == null) {
704 return;
705 }
706 getActivity().setResult(result, intent);
707 }
708
709 protected void setResult(int result) {
710 if (getActivity() == null) {
711 return;
712 }
713 getActivity().setResult(result);
714 }
715
Jason Monk39b46742015-09-10 15:52:51 -0400716 protected final Context getPrefContext() {
717 return getPreferenceManager().getContext();
718 }
719
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700720 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
721 int requestCode, Bundle extras) {
722 final Activity activity = getActivity();
723 if (activity instanceof SettingsActivity) {
724 SettingsActivity sa = (SettingsActivity) activity;
725 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
726 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700727 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700728 Log.w(TAG,
729 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
730 + "launch the given Fragment (name: " + fragmentClass
731 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700732 return false;
733 }
734 }
Jason Monk65bb0972015-12-17 10:39:44 -0500735
736 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
737
738 private int mHighlightPosition = -1;
739
740 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
741 super(preferenceGroup);
742 }
743
744 public void highlight(int position) {
745 mHighlightPosition = position;
746 notifyDataSetChanged();
747 }
748
749 @Override
750 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
751 super.onBindViewHolder(holder, position);
752 if (position == mHighlightPosition) {
753 View v = holder.itemView;
754 if (v.getBackground() != null) {
755 final int centerX = v.getWidth() / 2;
756 final int centerY = v.getHeight() / 2;
757 v.getBackground().setHotspot(centerX, centerY);
758 }
759 v.setPressed(true);
760 v.setPressed(false);
761 mHighlightPosition = -1;
762 }
763 }
764 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700765}