blob: 9e645ab08ce9b026732807f661a8dc6a641a08f1 [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
Jason Monkb5aa73f2015-03-31 12:59:33 -0400160 public void showLoadingWhenEmpty() {
161 View loading = getView().findViewById(R.id.loading_container);
162 getListView().setEmptyView(loading);
163 }
164
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700165 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700166 if (!mIsDataSetObserverRegistered) {
167 if (mCurrentRootAdapter != null) {
168 mCurrentRootAdapter.unregisterDataSetObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700169 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700170 mCurrentRootAdapter = getPreferenceScreen().getRootAdapter();
171 mCurrentRootAdapter.registerDataSetObserver(mDataSetObserver);
172 mIsDataSetObserverRegistered = true;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700173 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700174 }
175
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700176 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700177 if (mIsDataSetObserverRegistered) {
178 if (mCurrentRootAdapter != null) {
179 mCurrentRootAdapter.unregisterDataSetObserver(mDataSetObserver);
180 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700181 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700182 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700183 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700184 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700185
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700186 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700187 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700188 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700189 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700190 }
191
192 private Drawable getHighlightDrawable() {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700193 if (mHighlightDrawable == null) {
194 mHighlightDrawable = getActivity().getDrawable(R.drawable.preference_highlight);
195 }
196 return mHighlightDrawable;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700197 }
198
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700199 /**
200 * Return a valid ListView position or -1 if none is found
201 */
202 private int canUseListViewForHighLighting(String key) {
203 if (!hasListView()) {
204 return -1;
205 }
206
207 ListView listView = getListView();
208 ListAdapter adapter = listView.getAdapter();
209
210 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
211 return findListPositionFromKey(adapter, key);
212 }
213
214 return -1;
215 }
216
217 private void highlightPreference(String key) {
218 final Drawable highlight = getHighlightDrawable();
219
220 final int position = canUseListViewForHighLighting(key);
221 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700222 mPreferenceHighlighted = true;
223
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700224 final ListView listView = getListView();
225 final ListAdapter adapter = listView.getAdapter();
226
227 ((PreferenceGroupAdapter) adapter).setHighlightedDrawable(highlight);
228 ((PreferenceGroupAdapter) adapter).setHighlighted(position);
229
230 listView.post(new Runnable() {
231 @Override
232 public void run() {
233 listView.setSelection(position);
234 listView.postDelayed(new Runnable() {
235 @Override
236 public void run() {
Alan Viverette2fed4d42014-09-08 12:40:59 -0700237 final int index = position - listView.getFirstVisiblePosition();
238 if (index >= 0 && index < listView.getChildCount()) {
239 final View v = listView.getChildAt(index);
240 final int centerX = v.getWidth() / 2;
241 final int centerY = v.getHeight() / 2;
242 highlight.setHotspot(centerX, centerY);
243 v.setPressed(true);
244 v.setPressed(false);
245 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700246 }
247 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700248 }
249 });
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700250 }
251 }
252
253 private int findListPositionFromKey(ListAdapter adapter, String key) {
254 final int count = adapter.getCount();
255 for (int n = 0; n < count; n++) {
256 final Object item = adapter.getItem(n);
257 if (item instanceof Preference) {
258 Preference preference = (Preference) item;
259 final String preferenceKey = preference.getKey();
260 if (preferenceKey != null && preferenceKey.equals(key)) {
261 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700262 }
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700263 }
264 }
265 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700266 }
267
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700268 protected void removePreference(String key) {
269 Preference pref = findPreference(key);
270 if (pref != null) {
271 getPreferenceScreen().removePreference(pref);
272 }
273 }
274
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700275 /**
276 * Override this if you want to show a help item in the menu, by returning the resource id.
277 * @return the resource id for the help url
278 */
279 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400280 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700281 }
282
283 @Override
284 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400285 if (mHelpUri != null && getActivity() != null) {
286 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700287 }
288 }
289
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700290 /*
291 * The name is intentionally made different from Activity#finish(), so that
292 * users won't misunderstand its meaning.
293 */
294 public final void finishFragment() {
295 getActivity().onBackPressed();
296 }
297
Amith Yamasanid7993472010-08-18 13:59:28 -0700298 // Some helpers for functions used by the settings fragments when they were activities
299
300 /**
301 * Returns the ContentResolver from the owning Activity.
302 */
303 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700304 Context context = getActivity();
305 if (context != null) {
306 mContentResolver = context.getContentResolver();
307 }
308 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700309 }
310
311 /**
312 * Returns the specified system service from the owning Activity.
313 */
314 protected Object getSystemService(final String name) {
315 return getActivity().getSystemService(name);
316 }
317
318 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700319 * Returns the PackageManager from the owning Activity.
320 */
321 protected PackageManager getPackageManager() {
322 return getActivity().getPackageManager();
323 }
324
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800325 @Override
326 public void onDetach() {
327 if (isRemoving()) {
328 if (mDialogFragment != null) {
329 mDialogFragment.dismiss();
330 mDialogFragment = null;
331 }
332 }
333 super.onDetach();
334 }
335
Amith Yamasanid7993472010-08-18 13:59:28 -0700336 // Dialog management
337
338 protected void showDialog(int dialogId) {
339 if (mDialogFragment != null) {
340 Log.e(TAG, "Old dialog fragment not null!");
341 }
342 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800343 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700344 }
345
346 public Dialog onCreateDialog(int dialogId) {
347 return null;
348 }
349
350 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800351 // mDialogFragment may not be visible yet in parent fragment's onResume().
352 // To be able to dismiss dialog at that time, don't check
353 // mDialogFragment.isVisible().
354 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700355 mDialogFragment.dismiss();
356 }
357 mDialogFragment = null;
358 }
359
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800360 /**
361 * Sets the OnCancelListener of the dialog shown. This method can only be
362 * called after showDialog(int) and before removeDialog(int). The method
363 * does nothing otherwise.
364 */
365 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
366 if (mDialogFragment != null) {
367 mDialogFragment.mOnCancelListener = listener;
368 }
369 }
370
371 /**
372 * Sets the OnDismissListener of the dialog shown. This method can only be
373 * called after showDialog(int) and before removeDialog(int). The method
374 * does nothing otherwise.
375 */
376 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
377 if (mDialogFragment != null) {
378 mDialogFragment.mOnDismissListener = listener;
379 }
380 }
381
Amith Yamasanic861cf82012-10-02 14:51:46 -0700382 public void onDialogShowing() {
383 // override in subclass to attach a dismiss listener, for instance
384 }
385
Amith Yamasani43c69782010-12-01 09:04:36 -0800386 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800387 private static final String KEY_DIALOG_ID = "key_dialog_id";
388 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
389
Amith Yamasanid7993472010-08-18 13:59:28 -0700390 private int mDialogId;
391
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800392 private Fragment mParentFragment;
393
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800394 private DialogInterface.OnCancelListener mOnCancelListener;
395 private DialogInterface.OnDismissListener mOnDismissListener;
396
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800397 public SettingsDialogFragment() {
398 /* do nothing */
399 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700400
Amith Yamasani43c69782010-12-01 09:04:36 -0800401 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700402 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800403 if (!(fragment instanceof Fragment)) {
404 throw new IllegalArgumentException("fragment argument must be an instance of "
405 + Fragment.class.getName());
406 }
407 mParentFragment = (Fragment) fragment;
408 }
409
410 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800411 public void onSaveInstanceState(Bundle outState) {
412 super.onSaveInstanceState(outState);
413 if (mParentFragment != null) {
414 outState.putInt(KEY_DIALOG_ID, mDialogId);
415 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
416 }
417 }
418
419 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700420 public void onStart() {
421 super.onStart();
422
423 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
424 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
425 }
426 }
427
428 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800429 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800430 if (savedInstanceState != null) {
431 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800432 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800433 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700434 if (mParentFragment == null) {
435 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
436 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800437 if (!(mParentFragment instanceof DialogCreatable)) {
438 throw new IllegalArgumentException(
439 (mParentFragment != null
440 ? mParentFragment.getClass().getName()
441 : mParentFragmentId)
442 + " must implement "
443 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800444 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800445 // This dialog fragment could be created from non-SettingsPreferenceFragment
446 if (mParentFragment instanceof SettingsPreferenceFragment) {
447 // restore mDialogFragment in mParentFragment
448 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
449 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800450 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800451 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700452 }
453
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800454 @Override
455 public void onCancel(DialogInterface dialog) {
456 super.onCancel(dialog);
457 if (mOnCancelListener != null) {
458 mOnCancelListener.onCancel(dialog);
459 }
460 }
461
462 @Override
463 public void onDismiss(DialogInterface dialog) {
464 super.onDismiss(dialog);
465 if (mOnDismissListener != null) {
466 mOnDismissListener.onDismiss(dialog);
467 }
468 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800469
Amith Yamasanid7993472010-08-18 13:59:28 -0700470 public int getDialogId() {
471 return mDialogId;
472 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800473
474 @Override
475 public void onDetach() {
476 super.onDetach();
477
Amith Yamasani8875ede2011-01-31 12:46:57 -0800478 // This dialog fragment could be created from non-SettingsPreferenceFragment
479 if (mParentFragment instanceof SettingsPreferenceFragment) {
480 // in case the dialog is not explicitly removed by removeDialog()
481 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
482 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
483 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800484 }
485 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700486 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700487
488 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800489 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700490 }
491
492 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800493 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700494 }
495
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700496 public void finish() {
497 getActivity().onBackPressed();
498 }
499
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700500 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
501 int requestCode, Bundle extras) {
502 final Activity activity = getActivity();
503 if (activity instanceof SettingsActivity) {
504 SettingsActivity sa = (SettingsActivity) activity;
505 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
506 return true;
507 } else if (activity instanceof PreferenceActivity) {
508 PreferenceActivity sa = (PreferenceActivity) activity;
509 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700510 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700511 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700512 Log.w(TAG,
513 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
514 + "launch the given Fragment (name: " + fragmentClass
515 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700516 return false;
517 }
518 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700519}