blob: a992897623f49865b9a9e23e4b952a525eaab9b4 [file] [log] [blame]
Amith Yamasanid7993472010-08-18 13:59:28 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -070019import android.app.Activity;
Amith Yamasanid7993472010-08-18 13:59:28 -070020import android.app.Dialog;
21import android.app.DialogFragment;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -070022import android.app.Fragment;
Amith Yamasanid7993472010-08-18 13:59:28 -070023import android.content.ContentResolver;
Amith Yamasani350938e2013-04-09 10:22:47 -070024import android.content.Context;
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +080025import android.content.DialogInterface;
Amith Yamasanid7993472010-08-18 13:59:28 -070026import android.content.pm.PackageManager;
Amith Yamasanid7993472010-08-18 13:59:28 -070027import android.os.Bundle;
Jason Monk91e2f892016-02-23 15:31:09 -050028import android.support.annotation.XmlRes;
Jason Monk39b46742015-09-10 15:52:51 -040029import android.support.v7.preference.Preference;
Jason Monk65bb0972015-12-17 10:39:44 -050030import android.support.v7.preference.PreferenceGroup;
Jason Monk39b46742015-09-10 15:52:51 -040031import android.support.v7.preference.PreferenceGroupAdapter;
32import android.support.v7.preference.PreferenceScreen;
Jason Monk65bb0972015-12-17 10:39:44 -050033import android.support.v7.preference.PreferenceViewHolder;
34import android.support.v7.widget.LinearLayoutManager;
Jason Monk39b46742015-09-10 15:52:51 -040035import android.support.v7.widget.RecyclerView;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070036import android.text.TextUtils;
Jason Monk2071eda2016-02-25 13:55:48 -050037import android.util.ArrayMap;
Amith Yamasanid7993472010-08-18 13:59:28 -070038import android.util.Log;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070039import android.view.LayoutInflater;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070040import android.view.Menu;
41import android.view.MenuInflater;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070042import android.view.View;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070043import android.view.ViewGroup;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070044import android.widget.Button;
Jason Monk39b46742015-09-10 15:52:51 -040045import com.android.settings.applications.LayoutPreference;
John Spurlockb8e02b82015-04-15 21:15:55 -040046import com.android.settings.widget.FloatingActionButton;
Suprabh Shuklab84720c2016-04-05 14:37:20 -070047import com.android.settingslib.HelpUtils;
John Spurlockb8e02b82015-04-15 21:15:55 -040048
Jason Monk39b46742015-09-10 15:52:51 -040049import java.util.UUID;
50
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070051/**
Amith Yamasanid7993472010-08-18 13:59:28 -070052 * Base class for Settings fragments, with some helper functions and dialog management.
53 */
Chris Wren8a963ba2015-03-20 10:29:14 -040054public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceFragment
55 implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070056
Anna Galusza0285c802016-01-29 17:32:19 -080057 /**
58 * The Help Uri Resource key. This can be passed as an extra argument when creating the
59 * Fragment.
60 **/
61 public static final String HELP_URI_RESOURCE_KEY = "help_uri_resource";
62
Jason Monk65bb0972015-12-17 10:39:44 -050063 private static final String TAG = "SettingsPreference";
Amith Yamasanid7993472010-08-18 13:59:28 -070064
Fabrice Di Meglioeced7802014-09-04 13:01:55 -070065 private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070066
67 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070068
Amith Yamasanid7993472010-08-18 13:59:28 -070069 private SettingsDialogFragment mDialogFragment;
70
Jason Monk23acc2b2015-04-14 15:06:39 -040071 private String mHelpUri;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070072
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000073 private static final int ORDER_FIRST = -1;
74 private static final int ORDER_LAST = Integer.MAX_VALUE -1;
75
Amith Yamasani350938e2013-04-09 10:22:47 -070076 // Cache the content resolver for async callbacks
77 private ContentResolver mContentResolver;
78
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070079 private String mPreferenceKey;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070080 private boolean mPreferenceHighlighted = false;
81
Jason Monk39b46742015-09-10 15:52:51 -040082 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070083 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040084 private RecyclerView.AdapterDataObserver mDataSetObserver =
85 new RecyclerView.AdapterDataObserver() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070086 @Override
87 public void onChanged() {
Jason Monk39b46742015-09-10 15:52:51 -040088 onDataSetChanged();
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070089 }
90 };
91
Fabrice Di Meglio86159282014-07-21 16:02:27 -070092 private ViewGroup mPinnedHeaderFrameLayout;
John Spurlockb8e02b82015-04-15 21:15:55 -040093 private FloatingActionButton mFloatingActionButton;
Daichi Hirono5e76cdc2015-07-08 11:38:55 +090094 private ViewGroup mButtonBar;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070095
Jason Monk39b46742015-09-10 15:52:51 -040096 private LayoutPreference mHeader;
97
98 private LayoutPreference mFooter;
99 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -0500100 private LinearLayoutManager mLayoutManager;
101 private HighlightablePreferenceGroupAdapter mAdapter;
Jason Monk2071eda2016-02-25 13:55:48 -0500102 private ArrayMap<String, Preference> mPreferenceCache;
Jason Monkf38fb382016-03-18 14:23:01 -0400103 private boolean mAnimationAllowed;
Jason Monk39b46742015-09-10 15:52:51 -0400104
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700105 @Override
106 public void onCreate(Bundle icicle) {
107 super.onCreate(icicle);
108
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700109 if (icicle != null) {
110 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
111 }
112
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700113 // Prepare help url and enable menu if necessary
Anna Galusza0285c802016-01-29 17:32:19 -0800114 Bundle arguments = getArguments();
115 int helpResource;
116 if (arguments != null && arguments.containsKey(HELP_URI_RESOURCE_KEY)) {
117 helpResource = arguments.getInt(HELP_URI_RESOURCE_KEY);
118 } else {
119 helpResource = getHelpResource();
120 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700121 if (helpResource != 0) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400122 mHelpUri = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700123 }
124 }
125
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700126 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700127 public View onCreateView(LayoutInflater inflater, ViewGroup container,
128 Bundle savedInstanceState) {
129 final View root = super.onCreateView(inflater, container, savedInstanceState);
130 mPinnedHeaderFrameLayout = (ViewGroup) root.findViewById(R.id.pinned_header);
John Spurlockb8e02b82015-04-15 21:15:55 -0400131 mFloatingActionButton = (FloatingActionButton) root.findViewById(R.id.fab);
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900132 mButtonBar = (ViewGroup) root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700133 return root;
134 }
135
Jason Monk39b46742015-09-10 15:52:51 -0400136 @Override
137 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
138 }
139
Jason Monk91e2f892016-02-23 15:31:09 -0500140 @Override
141 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
142 super.addPreferencesFromResource(preferencesResId);
143 checkAvailablePrefs(getPreferenceScreen());
144 }
145
146 private void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
147 if (preferenceGroup == null) return;
148 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
149 Preference pref = preferenceGroup.getPreference(i);
150 if (pref instanceof SelfAvailablePreference
151 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
152 preferenceGroup.removePreference(pref);
153 } else if (pref instanceof PreferenceGroup) {
154 checkAvailablePrefs((PreferenceGroup) pref);
155 }
156 }
157 }
158
John Spurlockb8e02b82015-04-15 21:15:55 -0400159 public FloatingActionButton getFloatingActionButton() {
160 return mFloatingActionButton;
161 }
162
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900163 public ViewGroup getButtonBar() {
164 return mButtonBar;
165 }
166
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700167 public View setPinnedHeaderView(int layoutResId) {
168 final LayoutInflater inflater = getActivity().getLayoutInflater();
169 final View pinnedHeader =
170 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
171 setPinnedHeaderView(pinnedHeader);
172 return pinnedHeader;
173 }
174
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700175 public void setPinnedHeaderView(View pinnedHeader) {
176 mPinnedHeaderFrameLayout.addView(pinnedHeader);
177 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
178 }
179
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700180 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700181 public void onSaveInstanceState(Bundle outState) {
182 super.onSaveInstanceState(outState);
183
184 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
185 }
186
187 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700188 public void onActivityCreated(Bundle savedInstanceState) {
189 super.onActivityCreated(savedInstanceState);
Johan Redestig76218e52016-04-19 08:29:30 +0200190 setHasOptionsMenu(true);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700191 }
192
193 @Override
194 public void onResume() {
195 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700196
197 final Bundle args = getArguments();
198 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700199 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
200 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700201 }
202 }
203
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700204 @Override
205 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700206 registerObserverIfNeeded();
207 }
208
209 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700210 protected void onUnbindPreferences() {
211 unregisterObserverIfNeeded();
212 }
213
Jason Monkb5aa73f2015-03-31 12:59:33 -0400214 public void showLoadingWhenEmpty() {
215 View loading = getView().findViewById(R.id.loading_container);
Jason Monk39b46742015-09-10 15:52:51 -0400216 setEmptyView(loading);
Jason Monkb5aa73f2015-03-31 12:59:33 -0400217 }
218
Jason Monkb37e2882016-01-11 14:27:20 -0500219 public void setLoading(boolean loading, boolean animate) {
220 View loading_container = getView().findViewById(R.id.loading_container);
221 Utils.handleLoadingContainer(loading_container, getListView(), !loading, animate);
222 }
223
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700224 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700225 if (!mIsDataSetObserverRegistered) {
226 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400227 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700228 }
Jason Monk39b46742015-09-10 15:52:51 -0400229 mCurrentRootAdapter = getListView().getAdapter();
230 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700231 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500232 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700233 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700234 }
235
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700236 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700237 if (mIsDataSetObserverRegistered) {
238 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400239 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700240 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700241 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700242 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700243 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700244 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700245
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700246 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700247 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700248 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700249 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700250 }
251
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000252 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400253 highlightPreferenceIfNeeded();
254 updateEmptyView();
255 }
256
Jason Monk39b46742015-09-10 15:52:51 -0400257 public LayoutPreference getHeaderView() {
258 return mHeader;
259 }
260
261 public LayoutPreference getFooterView() {
262 return mFooter;
263 }
264
265 protected void setHeaderView(int resource) {
266 mHeader = new LayoutPreference(getPrefContext(), resource);
Udam Sainid553abc2016-02-16 17:54:13 -0800267 addPreferenceToTop(mHeader);
268 }
269
270 protected void setHeaderView(View view) {
271 mHeader = new LayoutPreference(getPrefContext(), view);
272 addPreferenceToTop(mHeader);
273 }
274
275 private void addPreferenceToTop(LayoutPreference preference) {
276 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400277 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800278 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400279 }
280 }
281
282 protected void setFooterView(int resource) {
283 setFooterView(resource != 0 ? new LayoutPreference(getPrefContext(), resource) : null);
284 }
285
286 protected void setFooterView(View v) {
287 setFooterView(v != null ? new LayoutPreference(getPrefContext(), v) : null);
288 }
289
290 private void setFooterView(LayoutPreference footer) {
291 if (getPreferenceScreen() != null && mFooter != null) {
292 getPreferenceScreen().removePreference(mFooter);
293 }
294 if (footer != null) {
295 mFooter = footer;
Sudheer Shanka5590e2e2016-01-22 20:40:56 +0000296 mFooter.setOrder(ORDER_LAST);
Jason Monk39b46742015-09-10 15:52:51 -0400297 if (getPreferenceScreen() != null) {
298 getPreferenceScreen().addPreference(mFooter);
299 }
300 } else {
301 mFooter = null;
302 }
303 }
304
305 @Override
306 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Jason Monk1cb12bb2016-03-29 13:21:48 -0400307 if (preferenceScreen != null && !preferenceScreen.isAttached()) {
Jason Monkf38fb382016-03-18 14:23:01 -0400308 // Without ids generated, the RecyclerView won't animate changes to the preferences.
309 preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed);
310 }
Jason Monk39b46742015-09-10 15:52:51 -0400311 super.setPreferenceScreen(preferenceScreen);
312 if (preferenceScreen != null) {
313 if (mHeader != null) {
314 preferenceScreen.addPreference(mHeader);
315 }
316 if (mFooter != null) {
317 preferenceScreen.addPreference(mFooter);
318 }
319 }
320 }
321
322 private void updateEmptyView() {
323 if (mEmptyView == null) return;
324 if (getPreferenceScreen() != null) {
325 boolean show = (getPreferenceScreen().getPreferenceCount()
326 - (mHeader != null ? 1 : 0)
327 - (mFooter != null ? 1 : 0)) <= 0;
328 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
329 } else {
330 mEmptyView.setVisibility(View.VISIBLE);
331 }
332 }
333
334 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000335 if (mEmptyView != null) {
336 mEmptyView.setVisibility(View.GONE);
337 }
Jason Monk39b46742015-09-10 15:52:51 -0400338 mEmptyView = v;
339 updateEmptyView();
340 }
341
342 public View getEmptyView() {
343 return mEmptyView;
344 }
345
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700346 /**
347 * Return a valid ListView position or -1 if none is found
348 */
349 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400350 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700351 return -1;
352 }
353
Jason Monk39b46742015-09-10 15:52:51 -0400354 RecyclerView listView = getListView();
355 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700356
357 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400358 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700359 }
360
361 return -1;
362 }
363
Jason Monk65bb0972015-12-17 10:39:44 -0500364 @Override
365 public RecyclerView.LayoutManager onCreateLayoutManager() {
366 mLayoutManager = new LinearLayoutManager(getContext());
367 return mLayoutManager;
368 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700369
Jason Monk65bb0972015-12-17 10:39:44 -0500370 @Override
371 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
372 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
373 return mAdapter;
374 }
375
Jason Monkf38fb382016-03-18 14:23:01 -0400376 protected void setAnimationAllowed(boolean animationAllowed) {
377 mAnimationAllowed = animationAllowed;
378 }
379
Jason Monk2071eda2016-02-25 13:55:48 -0500380 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
381 mPreferenceCache = new ArrayMap<String, Preference>();
382 final int N = group.getPreferenceCount();
383 for (int i = 0; i < N; i++) {
384 Preference p = group.getPreference(i);
385 if (TextUtils.isEmpty(p.getKey())) {
386 continue;
387 }
388 mPreferenceCache.put(p.getKey(), p);
389 }
390 }
391
392 protected Preference getCachedPreference(String key) {
393 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
394 }
395
396 protected void removeCachedPrefs(PreferenceGroup group) {
397 for (Preference p : mPreferenceCache.values()) {
398 group.removePreference(p);
399 }
400 }
401
Jason Monka6278442016-04-21 10:12:30 -0400402 protected int getCachedCount() {
403 return mPreferenceCache.size();
404 }
405
Jason Monk65bb0972015-12-17 10:39:44 -0500406 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700407 final int position = canUseListViewForHighLighting(key);
408 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700409 mPreferenceHighlighted = true;
Jason Monk65bb0972015-12-17 10:39:44 -0500410 mLayoutManager.scrollToPosition(position);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700411
Jason Monk65bb0972015-12-17 10:39:44 -0500412 getView().postDelayed(new Runnable() {
413 @Override
414 public void run() {
415 mAdapter.highlight(position);
416 }
417 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700418 }
419 }
420
Jason Monk39b46742015-09-10 15:52:51 -0400421 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
422 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700423 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400424 final Preference preference = adapter.getItem(n);
425 final String preferenceKey = preference.getKey();
426 if (preferenceKey != null && preferenceKey.equals(key)) {
427 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700428 }
429 }
430 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700431 }
432
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700433 protected void removePreference(String key) {
434 Preference pref = findPreference(key);
435 if (pref != null) {
436 getPreferenceScreen().removePreference(pref);
437 }
438 }
439
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700440 /**
441 * Override this if you want to show a help item in the menu, by returning the resource id.
442 * @return the resource id for the help url
443 */
444 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400445 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700446 }
447
448 @Override
449 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400450 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400451 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700452 }
453 }
454
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700455 /*
456 * The name is intentionally made different from Activity#finish(), so that
457 * users won't misunderstand its meaning.
458 */
459 public final void finishFragment() {
460 getActivity().onBackPressed();
461 }
462
Amith Yamasanid7993472010-08-18 13:59:28 -0700463 // Some helpers for functions used by the settings fragments when they were activities
464
465 /**
466 * Returns the ContentResolver from the owning Activity.
467 */
468 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700469 Context context = getActivity();
470 if (context != null) {
471 mContentResolver = context.getContentResolver();
472 }
473 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700474 }
475
476 /**
477 * Returns the specified system service from the owning Activity.
478 */
479 protected Object getSystemService(final String name) {
480 return getActivity().getSystemService(name);
481 }
482
483 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700484 * Returns the PackageManager from the owning Activity.
485 */
486 protected PackageManager getPackageManager() {
487 return getActivity().getPackageManager();
488 }
489
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800490 @Override
491 public void onDetach() {
492 if (isRemoving()) {
493 if (mDialogFragment != null) {
494 mDialogFragment.dismiss();
495 mDialogFragment = null;
496 }
497 }
498 super.onDetach();
499 }
500
Amith Yamasanid7993472010-08-18 13:59:28 -0700501 // Dialog management
502
503 protected void showDialog(int dialogId) {
504 if (mDialogFragment != null) {
505 Log.e(TAG, "Old dialog fragment not null!");
506 }
507 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800508 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700509 }
510
511 public Dialog onCreateDialog(int dialogId) {
512 return null;
513 }
514
515 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800516 // mDialogFragment may not be visible yet in parent fragment's onResume().
517 // To be able to dismiss dialog at that time, don't check
518 // mDialogFragment.isVisible().
519 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700520 mDialogFragment.dismiss();
521 }
522 mDialogFragment = null;
523 }
524
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800525 /**
526 * Sets the OnCancelListener of the dialog shown. This method can only be
527 * called after showDialog(int) and before removeDialog(int). The method
528 * does nothing otherwise.
529 */
530 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
531 if (mDialogFragment != null) {
532 mDialogFragment.mOnCancelListener = listener;
533 }
534 }
535
536 /**
537 * Sets the OnDismissListener of the dialog shown. This method can only be
538 * called after showDialog(int) and before removeDialog(int). The method
539 * does nothing otherwise.
540 */
541 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
542 if (mDialogFragment != null) {
543 mDialogFragment.mOnDismissListener = listener;
544 }
545 }
546
Amith Yamasanic861cf82012-10-02 14:51:46 -0700547 public void onDialogShowing() {
548 // override in subclass to attach a dismiss listener, for instance
549 }
550
Jason Monk39b46742015-09-10 15:52:51 -0400551 @Override
552 public void onDisplayPreferenceDialog(Preference preference) {
553 if (preference.getKey() == null) {
554 // Auto-key preferences that don't have a key, so the dialog can find them.
555 preference.setKey(UUID.randomUUID().toString());
556 }
557 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000558 if (preference instanceof RestrictedListPreference) {
559 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
560 .newInstance(preference.getKey());
561 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400562 f = CustomListPreference.CustomListPreferenceDialogFragment
563 .newInstance(preference.getKey());
564 } else if (preference instanceof CustomDialogPreference) {
565 f = CustomDialogPreference.CustomPreferenceDialogFragment
566 .newInstance(preference.getKey());
567 } else if (preference instanceof CustomEditTextPreference) {
568 f = CustomEditTextPreference.CustomPreferenceDialogFragment
569 .newInstance(preference.getKey());
570 } else {
571 super.onDisplayPreferenceDialog(preference);
572 return;
573 }
574 f.setTargetFragment(this, 0);
575 f.show(getFragmentManager(), "dialog_preference");
576 onDialogShowing();
577 }
578
Amith Yamasani43c69782010-12-01 09:04:36 -0800579 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800580 private static final String KEY_DIALOG_ID = "key_dialog_id";
581 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
582
Amith Yamasanid7993472010-08-18 13:59:28 -0700583 private int mDialogId;
584
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800585 private Fragment mParentFragment;
586
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800587 private DialogInterface.OnCancelListener mOnCancelListener;
588 private DialogInterface.OnDismissListener mOnDismissListener;
589
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800590 public SettingsDialogFragment() {
591 /* do nothing */
592 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700593
Amith Yamasani43c69782010-12-01 09:04:36 -0800594 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700595 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800596 if (!(fragment instanceof Fragment)) {
597 throw new IllegalArgumentException("fragment argument must be an instance of "
598 + Fragment.class.getName());
599 }
600 mParentFragment = (Fragment) fragment;
601 }
602
603 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800604 public void onSaveInstanceState(Bundle outState) {
605 super.onSaveInstanceState(outState);
606 if (mParentFragment != null) {
607 outState.putInt(KEY_DIALOG_ID, mDialogId);
608 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
609 }
610 }
611
612 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700613 public void onStart() {
614 super.onStart();
615
616 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
617 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
618 }
619 }
620
621 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800622 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800623 if (savedInstanceState != null) {
624 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800625 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800626 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700627 if (mParentFragment == null) {
628 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
629 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800630 if (!(mParentFragment instanceof DialogCreatable)) {
631 throw new IllegalArgumentException(
632 (mParentFragment != null
633 ? mParentFragment.getClass().getName()
634 : mParentFragmentId)
635 + " must implement "
636 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800637 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800638 // This dialog fragment could be created from non-SettingsPreferenceFragment
639 if (mParentFragment instanceof SettingsPreferenceFragment) {
640 // restore mDialogFragment in mParentFragment
641 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
642 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800643 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800644 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700645 }
646
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800647 @Override
648 public void onCancel(DialogInterface dialog) {
649 super.onCancel(dialog);
650 if (mOnCancelListener != null) {
651 mOnCancelListener.onCancel(dialog);
652 }
653 }
654
655 @Override
656 public void onDismiss(DialogInterface dialog) {
657 super.onDismiss(dialog);
658 if (mOnDismissListener != null) {
659 mOnDismissListener.onDismiss(dialog);
660 }
661 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800662
Amith Yamasanid7993472010-08-18 13:59:28 -0700663 public int getDialogId() {
664 return mDialogId;
665 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800666
667 @Override
668 public void onDetach() {
669 super.onDetach();
670
Amith Yamasani8875ede2011-01-31 12:46:57 -0800671 // This dialog fragment could be created from non-SettingsPreferenceFragment
672 if (mParentFragment instanceof SettingsPreferenceFragment) {
673 // in case the dialog is not explicitly removed by removeDialog()
674 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
675 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
676 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800677 }
678 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700679 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700680
681 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800682 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700683 }
684
685 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800686 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700687 }
688
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700689 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700690 Activity activity = getActivity();
Jason Monk30e9fc82016-02-10 12:55:37 -0500691 if (activity != null && !activity.getFragmentManager().popBackStackImmediate()) {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800692 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700693 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700694 }
695
Jason Monk39b46742015-09-10 15:52:51 -0400696 protected final Context getPrefContext() {
697 return getPreferenceManager().getContext();
698 }
699
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700700 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
701 int requestCode, Bundle extras) {
702 final Activity activity = getActivity();
703 if (activity instanceof SettingsActivity) {
704 SettingsActivity sa = (SettingsActivity) activity;
705 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
706 return true;
707 } else if (activity instanceof PreferenceActivity) {
708 PreferenceActivity sa = (PreferenceActivity) activity;
709 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700710 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700711 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700712 Log.w(TAG,
713 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
714 + "launch the given Fragment (name: " + fragmentClass
715 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700716 return false;
717 }
718 }
Jason Monk65bb0972015-12-17 10:39:44 -0500719
720 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
721
722 private int mHighlightPosition = -1;
723
724 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
725 super(preferenceGroup);
726 }
727
728 public void highlight(int position) {
729 mHighlightPosition = position;
730 notifyDataSetChanged();
731 }
732
733 @Override
734 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
735 super.onBindViewHolder(holder, position);
736 if (position == mHighlightPosition) {
737 View v = holder.itemView;
738 if (v.getBackground() != null) {
739 final int centerX = v.getWidth() / 2;
740 final int centerY = v.getHeight() / 2;
741 v.getBackground().setHotspot(centerX, centerY);
742 }
743 v.setPressed(true);
744 v.setPressed(false);
745 mHighlightPosition = -1;
746 }
747 }
748 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700749}