blob: eb2bea9e8f300750cc65a349c32a69fa1f11c4a3 [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 Meglioc853a422014-04-18 19:40:40 -070026import android.database.DataSetObserver;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070027import 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;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070037import android.view.View;
38import android.view.ViewGroup;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070039import android.widget.Button;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -070040import android.widget.ListAdapter;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070041import android.widget.ListView;
Amith Yamasanid7993472010-08-18 13:59:28 -070042
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070043/**
Amith Yamasanid7993472010-08-18 13:59:28 -070044 * Base class for Settings fragments, with some helper functions and dialog management.
45 */
Gilles Debunne64650542011-08-23 11:01:35 -070046public class SettingsPreferenceFragment extends PreferenceFragment implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070047
48 private static final String TAG = "SettingsPreferenceFragment";
49
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070050 private static final int MENU_HELP = Menu.FIRST + 100;
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070051 private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 400;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070052
53 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070054
Amith Yamasanid7993472010-08-18 13:59:28 -070055 private SettingsDialogFragment mDialogFragment;
56
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070057 private String mHelpUrl;
58
Amith Yamasani350938e2013-04-09 10:22:47 -070059 // Cache the content resolver for async callbacks
60 private ContentResolver mContentResolver;
61
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070062 private String mPreferenceKey;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070063 private boolean mPreferenceHighlighted = false;
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -070064 private Drawable mHighlightDrawable;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070065
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070066 private boolean mIsDataSetObserverRegistered = false;
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070067 private DataSetObserver mDataSetObserver = new DataSetObserver() {
68 @Override
69 public void onChanged() {
70 highlightPreferenceIfNeeded();
71 }
72
73 @Override
74 public void onInvalidated() {
75 highlightPreferenceIfNeeded();
76 }
77 };
78
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070079 @Override
80 public void onCreate(Bundle icicle) {
81 super.onCreate(icicle);
82
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070083 if (icicle != null) {
84 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
85 }
86
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070087 // Prepare help url and enable menu if necessary
88 int helpResource = getHelpResource();
89 if (helpResource != 0) {
90 mHelpUrl = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070091 }
92 }
93
Daisuke Miyakawab5647c52010-09-10 18:04:02 -070094 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070095 public void onSaveInstanceState(Bundle outState) {
96 super.onSaveInstanceState(outState);
97
98 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
99 }
100
101 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700102 public void onActivityCreated(Bundle savedInstanceState) {
103 super.onActivityCreated(savedInstanceState);
Amith Yamasanib3a593e2012-04-23 18:03:52 -0700104 if (!TextUtils.isEmpty(mHelpUrl)) {
105 setHasOptionsMenu(true);
106 }
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700107 }
108
109 @Override
110 public void onResume() {
111 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700112
113 final Bundle args = getArguments();
114 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700115 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
116 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700117 }
118 }
119
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700120 @Override
121 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700122 registerObserverIfNeeded();
123 }
124
125 @Override
126 public void onStop() {
127 super.onStop();
128
129 unregisterObserverIfNeeded();
130 }
131
132 public void registerObserverIfNeeded() {
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700133 if (!mIsDataSetObserverRegistered) {
134 getPreferenceScreen().getRootAdapter().registerDataSetObserver(mDataSetObserver);
135 mIsDataSetObserverRegistered = true;
136 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700137 }
138
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700139 public void unregisterObserverIfNeeded() {
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700140 if (mIsDataSetObserverRegistered) {
141 getPreferenceScreen().getRootAdapter().unregisterDataSetObserver(mDataSetObserver);
142 mIsDataSetObserverRegistered = false;
143 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700144 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700145
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700146 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700147 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700148 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700149 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700150 }
151
152 private Drawable getHighlightDrawable() {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700153 if (mHighlightDrawable == null) {
154 mHighlightDrawable = getActivity().getDrawable(R.drawable.preference_highlight);
155 }
156 return mHighlightDrawable;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700157 }
158
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700159 /**
160 * Return a valid ListView position or -1 if none is found
161 */
162 private int canUseListViewForHighLighting(String key) {
163 if (!hasListView()) {
164 return -1;
165 }
166
167 ListView listView = getListView();
168 ListAdapter adapter = listView.getAdapter();
169
170 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
171 return findListPositionFromKey(adapter, key);
172 }
173
174 return -1;
175 }
176
177 private void highlightPreference(String key) {
178 final Drawable highlight = getHighlightDrawable();
179
180 final int position = canUseListViewForHighLighting(key);
181 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700182 mPreferenceHighlighted = true;
183
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700184 final ListView listView = getListView();
185 final ListAdapter adapter = listView.getAdapter();
186
187 ((PreferenceGroupAdapter) adapter).setHighlightedDrawable(highlight);
188 ((PreferenceGroupAdapter) adapter).setHighlighted(position);
189
190 listView.post(new Runnable() {
191 @Override
192 public void run() {
193 listView.setSelection(position);
194 listView.postDelayed(new Runnable() {
195 @Override
196 public void run() {
Alan Viveretteba348ca2014-05-19 15:10:36 -0700197 final View v = listView.getChildAt(0);
198 final int centerX = v.getWidth() / 2;
199 final int centerY = v.getHeight() / 2;
200 highlight.setHotspot(centerX, centerY);
201 v.setPressed(true);
202 v.setPressed(false);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700203 }
204 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700205 }
206 });
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700207 }
208 }
209
210 private int findListPositionFromKey(ListAdapter adapter, String key) {
211 final int count = adapter.getCount();
212 for (int n = 0; n < count; n++) {
213 final Object item = adapter.getItem(n);
214 if (item instanceof Preference) {
215 Preference preference = (Preference) item;
216 final String preferenceKey = preference.getKey();
217 if (preferenceKey != null && preferenceKey.equals(key)) {
218 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700219 }
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700220 }
221 }
222 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700223 }
224
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700225 protected void removePreference(String key) {
226 Preference pref = findPreference(key);
227 if (pref != null) {
228 getPreferenceScreen().removePreference(pref);
229 }
230 }
231
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700232 /**
233 * Override this if you want to show a help item in the menu, by returning the resource id.
234 * @return the resource id for the help url
235 */
236 protected int getHelpResource() {
237 return 0;
238 }
239
240 @Override
241 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800242 if (mHelpUrl != null && getActivity() != null) {
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700243 MenuItem helpItem = menu.add(0, MENU_HELP, 0, R.string.help_label);
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800244 HelpUtils.prepareHelpMenuItem(getActivity(), helpItem, mHelpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700245 }
246 }
247
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700248 /*
249 * The name is intentionally made different from Activity#finish(), so that
250 * users won't misunderstand its meaning.
251 */
252 public final void finishFragment() {
253 getActivity().onBackPressed();
254 }
255
Amith Yamasanid7993472010-08-18 13:59:28 -0700256 // Some helpers for functions used by the settings fragments when they were activities
257
258 /**
259 * Returns the ContentResolver from the owning Activity.
260 */
261 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700262 Context context = getActivity();
263 if (context != null) {
264 mContentResolver = context.getContentResolver();
265 }
266 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700267 }
268
269 /**
270 * Returns the specified system service from the owning Activity.
271 */
272 protected Object getSystemService(final String name) {
273 return getActivity().getSystemService(name);
274 }
275
276 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700277 * Returns the PackageManager from the owning Activity.
278 */
279 protected PackageManager getPackageManager() {
280 return getActivity().getPackageManager();
281 }
282
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800283 @Override
284 public void onDetach() {
285 if (isRemoving()) {
286 if (mDialogFragment != null) {
287 mDialogFragment.dismiss();
288 mDialogFragment = null;
289 }
290 }
291 super.onDetach();
292 }
293
Amith Yamasanid7993472010-08-18 13:59:28 -0700294 // Dialog management
295
296 protected void showDialog(int dialogId) {
297 if (mDialogFragment != null) {
298 Log.e(TAG, "Old dialog fragment not null!");
299 }
300 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800301 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700302 }
303
304 public Dialog onCreateDialog(int dialogId) {
305 return null;
306 }
307
308 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800309 // mDialogFragment may not be visible yet in parent fragment's onResume().
310 // To be able to dismiss dialog at that time, don't check
311 // mDialogFragment.isVisible().
312 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700313 mDialogFragment.dismiss();
314 }
315 mDialogFragment = null;
316 }
317
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800318 /**
319 * Sets the OnCancelListener of the dialog shown. This method can only be
320 * called after showDialog(int) and before removeDialog(int). The method
321 * does nothing otherwise.
322 */
323 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
324 if (mDialogFragment != null) {
325 mDialogFragment.mOnCancelListener = listener;
326 }
327 }
328
329 /**
330 * Sets the OnDismissListener of the dialog shown. This method can only be
331 * called after showDialog(int) and before removeDialog(int). The method
332 * does nothing otherwise.
333 */
334 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
335 if (mDialogFragment != null) {
336 mDialogFragment.mOnDismissListener = listener;
337 }
338 }
339
Amith Yamasanic861cf82012-10-02 14:51:46 -0700340 public void onDialogShowing() {
341 // override in subclass to attach a dismiss listener, for instance
342 }
343
Amith Yamasani43c69782010-12-01 09:04:36 -0800344 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800345 private static final String KEY_DIALOG_ID = "key_dialog_id";
346 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
347
Amith Yamasanid7993472010-08-18 13:59:28 -0700348 private int mDialogId;
349
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800350 private Fragment mParentFragment;
351
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800352 private DialogInterface.OnCancelListener mOnCancelListener;
353 private DialogInterface.OnDismissListener mOnDismissListener;
354
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800355 public SettingsDialogFragment() {
356 /* do nothing */
357 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700358
Amith Yamasani43c69782010-12-01 09:04:36 -0800359 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700360 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800361 if (!(fragment instanceof Fragment)) {
362 throw new IllegalArgumentException("fragment argument must be an instance of "
363 + Fragment.class.getName());
364 }
365 mParentFragment = (Fragment) fragment;
366 }
367
368 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800369 public void onSaveInstanceState(Bundle outState) {
370 super.onSaveInstanceState(outState);
371 if (mParentFragment != null) {
372 outState.putInt(KEY_DIALOG_ID, mDialogId);
373 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
374 }
375 }
376
377 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700378 public void onStart() {
379 super.onStart();
380
381 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
382 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
383 }
384 }
385
386 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800387 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800388 if (savedInstanceState != null) {
389 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800390 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800391 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800392 if (!(mParentFragment instanceof DialogCreatable)) {
393 throw new IllegalArgumentException(
394 (mParentFragment != null
395 ? mParentFragment.getClass().getName()
396 : mParentFragmentId)
397 + " must implement "
398 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800399 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800400 // This dialog fragment could be created from non-SettingsPreferenceFragment
401 if (mParentFragment instanceof SettingsPreferenceFragment) {
402 // restore mDialogFragment in mParentFragment
403 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
404 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800405 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800406 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700407 }
408
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800409 @Override
410 public void onCancel(DialogInterface dialog) {
411 super.onCancel(dialog);
412 if (mOnCancelListener != null) {
413 mOnCancelListener.onCancel(dialog);
414 }
415 }
416
417 @Override
418 public void onDismiss(DialogInterface dialog) {
419 super.onDismiss(dialog);
420 if (mOnDismissListener != null) {
421 mOnDismissListener.onDismiss(dialog);
422 }
423 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800424
Amith Yamasanid7993472010-08-18 13:59:28 -0700425 public int getDialogId() {
426 return mDialogId;
427 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800428
429 @Override
430 public void onDetach() {
431 super.onDetach();
432
Amith Yamasani8875ede2011-01-31 12:46:57 -0800433 // This dialog fragment could be created from non-SettingsPreferenceFragment
434 if (mParentFragment instanceof SettingsPreferenceFragment) {
435 // in case the dialog is not explicitly removed by removeDialog()
436 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
437 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
438 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800439 }
440 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700441 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700442
443 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800444 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700445 }
446
447 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800448 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700449 }
450
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700451 public void finish() {
452 getActivity().onBackPressed();
453 }
454
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700455 public boolean startFragment(
456 Fragment caller, String fragmentClass, int requestCode, Bundle extras) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800457 if (getActivity() instanceof SettingsActivity) {
458 SettingsActivity sa = (SettingsActivity) getActivity();
459 sa.startPreferencePanel(fragmentClass, extras,
Gilles Debunne64650542011-08-23 11:01:35 -0700460 R.string.lock_settings_picker_title, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700461 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700462 } else {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800463 Log.w(TAG, "Parent isn't Settings activity, thus there's no way to launch the "
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700464 + "given Fragment (name: " + fragmentClass + ", requestCode: " + requestCode
465 + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700466 return false;
467 }
468 }
469
Amith Yamasanid7993472010-08-18 13:59:28 -0700470}