blob: 4f81a38e2f81f7972e210c16eb089540b254ae06 [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
Amith Yamasani350938e2013-04-09 10:22:47 -070065 // Cache the content resolver for async callbacks
66 private ContentResolver mContentResolver;
67
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070068 private String mPreferenceKey;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070069 private boolean mPreferenceHighlighted = false;
70
Jason Monk39b46742015-09-10 15:52:51 -040071 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070072 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040073 private RecyclerView.AdapterDataObserver mDataSetObserver =
74 new RecyclerView.AdapterDataObserver() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070075 @Override
76 public void onChanged() {
Jason Monk39b46742015-09-10 15:52:51 -040077 onDataSetChanged();
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070078 }
79 };
80
Fabrice Di Meglio86159282014-07-21 16:02:27 -070081 private ViewGroup mPinnedHeaderFrameLayout;
John Spurlockb8e02b82015-04-15 21:15:55 -040082 private FloatingActionButton mFloatingActionButton;
Daichi Hirono5e76cdc2015-07-08 11:38:55 +090083 private ViewGroup mButtonBar;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070084
Jason Monk39b46742015-09-10 15:52:51 -040085 private LayoutPreference mHeader;
86
87 private LayoutPreference mFooter;
88 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -050089 private LinearLayoutManager mLayoutManager;
90 private HighlightablePreferenceGroupAdapter mAdapter;
Jason Monk39b46742015-09-10 15:52:51 -040091
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070092 @Override
93 public void onCreate(Bundle icicle) {
94 super.onCreate(icicle);
95
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070096 if (icicle != null) {
97 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
98 }
99
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700100 // Prepare help url and enable menu if necessary
101 int helpResource = getHelpResource();
102 if (helpResource != 0) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400103 mHelpUri = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700104 }
105 }
106
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700107 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700108 public View onCreateView(LayoutInflater inflater, ViewGroup container,
109 Bundle savedInstanceState) {
110 final View root = super.onCreateView(inflater, container, savedInstanceState);
111 mPinnedHeaderFrameLayout = (ViewGroup) root.findViewById(R.id.pinned_header);
John Spurlockb8e02b82015-04-15 21:15:55 -0400112 mFloatingActionButton = (FloatingActionButton) root.findViewById(R.id.fab);
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900113 mButtonBar = (ViewGroup) root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700114 return root;
115 }
116
Jason Monk39b46742015-09-10 15:52:51 -0400117 @Override
118 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
119 }
120
John Spurlockb8e02b82015-04-15 21:15:55 -0400121 public FloatingActionButton getFloatingActionButton() {
122 return mFloatingActionButton;
123 }
124
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900125 public ViewGroup getButtonBar() {
126 return mButtonBar;
127 }
128
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700129 public View setPinnedHeaderView(int layoutResId) {
130 final LayoutInflater inflater = getActivity().getLayoutInflater();
131 final View pinnedHeader =
132 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
133 setPinnedHeaderView(pinnedHeader);
134 return pinnedHeader;
135 }
136
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700137 public void setPinnedHeaderView(View pinnedHeader) {
138 mPinnedHeaderFrameLayout.addView(pinnedHeader);
139 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
140 }
141
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700142 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700143 public void onSaveInstanceState(Bundle outState) {
144 super.onSaveInstanceState(outState);
145
146 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
147 }
148
149 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700150 public void onActivityCreated(Bundle savedInstanceState) {
151 super.onActivityCreated(savedInstanceState);
Anna Galuszab1795f52016-01-08 14:37:16 -0800152 if (!TextUtils.isEmpty(mHelpUri)
153 && !(getActivity() instanceof AccessibilitySettingsForSetupWizardActivity)) {
Amith Yamasanib3a593e2012-04-23 18:03:52 -0700154 setHasOptionsMenu(true);
155 }
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700156 }
157
158 @Override
159 public void onResume() {
160 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700161
162 final Bundle args = getArguments();
163 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700164 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
165 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700166 }
167 }
168
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700169 @Override
170 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700171 registerObserverIfNeeded();
172 }
173
174 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700175 protected void onUnbindPreferences() {
176 unregisterObserverIfNeeded();
177 }
178
179 @Override
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700180 public void onStop() {
181 super.onStop();
182
183 unregisterObserverIfNeeded();
184 }
185
Jason Monkb5aa73f2015-03-31 12:59:33 -0400186 public void showLoadingWhenEmpty() {
187 View loading = getView().findViewById(R.id.loading_container);
Jason Monk39b46742015-09-10 15:52:51 -0400188 setEmptyView(loading);
Jason Monkb5aa73f2015-03-31 12:59:33 -0400189 }
190
Jason Monkb37e2882016-01-11 14:27:20 -0500191 public void setLoading(boolean loading, boolean animate) {
192 View loading_container = getView().findViewById(R.id.loading_container);
193 Utils.handleLoadingContainer(loading_container, getListView(), !loading, animate);
194 }
195
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700196 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700197 if (!mIsDataSetObserverRegistered) {
198 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400199 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700200 }
Jason Monk39b46742015-09-10 15:52:51 -0400201 mCurrentRootAdapter = getListView().getAdapter();
202 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700203 mIsDataSetObserverRegistered = true;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700204 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700205 }
206
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700207 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700208 if (mIsDataSetObserverRegistered) {
209 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400210 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700211 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700212 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700213 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700214 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700215 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700216
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700217 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700218 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700219 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700220 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700221 }
222
Jason Monk39b46742015-09-10 15:52:51 -0400223 private void onDataSetChanged() {
224 highlightPreferenceIfNeeded();
225 updateEmptyView();
226 }
227
Jason Monk39b46742015-09-10 15:52:51 -0400228 public LayoutPreference getHeaderView() {
229 return mHeader;
230 }
231
232 public LayoutPreference getFooterView() {
233 return mFooter;
234 }
235
236 protected void setHeaderView(int resource) {
237 mHeader = new LayoutPreference(getPrefContext(), resource);
238 mHeader.setOrder(-1);
239 if (getPreferenceScreen() != null) {
240 getPreferenceScreen().addPreference(mHeader);
241 }
242 }
243
244 protected void setFooterView(int resource) {
245 setFooterView(resource != 0 ? new LayoutPreference(getPrefContext(), resource) : null);
246 }
247
248 protected void setFooterView(View v) {
249 setFooterView(v != null ? new LayoutPreference(getPrefContext(), v) : null);
250 }
251
252 private void setFooterView(LayoutPreference footer) {
253 if (getPreferenceScreen() != null && mFooter != null) {
254 getPreferenceScreen().removePreference(mFooter);
255 }
256 if (footer != null) {
257 mFooter = footer;
258 mFooter.setOrder(Integer.MAX_VALUE);
259 if (getPreferenceScreen() != null) {
260 getPreferenceScreen().addPreference(mFooter);
261 }
262 } else {
263 mFooter = null;
264 }
265 }
266
267 @Override
268 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
269 super.setPreferenceScreen(preferenceScreen);
270 if (preferenceScreen != null) {
271 if (mHeader != null) {
272 preferenceScreen.addPreference(mHeader);
273 }
274 if (mFooter != null) {
275 preferenceScreen.addPreference(mFooter);
276 }
277 }
278 }
279
280 private void updateEmptyView() {
281 if (mEmptyView == null) return;
282 if (getPreferenceScreen() != null) {
283 boolean show = (getPreferenceScreen().getPreferenceCount()
284 - (mHeader != null ? 1 : 0)
285 - (mFooter != null ? 1 : 0)) <= 0;
286 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
287 } else {
288 mEmptyView.setVisibility(View.VISIBLE);
289 }
290 }
291
292 public void setEmptyView(View v) {
293 mEmptyView = v;
294 updateEmptyView();
295 }
296
297 public View getEmptyView() {
298 return mEmptyView;
299 }
300
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700301 /**
302 * Return a valid ListView position or -1 if none is found
303 */
304 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400305 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700306 return -1;
307 }
308
Jason Monk39b46742015-09-10 15:52:51 -0400309 RecyclerView listView = getListView();
310 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700311
312 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400313 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700314 }
315
316 return -1;
317 }
318
Jason Monk65bb0972015-12-17 10:39:44 -0500319 @Override
320 public RecyclerView.LayoutManager onCreateLayoutManager() {
321 mLayoutManager = new LinearLayoutManager(getContext());
322 return mLayoutManager;
323 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700324
Jason Monk65bb0972015-12-17 10:39:44 -0500325 @Override
326 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
327 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
328 return mAdapter;
329 }
330
331 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700332 final int position = canUseListViewForHighLighting(key);
333 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700334 mPreferenceHighlighted = true;
Jason Monk65bb0972015-12-17 10:39:44 -0500335 mLayoutManager.scrollToPosition(position);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700336
Jason Monk65bb0972015-12-17 10:39:44 -0500337 getView().postDelayed(new Runnable() {
338 @Override
339 public void run() {
340 mAdapter.highlight(position);
341 }
342 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700343 }
344 }
345
Jason Monk39b46742015-09-10 15:52:51 -0400346 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
347 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700348 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400349 final Preference preference = adapter.getItem(n);
350 final String preferenceKey = preference.getKey();
351 if (preferenceKey != null && preferenceKey.equals(key)) {
352 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700353 }
354 }
355 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700356 }
357
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700358 protected void removePreference(String key) {
359 Preference pref = findPreference(key);
360 if (pref != null) {
361 getPreferenceScreen().removePreference(pref);
362 }
363 }
364
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700365 /**
366 * Override this if you want to show a help item in the menu, by returning the resource id.
367 * @return the resource id for the help url
368 */
369 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400370 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700371 }
372
373 @Override
374 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400375 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400376 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700377 }
378 }
379
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700380 /*
381 * The name is intentionally made different from Activity#finish(), so that
382 * users won't misunderstand its meaning.
383 */
384 public final void finishFragment() {
385 getActivity().onBackPressed();
386 }
387
Amith Yamasanid7993472010-08-18 13:59:28 -0700388 // Some helpers for functions used by the settings fragments when they were activities
389
390 /**
391 * Returns the ContentResolver from the owning Activity.
392 */
393 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700394 Context context = getActivity();
395 if (context != null) {
396 mContentResolver = context.getContentResolver();
397 }
398 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700399 }
400
401 /**
402 * Returns the specified system service from the owning Activity.
403 */
404 protected Object getSystemService(final String name) {
405 return getActivity().getSystemService(name);
406 }
407
408 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700409 * Returns the PackageManager from the owning Activity.
410 */
411 protected PackageManager getPackageManager() {
412 return getActivity().getPackageManager();
413 }
414
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800415 @Override
416 public void onDetach() {
417 if (isRemoving()) {
418 if (mDialogFragment != null) {
419 mDialogFragment.dismiss();
420 mDialogFragment = null;
421 }
422 }
423 super.onDetach();
424 }
425
Amith Yamasanid7993472010-08-18 13:59:28 -0700426 // Dialog management
427
428 protected void showDialog(int dialogId) {
429 if (mDialogFragment != null) {
430 Log.e(TAG, "Old dialog fragment not null!");
431 }
432 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800433 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700434 }
435
436 public Dialog onCreateDialog(int dialogId) {
437 return null;
438 }
439
440 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800441 // mDialogFragment may not be visible yet in parent fragment's onResume().
442 // To be able to dismiss dialog at that time, don't check
443 // mDialogFragment.isVisible().
444 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700445 mDialogFragment.dismiss();
446 }
447 mDialogFragment = null;
448 }
449
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800450 /**
451 * Sets the OnCancelListener of the dialog shown. This method can only be
452 * called after showDialog(int) and before removeDialog(int). The method
453 * does nothing otherwise.
454 */
455 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
456 if (mDialogFragment != null) {
457 mDialogFragment.mOnCancelListener = listener;
458 }
459 }
460
461 /**
462 * Sets the OnDismissListener of the dialog shown. This method can only be
463 * called after showDialog(int) and before removeDialog(int). The method
464 * does nothing otherwise.
465 */
466 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
467 if (mDialogFragment != null) {
468 mDialogFragment.mOnDismissListener = listener;
469 }
470 }
471
Amith Yamasanic861cf82012-10-02 14:51:46 -0700472 public void onDialogShowing() {
473 // override in subclass to attach a dismiss listener, for instance
474 }
475
Jason Monk39b46742015-09-10 15:52:51 -0400476 @Override
477 public void onDisplayPreferenceDialog(Preference preference) {
478 if (preference.getKey() == null) {
479 // Auto-key preferences that don't have a key, so the dialog can find them.
480 preference.setKey(UUID.randomUUID().toString());
481 }
482 DialogFragment f = null;
483 if (preference instanceof CustomListPreference) {
484 f = CustomListPreference.CustomListPreferenceDialogFragment
485 .newInstance(preference.getKey());
486 } else if (preference instanceof CustomDialogPreference) {
487 f = CustomDialogPreference.CustomPreferenceDialogFragment
488 .newInstance(preference.getKey());
489 } else if (preference instanceof CustomEditTextPreference) {
490 f = CustomEditTextPreference.CustomPreferenceDialogFragment
491 .newInstance(preference.getKey());
492 } else {
493 super.onDisplayPreferenceDialog(preference);
494 return;
495 }
496 f.setTargetFragment(this, 0);
497 f.show(getFragmentManager(), "dialog_preference");
498 onDialogShowing();
499 }
500
Amith Yamasani43c69782010-12-01 09:04:36 -0800501 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800502 private static final String KEY_DIALOG_ID = "key_dialog_id";
503 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
504
Amith Yamasanid7993472010-08-18 13:59:28 -0700505 private int mDialogId;
506
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800507 private Fragment mParentFragment;
508
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800509 private DialogInterface.OnCancelListener mOnCancelListener;
510 private DialogInterface.OnDismissListener mOnDismissListener;
511
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800512 public SettingsDialogFragment() {
513 /* do nothing */
514 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700515
Amith Yamasani43c69782010-12-01 09:04:36 -0800516 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700517 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800518 if (!(fragment instanceof Fragment)) {
519 throw new IllegalArgumentException("fragment argument must be an instance of "
520 + Fragment.class.getName());
521 }
522 mParentFragment = (Fragment) fragment;
523 }
524
525 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800526 public void onSaveInstanceState(Bundle outState) {
527 super.onSaveInstanceState(outState);
528 if (mParentFragment != null) {
529 outState.putInt(KEY_DIALOG_ID, mDialogId);
530 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
531 }
532 }
533
534 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700535 public void onStart() {
536 super.onStart();
537
538 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
539 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
540 }
541 }
542
543 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800544 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800545 if (savedInstanceState != null) {
546 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800547 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800548 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700549 if (mParentFragment == null) {
550 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
551 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800552 if (!(mParentFragment instanceof DialogCreatable)) {
553 throw new IllegalArgumentException(
554 (mParentFragment != null
555 ? mParentFragment.getClass().getName()
556 : mParentFragmentId)
557 + " must implement "
558 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800559 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800560 // This dialog fragment could be created from non-SettingsPreferenceFragment
561 if (mParentFragment instanceof SettingsPreferenceFragment) {
562 // restore mDialogFragment in mParentFragment
563 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
564 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800565 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800566 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700567 }
568
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800569 @Override
570 public void onCancel(DialogInterface dialog) {
571 super.onCancel(dialog);
572 if (mOnCancelListener != null) {
573 mOnCancelListener.onCancel(dialog);
574 }
575 }
576
577 @Override
578 public void onDismiss(DialogInterface dialog) {
579 super.onDismiss(dialog);
580 if (mOnDismissListener != null) {
581 mOnDismissListener.onDismiss(dialog);
582 }
583 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800584
Amith Yamasanid7993472010-08-18 13:59:28 -0700585 public int getDialogId() {
586 return mDialogId;
587 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800588
589 @Override
590 public void onDetach() {
591 super.onDetach();
592
Amith Yamasani8875ede2011-01-31 12:46:57 -0800593 // This dialog fragment could be created from non-SettingsPreferenceFragment
594 if (mParentFragment instanceof SettingsPreferenceFragment) {
595 // in case the dialog is not explicitly removed by removeDialog()
596 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
597 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
598 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800599 }
600 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700601 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700602
603 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800604 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700605 }
606
607 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800608 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700609 }
610
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700611 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700612 Activity activity = getActivity();
613 if (activity != null) {
614 activity.onBackPressed();
615 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700616 }
617
Jason Monk39b46742015-09-10 15:52:51 -0400618 protected final Context getPrefContext() {
619 return getPreferenceManager().getContext();
620 }
621
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700622 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
623 int requestCode, Bundle extras) {
624 final Activity activity = getActivity();
625 if (activity instanceof SettingsActivity) {
626 SettingsActivity sa = (SettingsActivity) activity;
627 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
628 return true;
629 } else if (activity instanceof PreferenceActivity) {
630 PreferenceActivity sa = (PreferenceActivity) activity;
631 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700632 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700633 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700634 Log.w(TAG,
635 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
636 + "launch the given Fragment (name: " + fragmentClass
637 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700638 return false;
639 }
640 }
Jason Monk65bb0972015-12-17 10:39:44 -0500641
642 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
643
644 private int mHighlightPosition = -1;
645
646 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
647 super(preferenceGroup);
648 }
649
650 public void highlight(int position) {
651 mHighlightPosition = position;
652 notifyDataSetChanged();
653 }
654
655 @Override
656 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
657 super.onBindViewHolder(holder, position);
658 if (position == mHighlightPosition) {
659 View v = holder.itemView;
660 if (v.getBackground() != null) {
661 final int centerX = v.getWidth() / 2;
662 final int centerY = v.getHeight() / 2;
663 v.getBackground().setHotspot(centerX, centerY);
664 }
665 v.setPressed(true);
666 v.setPressed(false);
667 mHighlightPosition = -1;
668 }
669 }
670 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700671}