blob: a8599bfe9c926bc028a807617ee553fdf0bb4363 [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;
Amith Yamasanid1ab8282012-05-18 09:50:08 -070021import com.android.settings.accounts.AuthenticatorHelper;
22import com.android.settings.accounts.ManageAccountsSettings;
Adam Powellfaba7e42012-03-26 17:28:38 -070023import com.android.settings.applications.ManageApplications;
Gilles Debunnecd8e5242011-07-25 11:36:15 -070024import com.android.settings.bluetooth.BluetoothEnabler;
Jeff Sharkey11d30122012-03-19 16:54:07 -070025import com.android.settings.deviceinfo.Memory;
Gilles Debunnecd8e5242011-07-25 11:36:15 -070026import com.android.settings.fuelgauge.PowerUsageSummary;
27import com.android.settings.wifi.WifiEnabler;
28
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080029import android.content.ComponentName;
Gilles Debunnee78c1872011-06-20 15:00:07 -070030import android.content.Context;
Amith Yamasani379d9b02010-09-27 12:03:59 -070031import android.content.Intent;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070032import android.content.pm.ActivityInfo;
33import android.content.pm.PackageManager;
34import android.content.pm.PackageManager.NameNotFoundException;
Amith Yamasanid1ab8282012-05-18 09:50:08 -070035import android.graphics.drawable.Drawable;
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -070036import android.os.Bundle;
Jeff Sharkey34e964d2012-04-21 15:41:48 -070037import android.os.INetworkManagementService;
38import android.os.RemoteException;
39import android.os.ServiceManager;
Amith Yamasanib810a0d2012-03-25 10:12:26 -070040import android.os.UserId;
Amith Yamasania4379d62011-07-22 10:34:58 -070041import android.preference.Preference;
Amith Yamasani02cf71a2010-09-21 15:48:52 -070042import android.preference.PreferenceActivity;
Amith Yamasanid1ab8282012-05-18 09:50:08 -070043import android.preference.PreferenceActivity.Header;
Amith Yamasania4379d62011-07-22 10:34:58 -070044import android.preference.PreferenceFragment;
Gilles Debunnee78c1872011-06-20 15:00:07 -070045import android.text.TextUtils;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080046import android.util.Log;
Gilles Debunnee78c1872011-06-20 15:00:07 -070047import android.view.LayoutInflater;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080048import android.view.View;
49import android.view.View.OnClickListener;
Gilles Debunnee78c1872011-06-20 15:00:07 -070050import android.view.ViewGroup;
51import android.widget.ArrayAdapter;
Amith Yamasani9e3a4702011-01-11 09:09:26 -080052import android.widget.Button;
Gilles Debunnee78c1872011-06-20 15:00:07 -070053import android.widget.ImageView;
54import android.widget.ListAdapter;
55import android.widget.Switch;
56import android.widget.TextView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080057
Gilles Debunnee78c1872011-06-20 15:00:07 -070058import java.util.ArrayList;
Amith Yamasanid1ab8282012-05-18 09:50:08 -070059import java.util.Collections;
60import java.util.Comparator;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070061import java.util.HashMap;
Amith Yamasani02cf71a2010-09-21 15:48:52 -070062import java.util.List;
Amith Yamasanid7993472010-08-18 13:59:28 -070063
64/**
65 * Top-level settings activity to handle single pane and double pane UI layout.
66 */
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -080067public class Settings extends PreferenceActivity implements ButtonBarHandler {
Amith Yamasanid7993472010-08-18 13:59:28 -070068
Gilles Debunnee78c1872011-06-20 15:00:07 -070069 private static final String LOG_TAG = "Settings";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070070 private static final String META_DATA_KEY_HEADER_ID =
Gilles Debunnee78c1872011-06-20 15:00:07 -070071 "com.android.settings.TOP_LEVEL_HEADER_ID";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070072 private static final String META_DATA_KEY_FRAGMENT_CLASS =
Gilles Debunnee78c1872011-06-20 15:00:07 -070073 "com.android.settings.FRAGMENT_CLASS";
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080074 private static final String META_DATA_KEY_PARENT_TITLE =
75 "com.android.settings.PARENT_FRAGMENT_TITLE";
76 private static final String META_DATA_KEY_PARENT_FRAGMENT_CLASS =
77 "com.android.settings.PARENT_FRAGMENT_CLASS";
78
Jeff Sharkey54d0af52011-08-11 18:26:57 -070079 private static final String EXTRA_CLEAR_UI_OPTIONS = "settings:remove_ui_options";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070080
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080081 private static final String SAVE_KEY_CURRENT_HEADER = "com.android.settings.CURRENT_HEADER";
82 private static final String SAVE_KEY_PARENT_HEADER = "com.android.settings.PARENT_HEADER";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070083
84 private String mFragmentClass;
85 private int mTopLevelHeaderId;
Amith Yamasani3965ae62010-11-15 14:45:19 -080086 private Header mFirstHeader;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080087 private Header mCurrentHeader;
88 private Header mParentHeader;
89 private boolean mInLocalHeaderSwitch;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070090
Amith Yamasanib810a0d2012-03-25 10:12:26 -070091 // Show only these settings for restricted users
92 private int[] SETTINGS_FOR_RESTRICTED = {
93 R.id.wifi_settings,
94 R.id.bluetooth_settings,
95 R.id.sound_settings,
96 R.id.display_settings,
Amith Yamasani3deeeb72012-04-05 14:44:48 -070097 R.id.security_settings,
Amith Yamasanid1ab8282012-05-18 09:50:08 -070098 R.id.account_settings,
Amith Yamasanib810a0d2012-03-25 10:12:26 -070099 R.id.about_settings
100 };
101
Amith Yamasanifa3eea52012-03-28 10:25:08 -0700102 private boolean mEnableUserManagement = false;
103
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700104 // TODO: Update Call Settings based on airplane mode state.
Amith Yamasanib61cf512010-09-12 08:17:50 -0700105
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700106 protected HashMap<Integer, Integer> mHeaderIndexMap = new HashMap<Integer, Integer>();
Gilles Debunnee78c1872011-06-20 15:00:07 -0700107 private List<Header> mHeaders;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700108
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700109 private AuthenticatorHelper mAuthenticatorHelper;
110 private Header mLastHeader;
111
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700112 @Override
113 protected void onCreate(Bundle savedInstanceState) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700114 if (getIntent().getBooleanExtra(EXTRA_CLEAR_UI_OPTIONS, false)) {
115 getWindow().setUiOptions(0);
116 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700117
Amith Yamasanifa3eea52012-03-28 10:25:08 -0700118 if (android.provider.Settings.Secure.getInt(getContentResolver(), "multiuser_enabled", -1)
119 > 0) {
120 mEnableUserManagement = true;
121 }
122
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700123 mAuthenticatorHelper = new AuthenticatorHelper();
124 mAuthenticatorHelper.updateAuthDescriptions(this);
125 mAuthenticatorHelper.onAccountsUpdated(this, null);
126
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700127 getMetaData();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800128 mInLocalHeaderSwitch = true;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700129 super.onCreate(savedInstanceState);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800130 mInLocalHeaderSwitch = false;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700131
Gilles Debunne3661b622011-06-27 11:19:16 -0700132 if (!onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700133 highlightHeader(mTopLevelHeaderId);
Amith Yamasani72aa19d2010-12-09 06:07:12 -0800134 // Force the title so that it doesn't get overridden by a direct launch of
135 // a specific settings screen.
136 setTitle(R.string.settings_label);
137 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800138
139 // Retrieve any saved state
140 if (savedInstanceState != null) {
141 mCurrentHeader = savedInstanceState.getParcelable(SAVE_KEY_CURRENT_HEADER);
142 mParentHeader = savedInstanceState.getParcelable(SAVE_KEY_PARENT_HEADER);
143 }
144
145 // If the current header was saved, switch to it
146 if (savedInstanceState != null && mCurrentHeader != null) {
147 //switchToHeaderLocal(mCurrentHeader);
148 showBreadCrumbs(mCurrentHeader.title, null);
149 }
150
151 if (mParentHeader != null) {
152 setParentTitle(mParentHeader.title, null, new OnClickListener() {
153 public void onClick(View v) {
154 switchToParent(mParentHeader.fragment);
155 }
156 });
157 }
Gilles Debunnedc7101f2011-06-27 12:00:49 -0700158
Amith Yamasani3d384f42012-05-11 15:22:04 -0700159 // Override up navigation for multi-pane, since we handle it in the fragment breadcrumbs
160 if (onIsMultiPane()) {
161 getActionBar().setDisplayHomeAsUpEnabled(false);
162 getActionBar().setHomeButtonEnabled(false);
163 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800164 }
165
166 @Override
167 protected void onSaveInstanceState(Bundle outState) {
168 super.onSaveInstanceState(outState);
169
170 // Save the current fragment, if it is the same as originally launched
171 if (mCurrentHeader != null) {
172 outState.putParcelable(SAVE_KEY_CURRENT_HEADER, mCurrentHeader);
173 }
174 if (mParentHeader != null) {
175 outState.putParcelable(SAVE_KEY_PARENT_HEADER, mParentHeader);
176 }
177 }
178
Gilles Debunnee78c1872011-06-20 15:00:07 -0700179 @Override
180 public void onResume() {
181 super.onResume();
182
183 ListAdapter listAdapter = getListAdapter();
184 if (listAdapter instanceof HeaderAdapter) {
185 ((HeaderAdapter) listAdapter).resume();
186 }
187 }
188
189 @Override
190 public void onPause() {
191 super.onPause();
192
193 ListAdapter listAdapter = getListAdapter();
194 if (listAdapter instanceof HeaderAdapter) {
195 ((HeaderAdapter) listAdapter).pause();
196 }
197 }
198
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800199 private void switchToHeaderLocal(Header header) {
200 mInLocalHeaderSwitch = true;
201 switchToHeader(header);
202 mInLocalHeaderSwitch = false;
203 }
204
205 @Override
206 public void switchToHeader(Header header) {
207 if (!mInLocalHeaderSwitch) {
208 mCurrentHeader = null;
209 mParentHeader = null;
210 }
211 super.switchToHeader(header);
212 }
213
214 /**
215 * Switch to parent fragment and store the grand parent's info
Jake Hamby2748fc22011-01-12 15:06:28 -0800216 * @param className name of the activity wrapper for the parent fragment.
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800217 */
218 private void switchToParent(String className) {
219 final ComponentName cn = new ComponentName(this, className);
220 try {
221 final PackageManager pm = getPackageManager();
222 final ActivityInfo parentInfo = pm.getActivityInfo(cn, PackageManager.GET_META_DATA);
223
224 if (parentInfo != null && parentInfo.metaData != null) {
225 String fragmentClass = parentInfo.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
226 CharSequence fragmentTitle = parentInfo.loadLabel(pm);
227 Header parentHeader = new Header();
228 parentHeader.fragment = fragmentClass;
229 parentHeader.title = fragmentTitle;
230 mCurrentHeader = parentHeader;
231
232 switchToHeaderLocal(parentHeader);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700233 highlightHeader(mTopLevelHeaderId);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800234
235 mParentHeader = new Header();
236 mParentHeader.fragment
237 = parentInfo.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
238 mParentHeader.title = parentInfo.metaData.getString(META_DATA_KEY_PARENT_TITLE);
239 }
240 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700241 Log.w(LOG_TAG, "Could not find parent activity : " + className);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800242 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700243 }
244
Amith Yamasani3965ae62010-11-15 14:45:19 -0800245 @Override
246 public void onNewIntent(Intent intent) {
247 super.onNewIntent(intent);
248
249 // If it is not launched from history, then reset to top-level
250 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0
Gilles Debunne3661b622011-06-27 11:19:16 -0700251 && mFirstHeader != null && !onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800252 switchToHeaderLocal(mFirstHeader);
Amith Yamasani3965ae62010-11-15 14:45:19 -0800253 }
254 }
255
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700256 private void highlightHeader(int id) {
257 if (id != 0) {
258 Integer index = mHeaderIndexMap.get(id);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700259 if (index != null) {
260 getListView().setItemChecked(index, true);
Amith Yamasani990fb522011-09-02 09:47:18 -0700261 getListView().smoothScrollToPosition(index);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700262 }
263 }
264 }
265
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700266 @Override
267 public Intent getIntent() {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700268 Intent superIntent = super.getIntent();
269 String startingFragment = getStartingFragmentClass(superIntent);
Gilles Debunne3661b622011-06-27 11:19:16 -0700270 // This is called from super.onCreate, isMultiPane() is not yet reliable
271 // Do not use onIsHidingHeaders either, which relies itself on this method
272 if (startingFragment != null && !onIsMultiPane()) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700273 Intent modIntent = new Intent(superIntent);
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700274 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700275 Bundle args = superIntent.getExtras();
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700276 if (args != null) {
277 args = new Bundle(args);
278 } else {
279 args = new Bundle();
280 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700281 args.putParcelable("intent", superIntent);
282 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras());
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700283 return modIntent;
284 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700285 return superIntent;
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700286 }
287
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700288 /**
Amith Yamasani379d9b02010-09-27 12:03:59 -0700289 * Checks if the component name in the intent is different from the Settings class and
290 * returns the class name to load as a fragment.
291 */
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700292 protected String getStartingFragmentClass(Intent intent) {
293 if (mFragmentClass != null) return mFragmentClass;
294
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700295 String intentClass = intent.getComponent().getClassName();
Amith Yamasani379d9b02010-09-27 12:03:59 -0700296 if (intentClass.equals(getClass().getName())) return null;
297
Dianne Hackbornee293792010-11-01 12:32:33 -0700298 if ("com.android.settings.ManageApplications".equals(intentClass)
299 || "com.android.settings.RunningServices".equals(intentClass)
300 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700301 // Old names of manage apps.
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700302 intentClass = com.android.settings.applications.ManageApplications.class.getName();
303 }
304
Amith Yamasani379d9b02010-09-27 12:03:59 -0700305 return intentClass;
306 }
307
308 /**
309 * Override initial header when an activity-alias is causing Settings to be launched
310 * for a specific fragment encoded in the android:name parameter.
311 */
312 @Override
313 public Header onGetInitialHeader() {
314 String fragmentClass = getStartingFragmentClass(super.getIntent());
315 if (fragmentClass != null) {
316 Header header = new Header();
317 header.fragment = fragmentClass;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800318 header.title = getTitle();
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700319 header.fragmentArguments = getIntent().getExtras();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800320 mCurrentHeader = header;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700321 return header;
322 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700323
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700324 return mFirstHeader;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700325 }
326
Dianne Hackbornb7258182011-03-15 16:23:55 -0700327 @Override
Dianne Hackborn48147dc2011-03-18 12:29:41 -0700328 public Intent onBuildStartFragmentIntent(String fragmentName, Bundle args,
329 int titleRes, int shortTitleRes) {
330 Intent intent = super.onBuildStartFragmentIntent(fragmentName, args,
331 titleRes, shortTitleRes);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700332
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700333 // some fragments want to avoid split actionbar
Gilles Debunne162e5412011-07-11 14:13:31 -0700334 if (DataUsageSummary.class.getName().equals(fragmentName) ||
335 PowerUsageSummary.class.getName().equals(fragmentName) ||
Gilles Debunnecd8e5242011-07-25 11:36:15 -0700336 AccountSyncSettings.class.getName().equals(fragmentName) ||
Jeff Sharkey11d30122012-03-19 16:54:07 -0700337 UserDictionarySettings.class.getName().equals(fragmentName) ||
Adam Powellfaba7e42012-03-26 17:28:38 -0700338 Memory.class.getName().equals(fragmentName) ||
Jeff Sharkeya339cba2012-05-08 11:42:49 -0700339 ManageApplications.class.getName().equals(fragmentName) ||
340 WirelessSettings.class.getName().equals(fragmentName) ||
341 SoundSettings.class.getName().equals(fragmentName) ||
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700342 PrivacySettings.class.getName().equals(fragmentName) ||
343 ManageAccountsSettings.class.getName().equals(fragmentName)) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700344 intent.putExtra(EXTRA_CLEAR_UI_OPTIONS, true);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700345 }
346
Dianne Hackbornb7258182011-03-15 16:23:55 -0700347 intent.setClass(this, SubSettings.class);
348 return intent;
349 }
350
Amith Yamasani379d9b02010-09-27 12:03:59 -0700351 /**
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700352 * Populate the activity with the top-level headers.
353 */
Amith Yamasanid7993472010-08-18 13:59:28 -0700354 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700355 public void onBuildHeaders(List<Header> headers) {
356 loadHeadersFromResource(R.xml.settings_headers, headers);
Amith Yamasanid7993472010-08-18 13:59:28 -0700357
Gilles Debunnee78c1872011-06-20 15:00:07 -0700358 updateHeaderList(headers);
359
360 mHeaders = headers;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700361 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700362
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700363 private void updateHeaderList(List<Header> target) {
364 int i = 0;
365 while (i < target.size()) {
366 Header header = target.get(i);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700367 // Ids are integers, so downcasting
368 int id = (int) header.id;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700369 if (id == R.id.dock_settings) {
370 if (!needsDockSettings())
371 target.remove(header);
372 } else if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
373 Utils.updateHeaderToSpecificActivityFromMetaDataOrRemove(this, target, header);
Gilles Debunne2454f492011-06-21 16:16:06 -0700374 } else if (id == R.id.wifi_settings) {
375 // Remove WiFi Settings if WiFi service is not available.
376 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
377 target.remove(header);
378 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700379 } else if (id == R.id.bluetooth_settings) {
380 // Remove Bluetooth Settings if Bluetooth service is not available.
Gilles Debunne2454f492011-06-21 16:16:06 -0700381 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700382 target.remove(header);
383 }
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700384 } else if (id == R.id.data_usage_settings) {
385 // Remove data usage when kernel module not enabled
386 final INetworkManagementService netManager = INetworkManagementService.Stub
387 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
388 try {
389 if (!netManager.isBandwidthControlEnabled()) {
390 target.remove(header);
391 }
392 } catch (RemoteException e) {
393 // ignored
394 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700395 } else if (id == R.id.account_settings) {
396 int headerIndex = i + 1;
397 i = insertAccountsHeaders(target, headerIndex);
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700398 } else if (id == R.id.user_settings) {
Amith Yamasanifa3eea52012-03-28 10:25:08 -0700399 if (!mEnableUserManagement
400 || !UserId.MU_ENABLED || UserId.myUserId() != 0
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700401 || !getResources().getBoolean(R.bool.enable_user_management)
402 || Utils.isMonkeyRunning()) {
403 target.remove(header);
404 }
405 }
406 if (UserId.MU_ENABLED && UserId.myUserId() != 0
407 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
408 target.remove(header);
Amith Yamasanid7993472010-08-18 13:59:28 -0700409 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700410
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700411 // Increment if the current one wasn't removed by the Utils code.
412 if (target.get(i) == header) {
Amith Yamasani3965ae62010-11-15 14:45:19 -0800413 // Hold on to the first header, when we need to reset to the top-level
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700414 if (mFirstHeader == null &&
415 HeaderAdapter.getHeaderType(header) != HeaderAdapter.HEADER_TYPE_CATEGORY) {
416 mFirstHeader = header;
417 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700418 mHeaderIndexMap.put(id, i);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700419 i++;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700420 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700421 }
422 }
423
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700424 private int insertAccountsHeaders(List<Header> target, int headerIndex) {
425 String[] accountTypes = mAuthenticatorHelper.getEnabledAccountTypes();
426 List<Header> accountHeaders = new ArrayList<Header>(accountTypes.length);
427 for (String accountType : accountTypes) {
428 CharSequence label = mAuthenticatorHelper.getLabelForType(this, accountType);
429 Header accHeader = new Header();
430 accHeader.title = label;
431 if (accHeader.extras == null) {
432 accHeader.extras = new Bundle();
433 }
434 accHeader.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
435 accHeader.breadCrumbTitle = label;
436 accHeader.breadCrumbShortTitle = label;
437 accHeader.fragment = ManageAccountsSettings.class.getName();
438 accHeader.fragmentArguments = new Bundle();
439 accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE,
440 accountType);
441 if (!isMultiPane()) {
442 accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_LABEL,
443 label.toString());
444 }
445 accountHeaders.add(accHeader);
446 }
447
448 // Sort by label
449 Collections.sort(accountHeaders, new Comparator<Header>() {
450 @Override
451 public int compare(Header h1, Header h2) {
452 return h1.title.toString().compareTo(h2.title.toString());
453 }
454 });
455
456 for (Header header : accountHeaders) {
457 target.add(headerIndex++, header);
458 }
459 return headerIndex;
460 }
461
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700462 private boolean needsDockSettings() {
463 return getResources().getBoolean(R.bool.has_dock_settings);
Amith Yamasanib61cf512010-09-12 08:17:50 -0700464 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700465
466 private void getMetaData() {
467 try {
468 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
469 PackageManager.GET_META_DATA);
470 if (ai == null || ai.metaData == null) return;
471 mTopLevelHeaderId = ai.metaData.getInt(META_DATA_KEY_HEADER_ID);
472 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700473
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800474 // Check if it has a parent specified and create a Header object
475 final int parentHeaderTitleRes = ai.metaData.getInt(META_DATA_KEY_PARENT_TITLE);
476 String parentFragmentClass = ai.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
477 if (parentFragmentClass != null) {
478 mParentHeader = new Header();
479 mParentHeader.fragment = parentFragmentClass;
480 if (parentHeaderTitleRes != 0) {
481 mParentHeader.title = getResources().getString(parentHeaderTitleRes);
482 }
483 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700484 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700485 // No recovery
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700486 }
487 }
488
Amith Yamasani9e3a4702011-01-11 09:09:26 -0800489 @Override
490 public boolean hasNextButton() {
491 return super.hasNextButton();
492 }
493
494 @Override
495 public Button getNextButton() {
496 return super.getNextButton();
497 }
498
Gilles Debunnee78c1872011-06-20 15:00:07 -0700499 private static class HeaderAdapter extends ArrayAdapter<Header> {
500 static final int HEADER_TYPE_CATEGORY = 0;
501 static final int HEADER_TYPE_NORMAL = 1;
502 static final int HEADER_TYPE_SWITCH = 2;
503 private static final int HEADER_TYPE_COUNT = HEADER_TYPE_SWITCH + 1;
504
505 private final WifiEnabler mWifiEnabler;
506 private final BluetoothEnabler mBluetoothEnabler;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700507 private AuthenticatorHelper mAuthHelper;
Gilles Debunnee78c1872011-06-20 15:00:07 -0700508
509 private static class HeaderViewHolder {
510 ImageView icon;
511 TextView title;
512 TextView summary;
513 Switch switch_;
514 }
515
516 private LayoutInflater mInflater;
517
518 static int getHeaderType(Header header) {
519 if (header.fragment == null && header.intent == null) {
520 return HEADER_TYPE_CATEGORY;
521 } else if (header.id == R.id.wifi_settings || header.id == R.id.bluetooth_settings) {
522 return HEADER_TYPE_SWITCH;
523 } else {
524 return HEADER_TYPE_NORMAL;
525 }
526 }
527
528 @Override
529 public int getItemViewType(int position) {
530 Header header = getItem(position);
531 return getHeaderType(header);
532 }
533
534 @Override
535 public boolean areAllItemsEnabled() {
536 return false; // because of categories
537 }
538
539 @Override
540 public boolean isEnabled(int position) {
541 return getItemViewType(position) != HEADER_TYPE_CATEGORY;
542 }
543
544 @Override
545 public int getViewTypeCount() {
546 return HEADER_TYPE_COUNT;
547 }
548
549 @Override
550 public boolean hasStableIds() {
551 return true;
552 }
553
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700554 public HeaderAdapter(Context context, List<Header> objects,
555 AuthenticatorHelper authenticatorHelper) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700556 super(context, 0, objects);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700557
558 mAuthHelper = authenticatorHelper;
Gilles Debunnee78c1872011-06-20 15:00:07 -0700559 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700560
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700561 // Temp Switches provided as placeholder until the adapter replaces these with actual
Gilles Debunnee78c1872011-06-20 15:00:07 -0700562 // Switches inflated from their layouts. Must be done before adapter is set in super
563 mWifiEnabler = new WifiEnabler(context, new Switch(context));
564 mBluetoothEnabler = new BluetoothEnabler(context, new Switch(context));
565 }
566
567 @Override
568 public View getView(int position, View convertView, ViewGroup parent) {
569 HeaderViewHolder holder;
570 Header header = getItem(position);
571 int headerType = getHeaderType(header);
572 View view = null;
573
574 if (convertView == null) {
575 holder = new HeaderViewHolder();
576 switch (headerType) {
577 case HEADER_TYPE_CATEGORY:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700578 view = new TextView(getContext(), null,
579 android.R.attr.listSeparatorTextViewStyle);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700580 holder.title = (TextView) view;
581 break;
582
583 case HEADER_TYPE_SWITCH:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700584 view = mInflater.inflate(R.layout.preference_header_switch_item, parent,
585 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700586 holder.icon = (ImageView) view.findViewById(R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700587 holder.title = (TextView)
588 view.findViewById(com.android.internal.R.id.title);
589 holder.summary = (TextView)
590 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700591 holder.switch_ = (Switch) view.findViewById(R.id.switchWidget);
592 break;
593
594 case HEADER_TYPE_NORMAL:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700595 view = mInflater.inflate(
596 com.android.internal.R.layout.preference_header_item, parent,
597 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700598 holder.icon = (ImageView) view.findViewById(com.android.internal.R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700599 holder.title = (TextView)
600 view.findViewById(com.android.internal.R.id.title);
601 holder.summary = (TextView)
602 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700603 break;
604 }
605 view.setTag(holder);
606 } else {
607 view = convertView;
608 holder = (HeaderViewHolder) view.getTag();
609 }
610
611 // All view fields must be updated every time, because the view may be recycled
612 switch (headerType) {
613 case HEADER_TYPE_CATEGORY:
614 holder.title.setText(header.getTitle(getContext().getResources()));
615 break;
616
617 case HEADER_TYPE_SWITCH:
618 // Would need a different treatment if the main menu had more switches
619 if (header.id == R.id.wifi_settings) {
620 mWifiEnabler.setSwitch(holder.switch_);
621 } else {
622 mBluetoothEnabler.setSwitch(holder.switch_);
623 }
624 // No break, fall through on purpose to update common fields
625
626 //$FALL-THROUGH$
627 case HEADER_TYPE_NORMAL:
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700628 if (header.extras != null && header.extras.containsKey(
629 ManageAccountsSettings.KEY_ACCOUNT_TYPE)) {
630 String accType = header.extras.getString(
631 ManageAccountsSettings.KEY_ACCOUNT_TYPE);
632 ViewGroup.LayoutParams lp = holder.icon.getLayoutParams();
633 lp.width = getContext().getResources().getDimensionPixelSize(
634 R.dimen.header_icon_width);
635 lp.height = lp.width;
636 holder.icon.setLayoutParams(lp);
637 Drawable icon = mAuthHelper.getDrawableForType(getContext(), accType);
638 holder.icon.setImageDrawable(icon);
639 } else {
640 holder.icon.setImageResource(header.iconRes);
641 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700642 holder.title.setText(header.getTitle(getContext().getResources()));
643 CharSequence summary = header.getSummary(getContext().getResources());
644 if (!TextUtils.isEmpty(summary)) {
645 holder.summary.setVisibility(View.VISIBLE);
646 holder.summary.setText(summary);
647 } else {
648 holder.summary.setVisibility(View.GONE);
649 }
650 break;
651 }
652
653 return view;
654 }
655
656 public void resume() {
657 mWifiEnabler.resume();
658 mBluetoothEnabler.resume();
659 }
660
661 public void pause() {
662 mWifiEnabler.pause();
663 mBluetoothEnabler.pause();
664 }
665 }
666
667 @Override
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700668 public void onHeaderClick(Header header, int position) {
669 boolean revert = false;
670 if (header.id == R.id.account_add) {
671 revert = true;
672 }
673
674 super.onHeaderClick(header, position);
675
676 if (revert && mLastHeader != null) {
677 highlightHeader((int) mLastHeader.id);
678 } else {
679 mLastHeader = header;
680 }
681 }
682
683 @Override
Amith Yamasania4379d62011-07-22 10:34:58 -0700684 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
685 // Override the fragment title for Wallpaper settings
Amith Yamasanidfb65432011-11-29 16:38:14 -0800686 int titleRes = pref.getTitleRes();
Amith Yamasania4379d62011-07-22 10:34:58 -0700687 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
Amith Yamasanidfb65432011-11-29 16:38:14 -0800688 titleRes = R.string.wallpaper_settings_fragment_title;
Amith Yamasania4379d62011-07-22 10:34:58 -0700689 }
Jean Chalard7dabe452012-05-10 18:08:07 +0900690 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
691 null, 0);
Amith Yamasania4379d62011-07-22 10:34:58 -0700692 return true;
693 }
694
Amith Yamasani3d384f42012-05-11 15:22:04 -0700695 public boolean shouldUpRecreateTask(Intent targetIntent) {
696 return super.shouldUpRecreateTask(new Intent(this, Settings.class));
697 }
698
Amith Yamasania4379d62011-07-22 10:34:58 -0700699 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700700 public void setListAdapter(ListAdapter adapter) {
701 if (mHeaders == null) {
702 mHeaders = new ArrayList<Header>();
703 // When the saved state provides the list of headers, onBuildHeaders is not called
704 // Copy the list of Headers from the adapter, preserving their order
705 for (int i = 0; i < adapter.getCount(); i++) {
706 mHeaders.add((Header) adapter.getItem(i));
707 }
708 }
709
710 // Ignore the adapter provided by PreferenceActivity and substitute ours instead
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700711 super.setListAdapter(new HeaderAdapter(this, mHeaders, mAuthenticatorHelper));
Gilles Debunnee78c1872011-06-20 15:00:07 -0700712 }
713
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700714 /*
715 * Settings subclasses for launching independently.
716 */
Gilles Debunnee78c1872011-06-20 15:00:07 -0700717 public static class BluetoothSettingsActivity extends Settings { /* empty */ }
718 public static class WirelessSettingsActivity extends Settings { /* empty */ }
719 public static class TetherSettingsActivity extends Settings { /* empty */ }
720 public static class VpnSettingsActivity extends Settings { /* empty */ }
721 public static class DateTimeSettingsActivity extends Settings { /* empty */ }
722 public static class StorageSettingsActivity extends Settings { /* empty */ }
723 public static class WifiSettingsActivity extends Settings { /* empty */ }
repo syncb98463f2011-06-30 10:58:43 -0700724 public static class WifiP2pSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700725 public static class InputMethodAndLanguageSettingsActivity extends Settings { /* empty */ }
Jeff Browne46c5f32012-04-05 11:42:18 -0700726 public static class KeyboardLayoutPickerActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700727 public static class InputMethodAndSubtypeEnablerActivity extends Settings { /* empty */ }
satoke077d2b2011-07-25 09:38:11 +0900728 public static class SpellCheckersSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700729 public static class LocalePickerActivity extends Settings { /* empty */ }
730 public static class UserDictionarySettingsActivity extends Settings { /* empty */ }
731 public static class SoundSettingsActivity extends Settings { /* empty */ }
732 public static class DisplaySettingsActivity extends Settings { /* empty */ }
733 public static class DeviceInfoSettingsActivity extends Settings { /* empty */ }
734 public static class ApplicationSettingsActivity extends Settings { /* empty */ }
735 public static class ManageApplicationsActivity extends Settings { /* empty */ }
736 public static class StorageUseActivity extends Settings { /* empty */ }
737 public static class DevelopmentSettingsActivity extends Settings { /* empty */ }
738 public static class AccessibilitySettingsActivity extends Settings { /* empty */ }
739 public static class SecuritySettingsActivity extends Settings { /* empty */ }
Gilles Debunnea6a8a142011-06-09 11:56:17 -0700740 public static class LocationSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700741 public static class PrivacySettingsActivity extends Settings { /* empty */ }
742 public static class DockSettingsActivity extends Settings { /* empty */ }
743 public static class RunningServicesActivity extends Settings { /* empty */ }
744 public static class ManageAccountsSettingsActivity extends Settings { /* empty */ }
745 public static class PowerUsageSummaryActivity extends Settings { /* empty */ }
746 public static class AccountSyncSettingsActivity extends Settings { /* empty */ }
747 public static class AccountSyncSettingsInAddAccountActivity extends Settings { /* empty */ }
748 public static class CryptKeeperSettingsActivity extends Settings { /* empty */ }
749 public static class DeviceAdminSettingsActivity extends Settings { /* empty */ }
750 public static class DataUsageSummaryActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700751 public static class AdvancedWifiSettingsActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700752 public static class TextToSpeechSettingsActivity extends Settings { /* empty */ }
Jeff Hamilton3d670de2011-09-21 16:44:36 -0500753 public static class AndroidBeamSettingsActivity extends Settings { /* empty */ }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800754}