blob: 7985ed9039808bcfc6d6f15acffea62c3247d43b [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;
John Spurlockb8e02b82015-04-15 21:15:55 -040048import com.android.settings.widget.FloatingActionButton;
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 */
Chris Wren8a963ba2015-03-20 10:29:14 -040056public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceFragment
57 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;
John Spurlockb8e02b82015-04-15 21:15:55 -040095 private FloatingActionButton mFloatingActionButton;
Daichi Hirono5e76cdc2015-07-08 11:38:55 +090096 private ViewGroup mButtonBar;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070097
Jason Monk39b46742015-09-10 15:52:51 -040098 private LayoutPreference mHeader;
99
100 private LayoutPreference mFooter;
101 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -0500102 private LinearLayoutManager mLayoutManager;
103 private HighlightablePreferenceGroupAdapter mAdapter;
Jason Monk2071eda2016-02-25 13:55:48 -0500104 private ArrayMap<String, Preference> mPreferenceCache;
Jason Monkf38fb382016-03-18 14:23:01 -0400105 private boolean mAnimationAllowed;
Jason Monk39b46742015-09-10 15:52:51 -0400106
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700107 @Override
108 public void onCreate(Bundle icicle) {
109 super.onCreate(icicle);
110
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700111 if (icicle != null) {
112 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
113 }
114
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700115 // Prepare help url and enable menu if necessary
Anna Galusza0285c802016-01-29 17:32:19 -0800116 Bundle arguments = getArguments();
117 int helpResource;
118 if (arguments != null && arguments.containsKey(HELP_URI_RESOURCE_KEY)) {
119 helpResource = arguments.getInt(HELP_URI_RESOURCE_KEY);
120 } else {
121 helpResource = getHelpResource();
122 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700123 if (helpResource != 0) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400124 mHelpUri = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700125 }
126 }
127
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700128 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700129 public View onCreateView(LayoutInflater inflater, ViewGroup container,
130 Bundle savedInstanceState) {
131 final View root = super.onCreateView(inflater, container, savedInstanceState);
132 mPinnedHeaderFrameLayout = (ViewGroup) root.findViewById(R.id.pinned_header);
John Spurlockb8e02b82015-04-15 21:15:55 -0400133 mFloatingActionButton = (FloatingActionButton) root.findViewById(R.id.fab);
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900134 mButtonBar = (ViewGroup) root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700135 return root;
136 }
137
Jason Monk39b46742015-09-10 15:52:51 -0400138 @Override
139 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
140 }
141
Jason Monk91e2f892016-02-23 15:31:09 -0500142 @Override
143 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
144 super.addPreferencesFromResource(preferencesResId);
145 checkAvailablePrefs(getPreferenceScreen());
146 }
147
148 private void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
149 if (preferenceGroup == null) return;
150 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
151 Preference pref = preferenceGroup.getPreference(i);
152 if (pref instanceof SelfAvailablePreference
153 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
154 preferenceGroup.removePreference(pref);
155 } else if (pref instanceof PreferenceGroup) {
156 checkAvailablePrefs((PreferenceGroup) pref);
157 }
158 }
159 }
160
John Spurlockb8e02b82015-04-15 21:15:55 -0400161 public FloatingActionButton getFloatingActionButton() {
162 return mFloatingActionButton;
163 }
164
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900165 public ViewGroup getButtonBar() {
166 return mButtonBar;
167 }
168
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700169 public View setPinnedHeaderView(int layoutResId) {
170 final LayoutInflater inflater = getActivity().getLayoutInflater();
171 final View pinnedHeader =
172 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
173 setPinnedHeaderView(pinnedHeader);
174 return pinnedHeader;
175 }
176
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700177 public void setPinnedHeaderView(View pinnedHeader) {
178 mPinnedHeaderFrameLayout.addView(pinnedHeader);
179 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
180 }
181
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700182 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700183 public void onSaveInstanceState(Bundle outState) {
184 super.onSaveInstanceState(outState);
185
186 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
187 }
188
189 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700190 public void onActivityCreated(Bundle savedInstanceState) {
191 super.onActivityCreated(savedInstanceState);
Johan Redestig76218e52016-04-19 08:29:30 +0200192 setHasOptionsMenu(true);
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 Monka6278442016-04-21 10:12:30 -0400404 protected int getCachedCount() {
405 return mPreferenceCache.size();
406 }
407
Jason Monk65bb0972015-12-17 10:39:44 -0500408 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700409 final int position = canUseListViewForHighLighting(key);
410 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700411 mPreferenceHighlighted = true;
Jason Monk65bb0972015-12-17 10:39:44 -0500412 mLayoutManager.scrollToPosition(position);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700413
Jason Monk65bb0972015-12-17 10:39:44 -0500414 getView().postDelayed(new Runnable() {
415 @Override
416 public void run() {
417 mAdapter.highlight(position);
418 }
419 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700420 }
421 }
422
Jason Monk39b46742015-09-10 15:52:51 -0400423 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
424 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700425 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400426 final Preference preference = adapter.getItem(n);
427 final String preferenceKey = preference.getKey();
428 if (preferenceKey != null && preferenceKey.equals(key)) {
429 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700430 }
431 }
432 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700433 }
434
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700435 protected void removePreference(String key) {
436 Preference pref = findPreference(key);
437 if (pref != null) {
438 getPreferenceScreen().removePreference(pref);
439 }
440 }
441
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700442 /**
443 * Override this if you want to show a help item in the menu, by returning the resource id.
444 * @return the resource id for the help url
445 */
446 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400447 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700448 }
449
450 @Override
451 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400452 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400453 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700454 }
455 }
456
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700457 /*
458 * The name is intentionally made different from Activity#finish(), so that
459 * users won't misunderstand its meaning.
460 */
461 public final void finishFragment() {
462 getActivity().onBackPressed();
463 }
464
Amith Yamasanid7993472010-08-18 13:59:28 -0700465 // Some helpers for functions used by the settings fragments when they were activities
466
467 /**
468 * Returns the ContentResolver from the owning Activity.
469 */
470 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700471 Context context = getActivity();
472 if (context != null) {
473 mContentResolver = context.getContentResolver();
474 }
475 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700476 }
477
478 /**
479 * Returns the specified system service from the owning Activity.
480 */
481 protected Object getSystemService(final String name) {
482 return getActivity().getSystemService(name);
483 }
484
485 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700486 * Returns the PackageManager from the owning Activity.
487 */
488 protected PackageManager getPackageManager() {
489 return getActivity().getPackageManager();
490 }
491
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800492 @Override
493 public void onDetach() {
494 if (isRemoving()) {
495 if (mDialogFragment != null) {
496 mDialogFragment.dismiss();
497 mDialogFragment = null;
498 }
499 }
500 super.onDetach();
501 }
502
Amith Yamasanid7993472010-08-18 13:59:28 -0700503 // Dialog management
504
505 protected void showDialog(int dialogId) {
506 if (mDialogFragment != null) {
507 Log.e(TAG, "Old dialog fragment not null!");
508 }
509 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800510 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700511 }
512
513 public Dialog onCreateDialog(int dialogId) {
514 return null;
515 }
516
517 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800518 // mDialogFragment may not be visible yet in parent fragment's onResume().
519 // To be able to dismiss dialog at that time, don't check
520 // mDialogFragment.isVisible().
521 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400522 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700523 }
524 mDialogFragment = null;
525 }
526
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800527 /**
528 * Sets the OnCancelListener of the dialog shown. This method can only be
529 * called after showDialog(int) and before removeDialog(int). The method
530 * does nothing otherwise.
531 */
532 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
533 if (mDialogFragment != null) {
534 mDialogFragment.mOnCancelListener = listener;
535 }
536 }
537
538 /**
539 * Sets the OnDismissListener of the dialog shown. This method can only be
540 * called after showDialog(int) and before removeDialog(int). The method
541 * does nothing otherwise.
542 */
543 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
544 if (mDialogFragment != null) {
545 mDialogFragment.mOnDismissListener = listener;
546 }
547 }
548
Amith Yamasanic861cf82012-10-02 14:51:46 -0700549 public void onDialogShowing() {
550 // override in subclass to attach a dismiss listener, for instance
551 }
552
Jason Monk39b46742015-09-10 15:52:51 -0400553 @Override
554 public void onDisplayPreferenceDialog(Preference preference) {
555 if (preference.getKey() == null) {
556 // Auto-key preferences that don't have a key, so the dialog can find them.
557 preference.setKey(UUID.randomUUID().toString());
558 }
559 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000560 if (preference instanceof RestrictedListPreference) {
561 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
562 .newInstance(preference.getKey());
563 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400564 f = CustomListPreference.CustomListPreferenceDialogFragment
565 .newInstance(preference.getKey());
566 } else if (preference instanceof CustomDialogPreference) {
567 f = CustomDialogPreference.CustomPreferenceDialogFragment
568 .newInstance(preference.getKey());
569 } else if (preference instanceof CustomEditTextPreference) {
570 f = CustomEditTextPreference.CustomPreferenceDialogFragment
571 .newInstance(preference.getKey());
572 } else {
573 super.onDisplayPreferenceDialog(preference);
574 return;
575 }
576 f.setTargetFragment(this, 0);
577 f.show(getFragmentManager(), "dialog_preference");
578 onDialogShowing();
579 }
580
Amith Yamasani43c69782010-12-01 09:04:36 -0800581 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800582 private static final String KEY_DIALOG_ID = "key_dialog_id";
583 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
584
Amith Yamasanid7993472010-08-18 13:59:28 -0700585 private int mDialogId;
586
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800587 private Fragment mParentFragment;
588
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800589 private DialogInterface.OnCancelListener mOnCancelListener;
590 private DialogInterface.OnDismissListener mOnDismissListener;
591
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800592 public SettingsDialogFragment() {
593 /* do nothing */
594 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700595
Amith Yamasani43c69782010-12-01 09:04:36 -0800596 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700597 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800598 if (!(fragment instanceof Fragment)) {
599 throw new IllegalArgumentException("fragment argument must be an instance of "
600 + Fragment.class.getName());
601 }
602 mParentFragment = (Fragment) fragment;
603 }
604
605 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800606 public void onSaveInstanceState(Bundle outState) {
607 super.onSaveInstanceState(outState);
608 if (mParentFragment != null) {
609 outState.putInt(KEY_DIALOG_ID, mDialogId);
610 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
611 }
612 }
613
614 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700615 public void onStart() {
616 super.onStart();
617
618 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
619 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
620 }
621 }
622
623 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800624 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800625 if (savedInstanceState != null) {
626 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800627 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800628 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700629 if (mParentFragment == null) {
630 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
631 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800632 if (!(mParentFragment instanceof DialogCreatable)) {
633 throw new IllegalArgumentException(
634 (mParentFragment != null
635 ? mParentFragment.getClass().getName()
636 : mParentFragmentId)
637 + " must implement "
638 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800639 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800640 // This dialog fragment could be created from non-SettingsPreferenceFragment
641 if (mParentFragment instanceof SettingsPreferenceFragment) {
642 // restore mDialogFragment in mParentFragment
643 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
644 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800645 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800646 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700647 }
648
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800649 @Override
650 public void onCancel(DialogInterface dialog) {
651 super.onCancel(dialog);
652 if (mOnCancelListener != null) {
653 mOnCancelListener.onCancel(dialog);
654 }
655 }
656
657 @Override
658 public void onDismiss(DialogInterface dialog) {
659 super.onDismiss(dialog);
660 if (mOnDismissListener != null) {
661 mOnDismissListener.onDismiss(dialog);
662 }
663 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800664
Amith Yamasanid7993472010-08-18 13:59:28 -0700665 public int getDialogId() {
666 return mDialogId;
667 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800668
669 @Override
670 public void onDetach() {
671 super.onDetach();
672
Amith Yamasani8875ede2011-01-31 12:46:57 -0800673 // This dialog fragment could be created from non-SettingsPreferenceFragment
674 if (mParentFragment instanceof SettingsPreferenceFragment) {
675 // in case the dialog is not explicitly removed by removeDialog()
676 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
677 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
678 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800679 }
680 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700681 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700682
683 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800684 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700685 }
686
687 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800688 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700689 }
690
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700691 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700692 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400693 if (activity == null) return;
694 if (getFragmentManager().getBackStackEntryCount() > 0) {
695 getFragmentManager().popBackStack();
696 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800697 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700698 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700699 }
700
Jason Monkb7e43802016-06-06 16:01:58 -0400701 protected Intent getIntent() {
702 if (getActivity() == null) {
703 return null;
704 }
705 return getActivity().getIntent();
706 }
707
708 protected void setResult(int result, Intent intent) {
709 if (getActivity() == null) {
710 return;
711 }
712 getActivity().setResult(result, intent);
713 }
714
715 protected void setResult(int result) {
716 if (getActivity() == null) {
717 return;
718 }
719 getActivity().setResult(result);
720 }
721
Jason Monk39b46742015-09-10 15:52:51 -0400722 protected final Context getPrefContext() {
723 return getPreferenceManager().getContext();
724 }
725
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700726 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
727 int requestCode, Bundle extras) {
728 final Activity activity = getActivity();
729 if (activity instanceof SettingsActivity) {
730 SettingsActivity sa = (SettingsActivity) activity;
731 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
732 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700733 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700734 Log.w(TAG,
735 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
736 + "launch the given Fragment (name: " + fragmentClass
737 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700738 return false;
739 }
740 }
Jason Monk65bb0972015-12-17 10:39:44 -0500741
742 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
743
744 private int mHighlightPosition = -1;
745
746 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
747 super(preferenceGroup);
748 }
749
750 public void highlight(int position) {
751 mHighlightPosition = position;
752 notifyDataSetChanged();
753 }
754
755 @Override
756 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
757 super.onBindViewHolder(holder, position);
758 if (position == mHighlightPosition) {
759 View v = holder.itemView;
760 if (v.getBackground() != null) {
761 final int centerX = v.getWidth() / 2;
762 final int centerY = v.getHeight() / 2;
763 v.getBackground().setHotspot(centerX, centerY);
764 }
765 v.setPressed(true);
766 v.setPressed(false);
767 mHighlightPosition = -1;
768 }
769 }
770 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700771}