blob: 55f4aa50a59e1aff33ed49ba919c9caacc172631 [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;
Amith Yamasani350938e2013-04-09 10:22:47 -070023import android.content.Context;
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +080024import android.content.DialogInterface;
Amith Yamasanid7993472010-08-18 13:59:28 -070025import android.content.pm.PackageManager;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070026import android.graphics.drawable.Drawable;
Amith Yamasanid7993472010-08-18 13:59:28 -070027import android.os.Bundle;
Amith Yamasani9627a8e2012-09-23 12:54:14 -070028import android.preference.Preference;
Amith Yamasanid7993472010-08-18 13:59:28 -070029import android.preference.PreferenceFragment;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -070030import android.preference.PreferenceGroupAdapter;
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;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -070037import android.widget.ListAdapter;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070038import android.widget.ListView;
Amith Yamasanid7993472010-08-18 13:59:28 -070039
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070040/**
Amith Yamasanid7993472010-08-18 13:59:28 -070041 * Base class for Settings fragments, with some helper functions and dialog management.
42 */
Gilles Debunne64650542011-08-23 11:01:35 -070043public class SettingsPreferenceFragment extends PreferenceFragment implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070044
45 private static final String TAG = "SettingsPreferenceFragment";
46
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070047 private static final int MENU_HELP = Menu.FIRST + 100;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070048 private static final int HIGHLIGHT_DURATION_MILLIS = 300;
49
50 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070051
Amith Yamasanid7993472010-08-18 13:59:28 -070052 private SettingsDialogFragment mDialogFragment;
53
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070054 private String mHelpUrl;
55
Amith Yamasani350938e2013-04-09 10:22:47 -070056 // Cache the content resolver for async callbacks
57 private ContentResolver mContentResolver;
58
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070059 private boolean mPreferenceHighlighted = false;
60
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070061 @Override
62 public void onCreate(Bundle icicle) {
63 super.onCreate(icicle);
64
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070065 if (icicle != null) {
66 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
67 }
68
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070069 // Prepare help url and enable menu if necessary
70 int helpResource = getHelpResource();
71 if (helpResource != 0) {
72 mHelpUrl = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070073 }
74 }
75
Daisuke Miyakawab5647c52010-09-10 18:04:02 -070076 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070077 public void onSaveInstanceState(Bundle outState) {
78 super.onSaveInstanceState(outState);
79
80 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
81 }
82
83 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -070084 public void onActivityCreated(Bundle savedInstanceState) {
85 super.onActivityCreated(savedInstanceState);
Amith Yamasanib3a593e2012-04-23 18:03:52 -070086 if (!TextUtils.isEmpty(mHelpUrl)) {
87 setHasOptionsMenu(true);
88 }
Fabrice Di Meglioc1457322014-04-04 19:07:50 -070089
90 final Bundle args = getArguments();
91 if (args != null) {
92 final String key = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070093 if (hasListView() && !TextUtils.isEmpty(key)) {
94 highlightPreference(key);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -070095 }
96 }
97 }
98
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070099 private void highlightPreference(String key) {
100 final int position = findPositionFromKey(key);
101 if (position >= 0) {
102 final ListView listView = getListView();
103 final ListAdapter adapter = listView.getAdapter();
104
105 if (adapter instanceof PreferenceGroupAdapter) {
106 final Drawable drawable = getHighlightDrawable();
107 ((PreferenceGroupAdapter) adapter).setHighlightedDrawable(drawable);
108 ((PreferenceGroupAdapter) adapter).setHighlighted(position);
109
110 listView.post(new Runnable() {
111 @Override
112 public void run() {
113 listView.setSelection(position);
114 if (!mPreferenceHighlighted) {
115 listView.postDelayed(new Runnable() {
116 @Override
117 public void run() {
118 final int centerX = listView.getWidth() / 2;
119 final int centerY = listView.getChildAt(0).getHeight() / 2;
120 drawable.setHotspot(0, centerX, centerY);
121 drawable.clearHotspots();
122 ((PreferenceGroupAdapter) adapter).setHighlighted(-1);
123 }
124 }, HIGHLIGHT_DURATION_MILLIS);
125
126 mPreferenceHighlighted = true;
127 }
128 }
129 });
130 }
131 }
132
133 }
134
135 private Drawable getHighlightDrawable() {
Fabrice Di Meglio906ff6f2014-04-16 18:01:38 -0700136 return getResources().getDrawable(R.drawable.preference_highlight);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700137 }
138
139 private int findPositionFromKey(String key) {
140 final ListAdapter adapter = getListView().getAdapter();
141 final int count = adapter.getCount();
142 for (int n = 0; n < count; n++) {
143 Object item = adapter.getItem(n);
144 if (item instanceof Preference) {
145 Preference preference = (Preference) item;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700146 final String preferenceKey = preference.getKey();
147 if (preferenceKey != null && preferenceKey.equals(key)) {
148 return n;
149 }
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700150 }
151 }
152 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700153 }
154
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700155 protected void removePreference(String key) {
156 Preference pref = findPreference(key);
157 if (pref != null) {
158 getPreferenceScreen().removePreference(pref);
159 }
160 }
161
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700162 /**
163 * Override this if you want to show a help item in the menu, by returning the resource id.
164 * @return the resource id for the help url
165 */
166 protected int getHelpResource() {
167 return 0;
168 }
169
170 @Override
171 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800172 if (mHelpUrl != null && getActivity() != null) {
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700173 MenuItem helpItem = menu.add(0, MENU_HELP, 0, R.string.help_label);
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800174 HelpUtils.prepareHelpMenuItem(getActivity(), helpItem, mHelpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700175 }
176 }
177
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700178 /*
179 * The name is intentionally made different from Activity#finish(), so that
180 * users won't misunderstand its meaning.
181 */
182 public final void finishFragment() {
183 getActivity().onBackPressed();
184 }
185
Amith Yamasanid7993472010-08-18 13:59:28 -0700186 // Some helpers for functions used by the settings fragments when they were activities
187
188 /**
189 * Returns the ContentResolver from the owning Activity.
190 */
191 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700192 Context context = getActivity();
193 if (context != null) {
194 mContentResolver = context.getContentResolver();
195 }
196 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700197 }
198
199 /**
200 * Returns the specified system service from the owning Activity.
201 */
202 protected Object getSystemService(final String name) {
203 return getActivity().getSystemService(name);
204 }
205
206 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700207 * Returns the PackageManager from the owning Activity.
208 */
209 protected PackageManager getPackageManager() {
210 return getActivity().getPackageManager();
211 }
212
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800213 @Override
214 public void onDetach() {
215 if (isRemoving()) {
216 if (mDialogFragment != null) {
217 mDialogFragment.dismiss();
218 mDialogFragment = null;
219 }
220 }
221 super.onDetach();
222 }
223
Amith Yamasanid7993472010-08-18 13:59:28 -0700224 // Dialog management
225
226 protected void showDialog(int dialogId) {
227 if (mDialogFragment != null) {
228 Log.e(TAG, "Old dialog fragment not null!");
229 }
230 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800231 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700232 }
233
234 public Dialog onCreateDialog(int dialogId) {
235 return null;
236 }
237
238 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800239 // mDialogFragment may not be visible yet in parent fragment's onResume().
240 // To be able to dismiss dialog at that time, don't check
241 // mDialogFragment.isVisible().
242 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700243 mDialogFragment.dismiss();
244 }
245 mDialogFragment = null;
246 }
247
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800248 /**
249 * Sets the OnCancelListener of the dialog shown. This method can only be
250 * called after showDialog(int) and before removeDialog(int). The method
251 * does nothing otherwise.
252 */
253 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
254 if (mDialogFragment != null) {
255 mDialogFragment.mOnCancelListener = listener;
256 }
257 }
258
259 /**
260 * Sets the OnDismissListener of the dialog shown. This method can only be
261 * called after showDialog(int) and before removeDialog(int). The method
262 * does nothing otherwise.
263 */
264 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
265 if (mDialogFragment != null) {
266 mDialogFragment.mOnDismissListener = listener;
267 }
268 }
269
Amith Yamasanic861cf82012-10-02 14:51:46 -0700270 public void onDialogShowing() {
271 // override in subclass to attach a dismiss listener, for instance
272 }
273
Amith Yamasani43c69782010-12-01 09:04:36 -0800274 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800275 private static final String KEY_DIALOG_ID = "key_dialog_id";
276 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
277
Amith Yamasanid7993472010-08-18 13:59:28 -0700278 private int mDialogId;
279
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800280 private Fragment mParentFragment;
281
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800282 private DialogInterface.OnCancelListener mOnCancelListener;
283 private DialogInterface.OnDismissListener mOnDismissListener;
284
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800285 public SettingsDialogFragment() {
286 /* do nothing */
287 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700288
Amith Yamasani43c69782010-12-01 09:04:36 -0800289 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700290 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800291 if (!(fragment instanceof Fragment)) {
292 throw new IllegalArgumentException("fragment argument must be an instance of "
293 + Fragment.class.getName());
294 }
295 mParentFragment = (Fragment) fragment;
296 }
297
298 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800299 public void onSaveInstanceState(Bundle outState) {
300 super.onSaveInstanceState(outState);
301 if (mParentFragment != null) {
302 outState.putInt(KEY_DIALOG_ID, mDialogId);
303 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
304 }
305 }
306
307 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700308 public void onStart() {
309 super.onStart();
310
311 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
312 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
313 }
314 }
315
316 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800317 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800318 if (savedInstanceState != null) {
319 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800320 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800321 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800322 if (!(mParentFragment instanceof DialogCreatable)) {
323 throw new IllegalArgumentException(
324 (mParentFragment != null
325 ? mParentFragment.getClass().getName()
326 : mParentFragmentId)
327 + " must implement "
328 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800329 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800330 // This dialog fragment could be created from non-SettingsPreferenceFragment
331 if (mParentFragment instanceof SettingsPreferenceFragment) {
332 // restore mDialogFragment in mParentFragment
333 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
334 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800335 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800336 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700337 }
338
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800339 @Override
340 public void onCancel(DialogInterface dialog) {
341 super.onCancel(dialog);
342 if (mOnCancelListener != null) {
343 mOnCancelListener.onCancel(dialog);
344 }
345 }
346
347 @Override
348 public void onDismiss(DialogInterface dialog) {
349 super.onDismiss(dialog);
350 if (mOnDismissListener != null) {
351 mOnDismissListener.onDismiss(dialog);
352 }
353 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800354
Amith Yamasanid7993472010-08-18 13:59:28 -0700355 public int getDialogId() {
356 return mDialogId;
357 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800358
359 @Override
360 public void onDetach() {
361 super.onDetach();
362
Amith Yamasani8875ede2011-01-31 12:46:57 -0800363 // This dialog fragment could be created from non-SettingsPreferenceFragment
364 if (mParentFragment instanceof SettingsPreferenceFragment) {
365 // in case the dialog is not explicitly removed by removeDialog()
366 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
367 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
368 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800369 }
370 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700371 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700372
373 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800374 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700375 }
376
377 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800378 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700379 }
380
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700381 public void finish() {
382 getActivity().onBackPressed();
383 }
384
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700385 public boolean startFragment(
386 Fragment caller, String fragmentClass, int requestCode, Bundle extras) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800387 if (getActivity() instanceof SettingsActivity) {
388 SettingsActivity sa = (SettingsActivity) getActivity();
389 sa.startPreferencePanel(fragmentClass, extras,
Gilles Debunne64650542011-08-23 11:01:35 -0700390 R.string.lock_settings_picker_title, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700391 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700392 } else {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800393 Log.w(TAG, "Parent isn't Settings activity, thus there's no way to launch the "
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700394 + "given Fragment (name: " + fragmentClass + ", requestCode: " + requestCode
395 + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700396 return false;
397 }
398 }
399
Amith Yamasanid7993472010-08-18 13:59:28 -0700400}