blob: e27c9ed00dbf3fce2aa2014e9c3bed19bed58c33 [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();
Fabrice Di Meglio5035ec62014-04-16 18:26:19 -0700141 if (adapter != null) {
142 final int count = adapter.getCount();
143 for (int n = 0; n < count; n++) {
144 Object item = adapter.getItem(n);
145 if (item instanceof Preference) {
146 Preference preference = (Preference) item;
147 final String preferenceKey = preference.getKey();
148 if (preferenceKey != null && preferenceKey.equals(key)) {
149 return n;
150 }
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700151 }
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700152 }
153 }
154 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700155 }
156
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700157 protected void removePreference(String key) {
158 Preference pref = findPreference(key);
159 if (pref != null) {
160 getPreferenceScreen().removePreference(pref);
161 }
162 }
163
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700164 /**
165 * Override this if you want to show a help item in the menu, by returning the resource id.
166 * @return the resource id for the help url
167 */
168 protected int getHelpResource() {
169 return 0;
170 }
171
172 @Override
173 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800174 if (mHelpUrl != null && getActivity() != null) {
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700175 MenuItem helpItem = menu.add(0, MENU_HELP, 0, R.string.help_label);
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800176 HelpUtils.prepareHelpMenuItem(getActivity(), helpItem, mHelpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700177 }
178 }
179
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700180 /*
181 * The name is intentionally made different from Activity#finish(), so that
182 * users won't misunderstand its meaning.
183 */
184 public final void finishFragment() {
185 getActivity().onBackPressed();
186 }
187
Amith Yamasanid7993472010-08-18 13:59:28 -0700188 // Some helpers for functions used by the settings fragments when they were activities
189
190 /**
191 * Returns the ContentResolver from the owning Activity.
192 */
193 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700194 Context context = getActivity();
195 if (context != null) {
196 mContentResolver = context.getContentResolver();
197 }
198 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700199 }
200
201 /**
202 * Returns the specified system service from the owning Activity.
203 */
204 protected Object getSystemService(final String name) {
205 return getActivity().getSystemService(name);
206 }
207
208 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700209 * Returns the PackageManager from the owning Activity.
210 */
211 protected PackageManager getPackageManager() {
212 return getActivity().getPackageManager();
213 }
214
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800215 @Override
216 public void onDetach() {
217 if (isRemoving()) {
218 if (mDialogFragment != null) {
219 mDialogFragment.dismiss();
220 mDialogFragment = null;
221 }
222 }
223 super.onDetach();
224 }
225
Amith Yamasanid7993472010-08-18 13:59:28 -0700226 // Dialog management
227
228 protected void showDialog(int dialogId) {
229 if (mDialogFragment != null) {
230 Log.e(TAG, "Old dialog fragment not null!");
231 }
232 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800233 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700234 }
235
236 public Dialog onCreateDialog(int dialogId) {
237 return null;
238 }
239
240 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800241 // mDialogFragment may not be visible yet in parent fragment's onResume().
242 // To be able to dismiss dialog at that time, don't check
243 // mDialogFragment.isVisible().
244 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700245 mDialogFragment.dismiss();
246 }
247 mDialogFragment = null;
248 }
249
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800250 /**
251 * Sets the OnCancelListener of the dialog shown. This method can only be
252 * called after showDialog(int) and before removeDialog(int). The method
253 * does nothing otherwise.
254 */
255 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
256 if (mDialogFragment != null) {
257 mDialogFragment.mOnCancelListener = listener;
258 }
259 }
260
261 /**
262 * Sets the OnDismissListener of the dialog shown. This method can only be
263 * called after showDialog(int) and before removeDialog(int). The method
264 * does nothing otherwise.
265 */
266 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
267 if (mDialogFragment != null) {
268 mDialogFragment.mOnDismissListener = listener;
269 }
270 }
271
Amith Yamasanic861cf82012-10-02 14:51:46 -0700272 public void onDialogShowing() {
273 // override in subclass to attach a dismiss listener, for instance
274 }
275
Amith Yamasani43c69782010-12-01 09:04:36 -0800276 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800277 private static final String KEY_DIALOG_ID = "key_dialog_id";
278 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
279
Amith Yamasanid7993472010-08-18 13:59:28 -0700280 private int mDialogId;
281
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800282 private Fragment mParentFragment;
283
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800284 private DialogInterface.OnCancelListener mOnCancelListener;
285 private DialogInterface.OnDismissListener mOnDismissListener;
286
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800287 public SettingsDialogFragment() {
288 /* do nothing */
289 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700290
Amith Yamasani43c69782010-12-01 09:04:36 -0800291 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700292 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800293 if (!(fragment instanceof Fragment)) {
294 throw new IllegalArgumentException("fragment argument must be an instance of "
295 + Fragment.class.getName());
296 }
297 mParentFragment = (Fragment) fragment;
298 }
299
300 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800301 public void onSaveInstanceState(Bundle outState) {
302 super.onSaveInstanceState(outState);
303 if (mParentFragment != null) {
304 outState.putInt(KEY_DIALOG_ID, mDialogId);
305 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
306 }
307 }
308
309 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700310 public void onStart() {
311 super.onStart();
312
313 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
314 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
315 }
316 }
317
318 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800319 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800320 if (savedInstanceState != null) {
321 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800322 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800323 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800324 if (!(mParentFragment instanceof DialogCreatable)) {
325 throw new IllegalArgumentException(
326 (mParentFragment != null
327 ? mParentFragment.getClass().getName()
328 : mParentFragmentId)
329 + " must implement "
330 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800331 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800332 // This dialog fragment could be created from non-SettingsPreferenceFragment
333 if (mParentFragment instanceof SettingsPreferenceFragment) {
334 // restore mDialogFragment in mParentFragment
335 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
336 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800337 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800338 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700339 }
340
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800341 @Override
342 public void onCancel(DialogInterface dialog) {
343 super.onCancel(dialog);
344 if (mOnCancelListener != null) {
345 mOnCancelListener.onCancel(dialog);
346 }
347 }
348
349 @Override
350 public void onDismiss(DialogInterface dialog) {
351 super.onDismiss(dialog);
352 if (mOnDismissListener != null) {
353 mOnDismissListener.onDismiss(dialog);
354 }
355 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800356
Amith Yamasanid7993472010-08-18 13:59:28 -0700357 public int getDialogId() {
358 return mDialogId;
359 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800360
361 @Override
362 public void onDetach() {
363 super.onDetach();
364
Amith Yamasani8875ede2011-01-31 12:46:57 -0800365 // This dialog fragment could be created from non-SettingsPreferenceFragment
366 if (mParentFragment instanceof SettingsPreferenceFragment) {
367 // in case the dialog is not explicitly removed by removeDialog()
368 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
369 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
370 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800371 }
372 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700373 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700374
375 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800376 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700377 }
378
379 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800380 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700381 }
382
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700383 public void finish() {
384 getActivity().onBackPressed();
385 }
386
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700387 public boolean startFragment(
388 Fragment caller, String fragmentClass, int requestCode, Bundle extras) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800389 if (getActivity() instanceof SettingsActivity) {
390 SettingsActivity sa = (SettingsActivity) getActivity();
391 sa.startPreferencePanel(fragmentClass, extras,
Gilles Debunne64650542011-08-23 11:01:35 -0700392 R.string.lock_settings_picker_title, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700393 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700394 } else {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800395 Log.w(TAG, "Parent isn't Settings activity, thus there's no way to launch the "
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700396 + "given Fragment (name: " + fragmentClass + ", requestCode: " + requestCode
397 + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700398 return false;
399 }
400 }
401
Amith Yamasanid7993472010-08-18 13:59:28 -0700402}