blob: 8500f8dde32dbd3fe04538b91498acf2a4515a24 [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
19import android.app.Dialog;
20import android.app.DialogFragment;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -070021import android.app.Fragment;
Amith Yamasanid7993472010-08-18 13:59:28 -070022import android.content.ContentResolver;
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +080023import android.content.DialogInterface;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070024import android.content.Intent;
Amith Yamasanid7993472010-08-18 13:59:28 -070025import android.content.pm.PackageManager;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070026import android.net.Uri;
Amith Yamasanid7993472010-08-18 13:59:28 -070027import android.os.Bundle;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070028import android.preference.PreferenceActivity;
Amith Yamasanid7993472010-08-18 13:59:28 -070029import android.preference.PreferenceFragment;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070030import android.text.TextUtils;
Amith Yamasanid7993472010-08-18 13:59:28 -070031import android.util.Log;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070032import android.view.Menu;
33import android.view.MenuInflater;
34import android.view.MenuItem;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070035import android.widget.Button;
Amith Yamasanid7993472010-08-18 13:59:28 -070036
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070037/**
Amith Yamasanid7993472010-08-18 13:59:28 -070038 * Base class for Settings fragments, with some helper functions and dialog management.
39 */
Gilles Debunne64650542011-08-23 11:01:35 -070040public class SettingsPreferenceFragment extends PreferenceFragment implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070041
42 private static final String TAG = "SettingsPreferenceFragment";
43
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070044 private static final int MENU_HELP = Menu.FIRST + 100;
45
Amith Yamasanid7993472010-08-18 13:59:28 -070046 private SettingsDialogFragment mDialogFragment;
47
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070048 private String mHelpUrl;
49
50 @Override
51 public void onCreate(Bundle icicle) {
52 super.onCreate(icicle);
53
54 // Prepare help url and enable menu if necessary
55 int helpResource = getHelpResource();
56 if (helpResource != 0) {
57 mHelpUrl = getResources().getString(helpResource);
58 if (!TextUtils.isEmpty(mHelpUrl)) {
59 setHasOptionsMenu(true);
60 }
61 }
62 }
63
Daisuke Miyakawab5647c52010-09-10 18:04:02 -070064 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -070065 public void onActivityCreated(Bundle savedInstanceState) {
66 super.onActivityCreated(savedInstanceState);
Amith Yamasanid7993472010-08-18 13:59:28 -070067 }
68
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070069 /**
70 * Override this if you want to show a help item in the menu, by returning the resource id.
71 * @return the resource id for the help url
72 */
73 protected int getHelpResource() {
74 return 0;
75 }
76
77 @Override
78 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
79 if (mHelpUrl != null) {
80 Intent helpIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(mHelpUrl));
81 helpIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
82 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
83 MenuItem helpItem = menu.add(0, MENU_HELP, 0, R.string.help_label);
84 helpItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
85 helpItem.setIntent(helpIntent);
86 }
87 }
88
Daisuke Miyakawab5647c52010-09-10 18:04:02 -070089 /*
90 * The name is intentionally made different from Activity#finish(), so that
91 * users won't misunderstand its meaning.
92 */
93 public final void finishFragment() {
94 getActivity().onBackPressed();
95 }
96
Amith Yamasanid7993472010-08-18 13:59:28 -070097 // Some helpers for functions used by the settings fragments when they were activities
98
99 /**
100 * Returns the ContentResolver from the owning Activity.
101 */
102 protected ContentResolver getContentResolver() {
103 return getActivity().getContentResolver();
104 }
105
106 /**
107 * Returns the specified system service from the owning Activity.
108 */
109 protected Object getSystemService(final String name) {
110 return getActivity().getSystemService(name);
111 }
112
113 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700114 * Returns the PackageManager from the owning Activity.
115 */
116 protected PackageManager getPackageManager() {
117 return getActivity().getPackageManager();
118 }
119
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800120 @Override
121 public void onDetach() {
122 if (isRemoving()) {
123 if (mDialogFragment != null) {
124 mDialogFragment.dismiss();
125 mDialogFragment = null;
126 }
127 }
128 super.onDetach();
129 }
130
Amith Yamasanid7993472010-08-18 13:59:28 -0700131 // Dialog management
132
133 protected void showDialog(int dialogId) {
134 if (mDialogFragment != null) {
135 Log.e(TAG, "Old dialog fragment not null!");
136 }
137 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -0700138 mDialogFragment.show(getActivity().getFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700139 }
140
141 public Dialog onCreateDialog(int dialogId) {
142 return null;
143 }
144
145 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800146 // mDialogFragment may not be visible yet in parent fragment's onResume().
147 // To be able to dismiss dialog at that time, don't check
148 // mDialogFragment.isVisible().
149 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700150 mDialogFragment.dismiss();
151 }
152 mDialogFragment = null;
153 }
154
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800155 /**
156 * Sets the OnCancelListener of the dialog shown. This method can only be
157 * called after showDialog(int) and before removeDialog(int). The method
158 * does nothing otherwise.
159 */
160 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
161 if (mDialogFragment != null) {
162 mDialogFragment.mOnCancelListener = listener;
163 }
164 }
165
166 /**
167 * Sets the OnDismissListener of the dialog shown. This method can only be
168 * called after showDialog(int) and before removeDialog(int). The method
169 * does nothing otherwise.
170 */
171 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
172 if (mDialogFragment != null) {
173 mDialogFragment.mOnDismissListener = listener;
174 }
175 }
176
Amith Yamasani43c69782010-12-01 09:04:36 -0800177 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800178 private static final String KEY_DIALOG_ID = "key_dialog_id";
179 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
180
Amith Yamasanid7993472010-08-18 13:59:28 -0700181 private int mDialogId;
182
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800183 private Fragment mParentFragment;
184
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800185 private DialogInterface.OnCancelListener mOnCancelListener;
186 private DialogInterface.OnDismissListener mOnDismissListener;
187
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800188 public SettingsDialogFragment() {
189 /* do nothing */
190 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700191
Amith Yamasani43c69782010-12-01 09:04:36 -0800192 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700193 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800194 if (!(fragment instanceof Fragment)) {
195 throw new IllegalArgumentException("fragment argument must be an instance of "
196 + Fragment.class.getName());
197 }
198 mParentFragment = (Fragment) fragment;
199 }
200
201 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800202 public void onSaveInstanceState(Bundle outState) {
203 super.onSaveInstanceState(outState);
204 if (mParentFragment != null) {
205 outState.putInt(KEY_DIALOG_ID, mDialogId);
206 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
207 }
208 }
209
210 @Override
211 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800212 if (savedInstanceState != null) {
213 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
214 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
215 if (mParentFragmentId > -1) {
216 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
217 if (!(mParentFragment instanceof DialogCreatable)) {
218 throw new IllegalArgumentException(
219 KEY_PARENT_FRAGMENT_ID + " must implement "
220 + DialogCreatable.class.getName());
221 }
222 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800223 // This dialog fragment could be created from non-SettingsPreferenceFragment
224 if (mParentFragment instanceof SettingsPreferenceFragment) {
225 // restore mDialogFragment in mParentFragment
226 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
227 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800228 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800229 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700230 }
231
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800232 @Override
233 public void onCancel(DialogInterface dialog) {
234 super.onCancel(dialog);
235 if (mOnCancelListener != null) {
236 mOnCancelListener.onCancel(dialog);
237 }
238 }
239
240 @Override
241 public void onDismiss(DialogInterface dialog) {
242 super.onDismiss(dialog);
243 if (mOnDismissListener != null) {
244 mOnDismissListener.onDismiss(dialog);
245 }
246 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800247
Amith Yamasanid7993472010-08-18 13:59:28 -0700248 public int getDialogId() {
249 return mDialogId;
250 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800251
252 @Override
253 public void onDetach() {
254 super.onDetach();
255
Amith Yamasani8875ede2011-01-31 12:46:57 -0800256 // This dialog fragment could be created from non-SettingsPreferenceFragment
257 if (mParentFragment instanceof SettingsPreferenceFragment) {
258 // in case the dialog is not explicitly removed by removeDialog()
259 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
260 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
261 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800262 }
263 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700264 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700265
266 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800267 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700268 }
269
270 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800271 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700272 }
273
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700274 public void finish() {
275 getActivity().onBackPressed();
276 }
277
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700278 public boolean startFragment(
279 Fragment caller, String fragmentClass, int requestCode, Bundle extras) {
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700280 if (getActivity() instanceof PreferenceActivity) {
281 PreferenceActivity preferenceActivity = (PreferenceActivity)getActivity();
Gilles Debunne64650542011-08-23 11:01:35 -0700282 preferenceActivity.startPreferencePanel(fragmentClass, extras,
283 R.string.lock_settings_picker_title, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700284 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700285 } else {
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700286 Log.w(TAG, "Parent isn't PreferenceActivity, thus there's no way to launch the "
287 + "given Fragment (name: " + fragmentClass + ", requestCode: " + requestCode
288 + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700289 return false;
290 }
291 }
292
Amith Yamasanid7993472010-08-18 13:59:28 -0700293}