blob: baf04d4f195cc2781f9d9a2b65980447c7c7a4a8 [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
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700128 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700129 public void onSaveInstanceState(Bundle outState) {
130 super.onSaveInstanceState(outState);
131
132 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
133 }
134
135 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700136 public void onActivityCreated(Bundle savedInstanceState) {
137 super.onActivityCreated(savedInstanceState);
Jason Monk23acc2b2015-04-14 15:06:39 -0400138 if (!TextUtils.isEmpty(mHelpUri)) {
Amith Yamasanib3a593e2012-04-23 18:03:52 -0700139 setHasOptionsMenu(true);
140 }
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700141 }
142
143 @Override
144 public void onResume() {
145 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700146
147 final Bundle args = getArguments();
148 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700149 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
150 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700151 }
152 }
153
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700154 @Override
155 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700156 registerObserverIfNeeded();
157 }
158
159 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700160 protected void onUnbindPreferences() {
161 unregisterObserverIfNeeded();
162 }
163
164 @Override
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700165 public void onStop() {
166 super.onStop();
167
168 unregisterObserverIfNeeded();
169 }
170
Jason Monkb5aa73f2015-03-31 12:59:33 -0400171 public void showLoadingWhenEmpty() {
172 View loading = getView().findViewById(R.id.loading_container);
173 getListView().setEmptyView(loading);
174 }
175
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700176 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700177 if (!mIsDataSetObserverRegistered) {
178 if (mCurrentRootAdapter != null) {
179 mCurrentRootAdapter.unregisterDataSetObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700180 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700181 mCurrentRootAdapter = getPreferenceScreen().getRootAdapter();
182 mCurrentRootAdapter.registerDataSetObserver(mDataSetObserver);
183 mIsDataSetObserverRegistered = true;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700184 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700185 }
186
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700187 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700188 if (mIsDataSetObserverRegistered) {
189 if (mCurrentRootAdapter != null) {
190 mCurrentRootAdapter.unregisterDataSetObserver(mDataSetObserver);
191 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700192 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700193 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700194 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700195 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700196
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700197 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700198 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700199 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700200 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700201 }
202
203 private Drawable getHighlightDrawable() {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700204 if (mHighlightDrawable == null) {
205 mHighlightDrawable = getActivity().getDrawable(R.drawable.preference_highlight);
206 }
207 return mHighlightDrawable;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700208 }
209
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700210 /**
211 * Return a valid ListView position or -1 if none is found
212 */
213 private int canUseListViewForHighLighting(String key) {
214 if (!hasListView()) {
215 return -1;
216 }
217
218 ListView listView = getListView();
219 ListAdapter adapter = listView.getAdapter();
220
221 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
222 return findListPositionFromKey(adapter, key);
223 }
224
225 return -1;
226 }
227
228 private void highlightPreference(String key) {
229 final Drawable highlight = getHighlightDrawable();
230
231 final int position = canUseListViewForHighLighting(key);
232 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700233 mPreferenceHighlighted = true;
234
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700235 final ListView listView = getListView();
236 final ListAdapter adapter = listView.getAdapter();
237
238 ((PreferenceGroupAdapter) adapter).setHighlightedDrawable(highlight);
239 ((PreferenceGroupAdapter) adapter).setHighlighted(position);
240
241 listView.post(new Runnable() {
242 @Override
243 public void run() {
244 listView.setSelection(position);
245 listView.postDelayed(new Runnable() {
246 @Override
247 public void run() {
Alan Viverette2fed4d42014-09-08 12:40:59 -0700248 final int index = position - listView.getFirstVisiblePosition();
249 if (index >= 0 && index < listView.getChildCount()) {
250 final View v = listView.getChildAt(index);
251 final int centerX = v.getWidth() / 2;
252 final int centerY = v.getHeight() / 2;
253 highlight.setHotspot(centerX, centerY);
254 v.setPressed(true);
255 v.setPressed(false);
256 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700257 }
258 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700259 }
260 });
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700261 }
262 }
263
264 private int findListPositionFromKey(ListAdapter adapter, String key) {
265 final int count = adapter.getCount();
266 for (int n = 0; n < count; n++) {
267 final Object item = adapter.getItem(n);
268 if (item instanceof Preference) {
269 Preference preference = (Preference) item;
270 final String preferenceKey = preference.getKey();
271 if (preferenceKey != null && preferenceKey.equals(key)) {
272 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700273 }
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700274 }
275 }
276 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700277 }
278
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700279 protected void removePreference(String key) {
280 Preference pref = findPreference(key);
281 if (pref != null) {
282 getPreferenceScreen().removePreference(pref);
283 }
284 }
285
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700286 /**
287 * Override this if you want to show a help item in the menu, by returning the resource id.
288 * @return the resource id for the help url
289 */
290 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400291 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700292 }
293
294 @Override
295 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400296 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400297 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700298 }
299 }
300
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700301 /*
302 * The name is intentionally made different from Activity#finish(), so that
303 * users won't misunderstand its meaning.
304 */
305 public final void finishFragment() {
306 getActivity().onBackPressed();
307 }
308
Amith Yamasanid7993472010-08-18 13:59:28 -0700309 // Some helpers for functions used by the settings fragments when they were activities
310
311 /**
312 * Returns the ContentResolver from the owning Activity.
313 */
314 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700315 Context context = getActivity();
316 if (context != null) {
317 mContentResolver = context.getContentResolver();
318 }
319 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700320 }
321
322 /**
323 * Returns the specified system service from the owning Activity.
324 */
325 protected Object getSystemService(final String name) {
326 return getActivity().getSystemService(name);
327 }
328
329 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700330 * Returns the PackageManager from the owning Activity.
331 */
332 protected PackageManager getPackageManager() {
333 return getActivity().getPackageManager();
334 }
335
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800336 @Override
337 public void onDetach() {
338 if (isRemoving()) {
339 if (mDialogFragment != null) {
340 mDialogFragment.dismiss();
341 mDialogFragment = null;
342 }
343 }
344 super.onDetach();
345 }
346
Amith Yamasanid7993472010-08-18 13:59:28 -0700347 // Dialog management
348
349 protected void showDialog(int dialogId) {
350 if (mDialogFragment != null) {
351 Log.e(TAG, "Old dialog fragment not null!");
352 }
353 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800354 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700355 }
356
357 public Dialog onCreateDialog(int dialogId) {
358 return null;
359 }
360
361 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800362 // mDialogFragment may not be visible yet in parent fragment's onResume().
363 // To be able to dismiss dialog at that time, don't check
364 // mDialogFragment.isVisible().
365 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700366 mDialogFragment.dismiss();
367 }
368 mDialogFragment = null;
369 }
370
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800371 /**
372 * Sets the OnCancelListener 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 setOnCancelListener(DialogInterface.OnCancelListener listener) {
377 if (mDialogFragment != null) {
378 mDialogFragment.mOnCancelListener = listener;
379 }
380 }
381
382 /**
383 * Sets the OnDismissListener of the dialog shown. This method can only be
384 * called after showDialog(int) and before removeDialog(int). The method
385 * does nothing otherwise.
386 */
387 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
388 if (mDialogFragment != null) {
389 mDialogFragment.mOnDismissListener = listener;
390 }
391 }
392
Amith Yamasanic861cf82012-10-02 14:51:46 -0700393 public void onDialogShowing() {
394 // override in subclass to attach a dismiss listener, for instance
395 }
396
Amith Yamasani43c69782010-12-01 09:04:36 -0800397 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800398 private static final String KEY_DIALOG_ID = "key_dialog_id";
399 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
400
Amith Yamasanid7993472010-08-18 13:59:28 -0700401 private int mDialogId;
402
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800403 private Fragment mParentFragment;
404
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800405 private DialogInterface.OnCancelListener mOnCancelListener;
406 private DialogInterface.OnDismissListener mOnDismissListener;
407
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800408 public SettingsDialogFragment() {
409 /* do nothing */
410 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700411
Amith Yamasani43c69782010-12-01 09:04:36 -0800412 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700413 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800414 if (!(fragment instanceof Fragment)) {
415 throw new IllegalArgumentException("fragment argument must be an instance of "
416 + Fragment.class.getName());
417 }
418 mParentFragment = (Fragment) fragment;
419 }
420
421 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800422 public void onSaveInstanceState(Bundle outState) {
423 super.onSaveInstanceState(outState);
424 if (mParentFragment != null) {
425 outState.putInt(KEY_DIALOG_ID, mDialogId);
426 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
427 }
428 }
429
430 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700431 public void onStart() {
432 super.onStart();
433
434 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
435 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
436 }
437 }
438
439 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800440 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800441 if (savedInstanceState != null) {
442 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800443 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800444 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700445 if (mParentFragment == null) {
446 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
447 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800448 if (!(mParentFragment instanceof DialogCreatable)) {
449 throw new IllegalArgumentException(
450 (mParentFragment != null
451 ? mParentFragment.getClass().getName()
452 : mParentFragmentId)
453 + " must implement "
454 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800455 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800456 // This dialog fragment could be created from non-SettingsPreferenceFragment
457 if (mParentFragment instanceof SettingsPreferenceFragment) {
458 // restore mDialogFragment in mParentFragment
459 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
460 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800461 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800462 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700463 }
464
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800465 @Override
466 public void onCancel(DialogInterface dialog) {
467 super.onCancel(dialog);
468 if (mOnCancelListener != null) {
469 mOnCancelListener.onCancel(dialog);
470 }
471 }
472
473 @Override
474 public void onDismiss(DialogInterface dialog) {
475 super.onDismiss(dialog);
476 if (mOnDismissListener != null) {
477 mOnDismissListener.onDismiss(dialog);
478 }
479 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800480
Amith Yamasanid7993472010-08-18 13:59:28 -0700481 public int getDialogId() {
482 return mDialogId;
483 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800484
485 @Override
486 public void onDetach() {
487 super.onDetach();
488
Amith Yamasani8875ede2011-01-31 12:46:57 -0800489 // This dialog fragment could be created from non-SettingsPreferenceFragment
490 if (mParentFragment instanceof SettingsPreferenceFragment) {
491 // in case the dialog is not explicitly removed by removeDialog()
492 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
493 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
494 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800495 }
496 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700497 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700498
499 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800500 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700501 }
502
503 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800504 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700505 }
506
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700507 public void finish() {
508 getActivity().onBackPressed();
509 }
510
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700511 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
512 int requestCode, Bundle extras) {
513 final Activity activity = getActivity();
514 if (activity instanceof SettingsActivity) {
515 SettingsActivity sa = (SettingsActivity) activity;
516 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
517 return true;
518 } else if (activity instanceof PreferenceActivity) {
519 PreferenceActivity sa = (PreferenceActivity) activity;
520 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700521 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700522 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700523 Log.w(TAG,
524 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
525 + "launch the given Fragment (name: " + fragmentClass
526 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700527 return false;
528 }
529 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700530}