blob: 28230a97aacfb6d03d10fe8f0fdf714d2affdd49 [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;
Fan Zhangd65184f2016-09-19 17:45:24 -070053import java.util.concurrent.atomic.AtomicInteger;
Jason Monk39b46742015-09-10 15:52:51 -040054
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070055/**
Amith Yamasanid7993472010-08-18 13:59:28 -070056 * Base class for Settings fragments, with some helper functions and dialog management.
57 */
Fan Zhangbf4be772016-08-04 16:05:55 -070058public abstract class SettingsPreferenceFragment extends InstrumentedFragment
Chris Wren8a963ba2015-03-20 10:29:14 -040059 implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070060
Anna Galusza0285c802016-01-29 17:32:19 -080061 /**
62 * The Help Uri Resource key. This can be passed as an extra argument when creating the
63 * Fragment.
64 **/
65 public static final String HELP_URI_RESOURCE_KEY = "help_uri_resource";
66
Jason Monk65bb0972015-12-17 10:39:44 -050067 private static final String TAG = "SettingsPreference";
Amith Yamasanid7993472010-08-18 13:59:28 -070068
Fabrice Di Meglioeced7802014-09-04 13:01:55 -070069 private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070070
71 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070072
Amith Yamasanid7993472010-08-18 13:59:28 -070073 private SettingsDialogFragment mDialogFragment;
74
Jason Monk23acc2b2015-04-14 15:06:39 -040075 private String mHelpUri;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070076
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000077 private static final int ORDER_FIRST = -1;
78 private static final int ORDER_LAST = Integer.MAX_VALUE -1;
79
Amith Yamasani350938e2013-04-09 10:22:47 -070080 // Cache the content resolver for async callbacks
81 private ContentResolver mContentResolver;
82
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070083 private String mPreferenceKey;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070084 private boolean mPreferenceHighlighted = false;
85
Jason Monk39b46742015-09-10 15:52:51 -040086 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070087 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040088 private RecyclerView.AdapterDataObserver mDataSetObserver =
89 new RecyclerView.AdapterDataObserver() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070090 @Override
91 public void onChanged() {
Jason Monk39b46742015-09-10 15:52:51 -040092 onDataSetChanged();
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070093 }
94 };
95
Fabrice Di Meglio86159282014-07-21 16:02:27 -070096 private ViewGroup mPinnedHeaderFrameLayout;
Daichi Hirono5e76cdc2015-07-08 11:38:55 +090097 private ViewGroup mButtonBar;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070098
Jason Monk39b46742015-09-10 15:52:51 -040099 private LayoutPreference mHeader;
100
101 private LayoutPreference mFooter;
102 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -0500103 private LinearLayoutManager mLayoutManager;
104 private HighlightablePreferenceGroupAdapter mAdapter;
Jason Monk2071eda2016-02-25 13:55:48 -0500105 private ArrayMap<String, Preference> mPreferenceCache;
Jason Monkf38fb382016-03-18 14:23:01 -0400106 private boolean mAnimationAllowed;
Jason Monk39b46742015-09-10 15:52:51 -0400107
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700108 @Override
109 public void onCreate(Bundle icicle) {
110 super.onCreate(icicle);
111
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700112 if (icicle != null) {
113 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
114 }
115
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700116 // Prepare help url and enable menu if necessary
Anna Galusza0285c802016-01-29 17:32:19 -0800117 Bundle arguments = getArguments();
118 int helpResource;
119 if (arguments != null && arguments.containsKey(HELP_URI_RESOURCE_KEY)) {
120 helpResource = arguments.getInt(HELP_URI_RESOURCE_KEY);
121 } else {
122 helpResource = getHelpResource();
123 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700124 if (helpResource != 0) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400125 mHelpUri = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700126 }
127 }
128
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700129 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700130 public View onCreateView(LayoutInflater inflater, ViewGroup container,
131 Bundle savedInstanceState) {
132 final View root = super.onCreateView(inflater, container, savedInstanceState);
133 mPinnedHeaderFrameLayout = (ViewGroup) root.findViewById(R.id.pinned_header);
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
Jason Monk91e2f892016-02-23 15:31:09 -0500139 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
140 super.addPreferencesFromResource(preferencesResId);
141 checkAvailablePrefs(getPreferenceScreen());
142 }
143
144 private void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
145 if (preferenceGroup == null) return;
146 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
147 Preference pref = preferenceGroup.getPreference(i);
148 if (pref instanceof SelfAvailablePreference
149 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
150 preferenceGroup.removePreference(pref);
151 } else if (pref instanceof PreferenceGroup) {
152 checkAvailablePrefs((PreferenceGroup) pref);
153 }
154 }
155 }
156
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900157 public ViewGroup getButtonBar() {
158 return mButtonBar;
159 }
160
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700161 public View setPinnedHeaderView(int layoutResId) {
162 final LayoutInflater inflater = getActivity().getLayoutInflater();
163 final View pinnedHeader =
164 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
165 setPinnedHeaderView(pinnedHeader);
166 return pinnedHeader;
167 }
168
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700169 public void setPinnedHeaderView(View pinnedHeader) {
170 mPinnedHeaderFrameLayout.addView(pinnedHeader);
171 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
172 }
173
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700174 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700175 public void onSaveInstanceState(Bundle outState) {
176 super.onSaveInstanceState(outState);
177
178 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
179 }
180
181 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700182 public void onActivityCreated(Bundle savedInstanceState) {
183 super.onActivityCreated(savedInstanceState);
Johan Redestig76218e52016-04-19 08:29:30 +0200184 setHasOptionsMenu(true);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700185 }
186
187 @Override
188 public void onResume() {
189 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700190
191 final Bundle args = getArguments();
192 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700193 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
194 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700195 }
196 }
197
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700198 @Override
199 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700200 registerObserverIfNeeded();
201 }
202
203 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700204 protected void onUnbindPreferences() {
205 unregisterObserverIfNeeded();
206 }
207
Jason Monkb5aa73f2015-03-31 12:59:33 -0400208 public void showLoadingWhenEmpty() {
209 View loading = getView().findViewById(R.id.loading_container);
Jason Monk39b46742015-09-10 15:52:51 -0400210 setEmptyView(loading);
Jason Monkb5aa73f2015-03-31 12:59:33 -0400211 }
212
Jason Monkb37e2882016-01-11 14:27:20 -0500213 public void setLoading(boolean loading, boolean animate) {
214 View loading_container = getView().findViewById(R.id.loading_container);
215 Utils.handleLoadingContainer(loading_container, getListView(), !loading, animate);
216 }
217
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700218 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700219 if (!mIsDataSetObserverRegistered) {
220 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400221 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700222 }
Jason Monk39b46742015-09-10 15:52:51 -0400223 mCurrentRootAdapter = getListView().getAdapter();
224 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700225 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500226 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700227 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700228 }
229
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700230 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700231 if (mIsDataSetObserverRegistered) {
232 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400233 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700234 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700235 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700236 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700237 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700238 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700239
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700240 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700241 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700242 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700243 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700244 }
245
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000246 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400247 highlightPreferenceIfNeeded();
248 updateEmptyView();
249 }
250
Jason Monk39b46742015-09-10 15:52:51 -0400251 public LayoutPreference getHeaderView() {
252 return mHeader;
253 }
254
255 public LayoutPreference getFooterView() {
256 return mFooter;
257 }
258
259 protected void setHeaderView(int resource) {
260 mHeader = new LayoutPreference(getPrefContext(), resource);
Udam Sainid553abc2016-02-16 17:54:13 -0800261 addPreferenceToTop(mHeader);
262 }
263
264 protected void setHeaderView(View view) {
265 mHeader = new LayoutPreference(getPrefContext(), view);
266 addPreferenceToTop(mHeader);
267 }
268
269 private void addPreferenceToTop(LayoutPreference preference) {
270 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400271 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800272 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400273 }
274 }
275
276 protected void setFooterView(int resource) {
277 setFooterView(resource != 0 ? new LayoutPreference(getPrefContext(), resource) : null);
278 }
279
280 protected void setFooterView(View v) {
281 setFooterView(v != null ? new LayoutPreference(getPrefContext(), v) : null);
282 }
283
284 private void setFooterView(LayoutPreference footer) {
285 if (getPreferenceScreen() != null && mFooter != null) {
286 getPreferenceScreen().removePreference(mFooter);
287 }
288 if (footer != null) {
289 mFooter = footer;
Sudheer Shanka5590e2e2016-01-22 20:40:56 +0000290 mFooter.setOrder(ORDER_LAST);
Jason Monk39b46742015-09-10 15:52:51 -0400291 if (getPreferenceScreen() != null) {
292 getPreferenceScreen().addPreference(mFooter);
293 }
294 } else {
295 mFooter = null;
296 }
297 }
298
299 @Override
300 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400301 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400302 // Without ids generated, the RecyclerView won't animate changes to the preferences.
303 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
304 }
Jason Monk39b46742015-09-10 15:52:51 -0400305 super.setPreferenceScreen(preferenceScreen);
306 if (preferenceScreen != null) {
307 if (mHeader != null) {
308 preferenceScreen.addPreference(mHeader);
309 }
310 if (mFooter != null) {
311 preferenceScreen.addPreference(mFooter);
312 }
313 }
314 }
315
316 private void updateEmptyView() {
317 if (mEmptyView == null) return;
318 if (getPreferenceScreen() != null) {
319 boolean show = (getPreferenceScreen().getPreferenceCount()
320 - (mHeader != null ? 1 : 0)
321 - (mFooter != null ? 1 : 0)) <= 0;
322 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
323 } else {
324 mEmptyView.setVisibility(View.VISIBLE);
325 }
326 }
327
328 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000329 if (mEmptyView != null) {
330 mEmptyView.setVisibility(View.GONE);
331 }
Jason Monk39b46742015-09-10 15:52:51 -0400332 mEmptyView = v;
333 updateEmptyView();
334 }
335
336 public View getEmptyView() {
337 return mEmptyView;
338 }
339
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700340 /**
341 * Return a valid ListView position or -1 if none is found
342 */
343 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400344 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700345 return -1;
346 }
347
Jason Monk39b46742015-09-10 15:52:51 -0400348 RecyclerView listView = getListView();
349 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700350
351 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400352 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700353 }
354
355 return -1;
356 }
357
Jason Monk65bb0972015-12-17 10:39:44 -0500358 @Override
359 public RecyclerView.LayoutManager onCreateLayoutManager() {
360 mLayoutManager = new LinearLayoutManager(getContext());
361 return mLayoutManager;
362 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700363
Jason Monk65bb0972015-12-17 10:39:44 -0500364 @Override
365 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
366 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
367 return mAdapter;
368 }
369
Jason Monkf38fb382016-03-18 14:23:01 -0400370 protected void setAnimationAllowed(boolean animationAllowed) {
371 mAnimationAllowed = animationAllowed;
372 }
373
Jason Monk2071eda2016-02-25 13:55:48 -0500374 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
375 mPreferenceCache = new ArrayMap<String, Preference>();
376 final int N = group.getPreferenceCount();
377 for (int i = 0; i < N; i++) {
378 Preference p = group.getPreference(i);
379 if (TextUtils.isEmpty(p.getKey())) {
380 continue;
381 }
382 mPreferenceCache.put(p.getKey(), p);
383 }
384 }
385
386 protected Preference getCachedPreference(String key) {
387 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
388 }
389
390 protected void removeCachedPrefs(PreferenceGroup group) {
391 for (Preference p : mPreferenceCache.values()) {
392 group.removePreference(p);
393 }
Jason Monkdb7868e2016-06-30 15:17:57 -0400394 mPreferenceCache = null;
Jason Monk2071eda2016-02-25 13:55:48 -0500395 }
396
Jason Monka6278442016-04-21 10:12:30 -0400397 protected int getCachedCount() {
Jason Monkdb7868e2016-06-30 15:17:57 -0400398 return mPreferenceCache != null ? mPreferenceCache.size() : 0;
Jason Monka6278442016-04-21 10:12:30 -0400399 }
400
Jason Monk65bb0972015-12-17 10:39:44 -0500401 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700402 final int position = canUseListViewForHighLighting(key);
403 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700404 mPreferenceHighlighted = true;
Jason Monk65bb0972015-12-17 10:39:44 -0500405 mLayoutManager.scrollToPosition(position);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700406
Jason Monk65bb0972015-12-17 10:39:44 -0500407 getView().postDelayed(new Runnable() {
408 @Override
409 public void run() {
410 mAdapter.highlight(position);
411 }
412 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700413 }
414 }
415
Jason Monk39b46742015-09-10 15:52:51 -0400416 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
417 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700418 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400419 final Preference preference = adapter.getItem(n);
420 final String preferenceKey = preference.getKey();
421 if (preferenceKey != null && preferenceKey.equals(key)) {
422 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700423 }
424 }
425 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700426 }
427
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700428 protected void removePreference(String key) {
429 Preference pref = findPreference(key);
430 if (pref != null) {
431 getPreferenceScreen().removePreference(pref);
432 }
433 }
434
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700435 /**
436 * Override this if you want to show a help item in the menu, by returning the resource id.
437 * @return the resource id for the help url
438 */
439 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400440 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700441 }
442
443 @Override
444 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400445 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400446 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700447 }
448 }
449
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700450 /*
451 * The name is intentionally made different from Activity#finish(), so that
452 * users won't misunderstand its meaning.
453 */
454 public final void finishFragment() {
455 getActivity().onBackPressed();
456 }
457
Amith Yamasanid7993472010-08-18 13:59:28 -0700458 // Some helpers for functions used by the settings fragments when they were activities
459
460 /**
461 * Returns the ContentResolver from the owning Activity.
462 */
463 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700464 Context context = getActivity();
465 if (context != null) {
466 mContentResolver = context.getContentResolver();
467 }
468 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700469 }
470
471 /**
472 * Returns the specified system service from the owning Activity.
473 */
474 protected Object getSystemService(final String name) {
475 return getActivity().getSystemService(name);
476 }
477
478 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700479 * Returns the PackageManager from the owning Activity.
480 */
481 protected PackageManager getPackageManager() {
482 return getActivity().getPackageManager();
483 }
484
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800485 @Override
486 public void onDetach() {
487 if (isRemoving()) {
488 if (mDialogFragment != null) {
489 mDialogFragment.dismiss();
490 mDialogFragment = null;
491 }
492 }
493 super.onDetach();
494 }
495
Amith Yamasanid7993472010-08-18 13:59:28 -0700496 // Dialog management
497
498 protected void showDialog(int dialogId) {
499 if (mDialogFragment != null) {
500 Log.e(TAG, "Old dialog fragment not null!");
501 }
502 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800503 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700504 }
505
Fan Zhangd65184f2016-09-19 17:45:24 -0700506 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700507 public Dialog onCreateDialog(int dialogId) {
508 return null;
509 }
510
Fan Zhangd65184f2016-09-19 17:45:24 -0700511 @Override
512 public int getDialogMetricsCategory(int dialogId) {
513 return 0;
514 }
515
Amith Yamasanid7993472010-08-18 13:59:28 -0700516 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800517 // mDialogFragment may not be visible yet in parent fragment's onResume().
518 // To be able to dismiss dialog at that time, don't check
519 // mDialogFragment.isVisible().
520 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Jason Monk8a7d0742016-07-15 13:18:48 -0400521 mDialogFragment.dismissAllowingStateLoss();
Amith Yamasanid7993472010-08-18 13:59:28 -0700522 }
523 mDialogFragment = null;
524 }
525
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800526 /**
527 * Sets the OnCancelListener of the dialog shown. This method can only be
528 * called after showDialog(int) and before removeDialog(int). The method
529 * does nothing otherwise.
530 */
531 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
532 if (mDialogFragment != null) {
533 mDialogFragment.mOnCancelListener = listener;
534 }
535 }
536
537 /**
538 * Sets the OnDismissListener of the dialog shown. This method can only be
539 * called after showDialog(int) and before removeDialog(int). The method
540 * does nothing otherwise.
541 */
542 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
543 if (mDialogFragment != null) {
544 mDialogFragment.mOnDismissListener = listener;
545 }
546 }
547
Amith Yamasanic861cf82012-10-02 14:51:46 -0700548 public void onDialogShowing() {
549 // override in subclass to attach a dismiss listener, for instance
550 }
551
Jason Monk39b46742015-09-10 15:52:51 -0400552 @Override
553 public void onDisplayPreferenceDialog(Preference preference) {
554 if (preference.getKey() == null) {
555 // Auto-key preferences that don't have a key, so the dialog can find them.
556 preference.setKey(UUID.randomUUID().toString());
557 }
558 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000559 if (preference instanceof RestrictedListPreference) {
560 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
561 .newInstance(preference.getKey());
562 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400563 f = CustomListPreference.CustomListPreferenceDialogFragment
564 .newInstance(preference.getKey());
565 } else if (preference instanceof CustomDialogPreference) {
566 f = CustomDialogPreference.CustomPreferenceDialogFragment
567 .newInstance(preference.getKey());
568 } else if (preference instanceof CustomEditTextPreference) {
569 f = CustomEditTextPreference.CustomPreferenceDialogFragment
570 .newInstance(preference.getKey());
571 } else {
572 super.onDisplayPreferenceDialog(preference);
573 return;
574 }
575 f.setTargetFragment(this, 0);
576 f.show(getFragmentManager(), "dialog_preference");
577 onDialogShowing();
578 }
579
Fan Zhangd65184f2016-09-19 17:45:24 -0700580 public static class SettingsDialogFragment extends InstrumentedDialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800581 private static final String KEY_DIALOG_ID = "key_dialog_id";
582 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
583
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800584 private Fragment mParentFragment;
585
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800586 private DialogInterface.OnCancelListener mOnCancelListener;
587 private DialogInterface.OnDismissListener mOnDismissListener;
588
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800589 public SettingsDialogFragment() {
590 /* do nothing */
591 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700592
Amith Yamasani43c69782010-12-01 09:04:36 -0800593 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Fan Zhangd65184f2016-09-19 17:45:24 -0700594 super(fragment, dialogId);
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800595 if (!(fragment instanceof Fragment)) {
596 throw new IllegalArgumentException("fragment argument must be an instance of "
597 + Fragment.class.getName());
598 }
599 mParentFragment = (Fragment) fragment;
600 }
601
Fan Zhangd65184f2016-09-19 17:45:24 -0700602
603 @Override
604 public int getMetricsCategory() {
605 final int metricsCategory = mDialogCreatable.getDialogMetricsCategory(mDialogId);
606 if (metricsCategory <= 0) {
607 throw new IllegalStateException("Dialog must provide a metrics category");
608 }
609 return metricsCategory;
610 }
611
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800612 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800613 public void onSaveInstanceState(Bundle outState) {
614 super.onSaveInstanceState(outState);
615 if (mParentFragment != null) {
616 outState.putInt(KEY_DIALOG_ID, mDialogId);
617 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
618 }
619 }
620
621 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700622 public void onStart() {
623 super.onStart();
624
625 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
626 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
627 }
628 }
629
630 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800631 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800632 if (savedInstanceState != null) {
633 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800634 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800635 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700636 if (mParentFragment == null) {
637 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
638 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800639 if (!(mParentFragment instanceof DialogCreatable)) {
640 throw new IllegalArgumentException(
641 (mParentFragment != null
642 ? mParentFragment.getClass().getName()
643 : mParentFragmentId)
644 + " must implement "
645 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800646 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800647 // This dialog fragment could be created from non-SettingsPreferenceFragment
648 if (mParentFragment instanceof SettingsPreferenceFragment) {
649 // restore mDialogFragment in mParentFragment
650 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
651 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800652 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800653 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700654 }
655
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800656 @Override
657 public void onCancel(DialogInterface dialog) {
658 super.onCancel(dialog);
659 if (mOnCancelListener != null) {
660 mOnCancelListener.onCancel(dialog);
661 }
662 }
663
664 @Override
665 public void onDismiss(DialogInterface dialog) {
666 super.onDismiss(dialog);
667 if (mOnDismissListener != null) {
668 mOnDismissListener.onDismiss(dialog);
669 }
670 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800671
Amith Yamasanid7993472010-08-18 13:59:28 -0700672 public int getDialogId() {
673 return mDialogId;
674 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800675
676 @Override
677 public void onDetach() {
678 super.onDetach();
679
Amith Yamasani8875ede2011-01-31 12:46:57 -0800680 // This dialog fragment could be created from non-SettingsPreferenceFragment
681 if (mParentFragment instanceof SettingsPreferenceFragment) {
682 // in case the dialog is not explicitly removed by removeDialog()
683 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
684 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
685 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800686 }
687 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700688 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700689
690 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800691 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700692 }
693
694 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800695 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700696 }
697
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700698 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700699 Activity activity = getActivity();
Jason Monk656bc602016-06-10 09:49:12 -0400700 if (activity == null) return;
701 if (getFragmentManager().getBackStackEntryCount() > 0) {
702 getFragmentManager().popBackStack();
703 } else {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800704 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700705 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700706 }
707
Jason Monkb7e43802016-06-06 16:01:58 -0400708 protected Intent getIntent() {
709 if (getActivity() == null) {
710 return null;
711 }
712 return getActivity().getIntent();
713 }
714
715 protected void setResult(int result, Intent intent) {
716 if (getActivity() == null) {
717 return;
718 }
719 getActivity().setResult(result, intent);
720 }
721
722 protected void setResult(int result) {
723 if (getActivity() == null) {
724 return;
725 }
726 getActivity().setResult(result);
727 }
728
Jason Monk39b46742015-09-10 15:52:51 -0400729 protected final Context getPrefContext() {
730 return getPreferenceManager().getContext();
731 }
732
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700733 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
734 int requestCode, Bundle extras) {
735 final Activity activity = getActivity();
736 if (activity instanceof SettingsActivity) {
737 SettingsActivity sa = (SettingsActivity) activity;
738 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
739 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700740 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700741 Log.w(TAG,
742 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
743 + "launch the given Fragment (name: " + fragmentClass
744 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700745 return false;
746 }
747 }
Jason Monk65bb0972015-12-17 10:39:44 -0500748
749 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
750
751 private int mHighlightPosition = -1;
752
753 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
754 super(preferenceGroup);
755 }
756
757 public void highlight(int position) {
758 mHighlightPosition = position;
759 notifyDataSetChanged();
760 }
761
762 @Override
763 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
764 super.onBindViewHolder(holder, position);
765 if (position == mHighlightPosition) {
766 View v = holder.itemView;
767 if (v.getBackground() != null) {
768 final int centerX = v.getWidth() / 2;
769 final int centerY = v.getHeight() / 2;
770 v.getBackground().setHotspot(centerX, centerY);
771 }
772 v.setPressed(true);
773 v.setPressed(false);
774 mHighlightPosition = -1;
775 }
776 }
777 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700778}