blob: 16df343d53ff739361904264f3562b7c3f450ad2 [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;
47
Jason Monk39b46742015-09-10 15:52:51 -040048import java.util.UUID;
49
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070050/**
Amith Yamasanid7993472010-08-18 13:59:28 -070051 * Base class for Settings fragments, with some helper functions and dialog management.
52 */
Chris Wren8a963ba2015-03-20 10:29:14 -040053public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceFragment
54 implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070055
Anna Galusza0285c802016-01-29 17:32:19 -080056 /**
57 * The Help Uri Resource key. This can be passed as an extra argument when creating the
58 * Fragment.
59 **/
60 public static final String HELP_URI_RESOURCE_KEY = "help_uri_resource";
61
Jason Monk65bb0972015-12-17 10:39:44 -050062 private static final String TAG = "SettingsPreference";
Amith Yamasanid7993472010-08-18 13:59:28 -070063
Fabrice Di Meglioeced7802014-09-04 13:01:55 -070064 private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070065
66 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070067
Amith Yamasanid7993472010-08-18 13:59:28 -070068 private SettingsDialogFragment mDialogFragment;
69
Jason Monk23acc2b2015-04-14 15:06:39 -040070 private String mHelpUri;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070071
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000072 private static final int ORDER_FIRST = -1;
73 private static final int ORDER_LAST = Integer.MAX_VALUE -1;
74
Amith Yamasani350938e2013-04-09 10:22:47 -070075 // Cache the content resolver for async callbacks
76 private ContentResolver mContentResolver;
77
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070078 private String mPreferenceKey;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070079 private boolean mPreferenceHighlighted = false;
80
Jason Monk39b46742015-09-10 15:52:51 -040081 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070082 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040083 private RecyclerView.AdapterDataObserver mDataSetObserver =
84 new RecyclerView.AdapterDataObserver() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070085 @Override
86 public void onChanged() {
Jason Monk39b46742015-09-10 15:52:51 -040087 onDataSetChanged();
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070088 }
89 };
90
Fabrice Di Meglio86159282014-07-21 16:02:27 -070091 private ViewGroup mPinnedHeaderFrameLayout;
John Spurlockb8e02b82015-04-15 21:15:55 -040092 private FloatingActionButton mFloatingActionButton;
Daichi Hirono5e76cdc2015-07-08 11:38:55 +090093 private ViewGroup mButtonBar;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070094
Jason Monk39b46742015-09-10 15:52:51 -040095 private LayoutPreference mHeader;
96
97 private LayoutPreference mFooter;
98 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -050099 private LinearLayoutManager mLayoutManager;
100 private HighlightablePreferenceGroupAdapter mAdapter;
Jason Monk2071eda2016-02-25 13:55:48 -0500101 private ArrayMap<String, Preference> mPreferenceCache;
Jason Monkf38fb382016-03-18 14:23:01 -0400102 private boolean mAnimationAllowed;
Jason Monk39b46742015-09-10 15:52:51 -0400103
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700104 @Override
105 public void onCreate(Bundle icicle) {
106 super.onCreate(icicle);
107
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700108 if (icicle != null) {
109 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
110 }
111
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700112 // Prepare help url and enable menu if necessary
Anna Galusza0285c802016-01-29 17:32:19 -0800113 Bundle arguments = getArguments();
114 int helpResource;
115 if (arguments != null && arguments.containsKey(HELP_URI_RESOURCE_KEY)) {
116 helpResource = arguments.getInt(HELP_URI_RESOURCE_KEY);
117 } else {
118 helpResource = getHelpResource();
119 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700120 if (helpResource != 0) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400121 mHelpUri = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700122 }
123 }
124
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700125 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700126 public View onCreateView(LayoutInflater inflater, ViewGroup container,
127 Bundle savedInstanceState) {
128 final View root = super.onCreateView(inflater, container, savedInstanceState);
129 mPinnedHeaderFrameLayout = (ViewGroup) root.findViewById(R.id.pinned_header);
John Spurlockb8e02b82015-04-15 21:15:55 -0400130 mFloatingActionButton = (FloatingActionButton) root.findViewById(R.id.fab);
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900131 mButtonBar = (ViewGroup) root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700132 return root;
133 }
134
Jason Monk39b46742015-09-10 15:52:51 -0400135 @Override
136 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
137 }
138
Jason Monk91e2f892016-02-23 15:31:09 -0500139 @Override
140 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
141 super.addPreferencesFromResource(preferencesResId);
142 checkAvailablePrefs(getPreferenceScreen());
143 }
144
145 private void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
146 if (preferenceGroup == null) return;
147 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
148 Preference pref = preferenceGroup.getPreference(i);
149 if (pref instanceof SelfAvailablePreference
150 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
151 preferenceGroup.removePreference(pref);
152 } else if (pref instanceof PreferenceGroup) {
153 checkAvailablePrefs((PreferenceGroup) pref);
154 }
155 }
156 }
157
John Spurlockb8e02b82015-04-15 21:15:55 -0400158 public FloatingActionButton getFloatingActionButton() {
159 return mFloatingActionButton;
160 }
161
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900162 public ViewGroup getButtonBar() {
163 return mButtonBar;
164 }
165
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700166 public View setPinnedHeaderView(int layoutResId) {
167 final LayoutInflater inflater = getActivity().getLayoutInflater();
168 final View pinnedHeader =
169 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
170 setPinnedHeaderView(pinnedHeader);
171 return pinnedHeader;
172 }
173
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700174 public void setPinnedHeaderView(View pinnedHeader) {
175 mPinnedHeaderFrameLayout.addView(pinnedHeader);
176 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
177 }
178
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700179 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700180 public void onSaveInstanceState(Bundle outState) {
181 super.onSaveInstanceState(outState);
182
183 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
184 }
185
186 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700187 public void onActivityCreated(Bundle savedInstanceState) {
188 super.onActivityCreated(savedInstanceState);
Anna Galusza384fa2e2016-02-03 15:51:12 -0800189 if (!TextUtils.isEmpty(mHelpUri)) {
Amith Yamasanib3a593e2012-04-23 18:03:52 -0700190 setHasOptionsMenu(true);
191 }
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700192 }
193
194 @Override
195 public void onResume() {
196 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700197
198 final Bundle args = getArguments();
199 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700200 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
201 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700202 }
203 }
204
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700205 @Override
206 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700207 registerObserverIfNeeded();
208 }
209
210 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700211 protected void onUnbindPreferences() {
212 unregisterObserverIfNeeded();
213 }
214
Jason Monkb5aa73f2015-03-31 12:59:33 -0400215 public void showLoadingWhenEmpty() {
216 View loading = getView().findViewById(R.id.loading_container);
Jason Monk39b46742015-09-10 15:52:51 -0400217 setEmptyView(loading);
Jason Monkb5aa73f2015-03-31 12:59:33 -0400218 }
219
Jason Monkb37e2882016-01-11 14:27:20 -0500220 public void setLoading(boolean loading, boolean animate) {
221 View loading_container = getView().findViewById(R.id.loading_container);
222 Utils.handleLoadingContainer(loading_container, getListView(), !loading, animate);
223 }
224
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700225 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700226 if (!mIsDataSetObserverRegistered) {
227 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400228 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700229 }
Jason Monk39b46742015-09-10 15:52:51 -0400230 mCurrentRootAdapter = getListView().getAdapter();
231 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700232 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500233 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700234 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700235 }
236
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700237 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700238 if (mIsDataSetObserverRegistered) {
239 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400240 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700241 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700242 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700243 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700244 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700245 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700246
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700247 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700248 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700249 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700250 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700251 }
252
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000253 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400254 highlightPreferenceIfNeeded();
255 updateEmptyView();
256 }
257
Jason Monk39b46742015-09-10 15:52:51 -0400258 public LayoutPreference getHeaderView() {
259 return mHeader;
260 }
261
262 public LayoutPreference getFooterView() {
263 return mFooter;
264 }
265
266 protected void setHeaderView(int resource) {
267 mHeader = new LayoutPreference(getPrefContext(), resource);
Udam Sainid553abc2016-02-16 17:54:13 -0800268 addPreferenceToTop(mHeader);
269 }
270
271 protected void setHeaderView(View view) {
272 mHeader = new LayoutPreference(getPrefContext(), view);
273 addPreferenceToTop(mHeader);
274 }
275
276 private void addPreferenceToTop(LayoutPreference preference) {
277 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400278 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800279 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400280 }
281 }
282
283 protected void setFooterView(int resource) {
284 setFooterView(resource != 0 ? new LayoutPreference(getPrefContext(), resource) : null);
285 }
286
287 protected void setFooterView(View v) {
288 setFooterView(v != null ? new LayoutPreference(getPrefContext(), v) : null);
289 }
290
291 private void setFooterView(LayoutPreference footer) {
292 if (getPreferenceScreen() != null && mFooter != null) {
293 getPreferenceScreen().removePreference(mFooter);
294 }
295 if (footer != null) {
296 mFooter = footer;
Sudheer Shanka5590e2e2016-01-22 20:40:56 +0000297 mFooter.setOrder(ORDER_LAST);
Jason Monk39b46742015-09-10 15:52:51 -0400298 if (getPreferenceScreen() != null) {
299 getPreferenceScreen().addPreference(mFooter);
300 }
301 } else {
302 mFooter = null;
303 }
304 }
305
306 @Override
307 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400308 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400309 // Without ids generated, the RecyclerView won't animate changes to the preferences.
310 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
311 }
Jason Monk39b46742015-09-10 15:52:51 -0400312 super.setPreferenceScreen(preferenceScreen);
313 if (preferenceScreen != null) {
314 if (mHeader != null) {
315 preferenceScreen.addPreference(mHeader);
316 }
317 if (mFooter != null) {
318 preferenceScreen.addPreference(mFooter);
319 }
320 }
321 }
322
323 private void updateEmptyView() {
324 if (mEmptyView == null) return;
325 if (getPreferenceScreen() != null) {
326 boolean show = (getPreferenceScreen().getPreferenceCount()
327 - (mHeader != null ? 1 : 0)
328 - (mFooter != null ? 1 : 0)) <= 0;
329 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
330 } else {
331 mEmptyView.setVisibility(View.VISIBLE);
332 }
333 }
334
335 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000336 if (mEmptyView != null) {
337 mEmptyView.setVisibility(View.GONE);
338 }
Jason Monk39b46742015-09-10 15:52:51 -0400339 mEmptyView = v;
340 updateEmptyView();
341 }
342
343 public View getEmptyView() {
344 return mEmptyView;
345 }
346
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700347 /**
348 * Return a valid ListView position or -1 if none is found
349 */
350 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400351 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700352 return -1;
353 }
354
Jason Monk39b46742015-09-10 15:52:51 -0400355 RecyclerView listView = getListView();
356 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700357
358 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400359 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700360 }
361
362 return -1;
363 }
364
Jason Monk65bb0972015-12-17 10:39:44 -0500365 @Override
366 public RecyclerView.LayoutManager onCreateLayoutManager() {
367 mLayoutManager = new LinearLayoutManager(getContext());
368 return mLayoutManager;
369 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700370
Jason Monk65bb0972015-12-17 10:39:44 -0500371 @Override
372 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
373 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
374 return mAdapter;
375 }
376
Jason Monkf38fb382016-03-18 14:23:01 -0400377 protected void setAnimationAllowed(boolean animationAllowed) {
378 mAnimationAllowed = animationAllowed;
379 }
380
Jason Monk2071eda2016-02-25 13:55:48 -0500381 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
382 mPreferenceCache = new ArrayMap<String, Preference>();
383 final int N = group.getPreferenceCount();
384 for (int i = 0; i < N; i++) {
385 Preference p = group.getPreference(i);
386 if (TextUtils.isEmpty(p.getKey())) {
387 continue;
388 }
389 mPreferenceCache.put(p.getKey(), p);
390 }
391 }
392
393 protected Preference getCachedPreference(String key) {
394 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
395 }
396
397 protected void removeCachedPrefs(PreferenceGroup group) {
398 for (Preference p : mPreferenceCache.values()) {
399 group.removePreference(p);
400 }
401 }
402
Jason Monk65bb0972015-12-17 10:39:44 -0500403 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700404 final int position = canUseListViewForHighLighting(key);
405 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700406 mPreferenceHighlighted = true;
Jason Monk65bb0972015-12-17 10:39:44 -0500407 mLayoutManager.scrollToPosition(position);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700408
Jason Monk65bb0972015-12-17 10:39:44 -0500409 getView().postDelayed(new Runnable() {
410 @Override
411 public void run() {
412 mAdapter.highlight(position);
413 }
414 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700415 }
416 }
417
Jason Monk39b46742015-09-10 15:52:51 -0400418 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
419 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700420 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400421 final Preference preference = adapter.getItem(n);
422 final String preferenceKey = preference.getKey();
423 if (preferenceKey != null && preferenceKey.equals(key)) {
424 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700425 }
426 }
427 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700428 }
429
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700430 protected void removePreference(String key) {
431 Preference pref = findPreference(key);
432 if (pref != null) {
433 getPreferenceScreen().removePreference(pref);
434 }
435 }
436
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700437 /**
438 * Override this if you want to show a help item in the menu, by returning the resource id.
439 * @return the resource id for the help url
440 */
441 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400442 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700443 }
444
445 @Override
446 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400447 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400448 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700449 }
450 }
451
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700452 /*
453 * The name is intentionally made different from Activity#finish(), so that
454 * users won't misunderstand its meaning.
455 */
456 public final void finishFragment() {
457 getActivity().onBackPressed();
458 }
459
Amith Yamasanid7993472010-08-18 13:59:28 -0700460 // Some helpers for functions used by the settings fragments when they were activities
461
462 /**
463 * Returns the ContentResolver from the owning Activity.
464 */
465 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700466 Context context = getActivity();
467 if (context != null) {
468 mContentResolver = context.getContentResolver();
469 }
470 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700471 }
472
473 /**
474 * Returns the specified system service from the owning Activity.
475 */
476 protected Object getSystemService(final String name) {
477 return getActivity().getSystemService(name);
478 }
479
480 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700481 * Returns the PackageManager from the owning Activity.
482 */
483 protected PackageManager getPackageManager() {
484 return getActivity().getPackageManager();
485 }
486
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800487 @Override
488 public void onDetach() {
489 if (isRemoving()) {
490 if (mDialogFragment != null) {
491 mDialogFragment.dismiss();
492 mDialogFragment = null;
493 }
494 }
495 super.onDetach();
496 }
497
Amith Yamasanid7993472010-08-18 13:59:28 -0700498 // Dialog management
499
500 protected void showDialog(int dialogId) {
501 if (mDialogFragment != null) {
502 Log.e(TAG, "Old dialog fragment not null!");
503 }
504 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800505 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700506 }
507
508 public Dialog onCreateDialog(int dialogId) {
509 return null;
510 }
511
512 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800513 // mDialogFragment may not be visible yet in parent fragment's onResume().
514 // To be able to dismiss dialog at that time, don't check
515 // mDialogFragment.isVisible().
516 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700517 mDialogFragment.dismiss();
518 }
519 mDialogFragment = null;
520 }
521
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800522 /**
523 * Sets the OnCancelListener of the dialog shown. This method can only be
524 * called after showDialog(int) and before removeDialog(int). The method
525 * does nothing otherwise.
526 */
527 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
528 if (mDialogFragment != null) {
529 mDialogFragment.mOnCancelListener = listener;
530 }
531 }
532
533 /**
534 * Sets the OnDismissListener of the dialog shown. This method can only be
535 * called after showDialog(int) and before removeDialog(int). The method
536 * does nothing otherwise.
537 */
538 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
539 if (mDialogFragment != null) {
540 mDialogFragment.mOnDismissListener = listener;
541 }
542 }
543
Amith Yamasanic861cf82012-10-02 14:51:46 -0700544 public void onDialogShowing() {
545 // override in subclass to attach a dismiss listener, for instance
546 }
547
Jason Monk39b46742015-09-10 15:52:51 -0400548 @Override
549 public void onDisplayPreferenceDialog(Preference preference) {
550 if (preference.getKey() == null) {
551 // Auto-key preferences that don't have a key, so the dialog can find them.
552 preference.setKey(UUID.randomUUID().toString());
553 }
554 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000555 if (preference instanceof RestrictedListPreference) {
556 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
557 .newInstance(preference.getKey());
558 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400559 f = CustomListPreference.CustomListPreferenceDialogFragment
560 .newInstance(preference.getKey());
561 } else if (preference instanceof CustomDialogPreference) {
562 f = CustomDialogPreference.CustomPreferenceDialogFragment
563 .newInstance(preference.getKey());
564 } else if (preference instanceof CustomEditTextPreference) {
565 f = CustomEditTextPreference.CustomPreferenceDialogFragment
566 .newInstance(preference.getKey());
567 } else {
568 super.onDisplayPreferenceDialog(preference);
569 return;
570 }
571 f.setTargetFragment(this, 0);
572 f.show(getFragmentManager(), "dialog_preference");
573 onDialogShowing();
574 }
575
Amith Yamasani43c69782010-12-01 09:04:36 -0800576 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800577 private static final String KEY_DIALOG_ID = "key_dialog_id";
578 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
579
Amith Yamasanid7993472010-08-18 13:59:28 -0700580 private int mDialogId;
581
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800582 private Fragment mParentFragment;
583
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800584 private DialogInterface.OnCancelListener mOnCancelListener;
585 private DialogInterface.OnDismissListener mOnDismissListener;
586
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800587 public SettingsDialogFragment() {
588 /* do nothing */
589 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700590
Amith Yamasani43c69782010-12-01 09:04:36 -0800591 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700592 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800593 if (!(fragment instanceof Fragment)) {
594 throw new IllegalArgumentException("fragment argument must be an instance of "
595 + Fragment.class.getName());
596 }
597 mParentFragment = (Fragment) fragment;
598 }
599
600 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800601 public void onSaveInstanceState(Bundle outState) {
602 super.onSaveInstanceState(outState);
603 if (mParentFragment != null) {
604 outState.putInt(KEY_DIALOG_ID, mDialogId);
605 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
606 }
607 }
608
609 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700610 public void onStart() {
611 super.onStart();
612
613 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
614 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
615 }
616 }
617
618 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800619 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800620 if (savedInstanceState != null) {
621 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800622 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800623 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700624 if (mParentFragment == null) {
625 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
626 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800627 if (!(mParentFragment instanceof DialogCreatable)) {
628 throw new IllegalArgumentException(
629 (mParentFragment != null
630 ? mParentFragment.getClass().getName()
631 : mParentFragmentId)
632 + " must implement "
633 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800634 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800635 // This dialog fragment could be created from non-SettingsPreferenceFragment
636 if (mParentFragment instanceof SettingsPreferenceFragment) {
637 // restore mDialogFragment in mParentFragment
638 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
639 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800640 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800641 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700642 }
643
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800644 @Override
645 public void onCancel(DialogInterface dialog) {
646 super.onCancel(dialog);
647 if (mOnCancelListener != null) {
648 mOnCancelListener.onCancel(dialog);
649 }
650 }
651
652 @Override
653 public void onDismiss(DialogInterface dialog) {
654 super.onDismiss(dialog);
655 if (mOnDismissListener != null) {
656 mOnDismissListener.onDismiss(dialog);
657 }
658 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800659
Amith Yamasanid7993472010-08-18 13:59:28 -0700660 public int getDialogId() {
661 return mDialogId;
662 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800663
664 @Override
665 public void onDetach() {
666 super.onDetach();
667
Amith Yamasani8875ede2011-01-31 12:46:57 -0800668 // This dialog fragment could be created from non-SettingsPreferenceFragment
669 if (mParentFragment instanceof SettingsPreferenceFragment) {
670 // in case the dialog is not explicitly removed by removeDialog()
671 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
672 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
673 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800674 }
675 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700676 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700677
678 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800679 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700680 }
681
682 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800683 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700684 }
685
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700686 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700687 Activity activity = getActivity();
Jason Monk30e9fc82016-02-10 12:55:37 -0500688 if (activity != null && !activity.getFragmentManager().popBackStackImmediate()) {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800689 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700690 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700691 }
692
Jason Monk39b46742015-09-10 15:52:51 -0400693 protected final Context getPrefContext() {
694 return getPreferenceManager().getContext();
695 }
696
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700697 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
698 int requestCode, Bundle extras) {
699 final Activity activity = getActivity();
700 if (activity instanceof SettingsActivity) {
701 SettingsActivity sa = (SettingsActivity) activity;
702 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
703 return true;
704 } else if (activity instanceof PreferenceActivity) {
705 PreferenceActivity sa = (PreferenceActivity) activity;
706 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700707 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700708 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700709 Log.w(TAG,
710 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
711 + "launch the given Fragment (name: " + fragmentClass
712 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700713 return false;
714 }
715 }
Jason Monk65bb0972015-12-17 10:39:44 -0500716
717 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
718
719 private int mHighlightPosition = -1;
720
721 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
722 super(preferenceGroup);
723 }
724
725 public void highlight(int position) {
726 mHighlightPosition = position;
727 notifyDataSetChanged();
728 }
729
730 @Override
731 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
732 super.onBindViewHolder(holder, position);
733 if (position == mHighlightPosition) {
734 View v = holder.itemView;
735 if (v.getBackground() != null) {
736 final int centerX = v.getWidth() / 2;
737 final int centerY = v.getHeight() / 2;
738 v.getBackground().setHotspot(centerX, centerY);
739 }
740 v.setPressed(true);
741 v.setPressed(false);
742 mHighlightPosition = -1;
743 }
744 }
745 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700746}