blob: f79def99ee458d78e5dcf7fd1d4e0bce390fa1c9 [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;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070027import android.graphics.drawable.Drawable;
Amith Yamasanid7993472010-08-18 13:59:28 -070028import android.os.Bundle;
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;
Amith Yamasanid7993472010-08-18 13:59:28 -070037import android.util.Log;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070038import android.view.LayoutInflater;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070039import android.view.Menu;
40import android.view.MenuInflater;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070041import android.view.View;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070042import android.view.ViewGroup;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070043import android.widget.Button;
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);
Jason Monk23acc2b2015-04-14 15:06:39 -0400152 if (!TextUtils.isEmpty(mHelpUri)) {
Amith Yamasanib3a593e2012-04-23 18:03:52 -0700153 setHasOptionsMenu(true);
154 }
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700155 }
156
157 @Override
158 public void onResume() {
159 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700160
161 final Bundle args = getArguments();
162 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700163 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
164 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700165 }
166 }
167
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700168 @Override
169 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700170 registerObserverIfNeeded();
171 }
172
173 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700174 protected void onUnbindPreferences() {
175 unregisterObserverIfNeeded();
176 }
177
178 @Override
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700179 public void onStop() {
180 super.onStop();
181
182 unregisterObserverIfNeeded();
183 }
184
Jason Monkb5aa73f2015-03-31 12:59:33 -0400185 public void showLoadingWhenEmpty() {
186 View loading = getView().findViewById(R.id.loading_container);
Jason Monk39b46742015-09-10 15:52:51 -0400187 setEmptyView(loading);
Jason Monkb5aa73f2015-03-31 12:59:33 -0400188 }
189
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700190 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700191 if (!mIsDataSetObserverRegistered) {
192 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400193 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700194 }
Jason Monk39b46742015-09-10 15:52:51 -0400195 mCurrentRootAdapter = getListView().getAdapter();
196 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700197 mIsDataSetObserverRegistered = true;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700198 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700199 }
200
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700201 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700202 if (mIsDataSetObserverRegistered) {
203 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400204 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700205 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700206 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700207 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700208 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700209 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700210
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700211 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700212 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700213 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700214 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700215 }
216
Jason Monk39b46742015-09-10 15:52:51 -0400217 private void onDataSetChanged() {
218 highlightPreferenceIfNeeded();
219 updateEmptyView();
220 }
221
Jason Monk39b46742015-09-10 15:52:51 -0400222 public LayoutPreference getHeaderView() {
223 return mHeader;
224 }
225
226 public LayoutPreference getFooterView() {
227 return mFooter;
228 }
229
230 protected void setHeaderView(int resource) {
231 mHeader = new LayoutPreference(getPrefContext(), resource);
232 mHeader.setOrder(-1);
233 if (getPreferenceScreen() != null) {
234 getPreferenceScreen().addPreference(mHeader);
235 }
236 }
237
238 protected void setFooterView(int resource) {
239 setFooterView(resource != 0 ? new LayoutPreference(getPrefContext(), resource) : null);
240 }
241
242 protected void setFooterView(View v) {
243 setFooterView(v != null ? new LayoutPreference(getPrefContext(), v) : null);
244 }
245
246 private void setFooterView(LayoutPreference footer) {
247 if (getPreferenceScreen() != null && mFooter != null) {
248 getPreferenceScreen().removePreference(mFooter);
249 }
250 if (footer != null) {
251 mFooter = footer;
252 mFooter.setOrder(Integer.MAX_VALUE);
253 if (getPreferenceScreen() != null) {
254 getPreferenceScreen().addPreference(mFooter);
255 }
256 } else {
257 mFooter = null;
258 }
259 }
260
261 @Override
262 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
263 super.setPreferenceScreen(preferenceScreen);
264 if (preferenceScreen != null) {
265 if (mHeader != null) {
266 preferenceScreen.addPreference(mHeader);
267 }
268 if (mFooter != null) {
269 preferenceScreen.addPreference(mFooter);
270 }
271 }
272 }
273
274 private void updateEmptyView() {
275 if (mEmptyView == null) return;
276 if (getPreferenceScreen() != null) {
277 boolean show = (getPreferenceScreen().getPreferenceCount()
278 - (mHeader != null ? 1 : 0)
279 - (mFooter != null ? 1 : 0)) <= 0;
280 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
281 } else {
282 mEmptyView.setVisibility(View.VISIBLE);
283 }
284 }
285
286 public void setEmptyView(View v) {
287 mEmptyView = v;
288 updateEmptyView();
289 }
290
291 public View getEmptyView() {
292 return mEmptyView;
293 }
294
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700295 /**
296 * Return a valid ListView position or -1 if none is found
297 */
298 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400299 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700300 return -1;
301 }
302
Jason Monk39b46742015-09-10 15:52:51 -0400303 RecyclerView listView = getListView();
304 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700305
306 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400307 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700308 }
309
310 return -1;
311 }
312
Jason Monk65bb0972015-12-17 10:39:44 -0500313 @Override
314 public RecyclerView.LayoutManager onCreateLayoutManager() {
315 mLayoutManager = new LinearLayoutManager(getContext());
316 return mLayoutManager;
317 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700318
Jason Monk65bb0972015-12-17 10:39:44 -0500319 @Override
320 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
321 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
322 return mAdapter;
323 }
324
325 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700326 final int position = canUseListViewForHighLighting(key);
327 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700328 mPreferenceHighlighted = true;
Jason Monk65bb0972015-12-17 10:39:44 -0500329 mLayoutManager.scrollToPosition(position);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700330
Jason Monk65bb0972015-12-17 10:39:44 -0500331 getView().postDelayed(new Runnable() {
332 @Override
333 public void run() {
334 mAdapter.highlight(position);
335 }
336 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700337 }
338 }
339
Jason Monk39b46742015-09-10 15:52:51 -0400340 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
341 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700342 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400343 final Preference preference = adapter.getItem(n);
344 final String preferenceKey = preference.getKey();
345 if (preferenceKey != null && preferenceKey.equals(key)) {
346 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700347 }
348 }
349 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700350 }
351
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700352 protected void removePreference(String key) {
353 Preference pref = findPreference(key);
354 if (pref != null) {
355 getPreferenceScreen().removePreference(pref);
356 }
357 }
358
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700359 /**
360 * Override this if you want to show a help item in the menu, by returning the resource id.
361 * @return the resource id for the help url
362 */
363 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400364 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700365 }
366
367 @Override
368 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400369 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400370 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700371 }
372 }
373
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700374 /*
375 * The name is intentionally made different from Activity#finish(), so that
376 * users won't misunderstand its meaning.
377 */
378 public final void finishFragment() {
379 getActivity().onBackPressed();
380 }
381
Amith Yamasanid7993472010-08-18 13:59:28 -0700382 // Some helpers for functions used by the settings fragments when they were activities
383
384 /**
385 * Returns the ContentResolver from the owning Activity.
386 */
387 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700388 Context context = getActivity();
389 if (context != null) {
390 mContentResolver = context.getContentResolver();
391 }
392 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700393 }
394
395 /**
396 * Returns the specified system service from the owning Activity.
397 */
398 protected Object getSystemService(final String name) {
399 return getActivity().getSystemService(name);
400 }
401
402 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700403 * Returns the PackageManager from the owning Activity.
404 */
405 protected PackageManager getPackageManager() {
406 return getActivity().getPackageManager();
407 }
408
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800409 @Override
410 public void onDetach() {
411 if (isRemoving()) {
412 if (mDialogFragment != null) {
413 mDialogFragment.dismiss();
414 mDialogFragment = null;
415 }
416 }
417 super.onDetach();
418 }
419
Amith Yamasanid7993472010-08-18 13:59:28 -0700420 // Dialog management
421
422 protected void showDialog(int dialogId) {
423 if (mDialogFragment != null) {
424 Log.e(TAG, "Old dialog fragment not null!");
425 }
426 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800427 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700428 }
429
430 public Dialog onCreateDialog(int dialogId) {
431 return null;
432 }
433
434 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800435 // mDialogFragment may not be visible yet in parent fragment's onResume().
436 // To be able to dismiss dialog at that time, don't check
437 // mDialogFragment.isVisible().
438 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700439 mDialogFragment.dismiss();
440 }
441 mDialogFragment = null;
442 }
443
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800444 /**
445 * Sets the OnCancelListener of the dialog shown. This method can only be
446 * called after showDialog(int) and before removeDialog(int). The method
447 * does nothing otherwise.
448 */
449 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
450 if (mDialogFragment != null) {
451 mDialogFragment.mOnCancelListener = listener;
452 }
453 }
454
455 /**
456 * Sets the OnDismissListener of the dialog shown. This method can only be
457 * called after showDialog(int) and before removeDialog(int). The method
458 * does nothing otherwise.
459 */
460 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
461 if (mDialogFragment != null) {
462 mDialogFragment.mOnDismissListener = listener;
463 }
464 }
465
Amith Yamasanic861cf82012-10-02 14:51:46 -0700466 public void onDialogShowing() {
467 // override in subclass to attach a dismiss listener, for instance
468 }
469
Jason Monk39b46742015-09-10 15:52:51 -0400470 @Override
471 public void onDisplayPreferenceDialog(Preference preference) {
472 if (preference.getKey() == null) {
473 // Auto-key preferences that don't have a key, so the dialog can find them.
474 preference.setKey(UUID.randomUUID().toString());
475 }
476 DialogFragment f = null;
477 if (preference instanceof CustomListPreference) {
478 f = CustomListPreference.CustomListPreferenceDialogFragment
479 .newInstance(preference.getKey());
480 } else if (preference instanceof CustomDialogPreference) {
481 f = CustomDialogPreference.CustomPreferenceDialogFragment
482 .newInstance(preference.getKey());
483 } else if (preference instanceof CustomEditTextPreference) {
484 f = CustomEditTextPreference.CustomPreferenceDialogFragment
485 .newInstance(preference.getKey());
486 } else {
487 super.onDisplayPreferenceDialog(preference);
488 return;
489 }
490 f.setTargetFragment(this, 0);
491 f.show(getFragmentManager(), "dialog_preference");
492 onDialogShowing();
493 }
494
Amith Yamasani43c69782010-12-01 09:04:36 -0800495 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800496 private static final String KEY_DIALOG_ID = "key_dialog_id";
497 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
498
Amith Yamasanid7993472010-08-18 13:59:28 -0700499 private int mDialogId;
500
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800501 private Fragment mParentFragment;
502
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800503 private DialogInterface.OnCancelListener mOnCancelListener;
504 private DialogInterface.OnDismissListener mOnDismissListener;
505
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800506 public SettingsDialogFragment() {
507 /* do nothing */
508 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700509
Amith Yamasani43c69782010-12-01 09:04:36 -0800510 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700511 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800512 if (!(fragment instanceof Fragment)) {
513 throw new IllegalArgumentException("fragment argument must be an instance of "
514 + Fragment.class.getName());
515 }
516 mParentFragment = (Fragment) fragment;
517 }
518
519 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800520 public void onSaveInstanceState(Bundle outState) {
521 super.onSaveInstanceState(outState);
522 if (mParentFragment != null) {
523 outState.putInt(KEY_DIALOG_ID, mDialogId);
524 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
525 }
526 }
527
528 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700529 public void onStart() {
530 super.onStart();
531
532 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
533 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
534 }
535 }
536
537 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800538 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800539 if (savedInstanceState != null) {
540 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800541 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800542 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700543 if (mParentFragment == null) {
544 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
545 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800546 if (!(mParentFragment instanceof DialogCreatable)) {
547 throw new IllegalArgumentException(
548 (mParentFragment != null
549 ? mParentFragment.getClass().getName()
550 : mParentFragmentId)
551 + " must implement "
552 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800553 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800554 // This dialog fragment could be created from non-SettingsPreferenceFragment
555 if (mParentFragment instanceof SettingsPreferenceFragment) {
556 // restore mDialogFragment in mParentFragment
557 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
558 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800559 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800560 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700561 }
562
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800563 @Override
564 public void onCancel(DialogInterface dialog) {
565 super.onCancel(dialog);
566 if (mOnCancelListener != null) {
567 mOnCancelListener.onCancel(dialog);
568 }
569 }
570
571 @Override
572 public void onDismiss(DialogInterface dialog) {
573 super.onDismiss(dialog);
574 if (mOnDismissListener != null) {
575 mOnDismissListener.onDismiss(dialog);
576 }
577 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800578
Amith Yamasanid7993472010-08-18 13:59:28 -0700579 public int getDialogId() {
580 return mDialogId;
581 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800582
583 @Override
584 public void onDetach() {
585 super.onDetach();
586
Amith Yamasani8875ede2011-01-31 12:46:57 -0800587 // This dialog fragment could be created from non-SettingsPreferenceFragment
588 if (mParentFragment instanceof SettingsPreferenceFragment) {
589 // in case the dialog is not explicitly removed by removeDialog()
590 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
591 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
592 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800593 }
594 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700595 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700596
597 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800598 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700599 }
600
601 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800602 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700603 }
604
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700605 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700606 Activity activity = getActivity();
607 if (activity != null) {
608 activity.onBackPressed();
609 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700610 }
611
Jason Monk39b46742015-09-10 15:52:51 -0400612 protected final Context getPrefContext() {
613 return getPreferenceManager().getContext();
614 }
615
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700616 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
617 int requestCode, Bundle extras) {
618 final Activity activity = getActivity();
619 if (activity instanceof SettingsActivity) {
620 SettingsActivity sa = (SettingsActivity) activity;
621 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
622 return true;
623 } else if (activity instanceof PreferenceActivity) {
624 PreferenceActivity sa = (PreferenceActivity) activity;
625 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700626 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700627 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700628 Log.w(TAG,
629 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
630 + "launch the given Fragment (name: " + fragmentClass
631 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700632 return false;
633 }
634 }
Jason Monk65bb0972015-12-17 10:39:44 -0500635
636 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
637
638 private int mHighlightPosition = -1;
639
640 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
641 super(preferenceGroup);
642 }
643
644 public void highlight(int position) {
645 mHighlightPosition = position;
646 notifyDataSetChanged();
647 }
648
649 @Override
650 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
651 super.onBindViewHolder(holder, position);
652 if (position == mHighlightPosition) {
653 View v = holder.itemView;
654 if (v.getBackground() != null) {
655 final int centerX = v.getWidth() / 2;
656 final int centerY = v.getHeight() / 2;
657 v.getBackground().setHotspot(centerX, centerY);
658 }
659 v.setPressed(true);
660 v.setPressed(false);
661 mHighlightPosition = -1;
662 }
663 }
664 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700665}