blob: 2572cef6ece5ad80cdc6658412f8f5c85a079ded [file] [log] [blame]
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -08001/*
2 * Copyright (C) 2008 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
Gilles Debunnecd8e5242011-07-25 11:36:15 -070019import com.android.settings.accounts.AccountSyncSettings;
20import com.android.settings.bluetooth.BluetoothEnabler;
21import com.android.settings.fuelgauge.PowerUsageSummary;
22import com.android.settings.wifi.WifiEnabler;
23
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080024import android.content.ComponentName;
Gilles Debunnee78c1872011-06-20 15:00:07 -070025import android.content.Context;
Amith Yamasani379d9b02010-09-27 12:03:59 -070026import android.content.Intent;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070027import android.content.pm.ActivityInfo;
28import android.content.pm.PackageManager;
29import android.content.pm.PackageManager.NameNotFoundException;
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -070030import android.os.Bundle;
Amith Yamasania4379d62011-07-22 10:34:58 -070031import android.preference.Preference;
Amith Yamasani02cf71a2010-09-21 15:48:52 -070032import android.preference.PreferenceActivity;
Amith Yamasania4379d62011-07-22 10:34:58 -070033import android.preference.PreferenceFragment;
Gilles Debunnee78c1872011-06-20 15:00:07 -070034import android.text.TextUtils;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080035import android.util.Log;
Gilles Debunnee78c1872011-06-20 15:00:07 -070036import android.view.LayoutInflater;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080037import android.view.View;
38import android.view.View.OnClickListener;
Gilles Debunnee78c1872011-06-20 15:00:07 -070039import android.view.ViewGroup;
40import android.widget.ArrayAdapter;
Amith Yamasani9e3a4702011-01-11 09:09:26 -080041import android.widget.Button;
Gilles Debunnee78c1872011-06-20 15:00:07 -070042import android.widget.ImageView;
43import android.widget.ListAdapter;
44import android.widget.Switch;
45import android.widget.TextView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080046
Gilles Debunnee78c1872011-06-20 15:00:07 -070047import java.util.ArrayList;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070048import java.util.HashMap;
Amith Yamasani02cf71a2010-09-21 15:48:52 -070049import java.util.List;
Amith Yamasanid7993472010-08-18 13:59:28 -070050
51/**
52 * Top-level settings activity to handle single pane and double pane UI layout.
53 */
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -080054public class Settings extends PreferenceActivity implements ButtonBarHandler {
Amith Yamasanid7993472010-08-18 13:59:28 -070055
Gilles Debunnee78c1872011-06-20 15:00:07 -070056 private static final String LOG_TAG = "Settings";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070057 private static final String META_DATA_KEY_HEADER_ID =
Gilles Debunnee78c1872011-06-20 15:00:07 -070058 "com.android.settings.TOP_LEVEL_HEADER_ID";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070059 private static final String META_DATA_KEY_FRAGMENT_CLASS =
Gilles Debunnee78c1872011-06-20 15:00:07 -070060 "com.android.settings.FRAGMENT_CLASS";
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080061 private static final String META_DATA_KEY_PARENT_TITLE =
62 "com.android.settings.PARENT_FRAGMENT_TITLE";
63 private static final String META_DATA_KEY_PARENT_FRAGMENT_CLASS =
64 "com.android.settings.PARENT_FRAGMENT_CLASS";
65
Jeff Sharkey54d0af52011-08-11 18:26:57 -070066 private static final String EXTRA_CLEAR_UI_OPTIONS = "settings:remove_ui_options";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070067
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080068 private static final String SAVE_KEY_CURRENT_HEADER = "com.android.settings.CURRENT_HEADER";
69 private static final String SAVE_KEY_PARENT_HEADER = "com.android.settings.PARENT_HEADER";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070070
71 private String mFragmentClass;
72 private int mTopLevelHeaderId;
Amith Yamasani3965ae62010-11-15 14:45:19 -080073 private Header mFirstHeader;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080074 private Header mCurrentHeader;
75 private Header mParentHeader;
76 private boolean mInLocalHeaderSwitch;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070077
Amith Yamasani02cf71a2010-09-21 15:48:52 -070078 // TODO: Update Call Settings based on airplane mode state.
Amith Yamasanib61cf512010-09-12 08:17:50 -070079
Amith Yamasani5203bdf2010-11-04 09:59:44 -070080 protected HashMap<Integer, Integer> mHeaderIndexMap = new HashMap<Integer, Integer>();
Gilles Debunnee78c1872011-06-20 15:00:07 -070081 private List<Header> mHeaders;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070082
83 @Override
84 protected void onCreate(Bundle savedInstanceState) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -070085 if (getIntent().getBooleanExtra(EXTRA_CLEAR_UI_OPTIONS, false)) {
86 getWindow().setUiOptions(0);
87 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070088
Amith Yamasani5203bdf2010-11-04 09:59:44 -070089 getMetaData();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080090 mInLocalHeaderSwitch = true;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070091 super.onCreate(savedInstanceState);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080092 mInLocalHeaderSwitch = false;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070093
Gilles Debunne3661b622011-06-27 11:19:16 -070094 if (!onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasani72aa19d2010-12-09 06:07:12 -080095 highlightHeader();
96 // Force the title so that it doesn't get overridden by a direct launch of
97 // a specific settings screen.
98 setTitle(R.string.settings_label);
99 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800100
101 // Retrieve any saved state
102 if (savedInstanceState != null) {
103 mCurrentHeader = savedInstanceState.getParcelable(SAVE_KEY_CURRENT_HEADER);
104 mParentHeader = savedInstanceState.getParcelable(SAVE_KEY_PARENT_HEADER);
105 }
106
107 // If the current header was saved, switch to it
108 if (savedInstanceState != null && mCurrentHeader != null) {
109 //switchToHeaderLocal(mCurrentHeader);
110 showBreadCrumbs(mCurrentHeader.title, null);
111 }
112
113 if (mParentHeader != null) {
114 setParentTitle(mParentHeader.title, null, new OnClickListener() {
115 public void onClick(View v) {
116 switchToParent(mParentHeader.fragment);
117 }
118 });
119 }
Gilles Debunnedc7101f2011-06-27 12:00:49 -0700120
121 // TODO Add support for android.R.id.home in all Setting's onOptionsItemSelected
122 // getActionBar().setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP,
123 // ActionBar.DISPLAY_HOME_AS_UP);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800124 }
125
126 @Override
127 protected void onSaveInstanceState(Bundle outState) {
128 super.onSaveInstanceState(outState);
129
130 // Save the current fragment, if it is the same as originally launched
131 if (mCurrentHeader != null) {
132 outState.putParcelable(SAVE_KEY_CURRENT_HEADER, mCurrentHeader);
133 }
134 if (mParentHeader != null) {
135 outState.putParcelable(SAVE_KEY_PARENT_HEADER, mParentHeader);
136 }
137 }
138
Gilles Debunnee78c1872011-06-20 15:00:07 -0700139 @Override
140 public void onResume() {
141 super.onResume();
142
143 ListAdapter listAdapter = getListAdapter();
144 if (listAdapter instanceof HeaderAdapter) {
145 ((HeaderAdapter) listAdapter).resume();
146 }
147 }
148
149 @Override
150 public void onPause() {
151 super.onPause();
152
153 ListAdapter listAdapter = getListAdapter();
154 if (listAdapter instanceof HeaderAdapter) {
155 ((HeaderAdapter) listAdapter).pause();
156 }
157 }
158
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800159 private void switchToHeaderLocal(Header header) {
160 mInLocalHeaderSwitch = true;
161 switchToHeader(header);
162 mInLocalHeaderSwitch = false;
163 }
164
165 @Override
166 public void switchToHeader(Header header) {
167 if (!mInLocalHeaderSwitch) {
168 mCurrentHeader = null;
169 mParentHeader = null;
170 }
171 super.switchToHeader(header);
172 }
173
174 /**
175 * Switch to parent fragment and store the grand parent's info
Jake Hamby2748fc22011-01-12 15:06:28 -0800176 * @param className name of the activity wrapper for the parent fragment.
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800177 */
178 private void switchToParent(String className) {
179 final ComponentName cn = new ComponentName(this, className);
180 try {
181 final PackageManager pm = getPackageManager();
182 final ActivityInfo parentInfo = pm.getActivityInfo(cn, PackageManager.GET_META_DATA);
183
184 if (parentInfo != null && parentInfo.metaData != null) {
185 String fragmentClass = parentInfo.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
186 CharSequence fragmentTitle = parentInfo.loadLabel(pm);
187 Header parentHeader = new Header();
188 parentHeader.fragment = fragmentClass;
189 parentHeader.title = fragmentTitle;
190 mCurrentHeader = parentHeader;
191
192 switchToHeaderLocal(parentHeader);
193
194 mParentHeader = new Header();
195 mParentHeader.fragment
196 = parentInfo.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
197 mParentHeader.title = parentInfo.metaData.getString(META_DATA_KEY_PARENT_TITLE);
198 }
199 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700200 Log.w(LOG_TAG, "Could not find parent activity : " + className);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800201 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700202 }
203
Amith Yamasani3965ae62010-11-15 14:45:19 -0800204 @Override
205 public void onNewIntent(Intent intent) {
206 super.onNewIntent(intent);
207
208 // If it is not launched from history, then reset to top-level
209 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0
Gilles Debunne3661b622011-06-27 11:19:16 -0700210 && mFirstHeader != null && !onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800211 switchToHeaderLocal(mFirstHeader);
Amith Yamasani3965ae62010-11-15 14:45:19 -0800212 }
213 }
214
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700215 private void highlightHeader() {
216 if (mTopLevelHeaderId != 0) {
217 Integer index = mHeaderIndexMap.get(mTopLevelHeaderId);
218 if (index != null) {
219 getListView().setItemChecked(index, true);
220 }
221 }
222 }
223
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700224 @Override
225 public Intent getIntent() {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700226 Intent superIntent = super.getIntent();
227 String startingFragment = getStartingFragmentClass(superIntent);
Gilles Debunne3661b622011-06-27 11:19:16 -0700228 // This is called from super.onCreate, isMultiPane() is not yet reliable
229 // Do not use onIsHidingHeaders either, which relies itself on this method
230 if (startingFragment != null && !onIsMultiPane()) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700231 Intent modIntent = new Intent(superIntent);
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700232 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700233 Bundle args = superIntent.getExtras();
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700234 if (args != null) {
235 args = new Bundle(args);
236 } else {
237 args = new Bundle();
238 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700239 args.putParcelable("intent", superIntent);
240 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras());
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700241 return modIntent;
242 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700243 return superIntent;
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700244 }
245
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700246 /**
Amith Yamasani379d9b02010-09-27 12:03:59 -0700247 * Checks if the component name in the intent is different from the Settings class and
248 * returns the class name to load as a fragment.
249 */
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700250 protected String getStartingFragmentClass(Intent intent) {
251 if (mFragmentClass != null) return mFragmentClass;
252
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700253 String intentClass = intent.getComponent().getClassName();
Amith Yamasani379d9b02010-09-27 12:03:59 -0700254 if (intentClass.equals(getClass().getName())) return null;
255
Dianne Hackbornee293792010-11-01 12:32:33 -0700256 if ("com.android.settings.ManageApplications".equals(intentClass)
257 || "com.android.settings.RunningServices".equals(intentClass)
258 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700259 // Old names of manage apps.
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700260 intentClass = com.android.settings.applications.ManageApplications.class.getName();
261 }
262
Amith Yamasani379d9b02010-09-27 12:03:59 -0700263 return intentClass;
264 }
265
266 /**
267 * Override initial header when an activity-alias is causing Settings to be launched
268 * for a specific fragment encoded in the android:name parameter.
269 */
270 @Override
271 public Header onGetInitialHeader() {
272 String fragmentClass = getStartingFragmentClass(super.getIntent());
273 if (fragmentClass != null) {
274 Header header = new Header();
275 header.fragment = fragmentClass;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800276 header.title = getTitle();
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700277 header.fragmentArguments = getIntent().getExtras();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800278 mCurrentHeader = header;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700279 return header;
280 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700281
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700282 return mFirstHeader;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700283 }
284
Dianne Hackbornb7258182011-03-15 16:23:55 -0700285 @Override
Dianne Hackborn48147dc2011-03-18 12:29:41 -0700286 public Intent onBuildStartFragmentIntent(String fragmentName, Bundle args,
287 int titleRes, int shortTitleRes) {
288 Intent intent = super.onBuildStartFragmentIntent(fragmentName, args,
289 titleRes, shortTitleRes);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700290
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700291 // some fragments want to avoid split actionbar
Gilles Debunne162e5412011-07-11 14:13:31 -0700292 if (DataUsageSummary.class.getName().equals(fragmentName) ||
293 PowerUsageSummary.class.getName().equals(fragmentName) ||
Gilles Debunnecd8e5242011-07-25 11:36:15 -0700294 AccountSyncSettings.class.getName().equals(fragmentName) ||
295 UserDictionarySettings.class.getName().equals(fragmentName)) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700296 intent.putExtra(EXTRA_CLEAR_UI_OPTIONS, true);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700297 }
298
Dianne Hackbornb7258182011-03-15 16:23:55 -0700299 intent.setClass(this, SubSettings.class);
300 return intent;
301 }
302
Amith Yamasani379d9b02010-09-27 12:03:59 -0700303 /**
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700304 * Populate the activity with the top-level headers.
305 */
Amith Yamasanid7993472010-08-18 13:59:28 -0700306 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700307 public void onBuildHeaders(List<Header> headers) {
308 loadHeadersFromResource(R.xml.settings_headers, headers);
Amith Yamasanid7993472010-08-18 13:59:28 -0700309
Gilles Debunnee78c1872011-06-20 15:00:07 -0700310 updateHeaderList(headers);
311
312 mHeaders = headers;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700313 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700314
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700315 private void updateHeaderList(List<Header> target) {
316 int i = 0;
317 while (i < target.size()) {
318 Header header = target.get(i);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700319 // Ids are integers, so downcasting
320 int id = (int) header.id;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700321 if (id == R.id.dock_settings) {
322 if (!needsDockSettings())
323 target.remove(header);
324 } else if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
325 Utils.updateHeaderToSpecificActivityFromMetaDataOrRemove(this, target, header);
Gilles Debunne2454f492011-06-21 16:16:06 -0700326 } else if (id == R.id.wifi_settings) {
327 // Remove WiFi Settings if WiFi service is not available.
328 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
329 target.remove(header);
330 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700331 } else if (id == R.id.bluetooth_settings) {
332 // Remove Bluetooth Settings if Bluetooth service is not available.
Gilles Debunne2454f492011-06-21 16:16:06 -0700333 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700334 target.remove(header);
335 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700336 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700337
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700338 // Increment if the current one wasn't removed by the Utils code.
339 if (target.get(i) == header) {
Amith Yamasani3965ae62010-11-15 14:45:19 -0800340 // Hold on to the first header, when we need to reset to the top-level
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700341 if (mFirstHeader == null &&
342 HeaderAdapter.getHeaderType(header) != HeaderAdapter.HEADER_TYPE_CATEGORY) {
343 mFirstHeader = header;
344 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700345 mHeaderIndexMap.put(id, i);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700346 i++;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700347 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700348 }
349 }
350
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700351 private boolean needsDockSettings() {
352 return getResources().getBoolean(R.bool.has_dock_settings);
Amith Yamasanib61cf512010-09-12 08:17:50 -0700353 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700354
355 private void getMetaData() {
356 try {
357 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
358 PackageManager.GET_META_DATA);
359 if (ai == null || ai.metaData == null) return;
360 mTopLevelHeaderId = ai.metaData.getInt(META_DATA_KEY_HEADER_ID);
361 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800362
363 // Check if it has a parent specified and create a Header object
364 final int parentHeaderTitleRes = ai.metaData.getInt(META_DATA_KEY_PARENT_TITLE);
365 String parentFragmentClass = ai.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
366 if (parentFragmentClass != null) {
367 mParentHeader = new Header();
368 mParentHeader.fragment = parentFragmentClass;
369 if (parentHeaderTitleRes != 0) {
370 mParentHeader.title = getResources().getString(parentHeaderTitleRes);
371 }
372 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700373 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700374 // No recovery
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700375 }
376 }
377
Amith Yamasani9e3a4702011-01-11 09:09:26 -0800378 @Override
379 public boolean hasNextButton() {
380 return super.hasNextButton();
381 }
382
383 @Override
384 public Button getNextButton() {
385 return super.getNextButton();
386 }
387
Gilles Debunnee78c1872011-06-20 15:00:07 -0700388 private static class HeaderAdapter extends ArrayAdapter<Header> {
389 static final int HEADER_TYPE_CATEGORY = 0;
390 static final int HEADER_TYPE_NORMAL = 1;
391 static final int HEADER_TYPE_SWITCH = 2;
392 private static final int HEADER_TYPE_COUNT = HEADER_TYPE_SWITCH + 1;
393
394 private final WifiEnabler mWifiEnabler;
395 private final BluetoothEnabler mBluetoothEnabler;
396
397 private static class HeaderViewHolder {
398 ImageView icon;
399 TextView title;
400 TextView summary;
401 Switch switch_;
402 }
403
404 private LayoutInflater mInflater;
405
406 static int getHeaderType(Header header) {
407 if (header.fragment == null && header.intent == null) {
408 return HEADER_TYPE_CATEGORY;
409 } else if (header.id == R.id.wifi_settings || header.id == R.id.bluetooth_settings) {
410 return HEADER_TYPE_SWITCH;
411 } else {
412 return HEADER_TYPE_NORMAL;
413 }
414 }
415
416 @Override
417 public int getItemViewType(int position) {
418 Header header = getItem(position);
419 return getHeaderType(header);
420 }
421
422 @Override
423 public boolean areAllItemsEnabled() {
424 return false; // because of categories
425 }
426
427 @Override
428 public boolean isEnabled(int position) {
429 return getItemViewType(position) != HEADER_TYPE_CATEGORY;
430 }
431
432 @Override
433 public int getViewTypeCount() {
434 return HEADER_TYPE_COUNT;
435 }
436
437 @Override
438 public boolean hasStableIds() {
439 return true;
440 }
441
442 public HeaderAdapter(Context context, List<Header> objects) {
443 super(context, 0, objects);
444 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
445
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700446 // Temp Switches provided as placeholder until the adapter replaces these with actual
Gilles Debunnee78c1872011-06-20 15:00:07 -0700447 // Switches inflated from their layouts. Must be done before adapter is set in super
448 mWifiEnabler = new WifiEnabler(context, new Switch(context));
449 mBluetoothEnabler = new BluetoothEnabler(context, new Switch(context));
450 }
451
452 @Override
453 public View getView(int position, View convertView, ViewGroup parent) {
454 HeaderViewHolder holder;
455 Header header = getItem(position);
456 int headerType = getHeaderType(header);
457 View view = null;
458
459 if (convertView == null) {
460 holder = new HeaderViewHolder();
461 switch (headerType) {
462 case HEADER_TYPE_CATEGORY:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700463 view = new TextView(getContext(), null,
464 android.R.attr.listSeparatorTextViewStyle);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700465 holder.title = (TextView) view;
466 break;
467
468 case HEADER_TYPE_SWITCH:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700469 view = mInflater.inflate(R.layout.preference_header_switch_item, parent,
470 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700471 holder.icon = (ImageView) view.findViewById(R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700472 holder.title = (TextView)
473 view.findViewById(com.android.internal.R.id.title);
474 holder.summary = (TextView)
475 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700476 holder.switch_ = (Switch) view.findViewById(R.id.switchWidget);
477 break;
478
479 case HEADER_TYPE_NORMAL:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700480 view = mInflater.inflate(
481 com.android.internal.R.layout.preference_header_item, parent,
482 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700483 holder.icon = (ImageView) view.findViewById(com.android.internal.R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700484 holder.title = (TextView)
485 view.findViewById(com.android.internal.R.id.title);
486 holder.summary = (TextView)
487 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700488 break;
489 }
490 view.setTag(holder);
491 } else {
492 view = convertView;
493 holder = (HeaderViewHolder) view.getTag();
494 }
495
496 // All view fields must be updated every time, because the view may be recycled
497 switch (headerType) {
498 case HEADER_TYPE_CATEGORY:
499 holder.title.setText(header.getTitle(getContext().getResources()));
500 break;
501
502 case HEADER_TYPE_SWITCH:
503 // Would need a different treatment if the main menu had more switches
504 if (header.id == R.id.wifi_settings) {
505 mWifiEnabler.setSwitch(holder.switch_);
506 } else {
507 mBluetoothEnabler.setSwitch(holder.switch_);
508 }
509 // No break, fall through on purpose to update common fields
510
511 //$FALL-THROUGH$
512 case HEADER_TYPE_NORMAL:
513 holder.icon.setImageResource(header.iconRes);
514 holder.title.setText(header.getTitle(getContext().getResources()));
515 CharSequence summary = header.getSummary(getContext().getResources());
516 if (!TextUtils.isEmpty(summary)) {
517 holder.summary.setVisibility(View.VISIBLE);
518 holder.summary.setText(summary);
519 } else {
520 holder.summary.setVisibility(View.GONE);
521 }
522 break;
523 }
524
525 return view;
526 }
527
528 public void resume() {
529 mWifiEnabler.resume();
530 mBluetoothEnabler.resume();
531 }
532
533 public void pause() {
534 mWifiEnabler.pause();
535 mBluetoothEnabler.pause();
536 }
537 }
538
539 @Override
Amith Yamasania4379d62011-07-22 10:34:58 -0700540 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
541 // Override the fragment title for Wallpaper settings
542 CharSequence title = pref.getTitle();
543 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
544 title = getString(R.string.wallpaper_settings_fragment_title);
545 }
546 startPreferencePanel(pref.getFragment(), pref.getExtras(), 0, title, null, 0);
547 return true;
548 }
549
550 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700551 public void setListAdapter(ListAdapter adapter) {
552 if (mHeaders == null) {
553 mHeaders = new ArrayList<Header>();
554 // When the saved state provides the list of headers, onBuildHeaders is not called
555 // Copy the list of Headers from the adapter, preserving their order
556 for (int i = 0; i < adapter.getCount(); i++) {
557 mHeaders.add((Header) adapter.getItem(i));
558 }
559 }
560
561 // Ignore the adapter provided by PreferenceActivity and substitute ours instead
562 super.setListAdapter(new HeaderAdapter(this, mHeaders));
563 }
564
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700565 /*
566 * Settings subclasses for launching independently.
567 */
Gilles Debunnee78c1872011-06-20 15:00:07 -0700568 public static class BluetoothSettingsActivity extends Settings { /* empty */ }
569 public static class WirelessSettingsActivity extends Settings { /* empty */ }
570 public static class TetherSettingsActivity extends Settings { /* empty */ }
571 public static class VpnSettingsActivity extends Settings { /* empty */ }
572 public static class DateTimeSettingsActivity extends Settings { /* empty */ }
573 public static class StorageSettingsActivity extends Settings { /* empty */ }
574 public static class WifiSettingsActivity extends Settings { /* empty */ }
repo syncb98463f2011-06-30 10:58:43 -0700575 public static class WifiP2pSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700576 public static class InputMethodAndLanguageSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700577 public static class InputMethodAndSubtypeEnablerActivity extends Settings { /* empty */ }
satoke077d2b2011-07-25 09:38:11 +0900578 public static class SpellCheckersSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700579 public static class LocalePickerActivity extends Settings { /* empty */ }
580 public static class UserDictionarySettingsActivity extends Settings { /* empty */ }
581 public static class SoundSettingsActivity extends Settings { /* empty */ }
582 public static class DisplaySettingsActivity extends Settings { /* empty */ }
583 public static class DeviceInfoSettingsActivity extends Settings { /* empty */ }
584 public static class ApplicationSettingsActivity extends Settings { /* empty */ }
585 public static class ManageApplicationsActivity extends Settings { /* empty */ }
586 public static class StorageUseActivity extends Settings { /* empty */ }
587 public static class DevelopmentSettingsActivity extends Settings { /* empty */ }
588 public static class AccessibilitySettingsActivity extends Settings { /* empty */ }
589 public static class SecuritySettingsActivity extends Settings { /* empty */ }
Gilles Debunnea6a8a142011-06-09 11:56:17 -0700590 public static class LocationSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700591 public static class PrivacySettingsActivity extends Settings { /* empty */ }
592 public static class DockSettingsActivity extends Settings { /* empty */ }
593 public static class RunningServicesActivity extends Settings { /* empty */ }
594 public static class ManageAccountsSettingsActivity extends Settings { /* empty */ }
595 public static class PowerUsageSummaryActivity extends Settings { /* empty */ }
596 public static class AccountSyncSettingsActivity extends Settings { /* empty */ }
597 public static class AccountSyncSettingsInAddAccountActivity extends Settings { /* empty */ }
598 public static class CryptKeeperSettingsActivity extends Settings { /* empty */ }
599 public static class DeviceAdminSettingsActivity extends Settings { /* empty */ }
600 public static class DataUsageSummaryActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700601 public static class AdvancedWifiSettingsActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700602 public static class TextToSpeechSettingsActivity extends Settings { /* empty */ }
Martijn Coenen3ae09142011-07-28 11:27:34 -0500603 public static class NfcSharingSettingsActivity extends Settings { /* empty */ }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800604}