blob: cc87c056518092a9fbafa40afb3d70be38d1a7a8 [file] [log] [blame]
Amith Yamasanid7993472010-08-18 13:59:28 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -070019import android.app.Activity;
Amith Yamasanid7993472010-08-18 13:59:28 -070020import android.app.Dialog;
21import android.app.DialogFragment;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -070022import android.app.Fragment;
Amith Yamasanid7993472010-08-18 13:59:28 -070023import android.content.ContentResolver;
Amith Yamasani350938e2013-04-09 10:22:47 -070024import android.content.Context;
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +080025import android.content.DialogInterface;
Jason Monkb7e43802016-06-06 16:01:58 -040026import android.content.Intent;
Amith Yamasanid7993472010-08-18 13:59:28 -070027import android.content.pm.PackageManager;
Amith Yamasanid7993472010-08-18 13:59:28 -070028import android.os.Bundle;
Jason Monk91e2f892016-02-23 15:31:09 -050029import android.support.annotation.XmlRes;
Jason Monk39b46742015-09-10 15:52:51 -040030import android.support.v7.preference.Preference;
Jason Monk65bb0972015-12-17 10:39:44 -050031import android.support.v7.preference.PreferenceGroup;
Jason Monk39b46742015-09-10 15:52:51 -040032import android.support.v7.preference.PreferenceGroupAdapter;
33import android.support.v7.preference.PreferenceScreen;
Jason Monk65bb0972015-12-17 10:39:44 -050034import android.support.v7.preference.PreferenceViewHolder;
35import android.support.v7.widget.LinearLayoutManager;
Jason Monk39b46742015-09-10 15:52:51 -040036import android.support.v7.widget.RecyclerView;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070037import android.text.TextUtils;
Jason Monk2071eda2016-02-25 13:55:48 -050038import android.util.ArrayMap;
Amith Yamasanid7993472010-08-18 13:59:28 -070039import android.util.Log;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070040import android.view.LayoutInflater;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070041import android.view.Menu;
42import android.view.MenuInflater;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070043import android.view.View;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070044import android.view.ViewGroup;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070045import android.widget.Button;
Jason Monkb7e43802016-06-06 16:01:58 -040046
Jason Monk39b46742015-09-10 15:52:51 -040047import com.android.settings.applications.LayoutPreference;
Fan Zhang65076132016-08-08 10:25:13 -070048import com.android.settings.core.InstrumentedFragment;
Suprabh Shuklab84720c2016-04-05 14:37:20 -070049import com.android.settingslib.HelpUtils;
John Spurlockb8e02b82015-04-15 21:15:55 -040050
Jason Monk39b46742015-09-10 15:52:51 -040051import java.util.UUID;
52
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070053/**
Amith Yamasanid7993472010-08-18 13:59:28 -070054 * Base class for Settings fragments, with some helper functions and dialog management.
55 */
Fan Zhangbf4be772016-08-04 16:05:55 -070056public abstract class SettingsPreferenceFragment extends InstrumentedFragment
Chris Wren8a963ba2015-03-20 10:29:14 -040057 implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070058
Anna Galusza0285c802016-01-29 17:32:19 -080059 /**
60 * The Help Uri Resource key. This can be passed as an extra argument when creating the
61 * Fragment.
62 **/
63 public static final String HELP_URI_RESOURCE_KEY = "help_uri_resource";
64
Jason Monk65bb0972015-12-17 10:39:44 -050065 private static final String TAG = "SettingsPreference";
Amith Yamasanid7993472010-08-18 13:59:28 -070066
Fabrice Di Meglioeced7802014-09-04 13:01:55 -070067 private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070068
69 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070070
Amith Yamasanid7993472010-08-18 13:59:28 -070071 private SettingsDialogFragment mDialogFragment;
72
Jason Monk23acc2b2015-04-14 15:06:39 -040073 private String mHelpUri;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070074
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000075 private static final int ORDER_FIRST = -1;
76 private static final int ORDER_LAST = Integer.MAX_VALUE -1;
77
Amith Yamasani350938e2013-04-09 10:22:47 -070078 // Cache the content resolver for async callbacks
79 private ContentResolver mContentResolver;
80
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070081 private String mPreferenceKey;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070082 private boolean mPreferenceHighlighted = false;
83
Jason Monk39b46742015-09-10 15:52:51 -040084 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070085 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040086 private RecyclerView.AdapterDataObserver mDataSetObserver =
87 new RecyclerView.AdapterDataObserver() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070088 @Override
89 public void onChanged() {
Jason Monk39b46742015-09-10 15:52:51 -040090 onDataSetChanged();
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070091 }
92 };
93
Fabrice Di Meglio86159282014-07-21 16:02:27 -070094 private ViewGroup mPinnedHeaderFrameLayout;
Daichi Hirono5e76cdc2015-07-08 11:38:55 +090095 private ViewGroup mButtonBar;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070096
Jason Monk39b46742015-09-10 15:52:51 -040097 private LayoutPreference mHeader;
98
99 private LayoutPreference mFooter;
100 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -0500101 private LinearLayoutManager mLayoutManager;
102 private HighlightablePreferenceGroupAdapter mAdapter;
Jason Monk2071eda2016-02-25 13:55:48 -0500103 private ArrayMap<String, Preference> mPreferenceCache;
Jason Monkf38fb382016-03-18 14:23:01 -0400104 private boolean mAnimationAllowed;
Jason Monk39b46742015-09-10 15:52:51 -0400105
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700106 @Override
107 public void onCreate(Bundle icicle) {
108 super.onCreate(icicle);
109
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700110 if (icicle != null) {
111 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
112 }
113
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700114 // Prepare help url and enable menu if necessary
Anna Galusza0285c802016-01-29 17:32:19 -0800115 Bundle arguments = getArguments();
116 int helpResource;
117 if (arguments != null && arguments.containsKey(HELP_URI_RESOURCE_KEY)) {
118 helpResource = arguments.getInt(HELP_URI_RESOURCE_KEY);
119 } else {
120 helpResource = getHelpResource();
121 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700122 if (helpResource != 0) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400123 mHelpUri = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700124 }
125 }
126
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700127 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700128 public View onCreateView(LayoutInflater inflater, ViewGroup container,
129 Bundle savedInstanceState) {
130 final View root = super.onCreateView(inflater, container, savedInstanceState);
131 mPinnedHeaderFrameLayout = (ViewGroup) root.findViewById(R.id.pinned_header);
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
Jason Monk91e2f892016-02-23 15:31:09 -0500137 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
138 super.addPreferencesFromResource(preferencesResId);
139 checkAvailablePrefs(getPreferenceScreen());
140 }
141
142 private void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
143 if (preferenceGroup == null) return;
144 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
145 Preference pref = preferenceGroup.getPreference(i);
146 if (pref instanceof SelfAvailablePreference
147 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
148 preferenceGroup.removePreference(pref);
149 } else if (pref instanceof PreferenceGroup) {
150 checkAvailablePrefs((PreferenceGroup) pref);
151 }
152 }
153 }
154
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900155 public ViewGroup getButtonBar() {
156 return mButtonBar;
157 }
158
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700159 public View setPinnedHeaderView(int layoutResId) {
160 final LayoutInflater inflater = getActivity().getLayoutInflater();
161 final View pinnedHeader =
162 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
163 setPinnedHeaderView(pinnedHeader);
164 return pinnedHeader;
165 }
166
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700167 public void setPinnedHeaderView(View pinnedHeader) {
168 mPinnedHeaderFrameLayout.addView(pinnedHeader);
169 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
170 }
171
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700172 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700173 public void onSaveInstanceState(Bundle outState) {
174 super.onSaveInstanceState(outState);
175
176 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
177 }
178
179 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700180 public void onActivityCreated(Bundle savedInstanceState) {
181 super.onActivityCreated(savedInstanceState);
Johan Redestig76218e52016-04-19 08:29:30 +0200182 setHasOptionsMenu(true);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700183 }
184
185 @Override
186 public void onResume() {
187 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700188
189 final Bundle args = getArguments();
190 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700191 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
192 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700193 }
194 }
195
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700196 @Override
197 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700198 registerObserverIfNeeded();
199 }
200
201 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700202 protected void onUnbindPreferences() {
203 unregisterObserverIfNeeded();
204 }
205
Jason Monkb5aa73f2015-03-31 12:59:33 -0400206 public void showLoadingWhenEmpty() {
207 View loading = getView().findViewById(R.id.loading_container);
Jason Monk39b46742015-09-10 15:52:51 -0400208 setEmptyView(loading);
Jason Monkb5aa73f2015-03-31 12:59:33 -0400209 }
210
Jason Monkb37e2882016-01-11 14:27:20 -0500211 public void setLoading(boolean loading, boolean animate) {
212 View loading_container = getView().findViewById(R.id.loading_container);
213 Utils.handleLoadingContainer(loading_container, getListView(), !loading, animate);
214 }
215
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700216 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700217 if (!mIsDataSetObserverRegistered) {
218 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400219 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700220 }
Jason Monk39b46742015-09-10 15:52:51 -0400221 mCurrentRootAdapter = getListView().getAdapter();
222 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700223 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500224 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700225 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700226 }
227
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700228 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700229 if (mIsDataSetObserverRegistered) {
230 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400231 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700232 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700233 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700234 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700235 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700236 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700237
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700238 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700239 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700240 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700241 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700242 }
243
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000244 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400245 highlightPreferenceIfNeeded();
246 updateEmptyView();
247 }
248
Jason Monk39b46742015-09-10 15:52:51 -0400249 public LayoutPreference getHeaderView() {
250 return mHeader;
251 }
252
253 public LayoutPreference getFooterView() {
254 return mFooter;
255 }
256
257 protected void setHeaderView(int resource) {
258 mHeader = new LayoutPreference(getPrefContext(), resource);
Udam Sainid553abc2016-02-16 17:54:13 -0800259 addPreferenceToTop(mHeader);
260 }
261
262 protected void setHeaderView(View view) {
263 mHeader = new LayoutPreference(getPrefContext(), view);
264 addPreferenceToTop(mHeader);
265 }
266
267 private void addPreferenceToTop(LayoutPreference preference) {
268 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400269 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800270 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400271 }
272 }
273
274 protected void setFooterView(int resource) {
275 setFooterView(resource != 0 ? new LayoutPreference(getPrefContext(), resource) : null);
276 }
277
278 protected void setFooterView(View v) {
279 setFooterView(v != null ? new LayoutPreference(getPrefContext(), v) : null);
280 }
281
282 private void setFooterView(LayoutPreference footer) {
283 if (getPreferenceScreen() != null && mFooter != null) {
284 getPreferenceScreen().removePreference(mFooter);
285 }
286 if (footer != null) {
287 mFooter = footer;
Sudheer Shanka5590e2e2016-01-22 20:40:56 +0000288 mFooter.setOrder(ORDER_LAST);
Jason Monk39b46742015-09-10 15:52:51 -0400289 if (getPreferenceScreen() != null) {
290 getPreferenceScreen().addPreference(mFooter);
291 }
292 } else {
293 mFooter = null;
294 }
295 }
296
297 @Override
298 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400299 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400300 // Without ids generated, the RecyclerView won't animate changes to the preferences.
301 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
302 }
Jason Monk39b46742015-09-10 15:52:51 -0400303 super.setPreferenceScreen(preferenceScreen);
304 if (preferenceScreen != null) {
305 if (mHeader != null) {
306 preferenceScreen.addPreference(mHeader);
307 }
308 if (mFooter != null) {
309 preferenceScreen.addPreference(mFooter);
310 }
311 }
312 }
313
314 private void updateEmptyView() {
315 if (mEmptyView == null) return;
316 if (getPreferenceScreen() != null) {
317 boolean show = (getPreferenceScreen().getPreferenceCount()
318 - (mHeader != null ? 1 : 0)
319 - (mFooter != null ? 1 : 0)) <= 0;
320 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
321 } else {
322 mEmptyView.setVisibility(View.VISIBLE);
323 }
324 }
325
326 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000327 if (mEmptyView != null) {
328 mEmptyView.setVisibility(View.GONE);
329 }
Jason Monk39b46742015-09-10 15:52:51 -0400330 mEmptyView = v;
331 updateEmptyView();
332 }
333
334 public View getEmptyView() {
335 return mEmptyView;
336 }
337
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700338 /**
339 * Return a valid ListView position or -1 if none is found
340 */
341 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400342 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700343 return -1;
344 }
345
Jason Monk39b46742015-09-10 15:52:51 -0400346 RecyclerView listView = getListView();
347 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700348
349 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400350 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700351 }
352
353 return -1;
354 }
355
Jason Monk65bb0972015-12-17 10:39:44 -0500356 @Override
357 public RecyclerView.LayoutManager onCreateLayoutManager() {
358 mLayoutManager = new LinearLayoutManager(getContext());
359 return mLayoutManager;
360 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700361
Jason Monk65bb0972015-12-17 10:39:44 -0500362 @Override
363 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
364 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
365 return mAdapter;
366 }
367
Jason Monkf38fb382016-03-18 14:23:01 -0400368 protected void setAnimationAllowed(boolean animationAllowed) {
369 mAnimationAllowed = animationAllowed;
370 }
371
Jason Monk2071eda2016-02-25 13:55:48 -0500372 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
373 mPreferenceCache = new ArrayMap<String, Preference>();
374 final int N = group.getPreferenceCount();
375 for (int i = 0; i < N; i++) {
376 Preference p = group.getPreference(i);
377 if (TextUtils.isEmpty(p.getKey())) {
378 continue;
379 }
380 mPreferenceCache.put(p.getKey(), p);
381 }
382 }
383
384 protected Preference getCachedPreference(String key) {
385 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
386 }
387
388 protected void removeCachedPrefs(PreferenceGroup group) {
389 for (Preference p : mPreferenceCache.values()) {
390 group.removePreference(p);
391 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400392 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500393 }
394
Jason Monka6278442016-04-21 10:12:30 -0400395 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400396 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400397 }
398
Jason Monk65bb0972015-12-17 10:39:44 -0500399 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700400 final int position = canUseListViewForHighLighting(key);
401 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700402 mPreferenceHighlighted = true;
Jason Monk65bb0972015-12-17 10:39:44 -0500403 mLayoutManager.scrollToPosition(position);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700404
Jason Monk65bb0972015-12-17 10:39:44 -0500405 getView().postDelayed(new Runnable() {
406 @Override
407 public void run() {
408 mAdapter.highlight(position);
409 }
410 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700411 }
412 }
413
Jason Monk39b46742015-09-10 15:52:51 -0400414 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
415 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700416 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400417 final Preference preference = adapter.getItem(n);
418 final String preferenceKey = preference.getKey();
419 if (preferenceKey != null && preferenceKey.equals(key)) {
420 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700421 }
422 }
423 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700424 }
425
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700426 protected void removePreference(String key) {
427 Preference pref = findPreference(key);
428 if (pref != null) {
429 getPreferenceScreen().removePreference(pref);
430 }
431 }
432
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700433 /**
434 * Override this if you want to show a help item in the menu, by returning the resource id.
435 * @return the resource id for the help url
436 */
437 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400438 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700439 }
440
441 @Override
442 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400443 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400444 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700445 }
446 }
447
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700448 /*
449 * The name is intentionally made different from Activity#finish(), so that
450 * users won't misunderstand its meaning.
451 */
452 public final void finishFragment() {
453 getActivity().onBackPressed();
454 }
455
Amith Yamasanid7993472010-08-18 13:59:28 -0700456 // Some helpers for functions used by the settings fragments when they were activities
457
458 /**
459 * Returns the ContentResolver from the owning Activity.
460 */
461 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700462 Context context = getActivity();
463 if (context != null) {
464 mContentResolver = context.getContentResolver();
465 }
466 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700467 }
468
469 /**
470 * Returns the specified system service from the owning Activity.
471 */
472 protected Object getSystemService(final String name) {
473 return getActivity().getSystemService(name);
474 }
475
476 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700477 * Returns the PackageManager from the owning Activity.
478 */
479 protected PackageManager getPackageManager() {
480 return getActivity().getPackageManager();
481 }
482
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800483 @Override
484 public void onDetach() {
485 if (isRemoving()) {
486 if (mDialogFragment != null) {
487 mDialogFragment.dismiss();
488 mDialogFragment = null;
489 }
490 }
491 super.onDetach();
492 }
493
Amith Yamasanid7993472010-08-18 13:59:28 -0700494 // Dialog management
495
496 protected void showDialog(int dialogId) {
497 if (mDialogFragment != null) {
498 Log.e(TAG, "Old dialog fragment not null!");
499 }
500 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800501 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700502 }
503
504 public Dialog onCreateDialog(int dialogId) {
505 return null;
506 }
507
508 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800509 // mDialogFragment may not be visible yet in parent fragment's onResume().
510 // To be able to dismiss dialog at that time, don't check
511 // mDialogFragment.isVisible().
512 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400513 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700514 }
515 mDialogFragment = null;
516 }
517
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800518 /**
519 * Sets the OnCancelListener of the dialog shown. This method can only be
520 * called after showDialog(int) and before removeDialog(int). The method
521 * does nothing otherwise.
522 */
523 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
524 if (mDialogFragment != null) {
525 mDialogFragment.mOnCancelListener = listener;
526 }
527 }
528
529 /**
530 * Sets the OnDismissListener of the dialog shown. This method can only be
531 * called after showDialog(int) and before removeDialog(int). The method
532 * does nothing otherwise.
533 */
534 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
535 if (mDialogFragment != null) {
536 mDialogFragment.mOnDismissListener = listener;
537 }
538 }
539
Amith Yamasanic861cf82012-10-02 14:51:46 -0700540 public void onDialogShowing() {
541 // override in subclass to attach a dismiss listener, for instance
542 }
543
Jason Monk39b46742015-09-10 15:52:51 -0400544 @Override
545 public void onDisplayPreferenceDialog(Preference preference) {
546 if (preference.getKey() == null) {
547 // Auto-key preferences that don't have a key, so the dialog can find them.
548 preference.setKey(UUID.randomUUID().toString());
549 }
550 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000551 if (preference instanceof RestrictedListPreference) {
552 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
553 .newInstance(preference.getKey());
554 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400555 f = CustomListPreference.CustomListPreferenceDialogFragment
556 .newInstance(preference.getKey());
557 } else if (preference instanceof CustomDialogPreference) {
558 f = CustomDialogPreference.CustomPreferenceDialogFragment
559 .newInstance(preference.getKey());
560 } else if (preference instanceof CustomEditTextPreference) {
561 f = CustomEditTextPreference.CustomPreferenceDialogFragment
562 .newInstance(preference.getKey());
563 } else {
564 super.onDisplayPreferenceDialog(preference);
565 return;
566 }
567 f.setTargetFragment(this, 0);
568 f.show(getFragmentManager(), "dialog_preference");
569 onDialogShowing();
570 }
571
Amith Yamasani43c69782010-12-01 09:04:36 -0800572 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800573 private static final String KEY_DIALOG_ID = "key_dialog_id";
574 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
575
Amith Yamasanid7993472010-08-18 13:59:28 -0700576 private int mDialogId;
577
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800578 private Fragment mParentFragment;
579
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800580 private DialogInterface.OnCancelListener mOnCancelListener;
581 private DialogInterface.OnDismissListener mOnDismissListener;
582
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800583 public SettingsDialogFragment() {
584 /* do nothing */
585 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700586
Amith Yamasani43c69782010-12-01 09:04:36 -0800587 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700588 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800589 if (!(fragment instanceof Fragment)) {
590 throw new IllegalArgumentException("fragment argument must be an instance of "
591 + Fragment.class.getName());
592 }
593 mParentFragment = (Fragment) fragment;
594 }
595
596 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800597 public void onSaveInstanceState(Bundle outState) {
598 super.onSaveInstanceState(outState);
599 if (mParentFragment != null) {
600 outState.putInt(KEY_DIALOG_ID, mDialogId);
601 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
602 }
603 }
604
605 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700606 public void onStart() {
607 super.onStart();
608
609 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
610 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
611 }
612 }
613
614 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800615 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800616 if (savedInstanceState != null) {
617 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800618 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800619 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700620 if (mParentFragment == null) {
621 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
622 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800623 if (!(mParentFragment instanceof DialogCreatable)) {
624 throw new IllegalArgumentException(
625 (mParentFragment != null
626 ? mParentFragment.getClass().getName()
627 : mParentFragmentId)
628 + " must implement "
629 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800630 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800631 // This dialog fragment could be created from non-SettingsPreferenceFragment
632 if (mParentFragment instanceof SettingsPreferenceFragment) {
633 // restore mDialogFragment in mParentFragment
634 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
635 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800636 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800637 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700638 }
639
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800640 @Override
641 public void onCancel(DialogInterface dialog) {
642 super.onCancel(dialog);
643 if (mOnCancelListener != null) {
644 mOnCancelListener.onCancel(dialog);
645 }
646 }
647
648 @Override
649 public void onDismiss(DialogInterface dialog) {
650 super.onDismiss(dialog);
651 if (mOnDismissListener != null) {
652 mOnDismissListener.onDismiss(dialog);
653 }
654 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800655
Amith Yamasanid7993472010-08-18 13:59:28 -0700656 public int getDialogId() {
657 return mDialogId;
658 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800659
660 @Override
661 public void onDetach() {
662 super.onDetach();
663
Amith Yamasani8875ede2011-01-31 12:46:57 -0800664 // This dialog fragment could be created from non-SettingsPreferenceFragment
665 if (mParentFragment instanceof SettingsPreferenceFragment) {
666 // in case the dialog is not explicitly removed by removeDialog()
667 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
668 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
669 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800670 }
671 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700672 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700673
674 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800675 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700676 }
677
678 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800679 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700680 }
681
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700682 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700683 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400684 if (activity == null) return;
685 if (getFragmentManager().getBackStackEntryCount() > 0) {
686 getFragmentManager().popBackStack();
687 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800688 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700689 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700690 }
691
Jason Monkb7e43802016-06-06 16:01:58 -0400692 protected Intent getIntent() {
693 if (getActivity() == null) {
694 return null;
695 }
696 return getActivity().getIntent();
697 }
698
699 protected void setResult(int result, Intent intent) {
700 if (getActivity() == null) {
701 return;
702 }
703 getActivity().setResult(result, intent);
704 }
705
706 protected void setResult(int result) {
707 if (getActivity() == null) {
708 return;
709 }
710 getActivity().setResult(result);
711 }
712
Jason Monk39b46742015-09-10 15:52:51 -0400713 protected final Context getPrefContext() {
714 return getPreferenceManager().getContext();
715 }
716
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700717 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
718 int requestCode, Bundle extras) {
719 final Activity activity = getActivity();
720 if (activity instanceof SettingsActivity) {
721 SettingsActivity sa = (SettingsActivity) activity;
722 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
723 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700724 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700725 Log.w(TAG,
726 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
727 + "launch the given Fragment (name: " + fragmentClass
728 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700729 return false;
730 }
731 }
Jason Monk65bb0972015-12-17 10:39:44 -0500732
733 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
734
735 private int mHighlightPosition = -1;
736
737 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
738 super(preferenceGroup);
739 }
740
741 public void highlight(int position) {
742 mHighlightPosition = position;
743 notifyDataSetChanged();
744 }
745
746 @Override
747 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
748 super.onBindViewHolder(holder, position);
749 if (position == mHighlightPosition) {
750 View v = holder.itemView;
751 if (v.getBackground() != null) {
752 final int centerX = v.getWidth() / 2;
753 final int centerY = v.getHeight() / 2;
754 v.getBackground().setHotspot(centerX, centerY);
755 }
756 v.setPressed(true);
757 v.setPressed(false);
758 mHighlightPosition = -1;
759 }
760 }
761 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700762}