blob: 61b975382d1d04a5ece70549c541248a78cefe10 [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 Meglioc853a422014-04-18 19:40:40 -070027import android.database.DataSetObserver;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070028import android.graphics.drawable.Drawable;
Amith Yamasanid7993472010-08-18 13:59:28 -070029import android.os.Bundle;
Amith Yamasani9627a8e2012-09-23 12:54:14 -070030import android.preference.Preference;
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -070031import android.preference.PreferenceActivity;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -070032import android.preference.PreferenceGroupAdapter;
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;
38import android.view.MenuItem;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070039import android.view.View;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070040import android.view.ViewGroup;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070041import android.widget.Button;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -070042import android.widget.ListAdapter;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070043import android.widget.ListView;
Amith Yamasanid7993472010-08-18 13:59:28 -070044
John Spurlockb8e02b82015-04-15 21:15:55 -040045import com.android.settings.widget.FloatingActionButton;
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
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -070070 private ListAdapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070071 private boolean mIsDataSetObserverRegistered = false;
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070072 private DataSetObserver mDataSetObserver = new DataSetObserver() {
73 @Override
74 public void onChanged() {
75 highlightPreferenceIfNeeded();
76 }
77
78 @Override
79 public void onInvalidated() {
80 highlightPreferenceIfNeeded();
81 }
82 };
83
Fabrice Di Meglio86159282014-07-21 16:02:27 -070084 private ViewGroup mPinnedHeaderFrameLayout;
John Spurlockb8e02b82015-04-15 21:15:55 -040085 private FloatingActionButton mFloatingActionButton;
Daichi Hirono5e76cdc2015-07-08 11:38:55 +090086 private ViewGroup mButtonBar;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070087
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070088 @Override
89 public void onCreate(Bundle icicle) {
90 super.onCreate(icicle);
91
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070092 if (icicle != null) {
93 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
94 }
95
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070096 // Prepare help url and enable menu if necessary
97 int helpResource = getHelpResource();
98 if (helpResource != 0) {
Jason Monk23acc2b2015-04-14 15:06:39 -040099 mHelpUri = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700100 }
101 }
102
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700103 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700104 public View onCreateView(LayoutInflater inflater, ViewGroup container,
105 Bundle savedInstanceState) {
106 final View root = super.onCreateView(inflater, container, savedInstanceState);
107 mPinnedHeaderFrameLayout = (ViewGroup) root.findViewById(R.id.pinned_header);
John Spurlockb8e02b82015-04-15 21:15:55 -0400108 mFloatingActionButton = (FloatingActionButton) root.findViewById(R.id.fab);
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900109 mButtonBar = (ViewGroup) root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700110 return root;
111 }
112
John Spurlockb8e02b82015-04-15 21:15:55 -0400113 public FloatingActionButton getFloatingActionButton() {
114 return mFloatingActionButton;
115 }
116
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900117 public ViewGroup getButtonBar() {
118 return mButtonBar;
119 }
120
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700121 public View setPinnedHeaderView(int layoutResId) {
122 final LayoutInflater inflater = getActivity().getLayoutInflater();
123 final View pinnedHeader =
124 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
125 setPinnedHeaderView(pinnedHeader);
126 return pinnedHeader;
127 }
128
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700129 public void setPinnedHeaderView(View pinnedHeader) {
130 mPinnedHeaderFrameLayout.addView(pinnedHeader);
131 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
132 }
133
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700134 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700135 public void onSaveInstanceState(Bundle outState) {
136 super.onSaveInstanceState(outState);
137
138 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
139 }
140
141 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700142 public void onActivityCreated(Bundle savedInstanceState) {
143 super.onActivityCreated(savedInstanceState);
Jason Monk23acc2b2015-04-14 15:06:39 -0400144 if (!TextUtils.isEmpty(mHelpUri)) {
Amith Yamasanib3a593e2012-04-23 18:03:52 -0700145 setHasOptionsMenu(true);
146 }
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700147 }
148
149 @Override
150 public void onResume() {
151 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700152
153 final Bundle args = getArguments();
154 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700155 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
156 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700157 }
158 }
159
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700160 @Override
161 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700162 registerObserverIfNeeded();
163 }
164
165 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700166 protected void onUnbindPreferences() {
167 unregisterObserverIfNeeded();
168 }
169
170 @Override
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700171 public void onStop() {
172 super.onStop();
173
174 unregisterObserverIfNeeded();
175 }
176
Jason Monkb5aa73f2015-03-31 12:59:33 -0400177 public void showLoadingWhenEmpty() {
178 View loading = getView().findViewById(R.id.loading_container);
179 getListView().setEmptyView(loading);
180 }
181
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700182 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700183 if (!mIsDataSetObserverRegistered) {
184 if (mCurrentRootAdapter != null) {
185 mCurrentRootAdapter.unregisterDataSetObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700186 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700187 mCurrentRootAdapter = getPreferenceScreen().getRootAdapter();
188 mCurrentRootAdapter.registerDataSetObserver(mDataSetObserver);
189 mIsDataSetObserverRegistered = true;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700190 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700191 }
192
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700193 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700194 if (mIsDataSetObserverRegistered) {
195 if (mCurrentRootAdapter != null) {
196 mCurrentRootAdapter.unregisterDataSetObserver(mDataSetObserver);
197 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700198 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700199 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700200 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700201 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700202
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700203 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700204 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700205 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700206 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700207 }
208
209 private Drawable getHighlightDrawable() {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700210 if (mHighlightDrawable == null) {
211 mHighlightDrawable = getActivity().getDrawable(R.drawable.preference_highlight);
212 }
213 return mHighlightDrawable;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700214 }
215
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700216 /**
217 * Return a valid ListView position or -1 if none is found
218 */
219 private int canUseListViewForHighLighting(String key) {
220 if (!hasListView()) {
221 return -1;
222 }
223
224 ListView listView = getListView();
225 ListAdapter adapter = listView.getAdapter();
226
227 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
228 return findListPositionFromKey(adapter, key);
229 }
230
231 return -1;
232 }
233
234 private void highlightPreference(String key) {
235 final Drawable highlight = getHighlightDrawable();
236
237 final int position = canUseListViewForHighLighting(key);
238 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700239 mPreferenceHighlighted = true;
240
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700241 final ListView listView = getListView();
242 final ListAdapter adapter = listView.getAdapter();
243
244 ((PreferenceGroupAdapter) adapter).setHighlightedDrawable(highlight);
245 ((PreferenceGroupAdapter) adapter).setHighlighted(position);
246
247 listView.post(new Runnable() {
248 @Override
249 public void run() {
250 listView.setSelection(position);
251 listView.postDelayed(new Runnable() {
252 @Override
253 public void run() {
Alan Viverette2fed4d42014-09-08 12:40:59 -0700254 final int index = position - listView.getFirstVisiblePosition();
255 if (index >= 0 && index < listView.getChildCount()) {
256 final View v = listView.getChildAt(index);
257 final int centerX = v.getWidth() / 2;
258 final int centerY = v.getHeight() / 2;
259 highlight.setHotspot(centerX, centerY);
260 v.setPressed(true);
261 v.setPressed(false);
262 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700263 }
264 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700265 }
266 });
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700267 }
268 }
269
270 private int findListPositionFromKey(ListAdapter adapter, String key) {
271 final int count = adapter.getCount();
272 for (int n = 0; n < count; n++) {
273 final Object item = adapter.getItem(n);
274 if (item instanceof Preference) {
275 Preference preference = (Preference) item;
276 final String preferenceKey = preference.getKey();
277 if (preferenceKey != null && preferenceKey.equals(key)) {
278 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700279 }
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700280 }
281 }
282 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700283 }
284
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700285 protected void removePreference(String key) {
286 Preference pref = findPreference(key);
287 if (pref != null) {
288 getPreferenceScreen().removePreference(pref);
289 }
290 }
291
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700292 /**
293 * Override this if you want to show a help item in the menu, by returning the resource id.
294 * @return the resource id for the help url
295 */
296 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400297 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700298 }
299
300 @Override
301 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400302 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400303 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700304 }
305 }
306
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700307 /*
308 * The name is intentionally made different from Activity#finish(), so that
309 * users won't misunderstand its meaning.
310 */
311 public final void finishFragment() {
312 getActivity().onBackPressed();
313 }
314
Amith Yamasanid7993472010-08-18 13:59:28 -0700315 // Some helpers for functions used by the settings fragments when they were activities
316
317 /**
318 * Returns the ContentResolver from the owning Activity.
319 */
320 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700321 Context context = getActivity();
322 if (context != null) {
323 mContentResolver = context.getContentResolver();
324 }
325 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700326 }
327
328 /**
329 * Returns the specified system service from the owning Activity.
330 */
331 protected Object getSystemService(final String name) {
332 return getActivity().getSystemService(name);
333 }
334
335 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700336 * Returns the PackageManager from the owning Activity.
337 */
338 protected PackageManager getPackageManager() {
339 return getActivity().getPackageManager();
340 }
341
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800342 @Override
343 public void onDetach() {
344 if (isRemoving()) {
345 if (mDialogFragment != null) {
346 mDialogFragment.dismiss();
347 mDialogFragment = null;
348 }
349 }
350 super.onDetach();
351 }
352
Amith Yamasanid7993472010-08-18 13:59:28 -0700353 // Dialog management
354
355 protected void showDialog(int dialogId) {
356 if (mDialogFragment != null) {
357 Log.e(TAG, "Old dialog fragment not null!");
358 }
359 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800360 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700361 }
362
363 public Dialog onCreateDialog(int dialogId) {
364 return null;
365 }
366
367 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800368 // mDialogFragment may not be visible yet in parent fragment's onResume().
369 // To be able to dismiss dialog at that time, don't check
370 // mDialogFragment.isVisible().
371 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700372 mDialogFragment.dismiss();
373 }
374 mDialogFragment = null;
375 }
376
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800377 /**
378 * Sets the OnCancelListener of the dialog shown. This method can only be
379 * called after showDialog(int) and before removeDialog(int). The method
380 * does nothing otherwise.
381 */
382 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
383 if (mDialogFragment != null) {
384 mDialogFragment.mOnCancelListener = listener;
385 }
386 }
387
388 /**
389 * Sets the OnDismissListener of the dialog shown. This method can only be
390 * called after showDialog(int) and before removeDialog(int). The method
391 * does nothing otherwise.
392 */
393 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
394 if (mDialogFragment != null) {
395 mDialogFragment.mOnDismissListener = listener;
396 }
397 }
398
Amith Yamasanic861cf82012-10-02 14:51:46 -0700399 public void onDialogShowing() {
400 // override in subclass to attach a dismiss listener, for instance
401 }
402
Amith Yamasani43c69782010-12-01 09:04:36 -0800403 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800404 private static final String KEY_DIALOG_ID = "key_dialog_id";
405 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
406
Amith Yamasanid7993472010-08-18 13:59:28 -0700407 private int mDialogId;
408
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800409 private Fragment mParentFragment;
410
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800411 private DialogInterface.OnCancelListener mOnCancelListener;
412 private DialogInterface.OnDismissListener mOnDismissListener;
413
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800414 public SettingsDialogFragment() {
415 /* do nothing */
416 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700417
Amith Yamasani43c69782010-12-01 09:04:36 -0800418 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700419 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800420 if (!(fragment instanceof Fragment)) {
421 throw new IllegalArgumentException("fragment argument must be an instance of "
422 + Fragment.class.getName());
423 }
424 mParentFragment = (Fragment) fragment;
425 }
426
427 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800428 public void onSaveInstanceState(Bundle outState) {
429 super.onSaveInstanceState(outState);
430 if (mParentFragment != null) {
431 outState.putInt(KEY_DIALOG_ID, mDialogId);
432 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
433 }
434 }
435
436 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700437 public void onStart() {
438 super.onStart();
439
440 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
441 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
442 }
443 }
444
445 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800446 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800447 if (savedInstanceState != null) {
448 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800449 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800450 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700451 if (mParentFragment == null) {
452 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
453 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800454 if (!(mParentFragment instanceof DialogCreatable)) {
455 throw new IllegalArgumentException(
456 (mParentFragment != null
457 ? mParentFragment.getClass().getName()
458 : mParentFragmentId)
459 + " must implement "
460 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800461 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800462 // This dialog fragment could be created from non-SettingsPreferenceFragment
463 if (mParentFragment instanceof SettingsPreferenceFragment) {
464 // restore mDialogFragment in mParentFragment
465 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
466 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800467 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800468 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700469 }
470
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800471 @Override
472 public void onCancel(DialogInterface dialog) {
473 super.onCancel(dialog);
474 if (mOnCancelListener != null) {
475 mOnCancelListener.onCancel(dialog);
476 }
477 }
478
479 @Override
480 public void onDismiss(DialogInterface dialog) {
481 super.onDismiss(dialog);
482 if (mOnDismissListener != null) {
483 mOnDismissListener.onDismiss(dialog);
484 }
485 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800486
Amith Yamasanid7993472010-08-18 13:59:28 -0700487 public int getDialogId() {
488 return mDialogId;
489 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800490
491 @Override
492 public void onDetach() {
493 super.onDetach();
494
Amith Yamasani8875ede2011-01-31 12:46:57 -0800495 // This dialog fragment could be created from non-SettingsPreferenceFragment
496 if (mParentFragment instanceof SettingsPreferenceFragment) {
497 // in case the dialog is not explicitly removed by removeDialog()
498 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
499 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
500 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800501 }
502 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700503 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700504
505 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800506 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700507 }
508
509 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800510 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700511 }
512
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700513 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700514 Activity activity = getActivity();
515 if (activity != null) {
516 activity.onBackPressed();
517 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700518 }
519
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700520 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
521 int requestCode, Bundle extras) {
522 final Activity activity = getActivity();
523 if (activity instanceof SettingsActivity) {
524 SettingsActivity sa = (SettingsActivity) activity;
525 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
526 return true;
527 } else if (activity instanceof PreferenceActivity) {
528 PreferenceActivity sa = (PreferenceActivity) activity;
529 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700530 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700531 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700532 Log.w(TAG,
533 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
534 + "launch the given Fragment (name: " + fragmentClass
535 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700536 return false;
537 }
538 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700539}