blob: 097fb95675e03bcd12bcfa62fe2cd8917d8fcc46 [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;
Amith Yamasanid7993472010-08-18 13:59:28 -070032import android.preference.PreferenceFragment;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -070033import android.preference.PreferenceGroupAdapter;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070034import android.text.TextUtils;
Amith Yamasanid7993472010-08-18 13:59:28 -070035import android.util.Log;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070036import android.view.LayoutInflater;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070037import android.view.Menu;
38import android.view.MenuInflater;
39import android.view.MenuItem;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070040import android.view.View;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070041import android.view.ViewGroup;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070042import android.widget.Button;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -070043import android.widget.ListAdapter;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070044import android.widget.ListView;
Amith Yamasanid7993472010-08-18 13:59:28 -070045
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070046/**
Amith Yamasanid7993472010-08-18 13:59:28 -070047 * Base class for Settings fragments, with some helper functions and dialog management.
48 */
Gilles Debunne64650542011-08-23 11:01:35 -070049public class SettingsPreferenceFragment extends PreferenceFragment implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070050
51 private static final String TAG = "SettingsPreferenceFragment";
52
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070053 private static final int MENU_HELP = Menu.FIRST + 100;
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070054 private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 400;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070055
56 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070057
Amith Yamasanid7993472010-08-18 13:59:28 -070058 private SettingsDialogFragment mDialogFragment;
59
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070060 private String mHelpUrl;
61
Amith Yamasani350938e2013-04-09 10:22:47 -070062 // Cache the content resolver for async callbacks
63 private ContentResolver mContentResolver;
64
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070065 private String mPreferenceKey;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070066 private boolean mPreferenceHighlighted = false;
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -070067 private Drawable mHighlightDrawable;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070068
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) {
95 mHelpUrl = 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);
Amith Yamasanib3a593e2012-04-23 18:03:52 -0700127 if (!TextUtils.isEmpty(mHelpUrl)) {
128 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
149 public void onStop() {
150 super.onStop();
151
152 unregisterObserverIfNeeded();
153 }
154
155 public void registerObserverIfNeeded() {
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700156 if (!mIsDataSetObserverRegistered) {
157 getPreferenceScreen().getRootAdapter().registerDataSetObserver(mDataSetObserver);
158 mIsDataSetObserverRegistered = true;
159 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700160 }
161
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700162 public void unregisterObserverIfNeeded() {
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700163 if (mIsDataSetObserverRegistered) {
164 getPreferenceScreen().getRootAdapter().unregisterDataSetObserver(mDataSetObserver);
165 mIsDataSetObserverRegistered = false;
166 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700167 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700168
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700169 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700170 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700171 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700172 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700173 }
174
175 private Drawable getHighlightDrawable() {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700176 if (mHighlightDrawable == null) {
177 mHighlightDrawable = getActivity().getDrawable(R.drawable.preference_highlight);
178 }
179 return mHighlightDrawable;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700180 }
181
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700182 /**
183 * Return a valid ListView position or -1 if none is found
184 */
185 private int canUseListViewForHighLighting(String key) {
186 if (!hasListView()) {
187 return -1;
188 }
189
190 ListView listView = getListView();
191 ListAdapter adapter = listView.getAdapter();
192
193 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
194 return findListPositionFromKey(adapter, key);
195 }
196
197 return -1;
198 }
199
200 private void highlightPreference(String key) {
201 final Drawable highlight = getHighlightDrawable();
202
203 final int position = canUseListViewForHighLighting(key);
204 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700205 mPreferenceHighlighted = true;
206
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700207 final ListView listView = getListView();
208 final ListAdapter adapter = listView.getAdapter();
209
210 ((PreferenceGroupAdapter) adapter).setHighlightedDrawable(highlight);
211 ((PreferenceGroupAdapter) adapter).setHighlighted(position);
212
213 listView.post(new Runnable() {
214 @Override
215 public void run() {
216 listView.setSelection(position);
217 listView.postDelayed(new Runnable() {
218 @Override
219 public void run() {
Alan Viveretteba348ca2014-05-19 15:10:36 -0700220 final View v = listView.getChildAt(0);
221 final int centerX = v.getWidth() / 2;
222 final int centerY = v.getHeight() / 2;
223 highlight.setHotspot(centerX, centerY);
224 v.setPressed(true);
225 v.setPressed(false);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700226 }
227 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700228 }
229 });
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700230 }
231 }
232
233 private int findListPositionFromKey(ListAdapter adapter, String key) {
234 final int count = adapter.getCount();
235 for (int n = 0; n < count; n++) {
236 final Object item = adapter.getItem(n);
237 if (item instanceof Preference) {
238 Preference preference = (Preference) item;
239 final String preferenceKey = preference.getKey();
240 if (preferenceKey != null && preferenceKey.equals(key)) {
241 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700242 }
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700243 }
244 }
245 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700246 }
247
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700248 protected void removePreference(String key) {
249 Preference pref = findPreference(key);
250 if (pref != null) {
251 getPreferenceScreen().removePreference(pref);
252 }
253 }
254
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700255 /**
256 * Override this if you want to show a help item in the menu, by returning the resource id.
257 * @return the resource id for the help url
258 */
259 protected int getHelpResource() {
260 return 0;
261 }
262
263 @Override
264 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800265 if (mHelpUrl != null && getActivity() != null) {
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700266 MenuItem helpItem = menu.add(0, MENU_HELP, 0, R.string.help_label);
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800267 HelpUtils.prepareHelpMenuItem(getActivity(), helpItem, mHelpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700268 }
269 }
270
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700271 /*
272 * The name is intentionally made different from Activity#finish(), so that
273 * users won't misunderstand its meaning.
274 */
275 public final void finishFragment() {
276 getActivity().onBackPressed();
277 }
278
Amith Yamasanid7993472010-08-18 13:59:28 -0700279 // Some helpers for functions used by the settings fragments when they were activities
280
281 /**
282 * Returns the ContentResolver from the owning Activity.
283 */
284 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700285 Context context = getActivity();
286 if (context != null) {
287 mContentResolver = context.getContentResolver();
288 }
289 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700290 }
291
292 /**
293 * Returns the specified system service from the owning Activity.
294 */
295 protected Object getSystemService(final String name) {
296 return getActivity().getSystemService(name);
297 }
298
299 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700300 * Returns the PackageManager from the owning Activity.
301 */
302 protected PackageManager getPackageManager() {
303 return getActivity().getPackageManager();
304 }
305
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800306 @Override
307 public void onDetach() {
308 if (isRemoving()) {
309 if (mDialogFragment != null) {
310 mDialogFragment.dismiss();
311 mDialogFragment = null;
312 }
313 }
314 super.onDetach();
315 }
316
Amith Yamasanid7993472010-08-18 13:59:28 -0700317 // Dialog management
318
319 protected void showDialog(int dialogId) {
320 if (mDialogFragment != null) {
321 Log.e(TAG, "Old dialog fragment not null!");
322 }
323 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800324 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700325 }
326
327 public Dialog onCreateDialog(int dialogId) {
328 return null;
329 }
330
331 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800332 // mDialogFragment may not be visible yet in parent fragment's onResume().
333 // To be able to dismiss dialog at that time, don't check
334 // mDialogFragment.isVisible().
335 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700336 mDialogFragment.dismiss();
337 }
338 mDialogFragment = null;
339 }
340
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800341 /**
342 * Sets the OnCancelListener of the dialog shown. This method can only be
343 * called after showDialog(int) and before removeDialog(int). The method
344 * does nothing otherwise.
345 */
346 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
347 if (mDialogFragment != null) {
348 mDialogFragment.mOnCancelListener = listener;
349 }
350 }
351
352 /**
353 * Sets the OnDismissListener of the dialog shown. This method can only be
354 * called after showDialog(int) and before removeDialog(int). The method
355 * does nothing otherwise.
356 */
357 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
358 if (mDialogFragment != null) {
359 mDialogFragment.mOnDismissListener = listener;
360 }
361 }
362
Amith Yamasanic861cf82012-10-02 14:51:46 -0700363 public void onDialogShowing() {
364 // override in subclass to attach a dismiss listener, for instance
365 }
366
Amith Yamasani43c69782010-12-01 09:04:36 -0800367 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800368 private static final String KEY_DIALOG_ID = "key_dialog_id";
369 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
370
Amith Yamasanid7993472010-08-18 13:59:28 -0700371 private int mDialogId;
372
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800373 private Fragment mParentFragment;
374
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800375 private DialogInterface.OnCancelListener mOnCancelListener;
376 private DialogInterface.OnDismissListener mOnDismissListener;
377
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800378 public SettingsDialogFragment() {
379 /* do nothing */
380 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700381
Amith Yamasani43c69782010-12-01 09:04:36 -0800382 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700383 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800384 if (!(fragment instanceof Fragment)) {
385 throw new IllegalArgumentException("fragment argument must be an instance of "
386 + Fragment.class.getName());
387 }
388 mParentFragment = (Fragment) fragment;
389 }
390
391 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800392 public void onSaveInstanceState(Bundle outState) {
393 super.onSaveInstanceState(outState);
394 if (mParentFragment != null) {
395 outState.putInt(KEY_DIALOG_ID, mDialogId);
396 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
397 }
398 }
399
400 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700401 public void onStart() {
402 super.onStart();
403
404 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
405 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
406 }
407 }
408
409 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800410 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800411 if (savedInstanceState != null) {
412 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800413 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800414 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800415 if (!(mParentFragment instanceof DialogCreatable)) {
416 throw new IllegalArgumentException(
417 (mParentFragment != null
418 ? mParentFragment.getClass().getName()
419 : mParentFragmentId)
420 + " must implement "
421 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800422 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800423 // This dialog fragment could be created from non-SettingsPreferenceFragment
424 if (mParentFragment instanceof SettingsPreferenceFragment) {
425 // restore mDialogFragment in mParentFragment
426 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
427 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800428 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800429 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700430 }
431
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800432 @Override
433 public void onCancel(DialogInterface dialog) {
434 super.onCancel(dialog);
435 if (mOnCancelListener != null) {
436 mOnCancelListener.onCancel(dialog);
437 }
438 }
439
440 @Override
441 public void onDismiss(DialogInterface dialog) {
442 super.onDismiss(dialog);
443 if (mOnDismissListener != null) {
444 mOnDismissListener.onDismiss(dialog);
445 }
446 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800447
Amith Yamasanid7993472010-08-18 13:59:28 -0700448 public int getDialogId() {
449 return mDialogId;
450 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800451
452 @Override
453 public void onDetach() {
454 super.onDetach();
455
Amith Yamasani8875ede2011-01-31 12:46:57 -0800456 // This dialog fragment could be created from non-SettingsPreferenceFragment
457 if (mParentFragment instanceof SettingsPreferenceFragment) {
458 // in case the dialog is not explicitly removed by removeDialog()
459 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
460 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
461 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800462 }
463 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700464 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700465
466 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800467 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700468 }
469
470 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800471 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700472 }
473
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700474 public void finish() {
475 getActivity().onBackPressed();
476 }
477
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700478 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
479 int requestCode, Bundle extras) {
480 final Activity activity = getActivity();
481 if (activity instanceof SettingsActivity) {
482 SettingsActivity sa = (SettingsActivity) activity;
483 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
484 return true;
485 } else if (activity instanceof PreferenceActivity) {
486 PreferenceActivity sa = (PreferenceActivity) activity;
487 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700488 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700489 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700490 Log.w(TAG,
491 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
492 + "launch the given Fragment (name: " + fragmentClass
493 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700494 return false;
495 }
496 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700497}