blob: 5e0c06de24df37f39909f9113ab60849afca04b0 [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;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070036import android.view.View;
37import android.view.ViewGroup;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070038import android.widget.Button;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -070039import android.widget.ListAdapter;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070040import android.widget.ListView;
Amith Yamasanid7993472010-08-18 13:59:28 -070041
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070042/**
Amith Yamasanid7993472010-08-18 13:59:28 -070043 * Base class for Settings fragments, with some helper functions and dialog management.
44 */
Gilles Debunne64650542011-08-23 11:01:35 -070045public class SettingsPreferenceFragment extends PreferenceFragment implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070046
47 private static final String TAG = "SettingsPreferenceFragment";
48
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070049 private static final int MENU_HELP = Menu.FIRST + 100;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070050 private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 300;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070051
52 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070053
Amith Yamasanid7993472010-08-18 13:59:28 -070054 private SettingsDialogFragment mDialogFragment;
55
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070056 private String mHelpUrl;
57
Amith Yamasani350938e2013-04-09 10:22:47 -070058 // Cache the content resolver for async callbacks
59 private ContentResolver mContentResolver;
60
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070061 private String mPreferenceKey;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070062 private boolean mPreferenceHighlighted = false;
63
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070064 @Override
65 public void onCreate(Bundle icicle) {
66 super.onCreate(icicle);
67
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070068 if (icicle != null) {
69 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
70 }
71
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070072 // Prepare help url and enable menu if necessary
73 int helpResource = getHelpResource();
74 if (helpResource != 0) {
75 mHelpUrl = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070076 }
77 }
78
Daisuke Miyakawab5647c52010-09-10 18:04:02 -070079 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070080 public void onSaveInstanceState(Bundle outState) {
81 super.onSaveInstanceState(outState);
82
83 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
84 }
85
86 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -070087 public void onActivityCreated(Bundle savedInstanceState) {
88 super.onActivityCreated(savedInstanceState);
Amith Yamasanib3a593e2012-04-23 18:03:52 -070089 if (!TextUtils.isEmpty(mHelpUrl)) {
90 setHasOptionsMenu(true);
91 }
Fabrice Di Meglioc1457322014-04-04 19:07:50 -070092
93 final Bundle args = getArguments();
94 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070095 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
96 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -070097 }
98 }
99
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700100 @Override
101 protected void onBindPreferences() {
102 highlightPreferenceIfNeeded();
103 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700104
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700105 public void highlightPreferenceIfNeeded() {
106 if (!mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
107 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700108 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700109 }
110
111 private Drawable getHighlightDrawable() {
Fabrice Di Meglio906ff6f2014-04-16 18:01:38 -0700112 return getResources().getDrawable(R.drawable.preference_highlight);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700113 }
114
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700115 /**
116 * Return a valid ListView position or -1 if none is found
117 */
118 private int canUseListViewForHighLighting(String key) {
119 if (!hasListView()) {
120 return -1;
121 }
122
123 ListView listView = getListView();
124 ListAdapter adapter = listView.getAdapter();
125
126 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
127 return findListPositionFromKey(adapter, key);
128 }
129
130 return -1;
131 }
132
133 private void highlightPreference(String key) {
134 final Drawable highlight = getHighlightDrawable();
135
136 final int position = canUseListViewForHighLighting(key);
137 if (position >= 0) {
138 final ListView listView = getListView();
139 final ListAdapter adapter = listView.getAdapter();
140
141 ((PreferenceGroupAdapter) adapter).setHighlightedDrawable(highlight);
142 ((PreferenceGroupAdapter) adapter).setHighlighted(position);
143
144 listView.post(new Runnable() {
145 @Override
146 public void run() {
147 listView.setSelection(position);
148 listView.postDelayed(new Runnable() {
149 @Override
150 public void run() {
151 final int centerX = listView.getWidth() / 2;
152 final int centerY = listView.getChildAt(0).getHeight() / 2;
153 highlight.setHotspot(0, centerX, centerY);
154 highlight.clearHotspots();
155 ((PreferenceGroupAdapter) adapter).setHighlighted(-1);
156 }
157 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
158
159 mPreferenceHighlighted = true;
160 }
161 });
162 } else {
163 // Try locating the Preference View thru its tag
164 View preferenceView = findPreferenceViewForKey(getView(), key);
165 if (preferenceView != null ) {
166 preferenceView.setBackground(highlight);
167 final int centerX = preferenceView.getWidth() / 2;
168 final int centerY = preferenceView.getHeight() / 2;
169 highlight.setHotspot(0, centerX, centerY);
170 highlight.clearHotspots();
171 }
172 }
173 }
174
175 private int findListPositionFromKey(ListAdapter adapter, String key) {
176 final int count = adapter.getCount();
177 for (int n = 0; n < count; n++) {
178 final Object item = adapter.getItem(n);
179 if (item instanceof Preference) {
180 Preference preference = (Preference) item;
181 final String preferenceKey = preference.getKey();
182 if (preferenceKey != null && preferenceKey.equals(key)) {
183 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700184 }
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700185 }
186 }
187 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700188 }
189
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700190 private View findPreferenceViewForKey(View root, String key) {
191 if (checkTag(root, key)) {
192 return root;
193 }
194 if (root instanceof ViewGroup) {
195 final ViewGroup group = (ViewGroup) root;
196 final int count = group.getChildCount();
197 for (int n = 0; n < count; n++) {
198 final View child = group.getChildAt(n);
199 final View view = findPreferenceViewForKey(child, key);
200 if (view != null) {
201 return view;
202 }
203 }
204 }
205 return null;
206 }
207
208 private boolean checkTag(View view, String key) {
209 final Object tag = view.getTag();
210 if (tag == null || !(tag instanceof String)) {
211 return false;
212 }
213 final String prefKey = (String) tag;
214 return (!TextUtils.isEmpty(prefKey) && prefKey.equals(key));
215 }
216
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700217 protected void removePreference(String key) {
218 Preference pref = findPreference(key);
219 if (pref != null) {
220 getPreferenceScreen().removePreference(pref);
221 }
222 }
223
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700224 /**
225 * Override this if you want to show a help item in the menu, by returning the resource id.
226 * @return the resource id for the help url
227 */
228 protected int getHelpResource() {
229 return 0;
230 }
231
232 @Override
233 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800234 if (mHelpUrl != null && getActivity() != null) {
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700235 MenuItem helpItem = menu.add(0, MENU_HELP, 0, R.string.help_label);
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800236 HelpUtils.prepareHelpMenuItem(getActivity(), helpItem, mHelpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700237 }
238 }
239
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700240 /*
241 * The name is intentionally made different from Activity#finish(), so that
242 * users won't misunderstand its meaning.
243 */
244 public final void finishFragment() {
245 getActivity().onBackPressed();
246 }
247
Amith Yamasanid7993472010-08-18 13:59:28 -0700248 // Some helpers for functions used by the settings fragments when they were activities
249
250 /**
251 * Returns the ContentResolver from the owning Activity.
252 */
253 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700254 Context context = getActivity();
255 if (context != null) {
256 mContentResolver = context.getContentResolver();
257 }
258 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700259 }
260
261 /**
262 * Returns the specified system service from the owning Activity.
263 */
264 protected Object getSystemService(final String name) {
265 return getActivity().getSystemService(name);
266 }
267
268 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700269 * Returns the PackageManager from the owning Activity.
270 */
271 protected PackageManager getPackageManager() {
272 return getActivity().getPackageManager();
273 }
274
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800275 @Override
276 public void onDetach() {
277 if (isRemoving()) {
278 if (mDialogFragment != null) {
279 mDialogFragment.dismiss();
280 mDialogFragment = null;
281 }
282 }
283 super.onDetach();
284 }
285
Amith Yamasanid7993472010-08-18 13:59:28 -0700286 // Dialog management
287
288 protected void showDialog(int dialogId) {
289 if (mDialogFragment != null) {
290 Log.e(TAG, "Old dialog fragment not null!");
291 }
292 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800293 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700294 }
295
296 public Dialog onCreateDialog(int dialogId) {
297 return null;
298 }
299
300 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800301 // mDialogFragment may not be visible yet in parent fragment's onResume().
302 // To be able to dismiss dialog at that time, don't check
303 // mDialogFragment.isVisible().
304 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700305 mDialogFragment.dismiss();
306 }
307 mDialogFragment = null;
308 }
309
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800310 /**
311 * Sets the OnCancelListener of the dialog shown. This method can only be
312 * called after showDialog(int) and before removeDialog(int). The method
313 * does nothing otherwise.
314 */
315 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
316 if (mDialogFragment != null) {
317 mDialogFragment.mOnCancelListener = listener;
318 }
319 }
320
321 /**
322 * Sets the OnDismissListener of the dialog shown. This method can only be
323 * called after showDialog(int) and before removeDialog(int). The method
324 * does nothing otherwise.
325 */
326 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
327 if (mDialogFragment != null) {
328 mDialogFragment.mOnDismissListener = listener;
329 }
330 }
331
Amith Yamasanic861cf82012-10-02 14:51:46 -0700332 public void onDialogShowing() {
333 // override in subclass to attach a dismiss listener, for instance
334 }
335
Amith Yamasani43c69782010-12-01 09:04:36 -0800336 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800337 private static final String KEY_DIALOG_ID = "key_dialog_id";
338 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
339
Amith Yamasanid7993472010-08-18 13:59:28 -0700340 private int mDialogId;
341
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800342 private Fragment mParentFragment;
343
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800344 private DialogInterface.OnCancelListener mOnCancelListener;
345 private DialogInterface.OnDismissListener mOnDismissListener;
346
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800347 public SettingsDialogFragment() {
348 /* do nothing */
349 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700350
Amith Yamasani43c69782010-12-01 09:04:36 -0800351 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700352 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800353 if (!(fragment instanceof Fragment)) {
354 throw new IllegalArgumentException("fragment argument must be an instance of "
355 + Fragment.class.getName());
356 }
357 mParentFragment = (Fragment) fragment;
358 }
359
360 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800361 public void onSaveInstanceState(Bundle outState) {
362 super.onSaveInstanceState(outState);
363 if (mParentFragment != null) {
364 outState.putInt(KEY_DIALOG_ID, mDialogId);
365 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
366 }
367 }
368
369 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700370 public void onStart() {
371 super.onStart();
372
373 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
374 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
375 }
376 }
377
378 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800379 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800380 if (savedInstanceState != null) {
381 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800382 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800383 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800384 if (!(mParentFragment instanceof DialogCreatable)) {
385 throw new IllegalArgumentException(
386 (mParentFragment != null
387 ? mParentFragment.getClass().getName()
388 : mParentFragmentId)
389 + " must implement "
390 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800391 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800392 // This dialog fragment could be created from non-SettingsPreferenceFragment
393 if (mParentFragment instanceof SettingsPreferenceFragment) {
394 // restore mDialogFragment in mParentFragment
395 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
396 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800397 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800398 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700399 }
400
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800401 @Override
402 public void onCancel(DialogInterface dialog) {
403 super.onCancel(dialog);
404 if (mOnCancelListener != null) {
405 mOnCancelListener.onCancel(dialog);
406 }
407 }
408
409 @Override
410 public void onDismiss(DialogInterface dialog) {
411 super.onDismiss(dialog);
412 if (mOnDismissListener != null) {
413 mOnDismissListener.onDismiss(dialog);
414 }
415 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800416
Amith Yamasanid7993472010-08-18 13:59:28 -0700417 public int getDialogId() {
418 return mDialogId;
419 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800420
421 @Override
422 public void onDetach() {
423 super.onDetach();
424
Amith Yamasani8875ede2011-01-31 12:46:57 -0800425 // This dialog fragment could be created from non-SettingsPreferenceFragment
426 if (mParentFragment instanceof SettingsPreferenceFragment) {
427 // in case the dialog is not explicitly removed by removeDialog()
428 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
429 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
430 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800431 }
432 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700433 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700434
435 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800436 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700437 }
438
439 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800440 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700441 }
442
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700443 public void finish() {
444 getActivity().onBackPressed();
445 }
446
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700447 public boolean startFragment(
448 Fragment caller, String fragmentClass, int requestCode, Bundle extras) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800449 if (getActivity() instanceof SettingsActivity) {
450 SettingsActivity sa = (SettingsActivity) getActivity();
451 sa.startPreferencePanel(fragmentClass, extras,
Gilles Debunne64650542011-08-23 11:01:35 -0700452 R.string.lock_settings_picker_title, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700453 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700454 } else {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800455 Log.w(TAG, "Parent isn't Settings activity, thus there's no way to launch the "
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700456 + "given Fragment (name: " + fragmentClass + ", requestCode: " + requestCode
457 + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700458 return false;
459 }
460 }
461
Amith Yamasanid7993472010-08-18 13:59:28 -0700462}