blob: 518d0ee671c95222c5e98013fcb94b0df9e09e66 [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
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080019import android.content.ComponentName;
Gilles Debunnee78c1872011-06-20 15:00:07 -070020import android.content.Context;
Amith Yamasani379d9b02010-09-27 12:03:59 -070021import android.content.Intent;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070022import android.content.pm.ActivityInfo;
23import android.content.pm.PackageManager;
24import android.content.pm.PackageManager.NameNotFoundException;
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -070025import android.os.Bundle;
Amith Yamasani02cf71a2010-09-21 15:48:52 -070026import android.preference.PreferenceActivity;
Gilles Debunnee78c1872011-06-20 15:00:07 -070027import android.text.TextUtils;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080028import android.util.Log;
Gilles Debunnee78c1872011-06-20 15:00:07 -070029import android.view.LayoutInflater;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080030import android.view.View;
31import android.view.View.OnClickListener;
Gilles Debunnee78c1872011-06-20 15:00:07 -070032import android.view.ViewGroup;
33import android.widget.ArrayAdapter;
Amith Yamasani9e3a4702011-01-11 09:09:26 -080034import android.widget.Button;
Gilles Debunnee78c1872011-06-20 15:00:07 -070035import android.widget.ImageView;
36import android.widget.ListAdapter;
37import android.widget.Switch;
38import android.widget.TextView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080039
Gilles Debunnee78c1872011-06-20 15:00:07 -070040import com.android.settings.bluetooth.BluetoothEnabler;
41import com.android.settings.wifi.WifiEnabler;
42
43import java.util.ArrayList;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070044import java.util.HashMap;
Amith Yamasani02cf71a2010-09-21 15:48:52 -070045import java.util.List;
Amith Yamasanid7993472010-08-18 13:59:28 -070046
47/**
48 * Top-level settings activity to handle single pane and double pane UI layout.
49 */
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -080050public class Settings extends PreferenceActivity implements ButtonBarHandler {
Amith Yamasanid7993472010-08-18 13:59:28 -070051
Gilles Debunnee78c1872011-06-20 15:00:07 -070052 private static final String LOG_TAG = "Settings";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070053 private static final String META_DATA_KEY_HEADER_ID =
Gilles Debunnee78c1872011-06-20 15:00:07 -070054 "com.android.settings.TOP_LEVEL_HEADER_ID";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070055 private static final String META_DATA_KEY_FRAGMENT_CLASS =
Gilles Debunnee78c1872011-06-20 15:00:07 -070056 "com.android.settings.FRAGMENT_CLASS";
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080057 private static final String META_DATA_KEY_PARENT_TITLE =
58 "com.android.settings.PARENT_FRAGMENT_TITLE";
59 private static final String META_DATA_KEY_PARENT_FRAGMENT_CLASS =
60 "com.android.settings.PARENT_FRAGMENT_CLASS";
61
62 private static final String SAVE_KEY_CURRENT_HEADER = "com.android.settings.CURRENT_HEADER";
63 private static final String SAVE_KEY_PARENT_HEADER = "com.android.settings.PARENT_HEADER";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070064
65 private String mFragmentClass;
66 private int mTopLevelHeaderId;
Amith Yamasani3965ae62010-11-15 14:45:19 -080067 private Header mFirstHeader;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080068 private Header mCurrentHeader;
69 private Header mParentHeader;
70 private boolean mInLocalHeaderSwitch;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070071
Amith Yamasani02cf71a2010-09-21 15:48:52 -070072 // TODO: Update Call Settings based on airplane mode state.
Amith Yamasanib61cf512010-09-12 08:17:50 -070073
Amith Yamasani5203bdf2010-11-04 09:59:44 -070074 protected HashMap<Integer, Integer> mHeaderIndexMap = new HashMap<Integer, Integer>();
Gilles Debunnee78c1872011-06-20 15:00:07 -070075 private List<Header> mHeaders;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070076
77 @Override
78 protected void onCreate(Bundle savedInstanceState) {
79 getMetaData();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080080 mInLocalHeaderSwitch = true;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070081 super.onCreate(savedInstanceState);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080082 mInLocalHeaderSwitch = false;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070083
Gilles Debunne3661b622011-06-27 11:19:16 -070084 if (!onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasani72aa19d2010-12-09 06:07:12 -080085 highlightHeader();
86 // Force the title so that it doesn't get overridden by a direct launch of
87 // a specific settings screen.
88 setTitle(R.string.settings_label);
89 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080090
91 // Retrieve any saved state
92 if (savedInstanceState != null) {
93 mCurrentHeader = savedInstanceState.getParcelable(SAVE_KEY_CURRENT_HEADER);
94 mParentHeader = savedInstanceState.getParcelable(SAVE_KEY_PARENT_HEADER);
95 }
96
97 // If the current header was saved, switch to it
98 if (savedInstanceState != null && mCurrentHeader != null) {
99 //switchToHeaderLocal(mCurrentHeader);
100 showBreadCrumbs(mCurrentHeader.title, null);
101 }
102
103 if (mParentHeader != null) {
104 setParentTitle(mParentHeader.title, null, new OnClickListener() {
105 public void onClick(View v) {
106 switchToParent(mParentHeader.fragment);
107 }
108 });
109 }
Gilles Debunnedc7101f2011-06-27 12:00:49 -0700110
111 // TODO Add support for android.R.id.home in all Setting's onOptionsItemSelected
112 // getActionBar().setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP,
113 // ActionBar.DISPLAY_HOME_AS_UP);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800114 }
115
116 @Override
117 protected void onSaveInstanceState(Bundle outState) {
118 super.onSaveInstanceState(outState);
119
120 // Save the current fragment, if it is the same as originally launched
121 if (mCurrentHeader != null) {
122 outState.putParcelable(SAVE_KEY_CURRENT_HEADER, mCurrentHeader);
123 }
124 if (mParentHeader != null) {
125 outState.putParcelable(SAVE_KEY_PARENT_HEADER, mParentHeader);
126 }
127 }
128
Gilles Debunnee78c1872011-06-20 15:00:07 -0700129 @Override
130 public void onResume() {
131 super.onResume();
132
133 ListAdapter listAdapter = getListAdapter();
134 if (listAdapter instanceof HeaderAdapter) {
135 ((HeaderAdapter) listAdapter).resume();
136 }
137 }
138
139 @Override
140 public void onPause() {
141 super.onPause();
142
143 ListAdapter listAdapter = getListAdapter();
144 if (listAdapter instanceof HeaderAdapter) {
145 ((HeaderAdapter) listAdapter).pause();
146 }
147 }
148
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800149 private void switchToHeaderLocal(Header header) {
150 mInLocalHeaderSwitch = true;
151 switchToHeader(header);
152 mInLocalHeaderSwitch = false;
153 }
154
155 @Override
156 public void switchToHeader(Header header) {
157 if (!mInLocalHeaderSwitch) {
158 mCurrentHeader = null;
159 mParentHeader = null;
160 }
161 super.switchToHeader(header);
162 }
163
164 /**
165 * Switch to parent fragment and store the grand parent's info
Jake Hamby2748fc22011-01-12 15:06:28 -0800166 * @param className name of the activity wrapper for the parent fragment.
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800167 */
168 private void switchToParent(String className) {
169 final ComponentName cn = new ComponentName(this, className);
170 try {
171 final PackageManager pm = getPackageManager();
172 final ActivityInfo parentInfo = pm.getActivityInfo(cn, PackageManager.GET_META_DATA);
173
174 if (parentInfo != null && parentInfo.metaData != null) {
175 String fragmentClass = parentInfo.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
176 CharSequence fragmentTitle = parentInfo.loadLabel(pm);
177 Header parentHeader = new Header();
178 parentHeader.fragment = fragmentClass;
179 parentHeader.title = fragmentTitle;
180 mCurrentHeader = parentHeader;
181
182 switchToHeaderLocal(parentHeader);
183
184 mParentHeader = new Header();
185 mParentHeader.fragment
186 = parentInfo.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
187 mParentHeader.title = parentInfo.metaData.getString(META_DATA_KEY_PARENT_TITLE);
188 }
189 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700190 Log.w(LOG_TAG, "Could not find parent activity : " + className);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800191 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700192 }
193
Amith Yamasani3965ae62010-11-15 14:45:19 -0800194 @Override
195 public void onNewIntent(Intent intent) {
196 super.onNewIntent(intent);
197
198 // If it is not launched from history, then reset to top-level
199 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0
Gilles Debunne3661b622011-06-27 11:19:16 -0700200 && mFirstHeader != null && !onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800201 switchToHeaderLocal(mFirstHeader);
Amith Yamasani3965ae62010-11-15 14:45:19 -0800202 }
203 }
204
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700205 private void highlightHeader() {
206 if (mTopLevelHeaderId != 0) {
207 Integer index = mHeaderIndexMap.get(mTopLevelHeaderId);
208 if (index != null) {
209 getListView().setItemChecked(index, true);
210 }
211 }
212 }
213
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700214 @Override
215 public Intent getIntent() {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700216 Intent superIntent = super.getIntent();
217 String startingFragment = getStartingFragmentClass(superIntent);
Gilles Debunne3661b622011-06-27 11:19:16 -0700218 // This is called from super.onCreate, isMultiPane() is not yet reliable
219 // Do not use onIsHidingHeaders either, which relies itself on this method
220 if (startingFragment != null && !onIsMultiPane()) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700221 Intent modIntent = new Intent(superIntent);
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700222 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700223 Bundle args = superIntent.getExtras();
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700224 if (args != null) {
225 args = new Bundle(args);
226 } else {
227 args = new Bundle();
228 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700229 args.putParcelable("intent", superIntent);
230 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras());
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700231 return modIntent;
232 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700233 return superIntent;
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700234 }
235
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700236 /**
Amith Yamasani379d9b02010-09-27 12:03:59 -0700237 * Checks if the component name in the intent is different from the Settings class and
238 * returns the class name to load as a fragment.
239 */
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700240 protected String getStartingFragmentClass(Intent intent) {
241 if (mFragmentClass != null) return mFragmentClass;
242
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700243 String intentClass = intent.getComponent().getClassName();
Amith Yamasani379d9b02010-09-27 12:03:59 -0700244 if (intentClass.equals(getClass().getName())) return null;
245
Dianne Hackbornee293792010-11-01 12:32:33 -0700246 if ("com.android.settings.ManageApplications".equals(intentClass)
247 || "com.android.settings.RunningServices".equals(intentClass)
248 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700249 // Old names of manage apps.
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700250 intentClass = com.android.settings.applications.ManageApplications.class.getName();
251 }
252
Amith Yamasani379d9b02010-09-27 12:03:59 -0700253 return intentClass;
254 }
255
256 /**
257 * Override initial header when an activity-alias is causing Settings to be launched
258 * for a specific fragment encoded in the android:name parameter.
259 */
260 @Override
261 public Header onGetInitialHeader() {
262 String fragmentClass = getStartingFragmentClass(super.getIntent());
263 if (fragmentClass != null) {
264 Header header = new Header();
265 header.fragment = fragmentClass;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800266 header.title = getTitle();
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700267 header.fragmentArguments = getIntent().getExtras();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800268 mCurrentHeader = header;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700269 return header;
270 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700271
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700272 return mFirstHeader;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700273 }
274
Dianne Hackbornb7258182011-03-15 16:23:55 -0700275 @Override
Dianne Hackborn48147dc2011-03-18 12:29:41 -0700276 public Intent onBuildStartFragmentIntent(String fragmentName, Bundle args,
277 int titleRes, int shortTitleRes) {
278 Intent intent = super.onBuildStartFragmentIntent(fragmentName, args,
279 titleRes, shortTitleRes);
Dianne Hackbornb7258182011-03-15 16:23:55 -0700280 intent.setClass(this, SubSettings.class);
281 return intent;
282 }
283
Amith Yamasani379d9b02010-09-27 12:03:59 -0700284 /**
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700285 * Populate the activity with the top-level headers.
286 */
Amith Yamasanid7993472010-08-18 13:59:28 -0700287 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700288 public void onBuildHeaders(List<Header> headers) {
289 loadHeadersFromResource(R.xml.settings_headers, headers);
Amith Yamasanid7993472010-08-18 13:59:28 -0700290
Gilles Debunnee78c1872011-06-20 15:00:07 -0700291 updateHeaderList(headers);
292
293 mHeaders = headers;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700294 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700295
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700296 private void updateHeaderList(List<Header> target) {
297 int i = 0;
298 while (i < target.size()) {
299 Header header = target.get(i);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700300 // Ids are integers, so downcasting
301 int id = (int) header.id;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700302 if (id == R.id.dock_settings) {
303 if (!needsDockSettings())
304 target.remove(header);
305 } else if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
306 Utils.updateHeaderToSpecificActivityFromMetaDataOrRemove(this, target, header);
Gilles Debunne2454f492011-06-21 16:16:06 -0700307 } else if (id == R.id.wifi_settings) {
308 // Remove WiFi Settings if WiFi service is not available.
309 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
310 target.remove(header);
311 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700312 } else if (id == R.id.bluetooth_settings) {
313 // Remove Bluetooth Settings if Bluetooth service is not available.
Gilles Debunne2454f492011-06-21 16:16:06 -0700314 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700315 target.remove(header);
316 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700317 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700318
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700319 // Increment if the current one wasn't removed by the Utils code.
320 if (target.get(i) == header) {
Amith Yamasani3965ae62010-11-15 14:45:19 -0800321 // Hold on to the first header, when we need to reset to the top-level
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700322 if (mFirstHeader == null &&
323 HeaderAdapter.getHeaderType(header) != HeaderAdapter.HEADER_TYPE_CATEGORY) {
324 mFirstHeader = header;
325 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700326 mHeaderIndexMap.put(id, i);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700327 i++;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700328 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700329 }
330 }
331
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700332 private boolean needsDockSettings() {
333 return getResources().getBoolean(R.bool.has_dock_settings);
Amith Yamasanib61cf512010-09-12 08:17:50 -0700334 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700335
336 private void getMetaData() {
337 try {
338 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
339 PackageManager.GET_META_DATA);
340 if (ai == null || ai.metaData == null) return;
341 mTopLevelHeaderId = ai.metaData.getInt(META_DATA_KEY_HEADER_ID);
342 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800343
344 // Check if it has a parent specified and create a Header object
345 final int parentHeaderTitleRes = ai.metaData.getInt(META_DATA_KEY_PARENT_TITLE);
346 String parentFragmentClass = ai.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
347 if (parentFragmentClass != null) {
348 mParentHeader = new Header();
349 mParentHeader.fragment = parentFragmentClass;
350 if (parentHeaderTitleRes != 0) {
351 mParentHeader.title = getResources().getString(parentHeaderTitleRes);
352 }
353 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700354 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700355 // No recovery
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700356 }
357 }
358
Amith Yamasani9e3a4702011-01-11 09:09:26 -0800359 @Override
360 public boolean hasNextButton() {
361 return super.hasNextButton();
362 }
363
364 @Override
365 public Button getNextButton() {
366 return super.getNextButton();
367 }
368
Gilles Debunnee78c1872011-06-20 15:00:07 -0700369 private static class HeaderAdapter extends ArrayAdapter<Header> {
370 static final int HEADER_TYPE_CATEGORY = 0;
371 static final int HEADER_TYPE_NORMAL = 1;
372 static final int HEADER_TYPE_SWITCH = 2;
373 private static final int HEADER_TYPE_COUNT = HEADER_TYPE_SWITCH + 1;
374
375 private final WifiEnabler mWifiEnabler;
376 private final BluetoothEnabler mBluetoothEnabler;
377
378 private static class HeaderViewHolder {
379 ImageView icon;
380 TextView title;
381 TextView summary;
382 Switch switch_;
383 }
384
385 private LayoutInflater mInflater;
386
387 static int getHeaderType(Header header) {
388 if (header.fragment == null && header.intent == null) {
389 return HEADER_TYPE_CATEGORY;
390 } else if (header.id == R.id.wifi_settings || header.id == R.id.bluetooth_settings) {
391 return HEADER_TYPE_SWITCH;
392 } else {
393 return HEADER_TYPE_NORMAL;
394 }
395 }
396
397 @Override
398 public int getItemViewType(int position) {
399 Header header = getItem(position);
400 return getHeaderType(header);
401 }
402
403 @Override
404 public boolean areAllItemsEnabled() {
405 return false; // because of categories
406 }
407
408 @Override
409 public boolean isEnabled(int position) {
410 return getItemViewType(position) != HEADER_TYPE_CATEGORY;
411 }
412
413 @Override
414 public int getViewTypeCount() {
415 return HEADER_TYPE_COUNT;
416 }
417
418 @Override
419 public boolean hasStableIds() {
420 return true;
421 }
422
423 public HeaderAdapter(Context context, List<Header> objects) {
424 super(context, 0, objects);
425 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
426
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700427 // Temp Switches provided as placeholder until the adapter replaces these with actual
Gilles Debunnee78c1872011-06-20 15:00:07 -0700428 // Switches inflated from their layouts. Must be done before adapter is set in super
429 mWifiEnabler = new WifiEnabler(context, new Switch(context));
430 mBluetoothEnabler = new BluetoothEnabler(context, new Switch(context));
431 }
432
433 @Override
434 public View getView(int position, View convertView, ViewGroup parent) {
435 HeaderViewHolder holder;
436 Header header = getItem(position);
437 int headerType = getHeaderType(header);
438 View view = null;
439
440 if (convertView == null) {
441 holder = new HeaderViewHolder();
442 switch (headerType) {
443 case HEADER_TYPE_CATEGORY:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700444 view = new TextView(getContext(), null,
445 android.R.attr.listSeparatorTextViewStyle);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700446 holder.title = (TextView) view;
447 break;
448
449 case HEADER_TYPE_SWITCH:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700450 view = mInflater.inflate(R.layout.preference_header_switch_item, parent,
451 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700452 holder.icon = (ImageView) view.findViewById(R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700453 holder.title = (TextView)
454 view.findViewById(com.android.internal.R.id.title);
455 holder.summary = (TextView)
456 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700457 holder.switch_ = (Switch) view.findViewById(R.id.switchWidget);
458 break;
459
460 case HEADER_TYPE_NORMAL:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700461 view = mInflater.inflate(
462 com.android.internal.R.layout.preference_header_item, parent,
463 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700464 holder.icon = (ImageView) view.findViewById(com.android.internal.R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700465 holder.title = (TextView)
466 view.findViewById(com.android.internal.R.id.title);
467 holder.summary = (TextView)
468 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700469 break;
470 }
471 view.setTag(holder);
472 } else {
473 view = convertView;
474 holder = (HeaderViewHolder) view.getTag();
475 }
476
477 // All view fields must be updated every time, because the view may be recycled
478 switch (headerType) {
479 case HEADER_TYPE_CATEGORY:
480 holder.title.setText(header.getTitle(getContext().getResources()));
481 break;
482
483 case HEADER_TYPE_SWITCH:
484 // Would need a different treatment if the main menu had more switches
485 if (header.id == R.id.wifi_settings) {
486 mWifiEnabler.setSwitch(holder.switch_);
487 } else {
488 mBluetoothEnabler.setSwitch(holder.switch_);
489 }
490 // No break, fall through on purpose to update common fields
491
492 //$FALL-THROUGH$
493 case HEADER_TYPE_NORMAL:
494 holder.icon.setImageResource(header.iconRes);
495 holder.title.setText(header.getTitle(getContext().getResources()));
496 CharSequence summary = header.getSummary(getContext().getResources());
497 if (!TextUtils.isEmpty(summary)) {
498 holder.summary.setVisibility(View.VISIBLE);
499 holder.summary.setText(summary);
500 } else {
501 holder.summary.setVisibility(View.GONE);
502 }
503 break;
504 }
505
506 return view;
507 }
508
509 public void resume() {
510 mWifiEnabler.resume();
511 mBluetoothEnabler.resume();
512 }
513
514 public void pause() {
515 mWifiEnabler.pause();
516 mBluetoothEnabler.pause();
517 }
518 }
519
520 @Override
521 public void setListAdapter(ListAdapter adapter) {
522 if (mHeaders == null) {
523 mHeaders = new ArrayList<Header>();
524 // When the saved state provides the list of headers, onBuildHeaders is not called
525 // Copy the list of Headers from the adapter, preserving their order
526 for (int i = 0; i < adapter.getCount(); i++) {
527 mHeaders.add((Header) adapter.getItem(i));
528 }
529 }
530
531 // Ignore the adapter provided by PreferenceActivity and substitute ours instead
532 super.setListAdapter(new HeaderAdapter(this, mHeaders));
533 }
534
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700535 /*
536 * Settings subclasses for launching independently.
537 */
Gilles Debunnee78c1872011-06-20 15:00:07 -0700538 public static class BluetoothSettingsActivity extends Settings { /* empty */ }
539 public static class WirelessSettingsActivity extends Settings { /* empty */ }
540 public static class TetherSettingsActivity extends Settings { /* empty */ }
541 public static class VpnSettingsActivity extends Settings { /* empty */ }
542 public static class DateTimeSettingsActivity extends Settings { /* empty */ }
543 public static class StorageSettingsActivity extends Settings { /* empty */ }
544 public static class WifiSettingsActivity extends Settings { /* empty */ }
545 public static class InputMethodAndLanguageSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700546 public static class InputMethodAndSubtypeEnablerActivity extends Settings { /* empty */ }
547 public static class LocalePickerActivity extends Settings { /* empty */ }
548 public static class UserDictionarySettingsActivity extends Settings { /* empty */ }
549 public static class SoundSettingsActivity extends Settings { /* empty */ }
550 public static class DisplaySettingsActivity extends Settings { /* empty */ }
551 public static class DeviceInfoSettingsActivity extends Settings { /* empty */ }
552 public static class ApplicationSettingsActivity extends Settings { /* empty */ }
553 public static class ManageApplicationsActivity extends Settings { /* empty */ }
554 public static class StorageUseActivity extends Settings { /* empty */ }
555 public static class DevelopmentSettingsActivity extends Settings { /* empty */ }
556 public static class AccessibilitySettingsActivity extends Settings { /* empty */ }
557 public static class SecuritySettingsActivity extends Settings { /* empty */ }
Gilles Debunnea6a8a142011-06-09 11:56:17 -0700558 public static class LocationSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700559 public static class PrivacySettingsActivity extends Settings { /* empty */ }
560 public static class DockSettingsActivity extends Settings { /* empty */ }
561 public static class RunningServicesActivity extends Settings { /* empty */ }
562 public static class ManageAccountsSettingsActivity extends Settings { /* empty */ }
563 public static class PowerUsageSummaryActivity extends Settings { /* empty */ }
564 public static class AccountSyncSettingsActivity extends Settings { /* empty */ }
565 public static class AccountSyncSettingsInAddAccountActivity extends Settings { /* empty */ }
566 public static class CryptKeeperSettingsActivity extends Settings { /* empty */ }
567 public static class DeviceAdminSettingsActivity extends Settings { /* empty */ }
568 public static class DataUsageSummaryActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700569 public static class AdvancedWifiSettingsActivity extends Settings { /* empty */ }
570 public static class AdvancedBluetoothSettingsActivity extends Settings { /* empty */ }
571 public static class TextToSpeechSettingsActivity extends Settings { /* empty */ }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800572}