blob: 575194be8ba9fc51da80a69abcd34cda999c2de6 [file] [log] [blame]
Amith Yamasanid7993472010-08-18 13:59:28 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -070019import android.app.Activity;
Amith Yamasanid7993472010-08-18 13:59:28 -070020import android.app.Dialog;
21import android.app.DialogFragment;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -070022import android.app.Fragment;
Amith Yamasanid7993472010-08-18 13:59:28 -070023import android.content.ContentResolver;
Amith Yamasani350938e2013-04-09 10:22:47 -070024import android.content.Context;
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +080025import android.content.DialogInterface;
Jason Monkb7e43802016-06-06 16:01:58 -040026import android.content.Intent;
Amith Yamasanid7993472010-08-18 13:59:28 -070027import android.content.pm.PackageManager;
Amith Yamasanid7993472010-08-18 13:59:28 -070028import android.os.Bundle;
Jason Monk91e2f892016-02-23 15:31:09 -050029import android.support.annotation.XmlRes;
Jason Monk39b46742015-09-10 15:52:51 -040030import android.support.v7.preference.Preference;
Jason Monk65bb0972015-12-17 10:39:44 -050031import android.support.v7.preference.PreferenceGroup;
Jason Monk39b46742015-09-10 15:52:51 -040032import android.support.v7.preference.PreferenceGroupAdapter;
33import android.support.v7.preference.PreferenceScreen;
Jason Monk65bb0972015-12-17 10:39:44 -050034import android.support.v7.preference.PreferenceViewHolder;
35import android.support.v7.widget.LinearLayoutManager;
Jason Monk39b46742015-09-10 15:52:51 -040036import android.support.v7.widget.RecyclerView;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070037import android.text.TextUtils;
Jason Monk2071eda2016-02-25 13:55:48 -050038import android.util.ArrayMap;
Amith Yamasanid7993472010-08-18 13:59:28 -070039import android.util.Log;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070040import android.view.LayoutInflater;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070041import android.view.Menu;
42import android.view.MenuInflater;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070043import android.view.View;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070044import android.view.ViewGroup;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070045import android.widget.Button;
Jason Monkb7e43802016-06-06 16:01:58 -040046
Jason Monk39b46742015-09-10 15:52:51 -040047import com.android.settings.applications.LayoutPreference;
Fan Zhang65076132016-08-08 10:25:13 -070048import com.android.settings.core.InstrumentedFragment;
Fan Zhangd65184f2016-09-19 17:45:24 -070049import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
Suprabh Shuklab84720c2016-04-05 14:37:20 -070050import com.android.settingslib.HelpUtils;
John Spurlockb8e02b82015-04-15 21:15:55 -040051
Jason Monk39b46742015-09-10 15:52:51 -040052import java.util.UUID;
53
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070054/**
Amith Yamasanid7993472010-08-18 13:59:28 -070055 * Base class for Settings fragments, with some helper functions and dialog management.
56 */
Fan Zhangbf4be772016-08-04 16:05:55 -070057public abstract class SettingsPreferenceFragment extends InstrumentedFragment
Chris Wren8a963ba2015-03-20 10:29:14 -040058 implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070059
Anna Galusza0285c802016-01-29 17:32:19 -080060 /**
61 * The Help Uri Resource key. This can be passed as an extra argument when creating the
62 * Fragment.
63 **/
64 public static final String HELP_URI_RESOURCE_KEY = "help_uri_resource";
65
Jason Monk65bb0972015-12-17 10:39:44 -050066 private static final String TAG = "SettingsPreference";
Amith Yamasanid7993472010-08-18 13:59:28 -070067
Fabrice Di Meglioeced7802014-09-04 13:01:55 -070068 private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070069
70 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070071
Amith Yamasanid7993472010-08-18 13:59:28 -070072 private SettingsDialogFragment mDialogFragment;
73
Jason Monk23acc2b2015-04-14 15:06:39 -040074 private String mHelpUri;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070075
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000076 private static final int ORDER_FIRST = -1;
77 private static final int ORDER_LAST = Integer.MAX_VALUE -1;
78
Amith Yamasani350938e2013-04-09 10:22:47 -070079 // Cache the content resolver for async callbacks
80 private ContentResolver mContentResolver;
81
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070082 private String mPreferenceKey;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070083 private boolean mPreferenceHighlighted = false;
84
Jason Monk39b46742015-09-10 15:52:51 -040085 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070086 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040087 private RecyclerView.AdapterDataObserver mDataSetObserver =
88 new RecyclerView.AdapterDataObserver() {
Tony Mantler0b825f52016-09-27 14:48:16 -070089 @Override
90 public void onChanged() {
91 onDataSetChanged();
92 }
93
94 @Override
95 public void onItemRangeChanged(int positionStart, int itemCount) {
96 onDataSetChanged();
97 }
98
99 @Override
100 public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
101 onDataSetChanged();
102 }
103
104 @Override
105 public void onItemRangeInserted(int positionStart, int itemCount) {
106 onDataSetChanged();
107 }
108
109 @Override
110 public void onItemRangeRemoved(int positionStart, int itemCount) {
111 onDataSetChanged();
112 }
113
114 @Override
115 public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
116 onDataSetChanged();
117 }
118 };
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700119
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700120 private ViewGroup mPinnedHeaderFrameLayout;
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900121 private ViewGroup mButtonBar;
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700122
Jason Monk39b46742015-09-10 15:52:51 -0400123 private LayoutPreference mHeader;
124
125 private LayoutPreference mFooter;
126 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -0500127 private LinearLayoutManager mLayoutManager;
128 private HighlightablePreferenceGroupAdapter mAdapter;
Jason Monk2071eda2016-02-25 13:55:48 -0500129 private ArrayMap<String, Preference> mPreferenceCache;
Jason Monkf38fb382016-03-18 14:23:01 -0400130 private boolean mAnimationAllowed;
Jason Monk39b46742015-09-10 15:52:51 -0400131
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700132 @Override
133 public void onCreate(Bundle icicle) {
134 super.onCreate(icicle);
135
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700136 if (icicle != null) {
137 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
138 }
139
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700140 // Prepare help url and enable menu if necessary
Anna Galusza0285c802016-01-29 17:32:19 -0800141 Bundle arguments = getArguments();
142 int helpResource;
143 if (arguments != null && arguments.containsKey(HELP_URI_RESOURCE_KEY)) {
144 helpResource = arguments.getInt(HELP_URI_RESOURCE_KEY);
145 } else {
146 helpResource = getHelpResource();
147 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700148 if (helpResource != 0) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400149 mHelpUri = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700150 }
151 }
152
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700153 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700154 public View onCreateView(LayoutInflater inflater, ViewGroup container,
155 Bundle savedInstanceState) {
156 final View root = super.onCreateView(inflater, container, savedInstanceState);
157 mPinnedHeaderFrameLayout = (ViewGroup) root.findViewById(R.id.pinned_header);
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900158 mButtonBar = (ViewGroup) root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700159 return root;
160 }
161
Jason Monk39b46742015-09-10 15:52:51 -0400162 @Override
Jason Monk91e2f892016-02-23 15:31:09 -0500163 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
164 super.addPreferencesFromResource(preferencesResId);
165 checkAvailablePrefs(getPreferenceScreen());
166 }
167
168 private void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
169 if (preferenceGroup == null) return;
170 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
171 Preference pref = preferenceGroup.getPreference(i);
172 if (pref instanceof SelfAvailablePreference
173 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
174 preferenceGroup.removePreference(pref);
175 } else if (pref instanceof PreferenceGroup) {
176 checkAvailablePrefs((PreferenceGroup) pref);
177 }
178 }
179 }
180
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900181 public ViewGroup getButtonBar() {
182 return mButtonBar;
183 }
184
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700185 public View setPinnedHeaderView(int layoutResId) {
186 final LayoutInflater inflater = getActivity().getLayoutInflater();
187 final View pinnedHeader =
188 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
189 setPinnedHeaderView(pinnedHeader);
190 return pinnedHeader;
191 }
192
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700193 public void setPinnedHeaderView(View pinnedHeader) {
194 mPinnedHeaderFrameLayout.addView(pinnedHeader);
195 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
196 }
197
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700198 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700199 public void onSaveInstanceState(Bundle outState) {
200 super.onSaveInstanceState(outState);
201
202 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
203 }
204
205 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700206 public void onActivityCreated(Bundle savedInstanceState) {
207 super.onActivityCreated(savedInstanceState);
Johan Redestig76218e52016-04-19 08:29:30 +0200208 setHasOptionsMenu(true);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700209 }
210
211 @Override
212 public void onResume() {
213 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700214
215 final Bundle args = getArguments();
216 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700217 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
218 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700219 }
220 }
221
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700222 @Override
223 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700224 registerObserverIfNeeded();
225 }
226
227 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700228 protected void onUnbindPreferences() {
229 unregisterObserverIfNeeded();
230 }
231
Jason Monkb5aa73f2015-03-31 12:59:33 -0400232 public void showLoadingWhenEmpty() {
233 View loading = getView().findViewById(R.id.loading_container);
Jason Monk39b46742015-09-10 15:52:51 -0400234 setEmptyView(loading);
Jason Monkb5aa73f2015-03-31 12:59:33 -0400235 }
236
Jason Monkb37e2882016-01-11 14:27:20 -0500237 public void setLoading(boolean loading, boolean animate) {
238 View loading_container = getView().findViewById(R.id.loading_container);
239 Utils.handleLoadingContainer(loading_container, getListView(), !loading, animate);
240 }
241
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700242 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700243 if (!mIsDataSetObserverRegistered) {
244 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400245 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700246 }
Jason Monk39b46742015-09-10 15:52:51 -0400247 mCurrentRootAdapter = getListView().getAdapter();
248 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700249 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500250 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700251 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700252 }
253
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700254 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700255 if (mIsDataSetObserverRegistered) {
256 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400257 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700258 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700259 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700260 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700261 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700262 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700263
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700264 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700265 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700266 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700267 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700268 }
269
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000270 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400271 highlightPreferenceIfNeeded();
272 updateEmptyView();
273 }
274
Jason Monk39b46742015-09-10 15:52:51 -0400275 public LayoutPreference getHeaderView() {
276 return mHeader;
277 }
278
279 public LayoutPreference getFooterView() {
280 return mFooter;
281 }
282
283 protected void setHeaderView(int resource) {
284 mHeader = new LayoutPreference(getPrefContext(), resource);
Udam Sainid553abc2016-02-16 17:54:13 -0800285 addPreferenceToTop(mHeader);
286 }
287
288 protected void setHeaderView(View view) {
289 mHeader = new LayoutPreference(getPrefContext(), view);
290 addPreferenceToTop(mHeader);
291 }
292
293 private void addPreferenceToTop(LayoutPreference preference) {
294 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400295 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800296 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400297 }
298 }
299
300 protected void setFooterView(int resource) {
301 setFooterView(resource != 0 ? new LayoutPreference(getPrefContext(), resource) : null);
302 }
303
304 protected void setFooterView(View v) {
305 setFooterView(v != null ? new LayoutPreference(getPrefContext(), v) : null);
306 }
307
308 private void setFooterView(LayoutPreference footer) {
309 if (getPreferenceScreen() != null && mFooter != null) {
310 getPreferenceScreen().removePreference(mFooter);
311 }
312 if (footer != null) {
313 mFooter = footer;
Sudheer Shanka5590e2e2016-01-22 20:40:56 +0000314 mFooter.setOrder(ORDER_LAST);
Jason Monk39b46742015-09-10 15:52:51 -0400315 if (getPreferenceScreen() != null) {
316 getPreferenceScreen().addPreference(mFooter);
317 }
318 } else {
319 mFooter = null;
320 }
321 }
322
323 @Override
324 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400325 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400326 // Without ids generated, the RecyclerView won't animate changes to the preferences.
327 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
328 }
Jason Monk39b46742015-09-10 15:52:51 -0400329 super.setPreferenceScreen(preferenceScreen);
330 if (preferenceScreen != null) {
331 if (mHeader != null) {
332 preferenceScreen.addPreference(mHeader);
333 }
334 if (mFooter != null) {
335 preferenceScreen.addPreference(mFooter);
336 }
337 }
338 }
339
340 private void updateEmptyView() {
341 if (mEmptyView == null) return;
342 if (getPreferenceScreen() != null) {
343 boolean show = (getPreferenceScreen().getPreferenceCount()
344 - (mHeader != null ? 1 : 0)
345 - (mFooter != null ? 1 : 0)) <= 0;
346 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
347 } else {
348 mEmptyView.setVisibility(View.VISIBLE);
349 }
350 }
351
352 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000353 if (mEmptyView != null) {
354 mEmptyView.setVisibility(View.GONE);
355 }
Jason Monk39b46742015-09-10 15:52:51 -0400356 mEmptyView = v;
357 updateEmptyView();
358 }
359
360 public View getEmptyView() {
361 return mEmptyView;
362 }
363
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700364 /**
365 * Return a valid ListView position or -1 if none is found
366 */
367 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400368 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700369 return -1;
370 }
371
Jason Monk39b46742015-09-10 15:52:51 -0400372 RecyclerView listView = getListView();
373 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700374
375 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400376 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700377 }
378
379 return -1;
380 }
381
Jason Monk65bb0972015-12-17 10:39:44 -0500382 @Override
383 public RecyclerView.LayoutManager onCreateLayoutManager() {
384 mLayoutManager = new LinearLayoutManager(getContext());
385 return mLayoutManager;
386 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700387
Jason Monk65bb0972015-12-17 10:39:44 -0500388 @Override
389 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
390 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
391 return mAdapter;
392 }
393
Jason Monkf38fb382016-03-18 14:23:01 -0400394 protected void setAnimationAllowed(boolean animationAllowed) {
395 mAnimationAllowed = animationAllowed;
396 }
397
Jason Monk2071eda2016-02-25 13:55:48 -0500398 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
399 mPreferenceCache = new ArrayMap<String, Preference>();
400 final int N = group.getPreferenceCount();
401 for (int i = 0; i < N; i++) {
402 Preference p = group.getPreference(i);
403 if (TextUtils.isEmpty(p.getKey())) {
404 continue;
405 }
406 mPreferenceCache.put(p.getKey(), p);
407 }
408 }
409
410 protected Preference getCachedPreference(String key) {
411 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
412 }
413
414 protected void removeCachedPrefs(PreferenceGroup group) {
415 for (Preference p : mPreferenceCache.values()) {
416 group.removePreference(p);
417 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400418 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500419 }
420
Jason Monka6278442016-04-21 10:12:30 -0400421 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400422 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400423 }
424
Jason Monk65bb0972015-12-17 10:39:44 -0500425 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700426 final int position = canUseListViewForHighLighting(key);
427 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700428 mPreferenceHighlighted = true;
Jason Monk65bb0972015-12-17 10:39:44 -0500429 mLayoutManager.scrollToPosition(position);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700430
Jason Monk65bb0972015-12-17 10:39:44 -0500431 getView().postDelayed(new Runnable() {
432 @Override
433 public void run() {
434 mAdapter.highlight(position);
435 }
436 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700437 }
438 }
439
Jason Monk39b46742015-09-10 15:52:51 -0400440 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
441 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700442 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400443 final Preference preference = adapter.getItem(n);
444 final String preferenceKey = preference.getKey();
445 if (preferenceKey != null && preferenceKey.equals(key)) {
446 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700447 }
448 }
449 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700450 }
451
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700452 protected void removePreference(String key) {
453 Preference pref = findPreference(key);
454 if (pref != null) {
455 getPreferenceScreen().removePreference(pref);
456 }
457 }
458
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700459 /**
460 * Override this if you want to show a help item in the menu, by returning the resource id.
461 * @return the resource id for the help url
462 */
463 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400464 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700465 }
466
467 @Override
468 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400469 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400470 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700471 }
472 }
473
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700474 /*
475 * The name is intentionally made different from Activity#finish(), so that
476 * users won't misunderstand its meaning.
477 */
478 public final void finishFragment() {
479 getActivity().onBackPressed();
480 }
481
Amith Yamasanid7993472010-08-18 13:59:28 -0700482 // Some helpers for functions used by the settings fragments when they were activities
483
484 /**
485 * Returns the ContentResolver from the owning Activity.
486 */
487 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700488 Context context = getActivity();
489 if (context != null) {
490 mContentResolver = context.getContentResolver();
491 }
492 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700493 }
494
495 /**
496 * Returns the specified system service from the owning Activity.
497 */
498 protected Object getSystemService(final String name) {
499 return getActivity().getSystemService(name);
500 }
501
502 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700503 * Returns the PackageManager from the owning Activity.
504 */
505 protected PackageManager getPackageManager() {
506 return getActivity().getPackageManager();
507 }
508
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800509 @Override
510 public void onDetach() {
511 if (isRemoving()) {
512 if (mDialogFragment != null) {
513 mDialogFragment.dismiss();
514 mDialogFragment = null;
515 }
516 }
517 super.onDetach();
518 }
519
Amith Yamasanid7993472010-08-18 13:59:28 -0700520 // Dialog management
521
522 protected void showDialog(int dialogId) {
523 if (mDialogFragment != null) {
524 Log.e(TAG, "Old dialog fragment not null!");
525 }
526 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800527 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700528 }
529
Fan Zhangd65184f2016-09-19 17:45:24 -0700530 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700531 public Dialog onCreateDialog(int dialogId) {
532 return null;
533 }
534
Fan Zhangd65184f2016-09-19 17:45:24 -0700535 @Override
536 public int getDialogMetricsCategory(int dialogId) {
537 return 0;
538 }
539
Amith Yamasanid7993472010-08-18 13:59:28 -0700540 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800541 // mDialogFragment may not be visible yet in parent fragment's onResume().
542 // To be able to dismiss dialog at that time, don't check
543 // mDialogFragment.isVisible().
544 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400545 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700546 }
547 mDialogFragment = null;
548 }
549
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800550 /**
551 * Sets the OnCancelListener of the dialog shown. This method can only be
552 * called after showDialog(int) and before removeDialog(int). The method
553 * does nothing otherwise.
554 */
555 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
556 if (mDialogFragment != null) {
557 mDialogFragment.mOnCancelListener = listener;
558 }
559 }
560
561 /**
562 * Sets the OnDismissListener of the dialog shown. This method can only be
563 * called after showDialog(int) and before removeDialog(int). The method
564 * does nothing otherwise.
565 */
566 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
567 if (mDialogFragment != null) {
568 mDialogFragment.mOnDismissListener = listener;
569 }
570 }
571
Amith Yamasanic861cf82012-10-02 14:51:46 -0700572 public void onDialogShowing() {
573 // override in subclass to attach a dismiss listener, for instance
574 }
575
Jason Monk39b46742015-09-10 15:52:51 -0400576 @Override
577 public void onDisplayPreferenceDialog(Preference preference) {
578 if (preference.getKey() == null) {
579 // Auto-key preferences that don't have a key, so the dialog can find them.
580 preference.setKey(UUID.randomUUID().toString());
581 }
582 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000583 if (preference instanceof RestrictedListPreference) {
584 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
585 .newInstance(preference.getKey());
586 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400587 f = CustomListPreference.CustomListPreferenceDialogFragment
588 .newInstance(preference.getKey());
589 } else if (preference instanceof CustomDialogPreference) {
590 f = CustomDialogPreference.CustomPreferenceDialogFragment
591 .newInstance(preference.getKey());
592 } else if (preference instanceof CustomEditTextPreference) {
593 f = CustomEditTextPreference.CustomPreferenceDialogFragment
594 .newInstance(preference.getKey());
595 } else {
596 super.onDisplayPreferenceDialog(preference);
597 return;
598 }
599 f.setTargetFragment(this, 0);
600 f.show(getFragmentManager(), "dialog_preference");
601 onDialogShowing();
602 }
603
Fan Zhangd65184f2016-09-19 17:45:24 -0700604 public static class SettingsDialogFragment extends InstrumentedDialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800605 private static final String KEY_DIALOG_ID = "key_dialog_id";
606 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
607
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800608 private Fragment mParentFragment;
609
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800610 private DialogInterface.OnCancelListener mOnCancelListener;
611 private DialogInterface.OnDismissListener mOnDismissListener;
612
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800613 public SettingsDialogFragment() {
614 /* do nothing */
615 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700616
Amith Yamasani43c69782010-12-01 09:04:36 -0800617 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Fan Zhangd65184f2016-09-19 17:45:24 -0700618 super(fragment, dialogId);
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800619 if (!(fragment instanceof Fragment)) {
620 throw new IllegalArgumentException("fragment argument must be an instance of "
621 + Fragment.class.getName());
622 }
623 mParentFragment = (Fragment) fragment;
624 }
625
Fan Zhangd65184f2016-09-19 17:45:24 -0700626
627 @Override
628 public int getMetricsCategory() {
629 final int metricsCategory = mDialogCreatable.getDialogMetricsCategory(mDialogId);
630 if (metricsCategory <= 0) {
631 throw new IllegalStateException("Dialog must provide a metrics category");
632 }
633 return metricsCategory;
634 }
635
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800636 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800637 public void onSaveInstanceState(Bundle outState) {
638 super.onSaveInstanceState(outState);
639 if (mParentFragment != null) {
640 outState.putInt(KEY_DIALOG_ID, mDialogId);
641 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
642 }
643 }
644
645 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700646 public void onStart() {
647 super.onStart();
648
649 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
650 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
651 }
652 }
653
654 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800655 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800656 if (savedInstanceState != null) {
657 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800658 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800659 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700660 if (mParentFragment == null) {
661 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
662 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800663 if (!(mParentFragment instanceof DialogCreatable)) {
664 throw new IllegalArgumentException(
665 (mParentFragment != null
666 ? mParentFragment.getClass().getName()
667 : mParentFragmentId)
668 + " must implement "
669 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800670 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800671 // This dialog fragment could be created from non-SettingsPreferenceFragment
672 if (mParentFragment instanceof SettingsPreferenceFragment) {
673 // restore mDialogFragment in mParentFragment
674 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
675 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800676 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800677 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700678 }
679
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800680 @Override
681 public void onCancel(DialogInterface dialog) {
682 super.onCancel(dialog);
683 if (mOnCancelListener != null) {
684 mOnCancelListener.onCancel(dialog);
685 }
686 }
687
688 @Override
689 public void onDismiss(DialogInterface dialog) {
690 super.onDismiss(dialog);
691 if (mOnDismissListener != null) {
692 mOnDismissListener.onDismiss(dialog);
693 }
694 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800695
Amith Yamasanid7993472010-08-18 13:59:28 -0700696 public int getDialogId() {
697 return mDialogId;
698 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800699
700 @Override
701 public void onDetach() {
702 super.onDetach();
703
Amith Yamasani8875ede2011-01-31 12:46:57 -0800704 // This dialog fragment could be created from non-SettingsPreferenceFragment
705 if (mParentFragment instanceof SettingsPreferenceFragment) {
706 // in case the dialog is not explicitly removed by removeDialog()
707 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
708 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
709 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800710 }
711 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700712 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700713
714 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800715 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700716 }
717
718 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800719 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700720 }
721
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700722 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700723 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400724 if (activity == null) return;
725 if (getFragmentManager().getBackStackEntryCount() > 0) {
726 getFragmentManager().popBackStack();
727 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800728 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700729 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700730 }
731
Jason Monkb7e43802016-06-06 16:01:58 -0400732 protected Intent getIntent() {
733 if (getActivity() == null) {
734 return null;
735 }
736 return getActivity().getIntent();
737 }
738
739 protected void setResult(int result, Intent intent) {
740 if (getActivity() == null) {
741 return;
742 }
743 getActivity().setResult(result, intent);
744 }
745
746 protected void setResult(int result) {
747 if (getActivity() == null) {
748 return;
749 }
750 getActivity().setResult(result);
751 }
752
Jason Monk39b46742015-09-10 15:52:51 -0400753 protected final Context getPrefContext() {
754 return getPreferenceManager().getContext();
755 }
756
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700757 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
758 int requestCode, Bundle extras) {
759 final Activity activity = getActivity();
760 if (activity instanceof SettingsActivity) {
761 SettingsActivity sa = (SettingsActivity) activity;
762 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
763 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700764 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700765 Log.w(TAG,
766 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
767 + "launch the given Fragment (name: " + fragmentClass
768 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700769 return false;
770 }
771 }
Jason Monk65bb0972015-12-17 10:39:44 -0500772
773 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
774
775 private int mHighlightPosition = -1;
776
777 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
778 super(preferenceGroup);
779 }
780
781 public void highlight(int position) {
782 mHighlightPosition = position;
783 notifyDataSetChanged();
784 }
785
786 @Override
787 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
788 super.onBindViewHolder(holder, position);
789 if (position == mHighlightPosition) {
790 View v = holder.itemView;
791 if (v.getBackground() != null) {
792 final int centerX = v.getWidth() / 2;
793 final int centerY = v.getHeight() / 2;
794 v.getBackground().setHotspot(centerX, centerY);
795 }
796 v.setPressed(true);
797 v.setPressed(false);
798 mHighlightPosition = -1;
799 }
800 }
801 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700802}