blob: bf41c803862409eb0e6ab6576fe855e71b8035b2 [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
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070045/**
Amith Yamasanid7993472010-08-18 13:59:28 -070046 * Base class for Settings fragments, with some helper functions and dialog management.
47 */
Chris Wren8a963ba2015-03-20 10:29:14 -040048public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceFragment
49 implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070050
51 private static final String TAG = "SettingsPreferenceFragment";
52
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070053 private static final int MENU_HELP = Menu.FIRST + 100;
Fabrice Di Meglioeced7802014-09-04 13:01:55 -070054 private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070055
56 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070057
Amith Yamasanid7993472010-08-18 13:59:28 -070058 private SettingsDialogFragment mDialogFragment;
59
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070060 private String mHelpUrl;
61
Amith Yamasani350938e2013-04-09 10:22:47 -070062 // Cache the content resolver for async callbacks
63 private ContentResolver mContentResolver;
64
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070065 private String mPreferenceKey;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070066 private boolean mPreferenceHighlighted = false;
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -070067 private Drawable mHighlightDrawable;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070068
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -070069 private ListAdapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070070 private boolean mIsDataSetObserverRegistered = false;
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070071 private DataSetObserver mDataSetObserver = new DataSetObserver() {
72 @Override
73 public void onChanged() {
74 highlightPreferenceIfNeeded();
75 }
76
77 @Override
78 public void onInvalidated() {
79 highlightPreferenceIfNeeded();
80 }
81 };
82
Fabrice Di Meglio86159282014-07-21 16:02:27 -070083 private ViewGroup mPinnedHeaderFrameLayout;
84
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070085 @Override
86 public void onCreate(Bundle icicle) {
87 super.onCreate(icicle);
88
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070089 if (icicle != null) {
90 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
91 }
92
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070093 // Prepare help url and enable menu if necessary
94 int helpResource = getHelpResource();
95 if (helpResource != 0) {
96 mHelpUrl = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070097 }
98 }
99
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700100 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700101 public View onCreateView(LayoutInflater inflater, ViewGroup container,
102 Bundle savedInstanceState) {
103 final View root = super.onCreateView(inflater, container, savedInstanceState);
104 mPinnedHeaderFrameLayout = (ViewGroup) root.findViewById(R.id.pinned_header);
105 return root;
106 }
107
108 public void setPinnedHeaderView(View pinnedHeader) {
109 mPinnedHeaderFrameLayout.addView(pinnedHeader);
110 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
111 }
112
113 public void clearPinnedHeaderView() {
114 mPinnedHeaderFrameLayout.removeAllViews();
115 mPinnedHeaderFrameLayout.setVisibility(View.GONE);
116 }
117
118 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700119 public void onSaveInstanceState(Bundle outState) {
120 super.onSaveInstanceState(outState);
121
122 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
123 }
124
125 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700126 public void onActivityCreated(Bundle savedInstanceState) {
127 super.onActivityCreated(savedInstanceState);
Amith Yamasanib3a593e2012-04-23 18:03:52 -0700128 if (!TextUtils.isEmpty(mHelpUrl)) {
129 setHasOptionsMenu(true);
130 }
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700131 }
132
133 @Override
134 public void onResume() {
135 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700136
137 final Bundle args = getArguments();
138 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700139 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
140 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700141 }
142 }
143
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700144 @Override
145 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700146 registerObserverIfNeeded();
147 }
148
149 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700150 protected void onUnbindPreferences() {
151 unregisterObserverIfNeeded();
152 }
153
154 @Override
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700155 public void onStop() {
156 super.onStop();
157
158 unregisterObserverIfNeeded();
159 }
160
161 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700162 if (!mIsDataSetObserverRegistered) {
163 if (mCurrentRootAdapter != null) {
164 mCurrentRootAdapter.unregisterDataSetObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700165 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700166 mCurrentRootAdapter = getPreferenceScreen().getRootAdapter();
167 mCurrentRootAdapter.registerDataSetObserver(mDataSetObserver);
168 mIsDataSetObserverRegistered = true;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700169 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700170 }
171
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700172 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700173 if (mIsDataSetObserverRegistered) {
174 if (mCurrentRootAdapter != null) {
175 mCurrentRootAdapter.unregisterDataSetObserver(mDataSetObserver);
176 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700177 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700178 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700179 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700180 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700181
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700182 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700183 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700184 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700185 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700186 }
187
188 private Drawable getHighlightDrawable() {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700189 if (mHighlightDrawable == null) {
190 mHighlightDrawable = getActivity().getDrawable(R.drawable.preference_highlight);
191 }
192 return mHighlightDrawable;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700193 }
194
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700195 /**
196 * Return a valid ListView position or -1 if none is found
197 */
198 private int canUseListViewForHighLighting(String key) {
199 if (!hasListView()) {
200 return -1;
201 }
202
203 ListView listView = getListView();
204 ListAdapter adapter = listView.getAdapter();
205
206 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
207 return findListPositionFromKey(adapter, key);
208 }
209
210 return -1;
211 }
212
213 private void highlightPreference(String key) {
214 final Drawable highlight = getHighlightDrawable();
215
216 final int position = canUseListViewForHighLighting(key);
217 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700218 mPreferenceHighlighted = true;
219
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700220 final ListView listView = getListView();
221 final ListAdapter adapter = listView.getAdapter();
222
223 ((PreferenceGroupAdapter) adapter).setHighlightedDrawable(highlight);
224 ((PreferenceGroupAdapter) adapter).setHighlighted(position);
225
226 listView.post(new Runnable() {
227 @Override
228 public void run() {
229 listView.setSelection(position);
230 listView.postDelayed(new Runnable() {
231 @Override
232 public void run() {
Alan Viverette2fed4d42014-09-08 12:40:59 -0700233 final int index = position - listView.getFirstVisiblePosition();
234 if (index >= 0 && index < listView.getChildCount()) {
235 final View v = listView.getChildAt(index);
236 final int centerX = v.getWidth() / 2;
237 final int centerY = v.getHeight() / 2;
238 highlight.setHotspot(centerX, centerY);
239 v.setPressed(true);
240 v.setPressed(false);
241 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700242 }
243 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700244 }
245 });
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700246 }
247 }
248
249 private int findListPositionFromKey(ListAdapter adapter, String key) {
250 final int count = adapter.getCount();
251 for (int n = 0; n < count; n++) {
252 final Object item = adapter.getItem(n);
253 if (item instanceof Preference) {
254 Preference preference = (Preference) item;
255 final String preferenceKey = preference.getKey();
256 if (preferenceKey != null && preferenceKey.equals(key)) {
257 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700258 }
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700259 }
260 }
261 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700262 }
263
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700264 protected void removePreference(String key) {
265 Preference pref = findPreference(key);
266 if (pref != null) {
267 getPreferenceScreen().removePreference(pref);
268 }
269 }
270
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700271 /**
272 * Override this if you want to show a help item in the menu, by returning the resource id.
273 * @return the resource id for the help url
274 */
275 protected int getHelpResource() {
276 return 0;
277 }
278
279 @Override
280 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800281 if (mHelpUrl != null && getActivity() != null) {
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700282 MenuItem helpItem = menu.add(0, MENU_HELP, 0, R.string.help_label);
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800283 HelpUtils.prepareHelpMenuItem(getActivity(), helpItem, mHelpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700284 }
285 }
286
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700287 /*
288 * The name is intentionally made different from Activity#finish(), so that
289 * users won't misunderstand its meaning.
290 */
291 public final void finishFragment() {
292 getActivity().onBackPressed();
293 }
294
Amith Yamasanid7993472010-08-18 13:59:28 -0700295 // Some helpers for functions used by the settings fragments when they were activities
296
297 /**
298 * Returns the ContentResolver from the owning Activity.
299 */
300 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700301 Context context = getActivity();
302 if (context != null) {
303 mContentResolver = context.getContentResolver();
304 }
305 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700306 }
307
308 /**
309 * Returns the specified system service from the owning Activity.
310 */
311 protected Object getSystemService(final String name) {
312 return getActivity().getSystemService(name);
313 }
314
315 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700316 * Returns the PackageManager from the owning Activity.
317 */
318 protected PackageManager getPackageManager() {
319 return getActivity().getPackageManager();
320 }
321
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800322 @Override
323 public void onDetach() {
324 if (isRemoving()) {
325 if (mDialogFragment != null) {
326 mDialogFragment.dismiss();
327 mDialogFragment = null;
328 }
329 }
330 super.onDetach();
331 }
332
Amith Yamasanid7993472010-08-18 13:59:28 -0700333 // Dialog management
334
335 protected void showDialog(int dialogId) {
336 if (mDialogFragment != null) {
337 Log.e(TAG, "Old dialog fragment not null!");
338 }
339 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800340 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700341 }
342
343 public Dialog onCreateDialog(int dialogId) {
344 return null;
345 }
346
347 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800348 // mDialogFragment may not be visible yet in parent fragment's onResume().
349 // To be able to dismiss dialog at that time, don't check
350 // mDialogFragment.isVisible().
351 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700352 mDialogFragment.dismiss();
353 }
354 mDialogFragment = null;
355 }
356
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800357 /**
358 * Sets the OnCancelListener of the dialog shown. This method can only be
359 * called after showDialog(int) and before removeDialog(int). The method
360 * does nothing otherwise.
361 */
362 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
363 if (mDialogFragment != null) {
364 mDialogFragment.mOnCancelListener = listener;
365 }
366 }
367
368 /**
369 * Sets the OnDismissListener of the dialog shown. This method can only be
370 * called after showDialog(int) and before removeDialog(int). The method
371 * does nothing otherwise.
372 */
373 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
374 if (mDialogFragment != null) {
375 mDialogFragment.mOnDismissListener = listener;
376 }
377 }
378
Amith Yamasanic861cf82012-10-02 14:51:46 -0700379 public void onDialogShowing() {
380 // override in subclass to attach a dismiss listener, for instance
381 }
382
Amith Yamasani43c69782010-12-01 09:04:36 -0800383 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800384 private static final String KEY_DIALOG_ID = "key_dialog_id";
385 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
386
Amith Yamasanid7993472010-08-18 13:59:28 -0700387 private int mDialogId;
388
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800389 private Fragment mParentFragment;
390
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800391 private DialogInterface.OnCancelListener mOnCancelListener;
392 private DialogInterface.OnDismissListener mOnDismissListener;
393
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800394 public SettingsDialogFragment() {
395 /* do nothing */
396 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700397
Amith Yamasani43c69782010-12-01 09:04:36 -0800398 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700399 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800400 if (!(fragment instanceof Fragment)) {
401 throw new IllegalArgumentException("fragment argument must be an instance of "
402 + Fragment.class.getName());
403 }
404 mParentFragment = (Fragment) fragment;
405 }
406
407 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800408 public void onSaveInstanceState(Bundle outState) {
409 super.onSaveInstanceState(outState);
410 if (mParentFragment != null) {
411 outState.putInt(KEY_DIALOG_ID, mDialogId);
412 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
413 }
414 }
415
416 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700417 public void onStart() {
418 super.onStart();
419
420 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
421 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
422 }
423 }
424
425 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800426 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800427 if (savedInstanceState != null) {
428 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800429 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800430 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700431 if (mParentFragment == null) {
432 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
433 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800434 if (!(mParentFragment instanceof DialogCreatable)) {
435 throw new IllegalArgumentException(
436 (mParentFragment != null
437 ? mParentFragment.getClass().getName()
438 : mParentFragmentId)
439 + " must implement "
440 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800441 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800442 // This dialog fragment could be created from non-SettingsPreferenceFragment
443 if (mParentFragment instanceof SettingsPreferenceFragment) {
444 // restore mDialogFragment in mParentFragment
445 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
446 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800447 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800448 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700449 }
450
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800451 @Override
452 public void onCancel(DialogInterface dialog) {
453 super.onCancel(dialog);
454 if (mOnCancelListener != null) {
455 mOnCancelListener.onCancel(dialog);
456 }
457 }
458
459 @Override
460 public void onDismiss(DialogInterface dialog) {
461 super.onDismiss(dialog);
462 if (mOnDismissListener != null) {
463 mOnDismissListener.onDismiss(dialog);
464 }
465 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800466
Amith Yamasanid7993472010-08-18 13:59:28 -0700467 public int getDialogId() {
468 return mDialogId;
469 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800470
471 @Override
472 public void onDetach() {
473 super.onDetach();
474
Amith Yamasani8875ede2011-01-31 12:46:57 -0800475 // This dialog fragment could be created from non-SettingsPreferenceFragment
476 if (mParentFragment instanceof SettingsPreferenceFragment) {
477 // in case the dialog is not explicitly removed by removeDialog()
478 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
479 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
480 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800481 }
482 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700483 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700484
485 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800486 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700487 }
488
489 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800490 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700491 }
492
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700493 public void finish() {
494 getActivity().onBackPressed();
495 }
496
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700497 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
498 int requestCode, Bundle extras) {
499 final Activity activity = getActivity();
500 if (activity instanceof SettingsActivity) {
501 SettingsActivity sa = (SettingsActivity) activity;
502 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
503 return true;
504 } else if (activity instanceof PreferenceActivity) {
505 PreferenceActivity sa = (PreferenceActivity) activity;
506 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700507 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700508 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700509 Log.w(TAG,
510 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
511 + "launch the given Fragment (name: " + fragmentClass
512 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700513 return false;
514 }
515 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700516}