blob: e0a284452c41732e58f59d1692c2eceda47eeabd [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;
Amith Yamasanid7993472010-08-18 13:59:28 -070026import android.content.pm.PackageManager;
Amith Yamasanid7993472010-08-18 13:59:28 -070027import android.os.Bundle;
Jason Monk91e2f892016-02-23 15:31:09 -050028import android.support.annotation.XmlRes;
Jason Monk39b46742015-09-10 15:52:51 -040029import android.support.v7.preference.Preference;
Jason Monk65bb0972015-12-17 10:39:44 -050030import android.support.v7.preference.PreferenceGroup;
Jason Monk39b46742015-09-10 15:52:51 -040031import android.support.v7.preference.PreferenceGroupAdapter;
32import android.support.v7.preference.PreferenceScreen;
Jason Monk65bb0972015-12-17 10:39:44 -050033import android.support.v7.preference.PreferenceViewHolder;
34import android.support.v7.widget.LinearLayoutManager;
Jason Monk39b46742015-09-10 15:52:51 -040035import android.support.v7.widget.RecyclerView;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070036import android.text.TextUtils;
Jason Monk2071eda2016-02-25 13:55:48 -050037import android.util.ArrayMap;
Amith Yamasanid7993472010-08-18 13:59:28 -070038import android.util.Log;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070039import android.view.LayoutInflater;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070040import android.view.Menu;
41import android.view.MenuInflater;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070042import android.view.View;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070043import android.view.ViewGroup;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070044import android.widget.Button;
Jason Monk39b46742015-09-10 15:52:51 -040045import com.android.settings.applications.LayoutPreference;
John Spurlockb8e02b82015-04-15 21:15:55 -040046import com.android.settings.widget.FloatingActionButton;
Suprabh Shuklab84720c2016-04-05 14:37:20 -070047import com.android.settingslib.HelpUtils;
John Spurlockb8e02b82015-04-15 21:15:55 -040048
Jason Monk39b46742015-09-10 15:52:51 -040049import java.util.UUID;
50
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070051/**
Amith Yamasanid7993472010-08-18 13:59:28 -070052 * Base class for Settings fragments, with some helper functions and dialog management.
53 */
Chris Wren8a963ba2015-03-20 10:29:14 -040054public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceFragment
55 implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070056
Anna Galusza0285c802016-01-29 17:32:19 -080057 /**
58 * The Help Uri Resource key. This can be passed as an extra argument when creating the
59 * Fragment.
60 **/
61 public static final String HELP_URI_RESOURCE_KEY = "help_uri_resource";
62
Jason Monk65bb0972015-12-17 10:39:44 -050063 private static final String TAG = "SettingsPreference";
Amith Yamasanid7993472010-08-18 13:59:28 -070064
Fabrice Di Meglioeced7802014-09-04 13:01:55 -070065 private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070066
67 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070068
Amith Yamasanid7993472010-08-18 13:59:28 -070069 private SettingsDialogFragment mDialogFragment;
70
Jason Monk23acc2b2015-04-14 15:06:39 -040071 private String mHelpUri;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070072
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000073 private static final int ORDER_FIRST = -1;
74 private static final int ORDER_LAST = Integer.MAX_VALUE -1;
75
Amith Yamasani350938e2013-04-09 10:22:47 -070076 // Cache the content resolver for async callbacks
77 private ContentResolver mContentResolver;
78
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070079 private String mPreferenceKey;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070080 private boolean mPreferenceHighlighted = false;
81
Jason Monk39b46742015-09-10 15:52:51 -040082 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070083 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040084 private RecyclerView.AdapterDataObserver mDataSetObserver =
85 new RecyclerView.AdapterDataObserver() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070086 @Override
87 public void onChanged() {
Jason Monk39b46742015-09-10 15:52:51 -040088 onDataSetChanged();
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070089 }
90 };
91
Fabrice Di Meglio86159282014-07-21 16:02:27 -070092 private ViewGroup mPinnedHeaderFrameLayout;
John Spurlockb8e02b82015-04-15 21:15:55 -040093 private FloatingActionButton mFloatingActionButton;
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);
John Spurlockb8e02b82015-04-15 21:15:55 -0400131 mFloatingActionButton = (FloatingActionButton) root.findViewById(R.id.fab);
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900132 mButtonBar = (ViewGroup) root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700133 return root;
134 }
135
Jason Monk39b46742015-09-10 15:52:51 -0400136 @Override
137 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
138 }
139
Jason Monk91e2f892016-02-23 15:31:09 -0500140 @Override
141 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
142 super.addPreferencesFromResource(preferencesResId);
143 checkAvailablePrefs(getPreferenceScreen());
144 }
145
146 private void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
147 if (preferenceGroup == null) return;
148 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
149 Preference pref = preferenceGroup.getPreference(i);
150 if (pref instanceof SelfAvailablePreference
151 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
152 preferenceGroup.removePreference(pref);
153 } else if (pref instanceof PreferenceGroup) {
154 checkAvailablePrefs((PreferenceGroup) pref);
155 }
156 }
157 }
158
John Spurlockb8e02b82015-04-15 21:15:55 -0400159 public FloatingActionButton getFloatingActionButton() {
160 return mFloatingActionButton;
161 }
162
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900163 public ViewGroup getButtonBar() {
164 return mButtonBar;
165 }
166
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700167 public View setPinnedHeaderView(int layoutResId) {
168 final LayoutInflater inflater = getActivity().getLayoutInflater();
169 final View pinnedHeader =
170 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
171 setPinnedHeaderView(pinnedHeader);
172 return pinnedHeader;
173 }
174
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700175 public void setPinnedHeaderView(View pinnedHeader) {
176 mPinnedHeaderFrameLayout.addView(pinnedHeader);
177 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
178 }
179
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700180 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700181 public void onSaveInstanceState(Bundle outState) {
182 super.onSaveInstanceState(outState);
183
184 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
185 }
186
187 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700188 public void onActivityCreated(Bundle savedInstanceState) {
189 super.onActivityCreated(savedInstanceState);
Anna Galusza384fa2e2016-02-03 15:51:12 -0800190 if (!TextUtils.isEmpty(mHelpUri)) {
Amith Yamasanib3a593e2012-04-23 18:03:52 -0700191 setHasOptionsMenu(true);
192 }
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700193 }
194
195 @Override
196 public void onResume() {
197 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700198
199 final Bundle args = getArguments();
200 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700201 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
202 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700203 }
204 }
205
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700206 @Override
207 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700208 registerObserverIfNeeded();
209 }
210
211 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700212 protected void onUnbindPreferences() {
213 unregisterObserverIfNeeded();
214 }
215
Jason Monkb5aa73f2015-03-31 12:59:33 -0400216 public void showLoadingWhenEmpty() {
217 View loading = getView().findViewById(R.id.loading_container);
Jason Monk39b46742015-09-10 15:52:51 -0400218 setEmptyView(loading);
Jason Monkb5aa73f2015-03-31 12:59:33 -0400219 }
220
Jason Monkb37e2882016-01-11 14:27:20 -0500221 public void setLoading(boolean loading, boolean animate) {
222 View loading_container = getView().findViewById(R.id.loading_container);
223 Utils.handleLoadingContainer(loading_container, getListView(), !loading, animate);
224 }
225
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700226 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700227 if (!mIsDataSetObserverRegistered) {
228 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400229 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700230 }
Jason Monk39b46742015-09-10 15:52:51 -0400231 mCurrentRootAdapter = getListView().getAdapter();
232 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700233 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500234 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700235 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700236 }
237
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700238 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700239 if (mIsDataSetObserverRegistered) {
240 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400241 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700242 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700243 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700244 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700245 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700246 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700247
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700248 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700249 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700250 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700251 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700252 }
253
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000254 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400255 highlightPreferenceIfNeeded();
256 updateEmptyView();
257 }
258
Jason Monk39b46742015-09-10 15:52:51 -0400259 public LayoutPreference getHeaderView() {
260 return mHeader;
261 }
262
263 public LayoutPreference getFooterView() {
264 return mFooter;
265 }
266
267 protected void setHeaderView(int resource) {
268 mHeader = new LayoutPreference(getPrefContext(), resource);
Udam Sainid553abc2016-02-16 17:54:13 -0800269 addPreferenceToTop(mHeader);
270 }
271
272 protected void setHeaderView(View view) {
273 mHeader = new LayoutPreference(getPrefContext(), view);
274 addPreferenceToTop(mHeader);
275 }
276
277 private void addPreferenceToTop(LayoutPreference preference) {
278 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400279 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800280 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400281 }
282 }
283
284 protected void setFooterView(int resource) {
285 setFooterView(resource != 0 ? new LayoutPreference(getPrefContext(), resource) : null);
286 }
287
288 protected void setFooterView(View v) {
289 setFooterView(v != null ? new LayoutPreference(getPrefContext(), v) : null);
290 }
291
292 private void setFooterView(LayoutPreference footer) {
293 if (getPreferenceScreen() != null && mFooter != null) {
294 getPreferenceScreen().removePreference(mFooter);
295 }
296 if (footer != null) {
297 mFooter = footer;
Sudheer Shanka5590e2e2016-01-22 20:40:56 +0000298 mFooter.setOrder(ORDER_LAST);
Jason Monk39b46742015-09-10 15:52:51 -0400299 if (getPreferenceScreen() != null) {
300 getPreferenceScreen().addPreference(mFooter);
301 }
302 } else {
303 mFooter = null;
304 }
305 }
306
307 @Override
308 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400309 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400310 // Without ids generated, the RecyclerView won't animate changes to the preferences.
311 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
312 }
Jason Monk39b46742015-09-10 15:52:51 -0400313 super.setPreferenceScreen(preferenceScreen);
314 if (preferenceScreen != null) {
315 if (mHeader != null) {
316 preferenceScreen.addPreference(mHeader);
317 }
318 if (mFooter != null) {
319 preferenceScreen.addPreference(mFooter);
320 }
321 }
322 }
323
324 private void updateEmptyView() {
325 if (mEmptyView == null) return;
326 if (getPreferenceScreen() != null) {
327 boolean show = (getPreferenceScreen().getPreferenceCount()
328 - (mHeader != null ? 1 : 0)
329 - (mFooter != null ? 1 : 0)) <= 0;
330 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
331 } else {
332 mEmptyView.setVisibility(View.VISIBLE);
333 }
334 }
335
336 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000337 if (mEmptyView != null) {
338 mEmptyView.setVisibility(View.GONE);
339 }
Jason Monk39b46742015-09-10 15:52:51 -0400340 mEmptyView = v;
341 updateEmptyView();
342 }
343
344 public View getEmptyView() {
345 return mEmptyView;
346 }
347
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700348 /**
349 * Return a valid ListView position or -1 if none is found
350 */
351 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400352 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700353 return -1;
354 }
355
Jason Monk39b46742015-09-10 15:52:51 -0400356 RecyclerView listView = getListView();
357 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700358
359 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400360 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700361 }
362
363 return -1;
364 }
365
Jason Monk65bb0972015-12-17 10:39:44 -0500366 @Override
367 public RecyclerView.LayoutManager onCreateLayoutManager() {
368 mLayoutManager = new LinearLayoutManager(getContext());
369 return mLayoutManager;
370 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700371
Jason Monk65bb0972015-12-17 10:39:44 -0500372 @Override
373 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
374 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
375 return mAdapter;
376 }
377
Jason Monkf38fb382016-03-18 14:23:01 -0400378 protected void setAnimationAllowed(boolean animationAllowed) {
379 mAnimationAllowed = animationAllowed;
380 }
381
Jason Monk2071eda2016-02-25 13:55:48 -0500382 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
383 mPreferenceCache = new ArrayMap<String, Preference>();
384 final int N = group.getPreferenceCount();
385 for (int i = 0; i < N; i++) {
386 Preference p = group.getPreference(i);
387 if (TextUtils.isEmpty(p.getKey())) {
388 continue;
389 }
390 mPreferenceCache.put(p.getKey(), p);
391 }
392 }
393
394 protected Preference getCachedPreference(String key) {
395 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
396 }
397
398 protected void removeCachedPrefs(PreferenceGroup group) {
399 for (Preference p : mPreferenceCache.values()) {
400 group.removePreference(p);
401 }
402 }
403
Jason Monk65bb0972015-12-17 10:39:44 -0500404 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700405 final int position = canUseListViewForHighLighting(key);
406 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700407 mPreferenceHighlighted = true;
Jason Monk65bb0972015-12-17 10:39:44 -0500408 mLayoutManager.scrollToPosition(position);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700409
Jason Monk65bb0972015-12-17 10:39:44 -0500410 getView().postDelayed(new Runnable() {
411 @Override
412 public void run() {
413 mAdapter.highlight(position);
414 }
415 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700416 }
417 }
418
Jason Monk39b46742015-09-10 15:52:51 -0400419 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
420 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700421 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400422 final Preference preference = adapter.getItem(n);
423 final String preferenceKey = preference.getKey();
424 if (preferenceKey != null && preferenceKey.equals(key)) {
425 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700426 }
427 }
428 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700429 }
430
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700431 protected void removePreference(String key) {
432 Preference pref = findPreference(key);
433 if (pref != null) {
434 getPreferenceScreen().removePreference(pref);
435 }
436 }
437
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700438 /**
439 * Override this if you want to show a help item in the menu, by returning the resource id.
440 * @return the resource id for the help url
441 */
442 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400443 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700444 }
445
446 @Override
447 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400448 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400449 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700450 }
451 }
452
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700453 /*
454 * The name is intentionally made different from Activity#finish(), so that
455 * users won't misunderstand its meaning.
456 */
457 public final void finishFragment() {
458 getActivity().onBackPressed();
459 }
460
Amith Yamasanid7993472010-08-18 13:59:28 -0700461 // Some helpers for functions used by the settings fragments when they were activities
462
463 /**
464 * Returns the ContentResolver from the owning Activity.
465 */
466 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700467 Context context = getActivity();
468 if (context != null) {
469 mContentResolver = context.getContentResolver();
470 }
471 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700472 }
473
474 /**
475 * Returns the specified system service from the owning Activity.
476 */
477 protected Object getSystemService(final String name) {
478 return getActivity().getSystemService(name);
479 }
480
481 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700482 * Returns the PackageManager from the owning Activity.
483 */
484 protected PackageManager getPackageManager() {
485 return getActivity().getPackageManager();
486 }
487
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800488 @Override
489 public void onDetach() {
490 if (isRemoving()) {
491 if (mDialogFragment != null) {
492 mDialogFragment.dismiss();
493 mDialogFragment = null;
494 }
495 }
496 super.onDetach();
497 }
498
Amith Yamasanid7993472010-08-18 13:59:28 -0700499 // Dialog management
500
501 protected void showDialog(int dialogId) {
502 if (mDialogFragment != null) {
503 Log.e(TAG, "Old dialog fragment not null!");
504 }
505 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800506 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700507 }
508
509 public Dialog onCreateDialog(int dialogId) {
510 return null;
511 }
512
513 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800514 // mDialogFragment may not be visible yet in parent fragment's onResume().
515 // To be able to dismiss dialog at that time, don't check
516 // mDialogFragment.isVisible().
517 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700518 mDialogFragment.dismiss();
519 }
520 mDialogFragment = null;
521 }
522
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800523 /**
524 * Sets the OnCancelListener of the dialog shown. This method can only be
525 * called after showDialog(int) and before removeDialog(int). The method
526 * does nothing otherwise.
527 */
528 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
529 if (mDialogFragment != null) {
530 mDialogFragment.mOnCancelListener = listener;
531 }
532 }
533
534 /**
535 * Sets the OnDismissListener of the dialog shown. This method can only be
536 * called after showDialog(int) and before removeDialog(int). The method
537 * does nothing otherwise.
538 */
539 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
540 if (mDialogFragment != null) {
541 mDialogFragment.mOnDismissListener = listener;
542 }
543 }
544
Amith Yamasanic861cf82012-10-02 14:51:46 -0700545 public void onDialogShowing() {
546 // override in subclass to attach a dismiss listener, for instance
547 }
548
Jason Monk39b46742015-09-10 15:52:51 -0400549 @Override
550 public void onDisplayPreferenceDialog(Preference preference) {
551 if (preference.getKey() == null) {
552 // Auto-key preferences that don't have a key, so the dialog can find them.
553 preference.setKey(UUID.randomUUID().toString());
554 }
555 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000556 if (preference instanceof RestrictedListPreference) {
557 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
558 .newInstance(preference.getKey());
559 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400560 f = CustomListPreference.CustomListPreferenceDialogFragment
561 .newInstance(preference.getKey());
562 } else if (preference instanceof CustomDialogPreference) {
563 f = CustomDialogPreference.CustomPreferenceDialogFragment
564 .newInstance(preference.getKey());
565 } else if (preference instanceof CustomEditTextPreference) {
566 f = CustomEditTextPreference.CustomPreferenceDialogFragment
567 .newInstance(preference.getKey());
568 } else {
569 super.onDisplayPreferenceDialog(preference);
570 return;
571 }
572 f.setTargetFragment(this, 0);
573 f.show(getFragmentManager(), "dialog_preference");
574 onDialogShowing();
575 }
576
Amith Yamasani43c69782010-12-01 09:04:36 -0800577 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800578 private static final String KEY_DIALOG_ID = "key_dialog_id";
579 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
580
Amith Yamasanid7993472010-08-18 13:59:28 -0700581 private int mDialogId;
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) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700593 mDialogId = 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
601 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800602 public void onSaveInstanceState(Bundle outState) {
603 super.onSaveInstanceState(outState);
604 if (mParentFragment != null) {
605 outState.putInt(KEY_DIALOG_ID, mDialogId);
606 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
607 }
608 }
609
610 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700611 public void onStart() {
612 super.onStart();
613
614 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
615 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
616 }
617 }
618
619 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800620 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800621 if (savedInstanceState != null) {
622 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800623 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800624 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700625 if (mParentFragment == null) {
626 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
627 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800628 if (!(mParentFragment instanceof DialogCreatable)) {
629 throw new IllegalArgumentException(
630 (mParentFragment != null
631 ? mParentFragment.getClass().getName()
632 : mParentFragmentId)
633 + " must implement "
634 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800635 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800636 // This dialog fragment could be created from non-SettingsPreferenceFragment
637 if (mParentFragment instanceof SettingsPreferenceFragment) {
638 // restore mDialogFragment in mParentFragment
639 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
640 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800641 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800642 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700643 }
644
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800645 @Override
646 public void onCancel(DialogInterface dialog) {
647 super.onCancel(dialog);
648 if (mOnCancelListener != null) {
649 mOnCancelListener.onCancel(dialog);
650 }
651 }
652
653 @Override
654 public void onDismiss(DialogInterface dialog) {
655 super.onDismiss(dialog);
656 if (mOnDismissListener != null) {
657 mOnDismissListener.onDismiss(dialog);
658 }
659 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800660
Amith Yamasanid7993472010-08-18 13:59:28 -0700661 public int getDialogId() {
662 return mDialogId;
663 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800664
665 @Override
666 public void onDetach() {
667 super.onDetach();
668
Amith Yamasani8875ede2011-01-31 12:46:57 -0800669 // This dialog fragment could be created from non-SettingsPreferenceFragment
670 if (mParentFragment instanceof SettingsPreferenceFragment) {
671 // in case the dialog is not explicitly removed by removeDialog()
672 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
673 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
674 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800675 }
676 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700677 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700678
679 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800680 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700681 }
682
683 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800684 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700685 }
686
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700687 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700688 Activity activity = getActivity();
Jason Monk30e9fc82016-02-10 12:55:37 -0500689 if (activity != null && !activity.getFragmentManager().popBackStackImmediate()) {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800690 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700691 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700692 }
693
Jason Monk39b46742015-09-10 15:52:51 -0400694 protected final Context getPrefContext() {
695 return getPreferenceManager().getContext();
696 }
697
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700698 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
699 int requestCode, Bundle extras) {
700 final Activity activity = getActivity();
701 if (activity instanceof SettingsActivity) {
702 SettingsActivity sa = (SettingsActivity) activity;
703 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
704 return true;
705 } else if (activity instanceof PreferenceActivity) {
706 PreferenceActivity sa = (PreferenceActivity) activity;
707 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700708 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700709 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700710 Log.w(TAG,
711 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
712 + "launch the given Fragment (name: " + fragmentClass
713 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700714 return false;
715 }
716 }
Jason Monk65bb0972015-12-17 10:39:44 -0500717
718 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
719
720 private int mHighlightPosition = -1;
721
722 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
723 super(preferenceGroup);
724 }
725
726 public void highlight(int position) {
727 mHighlightPosition = position;
728 notifyDataSetChanged();
729 }
730
731 @Override
732 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
733 super.onBindViewHolder(holder, position);
734 if (position == mHighlightPosition) {
735 View v = holder.itemView;
736 if (v.getBackground() != null) {
737 final int centerX = v.getWidth() / 2;
738 final int centerY = v.getHeight() / 2;
739 v.getBackground().setHotspot(centerX, centerY);
740 }
741 v.setPressed(true);
742 v.setPressed(false);
743 mHighlightPosition = -1;
744 }
745 }
746 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700747}