blob: 130137fba8e763c3bf94cb5e723b25d56e110f8f [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 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400402 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500403 }
404
Jason Monka6278442016-04-21 10:12:30 -0400405 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400406 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400407 }
408
Jason Monk65bb0972015-12-17 10:39:44 -0500409 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700410 final int position = canUseListViewForHighLighting(key);
411 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700412 mPreferenceHighlighted = true;
Jason Monk65bb0972015-12-17 10:39:44 -0500413 mLayoutManager.scrollToPosition(position);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700414
Jason Monk65bb0972015-12-17 10:39:44 -0500415 getView().postDelayed(new Runnable() {
416 @Override
417 public void run() {
418 mAdapter.highlight(position);
419 }
420 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700421 }
422 }
423
Jason Monk39b46742015-09-10 15:52:51 -0400424 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
425 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700426 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400427 final Preference preference = adapter.getItem(n);
428 final String preferenceKey = preference.getKey();
429 if (preferenceKey != null && preferenceKey.equals(key)) {
430 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700431 }
432 }
433 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700434 }
435
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700436 protected void removePreference(String key) {
437 Preference pref = findPreference(key);
438 if (pref != null) {
439 getPreferenceScreen().removePreference(pref);
440 }
441 }
442
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700443 /**
444 * Override this if you want to show a help item in the menu, by returning the resource id.
445 * @return the resource id for the help url
446 */
447 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400448 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700449 }
450
451 @Override
452 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400453 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400454 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700455 }
456 }
457
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700458 /*
459 * The name is intentionally made different from Activity#finish(), so that
460 * users won't misunderstand its meaning.
461 */
462 public final void finishFragment() {
463 getActivity().onBackPressed();
464 }
465
Amith Yamasanid7993472010-08-18 13:59:28 -0700466 // Some helpers for functions used by the settings fragments when they were activities
467
468 /**
469 * Returns the ContentResolver from the owning Activity.
470 */
471 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700472 Context context = getActivity();
473 if (context != null) {
474 mContentResolver = context.getContentResolver();
475 }
476 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700477 }
478
479 /**
480 * Returns the specified system service from the owning Activity.
481 */
482 protected Object getSystemService(final String name) {
483 return getActivity().getSystemService(name);
484 }
485
486 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700487 * Returns the PackageManager from the owning Activity.
488 */
489 protected PackageManager getPackageManager() {
490 return getActivity().getPackageManager();
491 }
492
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800493 @Override
494 public void onDetach() {
495 if (isRemoving()) {
496 if (mDialogFragment != null) {
497 mDialogFragment.dismiss();
498 mDialogFragment = null;
499 }
500 }
501 super.onDetach();
502 }
503
Amith Yamasanid7993472010-08-18 13:59:28 -0700504 // Dialog management
505
506 protected void showDialog(int dialogId) {
507 if (mDialogFragment != null) {
508 Log.e(TAG, "Old dialog fragment not null!");
509 }
510 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800511 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700512 }
513
514 public Dialog onCreateDialog(int dialogId) {
515 return null;
516 }
517
518 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800519 // mDialogFragment may not be visible yet in parent fragment's onResume().
520 // To be able to dismiss dialog at that time, don't check
521 // mDialogFragment.isVisible().
522 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700523 mDialogFragment.dismiss();
524 }
525 mDialogFragment = null;
526 }
527
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800528 /**
529 * Sets the OnCancelListener of the dialog shown. This method can only be
530 * called after showDialog(int) and before removeDialog(int). The method
531 * does nothing otherwise.
532 */
533 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
534 if (mDialogFragment != null) {
535 mDialogFragment.mOnCancelListener = listener;
536 }
537 }
538
539 /**
540 * Sets the OnDismissListener of the dialog shown. This method can only be
541 * called after showDialog(int) and before removeDialog(int). The method
542 * does nothing otherwise.
543 */
544 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
545 if (mDialogFragment != null) {
546 mDialogFragment.mOnDismissListener = listener;
547 }
548 }
549
Amith Yamasanic861cf82012-10-02 14:51:46 -0700550 public void onDialogShowing() {
551 // override in subclass to attach a dismiss listener, for instance
552 }
553
Jason Monk39b46742015-09-10 15:52:51 -0400554 @Override
555 public void onDisplayPreferenceDialog(Preference preference) {
556 if (preference.getKey() == null) {
557 // Auto-key preferences that don't have a key, so the dialog can find them.
558 preference.setKey(UUID.randomUUID().toString());
559 }
560 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000561 if (preference instanceof RestrictedListPreference) {
562 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
563 .newInstance(preference.getKey());
564 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400565 f = CustomListPreference.CustomListPreferenceDialogFragment
566 .newInstance(preference.getKey());
567 } else if (preference instanceof CustomDialogPreference) {
568 f = CustomDialogPreference.CustomPreferenceDialogFragment
569 .newInstance(preference.getKey());
570 } else if (preference instanceof CustomEditTextPreference) {
571 f = CustomEditTextPreference.CustomPreferenceDialogFragment
572 .newInstance(preference.getKey());
573 } else {
574 super.onDisplayPreferenceDialog(preference);
575 return;
576 }
577 f.setTargetFragment(this, 0);
578 f.show(getFragmentManager(), "dialog_preference");
579 onDialogShowing();
580 }
581
Amith Yamasani43c69782010-12-01 09:04:36 -0800582 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800583 private static final String KEY_DIALOG_ID = "key_dialog_id";
584 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
585
Amith Yamasanid7993472010-08-18 13:59:28 -0700586 private int mDialogId;
587
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800588 private Fragment mParentFragment;
589
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800590 private DialogInterface.OnCancelListener mOnCancelListener;
591 private DialogInterface.OnDismissListener mOnDismissListener;
592
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800593 public SettingsDialogFragment() {
594 /* do nothing */
595 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700596
Amith Yamasani43c69782010-12-01 09:04:36 -0800597 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700598 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800599 if (!(fragment instanceof Fragment)) {
600 throw new IllegalArgumentException("fragment argument must be an instance of "
601 + Fragment.class.getName());
602 }
603 mParentFragment = (Fragment) fragment;
604 }
605
606 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800607 public void onSaveInstanceState(Bundle outState) {
608 super.onSaveInstanceState(outState);
609 if (mParentFragment != null) {
610 outState.putInt(KEY_DIALOG_ID, mDialogId);
611 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
612 }
613 }
614
615 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700616 public void onStart() {
617 super.onStart();
618
619 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
620 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
621 }
622 }
623
624 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800625 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800626 if (savedInstanceState != null) {
627 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800628 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800629 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700630 if (mParentFragment == null) {
631 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
632 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800633 if (!(mParentFragment instanceof DialogCreatable)) {
634 throw new IllegalArgumentException(
635 (mParentFragment != null
636 ? mParentFragment.getClass().getName()
637 : mParentFragmentId)
638 + " must implement "
639 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800640 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800641 // This dialog fragment could be created from non-SettingsPreferenceFragment
642 if (mParentFragment instanceof SettingsPreferenceFragment) {
643 // restore mDialogFragment in mParentFragment
644 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
645 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800646 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800647 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700648 }
649
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800650 @Override
651 public void onCancel(DialogInterface dialog) {
652 super.onCancel(dialog);
653 if (mOnCancelListener != null) {
654 mOnCancelListener.onCancel(dialog);
655 }
656 }
657
658 @Override
659 public void onDismiss(DialogInterface dialog) {
660 super.onDismiss(dialog);
661 if (mOnDismissListener != null) {
662 mOnDismissListener.onDismiss(dialog);
663 }
664 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800665
Amith Yamasanid7993472010-08-18 13:59:28 -0700666 public int getDialogId() {
667 return mDialogId;
668 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800669
670 @Override
671 public void onDetach() {
672 super.onDetach();
673
Amith Yamasani8875ede2011-01-31 12:46:57 -0800674 // This dialog fragment could be created from non-SettingsPreferenceFragment
675 if (mParentFragment instanceof SettingsPreferenceFragment) {
676 // in case the dialog is not explicitly removed by removeDialog()
677 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
678 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
679 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800680 }
681 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700682 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700683
684 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800685 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700686 }
687
688 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800689 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700690 }
691
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700692 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700693 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400694 if (activity == null) return;
695 if (getFragmentManager().getBackStackEntryCount() > 0) {
696 getFragmentManager().popBackStack();
697 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800698 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700699 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700700 }
701
Jason Monkb7e43802016-06-06 16:01:58 -0400702 protected Intent getIntent() {
703 if (getActivity() == null) {
704 return null;
705 }
706 return getActivity().getIntent();
707 }
708
709 protected void setResult(int result, Intent intent) {
710 if (getActivity() == null) {
711 return;
712 }
713 getActivity().setResult(result, intent);
714 }
715
716 protected void setResult(int result) {
717 if (getActivity() == null) {
718 return;
719 }
720 getActivity().setResult(result);
721 }
722
Jason Monk39b46742015-09-10 15:52:51 -0400723 protected final Context getPrefContext() {
724 return getPreferenceManager().getContext();
725 }
726
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700727 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
728 int requestCode, Bundle extras) {
729 final Activity activity = getActivity();
730 if (activity instanceof SettingsActivity) {
731 SettingsActivity sa = (SettingsActivity) activity;
732 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
733 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700734 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700735 Log.w(TAG,
736 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
737 + "launch the given Fragment (name: " + fragmentClass
738 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700739 return false;
740 }
741 }
Jason Monk65bb0972015-12-17 10:39:44 -0500742
743 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
744
745 private int mHighlightPosition = -1;
746
747 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
748 super(preferenceGroup);
749 }
750
751 public void highlight(int position) {
752 mHighlightPosition = position;
753 notifyDataSetChanged();
754 }
755
756 @Override
757 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
758 super.onBindViewHolder(holder, position);
759 if (position == mHighlightPosition) {
760 View v = holder.itemView;
761 if (v.getBackground() != null) {
762 final int centerX = v.getWidth() / 2;
763 final int centerY = v.getHeight() / 2;
764 v.getBackground().setHotspot(centerX, centerY);
765 }
766 v.setPressed(true);
767 v.setPressed(false);
768 mHighlightPosition = -1;
769 }
770 }
771 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700772}