blob: 74cb0fe3f61192d5dbff9602f92053f54cfe6ac2 [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
Fabrice Di Meglioeced7802014-09-04 13:01:55 -070053 private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070054
55 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070056
Amith Yamasanid7993472010-08-18 13:59:28 -070057 private SettingsDialogFragment mDialogFragment;
58
Jason Monk23acc2b2015-04-14 15:06:39 -040059 private String mHelpUri;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070060
Amith Yamasani350938e2013-04-09 10:22:47 -070061 // Cache the content resolver for async callbacks
62 private ContentResolver mContentResolver;
63
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070064 private String mPreferenceKey;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070065 private boolean mPreferenceHighlighted = false;
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -070066 private Drawable mHighlightDrawable;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070067
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -070068 private ListAdapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070069 private boolean mIsDataSetObserverRegistered = false;
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070070 private DataSetObserver mDataSetObserver = new DataSetObserver() {
71 @Override
72 public void onChanged() {
73 highlightPreferenceIfNeeded();
74 }
75
76 @Override
77 public void onInvalidated() {
78 highlightPreferenceIfNeeded();
79 }
80 };
81
Fabrice Di Meglio86159282014-07-21 16:02:27 -070082 private ViewGroup mPinnedHeaderFrameLayout;
83
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070084 @Override
85 public void onCreate(Bundle icicle) {
86 super.onCreate(icicle);
87
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070088 if (icicle != null) {
89 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
90 }
91
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070092 // Prepare help url and enable menu if necessary
93 int helpResource = getHelpResource();
94 if (helpResource != 0) {
Jason Monk23acc2b2015-04-14 15:06:39 -040095 mHelpUri = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070096 }
97 }
98
Daisuke Miyakawab5647c52010-09-10 18:04:02 -070099 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700100 public View onCreateView(LayoutInflater inflater, ViewGroup container,
101 Bundle savedInstanceState) {
102 final View root = super.onCreateView(inflater, container, savedInstanceState);
103 mPinnedHeaderFrameLayout = (ViewGroup) root.findViewById(R.id.pinned_header);
104 return root;
105 }
106
107 public void setPinnedHeaderView(View pinnedHeader) {
108 mPinnedHeaderFrameLayout.addView(pinnedHeader);
109 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
110 }
111
112 public void clearPinnedHeaderView() {
113 mPinnedHeaderFrameLayout.removeAllViews();
114 mPinnedHeaderFrameLayout.setVisibility(View.GONE);
115 }
116
117 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700118 public void onSaveInstanceState(Bundle outState) {
119 super.onSaveInstanceState(outState);
120
121 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
122 }
123
124 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700125 public void onActivityCreated(Bundle savedInstanceState) {
126 super.onActivityCreated(savedInstanceState);
Jason Monk23acc2b2015-04-14 15:06:39 -0400127 if (!TextUtils.isEmpty(mHelpUri)) {
Amith Yamasanib3a593e2012-04-23 18:03:52 -0700128 setHasOptionsMenu(true);
129 }
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700130 }
131
132 @Override
133 public void onResume() {
134 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700135
136 final Bundle args = getArguments();
137 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700138 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
139 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700140 }
141 }
142
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700143 @Override
144 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700145 registerObserverIfNeeded();
146 }
147
148 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700149 protected void onUnbindPreferences() {
150 unregisterObserverIfNeeded();
151 }
152
153 @Override
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700154 public void onStop() {
155 super.onStop();
156
157 unregisterObserverIfNeeded();
158 }
159
160 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700161 if (!mIsDataSetObserverRegistered) {
162 if (mCurrentRootAdapter != null) {
163 mCurrentRootAdapter.unregisterDataSetObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700164 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700165 mCurrentRootAdapter = getPreferenceScreen().getRootAdapter();
166 mCurrentRootAdapter.registerDataSetObserver(mDataSetObserver);
167 mIsDataSetObserverRegistered = true;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700168 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700169 }
170
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700171 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700172 if (mIsDataSetObserverRegistered) {
173 if (mCurrentRootAdapter != null) {
174 mCurrentRootAdapter.unregisterDataSetObserver(mDataSetObserver);
175 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700176 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700177 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700178 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700179 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700180
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700181 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700182 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700183 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700184 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700185 }
186
187 private Drawable getHighlightDrawable() {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700188 if (mHighlightDrawable == null) {
189 mHighlightDrawable = getActivity().getDrawable(R.drawable.preference_highlight);
190 }
191 return mHighlightDrawable;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700192 }
193
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700194 /**
195 * Return a valid ListView position or -1 if none is found
196 */
197 private int canUseListViewForHighLighting(String key) {
198 if (!hasListView()) {
199 return -1;
200 }
201
202 ListView listView = getListView();
203 ListAdapter adapter = listView.getAdapter();
204
205 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
206 return findListPositionFromKey(adapter, key);
207 }
208
209 return -1;
210 }
211
212 private void highlightPreference(String key) {
213 final Drawable highlight = getHighlightDrawable();
214
215 final int position = canUseListViewForHighLighting(key);
216 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700217 mPreferenceHighlighted = true;
218
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700219 final ListView listView = getListView();
220 final ListAdapter adapter = listView.getAdapter();
221
222 ((PreferenceGroupAdapter) adapter).setHighlightedDrawable(highlight);
223 ((PreferenceGroupAdapter) adapter).setHighlighted(position);
224
225 listView.post(new Runnable() {
226 @Override
227 public void run() {
228 listView.setSelection(position);
229 listView.postDelayed(new Runnable() {
230 @Override
231 public void run() {
Alan Viverette2fed4d42014-09-08 12:40:59 -0700232 final int index = position - listView.getFirstVisiblePosition();
233 if (index >= 0 && index < listView.getChildCount()) {
234 final View v = listView.getChildAt(index);
235 final int centerX = v.getWidth() / 2;
236 final int centerY = v.getHeight() / 2;
237 highlight.setHotspot(centerX, centerY);
238 v.setPressed(true);
239 v.setPressed(false);
240 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700241 }
242 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700243 }
244 });
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700245 }
246 }
247
248 private int findListPositionFromKey(ListAdapter adapter, String key) {
249 final int count = adapter.getCount();
250 for (int n = 0; n < count; n++) {
251 final Object item = adapter.getItem(n);
252 if (item instanceof Preference) {
253 Preference preference = (Preference) item;
254 final String preferenceKey = preference.getKey();
255 if (preferenceKey != null && preferenceKey.equals(key)) {
256 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700257 }
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700258 }
259 }
260 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700261 }
262
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700263 protected void removePreference(String key) {
264 Preference pref = findPreference(key);
265 if (pref != null) {
266 getPreferenceScreen().removePreference(pref);
267 }
268 }
269
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700270 /**
271 * Override this if you want to show a help item in the menu, by returning the resource id.
272 * @return the resource id for the help url
273 */
274 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400275 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700276 }
277
278 @Override
279 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400280 if (mHelpUri != null && getActivity() != null) {
281 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700282 }
283 }
284
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700285 /*
286 * The name is intentionally made different from Activity#finish(), so that
287 * users won't misunderstand its meaning.
288 */
289 public final void finishFragment() {
290 getActivity().onBackPressed();
291 }
292
Amith Yamasanid7993472010-08-18 13:59:28 -0700293 // Some helpers for functions used by the settings fragments when they were activities
294
295 /**
296 * Returns the ContentResolver from the owning Activity.
297 */
298 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700299 Context context = getActivity();
300 if (context != null) {
301 mContentResolver = context.getContentResolver();
302 }
303 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700304 }
305
306 /**
307 * Returns the specified system service from the owning Activity.
308 */
309 protected Object getSystemService(final String name) {
310 return getActivity().getSystemService(name);
311 }
312
313 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700314 * Returns the PackageManager from the owning Activity.
315 */
316 protected PackageManager getPackageManager() {
317 return getActivity().getPackageManager();
318 }
319
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800320 @Override
321 public void onDetach() {
322 if (isRemoving()) {
323 if (mDialogFragment != null) {
324 mDialogFragment.dismiss();
325 mDialogFragment = null;
326 }
327 }
328 super.onDetach();
329 }
330
Amith Yamasanid7993472010-08-18 13:59:28 -0700331 // Dialog management
332
333 protected void showDialog(int dialogId) {
334 if (mDialogFragment != null) {
335 Log.e(TAG, "Old dialog fragment not null!");
336 }
337 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800338 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700339 }
340
341 public Dialog onCreateDialog(int dialogId) {
342 return null;
343 }
344
345 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800346 // mDialogFragment may not be visible yet in parent fragment's onResume().
347 // To be able to dismiss dialog at that time, don't check
348 // mDialogFragment.isVisible().
349 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700350 mDialogFragment.dismiss();
351 }
352 mDialogFragment = null;
353 }
354
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800355 /**
356 * Sets the OnCancelListener of the dialog shown. This method can only be
357 * called after showDialog(int) and before removeDialog(int). The method
358 * does nothing otherwise.
359 */
360 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
361 if (mDialogFragment != null) {
362 mDialogFragment.mOnCancelListener = listener;
363 }
364 }
365
366 /**
367 * Sets the OnDismissListener of the dialog shown. This method can only be
368 * called after showDialog(int) and before removeDialog(int). The method
369 * does nothing otherwise.
370 */
371 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
372 if (mDialogFragment != null) {
373 mDialogFragment.mOnDismissListener = listener;
374 }
375 }
376
Amith Yamasanic861cf82012-10-02 14:51:46 -0700377 public void onDialogShowing() {
378 // override in subclass to attach a dismiss listener, for instance
379 }
380
Amith Yamasani43c69782010-12-01 09:04:36 -0800381 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800382 private static final String KEY_DIALOG_ID = "key_dialog_id";
383 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
384
Amith Yamasanid7993472010-08-18 13:59:28 -0700385 private int mDialogId;
386
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800387 private Fragment mParentFragment;
388
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800389 private DialogInterface.OnCancelListener mOnCancelListener;
390 private DialogInterface.OnDismissListener mOnDismissListener;
391
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800392 public SettingsDialogFragment() {
393 /* do nothing */
394 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700395
Amith Yamasani43c69782010-12-01 09:04:36 -0800396 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700397 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800398 if (!(fragment instanceof Fragment)) {
399 throw new IllegalArgumentException("fragment argument must be an instance of "
400 + Fragment.class.getName());
401 }
402 mParentFragment = (Fragment) fragment;
403 }
404
405 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800406 public void onSaveInstanceState(Bundle outState) {
407 super.onSaveInstanceState(outState);
408 if (mParentFragment != null) {
409 outState.putInt(KEY_DIALOG_ID, mDialogId);
410 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
411 }
412 }
413
414 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700415 public void onStart() {
416 super.onStart();
417
418 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
419 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
420 }
421 }
422
423 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800424 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800425 if (savedInstanceState != null) {
426 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800427 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800428 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700429 if (mParentFragment == null) {
430 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
431 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800432 if (!(mParentFragment instanceof DialogCreatable)) {
433 throw new IllegalArgumentException(
434 (mParentFragment != null
435 ? mParentFragment.getClass().getName()
436 : mParentFragmentId)
437 + " must implement "
438 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800439 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800440 // This dialog fragment could be created from non-SettingsPreferenceFragment
441 if (mParentFragment instanceof SettingsPreferenceFragment) {
442 // restore mDialogFragment in mParentFragment
443 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
444 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800445 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800446 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700447 }
448
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800449 @Override
450 public void onCancel(DialogInterface dialog) {
451 super.onCancel(dialog);
452 if (mOnCancelListener != null) {
453 mOnCancelListener.onCancel(dialog);
454 }
455 }
456
457 @Override
458 public void onDismiss(DialogInterface dialog) {
459 super.onDismiss(dialog);
460 if (mOnDismissListener != null) {
461 mOnDismissListener.onDismiss(dialog);
462 }
463 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800464
Amith Yamasanid7993472010-08-18 13:59:28 -0700465 public int getDialogId() {
466 return mDialogId;
467 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800468
469 @Override
470 public void onDetach() {
471 super.onDetach();
472
Amith Yamasani8875ede2011-01-31 12:46:57 -0800473 // This dialog fragment could be created from non-SettingsPreferenceFragment
474 if (mParentFragment instanceof SettingsPreferenceFragment) {
475 // in case the dialog is not explicitly removed by removeDialog()
476 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
477 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
478 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800479 }
480 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700481 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700482
483 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800484 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700485 }
486
487 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800488 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700489 }
490
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700491 public void finish() {
492 getActivity().onBackPressed();
493 }
494
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700495 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
496 int requestCode, Bundle extras) {
497 final Activity activity = getActivity();
498 if (activity instanceof SettingsActivity) {
499 SettingsActivity sa = (SettingsActivity) activity;
500 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
501 return true;
502 } else if (activity instanceof PreferenceActivity) {
503 PreferenceActivity sa = (PreferenceActivity) activity;
504 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700505 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700506 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700507 Log.w(TAG,
508 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
509 + "launch the given Fragment (name: " + fragmentClass
510 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700511 return false;
512 }
513 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700514}