blob: f8c23d77ad8faef28c29a60e7256f85a9b5f1e6e [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 Yamasani56821db2012-06-05 13:20:45 -070029import android.accounts.Account;
30import android.accounts.AccountManager;
31import android.accounts.OnAccountsUpdateListener;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080032import android.content.ComponentName;
Gilles Debunnee78c1872011-06-20 15:00:07 -070033import android.content.Context;
Amith Yamasani379d9b02010-09-27 12:03:59 -070034import android.content.Intent;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070035import android.content.pm.ActivityInfo;
36import android.content.pm.PackageManager;
37import android.content.pm.PackageManager.NameNotFoundException;
Amith Yamasanid1ab8282012-05-18 09:50:08 -070038import android.graphics.drawable.Drawable;
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -070039import android.os.Bundle;
Jeff Sharkey34e964d2012-04-21 15:41:48 -070040import android.os.INetworkManagementService;
41import android.os.RemoteException;
42import android.os.ServiceManager;
Dianne Hackbornbb06a422012-08-16 11:01:57 -070043import android.os.UserHandle;
Amith Yamasania4379d62011-07-22 10:34:58 -070044import android.preference.Preference;
Amith Yamasani02cf71a2010-09-21 15:48:52 -070045import android.preference.PreferenceActivity;
Amith Yamasanid1ab8282012-05-18 09:50:08 -070046import android.preference.PreferenceActivity.Header;
Amith Yamasania4379d62011-07-22 10:34:58 -070047import android.preference.PreferenceFragment;
Gilles Debunnee78c1872011-06-20 15:00:07 -070048import android.text.TextUtils;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080049import android.util.Log;
Gilles Debunnee78c1872011-06-20 15:00:07 -070050import android.view.LayoutInflater;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080051import android.view.View;
52import android.view.View.OnClickListener;
Gilles Debunnee78c1872011-06-20 15:00:07 -070053import android.view.ViewGroup;
54import android.widget.ArrayAdapter;
Amith Yamasaniea7b28c2012-06-20 13:51:40 -070055import android.widget.BaseAdapter;
Amith Yamasani9e3a4702011-01-11 09:09:26 -080056import android.widget.Button;
Gilles Debunnee78c1872011-06-20 15:00:07 -070057import android.widget.ImageView;
58import android.widget.ListAdapter;
59import android.widget.Switch;
60import android.widget.TextView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080061
Gilles Debunnee78c1872011-06-20 15:00:07 -070062import java.util.ArrayList;
Amith Yamasanid1ab8282012-05-18 09:50:08 -070063import java.util.Collections;
64import java.util.Comparator;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070065import java.util.HashMap;
Amith Yamasani02cf71a2010-09-21 15:48:52 -070066import java.util.List;
Amith Yamasanid7993472010-08-18 13:59:28 -070067
68/**
69 * Top-level settings activity to handle single pane and double pane UI layout.
70 */
Amith Yamasani56821db2012-06-05 13:20:45 -070071public class Settings extends PreferenceActivity
72 implements ButtonBarHandler, OnAccountsUpdateListener {
Amith Yamasanid7993472010-08-18 13:59:28 -070073
Gilles Debunnee78c1872011-06-20 15:00:07 -070074 private static final String LOG_TAG = "Settings";
Amith Yamasaniea7b28c2012-06-20 13:51:40 -070075
Amith Yamasani5203bdf2010-11-04 09:59:44 -070076 private static final String META_DATA_KEY_HEADER_ID =
Gilles Debunnee78c1872011-06-20 15:00:07 -070077 "com.android.settings.TOP_LEVEL_HEADER_ID";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070078 private static final String META_DATA_KEY_FRAGMENT_CLASS =
Gilles Debunnee78c1872011-06-20 15:00:07 -070079 "com.android.settings.FRAGMENT_CLASS";
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080080 private static final String META_DATA_KEY_PARENT_TITLE =
81 "com.android.settings.PARENT_FRAGMENT_TITLE";
82 private static final String META_DATA_KEY_PARENT_FRAGMENT_CLASS =
83 "com.android.settings.PARENT_FRAGMENT_CLASS";
84
Jeff Sharkey54d0af52011-08-11 18:26:57 -070085 private static final String EXTRA_CLEAR_UI_OPTIONS = "settings:remove_ui_options";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070086
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080087 private static final String SAVE_KEY_CURRENT_HEADER = "com.android.settings.CURRENT_HEADER";
88 private static final String SAVE_KEY_PARENT_HEADER = "com.android.settings.PARENT_HEADER";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070089
90 private String mFragmentClass;
91 private int mTopLevelHeaderId;
Amith Yamasani3965ae62010-11-15 14:45:19 -080092 private Header mFirstHeader;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080093 private Header mCurrentHeader;
94 private Header mParentHeader;
95 private boolean mInLocalHeaderSwitch;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070096
Amith Yamasanib810a0d2012-03-25 10:12:26 -070097 // Show only these settings for restricted users
98 private int[] SETTINGS_FOR_RESTRICTED = {
Amith Yamasani32630392012-08-10 19:31:39 -070099 R.id.wireless_section,
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700100 R.id.wifi_settings,
101 R.id.bluetooth_settings,
Amith Yamasani32630392012-08-10 19:31:39 -0700102 R.id.device_section,
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700103 R.id.sound_settings,
104 R.id.display_settings,
Dianne Hackborn271c8b02012-08-20 17:24:39 -0700105 R.id.application_settings,
Amith Yamasani32630392012-08-10 19:31:39 -0700106 R.id.personal_section,
Amith Yamasani3deeeb72012-04-05 14:44:48 -0700107 R.id.security_settings,
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700108 R.id.account_settings,
Amith Yamasani32630392012-08-10 19:31:39 -0700109 R.id.account_add,
110 R.id.system_section,
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700111 R.id.about_settings
112 };
113
Amith Yamasanifa3eea52012-03-28 10:25:08 -0700114 private boolean mEnableUserManagement = false;
115
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700116 // TODO: Update Call Settings based on airplane mode state.
Amith Yamasanib61cf512010-09-12 08:17:50 -0700117
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700118 protected HashMap<Integer, Integer> mHeaderIndexMap = new HashMap<Integer, Integer>();
119
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700120 private AuthenticatorHelper mAuthenticatorHelper;
121 private Header mLastHeader;
Amith Yamasani86708a82012-06-06 20:23:08 -0700122 private boolean mListeningToAccountUpdates;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700123
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700124 @Override
125 protected void onCreate(Bundle savedInstanceState) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700126 if (getIntent().getBooleanExtra(EXTRA_CLEAR_UI_OPTIONS, false)) {
127 getWindow().setUiOptions(0);
128 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700129
Amith Yamasanifa3eea52012-03-28 10:25:08 -0700130 if (android.provider.Settings.Secure.getInt(getContentResolver(), "multiuser_enabled", -1)
131 > 0) {
132 mEnableUserManagement = true;
133 }
134
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700135 mAuthenticatorHelper = new AuthenticatorHelper();
136 mAuthenticatorHelper.updateAuthDescriptions(this);
137 mAuthenticatorHelper.onAccountsUpdated(this, null);
138
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700139 getMetaData();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800140 mInLocalHeaderSwitch = true;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700141 super.onCreate(savedInstanceState);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800142 mInLocalHeaderSwitch = false;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700143
Gilles Debunne3661b622011-06-27 11:19:16 -0700144 if (!onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700145 highlightHeader(mTopLevelHeaderId);
Amith Yamasani72aa19d2010-12-09 06:07:12 -0800146 // Force the title so that it doesn't get overridden by a direct launch of
147 // a specific settings screen.
148 setTitle(R.string.settings_label);
149 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800150
151 // Retrieve any saved state
152 if (savedInstanceState != null) {
153 mCurrentHeader = savedInstanceState.getParcelable(SAVE_KEY_CURRENT_HEADER);
154 mParentHeader = savedInstanceState.getParcelable(SAVE_KEY_PARENT_HEADER);
155 }
156
157 // If the current header was saved, switch to it
158 if (savedInstanceState != null && mCurrentHeader != null) {
159 //switchToHeaderLocal(mCurrentHeader);
160 showBreadCrumbs(mCurrentHeader.title, null);
161 }
162
163 if (mParentHeader != null) {
164 setParentTitle(mParentHeader.title, null, new OnClickListener() {
165 public void onClick(View v) {
166 switchToParent(mParentHeader.fragment);
167 }
168 });
169 }
Gilles Debunnedc7101f2011-06-27 12:00:49 -0700170
Amith Yamasani3d384f42012-05-11 15:22:04 -0700171 // Override up navigation for multi-pane, since we handle it in the fragment breadcrumbs
172 if (onIsMultiPane()) {
173 getActionBar().setDisplayHomeAsUpEnabled(false);
174 getActionBar().setHomeButtonEnabled(false);
175 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800176 }
177
178 @Override
179 protected void onSaveInstanceState(Bundle outState) {
180 super.onSaveInstanceState(outState);
181
182 // Save the current fragment, if it is the same as originally launched
183 if (mCurrentHeader != null) {
184 outState.putParcelable(SAVE_KEY_CURRENT_HEADER, mCurrentHeader);
185 }
186 if (mParentHeader != null) {
187 outState.putParcelable(SAVE_KEY_PARENT_HEADER, mParentHeader);
188 }
189 }
190
Gilles Debunnee78c1872011-06-20 15:00:07 -0700191 @Override
192 public void onResume() {
193 super.onResume();
194
195 ListAdapter listAdapter = getListAdapter();
196 if (listAdapter instanceof HeaderAdapter) {
197 ((HeaderAdapter) listAdapter).resume();
198 }
Amith Yamasaniea7b28c2012-06-20 13:51:40 -0700199 invalidateHeaders();
Gilles Debunnee78c1872011-06-20 15:00:07 -0700200 }
201
202 @Override
203 public void onPause() {
204 super.onPause();
205
206 ListAdapter listAdapter = getListAdapter();
207 if (listAdapter instanceof HeaderAdapter) {
208 ((HeaderAdapter) listAdapter).pause();
Amith Yamasani86708a82012-06-06 20:23:08 -0700209 }
210 }
211
212 @Override
213 public void onDestroy() {
214 super.onDestroy();
215 if (mListeningToAccountUpdates) {
Amith Yamasani56821db2012-06-05 13:20:45 -0700216 AccountManager.get(this).removeOnAccountsUpdatedListener(this);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700217 }
218 }
219
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800220 private void switchToHeaderLocal(Header header) {
221 mInLocalHeaderSwitch = true;
222 switchToHeader(header);
223 mInLocalHeaderSwitch = false;
224 }
225
226 @Override
227 public void switchToHeader(Header header) {
228 if (!mInLocalHeaderSwitch) {
229 mCurrentHeader = null;
230 mParentHeader = null;
231 }
232 super.switchToHeader(header);
233 }
234
235 /**
236 * Switch to parent fragment and store the grand parent's info
Jake Hamby2748fc22011-01-12 15:06:28 -0800237 * @param className name of the activity wrapper for the parent fragment.
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800238 */
239 private void switchToParent(String className) {
240 final ComponentName cn = new ComponentName(this, className);
241 try {
242 final PackageManager pm = getPackageManager();
243 final ActivityInfo parentInfo = pm.getActivityInfo(cn, PackageManager.GET_META_DATA);
244
245 if (parentInfo != null && parentInfo.metaData != null) {
246 String fragmentClass = parentInfo.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
247 CharSequence fragmentTitle = parentInfo.loadLabel(pm);
248 Header parentHeader = new Header();
249 parentHeader.fragment = fragmentClass;
250 parentHeader.title = fragmentTitle;
251 mCurrentHeader = parentHeader;
252
253 switchToHeaderLocal(parentHeader);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700254 highlightHeader(mTopLevelHeaderId);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800255
256 mParentHeader = new Header();
257 mParentHeader.fragment
258 = parentInfo.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
259 mParentHeader.title = parentInfo.metaData.getString(META_DATA_KEY_PARENT_TITLE);
260 }
261 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700262 Log.w(LOG_TAG, "Could not find parent activity : " + className);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800263 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700264 }
265
Amith Yamasani3965ae62010-11-15 14:45:19 -0800266 @Override
267 public void onNewIntent(Intent intent) {
268 super.onNewIntent(intent);
269
270 // If it is not launched from history, then reset to top-level
271 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0
Gilles Debunne3661b622011-06-27 11:19:16 -0700272 && mFirstHeader != null && !onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800273 switchToHeaderLocal(mFirstHeader);
Amith Yamasani3965ae62010-11-15 14:45:19 -0800274 }
275 }
276
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700277 private void highlightHeader(int id) {
278 if (id != 0) {
279 Integer index = mHeaderIndexMap.get(id);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700280 if (index != null) {
281 getListView().setItemChecked(index, true);
Amith Yamasani990fb522011-09-02 09:47:18 -0700282 getListView().smoothScrollToPosition(index);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700283 }
284 }
285 }
286
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700287 @Override
288 public Intent getIntent() {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700289 Intent superIntent = super.getIntent();
290 String startingFragment = getStartingFragmentClass(superIntent);
Gilles Debunne3661b622011-06-27 11:19:16 -0700291 // This is called from super.onCreate, isMultiPane() is not yet reliable
292 // Do not use onIsHidingHeaders either, which relies itself on this method
293 if (startingFragment != null && !onIsMultiPane()) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700294 Intent modIntent = new Intent(superIntent);
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700295 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700296 Bundle args = superIntent.getExtras();
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700297 if (args != null) {
298 args = new Bundle(args);
299 } else {
300 args = new Bundle();
301 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700302 args.putParcelable("intent", superIntent);
303 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras());
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700304 return modIntent;
305 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700306 return superIntent;
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700307 }
308
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700309 /**
Amith Yamasani379d9b02010-09-27 12:03:59 -0700310 * Checks if the component name in the intent is different from the Settings class and
311 * returns the class name to load as a fragment.
312 */
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700313 protected String getStartingFragmentClass(Intent intent) {
314 if (mFragmentClass != null) return mFragmentClass;
315
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700316 String intentClass = intent.getComponent().getClassName();
Amith Yamasani379d9b02010-09-27 12:03:59 -0700317 if (intentClass.equals(getClass().getName())) return null;
318
Dianne Hackbornee293792010-11-01 12:32:33 -0700319 if ("com.android.settings.ManageApplications".equals(intentClass)
320 || "com.android.settings.RunningServices".equals(intentClass)
321 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700322 // Old names of manage apps.
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700323 intentClass = com.android.settings.applications.ManageApplications.class.getName();
324 }
325
Amith Yamasani379d9b02010-09-27 12:03:59 -0700326 return intentClass;
327 }
328
329 /**
330 * Override initial header when an activity-alias is causing Settings to be launched
331 * for a specific fragment encoded in the android:name parameter.
332 */
333 @Override
334 public Header onGetInitialHeader() {
335 String fragmentClass = getStartingFragmentClass(super.getIntent());
336 if (fragmentClass != null) {
337 Header header = new Header();
338 header.fragment = fragmentClass;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800339 header.title = getTitle();
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700340 header.fragmentArguments = getIntent().getExtras();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800341 mCurrentHeader = header;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700342 return header;
343 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700344
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700345 return mFirstHeader;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700346 }
347
Dianne Hackbornb7258182011-03-15 16:23:55 -0700348 @Override
Dianne Hackborn48147dc2011-03-18 12:29:41 -0700349 public Intent onBuildStartFragmentIntent(String fragmentName, Bundle args,
350 int titleRes, int shortTitleRes) {
351 Intent intent = super.onBuildStartFragmentIntent(fragmentName, args,
352 titleRes, shortTitleRes);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700353
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700354 // some fragments want to avoid split actionbar
Gilles Debunne162e5412011-07-11 14:13:31 -0700355 if (DataUsageSummary.class.getName().equals(fragmentName) ||
356 PowerUsageSummary.class.getName().equals(fragmentName) ||
Gilles Debunnecd8e5242011-07-25 11:36:15 -0700357 AccountSyncSettings.class.getName().equals(fragmentName) ||
Jeff Sharkey11d30122012-03-19 16:54:07 -0700358 UserDictionarySettings.class.getName().equals(fragmentName) ||
Adam Powellfaba7e42012-03-26 17:28:38 -0700359 Memory.class.getName().equals(fragmentName) ||
Jeff Sharkeya339cba2012-05-08 11:42:49 -0700360 ManageApplications.class.getName().equals(fragmentName) ||
361 WirelessSettings.class.getName().equals(fragmentName) ||
362 SoundSettings.class.getName().equals(fragmentName) ||
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700363 PrivacySettings.class.getName().equals(fragmentName) ||
364 ManageAccountsSettings.class.getName().equals(fragmentName)) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700365 intent.putExtra(EXTRA_CLEAR_UI_OPTIONS, true);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700366 }
367
Dianne Hackbornb7258182011-03-15 16:23:55 -0700368 intent.setClass(this, SubSettings.class);
369 return intent;
370 }
Amith Yamasaniea7b28c2012-06-20 13:51:40 -0700371
Amith Yamasani379d9b02010-09-27 12:03:59 -0700372 /**
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700373 * Populate the activity with the top-level headers.
374 */
Amith Yamasanid7993472010-08-18 13:59:28 -0700375 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700376 public void onBuildHeaders(List<Header> headers) {
377 loadHeadersFromResource(R.xml.settings_headers, headers);
Amith Yamasanid7993472010-08-18 13:59:28 -0700378
Gilles Debunnee78c1872011-06-20 15:00:07 -0700379 updateHeaderList(headers);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700380 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700381
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700382 private void updateHeaderList(List<Header> target) {
383 int i = 0;
384 while (i < target.size()) {
385 Header header = target.get(i);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700386 // Ids are integers, so downcasting
387 int id = (int) header.id;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700388 if (id == R.id.dock_settings) {
389 if (!needsDockSettings())
390 target.remove(header);
391 } else if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
392 Utils.updateHeaderToSpecificActivityFromMetaDataOrRemove(this, target, header);
Gilles Debunne2454f492011-06-21 16:16:06 -0700393 } else if (id == R.id.wifi_settings) {
394 // Remove WiFi Settings if WiFi service is not available.
395 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
396 target.remove(header);
397 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700398 } else if (id == R.id.bluetooth_settings) {
399 // Remove Bluetooth Settings if Bluetooth service is not available.
Gilles Debunne2454f492011-06-21 16:16:06 -0700400 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700401 target.remove(header);
402 }
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700403 } else if (id == R.id.data_usage_settings) {
404 // Remove data usage when kernel module not enabled
405 final INetworkManagementService netManager = INetworkManagementService.Stub
406 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
407 try {
408 if (!netManager.isBandwidthControlEnabled()) {
409 target.remove(header);
410 }
411 } catch (RemoteException e) {
412 // ignored
413 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700414 } else if (id == R.id.account_settings) {
415 int headerIndex = i + 1;
416 i = insertAccountsHeaders(target, headerIndex);
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700417 } else if (id == R.id.user_settings) {
Amith Yamasanifa3eea52012-03-28 10:25:08 -0700418 if (!mEnableUserManagement
Dianne Hackbornbb06a422012-08-16 11:01:57 -0700419 || !UserHandle.MU_ENABLED || UserHandle.myUserId() != 0
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700420 || !getResources().getBoolean(R.bool.enable_user_management)
421 || Utils.isMonkeyRunning()) {
422 target.remove(header);
423 }
424 }
Dianne Hackbornbb06a422012-08-16 11:01:57 -0700425 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700426 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
427 target.remove(header);
Amith Yamasanid7993472010-08-18 13:59:28 -0700428 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700429
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700430 // Increment if the current one wasn't removed by the Utils code.
431 if (target.get(i) == header) {
Amith Yamasani3965ae62010-11-15 14:45:19 -0800432 // Hold on to the first header, when we need to reset to the top-level
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700433 if (mFirstHeader == null &&
434 HeaderAdapter.getHeaderType(header) != HeaderAdapter.HEADER_TYPE_CATEGORY) {
435 mFirstHeader = header;
436 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700437 mHeaderIndexMap.put(id, i);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700438 i++;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700439 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700440 }
441 }
442
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700443 private int insertAccountsHeaders(List<Header> target, int headerIndex) {
444 String[] accountTypes = mAuthenticatorHelper.getEnabledAccountTypes();
445 List<Header> accountHeaders = new ArrayList<Header>(accountTypes.length);
446 for (String accountType : accountTypes) {
447 CharSequence label = mAuthenticatorHelper.getLabelForType(this, accountType);
Brian Muramatsuc28af522012-06-28 14:25:14 -0700448 if (label == null) {
449 continue;
450 }
451
Amith Yamasani3882c2e2012-06-07 17:03:20 -0700452 Account[] accounts = AccountManager.get(this).getAccountsByType(accountType);
453 boolean skipToAccount = accounts.length == 1
454 && !mAuthenticatorHelper.hasAccountPreferences(accountType);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700455 Header accHeader = new Header();
456 accHeader.title = label;
457 if (accHeader.extras == null) {
458 accHeader.extras = new Bundle();
459 }
Amith Yamasani3882c2e2012-06-07 17:03:20 -0700460 if (skipToAccount) {
461 accHeader.breadCrumbTitleRes = R.string.account_sync_settings_title;
462 accHeader.breadCrumbShortTitleRes = R.string.account_sync_settings_title;
463 accHeader.fragment = AccountSyncSettings.class.getName();
464 accHeader.fragmentArguments = new Bundle();
465 // Need this for the icon
466 accHeader.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
467 accHeader.extras.putParcelable(AccountSyncSettings.ACCOUNT_KEY, accounts[0]);
468 accHeader.fragmentArguments.putParcelable(AccountSyncSettings.ACCOUNT_KEY,
469 accounts[0]);
470 } else {
471 accHeader.breadCrumbTitle = label;
472 accHeader.breadCrumbShortTitle = label;
473 accHeader.fragment = ManageAccountsSettings.class.getName();
474 accHeader.fragmentArguments = new Bundle();
475 accHeader.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
476 accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE,
477 accountType);
478 if (!isMultiPane()) {
479 accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_LABEL,
480 label.toString());
481 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700482 }
483 accountHeaders.add(accHeader);
484 }
485
486 // Sort by label
487 Collections.sort(accountHeaders, new Comparator<Header>() {
488 @Override
489 public int compare(Header h1, Header h2) {
490 return h1.title.toString().compareTo(h2.title.toString());
491 }
492 });
493
494 for (Header header : accountHeaders) {
495 target.add(headerIndex++, header);
496 }
Amith Yamasani86708a82012-06-06 20:23:08 -0700497 if (!mListeningToAccountUpdates) {
498 AccountManager.get(this).addOnAccountsUpdatedListener(this, null, true);
499 mListeningToAccountUpdates = true;
500 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700501 return headerIndex;
502 }
503
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700504 private boolean needsDockSettings() {
505 return getResources().getBoolean(R.bool.has_dock_settings);
Amith Yamasanib61cf512010-09-12 08:17:50 -0700506 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700507
508 private void getMetaData() {
509 try {
510 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
511 PackageManager.GET_META_DATA);
512 if (ai == null || ai.metaData == null) return;
513 mTopLevelHeaderId = ai.metaData.getInt(META_DATA_KEY_HEADER_ID);
514 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700515
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800516 // Check if it has a parent specified and create a Header object
517 final int parentHeaderTitleRes = ai.metaData.getInt(META_DATA_KEY_PARENT_TITLE);
518 String parentFragmentClass = ai.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
519 if (parentFragmentClass != null) {
520 mParentHeader = new Header();
521 mParentHeader.fragment = parentFragmentClass;
522 if (parentHeaderTitleRes != 0) {
523 mParentHeader.title = getResources().getString(parentHeaderTitleRes);
524 }
525 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700526 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700527 // No recovery
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700528 }
529 }
530
Amith Yamasani9e3a4702011-01-11 09:09:26 -0800531 @Override
532 public boolean hasNextButton() {
533 return super.hasNextButton();
534 }
535
536 @Override
537 public Button getNextButton() {
538 return super.getNextButton();
539 }
540
Gilles Debunnee78c1872011-06-20 15:00:07 -0700541 private static class HeaderAdapter extends ArrayAdapter<Header> {
542 static final int HEADER_TYPE_CATEGORY = 0;
543 static final int HEADER_TYPE_NORMAL = 1;
544 static final int HEADER_TYPE_SWITCH = 2;
545 private static final int HEADER_TYPE_COUNT = HEADER_TYPE_SWITCH + 1;
546
547 private final WifiEnabler mWifiEnabler;
548 private final BluetoothEnabler mBluetoothEnabler;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700549 private AuthenticatorHelper mAuthHelper;
Gilles Debunnee78c1872011-06-20 15:00:07 -0700550
551 private static class HeaderViewHolder {
552 ImageView icon;
553 TextView title;
554 TextView summary;
555 Switch switch_;
556 }
557
558 private LayoutInflater mInflater;
559
560 static int getHeaderType(Header header) {
561 if (header.fragment == null && header.intent == null) {
562 return HEADER_TYPE_CATEGORY;
563 } else if (header.id == R.id.wifi_settings || header.id == R.id.bluetooth_settings) {
564 return HEADER_TYPE_SWITCH;
565 } else {
566 return HEADER_TYPE_NORMAL;
567 }
568 }
569
570 @Override
571 public int getItemViewType(int position) {
572 Header header = getItem(position);
573 return getHeaderType(header);
574 }
575
576 @Override
577 public boolean areAllItemsEnabled() {
578 return false; // because of categories
579 }
580
581 @Override
582 public boolean isEnabled(int position) {
583 return getItemViewType(position) != HEADER_TYPE_CATEGORY;
584 }
585
586 @Override
587 public int getViewTypeCount() {
588 return HEADER_TYPE_COUNT;
589 }
590
591 @Override
592 public boolean hasStableIds() {
593 return true;
594 }
595
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700596 public HeaderAdapter(Context context, List<Header> objects,
597 AuthenticatorHelper authenticatorHelper) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700598 super(context, 0, objects);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700599
600 mAuthHelper = authenticatorHelper;
Gilles Debunnee78c1872011-06-20 15:00:07 -0700601 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700602
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700603 // Temp Switches provided as placeholder until the adapter replaces these with actual
Gilles Debunnee78c1872011-06-20 15:00:07 -0700604 // Switches inflated from their layouts. Must be done before adapter is set in super
605 mWifiEnabler = new WifiEnabler(context, new Switch(context));
606 mBluetoothEnabler = new BluetoothEnabler(context, new Switch(context));
607 }
608
609 @Override
610 public View getView(int position, View convertView, ViewGroup parent) {
611 HeaderViewHolder holder;
612 Header header = getItem(position);
613 int headerType = getHeaderType(header);
614 View view = null;
615
616 if (convertView == null) {
617 holder = new HeaderViewHolder();
618 switch (headerType) {
619 case HEADER_TYPE_CATEGORY:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700620 view = new TextView(getContext(), null,
621 android.R.attr.listSeparatorTextViewStyle);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700622 holder.title = (TextView) view;
623 break;
624
625 case HEADER_TYPE_SWITCH:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700626 view = mInflater.inflate(R.layout.preference_header_switch_item, parent,
627 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700628 holder.icon = (ImageView) view.findViewById(R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700629 holder.title = (TextView)
630 view.findViewById(com.android.internal.R.id.title);
631 holder.summary = (TextView)
632 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700633 holder.switch_ = (Switch) view.findViewById(R.id.switchWidget);
634 break;
635
636 case HEADER_TYPE_NORMAL:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700637 view = mInflater.inflate(
Amith Yamasanic8a93172012-06-08 13:35:47 -0700638 R.layout.preference_header_item, parent,
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700639 false);
Amith Yamasanic8a93172012-06-08 13:35:47 -0700640 holder.icon = (ImageView) view.findViewById(R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700641 holder.title = (TextView)
642 view.findViewById(com.android.internal.R.id.title);
643 holder.summary = (TextView)
644 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700645 break;
646 }
647 view.setTag(holder);
648 } else {
649 view = convertView;
650 holder = (HeaderViewHolder) view.getTag();
651 }
652
653 // All view fields must be updated every time, because the view may be recycled
654 switch (headerType) {
655 case HEADER_TYPE_CATEGORY:
656 holder.title.setText(header.getTitle(getContext().getResources()));
657 break;
658
659 case HEADER_TYPE_SWITCH:
660 // Would need a different treatment if the main menu had more switches
661 if (header.id == R.id.wifi_settings) {
662 mWifiEnabler.setSwitch(holder.switch_);
663 } else {
664 mBluetoothEnabler.setSwitch(holder.switch_);
665 }
666 // No break, fall through on purpose to update common fields
667
668 //$FALL-THROUGH$
669 case HEADER_TYPE_NORMAL:
Amith Yamasani3882c2e2012-06-07 17:03:20 -0700670 if (header.extras != null
671 && header.extras.containsKey(ManageAccountsSettings.KEY_ACCOUNT_TYPE)) {
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700672 String accType = header.extras.getString(
673 ManageAccountsSettings.KEY_ACCOUNT_TYPE);
674 ViewGroup.LayoutParams lp = holder.icon.getLayoutParams();
675 lp.width = getContext().getResources().getDimensionPixelSize(
676 R.dimen.header_icon_width);
677 lp.height = lp.width;
678 holder.icon.setLayoutParams(lp);
679 Drawable icon = mAuthHelper.getDrawableForType(getContext(), accType);
680 holder.icon.setImageDrawable(icon);
681 } else {
682 holder.icon.setImageResource(header.iconRes);
683 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700684 holder.title.setText(header.getTitle(getContext().getResources()));
685 CharSequence summary = header.getSummary(getContext().getResources());
686 if (!TextUtils.isEmpty(summary)) {
687 holder.summary.setVisibility(View.VISIBLE);
688 holder.summary.setText(summary);
689 } else {
690 holder.summary.setVisibility(View.GONE);
691 }
692 break;
693 }
694
695 return view;
696 }
697
698 public void resume() {
699 mWifiEnabler.resume();
700 mBluetoothEnabler.resume();
701 }
Brian Muramatsuc28af522012-06-28 14:25:14 -0700702
Gilles Debunnee78c1872011-06-20 15:00:07 -0700703 public void pause() {
704 mWifiEnabler.pause();
705 mBluetoothEnabler.pause();
706 }
707 }
708
709 @Override
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700710 public void onHeaderClick(Header header, int position) {
711 boolean revert = false;
712 if (header.id == R.id.account_add) {
713 revert = true;
714 }
715
716 super.onHeaderClick(header, position);
717
718 if (revert && mLastHeader != null) {
719 highlightHeader((int) mLastHeader.id);
720 } else {
721 mLastHeader = header;
722 }
723 }
724
725 @Override
Amith Yamasania4379d62011-07-22 10:34:58 -0700726 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
727 // Override the fragment title for Wallpaper settings
Amith Yamasanidfb65432011-11-29 16:38:14 -0800728 int titleRes = pref.getTitleRes();
Amith Yamasania4379d62011-07-22 10:34:58 -0700729 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
Amith Yamasanidfb65432011-11-29 16:38:14 -0800730 titleRes = R.string.wallpaper_settings_fragment_title;
Amith Yamasania4379d62011-07-22 10:34:58 -0700731 }
Jean Chalard7dabe452012-05-10 18:08:07 +0900732 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
733 null, 0);
Amith Yamasania4379d62011-07-22 10:34:58 -0700734 return true;
735 }
736
Amith Yamasani3d384f42012-05-11 15:22:04 -0700737 public boolean shouldUpRecreateTask(Intent targetIntent) {
738 return super.shouldUpRecreateTask(new Intent(this, Settings.class));
739 }
740
Amith Yamasania4379d62011-07-22 10:34:58 -0700741 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700742 public void setListAdapter(ListAdapter adapter) {
Amith Yamasaniea7b28c2012-06-20 13:51:40 -0700743 if (adapter == null) {
744 super.setListAdapter(null);
745 } else {
746 super.setListAdapter(new HeaderAdapter(this, getHeaders(), mAuthenticatorHelper));
Gilles Debunnee78c1872011-06-20 15:00:07 -0700747 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700748 }
749
Amith Yamasani56821db2012-06-05 13:20:45 -0700750 @Override
751 public void onAccountsUpdated(Account[] accounts) {
752 mAuthenticatorHelper.onAccountsUpdated(this, accounts);
753 invalidateHeaders();
754 }
755
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700756 /*
757 * Settings subclasses for launching independently.
758 */
Gilles Debunnee78c1872011-06-20 15:00:07 -0700759 public static class BluetoothSettingsActivity extends Settings { /* empty */ }
760 public static class WirelessSettingsActivity extends Settings { /* empty */ }
761 public static class TetherSettingsActivity extends Settings { /* empty */ }
762 public static class VpnSettingsActivity extends Settings { /* empty */ }
763 public static class DateTimeSettingsActivity extends Settings { /* empty */ }
764 public static class StorageSettingsActivity extends Settings { /* empty */ }
765 public static class WifiSettingsActivity extends Settings { /* empty */ }
repo syncb98463f2011-06-30 10:58:43 -0700766 public static class WifiP2pSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700767 public static class InputMethodAndLanguageSettingsActivity extends Settings { /* empty */ }
Jeff Browne46c5f32012-04-05 11:42:18 -0700768 public static class KeyboardLayoutPickerActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700769 public static class InputMethodAndSubtypeEnablerActivity extends Settings { /* empty */ }
satoke077d2b2011-07-25 09:38:11 +0900770 public static class SpellCheckersSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700771 public static class LocalePickerActivity extends Settings { /* empty */ }
772 public static class UserDictionarySettingsActivity extends Settings { /* empty */ }
773 public static class SoundSettingsActivity extends Settings { /* empty */ }
774 public static class DisplaySettingsActivity extends Settings { /* empty */ }
775 public static class DeviceInfoSettingsActivity extends Settings { /* empty */ }
776 public static class ApplicationSettingsActivity extends Settings { /* empty */ }
777 public static class ManageApplicationsActivity extends Settings { /* empty */ }
778 public static class StorageUseActivity extends Settings { /* empty */ }
779 public static class DevelopmentSettingsActivity extends Settings { /* empty */ }
780 public static class AccessibilitySettingsActivity extends Settings { /* empty */ }
781 public static class SecuritySettingsActivity extends Settings { /* empty */ }
Gilles Debunnea6a8a142011-06-09 11:56:17 -0700782 public static class LocationSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700783 public static class PrivacySettingsActivity extends Settings { /* empty */ }
784 public static class DockSettingsActivity extends Settings { /* empty */ }
785 public static class RunningServicesActivity extends Settings { /* empty */ }
786 public static class ManageAccountsSettingsActivity extends Settings { /* empty */ }
787 public static class PowerUsageSummaryActivity extends Settings { /* empty */ }
788 public static class AccountSyncSettingsActivity extends Settings { /* empty */ }
789 public static class AccountSyncSettingsInAddAccountActivity extends Settings { /* empty */ }
790 public static class CryptKeeperSettingsActivity extends Settings { /* empty */ }
791 public static class DeviceAdminSettingsActivity extends Settings { /* empty */ }
792 public static class DataUsageSummaryActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700793 public static class AdvancedWifiSettingsActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700794 public static class TextToSpeechSettingsActivity extends Settings { /* empty */ }
Jeff Hamilton3d670de2011-09-21 16:44:36 -0500795 public static class AndroidBeamSettingsActivity extends Settings { /* empty */ }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800796}