blob: c1e8d351e9b473e8766035e5be222afbb5e84cd5 [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 Monk39b46742015-09-10 15:52:51 -040028import android.support.v7.preference.Preference;
Jason Monk65bb0972015-12-17 10:39:44 -050029import android.support.v7.preference.PreferenceGroup;
Jason Monk39b46742015-09-10 15:52:51 -040030import android.support.v7.preference.PreferenceGroupAdapter;
31import android.support.v7.preference.PreferenceScreen;
Jason Monk65bb0972015-12-17 10:39:44 -050032import android.support.v7.preference.PreferenceViewHolder;
33import android.support.v7.widget.LinearLayoutManager;
Jason Monk39b46742015-09-10 15:52:51 -040034import android.support.v7.widget.RecyclerView;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070035import android.text.TextUtils;
Amith Yamasanid7993472010-08-18 13:59:28 -070036import android.util.Log;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070037import android.view.LayoutInflater;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070038import android.view.Menu;
39import android.view.MenuInflater;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070040import android.view.View;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070041import android.view.ViewGroup;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070042import android.widget.Button;
Anna Galuszab1795f52016-01-08 14:37:16 -080043import com.android.settings.accessibility.AccessibilitySettingsForSetupWizardActivity;
Jason Monk39b46742015-09-10 15:52:51 -040044import com.android.settings.applications.LayoutPreference;
John Spurlockb8e02b82015-04-15 21:15:55 -040045import com.android.settings.widget.FloatingActionButton;
46
Jason Monk39b46742015-09-10 15:52:51 -040047import java.util.UUID;
48
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070049/**
Amith Yamasanid7993472010-08-18 13:59:28 -070050 * Base class for Settings fragments, with some helper functions and dialog management.
51 */
Chris Wren8a963ba2015-03-20 10:29:14 -040052public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceFragment
53 implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070054
Jason Monk65bb0972015-12-17 10:39:44 -050055 private static final String TAG = "SettingsPreference";
Amith Yamasanid7993472010-08-18 13:59:28 -070056
Fabrice Di Meglioeced7802014-09-04 13:01:55 -070057 private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070058
59 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070060
Amith Yamasanid7993472010-08-18 13:59:28 -070061 private SettingsDialogFragment mDialogFragment;
62
Jason Monk23acc2b2015-04-14 15:06:39 -040063 private String mHelpUri;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070064
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000065 private static final int ORDER_FIRST = -1;
66 private static final int ORDER_LAST = Integer.MAX_VALUE -1;
67
Amith Yamasani350938e2013-04-09 10:22:47 -070068 // Cache the content resolver for async callbacks
69 private ContentResolver mContentResolver;
70
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070071 private String mPreferenceKey;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070072 private boolean mPreferenceHighlighted = false;
73
Jason Monk39b46742015-09-10 15:52:51 -040074 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070075 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040076 private RecyclerView.AdapterDataObserver mDataSetObserver =
77 new RecyclerView.AdapterDataObserver() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070078 @Override
79 public void onChanged() {
Jason Monk39b46742015-09-10 15:52:51 -040080 onDataSetChanged();
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070081 }
82 };
83
Fabrice Di Meglio86159282014-07-21 16:02:27 -070084 private ViewGroup mPinnedHeaderFrameLayout;
John Spurlockb8e02b82015-04-15 21:15:55 -040085 private FloatingActionButton mFloatingActionButton;
Daichi Hirono5e76cdc2015-07-08 11:38:55 +090086 private ViewGroup mButtonBar;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070087
Jason Monk39b46742015-09-10 15:52:51 -040088 private LayoutPreference mHeader;
89
90 private LayoutPreference mFooter;
91 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -050092 private LinearLayoutManager mLayoutManager;
93 private HighlightablePreferenceGroupAdapter mAdapter;
Jason Monk39b46742015-09-10 15:52:51 -040094
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070095 @Override
96 public void onCreate(Bundle icicle) {
97 super.onCreate(icicle);
98
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070099 if (icicle != null) {
100 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
101 }
102
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700103 // Prepare help url and enable menu if necessary
104 int helpResource = getHelpResource();
105 if (helpResource != 0) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400106 mHelpUri = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700107 }
108 }
109
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700110 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700111 public View onCreateView(LayoutInflater inflater, ViewGroup container,
112 Bundle savedInstanceState) {
113 final View root = super.onCreateView(inflater, container, savedInstanceState);
114 mPinnedHeaderFrameLayout = (ViewGroup) root.findViewById(R.id.pinned_header);
John Spurlockb8e02b82015-04-15 21:15:55 -0400115 mFloatingActionButton = (FloatingActionButton) root.findViewById(R.id.fab);
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900116 mButtonBar = (ViewGroup) root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700117 return root;
118 }
119
Jason Monk39b46742015-09-10 15:52:51 -0400120 @Override
121 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
122 }
123
John Spurlockb8e02b82015-04-15 21:15:55 -0400124 public FloatingActionButton getFloatingActionButton() {
125 return mFloatingActionButton;
126 }
127
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900128 public ViewGroup getButtonBar() {
129 return mButtonBar;
130 }
131
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700132 public View setPinnedHeaderView(int layoutResId) {
133 final LayoutInflater inflater = getActivity().getLayoutInflater();
134 final View pinnedHeader =
135 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
136 setPinnedHeaderView(pinnedHeader);
137 return pinnedHeader;
138 }
139
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700140 public void setPinnedHeaderView(View pinnedHeader) {
141 mPinnedHeaderFrameLayout.addView(pinnedHeader);
142 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
143 }
144
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700145 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700146 public void onSaveInstanceState(Bundle outState) {
147 super.onSaveInstanceState(outState);
148
149 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
150 }
151
152 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700153 public void onActivityCreated(Bundle savedInstanceState) {
154 super.onActivityCreated(savedInstanceState);
Anna Galuszab1795f52016-01-08 14:37:16 -0800155 if (!TextUtils.isEmpty(mHelpUri)
156 && !(getActivity() instanceof AccessibilitySettingsForSetupWizardActivity)) {
Amith Yamasanib3a593e2012-04-23 18:03:52 -0700157 setHasOptionsMenu(true);
158 }
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700159 }
160
161 @Override
162 public void onResume() {
163 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700164
165 final Bundle args = getArguments();
166 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700167 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
168 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700169 }
170 }
171
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700172 @Override
173 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700174 registerObserverIfNeeded();
175 }
176
177 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700178 protected void onUnbindPreferences() {
179 unregisterObserverIfNeeded();
180 }
181
182 @Override
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700183 public void onStop() {
184 super.onStop();
185
186 unregisterObserverIfNeeded();
187 }
188
Jason Monkb5aa73f2015-03-31 12:59:33 -0400189 public void showLoadingWhenEmpty() {
190 View loading = getView().findViewById(R.id.loading_container);
Jason Monk39b46742015-09-10 15:52:51 -0400191 setEmptyView(loading);
Jason Monkb5aa73f2015-03-31 12:59:33 -0400192 }
193
Jason Monkb37e2882016-01-11 14:27:20 -0500194 public void setLoading(boolean loading, boolean animate) {
195 View loading_container = getView().findViewById(R.id.loading_container);
196 Utils.handleLoadingContainer(loading_container, getListView(), !loading, animate);
197 }
198
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700199 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700200 if (!mIsDataSetObserverRegistered) {
201 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400202 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700203 }
Jason Monk39b46742015-09-10 15:52:51 -0400204 mCurrentRootAdapter = getListView().getAdapter();
205 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700206 mIsDataSetObserverRegistered = true;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700207 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700208 }
209
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700210 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700211 if (mIsDataSetObserverRegistered) {
212 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400213 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700214 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700215 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700216 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700217 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700218 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700219
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700220 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700221 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700222 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700223 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700224 }
225
Jason Monk39b46742015-09-10 15:52:51 -0400226 private void onDataSetChanged() {
227 highlightPreferenceIfNeeded();
228 updateEmptyView();
229 }
230
Jason Monk39b46742015-09-10 15:52:51 -0400231 public LayoutPreference getHeaderView() {
232 return mHeader;
233 }
234
235 public LayoutPreference getFooterView() {
236 return mFooter;
237 }
238
239 protected void setHeaderView(int resource) {
240 mHeader = new LayoutPreference(getPrefContext(), resource);
Sudheer Shanka5590e2e2016-01-22 20:40:56 +0000241 mHeader.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400242 if (getPreferenceScreen() != null) {
243 getPreferenceScreen().addPreference(mHeader);
244 }
245 }
246
247 protected void setFooterView(int resource) {
248 setFooterView(resource != 0 ? new LayoutPreference(getPrefContext(), resource) : null);
249 }
250
251 protected void setFooterView(View v) {
252 setFooterView(v != null ? new LayoutPreference(getPrefContext(), v) : null);
253 }
254
255 private void setFooterView(LayoutPreference footer) {
256 if (getPreferenceScreen() != null && mFooter != null) {
257 getPreferenceScreen().removePreference(mFooter);
258 }
259 if (footer != null) {
260 mFooter = footer;
Sudheer Shanka5590e2e2016-01-22 20:40:56 +0000261 mFooter.setOrder(ORDER_LAST);
Jason Monk39b46742015-09-10 15:52:51 -0400262 if (getPreferenceScreen() != null) {
263 getPreferenceScreen().addPreference(mFooter);
264 }
265 } else {
266 mFooter = null;
267 }
268 }
269
270 @Override
271 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
272 super.setPreferenceScreen(preferenceScreen);
273 if (preferenceScreen != null) {
274 if (mHeader != null) {
275 preferenceScreen.addPreference(mHeader);
276 }
277 if (mFooter != null) {
278 preferenceScreen.addPreference(mFooter);
279 }
280 }
281 }
282
283 private void updateEmptyView() {
284 if (mEmptyView == null) return;
285 if (getPreferenceScreen() != null) {
286 boolean show = (getPreferenceScreen().getPreferenceCount()
287 - (mHeader != null ? 1 : 0)
288 - (mFooter != null ? 1 : 0)) <= 0;
289 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
290 } else {
291 mEmptyView.setVisibility(View.VISIBLE);
292 }
293 }
294
295 public void setEmptyView(View v) {
296 mEmptyView = v;
297 updateEmptyView();
298 }
299
300 public View getEmptyView() {
301 return mEmptyView;
302 }
303
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700304 /**
305 * Return a valid ListView position or -1 if none is found
306 */
307 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400308 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700309 return -1;
310 }
311
Jason Monk39b46742015-09-10 15:52:51 -0400312 RecyclerView listView = getListView();
313 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700314
315 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400316 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700317 }
318
319 return -1;
320 }
321
Jason Monk65bb0972015-12-17 10:39:44 -0500322 @Override
323 public RecyclerView.LayoutManager onCreateLayoutManager() {
324 mLayoutManager = new LinearLayoutManager(getContext());
325 return mLayoutManager;
326 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700327
Jason Monk65bb0972015-12-17 10:39:44 -0500328 @Override
329 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
330 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
331 return mAdapter;
332 }
333
334 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700335 final int position = canUseListViewForHighLighting(key);
336 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700337 mPreferenceHighlighted = true;
Jason Monk65bb0972015-12-17 10:39:44 -0500338 mLayoutManager.scrollToPosition(position);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700339
Jason Monk65bb0972015-12-17 10:39:44 -0500340 getView().postDelayed(new Runnable() {
341 @Override
342 public void run() {
343 mAdapter.highlight(position);
344 }
345 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700346 }
347 }
348
Jason Monk39b46742015-09-10 15:52:51 -0400349 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
350 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700351 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400352 final Preference preference = adapter.getItem(n);
353 final String preferenceKey = preference.getKey();
354 if (preferenceKey != null && preferenceKey.equals(key)) {
355 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700356 }
357 }
358 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700359 }
360
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700361 protected void removePreference(String key) {
362 Preference pref = findPreference(key);
363 if (pref != null) {
364 getPreferenceScreen().removePreference(pref);
365 }
366 }
367
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700368 /**
369 * Override this if you want to show a help item in the menu, by returning the resource id.
370 * @return the resource id for the help url
371 */
372 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400373 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700374 }
375
376 @Override
377 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400378 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400379 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700380 }
381 }
382
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700383 /*
384 * The name is intentionally made different from Activity#finish(), so that
385 * users won't misunderstand its meaning.
386 */
387 public final void finishFragment() {
388 getActivity().onBackPressed();
389 }
390
Amith Yamasanid7993472010-08-18 13:59:28 -0700391 // Some helpers for functions used by the settings fragments when they were activities
392
393 /**
394 * Returns the ContentResolver from the owning Activity.
395 */
396 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700397 Context context = getActivity();
398 if (context != null) {
399 mContentResolver = context.getContentResolver();
400 }
401 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700402 }
403
404 /**
405 * Returns the specified system service from the owning Activity.
406 */
407 protected Object getSystemService(final String name) {
408 return getActivity().getSystemService(name);
409 }
410
411 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700412 * Returns the PackageManager from the owning Activity.
413 */
414 protected PackageManager getPackageManager() {
415 return getActivity().getPackageManager();
416 }
417
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800418 @Override
419 public void onDetach() {
420 if (isRemoving()) {
421 if (mDialogFragment != null) {
422 mDialogFragment.dismiss();
423 mDialogFragment = null;
424 }
425 }
426 super.onDetach();
427 }
428
Amith Yamasanid7993472010-08-18 13:59:28 -0700429 // Dialog management
430
431 protected void showDialog(int dialogId) {
432 if (mDialogFragment != null) {
433 Log.e(TAG, "Old dialog fragment not null!");
434 }
435 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800436 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700437 }
438
439 public Dialog onCreateDialog(int dialogId) {
440 return null;
441 }
442
443 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800444 // mDialogFragment may not be visible yet in parent fragment's onResume().
445 // To be able to dismiss dialog at that time, don't check
446 // mDialogFragment.isVisible().
447 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700448 mDialogFragment.dismiss();
449 }
450 mDialogFragment = null;
451 }
452
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800453 /**
454 * Sets the OnCancelListener of the dialog shown. This method can only be
455 * called after showDialog(int) and before removeDialog(int). The method
456 * does nothing otherwise.
457 */
458 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
459 if (mDialogFragment != null) {
460 mDialogFragment.mOnCancelListener = listener;
461 }
462 }
463
464 /**
465 * Sets the OnDismissListener of the dialog shown. This method can only be
466 * called after showDialog(int) and before removeDialog(int). The method
467 * does nothing otherwise.
468 */
469 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
470 if (mDialogFragment != null) {
471 mDialogFragment.mOnDismissListener = listener;
472 }
473 }
474
Amith Yamasanic861cf82012-10-02 14:51:46 -0700475 public void onDialogShowing() {
476 // override in subclass to attach a dismiss listener, for instance
477 }
478
Jason Monk39b46742015-09-10 15:52:51 -0400479 @Override
480 public void onDisplayPreferenceDialog(Preference preference) {
481 if (preference.getKey() == null) {
482 // Auto-key preferences that don't have a key, so the dialog can find them.
483 preference.setKey(UUID.randomUUID().toString());
484 }
485 DialogFragment f = null;
486 if (preference instanceof CustomListPreference) {
487 f = CustomListPreference.CustomListPreferenceDialogFragment
488 .newInstance(preference.getKey());
489 } else if (preference instanceof CustomDialogPreference) {
490 f = CustomDialogPreference.CustomPreferenceDialogFragment
491 .newInstance(preference.getKey());
492 } else if (preference instanceof CustomEditTextPreference) {
493 f = CustomEditTextPreference.CustomPreferenceDialogFragment
494 .newInstance(preference.getKey());
495 } else {
496 super.onDisplayPreferenceDialog(preference);
497 return;
498 }
499 f.setTargetFragment(this, 0);
500 f.show(getFragmentManager(), "dialog_preference");
501 onDialogShowing();
502 }
503
Amith Yamasani43c69782010-12-01 09:04:36 -0800504 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800505 private static final String KEY_DIALOG_ID = "key_dialog_id";
506 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
507
Amith Yamasanid7993472010-08-18 13:59:28 -0700508 private int mDialogId;
509
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800510 private Fragment mParentFragment;
511
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800512 private DialogInterface.OnCancelListener mOnCancelListener;
513 private DialogInterface.OnDismissListener mOnDismissListener;
514
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800515 public SettingsDialogFragment() {
516 /* do nothing */
517 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700518
Amith Yamasani43c69782010-12-01 09:04:36 -0800519 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700520 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800521 if (!(fragment instanceof Fragment)) {
522 throw new IllegalArgumentException("fragment argument must be an instance of "
523 + Fragment.class.getName());
524 }
525 mParentFragment = (Fragment) fragment;
526 }
527
528 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800529 public void onSaveInstanceState(Bundle outState) {
530 super.onSaveInstanceState(outState);
531 if (mParentFragment != null) {
532 outState.putInt(KEY_DIALOG_ID, mDialogId);
533 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
534 }
535 }
536
537 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700538 public void onStart() {
539 super.onStart();
540
541 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
542 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
543 }
544 }
545
546 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800547 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800548 if (savedInstanceState != null) {
549 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800550 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800551 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700552 if (mParentFragment == null) {
553 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
554 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800555 if (!(mParentFragment instanceof DialogCreatable)) {
556 throw new IllegalArgumentException(
557 (mParentFragment != null
558 ? mParentFragment.getClass().getName()
559 : mParentFragmentId)
560 + " must implement "
561 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800562 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800563 // This dialog fragment could be created from non-SettingsPreferenceFragment
564 if (mParentFragment instanceof SettingsPreferenceFragment) {
565 // restore mDialogFragment in mParentFragment
566 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
567 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800568 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800569 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700570 }
571
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800572 @Override
573 public void onCancel(DialogInterface dialog) {
574 super.onCancel(dialog);
575 if (mOnCancelListener != null) {
576 mOnCancelListener.onCancel(dialog);
577 }
578 }
579
580 @Override
581 public void onDismiss(DialogInterface dialog) {
582 super.onDismiss(dialog);
583 if (mOnDismissListener != null) {
584 mOnDismissListener.onDismiss(dialog);
585 }
586 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800587
Amith Yamasanid7993472010-08-18 13:59:28 -0700588 public int getDialogId() {
589 return mDialogId;
590 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800591
592 @Override
593 public void onDetach() {
594 super.onDetach();
595
Amith Yamasani8875ede2011-01-31 12:46:57 -0800596 // This dialog fragment could be created from non-SettingsPreferenceFragment
597 if (mParentFragment instanceof SettingsPreferenceFragment) {
598 // in case the dialog is not explicitly removed by removeDialog()
599 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
600 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
601 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800602 }
603 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700604 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700605
606 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800607 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700608 }
609
610 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800611 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700612 }
613
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700614 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700615 Activity activity = getActivity();
616 if (activity != null) {
617 activity.onBackPressed();
618 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700619 }
620
Jason Monk39b46742015-09-10 15:52:51 -0400621 protected final Context getPrefContext() {
622 return getPreferenceManager().getContext();
623 }
624
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700625 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
626 int requestCode, Bundle extras) {
627 final Activity activity = getActivity();
628 if (activity instanceof SettingsActivity) {
629 SettingsActivity sa = (SettingsActivity) activity;
630 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
631 return true;
632 } else if (activity instanceof PreferenceActivity) {
633 PreferenceActivity sa = (PreferenceActivity) activity;
634 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700635 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700636 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700637 Log.w(TAG,
638 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
639 + "launch the given Fragment (name: " + fragmentClass
640 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700641 return false;
642 }
643 }
Jason Monk65bb0972015-12-17 10:39:44 -0500644
645 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
646
647 private int mHighlightPosition = -1;
648
649 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
650 super(preferenceGroup);
651 }
652
653 public void highlight(int position) {
654 mHighlightPosition = position;
655 notifyDataSetChanged();
656 }
657
658 @Override
659 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
660 super.onBindViewHolder(holder, position);
661 if (position == mHighlightPosition) {
662 View v = holder.itemView;
663 if (v.getBackground() != null) {
664 final int centerX = v.getWidth() / 2;
665 final int centerY = v.getHeight() / 2;
666 v.getBackground().setHotspot(centerX, centerY);
667 }
668 v.setPressed(true);
669 v.setPressed(false);
670 mHighlightPosition = -1;
671 }
672 }
673 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700674}