blob: 9d000244689894f5f322dca11ef5639bd27fd71e [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 Yamasanib810a0d2012-03-25 10:12:26 -070019import com.android.internal.util.ArrayUtils;
Gilles Debunnecd8e5242011-07-25 11:36:15 -070020import com.android.settings.accounts.AccountSyncSettings;
21import com.android.settings.bluetooth.BluetoothEnabler;
Jeff Sharkey11d30122012-03-19 16:54:07 -070022import com.android.settings.deviceinfo.Memory;
Gilles Debunnecd8e5242011-07-25 11:36:15 -070023import com.android.settings.fuelgauge.PowerUsageSummary;
24import com.android.settings.wifi.WifiEnabler;
25
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080026import android.content.ComponentName;
Gilles Debunnee78c1872011-06-20 15:00:07 -070027import android.content.Context;
Amith Yamasani379d9b02010-09-27 12:03:59 -070028import android.content.Intent;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070029import android.content.pm.ActivityInfo;
30import android.content.pm.PackageManager;
31import android.content.pm.PackageManager.NameNotFoundException;
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -070032import android.os.Bundle;
Amith Yamasanib810a0d2012-03-25 10:12:26 -070033import android.os.UserId;
Amith Yamasania4379d62011-07-22 10:34:58 -070034import android.preference.Preference;
Amith Yamasani02cf71a2010-09-21 15:48:52 -070035import android.preference.PreferenceActivity;
Amith Yamasania4379d62011-07-22 10:34:58 -070036import android.preference.PreferenceFragment;
Gilles Debunnee78c1872011-06-20 15:00:07 -070037import android.text.TextUtils;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080038import android.util.Log;
Gilles Debunnee78c1872011-06-20 15:00:07 -070039import android.view.LayoutInflater;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080040import android.view.View;
41import android.view.View.OnClickListener;
Gilles Debunnee78c1872011-06-20 15:00:07 -070042import android.view.ViewGroup;
43import android.widget.ArrayAdapter;
Amith Yamasani9e3a4702011-01-11 09:09:26 -080044import android.widget.Button;
Gilles Debunnee78c1872011-06-20 15:00:07 -070045import android.widget.ImageView;
46import android.widget.ListAdapter;
47import android.widget.Switch;
48import android.widget.TextView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080049
Gilles Debunnee78c1872011-06-20 15:00:07 -070050import java.util.ArrayList;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070051import java.util.HashMap;
Amith Yamasani02cf71a2010-09-21 15:48:52 -070052import java.util.List;
Amith Yamasanid7993472010-08-18 13:59:28 -070053
54/**
55 * Top-level settings activity to handle single pane and double pane UI layout.
56 */
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -080057public class Settings extends PreferenceActivity implements ButtonBarHandler {
Amith Yamasanid7993472010-08-18 13:59:28 -070058
Gilles Debunnee78c1872011-06-20 15:00:07 -070059 private static final String LOG_TAG = "Settings";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070060 private static final String META_DATA_KEY_HEADER_ID =
Gilles Debunnee78c1872011-06-20 15:00:07 -070061 "com.android.settings.TOP_LEVEL_HEADER_ID";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070062 private static final String META_DATA_KEY_FRAGMENT_CLASS =
Gilles Debunnee78c1872011-06-20 15:00:07 -070063 "com.android.settings.FRAGMENT_CLASS";
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080064 private static final String META_DATA_KEY_PARENT_TITLE =
65 "com.android.settings.PARENT_FRAGMENT_TITLE";
66 private static final String META_DATA_KEY_PARENT_FRAGMENT_CLASS =
67 "com.android.settings.PARENT_FRAGMENT_CLASS";
68
Jeff Sharkey54d0af52011-08-11 18:26:57 -070069 private static final String EXTRA_CLEAR_UI_OPTIONS = "settings:remove_ui_options";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070070
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080071 private static final String SAVE_KEY_CURRENT_HEADER = "com.android.settings.CURRENT_HEADER";
72 private static final String SAVE_KEY_PARENT_HEADER = "com.android.settings.PARENT_HEADER";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070073
74 private String mFragmentClass;
75 private int mTopLevelHeaderId;
Amith Yamasani3965ae62010-11-15 14:45:19 -080076 private Header mFirstHeader;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080077 private Header mCurrentHeader;
78 private Header mParentHeader;
79 private boolean mInLocalHeaderSwitch;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070080
Amith Yamasanib810a0d2012-03-25 10:12:26 -070081 // Show only these settings for restricted users
82 private int[] SETTINGS_FOR_RESTRICTED = {
83 R.id.wifi_settings,
84 R.id.bluetooth_settings,
85 R.id.sound_settings,
86 R.id.display_settings,
87 //R.id.security_settings,
88 R.id.sync_settings,
89 R.id.about_settings
90 };
91
Amith Yamasanifa3eea52012-03-28 10:25:08 -070092 private boolean mEnableUserManagement = false;
93
Amith Yamasani02cf71a2010-09-21 15:48:52 -070094 // TODO: Update Call Settings based on airplane mode state.
Amith Yamasanib61cf512010-09-12 08:17:50 -070095
Amith Yamasani5203bdf2010-11-04 09:59:44 -070096 protected HashMap<Integer, Integer> mHeaderIndexMap = new HashMap<Integer, Integer>();
Gilles Debunnee78c1872011-06-20 15:00:07 -070097 private List<Header> mHeaders;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070098
99 @Override
100 protected void onCreate(Bundle savedInstanceState) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700101 if (getIntent().getBooleanExtra(EXTRA_CLEAR_UI_OPTIONS, false)) {
102 getWindow().setUiOptions(0);
103 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700104
Amith Yamasanifa3eea52012-03-28 10:25:08 -0700105 if (android.provider.Settings.Secure.getInt(getContentResolver(), "multiuser_enabled", -1)
106 > 0) {
107 mEnableUserManagement = true;
108 }
109
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700110 getMetaData();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800111 mInLocalHeaderSwitch = true;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700112 super.onCreate(savedInstanceState);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800113 mInLocalHeaderSwitch = false;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700114
Gilles Debunne3661b622011-06-27 11:19:16 -0700115 if (!onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasani72aa19d2010-12-09 06:07:12 -0800116 highlightHeader();
117 // Force the title so that it doesn't get overridden by a direct launch of
118 // a specific settings screen.
119 setTitle(R.string.settings_label);
120 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800121
122 // Retrieve any saved state
123 if (savedInstanceState != null) {
124 mCurrentHeader = savedInstanceState.getParcelable(SAVE_KEY_CURRENT_HEADER);
125 mParentHeader = savedInstanceState.getParcelable(SAVE_KEY_PARENT_HEADER);
126 }
127
128 // If the current header was saved, switch to it
129 if (savedInstanceState != null && mCurrentHeader != null) {
130 //switchToHeaderLocal(mCurrentHeader);
131 showBreadCrumbs(mCurrentHeader.title, null);
132 }
133
134 if (mParentHeader != null) {
135 setParentTitle(mParentHeader.title, null, new OnClickListener() {
136 public void onClick(View v) {
137 switchToParent(mParentHeader.fragment);
138 }
139 });
140 }
Gilles Debunnedc7101f2011-06-27 12:00:49 -0700141
142 // TODO Add support for android.R.id.home in all Setting's onOptionsItemSelected
143 // getActionBar().setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP,
144 // ActionBar.DISPLAY_HOME_AS_UP);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800145 }
146
147 @Override
148 protected void onSaveInstanceState(Bundle outState) {
149 super.onSaveInstanceState(outState);
150
151 // Save the current fragment, if it is the same as originally launched
152 if (mCurrentHeader != null) {
153 outState.putParcelable(SAVE_KEY_CURRENT_HEADER, mCurrentHeader);
154 }
155 if (mParentHeader != null) {
156 outState.putParcelable(SAVE_KEY_PARENT_HEADER, mParentHeader);
157 }
158 }
159
Gilles Debunnee78c1872011-06-20 15:00:07 -0700160 @Override
161 public void onResume() {
162 super.onResume();
163
164 ListAdapter listAdapter = getListAdapter();
165 if (listAdapter instanceof HeaderAdapter) {
166 ((HeaderAdapter) listAdapter).resume();
167 }
168 }
169
170 @Override
171 public void onPause() {
172 super.onPause();
173
174 ListAdapter listAdapter = getListAdapter();
175 if (listAdapter instanceof HeaderAdapter) {
176 ((HeaderAdapter) listAdapter).pause();
177 }
178 }
179
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800180 private void switchToHeaderLocal(Header header) {
181 mInLocalHeaderSwitch = true;
182 switchToHeader(header);
183 mInLocalHeaderSwitch = false;
184 }
185
186 @Override
187 public void switchToHeader(Header header) {
188 if (!mInLocalHeaderSwitch) {
189 mCurrentHeader = null;
190 mParentHeader = null;
191 }
192 super.switchToHeader(header);
193 }
194
195 /**
196 * Switch to parent fragment and store the grand parent's info
Jake Hamby2748fc22011-01-12 15:06:28 -0800197 * @param className name of the activity wrapper for the parent fragment.
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800198 */
199 private void switchToParent(String className) {
200 final ComponentName cn = new ComponentName(this, className);
201 try {
202 final PackageManager pm = getPackageManager();
203 final ActivityInfo parentInfo = pm.getActivityInfo(cn, PackageManager.GET_META_DATA);
204
205 if (parentInfo != null && parentInfo.metaData != null) {
206 String fragmentClass = parentInfo.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
207 CharSequence fragmentTitle = parentInfo.loadLabel(pm);
208 Header parentHeader = new Header();
209 parentHeader.fragment = fragmentClass;
210 parentHeader.title = fragmentTitle;
211 mCurrentHeader = parentHeader;
212
213 switchToHeaderLocal(parentHeader);
Amith Yamasani990fb522011-09-02 09:47:18 -0700214 highlightHeader();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800215
216 mParentHeader = new Header();
217 mParentHeader.fragment
218 = parentInfo.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
219 mParentHeader.title = parentInfo.metaData.getString(META_DATA_KEY_PARENT_TITLE);
220 }
221 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700222 Log.w(LOG_TAG, "Could not find parent activity : " + className);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800223 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700224 }
225
Amith Yamasani3965ae62010-11-15 14:45:19 -0800226 @Override
227 public void onNewIntent(Intent intent) {
228 super.onNewIntent(intent);
229
230 // If it is not launched from history, then reset to top-level
231 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0
Gilles Debunne3661b622011-06-27 11:19:16 -0700232 && mFirstHeader != null && !onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800233 switchToHeaderLocal(mFirstHeader);
Amith Yamasani3965ae62010-11-15 14:45:19 -0800234 }
235 }
236
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700237 private void highlightHeader() {
238 if (mTopLevelHeaderId != 0) {
239 Integer index = mHeaderIndexMap.get(mTopLevelHeaderId);
240 if (index != null) {
241 getListView().setItemChecked(index, true);
Amith Yamasani990fb522011-09-02 09:47:18 -0700242 getListView().smoothScrollToPosition(index);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700243 }
244 }
245 }
246
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700247 @Override
248 public Intent getIntent() {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700249 Intent superIntent = super.getIntent();
250 String startingFragment = getStartingFragmentClass(superIntent);
Gilles Debunne3661b622011-06-27 11:19:16 -0700251 // This is called from super.onCreate, isMultiPane() is not yet reliable
252 // Do not use onIsHidingHeaders either, which relies itself on this method
253 if (startingFragment != null && !onIsMultiPane()) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700254 Intent modIntent = new Intent(superIntent);
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700255 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700256 Bundle args = superIntent.getExtras();
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700257 if (args != null) {
258 args = new Bundle(args);
259 } else {
260 args = new Bundle();
261 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700262 args.putParcelable("intent", superIntent);
263 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras());
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700264 return modIntent;
265 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700266 return superIntent;
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700267 }
268
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700269 /**
Amith Yamasani379d9b02010-09-27 12:03:59 -0700270 * Checks if the component name in the intent is different from the Settings class and
271 * returns the class name to load as a fragment.
272 */
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700273 protected String getStartingFragmentClass(Intent intent) {
274 if (mFragmentClass != null) return mFragmentClass;
275
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700276 String intentClass = intent.getComponent().getClassName();
Amith Yamasani379d9b02010-09-27 12:03:59 -0700277 if (intentClass.equals(getClass().getName())) return null;
278
Dianne Hackbornee293792010-11-01 12:32:33 -0700279 if ("com.android.settings.ManageApplications".equals(intentClass)
280 || "com.android.settings.RunningServices".equals(intentClass)
281 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700282 // Old names of manage apps.
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700283 intentClass = com.android.settings.applications.ManageApplications.class.getName();
284 }
285
Amith Yamasani379d9b02010-09-27 12:03:59 -0700286 return intentClass;
287 }
288
289 /**
290 * Override initial header when an activity-alias is causing Settings to be launched
291 * for a specific fragment encoded in the android:name parameter.
292 */
293 @Override
294 public Header onGetInitialHeader() {
295 String fragmentClass = getStartingFragmentClass(super.getIntent());
296 if (fragmentClass != null) {
297 Header header = new Header();
298 header.fragment = fragmentClass;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800299 header.title = getTitle();
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700300 header.fragmentArguments = getIntent().getExtras();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800301 mCurrentHeader = header;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700302 return header;
303 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700304
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700305 return mFirstHeader;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700306 }
307
Dianne Hackbornb7258182011-03-15 16:23:55 -0700308 @Override
Dianne Hackborn48147dc2011-03-18 12:29:41 -0700309 public Intent onBuildStartFragmentIntent(String fragmentName, Bundle args,
310 int titleRes, int shortTitleRes) {
311 Intent intent = super.onBuildStartFragmentIntent(fragmentName, args,
312 titleRes, shortTitleRes);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700313
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700314 // some fragments want to avoid split actionbar
Gilles Debunne162e5412011-07-11 14:13:31 -0700315 if (DataUsageSummary.class.getName().equals(fragmentName) ||
316 PowerUsageSummary.class.getName().equals(fragmentName) ||
Gilles Debunnecd8e5242011-07-25 11:36:15 -0700317 AccountSyncSettings.class.getName().equals(fragmentName) ||
Jeff Sharkey11d30122012-03-19 16:54:07 -0700318 UserDictionarySettings.class.getName().equals(fragmentName) ||
319 Memory.class.getName().equals(fragmentName)) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700320 intent.putExtra(EXTRA_CLEAR_UI_OPTIONS, true);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700321 }
322
Dianne Hackbornb7258182011-03-15 16:23:55 -0700323 intent.setClass(this, SubSettings.class);
324 return intent;
325 }
326
Amith Yamasani379d9b02010-09-27 12:03:59 -0700327 /**
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700328 * Populate the activity with the top-level headers.
329 */
Amith Yamasanid7993472010-08-18 13:59:28 -0700330 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700331 public void onBuildHeaders(List<Header> headers) {
332 loadHeadersFromResource(R.xml.settings_headers, headers);
Amith Yamasanid7993472010-08-18 13:59:28 -0700333
Gilles Debunnee78c1872011-06-20 15:00:07 -0700334 updateHeaderList(headers);
335
336 mHeaders = headers;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700337 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700338
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700339 private void updateHeaderList(List<Header> target) {
340 int i = 0;
341 while (i < target.size()) {
342 Header header = target.get(i);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700343 // Ids are integers, so downcasting
344 int id = (int) header.id;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700345 if (id == R.id.dock_settings) {
346 if (!needsDockSettings())
347 target.remove(header);
348 } else if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
349 Utils.updateHeaderToSpecificActivityFromMetaDataOrRemove(this, target, header);
Gilles Debunne2454f492011-06-21 16:16:06 -0700350 } else if (id == R.id.wifi_settings) {
351 // Remove WiFi Settings if WiFi service is not available.
352 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
353 target.remove(header);
354 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700355 } else if (id == R.id.bluetooth_settings) {
356 // Remove Bluetooth Settings if Bluetooth service is not available.
Gilles Debunne2454f492011-06-21 16:16:06 -0700357 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700358 target.remove(header);
359 }
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700360 } else if (id == R.id.user_settings) {
Amith Yamasanifa3eea52012-03-28 10:25:08 -0700361 if (!mEnableUserManagement
362 || !UserId.MU_ENABLED || UserId.myUserId() != 0
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700363 || !getResources().getBoolean(R.bool.enable_user_management)
364 || Utils.isMonkeyRunning()) {
365 target.remove(header);
366 }
367 }
368 if (UserId.MU_ENABLED && UserId.myUserId() != 0
369 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
370 target.remove(header);
Amith Yamasanid7993472010-08-18 13:59:28 -0700371 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700372
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700373 // Increment if the current one wasn't removed by the Utils code.
374 if (target.get(i) == header) {
Amith Yamasani3965ae62010-11-15 14:45:19 -0800375 // Hold on to the first header, when we need to reset to the top-level
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700376 if (mFirstHeader == null &&
377 HeaderAdapter.getHeaderType(header) != HeaderAdapter.HEADER_TYPE_CATEGORY) {
378 mFirstHeader = header;
379 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700380 mHeaderIndexMap.put(id, i);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700381 i++;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700382 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700383 }
384 }
385
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700386 private boolean needsDockSettings() {
387 return getResources().getBoolean(R.bool.has_dock_settings);
Amith Yamasanib61cf512010-09-12 08:17:50 -0700388 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700389
390 private void getMetaData() {
391 try {
392 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
393 PackageManager.GET_META_DATA);
394 if (ai == null || ai.metaData == null) return;
395 mTopLevelHeaderId = ai.metaData.getInt(META_DATA_KEY_HEADER_ID);
396 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800397
398 // Check if it has a parent specified and create a Header object
399 final int parentHeaderTitleRes = ai.metaData.getInt(META_DATA_KEY_PARENT_TITLE);
400 String parentFragmentClass = ai.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
401 if (parentFragmentClass != null) {
402 mParentHeader = new Header();
403 mParentHeader.fragment = parentFragmentClass;
404 if (parentHeaderTitleRes != 0) {
405 mParentHeader.title = getResources().getString(parentHeaderTitleRes);
406 }
407 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700408 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700409 // No recovery
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700410 }
411 }
412
Amith Yamasani9e3a4702011-01-11 09:09:26 -0800413 @Override
414 public boolean hasNextButton() {
415 return super.hasNextButton();
416 }
417
418 @Override
419 public Button getNextButton() {
420 return super.getNextButton();
421 }
422
Gilles Debunnee78c1872011-06-20 15:00:07 -0700423 private static class HeaderAdapter extends ArrayAdapter<Header> {
424 static final int HEADER_TYPE_CATEGORY = 0;
425 static final int HEADER_TYPE_NORMAL = 1;
426 static final int HEADER_TYPE_SWITCH = 2;
427 private static final int HEADER_TYPE_COUNT = HEADER_TYPE_SWITCH + 1;
428
429 private final WifiEnabler mWifiEnabler;
430 private final BluetoothEnabler mBluetoothEnabler;
431
432 private static class HeaderViewHolder {
433 ImageView icon;
434 TextView title;
435 TextView summary;
436 Switch switch_;
437 }
438
439 private LayoutInflater mInflater;
440
441 static int getHeaderType(Header header) {
442 if (header.fragment == null && header.intent == null) {
443 return HEADER_TYPE_CATEGORY;
444 } else if (header.id == R.id.wifi_settings || header.id == R.id.bluetooth_settings) {
445 return HEADER_TYPE_SWITCH;
446 } else {
447 return HEADER_TYPE_NORMAL;
448 }
449 }
450
451 @Override
452 public int getItemViewType(int position) {
453 Header header = getItem(position);
454 return getHeaderType(header);
455 }
456
457 @Override
458 public boolean areAllItemsEnabled() {
459 return false; // because of categories
460 }
461
462 @Override
463 public boolean isEnabled(int position) {
464 return getItemViewType(position) != HEADER_TYPE_CATEGORY;
465 }
466
467 @Override
468 public int getViewTypeCount() {
469 return HEADER_TYPE_COUNT;
470 }
471
472 @Override
473 public boolean hasStableIds() {
474 return true;
475 }
476
477 public HeaderAdapter(Context context, List<Header> objects) {
478 super(context, 0, objects);
479 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
480
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700481 // Temp Switches provided as placeholder until the adapter replaces these with actual
Gilles Debunnee78c1872011-06-20 15:00:07 -0700482 // Switches inflated from their layouts. Must be done before adapter is set in super
483 mWifiEnabler = new WifiEnabler(context, new Switch(context));
484 mBluetoothEnabler = new BluetoothEnabler(context, new Switch(context));
485 }
486
487 @Override
488 public View getView(int position, View convertView, ViewGroup parent) {
489 HeaderViewHolder holder;
490 Header header = getItem(position);
491 int headerType = getHeaderType(header);
492 View view = null;
493
494 if (convertView == null) {
495 holder = new HeaderViewHolder();
496 switch (headerType) {
497 case HEADER_TYPE_CATEGORY:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700498 view = new TextView(getContext(), null,
499 android.R.attr.listSeparatorTextViewStyle);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700500 holder.title = (TextView) view;
501 break;
502
503 case HEADER_TYPE_SWITCH:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700504 view = mInflater.inflate(R.layout.preference_header_switch_item, parent,
505 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700506 holder.icon = (ImageView) view.findViewById(R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700507 holder.title = (TextView)
508 view.findViewById(com.android.internal.R.id.title);
509 holder.summary = (TextView)
510 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700511 holder.switch_ = (Switch) view.findViewById(R.id.switchWidget);
512 break;
513
514 case HEADER_TYPE_NORMAL:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700515 view = mInflater.inflate(
516 com.android.internal.R.layout.preference_header_item, parent,
517 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700518 holder.icon = (ImageView) view.findViewById(com.android.internal.R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700519 holder.title = (TextView)
520 view.findViewById(com.android.internal.R.id.title);
521 holder.summary = (TextView)
522 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700523 break;
524 }
525 view.setTag(holder);
526 } else {
527 view = convertView;
528 holder = (HeaderViewHolder) view.getTag();
529 }
530
531 // All view fields must be updated every time, because the view may be recycled
532 switch (headerType) {
533 case HEADER_TYPE_CATEGORY:
534 holder.title.setText(header.getTitle(getContext().getResources()));
535 break;
536
537 case HEADER_TYPE_SWITCH:
538 // Would need a different treatment if the main menu had more switches
539 if (header.id == R.id.wifi_settings) {
540 mWifiEnabler.setSwitch(holder.switch_);
541 } else {
542 mBluetoothEnabler.setSwitch(holder.switch_);
543 }
544 // No break, fall through on purpose to update common fields
545
546 //$FALL-THROUGH$
547 case HEADER_TYPE_NORMAL:
548 holder.icon.setImageResource(header.iconRes);
549 holder.title.setText(header.getTitle(getContext().getResources()));
550 CharSequence summary = header.getSummary(getContext().getResources());
551 if (!TextUtils.isEmpty(summary)) {
552 holder.summary.setVisibility(View.VISIBLE);
553 holder.summary.setText(summary);
554 } else {
555 holder.summary.setVisibility(View.GONE);
556 }
557 break;
558 }
559
560 return view;
561 }
562
563 public void resume() {
564 mWifiEnabler.resume();
565 mBluetoothEnabler.resume();
566 }
567
568 public void pause() {
569 mWifiEnabler.pause();
570 mBluetoothEnabler.pause();
571 }
572 }
573
574 @Override
Amith Yamasania4379d62011-07-22 10:34:58 -0700575 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
576 // Override the fragment title for Wallpaper settings
Amith Yamasanidfb65432011-11-29 16:38:14 -0800577 int titleRes = pref.getTitleRes();
Amith Yamasania4379d62011-07-22 10:34:58 -0700578 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
Amith Yamasanidfb65432011-11-29 16:38:14 -0800579 titleRes = R.string.wallpaper_settings_fragment_title;
Amith Yamasania4379d62011-07-22 10:34:58 -0700580 }
Amith Yamasanidfb65432011-11-29 16:38:14 -0800581 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, null, null, 0);
Amith Yamasania4379d62011-07-22 10:34:58 -0700582 return true;
583 }
584
585 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700586 public void setListAdapter(ListAdapter adapter) {
587 if (mHeaders == null) {
588 mHeaders = new ArrayList<Header>();
589 // When the saved state provides the list of headers, onBuildHeaders is not called
590 // Copy the list of Headers from the adapter, preserving their order
591 for (int i = 0; i < adapter.getCount(); i++) {
592 mHeaders.add((Header) adapter.getItem(i));
593 }
594 }
595
596 // Ignore the adapter provided by PreferenceActivity and substitute ours instead
597 super.setListAdapter(new HeaderAdapter(this, mHeaders));
598 }
599
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700600 /*
601 * Settings subclasses for launching independently.
602 */
Gilles Debunnee78c1872011-06-20 15:00:07 -0700603 public static class BluetoothSettingsActivity extends Settings { /* empty */ }
604 public static class WirelessSettingsActivity extends Settings { /* empty */ }
605 public static class TetherSettingsActivity extends Settings { /* empty */ }
606 public static class VpnSettingsActivity extends Settings { /* empty */ }
607 public static class DateTimeSettingsActivity extends Settings { /* empty */ }
608 public static class StorageSettingsActivity extends Settings { /* empty */ }
609 public static class WifiSettingsActivity extends Settings { /* empty */ }
repo syncb98463f2011-06-30 10:58:43 -0700610 public static class WifiP2pSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700611 public static class InputMethodAndLanguageSettingsActivity extends Settings { /* empty */ }
Jeff Browne46c5f32012-04-05 11:42:18 -0700612 public static class KeyboardLayoutPickerActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700613 public static class InputMethodAndSubtypeEnablerActivity extends Settings { /* empty */ }
satoke077d2b2011-07-25 09:38:11 +0900614 public static class SpellCheckersSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700615 public static class LocalePickerActivity extends Settings { /* empty */ }
616 public static class UserDictionarySettingsActivity extends Settings { /* empty */ }
617 public static class SoundSettingsActivity extends Settings { /* empty */ }
618 public static class DisplaySettingsActivity extends Settings { /* empty */ }
619 public static class DeviceInfoSettingsActivity extends Settings { /* empty */ }
620 public static class ApplicationSettingsActivity extends Settings { /* empty */ }
621 public static class ManageApplicationsActivity extends Settings { /* empty */ }
622 public static class StorageUseActivity extends Settings { /* empty */ }
623 public static class DevelopmentSettingsActivity extends Settings { /* empty */ }
624 public static class AccessibilitySettingsActivity extends Settings { /* empty */ }
625 public static class SecuritySettingsActivity extends Settings { /* empty */ }
Gilles Debunnea6a8a142011-06-09 11:56:17 -0700626 public static class LocationSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700627 public static class PrivacySettingsActivity extends Settings { /* empty */ }
628 public static class DockSettingsActivity extends Settings { /* empty */ }
629 public static class RunningServicesActivity extends Settings { /* empty */ }
630 public static class ManageAccountsSettingsActivity extends Settings { /* empty */ }
631 public static class PowerUsageSummaryActivity extends Settings { /* empty */ }
632 public static class AccountSyncSettingsActivity extends Settings { /* empty */ }
633 public static class AccountSyncSettingsInAddAccountActivity extends Settings { /* empty */ }
634 public static class CryptKeeperSettingsActivity extends Settings { /* empty */ }
635 public static class DeviceAdminSettingsActivity extends Settings { /* empty */ }
636 public static class DataUsageSummaryActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700637 public static class AdvancedWifiSettingsActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700638 public static class TextToSpeechSettingsActivity extends Settings { /* empty */ }
Jeff Hamilton3d670de2011-09-21 16:44:36 -0500639 public static class AndroidBeamSettingsActivity extends Settings { /* empty */ }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800640}