blob: b418826f74ecbd64957d940521b1af5939f8de6d [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;
Amith Yamasani9627a8e2012-09-23 12:54:14 -070028import android.preference.Preference;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070029import android.preference.PreferenceActivity;
Amith Yamasanid7993472010-08-18 13:59:28 -070030import android.preference.PreferenceFragment;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070031import android.text.TextUtils;
Amith Yamasanid7993472010-08-18 13:59:28 -070032import android.util.Log;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070033import android.view.Menu;
34import android.view.MenuInflater;
35import android.view.MenuItem;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070036import android.widget.Button;
Amith Yamasanid7993472010-08-18 13:59:28 -070037
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070038/**
Amith Yamasanid7993472010-08-18 13:59:28 -070039 * Base class for Settings fragments, with some helper functions and dialog management.
40 */
Gilles Debunne64650542011-08-23 11:01:35 -070041public class SettingsPreferenceFragment extends PreferenceFragment implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070042
43 private static final String TAG = "SettingsPreferenceFragment";
44
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070045 private static final int MENU_HELP = Menu.FIRST + 100;
46
Amith Yamasanid7993472010-08-18 13:59:28 -070047 private SettingsDialogFragment mDialogFragment;
48
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070049 private String mHelpUrl;
50
51 @Override
52 public void onCreate(Bundle icicle) {
53 super.onCreate(icicle);
54
55 // Prepare help url and enable menu if necessary
56 int helpResource = getHelpResource();
57 if (helpResource != 0) {
58 mHelpUrl = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070059 }
60 }
61
Daisuke Miyakawab5647c52010-09-10 18:04:02 -070062 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -070063 public void onActivityCreated(Bundle savedInstanceState) {
64 super.onActivityCreated(savedInstanceState);
Amith Yamasanib3a593e2012-04-23 18:03:52 -070065 if (!TextUtils.isEmpty(mHelpUrl)) {
66 setHasOptionsMenu(true);
67 }
Amith Yamasanid7993472010-08-18 13:59:28 -070068 }
69
Amith Yamasani9627a8e2012-09-23 12:54:14 -070070 protected void removePreference(String key) {
71 Preference pref = findPreference(key);
72 if (pref != null) {
73 getPreferenceScreen().removePreference(pref);
74 }
75 }
76
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070077 /**
78 * Override this if you want to show a help item in the menu, by returning the resource id.
79 * @return the resource id for the help url
80 */
81 protected int getHelpResource() {
82 return 0;
83 }
84
85 @Override
86 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -080087 if (mHelpUrl != null && getActivity() != null) {
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070088 MenuItem helpItem = menu.add(0, MENU_HELP, 0, R.string.help_label);
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -080089 HelpUtils.prepareHelpMenuItem(getActivity(), helpItem, mHelpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070090 }
91 }
92
Daisuke Miyakawab5647c52010-09-10 18:04:02 -070093 /*
94 * The name is intentionally made different from Activity#finish(), so that
95 * users won't misunderstand its meaning.
96 */
97 public final void finishFragment() {
98 getActivity().onBackPressed();
99 }
100
Amith Yamasanid7993472010-08-18 13:59:28 -0700101 // Some helpers for functions used by the settings fragments when they were activities
102
103 /**
104 * Returns the ContentResolver from the owning Activity.
105 */
106 protected ContentResolver getContentResolver() {
107 return getActivity().getContentResolver();
108 }
109
110 /**
111 * Returns the specified system service from the owning Activity.
112 */
113 protected Object getSystemService(final String name) {
114 return getActivity().getSystemService(name);
115 }
116
117 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700118 * Returns the PackageManager from the owning Activity.
119 */
120 protected PackageManager getPackageManager() {
121 return getActivity().getPackageManager();
122 }
123
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800124 @Override
125 public void onDetach() {
126 if (isRemoving()) {
127 if (mDialogFragment != null) {
128 mDialogFragment.dismiss();
129 mDialogFragment = null;
130 }
131 }
132 super.onDetach();
133 }
134
Amith Yamasanid7993472010-08-18 13:59:28 -0700135 // Dialog management
136
137 protected void showDialog(int dialogId) {
138 if (mDialogFragment != null) {
139 Log.e(TAG, "Old dialog fragment not null!");
140 }
141 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -0700142 mDialogFragment.show(getActivity().getFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700143 }
144
145 public Dialog onCreateDialog(int dialogId) {
146 return null;
147 }
148
149 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800150 // mDialogFragment may not be visible yet in parent fragment's onResume().
151 // To be able to dismiss dialog at that time, don't check
152 // mDialogFragment.isVisible().
153 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700154 mDialogFragment.dismiss();
155 }
156 mDialogFragment = null;
157 }
158
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800159 /**
160 * Sets the OnCancelListener of the dialog shown. This method can only be
161 * called after showDialog(int) and before removeDialog(int). The method
162 * does nothing otherwise.
163 */
164 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
165 if (mDialogFragment != null) {
166 mDialogFragment.mOnCancelListener = listener;
167 }
168 }
169
170 /**
171 * Sets the OnDismissListener of the dialog shown. This method can only be
172 * called after showDialog(int) and before removeDialog(int). The method
173 * does nothing otherwise.
174 */
175 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
176 if (mDialogFragment != null) {
177 mDialogFragment.mOnDismissListener = listener;
178 }
179 }
180
Amith Yamasanic861cf82012-10-02 14:51:46 -0700181 public void onDialogShowing() {
182 // override in subclass to attach a dismiss listener, for instance
183 }
184
Amith Yamasani43c69782010-12-01 09:04:36 -0800185 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800186 private static final String KEY_DIALOG_ID = "key_dialog_id";
187 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
188
Amith Yamasanid7993472010-08-18 13:59:28 -0700189 private int mDialogId;
190
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800191 private Fragment mParentFragment;
192
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800193 private DialogInterface.OnCancelListener mOnCancelListener;
194 private DialogInterface.OnDismissListener mOnDismissListener;
195
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800196 public SettingsDialogFragment() {
197 /* do nothing */
198 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700199
Amith Yamasani43c69782010-12-01 09:04:36 -0800200 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700201 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800202 if (!(fragment instanceof Fragment)) {
203 throw new IllegalArgumentException("fragment argument must be an instance of "
204 + Fragment.class.getName());
205 }
206 mParentFragment = (Fragment) fragment;
207 }
208
209 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800210 public void onSaveInstanceState(Bundle outState) {
211 super.onSaveInstanceState(outState);
212 if (mParentFragment != null) {
213 outState.putInt(KEY_DIALOG_ID, mDialogId);
214 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
215 }
216 }
217
218 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700219 public void onStart() {
220 super.onStart();
221
222 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
223 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
224 }
225 }
226
227 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800228 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800229 if (savedInstanceState != null) {
230 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
231 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
232 if (mParentFragmentId > -1) {
233 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
234 if (!(mParentFragment instanceof DialogCreatable)) {
235 throw new IllegalArgumentException(
236 KEY_PARENT_FRAGMENT_ID + " must implement "
237 + DialogCreatable.class.getName());
238 }
239 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800240 // This dialog fragment could be created from non-SettingsPreferenceFragment
241 if (mParentFragment instanceof SettingsPreferenceFragment) {
242 // restore mDialogFragment in mParentFragment
243 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
244 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800245 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800246 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700247 }
248
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800249 @Override
250 public void onCancel(DialogInterface dialog) {
251 super.onCancel(dialog);
252 if (mOnCancelListener != null) {
253 mOnCancelListener.onCancel(dialog);
254 }
255 }
256
257 @Override
258 public void onDismiss(DialogInterface dialog) {
259 super.onDismiss(dialog);
260 if (mOnDismissListener != null) {
261 mOnDismissListener.onDismiss(dialog);
262 }
263 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800264
Amith Yamasanid7993472010-08-18 13:59:28 -0700265 public int getDialogId() {
266 return mDialogId;
267 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800268
269 @Override
270 public void onDetach() {
271 super.onDetach();
272
Amith Yamasani8875ede2011-01-31 12:46:57 -0800273 // This dialog fragment could be created from non-SettingsPreferenceFragment
274 if (mParentFragment instanceof SettingsPreferenceFragment) {
275 // in case the dialog is not explicitly removed by removeDialog()
276 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
277 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
278 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800279 }
280 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700281 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700282
283 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800284 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700285 }
286
287 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800288 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700289 }
290
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700291 public void finish() {
292 getActivity().onBackPressed();
293 }
294
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700295 public boolean startFragment(
296 Fragment caller, String fragmentClass, int requestCode, Bundle extras) {
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700297 if (getActivity() instanceof PreferenceActivity) {
298 PreferenceActivity preferenceActivity = (PreferenceActivity)getActivity();
Gilles Debunne64650542011-08-23 11:01:35 -0700299 preferenceActivity.startPreferencePanel(fragmentClass, extras,
300 R.string.lock_settings_picker_title, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700301 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700302 } else {
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700303 Log.w(TAG, "Parent isn't PreferenceActivity, thus there's no way to launch the "
304 + "given Fragment (name: " + fragmentClass + ", requestCode: " + requestCode
305 + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700306 return false;
307 }
308 }
309
Amith Yamasanid7993472010-08-18 13:59:28 -0700310}