blob: 17ff4b28c6dae25f6731394bf65caa67c2de0255 [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
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700115 public void setPinnedHeaderView(View pinnedHeader) {
116 mPinnedHeaderFrameLayout.addView(pinnedHeader);
117 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
118 }
119
120 public void clearPinnedHeaderView() {
121 mPinnedHeaderFrameLayout.removeAllViews();
122 mPinnedHeaderFrameLayout.setVisibility(View.GONE);
123 }
124
125 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700126 public void onSaveInstanceState(Bundle outState) {
127 super.onSaveInstanceState(outState);
128
129 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
130 }
131
132 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700133 public void onActivityCreated(Bundle savedInstanceState) {
134 super.onActivityCreated(savedInstanceState);
Jason Monk23acc2b2015-04-14 15:06:39 -0400135 if (!TextUtils.isEmpty(mHelpUri)) {
Amith Yamasanib3a593e2012-04-23 18:03:52 -0700136 setHasOptionsMenu(true);
137 }
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700138 }
139
140 @Override
141 public void onResume() {
142 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700143
144 final Bundle args = getArguments();
145 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700146 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
147 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700148 }
149 }
150
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700151 @Override
152 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700153 registerObserverIfNeeded();
154 }
155
156 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700157 protected void onUnbindPreferences() {
158 unregisterObserverIfNeeded();
159 }
160
161 @Override
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700162 public void onStop() {
163 super.onStop();
164
165 unregisterObserverIfNeeded();
166 }
167
Jason Monkb5aa73f2015-03-31 12:59:33 -0400168 public void showLoadingWhenEmpty() {
169 View loading = getView().findViewById(R.id.loading_container);
170 getListView().setEmptyView(loading);
171 }
172
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700173 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700174 if (!mIsDataSetObserverRegistered) {
175 if (mCurrentRootAdapter != null) {
176 mCurrentRootAdapter.unregisterDataSetObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700177 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700178 mCurrentRootAdapter = getPreferenceScreen().getRootAdapter();
179 mCurrentRootAdapter.registerDataSetObserver(mDataSetObserver);
180 mIsDataSetObserverRegistered = true;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700181 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700182 }
183
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700184 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700185 if (mIsDataSetObserverRegistered) {
186 if (mCurrentRootAdapter != null) {
187 mCurrentRootAdapter.unregisterDataSetObserver(mDataSetObserver);
188 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700189 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700190 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700191 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700192 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700193
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700194 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700195 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700196 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700197 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700198 }
199
200 private Drawable getHighlightDrawable() {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700201 if (mHighlightDrawable == null) {
202 mHighlightDrawable = getActivity().getDrawable(R.drawable.preference_highlight);
203 }
204 return mHighlightDrawable;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700205 }
206
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700207 /**
208 * Return a valid ListView position or -1 if none is found
209 */
210 private int canUseListViewForHighLighting(String key) {
211 if (!hasListView()) {
212 return -1;
213 }
214
215 ListView listView = getListView();
216 ListAdapter adapter = listView.getAdapter();
217
218 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
219 return findListPositionFromKey(adapter, key);
220 }
221
222 return -1;
223 }
224
225 private void highlightPreference(String key) {
226 final Drawable highlight = getHighlightDrawable();
227
228 final int position = canUseListViewForHighLighting(key);
229 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700230 mPreferenceHighlighted = true;
231
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700232 final ListView listView = getListView();
233 final ListAdapter adapter = listView.getAdapter();
234
235 ((PreferenceGroupAdapter) adapter).setHighlightedDrawable(highlight);
236 ((PreferenceGroupAdapter) adapter).setHighlighted(position);
237
238 listView.post(new Runnable() {
239 @Override
240 public void run() {
241 listView.setSelection(position);
242 listView.postDelayed(new Runnable() {
243 @Override
244 public void run() {
Alan Viverette2fed4d42014-09-08 12:40:59 -0700245 final int index = position - listView.getFirstVisiblePosition();
246 if (index >= 0 && index < listView.getChildCount()) {
247 final View v = listView.getChildAt(index);
248 final int centerX = v.getWidth() / 2;
249 final int centerY = v.getHeight() / 2;
250 highlight.setHotspot(centerX, centerY);
251 v.setPressed(true);
252 v.setPressed(false);
253 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700254 }
255 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700256 }
257 });
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700258 }
259 }
260
261 private int findListPositionFromKey(ListAdapter adapter, String key) {
262 final int count = adapter.getCount();
263 for (int n = 0; n < count; n++) {
264 final Object item = adapter.getItem(n);
265 if (item instanceof Preference) {
266 Preference preference = (Preference) item;
267 final String preferenceKey = preference.getKey();
268 if (preferenceKey != null && preferenceKey.equals(key)) {
269 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700270 }
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700271 }
272 }
273 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700274 }
275
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700276 protected void removePreference(String key) {
277 Preference pref = findPreference(key);
278 if (pref != null) {
279 getPreferenceScreen().removePreference(pref);
280 }
281 }
282
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700283 /**
284 * Override this if you want to show a help item in the menu, by returning the resource id.
285 * @return the resource id for the help url
286 */
287 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400288 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700289 }
290
291 @Override
292 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400293 if (mHelpUri != null && getActivity() != null) {
294 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700295 }
296 }
297
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700298 /*
299 * The name is intentionally made different from Activity#finish(), so that
300 * users won't misunderstand its meaning.
301 */
302 public final void finishFragment() {
303 getActivity().onBackPressed();
304 }
305
Amith Yamasanid7993472010-08-18 13:59:28 -0700306 // Some helpers for functions used by the settings fragments when they were activities
307
308 /**
309 * Returns the ContentResolver from the owning Activity.
310 */
311 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700312 Context context = getActivity();
313 if (context != null) {
314 mContentResolver = context.getContentResolver();
315 }
316 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700317 }
318
319 /**
320 * Returns the specified system service from the owning Activity.
321 */
322 protected Object getSystemService(final String name) {
323 return getActivity().getSystemService(name);
324 }
325
326 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700327 * Returns the PackageManager from the owning Activity.
328 */
329 protected PackageManager getPackageManager() {
330 return getActivity().getPackageManager();
331 }
332
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800333 @Override
334 public void onDetach() {
335 if (isRemoving()) {
336 if (mDialogFragment != null) {
337 mDialogFragment.dismiss();
338 mDialogFragment = null;
339 }
340 }
341 super.onDetach();
342 }
343
Amith Yamasanid7993472010-08-18 13:59:28 -0700344 // Dialog management
345
346 protected void showDialog(int dialogId) {
347 if (mDialogFragment != null) {
348 Log.e(TAG, "Old dialog fragment not null!");
349 }
350 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800351 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700352 }
353
354 public Dialog onCreateDialog(int dialogId) {
355 return null;
356 }
357
358 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800359 // mDialogFragment may not be visible yet in parent fragment's onResume().
360 // To be able to dismiss dialog at that time, don't check
361 // mDialogFragment.isVisible().
362 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700363 mDialogFragment.dismiss();
364 }
365 mDialogFragment = null;
366 }
367
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800368 /**
369 * Sets the OnCancelListener 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 setOnCancelListener(DialogInterface.OnCancelListener listener) {
374 if (mDialogFragment != null) {
375 mDialogFragment.mOnCancelListener = listener;
376 }
377 }
378
379 /**
380 * Sets the OnDismissListener of the dialog shown. This method can only be
381 * called after showDialog(int) and before removeDialog(int). The method
382 * does nothing otherwise.
383 */
384 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
385 if (mDialogFragment != null) {
386 mDialogFragment.mOnDismissListener = listener;
387 }
388 }
389
Amith Yamasanic861cf82012-10-02 14:51:46 -0700390 public void onDialogShowing() {
391 // override in subclass to attach a dismiss listener, for instance
392 }
393
Amith Yamasani43c69782010-12-01 09:04:36 -0800394 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800395 private static final String KEY_DIALOG_ID = "key_dialog_id";
396 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
397
Amith Yamasanid7993472010-08-18 13:59:28 -0700398 private int mDialogId;
399
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800400 private Fragment mParentFragment;
401
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800402 private DialogInterface.OnCancelListener mOnCancelListener;
403 private DialogInterface.OnDismissListener mOnDismissListener;
404
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800405 public SettingsDialogFragment() {
406 /* do nothing */
407 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700408
Amith Yamasani43c69782010-12-01 09:04:36 -0800409 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700410 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800411 if (!(fragment instanceof Fragment)) {
412 throw new IllegalArgumentException("fragment argument must be an instance of "
413 + Fragment.class.getName());
414 }
415 mParentFragment = (Fragment) fragment;
416 }
417
418 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800419 public void onSaveInstanceState(Bundle outState) {
420 super.onSaveInstanceState(outState);
421 if (mParentFragment != null) {
422 outState.putInt(KEY_DIALOG_ID, mDialogId);
423 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
424 }
425 }
426
427 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700428 public void onStart() {
429 super.onStart();
430
431 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
432 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
433 }
434 }
435
436 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800437 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800438 if (savedInstanceState != null) {
439 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800440 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800441 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700442 if (mParentFragment == null) {
443 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
444 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800445 if (!(mParentFragment instanceof DialogCreatable)) {
446 throw new IllegalArgumentException(
447 (mParentFragment != null
448 ? mParentFragment.getClass().getName()
449 : mParentFragmentId)
450 + " must implement "
451 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800452 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800453 // This dialog fragment could be created from non-SettingsPreferenceFragment
454 if (mParentFragment instanceof SettingsPreferenceFragment) {
455 // restore mDialogFragment in mParentFragment
456 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
457 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800458 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800459 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700460 }
461
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800462 @Override
463 public void onCancel(DialogInterface dialog) {
464 super.onCancel(dialog);
465 if (mOnCancelListener != null) {
466 mOnCancelListener.onCancel(dialog);
467 }
468 }
469
470 @Override
471 public void onDismiss(DialogInterface dialog) {
472 super.onDismiss(dialog);
473 if (mOnDismissListener != null) {
474 mOnDismissListener.onDismiss(dialog);
475 }
476 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800477
Amith Yamasanid7993472010-08-18 13:59:28 -0700478 public int getDialogId() {
479 return mDialogId;
480 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800481
482 @Override
483 public void onDetach() {
484 super.onDetach();
485
Amith Yamasani8875ede2011-01-31 12:46:57 -0800486 // This dialog fragment could be created from non-SettingsPreferenceFragment
487 if (mParentFragment instanceof SettingsPreferenceFragment) {
488 // in case the dialog is not explicitly removed by removeDialog()
489 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
490 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
491 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800492 }
493 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700494 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700495
496 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800497 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700498 }
499
500 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800501 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700502 }
503
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700504 public void finish() {
505 getActivity().onBackPressed();
506 }
507
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700508 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
509 int requestCode, Bundle extras) {
510 final Activity activity = getActivity();
511 if (activity instanceof SettingsActivity) {
512 SettingsActivity sa = (SettingsActivity) activity;
513 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
514 return true;
515 } else if (activity instanceof PreferenceActivity) {
516 PreferenceActivity sa = (PreferenceActivity) activity;
517 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700518 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700519 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700520 Log.w(TAG,
521 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
522 + "launch the given Fragment (name: " + fragmentClass
523 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700524 return false;
525 }
526 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700527}