blob: 34c773a555a7d914f417ecf2e6104f6f97bf5c05 [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;
30import android.support.v7.preference.PreferenceGroupAdapter;
31import android.support.v7.preference.PreferenceScreen;
32import android.support.v7.widget.RecyclerView;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070033import android.text.TextUtils;
Amith Yamasanid7993472010-08-18 13:59:28 -070034import android.util.Log;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070035import android.view.LayoutInflater;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070036import android.view.Menu;
37import android.view.MenuInflater;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070038import android.view.View;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070039import android.view.ViewGroup;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070040import android.widget.Button;
Amith Yamasanid7993472010-08-18 13:59:28 -070041
Jason Monk39b46742015-09-10 15:52:51 -040042import com.android.settings.applications.LayoutPreference;
John Spurlockb8e02b82015-04-15 21:15:55 -040043import com.android.settings.widget.FloatingActionButton;
44
Jason Monk39b46742015-09-10 15:52:51 -040045import java.util.UUID;
46
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070047/**
Amith Yamasanid7993472010-08-18 13:59:28 -070048 * Base class for Settings fragments, with some helper functions and dialog management.
49 */
Chris Wren8a963ba2015-03-20 10:29:14 -040050public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceFragment
51 implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070052
53 private static final String TAG = "SettingsPreferenceFragment";
54
Fabrice Di Meglioeced7802014-09-04 13:01:55 -070055 private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070056
57 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070058
Amith Yamasanid7993472010-08-18 13:59:28 -070059 private SettingsDialogFragment mDialogFragment;
60
Jason Monk23acc2b2015-04-14 15:06:39 -040061 private String mHelpUri;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070062
Amith Yamasani350938e2013-04-09 10:22:47 -070063 // Cache the content resolver for async callbacks
64 private ContentResolver mContentResolver;
65
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070066 private String mPreferenceKey;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070067 private boolean mPreferenceHighlighted = false;
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -070068 private Drawable mHighlightDrawable;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070069
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;
88
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070089 @Override
90 public void onCreate(Bundle icicle) {
91 super.onCreate(icicle);
92
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070093 if (icicle != null) {
94 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
95 }
96
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070097 // Prepare help url and enable menu if necessary
98 int helpResource = getHelpResource();
99 if (helpResource != 0) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400100 mHelpUri = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700101 }
102 }
103
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700104 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700105 public View onCreateView(LayoutInflater inflater, ViewGroup container,
106 Bundle savedInstanceState) {
107 final View root = super.onCreateView(inflater, container, savedInstanceState);
108 mPinnedHeaderFrameLayout = (ViewGroup) root.findViewById(R.id.pinned_header);
John Spurlockb8e02b82015-04-15 21:15:55 -0400109 mFloatingActionButton = (FloatingActionButton) root.findViewById(R.id.fab);
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900110 mButtonBar = (ViewGroup) root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700111 return root;
112 }
113
Jason Monk39b46742015-09-10 15:52:51 -0400114 @Override
115 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
116 }
117
John Spurlockb8e02b82015-04-15 21:15:55 -0400118 public FloatingActionButton getFloatingActionButton() {
119 return mFloatingActionButton;
120 }
121
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900122 public ViewGroup getButtonBar() {
123 return mButtonBar;
124 }
125
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700126 public View setPinnedHeaderView(int layoutResId) {
127 final LayoutInflater inflater = getActivity().getLayoutInflater();
128 final View pinnedHeader =
129 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
130 setPinnedHeaderView(pinnedHeader);
131 return pinnedHeader;
132 }
133
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700134 public void setPinnedHeaderView(View pinnedHeader) {
135 mPinnedHeaderFrameLayout.addView(pinnedHeader);
136 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
137 }
138
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700139 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700140 public void onSaveInstanceState(Bundle outState) {
141 super.onSaveInstanceState(outState);
142
143 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
144 }
145
146 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700147 public void onActivityCreated(Bundle savedInstanceState) {
148 super.onActivityCreated(savedInstanceState);
Jason Monk23acc2b2015-04-14 15:06:39 -0400149 if (!TextUtils.isEmpty(mHelpUri)) {
Amith Yamasanib3a593e2012-04-23 18:03:52 -0700150 setHasOptionsMenu(true);
151 }
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700152 }
153
154 @Override
155 public void onResume() {
156 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700157
158 final Bundle args = getArguments();
159 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700160 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
161 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700162 }
163 }
164
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700165 @Override
166 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700167 registerObserverIfNeeded();
168 }
169
170 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700171 protected void onUnbindPreferences() {
172 unregisterObserverIfNeeded();
173 }
174
175 @Override
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700176 public void onStop() {
177 super.onStop();
178
179 unregisterObserverIfNeeded();
180 }
181
Jason Monkb5aa73f2015-03-31 12:59:33 -0400182 public void showLoadingWhenEmpty() {
183 View loading = getView().findViewById(R.id.loading_container);
Jason Monk39b46742015-09-10 15:52:51 -0400184 setEmptyView(loading);
Jason Monkb5aa73f2015-03-31 12:59:33 -0400185 }
186
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700187 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700188 if (!mIsDataSetObserverRegistered) {
189 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400190 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700191 }
Jason Monk39b46742015-09-10 15:52:51 -0400192 mCurrentRootAdapter = getListView().getAdapter();
193 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700194 mIsDataSetObserverRegistered = true;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700195 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700196 }
197
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700198 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700199 if (mIsDataSetObserverRegistered) {
200 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400201 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700202 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700203 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700204 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700205 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700206 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700207
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700208 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700209 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700210 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700211 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700212 }
213
Jason Monk39b46742015-09-10 15:52:51 -0400214 private void onDataSetChanged() {
215 highlightPreferenceIfNeeded();
216 updateEmptyView();
217 }
218
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700219 private Drawable getHighlightDrawable() {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700220 if (mHighlightDrawable == null) {
221 mHighlightDrawable = getActivity().getDrawable(R.drawable.preference_highlight);
222 }
223 return mHighlightDrawable;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700224 }
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
317 private void highlightPreference(String key) {
318 final Drawable highlight = getHighlightDrawable();
319
320 final int position = canUseListViewForHighLighting(key);
321 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700322 mPreferenceHighlighted = true;
323
Jason Monk39b46742015-09-10 15:52:51 -0400324 // TODO: Need to find a way to scroll to and highlight search items now
325 // that we are using RecyclerView instead.
326// final RecyclerView listView = getListView();
327// final RecyclerView.Adapter adapter = listView.getAdapter();
328//
329//// ((PreferenceGroupAdapter) adapter).setHighlightedDrawable(highlight);
330//// ((PreferenceGroupAdapter) adapter).setHighlighted(position);
331//
332// listView.post(new Runnable() {
333// @Override
334// public void run() {
335// listView.setSelection(position);
336// listView.postDelayed(new Runnable() {
337// @Override
338// public void run() {
339// final int index = position - listView.getFirstVisiblePosition();
340// if (index >= 0 && index < listView.getChildCount()) {
341// final View v = listView.getChildAt(index);
342// final int centerX = v.getWidth() / 2;
343// final int centerY = v.getHeight() / 2;
344// highlight.setHotspot(centerX, centerY);
345// v.setPressed(true);
346// v.setPressed(false);
347// }
348// }
349// }, DELAY_HIGHLIGHT_DURATION_MILLIS);
350// }
351// });
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700352 }
353 }
354
Jason Monk39b46742015-09-10 15:52:51 -0400355 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
356 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700357 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400358 final Preference preference = adapter.getItem(n);
359 final String preferenceKey = preference.getKey();
360 if (preferenceKey != null && preferenceKey.equals(key)) {
361 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700362 }
363 }
364 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700365 }
366
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700367 protected void removePreference(String key) {
368 Preference pref = findPreference(key);
369 if (pref != null) {
370 getPreferenceScreen().removePreference(pref);
371 }
372 }
373
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700374 /**
375 * Override this if you want to show a help item in the menu, by returning the resource id.
376 * @return the resource id for the help url
377 */
378 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400379 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700380 }
381
382 @Override
383 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400384 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400385 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700386 }
387 }
388
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700389 /*
390 * The name is intentionally made different from Activity#finish(), so that
391 * users won't misunderstand its meaning.
392 */
393 public final void finishFragment() {
394 getActivity().onBackPressed();
395 }
396
Amith Yamasanid7993472010-08-18 13:59:28 -0700397 // Some helpers for functions used by the settings fragments when they were activities
398
399 /**
400 * Returns the ContentResolver from the owning Activity.
401 */
402 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700403 Context context = getActivity();
404 if (context != null) {
405 mContentResolver = context.getContentResolver();
406 }
407 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700408 }
409
410 /**
411 * Returns the specified system service from the owning Activity.
412 */
413 protected Object getSystemService(final String name) {
414 return getActivity().getSystemService(name);
415 }
416
417 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700418 * Returns the PackageManager from the owning Activity.
419 */
420 protected PackageManager getPackageManager() {
421 return getActivity().getPackageManager();
422 }
423
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800424 @Override
425 public void onDetach() {
426 if (isRemoving()) {
427 if (mDialogFragment != null) {
428 mDialogFragment.dismiss();
429 mDialogFragment = null;
430 }
431 }
432 super.onDetach();
433 }
434
Amith Yamasanid7993472010-08-18 13:59:28 -0700435 // Dialog management
436
437 protected void showDialog(int dialogId) {
438 if (mDialogFragment != null) {
439 Log.e(TAG, "Old dialog fragment not null!");
440 }
441 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800442 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700443 }
444
445 public Dialog onCreateDialog(int dialogId) {
446 return null;
447 }
448
449 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800450 // mDialogFragment may not be visible yet in parent fragment's onResume().
451 // To be able to dismiss dialog at that time, don't check
452 // mDialogFragment.isVisible().
453 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700454 mDialogFragment.dismiss();
455 }
456 mDialogFragment = null;
457 }
458
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800459 /**
460 * Sets the OnCancelListener 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 setOnCancelListener(DialogInterface.OnCancelListener listener) {
465 if (mDialogFragment != null) {
466 mDialogFragment.mOnCancelListener = listener;
467 }
468 }
469
470 /**
471 * Sets the OnDismissListener of the dialog shown. This method can only be
472 * called after showDialog(int) and before removeDialog(int). The method
473 * does nothing otherwise.
474 */
475 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
476 if (mDialogFragment != null) {
477 mDialogFragment.mOnDismissListener = listener;
478 }
479 }
480
Amith Yamasanic861cf82012-10-02 14:51:46 -0700481 public void onDialogShowing() {
482 // override in subclass to attach a dismiss listener, for instance
483 }
484
Jason Monk39b46742015-09-10 15:52:51 -0400485 @Override
486 public void onDisplayPreferenceDialog(Preference preference) {
487 if (preference.getKey() == null) {
488 // Auto-key preferences that don't have a key, so the dialog can find them.
489 preference.setKey(UUID.randomUUID().toString());
490 }
491 DialogFragment f = null;
492 if (preference instanceof CustomListPreference) {
493 f = CustomListPreference.CustomListPreferenceDialogFragment
494 .newInstance(preference.getKey());
495 } else if (preference instanceof CustomDialogPreference) {
496 f = CustomDialogPreference.CustomPreferenceDialogFragment
497 .newInstance(preference.getKey());
498 } else if (preference instanceof CustomEditTextPreference) {
499 f = CustomEditTextPreference.CustomPreferenceDialogFragment
500 .newInstance(preference.getKey());
501 } else {
502 super.onDisplayPreferenceDialog(preference);
503 return;
504 }
505 f.setTargetFragment(this, 0);
506 f.show(getFragmentManager(), "dialog_preference");
507 onDialogShowing();
508 }
509
Amith Yamasani43c69782010-12-01 09:04:36 -0800510 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800511 private static final String KEY_DIALOG_ID = "key_dialog_id";
512 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
513
Amith Yamasanid7993472010-08-18 13:59:28 -0700514 private int mDialogId;
515
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800516 private Fragment mParentFragment;
517
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800518 private DialogInterface.OnCancelListener mOnCancelListener;
519 private DialogInterface.OnDismissListener mOnDismissListener;
520
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800521 public SettingsDialogFragment() {
522 /* do nothing */
523 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700524
Amith Yamasani43c69782010-12-01 09:04:36 -0800525 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700526 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800527 if (!(fragment instanceof Fragment)) {
528 throw new IllegalArgumentException("fragment argument must be an instance of "
529 + Fragment.class.getName());
530 }
531 mParentFragment = (Fragment) fragment;
532 }
533
534 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800535 public void onSaveInstanceState(Bundle outState) {
536 super.onSaveInstanceState(outState);
537 if (mParentFragment != null) {
538 outState.putInt(KEY_DIALOG_ID, mDialogId);
539 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
540 }
541 }
542
543 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700544 public void onStart() {
545 super.onStart();
546
547 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
548 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
549 }
550 }
551
552 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800553 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800554 if (savedInstanceState != null) {
555 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800556 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800557 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700558 if (mParentFragment == null) {
559 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
560 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800561 if (!(mParentFragment instanceof DialogCreatable)) {
562 throw new IllegalArgumentException(
563 (mParentFragment != null
564 ? mParentFragment.getClass().getName()
565 : mParentFragmentId)
566 + " must implement "
567 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800568 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800569 // This dialog fragment could be created from non-SettingsPreferenceFragment
570 if (mParentFragment instanceof SettingsPreferenceFragment) {
571 // restore mDialogFragment in mParentFragment
572 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
573 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800574 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800575 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700576 }
577
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800578 @Override
579 public void onCancel(DialogInterface dialog) {
580 super.onCancel(dialog);
581 if (mOnCancelListener != null) {
582 mOnCancelListener.onCancel(dialog);
583 }
584 }
585
586 @Override
587 public void onDismiss(DialogInterface dialog) {
588 super.onDismiss(dialog);
589 if (mOnDismissListener != null) {
590 mOnDismissListener.onDismiss(dialog);
591 }
592 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800593
Amith Yamasanid7993472010-08-18 13:59:28 -0700594 public int getDialogId() {
595 return mDialogId;
596 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800597
598 @Override
599 public void onDetach() {
600 super.onDetach();
601
Amith Yamasani8875ede2011-01-31 12:46:57 -0800602 // This dialog fragment could be created from non-SettingsPreferenceFragment
603 if (mParentFragment instanceof SettingsPreferenceFragment) {
604 // in case the dialog is not explicitly removed by removeDialog()
605 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
606 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
607 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800608 }
609 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700610 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700611
612 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800613 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700614 }
615
616 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800617 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700618 }
619
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700620 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700621 Activity activity = getActivity();
622 if (activity != null) {
623 activity.onBackPressed();
624 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700625 }
626
Jason Monk39b46742015-09-10 15:52:51 -0400627 protected final Context getPrefContext() {
628 return getPreferenceManager().getContext();
629 }
630
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700631 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
632 int requestCode, Bundle extras) {
633 final Activity activity = getActivity();
634 if (activity instanceof SettingsActivity) {
635 SettingsActivity sa = (SettingsActivity) activity;
636 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
637 return true;
638 } else if (activity instanceof PreferenceActivity) {
639 PreferenceActivity sa = (PreferenceActivity) activity;
640 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700641 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700642 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700643 Log.w(TAG,
644 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
645 + "launch the given Fragment (name: " + fragmentClass
646 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700647 return false;
648 }
649 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700650}