blob: 2c566a1115557aef2f7bc6cbbfd8d00eddac755a [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;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070086
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070087 @Override
88 public void onCreate(Bundle icicle) {
89 super.onCreate(icicle);
90
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070091 if (icicle != null) {
92 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
93 }
94
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070095 // Prepare help url and enable menu if necessary
96 int helpResource = getHelpResource();
97 if (helpResource != 0) {
Jason Monk23acc2b2015-04-14 15:06:39 -040098 mHelpUri = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070099 }
100 }
101
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700102 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700103 public View onCreateView(LayoutInflater inflater, ViewGroup container,
104 Bundle savedInstanceState) {
105 final View root = super.onCreateView(inflater, container, savedInstanceState);
106 mPinnedHeaderFrameLayout = (ViewGroup) root.findViewById(R.id.pinned_header);
John Spurlockb8e02b82015-04-15 21:15:55 -0400107 mFloatingActionButton = (FloatingActionButton) root.findViewById(R.id.fab);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700108 return root;
109 }
110
John Spurlockb8e02b82015-04-15 21:15:55 -0400111 public FloatingActionButton getFloatingActionButton() {
112 return mFloatingActionButton;
113 }
114
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700115 public View setPinnedHeaderView(int layoutResId) {
116 final LayoutInflater inflater = getActivity().getLayoutInflater();
117 final View pinnedHeader =
118 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
119 setPinnedHeaderView(pinnedHeader);
120 return pinnedHeader;
121 }
122
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700123 public void setPinnedHeaderView(View pinnedHeader) {
124 mPinnedHeaderFrameLayout.addView(pinnedHeader);
125 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
126 }
127
128 public void clearPinnedHeaderView() {
129 mPinnedHeaderFrameLayout.removeAllViews();
130 mPinnedHeaderFrameLayout.setVisibility(View.GONE);
131 }
132
133 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700134 public void onSaveInstanceState(Bundle outState) {
135 super.onSaveInstanceState(outState);
136
137 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
138 }
139
140 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700141 public void onActivityCreated(Bundle savedInstanceState) {
142 super.onActivityCreated(savedInstanceState);
Jason Monk23acc2b2015-04-14 15:06:39 -0400143 if (!TextUtils.isEmpty(mHelpUri)) {
Amith Yamasanib3a593e2012-04-23 18:03:52 -0700144 setHasOptionsMenu(true);
145 }
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700146 }
147
148 @Override
149 public void onResume() {
150 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700151
152 final Bundle args = getArguments();
153 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700154 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
155 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700156 }
157 }
158
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700159 @Override
160 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700161 registerObserverIfNeeded();
162 }
163
164 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700165 protected void onUnbindPreferences() {
166 unregisterObserverIfNeeded();
167 }
168
169 @Override
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700170 public void onStop() {
171 super.onStop();
172
173 unregisterObserverIfNeeded();
174 }
175
Jason Monkb5aa73f2015-03-31 12:59:33 -0400176 public void showLoadingWhenEmpty() {
177 View loading = getView().findViewById(R.id.loading_container);
178 getListView().setEmptyView(loading);
179 }
180
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700181 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700182 if (!mIsDataSetObserverRegistered) {
183 if (mCurrentRootAdapter != null) {
184 mCurrentRootAdapter.unregisterDataSetObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700185 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700186 mCurrentRootAdapter = getPreferenceScreen().getRootAdapter();
187 mCurrentRootAdapter.registerDataSetObserver(mDataSetObserver);
188 mIsDataSetObserverRegistered = true;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700189 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700190 }
191
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700192 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700193 if (mIsDataSetObserverRegistered) {
194 if (mCurrentRootAdapter != null) {
195 mCurrentRootAdapter.unregisterDataSetObserver(mDataSetObserver);
196 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700197 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700198 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700199 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700200 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700201
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700202 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700203 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700204 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700205 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700206 }
207
208 private Drawable getHighlightDrawable() {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700209 if (mHighlightDrawable == null) {
210 mHighlightDrawable = getActivity().getDrawable(R.drawable.preference_highlight);
211 }
212 return mHighlightDrawable;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700213 }
214
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700215 /**
216 * Return a valid ListView position or -1 if none is found
217 */
218 private int canUseListViewForHighLighting(String key) {
219 if (!hasListView()) {
220 return -1;
221 }
222
223 ListView listView = getListView();
224 ListAdapter adapter = listView.getAdapter();
225
226 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
227 return findListPositionFromKey(adapter, key);
228 }
229
230 return -1;
231 }
232
233 private void highlightPreference(String key) {
234 final Drawable highlight = getHighlightDrawable();
235
236 final int position = canUseListViewForHighLighting(key);
237 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700238 mPreferenceHighlighted = true;
239
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700240 final ListView listView = getListView();
241 final ListAdapter adapter = listView.getAdapter();
242
243 ((PreferenceGroupAdapter) adapter).setHighlightedDrawable(highlight);
244 ((PreferenceGroupAdapter) adapter).setHighlighted(position);
245
246 listView.post(new Runnable() {
247 @Override
248 public void run() {
249 listView.setSelection(position);
250 listView.postDelayed(new Runnable() {
251 @Override
252 public void run() {
Alan Viverette2fed4d42014-09-08 12:40:59 -0700253 final int index = position - listView.getFirstVisiblePosition();
254 if (index >= 0 && index < listView.getChildCount()) {
255 final View v = listView.getChildAt(index);
256 final int centerX = v.getWidth() / 2;
257 final int centerY = v.getHeight() / 2;
258 highlight.setHotspot(centerX, centerY);
259 v.setPressed(true);
260 v.setPressed(false);
261 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700262 }
263 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700264 }
265 });
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700266 }
267 }
268
269 private int findListPositionFromKey(ListAdapter adapter, String key) {
270 final int count = adapter.getCount();
271 for (int n = 0; n < count; n++) {
272 final Object item = adapter.getItem(n);
273 if (item instanceof Preference) {
274 Preference preference = (Preference) item;
275 final String preferenceKey = preference.getKey();
276 if (preferenceKey != null && preferenceKey.equals(key)) {
277 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700278 }
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700279 }
280 }
281 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700282 }
283
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700284 protected void removePreference(String key) {
285 Preference pref = findPreference(key);
286 if (pref != null) {
287 getPreferenceScreen().removePreference(pref);
288 }
289 }
290
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700291 /**
292 * Override this if you want to show a help item in the menu, by returning the resource id.
293 * @return the resource id for the help url
294 */
295 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400296 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700297 }
298
299 @Override
300 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400301 if (mHelpUri != null && getActivity() != null) {
302 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700303 }
304 }
305
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700306 /*
307 * The name is intentionally made different from Activity#finish(), so that
308 * users won't misunderstand its meaning.
309 */
310 public final void finishFragment() {
311 getActivity().onBackPressed();
312 }
313
Amith Yamasanid7993472010-08-18 13:59:28 -0700314 // Some helpers for functions used by the settings fragments when they were activities
315
316 /**
317 * Returns the ContentResolver from the owning Activity.
318 */
319 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700320 Context context = getActivity();
321 if (context != null) {
322 mContentResolver = context.getContentResolver();
323 }
324 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700325 }
326
327 /**
328 * Returns the specified system service from the owning Activity.
329 */
330 protected Object getSystemService(final String name) {
331 return getActivity().getSystemService(name);
332 }
333
334 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700335 * Returns the PackageManager from the owning Activity.
336 */
337 protected PackageManager getPackageManager() {
338 return getActivity().getPackageManager();
339 }
340
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800341 @Override
342 public void onDetach() {
343 if (isRemoving()) {
344 if (mDialogFragment != null) {
345 mDialogFragment.dismiss();
346 mDialogFragment = null;
347 }
348 }
349 super.onDetach();
350 }
351
Amith Yamasanid7993472010-08-18 13:59:28 -0700352 // Dialog management
353
354 protected void showDialog(int dialogId) {
355 if (mDialogFragment != null) {
356 Log.e(TAG, "Old dialog fragment not null!");
357 }
358 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800359 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700360 }
361
362 public Dialog onCreateDialog(int dialogId) {
363 return null;
364 }
365
366 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800367 // mDialogFragment may not be visible yet in parent fragment's onResume().
368 // To be able to dismiss dialog at that time, don't check
369 // mDialogFragment.isVisible().
370 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700371 mDialogFragment.dismiss();
372 }
373 mDialogFragment = null;
374 }
375
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800376 /**
377 * Sets the OnCancelListener of the dialog shown. This method can only be
378 * called after showDialog(int) and before removeDialog(int). The method
379 * does nothing otherwise.
380 */
381 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
382 if (mDialogFragment != null) {
383 mDialogFragment.mOnCancelListener = listener;
384 }
385 }
386
387 /**
388 * Sets the OnDismissListener of the dialog shown. This method can only be
389 * called after showDialog(int) and before removeDialog(int). The method
390 * does nothing otherwise.
391 */
392 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
393 if (mDialogFragment != null) {
394 mDialogFragment.mOnDismissListener = listener;
395 }
396 }
397
Amith Yamasanic861cf82012-10-02 14:51:46 -0700398 public void onDialogShowing() {
399 // override in subclass to attach a dismiss listener, for instance
400 }
401
Amith Yamasani43c69782010-12-01 09:04:36 -0800402 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800403 private static final String KEY_DIALOG_ID = "key_dialog_id";
404 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
405
Amith Yamasanid7993472010-08-18 13:59:28 -0700406 private int mDialogId;
407
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800408 private Fragment mParentFragment;
409
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800410 private DialogInterface.OnCancelListener mOnCancelListener;
411 private DialogInterface.OnDismissListener mOnDismissListener;
412
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800413 public SettingsDialogFragment() {
414 /* do nothing */
415 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700416
Amith Yamasani43c69782010-12-01 09:04:36 -0800417 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700418 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800419 if (!(fragment instanceof Fragment)) {
420 throw new IllegalArgumentException("fragment argument must be an instance of "
421 + Fragment.class.getName());
422 }
423 mParentFragment = (Fragment) fragment;
424 }
425
426 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800427 public void onSaveInstanceState(Bundle outState) {
428 super.onSaveInstanceState(outState);
429 if (mParentFragment != null) {
430 outState.putInt(KEY_DIALOG_ID, mDialogId);
431 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
432 }
433 }
434
435 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700436 public void onStart() {
437 super.onStart();
438
439 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
440 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
441 }
442 }
443
444 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800445 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800446 if (savedInstanceState != null) {
447 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800448 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800449 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700450 if (mParentFragment == null) {
451 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
452 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800453 if (!(mParentFragment instanceof DialogCreatable)) {
454 throw new IllegalArgumentException(
455 (mParentFragment != null
456 ? mParentFragment.getClass().getName()
457 : mParentFragmentId)
458 + " must implement "
459 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800460 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800461 // This dialog fragment could be created from non-SettingsPreferenceFragment
462 if (mParentFragment instanceof SettingsPreferenceFragment) {
463 // restore mDialogFragment in mParentFragment
464 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
465 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800466 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800467 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700468 }
469
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800470 @Override
471 public void onCancel(DialogInterface dialog) {
472 super.onCancel(dialog);
473 if (mOnCancelListener != null) {
474 mOnCancelListener.onCancel(dialog);
475 }
476 }
477
478 @Override
479 public void onDismiss(DialogInterface dialog) {
480 super.onDismiss(dialog);
481 if (mOnDismissListener != null) {
482 mOnDismissListener.onDismiss(dialog);
483 }
484 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800485
Amith Yamasanid7993472010-08-18 13:59:28 -0700486 public int getDialogId() {
487 return mDialogId;
488 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800489
490 @Override
491 public void onDetach() {
492 super.onDetach();
493
Amith Yamasani8875ede2011-01-31 12:46:57 -0800494 // This dialog fragment could be created from non-SettingsPreferenceFragment
495 if (mParentFragment instanceof SettingsPreferenceFragment) {
496 // in case the dialog is not explicitly removed by removeDialog()
497 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
498 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
499 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800500 }
501 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700502 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700503
504 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800505 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700506 }
507
508 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800509 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700510 }
511
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700512 public void finish() {
513 getActivity().onBackPressed();
514 }
515
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700516 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
517 int requestCode, Bundle extras) {
518 final Activity activity = getActivity();
519 if (activity instanceof SettingsActivity) {
520 SettingsActivity sa = (SettingsActivity) activity;
521 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
522 return true;
523 } else if (activity instanceof PreferenceActivity) {
524 PreferenceActivity sa = (PreferenceActivity) activity;
525 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700526 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700527 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700528 Log.w(TAG,
529 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
530 + "launch the given Fragment (name: " + fragmentClass
531 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700532 return false;
533 }
534 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700535}