blob: 2ba49ce618b2cf9c03458ddd8da4be318d7dfb2d [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;
Jason Monk39b46742015-09-10 15:52:51 -040043import com.android.settings.applications.LayoutPreference;
John Spurlockb8e02b82015-04-15 21:15:55 -040044import com.android.settings.widget.FloatingActionButton;
45
Jason Monk39b46742015-09-10 15:52:51 -040046import java.util.UUID;
47
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070048/**
Amith Yamasanid7993472010-08-18 13:59:28 -070049 * Base class for Settings fragments, with some helper functions and dialog management.
50 */
Chris Wren8a963ba2015-03-20 10:29:14 -040051public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceFragment
52 implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070053
Jason Monk65bb0972015-12-17 10:39:44 -050054 private static final String TAG = "SettingsPreference";
Amith Yamasanid7993472010-08-18 13:59:28 -070055
Fabrice Di Meglioeced7802014-09-04 13:01:55 -070056 private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070057
58 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070059
Amith Yamasanid7993472010-08-18 13:59:28 -070060 private SettingsDialogFragment mDialogFragment;
61
Jason Monk23acc2b2015-04-14 15:06:39 -040062 private String mHelpUri;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070063
Amith Yamasani350938e2013-04-09 10:22:47 -070064 // Cache the content resolver for async callbacks
65 private ContentResolver mContentResolver;
66
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070067 private String mPreferenceKey;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070068 private boolean mPreferenceHighlighted = false;
69
Jason Monk39b46742015-09-10 15:52:51 -040070 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070071 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040072 private RecyclerView.AdapterDataObserver mDataSetObserver =
73 new RecyclerView.AdapterDataObserver() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070074 @Override
75 public void onChanged() {
Jason Monk39b46742015-09-10 15:52:51 -040076 onDataSetChanged();
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070077 }
78 };
79
Fabrice Di Meglio86159282014-07-21 16:02:27 -070080 private ViewGroup mPinnedHeaderFrameLayout;
John Spurlockb8e02b82015-04-15 21:15:55 -040081 private FloatingActionButton mFloatingActionButton;
Daichi Hirono5e76cdc2015-07-08 11:38:55 +090082 private ViewGroup mButtonBar;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070083
Jason Monk39b46742015-09-10 15:52:51 -040084 private LayoutPreference mHeader;
85
86 private LayoutPreference mFooter;
87 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -050088 private LinearLayoutManager mLayoutManager;
89 private HighlightablePreferenceGroupAdapter mAdapter;
Jason Monk39b46742015-09-10 15:52:51 -040090
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070091 @Override
92 public void onCreate(Bundle icicle) {
93 super.onCreate(icicle);
94
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070095 if (icicle != null) {
96 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
97 }
98
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070099 // Prepare help url and enable menu if necessary
100 int helpResource = getHelpResource();
101 if (helpResource != 0) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400102 mHelpUri = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700103 }
104 }
105
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700106 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700107 public View onCreateView(LayoutInflater inflater, ViewGroup container,
108 Bundle savedInstanceState) {
109 final View root = super.onCreateView(inflater, container, savedInstanceState);
110 mPinnedHeaderFrameLayout = (ViewGroup) root.findViewById(R.id.pinned_header);
John Spurlockb8e02b82015-04-15 21:15:55 -0400111 mFloatingActionButton = (FloatingActionButton) root.findViewById(R.id.fab);
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900112 mButtonBar = (ViewGroup) root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700113 return root;
114 }
115
Jason Monk39b46742015-09-10 15:52:51 -0400116 @Override
117 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
118 }
119
John Spurlockb8e02b82015-04-15 21:15:55 -0400120 public FloatingActionButton getFloatingActionButton() {
121 return mFloatingActionButton;
122 }
123
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900124 public ViewGroup getButtonBar() {
125 return mButtonBar;
126 }
127
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700128 public View setPinnedHeaderView(int layoutResId) {
129 final LayoutInflater inflater = getActivity().getLayoutInflater();
130 final View pinnedHeader =
131 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
132 setPinnedHeaderView(pinnedHeader);
133 return pinnedHeader;
134 }
135
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700136 public void setPinnedHeaderView(View pinnedHeader) {
137 mPinnedHeaderFrameLayout.addView(pinnedHeader);
138 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
139 }
140
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700141 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700142 public void onSaveInstanceState(Bundle outState) {
143 super.onSaveInstanceState(outState);
144
145 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
146 }
147
148 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700149 public void onActivityCreated(Bundle savedInstanceState) {
150 super.onActivityCreated(savedInstanceState);
Jason Monk23acc2b2015-04-14 15:06:39 -0400151 if (!TextUtils.isEmpty(mHelpUri)) {
Amith Yamasanib3a593e2012-04-23 18:03:52 -0700152 setHasOptionsMenu(true);
153 }
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700154 }
155
156 @Override
157 public void onResume() {
158 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700159
160 final Bundle args = getArguments();
161 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700162 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
163 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700164 }
165 }
166
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700167 @Override
168 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700169 registerObserverIfNeeded();
170 }
171
172 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700173 protected void onUnbindPreferences() {
174 unregisterObserverIfNeeded();
175 }
176
177 @Override
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700178 public void onStop() {
179 super.onStop();
180
181 unregisterObserverIfNeeded();
182 }
183
Jason Monkb5aa73f2015-03-31 12:59:33 -0400184 public void showLoadingWhenEmpty() {
185 View loading = getView().findViewById(R.id.loading_container);
Jason Monk39b46742015-09-10 15:52:51 -0400186 setEmptyView(loading);
Jason Monkb5aa73f2015-03-31 12:59:33 -0400187 }
188
Jason Monkb37e2882016-01-11 14:27:20 -0500189 public void setLoading(boolean loading, boolean animate) {
190 View loading_container = getView().findViewById(R.id.loading_container);
191 Utils.handleLoadingContainer(loading_container, getListView(), !loading, animate);
192 }
193
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700194 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700195 if (!mIsDataSetObserverRegistered) {
196 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400197 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700198 }
Jason Monk39b46742015-09-10 15:52:51 -0400199 mCurrentRootAdapter = getListView().getAdapter();
200 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700201 mIsDataSetObserverRegistered = true;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700202 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700203 }
204
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700205 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700206 if (mIsDataSetObserverRegistered) {
207 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400208 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700209 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700210 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700211 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700212 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700213 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700214
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700215 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700216 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700217 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700218 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700219 }
220
Jason Monk39b46742015-09-10 15:52:51 -0400221 private void onDataSetChanged() {
222 highlightPreferenceIfNeeded();
223 updateEmptyView();
224 }
225
Jason Monk39b46742015-09-10 15:52:51 -0400226 public LayoutPreference getHeaderView() {
227 return mHeader;
228 }
229
230 public LayoutPreference getFooterView() {
231 return mFooter;
232 }
233
234 protected void setHeaderView(int resource) {
235 mHeader = new LayoutPreference(getPrefContext(), resource);
236 mHeader.setOrder(-1);
237 if (getPreferenceScreen() != null) {
238 getPreferenceScreen().addPreference(mHeader);
239 }
240 }
241
242 protected void setFooterView(int resource) {
243 setFooterView(resource != 0 ? new LayoutPreference(getPrefContext(), resource) : null);
244 }
245
246 protected void setFooterView(View v) {
247 setFooterView(v != null ? new LayoutPreference(getPrefContext(), v) : null);
248 }
249
250 private void setFooterView(LayoutPreference footer) {
251 if (getPreferenceScreen() != null && mFooter != null) {
252 getPreferenceScreen().removePreference(mFooter);
253 }
254 if (footer != null) {
255 mFooter = footer;
256 mFooter.setOrder(Integer.MAX_VALUE);
257 if (getPreferenceScreen() != null) {
258 getPreferenceScreen().addPreference(mFooter);
259 }
260 } else {
261 mFooter = null;
262 }
263 }
264
265 @Override
266 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
267 super.setPreferenceScreen(preferenceScreen);
268 if (preferenceScreen != null) {
269 if (mHeader != null) {
270 preferenceScreen.addPreference(mHeader);
271 }
272 if (mFooter != null) {
273 preferenceScreen.addPreference(mFooter);
274 }
275 }
276 }
277
278 private void updateEmptyView() {
279 if (mEmptyView == null) return;
280 if (getPreferenceScreen() != null) {
281 boolean show = (getPreferenceScreen().getPreferenceCount()
282 - (mHeader != null ? 1 : 0)
283 - (mFooter != null ? 1 : 0)) <= 0;
284 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
285 } else {
286 mEmptyView.setVisibility(View.VISIBLE);
287 }
288 }
289
290 public void setEmptyView(View v) {
291 mEmptyView = v;
292 updateEmptyView();
293 }
294
295 public View getEmptyView() {
296 return mEmptyView;
297 }
298
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700299 /**
300 * Return a valid ListView position or -1 if none is found
301 */
302 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400303 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700304 return -1;
305 }
306
Jason Monk39b46742015-09-10 15:52:51 -0400307 RecyclerView listView = getListView();
308 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700309
310 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400311 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700312 }
313
314 return -1;
315 }
316
Jason Monk65bb0972015-12-17 10:39:44 -0500317 @Override
318 public RecyclerView.LayoutManager onCreateLayoutManager() {
319 mLayoutManager = new LinearLayoutManager(getContext());
320 return mLayoutManager;
321 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700322
Jason Monk65bb0972015-12-17 10:39:44 -0500323 @Override
324 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
325 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
326 return mAdapter;
327 }
328
329 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700330 final int position = canUseListViewForHighLighting(key);
331 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700332 mPreferenceHighlighted = true;
Jason Monk65bb0972015-12-17 10:39:44 -0500333 mLayoutManager.scrollToPosition(position);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700334
Jason Monk65bb0972015-12-17 10:39:44 -0500335 getView().postDelayed(new Runnable() {
336 @Override
337 public void run() {
338 mAdapter.highlight(position);
339 }
340 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700341 }
342 }
343
Jason Monk39b46742015-09-10 15:52:51 -0400344 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
345 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700346 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400347 final Preference preference = adapter.getItem(n);
348 final String preferenceKey = preference.getKey();
349 if (preferenceKey != null && preferenceKey.equals(key)) {
350 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700351 }
352 }
353 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700354 }
355
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700356 protected void removePreference(String key) {
357 Preference pref = findPreference(key);
358 if (pref != null) {
359 getPreferenceScreen().removePreference(pref);
360 }
361 }
362
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700363 /**
364 * Override this if you want to show a help item in the menu, by returning the resource id.
365 * @return the resource id for the help url
366 */
367 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400368 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700369 }
370
371 @Override
372 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400373 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400374 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700375 }
376 }
377
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700378 /*
379 * The name is intentionally made different from Activity#finish(), so that
380 * users won't misunderstand its meaning.
381 */
382 public final void finishFragment() {
383 getActivity().onBackPressed();
384 }
385
Amith Yamasanid7993472010-08-18 13:59:28 -0700386 // Some helpers for functions used by the settings fragments when they were activities
387
388 /**
389 * Returns the ContentResolver from the owning Activity.
390 */
391 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700392 Context context = getActivity();
393 if (context != null) {
394 mContentResolver = context.getContentResolver();
395 }
396 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700397 }
398
399 /**
400 * Returns the specified system service from the owning Activity.
401 */
402 protected Object getSystemService(final String name) {
403 return getActivity().getSystemService(name);
404 }
405
406 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700407 * Returns the PackageManager from the owning Activity.
408 */
409 protected PackageManager getPackageManager() {
410 return getActivity().getPackageManager();
411 }
412
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800413 @Override
414 public void onDetach() {
415 if (isRemoving()) {
416 if (mDialogFragment != null) {
417 mDialogFragment.dismiss();
418 mDialogFragment = null;
419 }
420 }
421 super.onDetach();
422 }
423
Amith Yamasanid7993472010-08-18 13:59:28 -0700424 // Dialog management
425
426 protected void showDialog(int dialogId) {
427 if (mDialogFragment != null) {
428 Log.e(TAG, "Old dialog fragment not null!");
429 }
430 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800431 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700432 }
433
434 public Dialog onCreateDialog(int dialogId) {
435 return null;
436 }
437
438 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800439 // mDialogFragment may not be visible yet in parent fragment's onResume().
440 // To be able to dismiss dialog at that time, don't check
441 // mDialogFragment.isVisible().
442 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700443 mDialogFragment.dismiss();
444 }
445 mDialogFragment = null;
446 }
447
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800448 /**
449 * Sets the OnCancelListener of the dialog shown. This method can only be
450 * called after showDialog(int) and before removeDialog(int). The method
451 * does nothing otherwise.
452 */
453 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
454 if (mDialogFragment != null) {
455 mDialogFragment.mOnCancelListener = listener;
456 }
457 }
458
459 /**
460 * Sets the OnDismissListener of the dialog shown. This method can only be
461 * called after showDialog(int) and before removeDialog(int). The method
462 * does nothing otherwise.
463 */
464 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
465 if (mDialogFragment != null) {
466 mDialogFragment.mOnDismissListener = listener;
467 }
468 }
469
Amith Yamasanic861cf82012-10-02 14:51:46 -0700470 public void onDialogShowing() {
471 // override in subclass to attach a dismiss listener, for instance
472 }
473
Jason Monk39b46742015-09-10 15:52:51 -0400474 @Override
475 public void onDisplayPreferenceDialog(Preference preference) {
476 if (preference.getKey() == null) {
477 // Auto-key preferences that don't have a key, so the dialog can find them.
478 preference.setKey(UUID.randomUUID().toString());
479 }
480 DialogFragment f = null;
481 if (preference instanceof CustomListPreference) {
482 f = CustomListPreference.CustomListPreferenceDialogFragment
483 .newInstance(preference.getKey());
484 } else if (preference instanceof CustomDialogPreference) {
485 f = CustomDialogPreference.CustomPreferenceDialogFragment
486 .newInstance(preference.getKey());
487 } else if (preference instanceof CustomEditTextPreference) {
488 f = CustomEditTextPreference.CustomPreferenceDialogFragment
489 .newInstance(preference.getKey());
490 } else {
491 super.onDisplayPreferenceDialog(preference);
492 return;
493 }
494 f.setTargetFragment(this, 0);
495 f.show(getFragmentManager(), "dialog_preference");
496 onDialogShowing();
497 }
498
Amith Yamasani43c69782010-12-01 09:04:36 -0800499 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800500 private static final String KEY_DIALOG_ID = "key_dialog_id";
501 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
502
Amith Yamasanid7993472010-08-18 13:59:28 -0700503 private int mDialogId;
504
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800505 private Fragment mParentFragment;
506
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800507 private DialogInterface.OnCancelListener mOnCancelListener;
508 private DialogInterface.OnDismissListener mOnDismissListener;
509
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800510 public SettingsDialogFragment() {
511 /* do nothing */
512 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700513
Amith Yamasani43c69782010-12-01 09:04:36 -0800514 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700515 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800516 if (!(fragment instanceof Fragment)) {
517 throw new IllegalArgumentException("fragment argument must be an instance of "
518 + Fragment.class.getName());
519 }
520 mParentFragment = (Fragment) fragment;
521 }
522
523 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800524 public void onSaveInstanceState(Bundle outState) {
525 super.onSaveInstanceState(outState);
526 if (mParentFragment != null) {
527 outState.putInt(KEY_DIALOG_ID, mDialogId);
528 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
529 }
530 }
531
532 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700533 public void onStart() {
534 super.onStart();
535
536 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
537 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
538 }
539 }
540
541 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800542 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800543 if (savedInstanceState != null) {
544 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800545 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800546 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700547 if (mParentFragment == null) {
548 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
549 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800550 if (!(mParentFragment instanceof DialogCreatable)) {
551 throw new IllegalArgumentException(
552 (mParentFragment != null
553 ? mParentFragment.getClass().getName()
554 : mParentFragmentId)
555 + " must implement "
556 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800557 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800558 // This dialog fragment could be created from non-SettingsPreferenceFragment
559 if (mParentFragment instanceof SettingsPreferenceFragment) {
560 // restore mDialogFragment in mParentFragment
561 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
562 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800563 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800564 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700565 }
566
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800567 @Override
568 public void onCancel(DialogInterface dialog) {
569 super.onCancel(dialog);
570 if (mOnCancelListener != null) {
571 mOnCancelListener.onCancel(dialog);
572 }
573 }
574
575 @Override
576 public void onDismiss(DialogInterface dialog) {
577 super.onDismiss(dialog);
578 if (mOnDismissListener != null) {
579 mOnDismissListener.onDismiss(dialog);
580 }
581 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800582
Amith Yamasanid7993472010-08-18 13:59:28 -0700583 public int getDialogId() {
584 return mDialogId;
585 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800586
587 @Override
588 public void onDetach() {
589 super.onDetach();
590
Amith Yamasani8875ede2011-01-31 12:46:57 -0800591 // This dialog fragment could be created from non-SettingsPreferenceFragment
592 if (mParentFragment instanceof SettingsPreferenceFragment) {
593 // in case the dialog is not explicitly removed by removeDialog()
594 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
595 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
596 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800597 }
598 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700599 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700600
601 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800602 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700603 }
604
605 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800606 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700607 }
608
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700609 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700610 Activity activity = getActivity();
611 if (activity != null) {
612 activity.onBackPressed();
613 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700614 }
615
Jason Monk39b46742015-09-10 15:52:51 -0400616 protected final Context getPrefContext() {
617 return getPreferenceManager().getContext();
618 }
619
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700620 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
621 int requestCode, Bundle extras) {
622 final Activity activity = getActivity();
623 if (activity instanceof SettingsActivity) {
624 SettingsActivity sa = (SettingsActivity) activity;
625 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
626 return true;
627 } else if (activity instanceof PreferenceActivity) {
628 PreferenceActivity sa = (PreferenceActivity) activity;
629 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700630 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700631 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700632 Log.w(TAG,
633 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
634 + "launch the given Fragment (name: " + fragmentClass
635 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700636 return false;
637 }
638 }
Jason Monk65bb0972015-12-17 10:39:44 -0500639
640 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
641
642 private int mHighlightPosition = -1;
643
644 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
645 super(preferenceGroup);
646 }
647
648 public void highlight(int position) {
649 mHighlightPosition = position;
650 notifyDataSetChanged();
651 }
652
653 @Override
654 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
655 super.onBindViewHolder(holder, position);
656 if (position == mHighlightPosition) {
657 View v = holder.itemView;
658 if (v.getBackground() != null) {
659 final int centerX = v.getWidth() / 2;
660 final int centerY = v.getHeight() / 2;
661 v.getBackground().setHotspot(centerX, centerY);
662 }
663 v.setPressed(true);
664 v.setPressed(false);
665 mHighlightPosition = -1;
666 }
667 }
668 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700669}