blob: f09125c1b21b8b26531ecb59e080744a04764a8d [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 Debunne162e5412011-07-11 14:13:31 -070040import com.android.settings.accounts.ManageAccountsSettings;
Gilles Debunnee78c1872011-06-20 15:00:07 -070041import com.android.settings.bluetooth.BluetoothEnabler;
Gilles Debunne162e5412011-07-11 14:13:31 -070042import com.android.settings.fuelgauge.PowerUsageSummary;
Gilles Debunnee78c1872011-06-20 15:00:07 -070043import com.android.settings.wifi.WifiEnabler;
44
45import java.util.ArrayList;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070046import java.util.HashMap;
Amith Yamasani02cf71a2010-09-21 15:48:52 -070047import java.util.List;
Amith Yamasanid7993472010-08-18 13:59:28 -070048
49/**
50 * Top-level settings activity to handle single pane and double pane UI layout.
51 */
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -080052public class Settings extends PreferenceActivity implements ButtonBarHandler {
Amith Yamasanid7993472010-08-18 13:59:28 -070053
Gilles Debunnee78c1872011-06-20 15:00:07 -070054 private static final String LOG_TAG = "Settings";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070055 private static final String META_DATA_KEY_HEADER_ID =
Gilles Debunnee78c1872011-06-20 15:00:07 -070056 "com.android.settings.TOP_LEVEL_HEADER_ID";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070057 private static final String META_DATA_KEY_FRAGMENT_CLASS =
Gilles Debunnee78c1872011-06-20 15:00:07 -070058 "com.android.settings.FRAGMENT_CLASS";
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080059 private static final String META_DATA_KEY_PARENT_TITLE =
60 "com.android.settings.PARENT_FRAGMENT_TITLE";
61 private static final String META_DATA_KEY_PARENT_FRAGMENT_CLASS =
62 "com.android.settings.PARENT_FRAGMENT_CLASS";
63
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070064 private static final String EXTRA_THEME = "settings:theme";
65
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080066 private static final String SAVE_KEY_CURRENT_HEADER = "com.android.settings.CURRENT_HEADER";
67 private static final String SAVE_KEY_PARENT_HEADER = "com.android.settings.PARENT_HEADER";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070068
69 private String mFragmentClass;
70 private int mTopLevelHeaderId;
Amith Yamasani3965ae62010-11-15 14:45:19 -080071 private Header mFirstHeader;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080072 private Header mCurrentHeader;
73 private Header mParentHeader;
74 private boolean mInLocalHeaderSwitch;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070075
Amith Yamasani02cf71a2010-09-21 15:48:52 -070076 // TODO: Update Call Settings based on airplane mode state.
Amith Yamasanib61cf512010-09-12 08:17:50 -070077
Amith Yamasani5203bdf2010-11-04 09:59:44 -070078 protected HashMap<Integer, Integer> mHeaderIndexMap = new HashMap<Integer, Integer>();
Gilles Debunnee78c1872011-06-20 15:00:07 -070079 private List<Header> mHeaders;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070080
81 @Override
82 protected void onCreate(Bundle savedInstanceState) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070083 final int theme = getIntent().getIntExtra(
84 EXTRA_THEME, android.R.style.Theme_Holo_SolidActionBar_SplitActionBarWhenNarrow);
85 setTheme(theme);
86
Amith Yamasani5203bdf2010-11-04 09:59:44 -070087 getMetaData();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080088 mInLocalHeaderSwitch = true;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070089 super.onCreate(savedInstanceState);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080090 mInLocalHeaderSwitch = false;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070091
Gilles Debunne3661b622011-06-27 11:19:16 -070092 if (!onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasani72aa19d2010-12-09 06:07:12 -080093 highlightHeader();
94 // Force the title so that it doesn't get overridden by a direct launch of
95 // a specific settings screen.
96 setTitle(R.string.settings_label);
97 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080098
99 // Retrieve any saved state
100 if (savedInstanceState != null) {
101 mCurrentHeader = savedInstanceState.getParcelable(SAVE_KEY_CURRENT_HEADER);
102 mParentHeader = savedInstanceState.getParcelable(SAVE_KEY_PARENT_HEADER);
103 }
104
105 // If the current header was saved, switch to it
106 if (savedInstanceState != null && mCurrentHeader != null) {
107 //switchToHeaderLocal(mCurrentHeader);
108 showBreadCrumbs(mCurrentHeader.title, null);
109 }
110
111 if (mParentHeader != null) {
112 setParentTitle(mParentHeader.title, null, new OnClickListener() {
113 public void onClick(View v) {
114 switchToParent(mParentHeader.fragment);
115 }
116 });
117 }
Gilles Debunnedc7101f2011-06-27 12:00:49 -0700118
119 // TODO Add support for android.R.id.home in all Setting's onOptionsItemSelected
120 // getActionBar().setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP,
121 // ActionBar.DISPLAY_HOME_AS_UP);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800122 }
123
124 @Override
125 protected void onSaveInstanceState(Bundle outState) {
126 super.onSaveInstanceState(outState);
127
128 // Save the current fragment, if it is the same as originally launched
129 if (mCurrentHeader != null) {
130 outState.putParcelable(SAVE_KEY_CURRENT_HEADER, mCurrentHeader);
131 }
132 if (mParentHeader != null) {
133 outState.putParcelable(SAVE_KEY_PARENT_HEADER, mParentHeader);
134 }
135 }
136
Gilles Debunnee78c1872011-06-20 15:00:07 -0700137 @Override
138 public void onResume() {
139 super.onResume();
140
141 ListAdapter listAdapter = getListAdapter();
142 if (listAdapter instanceof HeaderAdapter) {
143 ((HeaderAdapter) listAdapter).resume();
144 }
145 }
146
147 @Override
148 public void onPause() {
149 super.onPause();
150
151 ListAdapter listAdapter = getListAdapter();
152 if (listAdapter instanceof HeaderAdapter) {
153 ((HeaderAdapter) listAdapter).pause();
154 }
155 }
156
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800157 private void switchToHeaderLocal(Header header) {
158 mInLocalHeaderSwitch = true;
159 switchToHeader(header);
160 mInLocalHeaderSwitch = false;
161 }
162
163 @Override
164 public void switchToHeader(Header header) {
165 if (!mInLocalHeaderSwitch) {
166 mCurrentHeader = null;
167 mParentHeader = null;
168 }
169 super.switchToHeader(header);
170 }
171
172 /**
173 * Switch to parent fragment and store the grand parent's info
Jake Hamby2748fc22011-01-12 15:06:28 -0800174 * @param className name of the activity wrapper for the parent fragment.
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800175 */
176 private void switchToParent(String className) {
177 final ComponentName cn = new ComponentName(this, className);
178 try {
179 final PackageManager pm = getPackageManager();
180 final ActivityInfo parentInfo = pm.getActivityInfo(cn, PackageManager.GET_META_DATA);
181
182 if (parentInfo != null && parentInfo.metaData != null) {
183 String fragmentClass = parentInfo.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
184 CharSequence fragmentTitle = parentInfo.loadLabel(pm);
185 Header parentHeader = new Header();
186 parentHeader.fragment = fragmentClass;
187 parentHeader.title = fragmentTitle;
188 mCurrentHeader = parentHeader;
189
190 switchToHeaderLocal(parentHeader);
191
192 mParentHeader = new Header();
193 mParentHeader.fragment
194 = parentInfo.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
195 mParentHeader.title = parentInfo.metaData.getString(META_DATA_KEY_PARENT_TITLE);
196 }
197 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700198 Log.w(LOG_TAG, "Could not find parent activity : " + className);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800199 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700200 }
201
Amith Yamasani3965ae62010-11-15 14:45:19 -0800202 @Override
203 public void onNewIntent(Intent intent) {
204 super.onNewIntent(intent);
205
206 // If it is not launched from history, then reset to top-level
207 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0
Gilles Debunne3661b622011-06-27 11:19:16 -0700208 && mFirstHeader != null && !onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800209 switchToHeaderLocal(mFirstHeader);
Amith Yamasani3965ae62010-11-15 14:45:19 -0800210 }
211 }
212
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700213 private void highlightHeader() {
214 if (mTopLevelHeaderId != 0) {
215 Integer index = mHeaderIndexMap.get(mTopLevelHeaderId);
216 if (index != null) {
217 getListView().setItemChecked(index, true);
218 }
219 }
220 }
221
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700222 @Override
223 public Intent getIntent() {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700224 Intent superIntent = super.getIntent();
225 String startingFragment = getStartingFragmentClass(superIntent);
Gilles Debunne3661b622011-06-27 11:19:16 -0700226 // This is called from super.onCreate, isMultiPane() is not yet reliable
227 // Do not use onIsHidingHeaders either, which relies itself on this method
228 if (startingFragment != null && !onIsMultiPane()) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700229 Intent modIntent = new Intent(superIntent);
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700230 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700231 Bundle args = superIntent.getExtras();
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700232 if (args != null) {
233 args = new Bundle(args);
234 } else {
235 args = new Bundle();
236 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700237 args.putParcelable("intent", superIntent);
238 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras());
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700239 return modIntent;
240 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700241 return superIntent;
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700242 }
243
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700244 /**
Amith Yamasani379d9b02010-09-27 12:03:59 -0700245 * Checks if the component name in the intent is different from the Settings class and
246 * returns the class name to load as a fragment.
247 */
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700248 protected String getStartingFragmentClass(Intent intent) {
249 if (mFragmentClass != null) return mFragmentClass;
250
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700251 String intentClass = intent.getComponent().getClassName();
Amith Yamasani379d9b02010-09-27 12:03:59 -0700252 if (intentClass.equals(getClass().getName())) return null;
253
Dianne Hackbornee293792010-11-01 12:32:33 -0700254 if ("com.android.settings.ManageApplications".equals(intentClass)
255 || "com.android.settings.RunningServices".equals(intentClass)
256 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700257 // Old names of manage apps.
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700258 intentClass = com.android.settings.applications.ManageApplications.class.getName();
259 }
260
Amith Yamasani379d9b02010-09-27 12:03:59 -0700261 return intentClass;
262 }
263
264 /**
265 * Override initial header when an activity-alias is causing Settings to be launched
266 * for a specific fragment encoded in the android:name parameter.
267 */
268 @Override
269 public Header onGetInitialHeader() {
270 String fragmentClass = getStartingFragmentClass(super.getIntent());
271 if (fragmentClass != null) {
272 Header header = new Header();
273 header.fragment = fragmentClass;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800274 header.title = getTitle();
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700275 header.fragmentArguments = getIntent().getExtras();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800276 mCurrentHeader = header;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700277 return header;
278 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700279
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700280 return mFirstHeader;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700281 }
282
Dianne Hackbornb7258182011-03-15 16:23:55 -0700283 @Override
Dianne Hackborn48147dc2011-03-18 12:29:41 -0700284 public Intent onBuildStartFragmentIntent(String fragmentName, Bundle args,
285 int titleRes, int shortTitleRes) {
286 Intent intent = super.onBuildStartFragmentIntent(fragmentName, args,
287 titleRes, shortTitleRes);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700288
289 // some fragments would like a custom activity theme
Gilles Debunne162e5412011-07-11 14:13:31 -0700290 if (DataUsageSummary.class.getName().equals(fragmentName) ||
291 PowerUsageSummary.class.getName().equals(fragmentName) ||
292 UserDictionarySettings.class.getName().equals(fragmentName) ||
293 ManageAccountsSettings.class.getName().equals(fragmentName)) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700294 intent.putExtra(EXTRA_THEME, android.R.style.Theme_Holo_SolidActionBar);
295 }
296
Dianne Hackbornb7258182011-03-15 16:23:55 -0700297 intent.setClass(this, SubSettings.class);
298 return intent;
299 }
300
Amith Yamasani379d9b02010-09-27 12:03:59 -0700301 /**
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700302 * Populate the activity with the top-level headers.
303 */
Amith Yamasanid7993472010-08-18 13:59:28 -0700304 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700305 public void onBuildHeaders(List<Header> headers) {
306 loadHeadersFromResource(R.xml.settings_headers, headers);
Amith Yamasanid7993472010-08-18 13:59:28 -0700307
Gilles Debunnee78c1872011-06-20 15:00:07 -0700308 updateHeaderList(headers);
309
310 mHeaders = headers;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700311 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700312
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700313 private void updateHeaderList(List<Header> target) {
314 int i = 0;
315 while (i < target.size()) {
316 Header header = target.get(i);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700317 // Ids are integers, so downcasting
318 int id = (int) header.id;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700319 if (id == R.id.dock_settings) {
320 if (!needsDockSettings())
321 target.remove(header);
322 } else if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
323 Utils.updateHeaderToSpecificActivityFromMetaDataOrRemove(this, target, header);
Gilles Debunne2454f492011-06-21 16:16:06 -0700324 } else if (id == R.id.wifi_settings) {
325 // Remove WiFi Settings if WiFi service is not available.
326 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
327 target.remove(header);
328 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700329 } else if (id == R.id.bluetooth_settings) {
330 // Remove Bluetooth Settings if Bluetooth service is not available.
Gilles Debunne2454f492011-06-21 16:16:06 -0700331 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700332 target.remove(header);
333 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700334 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700335
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700336 // Increment if the current one wasn't removed by the Utils code.
337 if (target.get(i) == header) {
Amith Yamasani3965ae62010-11-15 14:45:19 -0800338 // Hold on to the first header, when we need to reset to the top-level
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700339 if (mFirstHeader == null &&
340 HeaderAdapter.getHeaderType(header) != HeaderAdapter.HEADER_TYPE_CATEGORY) {
341 mFirstHeader = header;
342 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700343 mHeaderIndexMap.put(id, i);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700344 i++;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700345 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700346 }
347 }
348
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700349 private boolean needsDockSettings() {
350 return getResources().getBoolean(R.bool.has_dock_settings);
Amith Yamasanib61cf512010-09-12 08:17:50 -0700351 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700352
353 private void getMetaData() {
354 try {
355 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
356 PackageManager.GET_META_DATA);
357 if (ai == null || ai.metaData == null) return;
358 mTopLevelHeaderId = ai.metaData.getInt(META_DATA_KEY_HEADER_ID);
359 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800360
361 // Check if it has a parent specified and create a Header object
362 final int parentHeaderTitleRes = ai.metaData.getInt(META_DATA_KEY_PARENT_TITLE);
363 String parentFragmentClass = ai.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
364 if (parentFragmentClass != null) {
365 mParentHeader = new Header();
366 mParentHeader.fragment = parentFragmentClass;
367 if (parentHeaderTitleRes != 0) {
368 mParentHeader.title = getResources().getString(parentHeaderTitleRes);
369 }
370 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700371 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700372 // No recovery
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700373 }
374 }
375
Amith Yamasani9e3a4702011-01-11 09:09:26 -0800376 @Override
377 public boolean hasNextButton() {
378 return super.hasNextButton();
379 }
380
381 @Override
382 public Button getNextButton() {
383 return super.getNextButton();
384 }
385
Gilles Debunnee78c1872011-06-20 15:00:07 -0700386 private static class HeaderAdapter extends ArrayAdapter<Header> {
387 static final int HEADER_TYPE_CATEGORY = 0;
388 static final int HEADER_TYPE_NORMAL = 1;
389 static final int HEADER_TYPE_SWITCH = 2;
390 private static final int HEADER_TYPE_COUNT = HEADER_TYPE_SWITCH + 1;
391
392 private final WifiEnabler mWifiEnabler;
393 private final BluetoothEnabler mBluetoothEnabler;
394
395 private static class HeaderViewHolder {
396 ImageView icon;
397 TextView title;
398 TextView summary;
399 Switch switch_;
400 }
401
402 private LayoutInflater mInflater;
403
404 static int getHeaderType(Header header) {
405 if (header.fragment == null && header.intent == null) {
406 return HEADER_TYPE_CATEGORY;
407 } else if (header.id == R.id.wifi_settings || header.id == R.id.bluetooth_settings) {
408 return HEADER_TYPE_SWITCH;
409 } else {
410 return HEADER_TYPE_NORMAL;
411 }
412 }
413
414 @Override
415 public int getItemViewType(int position) {
416 Header header = getItem(position);
417 return getHeaderType(header);
418 }
419
420 @Override
421 public boolean areAllItemsEnabled() {
422 return false; // because of categories
423 }
424
425 @Override
426 public boolean isEnabled(int position) {
427 return getItemViewType(position) != HEADER_TYPE_CATEGORY;
428 }
429
430 @Override
431 public int getViewTypeCount() {
432 return HEADER_TYPE_COUNT;
433 }
434
435 @Override
436 public boolean hasStableIds() {
437 return true;
438 }
439
440 public HeaderAdapter(Context context, List<Header> objects) {
441 super(context, 0, objects);
442 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
443
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700444 // Temp Switches provided as placeholder until the adapter replaces these with actual
Gilles Debunnee78c1872011-06-20 15:00:07 -0700445 // Switches inflated from their layouts. Must be done before adapter is set in super
446 mWifiEnabler = new WifiEnabler(context, new Switch(context));
447 mBluetoothEnabler = new BluetoothEnabler(context, new Switch(context));
448 }
449
450 @Override
451 public View getView(int position, View convertView, ViewGroup parent) {
452 HeaderViewHolder holder;
453 Header header = getItem(position);
454 int headerType = getHeaderType(header);
455 View view = null;
456
457 if (convertView == null) {
458 holder = new HeaderViewHolder();
459 switch (headerType) {
460 case HEADER_TYPE_CATEGORY:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700461 view = new TextView(getContext(), null,
462 android.R.attr.listSeparatorTextViewStyle);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700463 holder.title = (TextView) view;
464 break;
465
466 case HEADER_TYPE_SWITCH:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700467 view = mInflater.inflate(R.layout.preference_header_switch_item, parent,
468 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700469 holder.icon = (ImageView) view.findViewById(R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700470 holder.title = (TextView)
471 view.findViewById(com.android.internal.R.id.title);
472 holder.summary = (TextView)
473 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700474 holder.switch_ = (Switch) view.findViewById(R.id.switchWidget);
475 break;
476
477 case HEADER_TYPE_NORMAL:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700478 view = mInflater.inflate(
479 com.android.internal.R.layout.preference_header_item, parent,
480 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700481 holder.icon = (ImageView) view.findViewById(com.android.internal.R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700482 holder.title = (TextView)
483 view.findViewById(com.android.internal.R.id.title);
484 holder.summary = (TextView)
485 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700486 break;
487 }
488 view.setTag(holder);
489 } else {
490 view = convertView;
491 holder = (HeaderViewHolder) view.getTag();
492 }
493
494 // All view fields must be updated every time, because the view may be recycled
495 switch (headerType) {
496 case HEADER_TYPE_CATEGORY:
497 holder.title.setText(header.getTitle(getContext().getResources()));
498 break;
499
500 case HEADER_TYPE_SWITCH:
501 // Would need a different treatment if the main menu had more switches
502 if (header.id == R.id.wifi_settings) {
503 mWifiEnabler.setSwitch(holder.switch_);
504 } else {
505 mBluetoothEnabler.setSwitch(holder.switch_);
506 }
507 // No break, fall through on purpose to update common fields
508
509 //$FALL-THROUGH$
510 case HEADER_TYPE_NORMAL:
511 holder.icon.setImageResource(header.iconRes);
512 holder.title.setText(header.getTitle(getContext().getResources()));
513 CharSequence summary = header.getSummary(getContext().getResources());
514 if (!TextUtils.isEmpty(summary)) {
515 holder.summary.setVisibility(View.VISIBLE);
516 holder.summary.setText(summary);
517 } else {
518 holder.summary.setVisibility(View.GONE);
519 }
520 break;
521 }
522
523 return view;
524 }
525
526 public void resume() {
527 mWifiEnabler.resume();
528 mBluetoothEnabler.resume();
529 }
530
531 public void pause() {
532 mWifiEnabler.pause();
533 mBluetoothEnabler.pause();
534 }
535 }
536
537 @Override
538 public void setListAdapter(ListAdapter adapter) {
539 if (mHeaders == null) {
540 mHeaders = new ArrayList<Header>();
541 // When the saved state provides the list of headers, onBuildHeaders is not called
542 // Copy the list of Headers from the adapter, preserving their order
543 for (int i = 0; i < adapter.getCount(); i++) {
544 mHeaders.add((Header) adapter.getItem(i));
545 }
546 }
547
548 // Ignore the adapter provided by PreferenceActivity and substitute ours instead
549 super.setListAdapter(new HeaderAdapter(this, mHeaders));
550 }
551
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700552 /*
553 * Settings subclasses for launching independently.
554 */
Gilles Debunnee78c1872011-06-20 15:00:07 -0700555 public static class BluetoothSettingsActivity extends Settings { /* empty */ }
556 public static class WirelessSettingsActivity extends Settings { /* empty */ }
557 public static class TetherSettingsActivity extends Settings { /* empty */ }
558 public static class VpnSettingsActivity extends Settings { /* empty */ }
559 public static class DateTimeSettingsActivity extends Settings { /* empty */ }
560 public static class StorageSettingsActivity extends Settings { /* empty */ }
561 public static class WifiSettingsActivity extends Settings { /* empty */ }
562 public static class InputMethodAndLanguageSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700563 public static class InputMethodAndSubtypeEnablerActivity extends Settings { /* empty */ }
satoke077d2b2011-07-25 09:38:11 +0900564 public static class SpellCheckersSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700565 public static class LocalePickerActivity extends Settings { /* empty */ }
566 public static class UserDictionarySettingsActivity extends Settings { /* empty */ }
567 public static class SoundSettingsActivity extends Settings { /* empty */ }
568 public static class DisplaySettingsActivity extends Settings { /* empty */ }
569 public static class DeviceInfoSettingsActivity extends Settings { /* empty */ }
570 public static class ApplicationSettingsActivity extends Settings { /* empty */ }
571 public static class ManageApplicationsActivity extends Settings { /* empty */ }
572 public static class StorageUseActivity extends Settings { /* empty */ }
573 public static class DevelopmentSettingsActivity extends Settings { /* empty */ }
574 public static class AccessibilitySettingsActivity extends Settings { /* empty */ }
575 public static class SecuritySettingsActivity extends Settings { /* empty */ }
Gilles Debunnea6a8a142011-06-09 11:56:17 -0700576 public static class LocationSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700577 public static class PrivacySettingsActivity extends Settings { /* empty */ }
578 public static class DockSettingsActivity extends Settings { /* empty */ }
579 public static class RunningServicesActivity extends Settings { /* empty */ }
580 public static class ManageAccountsSettingsActivity extends Settings { /* empty */ }
581 public static class PowerUsageSummaryActivity extends Settings { /* empty */ }
582 public static class AccountSyncSettingsActivity extends Settings { /* empty */ }
583 public static class AccountSyncSettingsInAddAccountActivity extends Settings { /* empty */ }
584 public static class CryptKeeperSettingsActivity extends Settings { /* empty */ }
585 public static class DeviceAdminSettingsActivity extends Settings { /* empty */ }
586 public static class DataUsageSummaryActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700587 public static class AdvancedWifiSettingsActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700588 public static class TextToSpeechSettingsActivity extends Settings { /* empty */ }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800589}