blob: ad924d6a255feec7c9b9b762e95964dee199b102 [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.content.res.TypedArray;
27import android.graphics.drawable.Drawable;
Amith Yamasanid7993472010-08-18 13:59:28 -070028import android.os.Bundle;
Amith Yamasani9627a8e2012-09-23 12:54:14 -070029import android.preference.Preference;
Amith Yamasanid7993472010-08-18 13:59:28 -070030import android.preference.PreferenceFragment;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -070031import android.preference.PreferenceGroupAdapter;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070032import android.text.TextUtils;
Amith Yamasanid7993472010-08-18 13:59:28 -070033import android.util.Log;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070034import android.view.Menu;
35import android.view.MenuInflater;
36import android.view.MenuItem;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070037import android.widget.Button;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -070038import android.widget.ListAdapter;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070039import android.widget.ListView;
Amith Yamasanid7993472010-08-18 13:59:28 -070040
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070041/**
Amith Yamasanid7993472010-08-18 13:59:28 -070042 * Base class for Settings fragments, with some helper functions and dialog management.
43 */
Gilles Debunne64650542011-08-23 11:01:35 -070044public class SettingsPreferenceFragment extends PreferenceFragment implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070045
46 private static final String TAG = "SettingsPreferenceFragment";
47
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070048 private static final int MENU_HELP = Menu.FIRST + 100;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070049 private static final int HIGHLIGHT_DURATION_MILLIS = 300;
50
51 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070052
Amith Yamasanid7993472010-08-18 13:59:28 -070053 private SettingsDialogFragment mDialogFragment;
54
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070055 private String mHelpUrl;
56
Amith Yamasani350938e2013-04-09 10:22:47 -070057 // Cache the content resolver for async callbacks
58 private ContentResolver mContentResolver;
59
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070060 private boolean mPreferenceHighlighted = false;
61
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070062 @Override
63 public void onCreate(Bundle icicle) {
64 super.onCreate(icicle);
65
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070066 if (icicle != null) {
67 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
68 }
69
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070070 // Prepare help url and enable menu if necessary
71 int helpResource = getHelpResource();
72 if (helpResource != 0) {
73 mHelpUrl = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070074 }
75 }
76
Daisuke Miyakawab5647c52010-09-10 18:04:02 -070077 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070078 public void onSaveInstanceState(Bundle outState) {
79 super.onSaveInstanceState(outState);
80
81 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
82 }
83
84 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -070085 public void onActivityCreated(Bundle savedInstanceState) {
86 super.onActivityCreated(savedInstanceState);
Amith Yamasanib3a593e2012-04-23 18:03:52 -070087 if (!TextUtils.isEmpty(mHelpUrl)) {
88 setHasOptionsMenu(true);
89 }
Fabrice Di Meglioc1457322014-04-04 19:07:50 -070090
91 final Bundle args = getArguments();
92 if (args != null) {
93 final String key = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070094 if (hasListView() && !TextUtils.isEmpty(key)) {
95 highlightPreference(key);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -070096 }
97 }
98 }
99
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700100 private void highlightPreference(String key) {
101 final int position = findPositionFromKey(key);
102 if (position >= 0) {
103 final ListView listView = getListView();
104 final ListAdapter adapter = listView.getAdapter();
105
106 if (adapter instanceof PreferenceGroupAdapter) {
107 final Drawable drawable = getHighlightDrawable();
108 ((PreferenceGroupAdapter) adapter).setHighlightedDrawable(drawable);
109 ((PreferenceGroupAdapter) adapter).setHighlighted(position);
110
111 listView.post(new Runnable() {
112 @Override
113 public void run() {
114 listView.setSelection(position);
115 if (!mPreferenceHighlighted) {
116 listView.postDelayed(new Runnable() {
117 @Override
118 public void run() {
119 final int centerX = listView.getWidth() / 2;
120 final int centerY = listView.getChildAt(0).getHeight() / 2;
121 drawable.setHotspot(0, centerX, centerY);
122 drawable.clearHotspots();
123 ((PreferenceGroupAdapter) adapter).setHighlighted(-1);
124 }
125 }, HIGHLIGHT_DURATION_MILLIS);
126
127 mPreferenceHighlighted = true;
128 }
129 }
130 });
131 }
132 }
133
134 }
135
136 private Drawable getHighlightDrawable() {
137 final int[] attrs = new int[] { android.R.attr.selectableItemBackground };
138 TypedArray ta = getActivity().getTheme().obtainStyledAttributes(attrs);
139 return ta.getDrawable(0);
140 }
141
142 private int findPositionFromKey(String key) {
143 final ListAdapter adapter = getListView().getAdapter();
144 final int count = adapter.getCount();
145 for (int n = 0; n < count; n++) {
146 Object item = adapter.getItem(n);
147 if (item instanceof Preference) {
148 Preference preference = (Preference) item;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700149 final String preferenceKey = preference.getKey();
150 if (preferenceKey != null && preferenceKey.equals(key)) {
151 return n;
152 }
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700153 }
154 }
155 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700156 }
157
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700158 protected void removePreference(String key) {
159 Preference pref = findPreference(key);
160 if (pref != null) {
161 getPreferenceScreen().removePreference(pref);
162 }
163 }
164
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700165 /**
166 * Override this if you want to show a help item in the menu, by returning the resource id.
167 * @return the resource id for the help url
168 */
169 protected int getHelpResource() {
170 return 0;
171 }
172
173 @Override
174 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800175 if (mHelpUrl != null && getActivity() != null) {
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700176 MenuItem helpItem = menu.add(0, MENU_HELP, 0, R.string.help_label);
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800177 HelpUtils.prepareHelpMenuItem(getActivity(), helpItem, mHelpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700178 }
179 }
180
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700181 /*
182 * The name is intentionally made different from Activity#finish(), so that
183 * users won't misunderstand its meaning.
184 */
185 public final void finishFragment() {
186 getActivity().onBackPressed();
187 }
188
Amith Yamasanid7993472010-08-18 13:59:28 -0700189 // Some helpers for functions used by the settings fragments when they were activities
190
191 /**
192 * Returns the ContentResolver from the owning Activity.
193 */
194 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700195 Context context = getActivity();
196 if (context != null) {
197 mContentResolver = context.getContentResolver();
198 }
199 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700200 }
201
202 /**
203 * Returns the specified system service from the owning Activity.
204 */
205 protected Object getSystemService(final String name) {
206 return getActivity().getSystemService(name);
207 }
208
209 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700210 * Returns the PackageManager from the owning Activity.
211 */
212 protected PackageManager getPackageManager() {
213 return getActivity().getPackageManager();
214 }
215
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800216 @Override
217 public void onDetach() {
218 if (isRemoving()) {
219 if (mDialogFragment != null) {
220 mDialogFragment.dismiss();
221 mDialogFragment = null;
222 }
223 }
224 super.onDetach();
225 }
226
Amith Yamasanid7993472010-08-18 13:59:28 -0700227 // Dialog management
228
229 protected void showDialog(int dialogId) {
230 if (mDialogFragment != null) {
231 Log.e(TAG, "Old dialog fragment not null!");
232 }
233 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800234 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700235 }
236
237 public Dialog onCreateDialog(int dialogId) {
238 return null;
239 }
240
241 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800242 // mDialogFragment may not be visible yet in parent fragment's onResume().
243 // To be able to dismiss dialog at that time, don't check
244 // mDialogFragment.isVisible().
245 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700246 mDialogFragment.dismiss();
247 }
248 mDialogFragment = null;
249 }
250
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800251 /**
252 * Sets the OnCancelListener of the dialog shown. This method can only be
253 * called after showDialog(int) and before removeDialog(int). The method
254 * does nothing otherwise.
255 */
256 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
257 if (mDialogFragment != null) {
258 mDialogFragment.mOnCancelListener = listener;
259 }
260 }
261
262 /**
263 * Sets the OnDismissListener of the dialog shown. This method can only be
264 * called after showDialog(int) and before removeDialog(int). The method
265 * does nothing otherwise.
266 */
267 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
268 if (mDialogFragment != null) {
269 mDialogFragment.mOnDismissListener = listener;
270 }
271 }
272
Amith Yamasanic861cf82012-10-02 14:51:46 -0700273 public void onDialogShowing() {
274 // override in subclass to attach a dismiss listener, for instance
275 }
276
Amith Yamasani43c69782010-12-01 09:04:36 -0800277 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800278 private static final String KEY_DIALOG_ID = "key_dialog_id";
279 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
280
Amith Yamasanid7993472010-08-18 13:59:28 -0700281 private int mDialogId;
282
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800283 private Fragment mParentFragment;
284
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800285 private DialogInterface.OnCancelListener mOnCancelListener;
286 private DialogInterface.OnDismissListener mOnDismissListener;
287
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800288 public SettingsDialogFragment() {
289 /* do nothing */
290 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700291
Amith Yamasani43c69782010-12-01 09:04:36 -0800292 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700293 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800294 if (!(fragment instanceof Fragment)) {
295 throw new IllegalArgumentException("fragment argument must be an instance of "
296 + Fragment.class.getName());
297 }
298 mParentFragment = (Fragment) fragment;
299 }
300
301 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800302 public void onSaveInstanceState(Bundle outState) {
303 super.onSaveInstanceState(outState);
304 if (mParentFragment != null) {
305 outState.putInt(KEY_DIALOG_ID, mDialogId);
306 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
307 }
308 }
309
310 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700311 public void onStart() {
312 super.onStart();
313
314 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
315 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
316 }
317 }
318
319 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800320 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800321 if (savedInstanceState != null) {
322 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800323 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800324 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800325 if (!(mParentFragment instanceof DialogCreatable)) {
326 throw new IllegalArgumentException(
327 (mParentFragment != null
328 ? mParentFragment.getClass().getName()
329 : mParentFragmentId)
330 + " must implement "
331 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800332 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800333 // This dialog fragment could be created from non-SettingsPreferenceFragment
334 if (mParentFragment instanceof SettingsPreferenceFragment) {
335 // restore mDialogFragment in mParentFragment
336 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
337 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800338 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800339 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700340 }
341
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800342 @Override
343 public void onCancel(DialogInterface dialog) {
344 super.onCancel(dialog);
345 if (mOnCancelListener != null) {
346 mOnCancelListener.onCancel(dialog);
347 }
348 }
349
350 @Override
351 public void onDismiss(DialogInterface dialog) {
352 super.onDismiss(dialog);
353 if (mOnDismissListener != null) {
354 mOnDismissListener.onDismiss(dialog);
355 }
356 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800357
Amith Yamasanid7993472010-08-18 13:59:28 -0700358 public int getDialogId() {
359 return mDialogId;
360 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800361
362 @Override
363 public void onDetach() {
364 super.onDetach();
365
Amith Yamasani8875ede2011-01-31 12:46:57 -0800366 // This dialog fragment could be created from non-SettingsPreferenceFragment
367 if (mParentFragment instanceof SettingsPreferenceFragment) {
368 // in case the dialog is not explicitly removed by removeDialog()
369 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
370 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
371 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800372 }
373 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700374 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700375
376 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800377 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700378 }
379
380 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800381 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700382 }
383
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700384 public void finish() {
385 getActivity().onBackPressed();
386 }
387
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700388 public boolean startFragment(
389 Fragment caller, String fragmentClass, int requestCode, Bundle extras) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800390 if (getActivity() instanceof SettingsActivity) {
391 SettingsActivity sa = (SettingsActivity) getActivity();
392 sa.startPreferencePanel(fragmentClass, extras,
Gilles Debunne64650542011-08-23 11:01:35 -0700393 R.string.lock_settings_picker_title, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700394 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700395 } else {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800396 Log.w(TAG, "Parent isn't Settings activity, thus there's no way to launch the "
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700397 + "given Fragment (name: " + fragmentClass + ", requestCode: " + requestCode
398 + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700399 return false;
400 }
401 }
402
Amith Yamasanid7993472010-08-18 13:59:28 -0700403}