blob: 15366e846411c107988f5a17ecd1148723f47bc2 [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
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070062 private static final String EXTRA_THEME = "settings:theme";
63
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080064 private static final String SAVE_KEY_CURRENT_HEADER = "com.android.settings.CURRENT_HEADER";
65 private static final String SAVE_KEY_PARENT_HEADER = "com.android.settings.PARENT_HEADER";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070066
67 private String mFragmentClass;
68 private int mTopLevelHeaderId;
Amith Yamasani3965ae62010-11-15 14:45:19 -080069 private Header mFirstHeader;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080070 private Header mCurrentHeader;
71 private Header mParentHeader;
72 private boolean mInLocalHeaderSwitch;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070073
Amith Yamasani02cf71a2010-09-21 15:48:52 -070074 // TODO: Update Call Settings based on airplane mode state.
Amith Yamasanib61cf512010-09-12 08:17:50 -070075
Amith Yamasani5203bdf2010-11-04 09:59:44 -070076 protected HashMap<Integer, Integer> mHeaderIndexMap = new HashMap<Integer, Integer>();
Gilles Debunnee78c1872011-06-20 15:00:07 -070077 private List<Header> mHeaders;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070078
79 @Override
80 protected void onCreate(Bundle savedInstanceState) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070081 final int theme = getIntent().getIntExtra(
82 EXTRA_THEME, android.R.style.Theme_Holo_SolidActionBar_SplitActionBarWhenNarrow);
83 setTheme(theme);
84
Amith Yamasani5203bdf2010-11-04 09:59:44 -070085 getMetaData();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080086 mInLocalHeaderSwitch = true;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070087 super.onCreate(savedInstanceState);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080088 mInLocalHeaderSwitch = false;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070089
Gilles Debunne3661b622011-06-27 11:19:16 -070090 if (!onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasani72aa19d2010-12-09 06:07:12 -080091 highlightHeader();
92 // Force the title so that it doesn't get overridden by a direct launch of
93 // a specific settings screen.
94 setTitle(R.string.settings_label);
95 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080096
97 // Retrieve any saved state
98 if (savedInstanceState != null) {
99 mCurrentHeader = savedInstanceState.getParcelable(SAVE_KEY_CURRENT_HEADER);
100 mParentHeader = savedInstanceState.getParcelable(SAVE_KEY_PARENT_HEADER);
101 }
102
103 // If the current header was saved, switch to it
104 if (savedInstanceState != null && mCurrentHeader != null) {
105 //switchToHeaderLocal(mCurrentHeader);
106 showBreadCrumbs(mCurrentHeader.title, null);
107 }
108
109 if (mParentHeader != null) {
110 setParentTitle(mParentHeader.title, null, new OnClickListener() {
111 public void onClick(View v) {
112 switchToParent(mParentHeader.fragment);
113 }
114 });
115 }
Gilles Debunnedc7101f2011-06-27 12:00:49 -0700116
117 // TODO Add support for android.R.id.home in all Setting's onOptionsItemSelected
118 // getActionBar().setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP,
119 // ActionBar.DISPLAY_HOME_AS_UP);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800120 }
121
122 @Override
123 protected void onSaveInstanceState(Bundle outState) {
124 super.onSaveInstanceState(outState);
125
126 // Save the current fragment, if it is the same as originally launched
127 if (mCurrentHeader != null) {
128 outState.putParcelable(SAVE_KEY_CURRENT_HEADER, mCurrentHeader);
129 }
130 if (mParentHeader != null) {
131 outState.putParcelable(SAVE_KEY_PARENT_HEADER, mParentHeader);
132 }
133 }
134
Gilles Debunnee78c1872011-06-20 15:00:07 -0700135 @Override
136 public void onResume() {
137 super.onResume();
138
139 ListAdapter listAdapter = getListAdapter();
140 if (listAdapter instanceof HeaderAdapter) {
141 ((HeaderAdapter) listAdapter).resume();
142 }
143 }
144
145 @Override
146 public void onPause() {
147 super.onPause();
148
149 ListAdapter listAdapter = getListAdapter();
150 if (listAdapter instanceof HeaderAdapter) {
151 ((HeaderAdapter) listAdapter).pause();
152 }
153 }
154
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800155 private void switchToHeaderLocal(Header header) {
156 mInLocalHeaderSwitch = true;
157 switchToHeader(header);
158 mInLocalHeaderSwitch = false;
159 }
160
161 @Override
162 public void switchToHeader(Header header) {
163 if (!mInLocalHeaderSwitch) {
164 mCurrentHeader = null;
165 mParentHeader = null;
166 }
167 super.switchToHeader(header);
168 }
169
170 /**
171 * Switch to parent fragment and store the grand parent's info
Jake Hamby2748fc22011-01-12 15:06:28 -0800172 * @param className name of the activity wrapper for the parent fragment.
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800173 */
174 private void switchToParent(String className) {
175 final ComponentName cn = new ComponentName(this, className);
176 try {
177 final PackageManager pm = getPackageManager();
178 final ActivityInfo parentInfo = pm.getActivityInfo(cn, PackageManager.GET_META_DATA);
179
180 if (parentInfo != null && parentInfo.metaData != null) {
181 String fragmentClass = parentInfo.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
182 CharSequence fragmentTitle = parentInfo.loadLabel(pm);
183 Header parentHeader = new Header();
184 parentHeader.fragment = fragmentClass;
185 parentHeader.title = fragmentTitle;
186 mCurrentHeader = parentHeader;
187
188 switchToHeaderLocal(parentHeader);
189
190 mParentHeader = new Header();
191 mParentHeader.fragment
192 = parentInfo.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
193 mParentHeader.title = parentInfo.metaData.getString(META_DATA_KEY_PARENT_TITLE);
194 }
195 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700196 Log.w(LOG_TAG, "Could not find parent activity : " + className);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800197 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700198 }
199
Amith Yamasani3965ae62010-11-15 14:45:19 -0800200 @Override
201 public void onNewIntent(Intent intent) {
202 super.onNewIntent(intent);
203
204 // If it is not launched from history, then reset to top-level
205 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0
Gilles Debunne3661b622011-06-27 11:19:16 -0700206 && mFirstHeader != null && !onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800207 switchToHeaderLocal(mFirstHeader);
Amith Yamasani3965ae62010-11-15 14:45:19 -0800208 }
209 }
210
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700211 private void highlightHeader() {
212 if (mTopLevelHeaderId != 0) {
213 Integer index = mHeaderIndexMap.get(mTopLevelHeaderId);
214 if (index != null) {
215 getListView().setItemChecked(index, true);
216 }
217 }
218 }
219
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700220 @Override
221 public Intent getIntent() {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700222 Intent superIntent = super.getIntent();
223 String startingFragment = getStartingFragmentClass(superIntent);
Gilles Debunne3661b622011-06-27 11:19:16 -0700224 // This is called from super.onCreate, isMultiPane() is not yet reliable
225 // Do not use onIsHidingHeaders either, which relies itself on this method
226 if (startingFragment != null && !onIsMultiPane()) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700227 Intent modIntent = new Intent(superIntent);
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700228 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700229 Bundle args = superIntent.getExtras();
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700230 if (args != null) {
231 args = new Bundle(args);
232 } else {
233 args = new Bundle();
234 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700235 args.putParcelable("intent", superIntent);
236 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras());
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700237 return modIntent;
238 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700239 return superIntent;
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700240 }
241
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700242 /**
Amith Yamasani379d9b02010-09-27 12:03:59 -0700243 * Checks if the component name in the intent is different from the Settings class and
244 * returns the class name to load as a fragment.
245 */
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700246 protected String getStartingFragmentClass(Intent intent) {
247 if (mFragmentClass != null) return mFragmentClass;
248
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700249 String intentClass = intent.getComponent().getClassName();
Amith Yamasani379d9b02010-09-27 12:03:59 -0700250 if (intentClass.equals(getClass().getName())) return null;
251
Dianne Hackbornee293792010-11-01 12:32:33 -0700252 if ("com.android.settings.ManageApplications".equals(intentClass)
253 || "com.android.settings.RunningServices".equals(intentClass)
254 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700255 // Old names of manage apps.
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700256 intentClass = com.android.settings.applications.ManageApplications.class.getName();
257 }
258
Amith Yamasani379d9b02010-09-27 12:03:59 -0700259 return intentClass;
260 }
261
262 /**
263 * Override initial header when an activity-alias is causing Settings to be launched
264 * for a specific fragment encoded in the android:name parameter.
265 */
266 @Override
267 public Header onGetInitialHeader() {
268 String fragmentClass = getStartingFragmentClass(super.getIntent());
269 if (fragmentClass != null) {
270 Header header = new Header();
271 header.fragment = fragmentClass;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800272 header.title = getTitle();
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700273 header.fragmentArguments = getIntent().getExtras();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800274 mCurrentHeader = header;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700275 return header;
276 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700277
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700278 return mFirstHeader;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700279 }
280
Dianne Hackbornb7258182011-03-15 16:23:55 -0700281 @Override
Dianne Hackborn48147dc2011-03-18 12:29:41 -0700282 public Intent onBuildStartFragmentIntent(String fragmentName, Bundle args,
283 int titleRes, int shortTitleRes) {
284 Intent intent = super.onBuildStartFragmentIntent(fragmentName, args,
285 titleRes, shortTitleRes);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700286
287 // some fragments would like a custom activity theme
288 if (DataUsageSummary.class.getName().equals(fragmentName)) {
289 intent.putExtra(EXTRA_THEME, android.R.style.Theme_Holo_SolidActionBar);
290 }
291
Dianne Hackbornb7258182011-03-15 16:23:55 -0700292 intent.setClass(this, SubSettings.class);
293 return intent;
294 }
295
Amith Yamasani379d9b02010-09-27 12:03:59 -0700296 /**
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700297 * Populate the activity with the top-level headers.
298 */
Amith Yamasanid7993472010-08-18 13:59:28 -0700299 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700300 public void onBuildHeaders(List<Header> headers) {
301 loadHeadersFromResource(R.xml.settings_headers, headers);
Amith Yamasanid7993472010-08-18 13:59:28 -0700302
Gilles Debunnee78c1872011-06-20 15:00:07 -0700303 updateHeaderList(headers);
304
305 mHeaders = headers;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700306 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700307
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700308 private void updateHeaderList(List<Header> target) {
309 int i = 0;
310 while (i < target.size()) {
311 Header header = target.get(i);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700312 // Ids are integers, so downcasting
313 int id = (int) header.id;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700314 if (id == R.id.dock_settings) {
315 if (!needsDockSettings())
316 target.remove(header);
317 } else if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
318 Utils.updateHeaderToSpecificActivityFromMetaDataOrRemove(this, target, header);
Gilles Debunne2454f492011-06-21 16:16:06 -0700319 } else if (id == R.id.wifi_settings) {
320 // Remove WiFi Settings if WiFi service is not available.
321 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
322 target.remove(header);
323 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700324 } else if (id == R.id.bluetooth_settings) {
325 // Remove Bluetooth Settings if Bluetooth service is not available.
Gilles Debunne2454f492011-06-21 16:16:06 -0700326 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700327 target.remove(header);
328 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700329 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700330
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700331 // Increment if the current one wasn't removed by the Utils code.
332 if (target.get(i) == header) {
Amith Yamasani3965ae62010-11-15 14:45:19 -0800333 // Hold on to the first header, when we need to reset to the top-level
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700334 if (mFirstHeader == null &&
335 HeaderAdapter.getHeaderType(header) != HeaderAdapter.HEADER_TYPE_CATEGORY) {
336 mFirstHeader = header;
337 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700338 mHeaderIndexMap.put(id, i);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700339 i++;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700340 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700341 }
342 }
343
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700344 private boolean needsDockSettings() {
345 return getResources().getBoolean(R.bool.has_dock_settings);
Amith Yamasanib61cf512010-09-12 08:17:50 -0700346 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700347
348 private void getMetaData() {
349 try {
350 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
351 PackageManager.GET_META_DATA);
352 if (ai == null || ai.metaData == null) return;
353 mTopLevelHeaderId = ai.metaData.getInt(META_DATA_KEY_HEADER_ID);
354 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800355
356 // Check if it has a parent specified and create a Header object
357 final int parentHeaderTitleRes = ai.metaData.getInt(META_DATA_KEY_PARENT_TITLE);
358 String parentFragmentClass = ai.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
359 if (parentFragmentClass != null) {
360 mParentHeader = new Header();
361 mParentHeader.fragment = parentFragmentClass;
362 if (parentHeaderTitleRes != 0) {
363 mParentHeader.title = getResources().getString(parentHeaderTitleRes);
364 }
365 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700366 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700367 // No recovery
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700368 }
369 }
370
Amith Yamasani9e3a4702011-01-11 09:09:26 -0800371 @Override
372 public boolean hasNextButton() {
373 return super.hasNextButton();
374 }
375
376 @Override
377 public Button getNextButton() {
378 return super.getNextButton();
379 }
380
Gilles Debunnee78c1872011-06-20 15:00:07 -0700381 private static class HeaderAdapter extends ArrayAdapter<Header> {
382 static final int HEADER_TYPE_CATEGORY = 0;
383 static final int HEADER_TYPE_NORMAL = 1;
384 static final int HEADER_TYPE_SWITCH = 2;
385 private static final int HEADER_TYPE_COUNT = HEADER_TYPE_SWITCH + 1;
386
387 private final WifiEnabler mWifiEnabler;
388 private final BluetoothEnabler mBluetoothEnabler;
389
390 private static class HeaderViewHolder {
391 ImageView icon;
392 TextView title;
393 TextView summary;
394 Switch switch_;
395 }
396
397 private LayoutInflater mInflater;
398
399 static int getHeaderType(Header header) {
400 if (header.fragment == null && header.intent == null) {
401 return HEADER_TYPE_CATEGORY;
402 } else if (header.id == R.id.wifi_settings || header.id == R.id.bluetooth_settings) {
403 return HEADER_TYPE_SWITCH;
404 } else {
405 return HEADER_TYPE_NORMAL;
406 }
407 }
408
409 @Override
410 public int getItemViewType(int position) {
411 Header header = getItem(position);
412 return getHeaderType(header);
413 }
414
415 @Override
416 public boolean areAllItemsEnabled() {
417 return false; // because of categories
418 }
419
420 @Override
421 public boolean isEnabled(int position) {
422 return getItemViewType(position) != HEADER_TYPE_CATEGORY;
423 }
424
425 @Override
426 public int getViewTypeCount() {
427 return HEADER_TYPE_COUNT;
428 }
429
430 @Override
431 public boolean hasStableIds() {
432 return true;
433 }
434
435 public HeaderAdapter(Context context, List<Header> objects) {
436 super(context, 0, objects);
437 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
438
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700439 // Temp Switches provided as placeholder until the adapter replaces these with actual
Gilles Debunnee78c1872011-06-20 15:00:07 -0700440 // Switches inflated from their layouts. Must be done before adapter is set in super
441 mWifiEnabler = new WifiEnabler(context, new Switch(context));
442 mBluetoothEnabler = new BluetoothEnabler(context, new Switch(context));
443 }
444
445 @Override
446 public View getView(int position, View convertView, ViewGroup parent) {
447 HeaderViewHolder holder;
448 Header header = getItem(position);
449 int headerType = getHeaderType(header);
450 View view = null;
451
452 if (convertView == null) {
453 holder = new HeaderViewHolder();
454 switch (headerType) {
455 case HEADER_TYPE_CATEGORY:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700456 view = new TextView(getContext(), null,
457 android.R.attr.listSeparatorTextViewStyle);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700458 holder.title = (TextView) view;
459 break;
460
461 case HEADER_TYPE_SWITCH:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700462 view = mInflater.inflate(R.layout.preference_header_switch_item, parent,
463 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700464 holder.icon = (ImageView) view.findViewById(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 holder.switch_ = (Switch) view.findViewById(R.id.switchWidget);
470 break;
471
472 case HEADER_TYPE_NORMAL:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700473 view = mInflater.inflate(
474 com.android.internal.R.layout.preference_header_item, parent,
475 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700476 holder.icon = (ImageView) view.findViewById(com.android.internal.R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700477 holder.title = (TextView)
478 view.findViewById(com.android.internal.R.id.title);
479 holder.summary = (TextView)
480 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700481 break;
482 }
483 view.setTag(holder);
484 } else {
485 view = convertView;
486 holder = (HeaderViewHolder) view.getTag();
487 }
488
489 // All view fields must be updated every time, because the view may be recycled
490 switch (headerType) {
491 case HEADER_TYPE_CATEGORY:
492 holder.title.setText(header.getTitle(getContext().getResources()));
493 break;
494
495 case HEADER_TYPE_SWITCH:
496 // Would need a different treatment if the main menu had more switches
497 if (header.id == R.id.wifi_settings) {
498 mWifiEnabler.setSwitch(holder.switch_);
499 } else {
500 mBluetoothEnabler.setSwitch(holder.switch_);
501 }
502 // No break, fall through on purpose to update common fields
503
504 //$FALL-THROUGH$
505 case HEADER_TYPE_NORMAL:
506 holder.icon.setImageResource(header.iconRes);
507 holder.title.setText(header.getTitle(getContext().getResources()));
508 CharSequence summary = header.getSummary(getContext().getResources());
509 if (!TextUtils.isEmpty(summary)) {
510 holder.summary.setVisibility(View.VISIBLE);
511 holder.summary.setText(summary);
512 } else {
513 holder.summary.setVisibility(View.GONE);
514 }
515 break;
516 }
517
518 return view;
519 }
520
521 public void resume() {
522 mWifiEnabler.resume();
523 mBluetoothEnabler.resume();
524 }
525
526 public void pause() {
527 mWifiEnabler.pause();
528 mBluetoothEnabler.pause();
529 }
530 }
531
532 @Override
533 public void setListAdapter(ListAdapter adapter) {
534 if (mHeaders == null) {
535 mHeaders = new ArrayList<Header>();
536 // When the saved state provides the list of headers, onBuildHeaders is not called
537 // Copy the list of Headers from the adapter, preserving their order
538 for (int i = 0; i < adapter.getCount(); i++) {
539 mHeaders.add((Header) adapter.getItem(i));
540 }
541 }
542
543 // Ignore the adapter provided by PreferenceActivity and substitute ours instead
544 super.setListAdapter(new HeaderAdapter(this, mHeaders));
545 }
546
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700547 /*
548 * Settings subclasses for launching independently.
549 */
Gilles Debunnee78c1872011-06-20 15:00:07 -0700550 public static class BluetoothSettingsActivity extends Settings { /* empty */ }
551 public static class WirelessSettingsActivity extends Settings { /* empty */ }
552 public static class TetherSettingsActivity extends Settings { /* empty */ }
553 public static class VpnSettingsActivity extends Settings { /* empty */ }
554 public static class DateTimeSettingsActivity extends Settings { /* empty */ }
555 public static class StorageSettingsActivity extends Settings { /* empty */ }
556 public static class WifiSettingsActivity extends Settings { /* empty */ }
557 public static class InputMethodAndLanguageSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700558 public static class InputMethodAndSubtypeEnablerActivity extends Settings { /* empty */ }
559 public static class LocalePickerActivity extends Settings { /* empty */ }
560 public static class UserDictionarySettingsActivity extends Settings { /* empty */ }
561 public static class SoundSettingsActivity extends Settings { /* empty */ }
562 public static class DisplaySettingsActivity extends Settings { /* empty */ }
563 public static class DeviceInfoSettingsActivity extends Settings { /* empty */ }
564 public static class ApplicationSettingsActivity extends Settings { /* empty */ }
565 public static class ManageApplicationsActivity extends Settings { /* empty */ }
566 public static class StorageUseActivity extends Settings { /* empty */ }
567 public static class DevelopmentSettingsActivity extends Settings { /* empty */ }
568 public static class AccessibilitySettingsActivity extends Settings { /* empty */ }
569 public static class SecuritySettingsActivity extends Settings { /* empty */ }
Gilles Debunnea6a8a142011-06-09 11:56:17 -0700570 public static class LocationSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700571 public static class PrivacySettingsActivity extends Settings { /* empty */ }
572 public static class DockSettingsActivity extends Settings { /* empty */ }
573 public static class RunningServicesActivity extends Settings { /* empty */ }
574 public static class ManageAccountsSettingsActivity extends Settings { /* empty */ }
575 public static class PowerUsageSummaryActivity extends Settings { /* empty */ }
576 public static class AccountSyncSettingsActivity extends Settings { /* empty */ }
577 public static class AccountSyncSettingsInAddAccountActivity extends Settings { /* empty */ }
578 public static class CryptKeeperSettingsActivity extends Settings { /* empty */ }
579 public static class DeviceAdminSettingsActivity extends Settings { /* empty */ }
580 public static class DataUsageSummaryActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700581 public static class AdvancedWifiSettingsActivity extends Settings { /* empty */ }
582 public static class AdvancedBluetoothSettingsActivity extends Settings { /* empty */ }
583 public static class TextToSpeechSettingsActivity extends Settings { /* empty */ }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800584}