blob: 37b47c2c4a6795dc0c84ae69880f2da783014073 [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;
Amith Yamasanib810a0d2012-03-25 10:12:26 -070043import android.os.UserId;
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 Yamasani9e3a4702011-01-11 09:09:26 -080055import android.widget.Button;
Gilles Debunnee78c1872011-06-20 15:00:07 -070056import android.widget.ImageView;
57import android.widget.ListAdapter;
58import android.widget.Switch;
59import android.widget.TextView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080060
Gilles Debunnee78c1872011-06-20 15:00:07 -070061import java.util.ArrayList;
Amith Yamasanid1ab8282012-05-18 09:50:08 -070062import java.util.Collections;
63import java.util.Comparator;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070064import java.util.HashMap;
Amith Yamasani02cf71a2010-09-21 15:48:52 -070065import java.util.List;
Amith Yamasanid7993472010-08-18 13:59:28 -070066
67/**
68 * Top-level settings activity to handle single pane and double pane UI layout.
69 */
Amith Yamasani56821db2012-06-05 13:20:45 -070070public class Settings extends PreferenceActivity
71 implements ButtonBarHandler, OnAccountsUpdateListener {
Amith Yamasanid7993472010-08-18 13:59:28 -070072
Gilles Debunnee78c1872011-06-20 15:00:07 -070073 private static final String LOG_TAG = "Settings";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070074 private static final String META_DATA_KEY_HEADER_ID =
Gilles Debunnee78c1872011-06-20 15:00:07 -070075 "com.android.settings.TOP_LEVEL_HEADER_ID";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070076 private static final String META_DATA_KEY_FRAGMENT_CLASS =
Gilles Debunnee78c1872011-06-20 15:00:07 -070077 "com.android.settings.FRAGMENT_CLASS";
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080078 private static final String META_DATA_KEY_PARENT_TITLE =
79 "com.android.settings.PARENT_FRAGMENT_TITLE";
80 private static final String META_DATA_KEY_PARENT_FRAGMENT_CLASS =
81 "com.android.settings.PARENT_FRAGMENT_CLASS";
82
Jeff Sharkey54d0af52011-08-11 18:26:57 -070083 private static final String EXTRA_CLEAR_UI_OPTIONS = "settings:remove_ui_options";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070084
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080085 private static final String SAVE_KEY_CURRENT_HEADER = "com.android.settings.CURRENT_HEADER";
86 private static final String SAVE_KEY_PARENT_HEADER = "com.android.settings.PARENT_HEADER";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070087
88 private String mFragmentClass;
89 private int mTopLevelHeaderId;
Amith Yamasani3965ae62010-11-15 14:45:19 -080090 private Header mFirstHeader;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080091 private Header mCurrentHeader;
92 private Header mParentHeader;
93 private boolean mInLocalHeaderSwitch;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070094
Amith Yamasanib810a0d2012-03-25 10:12:26 -070095 // Show only these settings for restricted users
96 private int[] SETTINGS_FOR_RESTRICTED = {
97 R.id.wifi_settings,
98 R.id.bluetooth_settings,
99 R.id.sound_settings,
100 R.id.display_settings,
Amith Yamasani3deeeb72012-04-05 14:44:48 -0700101 R.id.security_settings,
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700102 R.id.account_settings,
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700103 R.id.about_settings
104 };
105
Amith Yamasanifa3eea52012-03-28 10:25:08 -0700106 private boolean mEnableUserManagement = false;
107
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700108 // TODO: Update Call Settings based on airplane mode state.
Amith Yamasanib61cf512010-09-12 08:17:50 -0700109
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700110 protected HashMap<Integer, Integer> mHeaderIndexMap = new HashMap<Integer, Integer>();
Gilles Debunnee78c1872011-06-20 15:00:07 -0700111 private List<Header> mHeaders;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700112
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700113 private AuthenticatorHelper mAuthenticatorHelper;
114 private Header mLastHeader;
Amith Yamasani86708a82012-06-06 20:23:08 -0700115 private boolean mListeningToAccountUpdates;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700116
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700117 @Override
118 protected void onCreate(Bundle savedInstanceState) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700119 if (getIntent().getBooleanExtra(EXTRA_CLEAR_UI_OPTIONS, false)) {
120 getWindow().setUiOptions(0);
121 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700122
Amith Yamasanifa3eea52012-03-28 10:25:08 -0700123 if (android.provider.Settings.Secure.getInt(getContentResolver(), "multiuser_enabled", -1)
124 > 0) {
125 mEnableUserManagement = true;
126 }
127
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700128 mAuthenticatorHelper = new AuthenticatorHelper();
129 mAuthenticatorHelper.updateAuthDescriptions(this);
130 mAuthenticatorHelper.onAccountsUpdated(this, null);
131
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700132 getMetaData();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800133 mInLocalHeaderSwitch = true;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700134 super.onCreate(savedInstanceState);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800135 mInLocalHeaderSwitch = false;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700136
Gilles Debunne3661b622011-06-27 11:19:16 -0700137 if (!onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700138 highlightHeader(mTopLevelHeaderId);
Amith Yamasani72aa19d2010-12-09 06:07:12 -0800139 // Force the title so that it doesn't get overridden by a direct launch of
140 // a specific settings screen.
141 setTitle(R.string.settings_label);
142 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800143
144 // Retrieve any saved state
145 if (savedInstanceState != null) {
146 mCurrentHeader = savedInstanceState.getParcelable(SAVE_KEY_CURRENT_HEADER);
147 mParentHeader = savedInstanceState.getParcelable(SAVE_KEY_PARENT_HEADER);
148 }
149
150 // If the current header was saved, switch to it
151 if (savedInstanceState != null && mCurrentHeader != null) {
152 //switchToHeaderLocal(mCurrentHeader);
153 showBreadCrumbs(mCurrentHeader.title, null);
154 }
155
156 if (mParentHeader != null) {
157 setParentTitle(mParentHeader.title, null, new OnClickListener() {
158 public void onClick(View v) {
159 switchToParent(mParentHeader.fragment);
160 }
161 });
162 }
Gilles Debunnedc7101f2011-06-27 12:00:49 -0700163
Amith Yamasani3d384f42012-05-11 15:22:04 -0700164 // Override up navigation for multi-pane, since we handle it in the fragment breadcrumbs
165 if (onIsMultiPane()) {
166 getActionBar().setDisplayHomeAsUpEnabled(false);
167 getActionBar().setHomeButtonEnabled(false);
168 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800169 }
170
171 @Override
172 protected void onSaveInstanceState(Bundle outState) {
173 super.onSaveInstanceState(outState);
174
175 // Save the current fragment, if it is the same as originally launched
176 if (mCurrentHeader != null) {
177 outState.putParcelable(SAVE_KEY_CURRENT_HEADER, mCurrentHeader);
178 }
179 if (mParentHeader != null) {
180 outState.putParcelable(SAVE_KEY_PARENT_HEADER, mParentHeader);
181 }
182 }
183
Gilles Debunnee78c1872011-06-20 15:00:07 -0700184 @Override
185 public void onResume() {
186 super.onResume();
187
188 ListAdapter listAdapter = getListAdapter();
189 if (listAdapter instanceof HeaderAdapter) {
190 ((HeaderAdapter) listAdapter).resume();
191 }
192 }
193
194 @Override
195 public void onPause() {
196 super.onPause();
197
198 ListAdapter listAdapter = getListAdapter();
199 if (listAdapter instanceof HeaderAdapter) {
200 ((HeaderAdapter) listAdapter).pause();
Amith Yamasani86708a82012-06-06 20:23:08 -0700201 }
202 }
203
204 @Override
205 public void onDestroy() {
206 super.onDestroy();
207 if (mListeningToAccountUpdates) {
Amith Yamasani56821db2012-06-05 13:20:45 -0700208 AccountManager.get(this).removeOnAccountsUpdatedListener(this);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700209 }
210 }
211
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800212 private void switchToHeaderLocal(Header header) {
213 mInLocalHeaderSwitch = true;
214 switchToHeader(header);
215 mInLocalHeaderSwitch = false;
216 }
217
218 @Override
219 public void switchToHeader(Header header) {
220 if (!mInLocalHeaderSwitch) {
221 mCurrentHeader = null;
222 mParentHeader = null;
223 }
224 super.switchToHeader(header);
225 }
226
227 /**
228 * Switch to parent fragment and store the grand parent's info
Jake Hamby2748fc22011-01-12 15:06:28 -0800229 * @param className name of the activity wrapper for the parent fragment.
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800230 */
231 private void switchToParent(String className) {
232 final ComponentName cn = new ComponentName(this, className);
233 try {
234 final PackageManager pm = getPackageManager();
235 final ActivityInfo parentInfo = pm.getActivityInfo(cn, PackageManager.GET_META_DATA);
236
237 if (parentInfo != null && parentInfo.metaData != null) {
238 String fragmentClass = parentInfo.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
239 CharSequence fragmentTitle = parentInfo.loadLabel(pm);
240 Header parentHeader = new Header();
241 parentHeader.fragment = fragmentClass;
242 parentHeader.title = fragmentTitle;
243 mCurrentHeader = parentHeader;
244
245 switchToHeaderLocal(parentHeader);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700246 highlightHeader(mTopLevelHeaderId);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800247
248 mParentHeader = new Header();
249 mParentHeader.fragment
250 = parentInfo.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
251 mParentHeader.title = parentInfo.metaData.getString(META_DATA_KEY_PARENT_TITLE);
252 }
253 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700254 Log.w(LOG_TAG, "Could not find parent activity : " + className);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800255 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700256 }
257
Amith Yamasani3965ae62010-11-15 14:45:19 -0800258 @Override
259 public void onNewIntent(Intent intent) {
260 super.onNewIntent(intent);
261
262 // If it is not launched from history, then reset to top-level
263 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0
Gilles Debunne3661b622011-06-27 11:19:16 -0700264 && mFirstHeader != null && !onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800265 switchToHeaderLocal(mFirstHeader);
Amith Yamasani3965ae62010-11-15 14:45:19 -0800266 }
267 }
268
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700269 private void highlightHeader(int id) {
270 if (id != 0) {
271 Integer index = mHeaderIndexMap.get(id);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700272 if (index != null) {
273 getListView().setItemChecked(index, true);
Amith Yamasani990fb522011-09-02 09:47:18 -0700274 getListView().smoothScrollToPosition(index);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700275 }
276 }
277 }
278
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700279 @Override
280 public Intent getIntent() {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700281 Intent superIntent = super.getIntent();
282 String startingFragment = getStartingFragmentClass(superIntent);
Gilles Debunne3661b622011-06-27 11:19:16 -0700283 // This is called from super.onCreate, isMultiPane() is not yet reliable
284 // Do not use onIsHidingHeaders either, which relies itself on this method
285 if (startingFragment != null && !onIsMultiPane()) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700286 Intent modIntent = new Intent(superIntent);
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700287 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700288 Bundle args = superIntent.getExtras();
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700289 if (args != null) {
290 args = new Bundle(args);
291 } else {
292 args = new Bundle();
293 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700294 args.putParcelable("intent", superIntent);
295 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras());
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700296 return modIntent;
297 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700298 return superIntent;
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700299 }
300
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700301 /**
Amith Yamasani379d9b02010-09-27 12:03:59 -0700302 * Checks if the component name in the intent is different from the Settings class and
303 * returns the class name to load as a fragment.
304 */
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700305 protected String getStartingFragmentClass(Intent intent) {
306 if (mFragmentClass != null) return mFragmentClass;
307
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700308 String intentClass = intent.getComponent().getClassName();
Amith Yamasani379d9b02010-09-27 12:03:59 -0700309 if (intentClass.equals(getClass().getName())) return null;
310
Dianne Hackbornee293792010-11-01 12:32:33 -0700311 if ("com.android.settings.ManageApplications".equals(intentClass)
312 || "com.android.settings.RunningServices".equals(intentClass)
313 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700314 // Old names of manage apps.
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700315 intentClass = com.android.settings.applications.ManageApplications.class.getName();
316 }
317
Amith Yamasani379d9b02010-09-27 12:03:59 -0700318 return intentClass;
319 }
320
321 /**
322 * Override initial header when an activity-alias is causing Settings to be launched
323 * for a specific fragment encoded in the android:name parameter.
324 */
325 @Override
326 public Header onGetInitialHeader() {
327 String fragmentClass = getStartingFragmentClass(super.getIntent());
328 if (fragmentClass != null) {
329 Header header = new Header();
330 header.fragment = fragmentClass;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800331 header.title = getTitle();
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700332 header.fragmentArguments = getIntent().getExtras();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800333 mCurrentHeader = header;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700334 return header;
335 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700336
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700337 return mFirstHeader;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700338 }
339
Dianne Hackbornb7258182011-03-15 16:23:55 -0700340 @Override
Dianne Hackborn48147dc2011-03-18 12:29:41 -0700341 public Intent onBuildStartFragmentIntent(String fragmentName, Bundle args,
342 int titleRes, int shortTitleRes) {
343 Intent intent = super.onBuildStartFragmentIntent(fragmentName, args,
344 titleRes, shortTitleRes);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700345
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700346 // some fragments want to avoid split actionbar
Gilles Debunne162e5412011-07-11 14:13:31 -0700347 if (DataUsageSummary.class.getName().equals(fragmentName) ||
348 PowerUsageSummary.class.getName().equals(fragmentName) ||
Gilles Debunnecd8e5242011-07-25 11:36:15 -0700349 AccountSyncSettings.class.getName().equals(fragmentName) ||
Jeff Sharkey11d30122012-03-19 16:54:07 -0700350 UserDictionarySettings.class.getName().equals(fragmentName) ||
Adam Powellfaba7e42012-03-26 17:28:38 -0700351 Memory.class.getName().equals(fragmentName) ||
Jeff Sharkeya339cba2012-05-08 11:42:49 -0700352 ManageApplications.class.getName().equals(fragmentName) ||
353 WirelessSettings.class.getName().equals(fragmentName) ||
354 SoundSettings.class.getName().equals(fragmentName) ||
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700355 PrivacySettings.class.getName().equals(fragmentName) ||
356 ManageAccountsSettings.class.getName().equals(fragmentName)) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700357 intent.putExtra(EXTRA_CLEAR_UI_OPTIONS, true);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700358 }
359
Dianne Hackbornb7258182011-03-15 16:23:55 -0700360 intent.setClass(this, SubSettings.class);
361 return intent;
362 }
363
Amith Yamasani379d9b02010-09-27 12:03:59 -0700364 /**
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700365 * Populate the activity with the top-level headers.
366 */
Amith Yamasanid7993472010-08-18 13:59:28 -0700367 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700368 public void onBuildHeaders(List<Header> headers) {
369 loadHeadersFromResource(R.xml.settings_headers, headers);
Amith Yamasanid7993472010-08-18 13:59:28 -0700370
Gilles Debunnee78c1872011-06-20 15:00:07 -0700371 updateHeaderList(headers);
372
373 mHeaders = headers;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700374 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700375
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700376 private void updateHeaderList(List<Header> target) {
377 int i = 0;
378 while (i < target.size()) {
379 Header header = target.get(i);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700380 // Ids are integers, so downcasting
381 int id = (int) header.id;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700382 if (id == R.id.dock_settings) {
383 if (!needsDockSettings())
384 target.remove(header);
385 } else if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
386 Utils.updateHeaderToSpecificActivityFromMetaDataOrRemove(this, target, header);
Gilles Debunne2454f492011-06-21 16:16:06 -0700387 } else if (id == R.id.wifi_settings) {
388 // Remove WiFi Settings if WiFi service is not available.
389 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
390 target.remove(header);
391 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700392 } else if (id == R.id.bluetooth_settings) {
393 // Remove Bluetooth Settings if Bluetooth service is not available.
Gilles Debunne2454f492011-06-21 16:16:06 -0700394 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700395 target.remove(header);
396 }
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700397 } else if (id == R.id.data_usage_settings) {
398 // Remove data usage when kernel module not enabled
399 final INetworkManagementService netManager = INetworkManagementService.Stub
400 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
401 try {
402 if (!netManager.isBandwidthControlEnabled()) {
403 target.remove(header);
404 }
405 } catch (RemoteException e) {
406 // ignored
407 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700408 } else if (id == R.id.account_settings) {
409 int headerIndex = i + 1;
410 i = insertAccountsHeaders(target, headerIndex);
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700411 } else if (id == R.id.user_settings) {
Amith Yamasanifa3eea52012-03-28 10:25:08 -0700412 if (!mEnableUserManagement
413 || !UserId.MU_ENABLED || UserId.myUserId() != 0
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700414 || !getResources().getBoolean(R.bool.enable_user_management)
415 || Utils.isMonkeyRunning()) {
416 target.remove(header);
417 }
418 }
419 if (UserId.MU_ENABLED && UserId.myUserId() != 0
420 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
421 target.remove(header);
Amith Yamasanid7993472010-08-18 13:59:28 -0700422 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700423
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700424 // Increment if the current one wasn't removed by the Utils code.
425 if (target.get(i) == header) {
Amith Yamasani3965ae62010-11-15 14:45:19 -0800426 // Hold on to the first header, when we need to reset to the top-level
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700427 if (mFirstHeader == null &&
428 HeaderAdapter.getHeaderType(header) != HeaderAdapter.HEADER_TYPE_CATEGORY) {
429 mFirstHeader = header;
430 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700431 mHeaderIndexMap.put(id, i);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700432 i++;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700433 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700434 }
435 }
436
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700437 private int insertAccountsHeaders(List<Header> target, int headerIndex) {
438 String[] accountTypes = mAuthenticatorHelper.getEnabledAccountTypes();
439 List<Header> accountHeaders = new ArrayList<Header>(accountTypes.length);
440 for (String accountType : accountTypes) {
441 CharSequence label = mAuthenticatorHelper.getLabelForType(this, accountType);
Amith Yamasani3882c2e2012-06-07 17:03:20 -0700442 Account[] accounts = AccountManager.get(this).getAccountsByType(accountType);
443 boolean skipToAccount = accounts.length == 1
444 && !mAuthenticatorHelper.hasAccountPreferences(accountType);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700445 Header accHeader = new Header();
446 accHeader.title = label;
447 if (accHeader.extras == null) {
448 accHeader.extras = new Bundle();
449 }
Amith Yamasani3882c2e2012-06-07 17:03:20 -0700450 if (skipToAccount) {
451 accHeader.breadCrumbTitleRes = R.string.account_sync_settings_title;
452 accHeader.breadCrumbShortTitleRes = R.string.account_sync_settings_title;
453 accHeader.fragment = AccountSyncSettings.class.getName();
454 accHeader.fragmentArguments = new Bundle();
455 // Need this for the icon
456 accHeader.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
457 accHeader.extras.putParcelable(AccountSyncSettings.ACCOUNT_KEY, accounts[0]);
458 accHeader.fragmentArguments.putParcelable(AccountSyncSettings.ACCOUNT_KEY,
459 accounts[0]);
460 } else {
461 accHeader.breadCrumbTitle = label;
462 accHeader.breadCrumbShortTitle = label;
463 accHeader.fragment = ManageAccountsSettings.class.getName();
464 accHeader.fragmentArguments = new Bundle();
465 accHeader.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
466 accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE,
467 accountType);
468 if (!isMultiPane()) {
469 accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_LABEL,
470 label.toString());
471 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700472 }
473 accountHeaders.add(accHeader);
474 }
475
476 // Sort by label
477 Collections.sort(accountHeaders, new Comparator<Header>() {
478 @Override
479 public int compare(Header h1, Header h2) {
480 return h1.title.toString().compareTo(h2.title.toString());
481 }
482 });
483
484 for (Header header : accountHeaders) {
485 target.add(headerIndex++, header);
486 }
Amith Yamasani86708a82012-06-06 20:23:08 -0700487 if (!mListeningToAccountUpdates) {
488 AccountManager.get(this).addOnAccountsUpdatedListener(this, null, true);
489 mListeningToAccountUpdates = true;
490 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700491 return headerIndex;
492 }
493
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700494 private boolean needsDockSettings() {
495 return getResources().getBoolean(R.bool.has_dock_settings);
Amith Yamasanib61cf512010-09-12 08:17:50 -0700496 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700497
498 private void getMetaData() {
499 try {
500 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
501 PackageManager.GET_META_DATA);
502 if (ai == null || ai.metaData == null) return;
503 mTopLevelHeaderId = ai.metaData.getInt(META_DATA_KEY_HEADER_ID);
504 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700505
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800506 // Check if it has a parent specified and create a Header object
507 final int parentHeaderTitleRes = ai.metaData.getInt(META_DATA_KEY_PARENT_TITLE);
508 String parentFragmentClass = ai.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
509 if (parentFragmentClass != null) {
510 mParentHeader = new Header();
511 mParentHeader.fragment = parentFragmentClass;
512 if (parentHeaderTitleRes != 0) {
513 mParentHeader.title = getResources().getString(parentHeaderTitleRes);
514 }
515 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700516 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700517 // No recovery
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700518 }
519 }
520
Amith Yamasani9e3a4702011-01-11 09:09:26 -0800521 @Override
522 public boolean hasNextButton() {
523 return super.hasNextButton();
524 }
525
526 @Override
527 public Button getNextButton() {
528 return super.getNextButton();
529 }
530
Gilles Debunnee78c1872011-06-20 15:00:07 -0700531 private static class HeaderAdapter extends ArrayAdapter<Header> {
532 static final int HEADER_TYPE_CATEGORY = 0;
533 static final int HEADER_TYPE_NORMAL = 1;
534 static final int HEADER_TYPE_SWITCH = 2;
535 private static final int HEADER_TYPE_COUNT = HEADER_TYPE_SWITCH + 1;
536
537 private final WifiEnabler mWifiEnabler;
538 private final BluetoothEnabler mBluetoothEnabler;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700539 private AuthenticatorHelper mAuthHelper;
Gilles Debunnee78c1872011-06-20 15:00:07 -0700540
541 private static class HeaderViewHolder {
542 ImageView icon;
543 TextView title;
544 TextView summary;
545 Switch switch_;
546 }
547
548 private LayoutInflater mInflater;
549
550 static int getHeaderType(Header header) {
551 if (header.fragment == null && header.intent == null) {
552 return HEADER_TYPE_CATEGORY;
553 } else if (header.id == R.id.wifi_settings || header.id == R.id.bluetooth_settings) {
554 return HEADER_TYPE_SWITCH;
555 } else {
556 return HEADER_TYPE_NORMAL;
557 }
558 }
559
560 @Override
561 public int getItemViewType(int position) {
562 Header header = getItem(position);
563 return getHeaderType(header);
564 }
565
566 @Override
567 public boolean areAllItemsEnabled() {
568 return false; // because of categories
569 }
570
571 @Override
572 public boolean isEnabled(int position) {
573 return getItemViewType(position) != HEADER_TYPE_CATEGORY;
574 }
575
576 @Override
577 public int getViewTypeCount() {
578 return HEADER_TYPE_COUNT;
579 }
580
581 @Override
582 public boolean hasStableIds() {
583 return true;
584 }
585
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700586 public HeaderAdapter(Context context, List<Header> objects,
587 AuthenticatorHelper authenticatorHelper) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700588 super(context, 0, objects);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700589
590 mAuthHelper = authenticatorHelper;
Gilles Debunnee78c1872011-06-20 15:00:07 -0700591 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700592
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700593 // Temp Switches provided as placeholder until the adapter replaces these with actual
Gilles Debunnee78c1872011-06-20 15:00:07 -0700594 // Switches inflated from their layouts. Must be done before adapter is set in super
595 mWifiEnabler = new WifiEnabler(context, new Switch(context));
596 mBluetoothEnabler = new BluetoothEnabler(context, new Switch(context));
597 }
598
599 @Override
600 public View getView(int position, View convertView, ViewGroup parent) {
601 HeaderViewHolder holder;
602 Header header = getItem(position);
603 int headerType = getHeaderType(header);
604 View view = null;
605
606 if (convertView == null) {
607 holder = new HeaderViewHolder();
608 switch (headerType) {
609 case HEADER_TYPE_CATEGORY:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700610 view = new TextView(getContext(), null,
611 android.R.attr.listSeparatorTextViewStyle);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700612 holder.title = (TextView) view;
613 break;
614
615 case HEADER_TYPE_SWITCH:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700616 view = mInflater.inflate(R.layout.preference_header_switch_item, parent,
617 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700618 holder.icon = (ImageView) view.findViewById(R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700619 holder.title = (TextView)
620 view.findViewById(com.android.internal.R.id.title);
621 holder.summary = (TextView)
622 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700623 holder.switch_ = (Switch) view.findViewById(R.id.switchWidget);
624 break;
625
626 case HEADER_TYPE_NORMAL:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700627 view = mInflater.inflate(
Amith Yamasanic8a93172012-06-08 13:35:47 -0700628 R.layout.preference_header_item, parent,
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700629 false);
Amith Yamasanic8a93172012-06-08 13:35:47 -0700630 holder.icon = (ImageView) view.findViewById(R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700631 holder.title = (TextView)
632 view.findViewById(com.android.internal.R.id.title);
633 holder.summary = (TextView)
634 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700635 break;
636 }
637 view.setTag(holder);
638 } else {
639 view = convertView;
640 holder = (HeaderViewHolder) view.getTag();
641 }
642
643 // All view fields must be updated every time, because the view may be recycled
644 switch (headerType) {
645 case HEADER_TYPE_CATEGORY:
646 holder.title.setText(header.getTitle(getContext().getResources()));
647 break;
648
649 case HEADER_TYPE_SWITCH:
650 // Would need a different treatment if the main menu had more switches
651 if (header.id == R.id.wifi_settings) {
652 mWifiEnabler.setSwitch(holder.switch_);
653 } else {
654 mBluetoothEnabler.setSwitch(holder.switch_);
655 }
656 // No break, fall through on purpose to update common fields
657
658 //$FALL-THROUGH$
659 case HEADER_TYPE_NORMAL:
Amith Yamasani3882c2e2012-06-07 17:03:20 -0700660 if (header.extras != null
661 && header.extras.containsKey(ManageAccountsSettings.KEY_ACCOUNT_TYPE)) {
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700662 String accType = header.extras.getString(
663 ManageAccountsSettings.KEY_ACCOUNT_TYPE);
664 ViewGroup.LayoutParams lp = holder.icon.getLayoutParams();
665 lp.width = getContext().getResources().getDimensionPixelSize(
666 R.dimen.header_icon_width);
667 lp.height = lp.width;
668 holder.icon.setLayoutParams(lp);
669 Drawable icon = mAuthHelper.getDrawableForType(getContext(), accType);
670 holder.icon.setImageDrawable(icon);
671 } else {
672 holder.icon.setImageResource(header.iconRes);
673 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700674 holder.title.setText(header.getTitle(getContext().getResources()));
675 CharSequence summary = header.getSummary(getContext().getResources());
676 if (!TextUtils.isEmpty(summary)) {
677 holder.summary.setVisibility(View.VISIBLE);
678 holder.summary.setText(summary);
679 } else {
680 holder.summary.setVisibility(View.GONE);
681 }
682 break;
683 }
684
685 return view;
686 }
687
688 public void resume() {
689 mWifiEnabler.resume();
690 mBluetoothEnabler.resume();
691 }
692
693 public void pause() {
694 mWifiEnabler.pause();
695 mBluetoothEnabler.pause();
696 }
697 }
698
699 @Override
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700700 public void onHeaderClick(Header header, int position) {
701 boolean revert = false;
702 if (header.id == R.id.account_add) {
703 revert = true;
704 }
705
706 super.onHeaderClick(header, position);
707
708 if (revert && mLastHeader != null) {
709 highlightHeader((int) mLastHeader.id);
710 } else {
711 mLastHeader = header;
712 }
713 }
714
715 @Override
Amith Yamasania4379d62011-07-22 10:34:58 -0700716 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
717 // Override the fragment title for Wallpaper settings
Amith Yamasanidfb65432011-11-29 16:38:14 -0800718 int titleRes = pref.getTitleRes();
Amith Yamasania4379d62011-07-22 10:34:58 -0700719 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
Amith Yamasanidfb65432011-11-29 16:38:14 -0800720 titleRes = R.string.wallpaper_settings_fragment_title;
Amith Yamasania4379d62011-07-22 10:34:58 -0700721 }
Jean Chalard7dabe452012-05-10 18:08:07 +0900722 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
723 null, 0);
Amith Yamasania4379d62011-07-22 10:34:58 -0700724 return true;
725 }
726
Amith Yamasani3d384f42012-05-11 15:22:04 -0700727 public boolean shouldUpRecreateTask(Intent targetIntent) {
728 return super.shouldUpRecreateTask(new Intent(this, Settings.class));
729 }
730
Amith Yamasania4379d62011-07-22 10:34:58 -0700731 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700732 public void setListAdapter(ListAdapter adapter) {
733 if (mHeaders == null) {
734 mHeaders = new ArrayList<Header>();
735 // When the saved state provides the list of headers, onBuildHeaders is not called
736 // Copy the list of Headers from the adapter, preserving their order
737 for (int i = 0; i < adapter.getCount(); i++) {
738 mHeaders.add((Header) adapter.getItem(i));
739 }
740 }
741
742 // Ignore the adapter provided by PreferenceActivity and substitute ours instead
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700743 super.setListAdapter(new HeaderAdapter(this, mHeaders, mAuthenticatorHelper));
Gilles Debunnee78c1872011-06-20 15:00:07 -0700744 }
745
Amith Yamasani56821db2012-06-05 13:20:45 -0700746 @Override
747 public void onAccountsUpdated(Account[] accounts) {
748 mAuthenticatorHelper.onAccountsUpdated(this, accounts);
749 invalidateHeaders();
750 }
751
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700752 /*
753 * Settings subclasses for launching independently.
754 */
Gilles Debunnee78c1872011-06-20 15:00:07 -0700755 public static class BluetoothSettingsActivity extends Settings { /* empty */ }
756 public static class WirelessSettingsActivity extends Settings { /* empty */ }
757 public static class TetherSettingsActivity extends Settings { /* empty */ }
758 public static class VpnSettingsActivity extends Settings { /* empty */ }
759 public static class DateTimeSettingsActivity extends Settings { /* empty */ }
760 public static class StorageSettingsActivity extends Settings { /* empty */ }
761 public static class WifiSettingsActivity extends Settings { /* empty */ }
repo syncb98463f2011-06-30 10:58:43 -0700762 public static class WifiP2pSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700763 public static class InputMethodAndLanguageSettingsActivity extends Settings { /* empty */ }
Jeff Browne46c5f32012-04-05 11:42:18 -0700764 public static class KeyboardLayoutPickerActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700765 public static class InputMethodAndSubtypeEnablerActivity extends Settings { /* empty */ }
satoke077d2b2011-07-25 09:38:11 +0900766 public static class SpellCheckersSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700767 public static class LocalePickerActivity extends Settings { /* empty */ }
768 public static class UserDictionarySettingsActivity extends Settings { /* empty */ }
769 public static class SoundSettingsActivity extends Settings { /* empty */ }
770 public static class DisplaySettingsActivity extends Settings { /* empty */ }
771 public static class DeviceInfoSettingsActivity extends Settings { /* empty */ }
772 public static class ApplicationSettingsActivity extends Settings { /* empty */ }
773 public static class ManageApplicationsActivity extends Settings { /* empty */ }
774 public static class StorageUseActivity extends Settings { /* empty */ }
775 public static class DevelopmentSettingsActivity extends Settings { /* empty */ }
776 public static class AccessibilitySettingsActivity extends Settings { /* empty */ }
777 public static class SecuritySettingsActivity extends Settings { /* empty */ }
Gilles Debunnea6a8a142011-06-09 11:56:17 -0700778 public static class LocationSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700779 public static class PrivacySettingsActivity extends Settings { /* empty */ }
780 public static class DockSettingsActivity extends Settings { /* empty */ }
781 public static class RunningServicesActivity extends Settings { /* empty */ }
782 public static class ManageAccountsSettingsActivity extends Settings { /* empty */ }
783 public static class PowerUsageSummaryActivity extends Settings { /* empty */ }
784 public static class AccountSyncSettingsActivity extends Settings { /* empty */ }
785 public static class AccountSyncSettingsInAddAccountActivity extends Settings { /* empty */ }
786 public static class CryptKeeperSettingsActivity extends Settings { /* empty */ }
787 public static class DeviceAdminSettingsActivity extends Settings { /* empty */ }
788 public static class DataUsageSummaryActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700789 public static class AdvancedWifiSettingsActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700790 public static class TextToSpeechSettingsActivity extends Settings { /* empty */ }
Jeff Hamilton3d670de2011-09-21 16:44:36 -0500791 public static class AndroidBeamSettingsActivity extends Settings { /* empty */ }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800792}