blob: 88b3e87fdbae0738bc3bbc1389a8f944ba529347 [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;
Jeff Sharkey9fd7ac12012-08-25 00:06:08 -070027import com.android.settings.vpn2.VpnSettings;
Gilles Debunnecd8e5242011-07-25 11:36:15 -070028import com.android.settings.wifi.WifiEnabler;
29
Amith Yamasani56821db2012-06-05 13:20:45 -070030import android.accounts.Account;
31import android.accounts.AccountManager;
32import android.accounts.OnAccountsUpdateListener;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080033import android.content.ComponentName;
Gilles Debunnee78c1872011-06-20 15:00:07 -070034import android.content.Context;
Amith Yamasani379d9b02010-09-27 12:03:59 -070035import android.content.Intent;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070036import android.content.pm.ActivityInfo;
37import android.content.pm.PackageManager;
38import android.content.pm.PackageManager.NameNotFoundException;
Amith Yamasanid1ab8282012-05-18 09:50:08 -070039import android.graphics.drawable.Drawable;
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -070040import android.os.Bundle;
Jeff Sharkey34e964d2012-04-21 15:41:48 -070041import android.os.INetworkManagementService;
42import android.os.RemoteException;
43import android.os.ServiceManager;
Dianne Hackbornbb06a422012-08-16 11:01:57 -070044import android.os.UserHandle;
Amith Yamasania4379d62011-07-22 10:34:58 -070045import android.preference.Preference;
Amith Yamasani02cf71a2010-09-21 15:48:52 -070046import android.preference.PreferenceActivity;
Amith Yamasanid1ab8282012-05-18 09:50:08 -070047import android.preference.PreferenceActivity.Header;
Amith Yamasania4379d62011-07-22 10:34:58 -070048import android.preference.PreferenceFragment;
Gilles Debunnee78c1872011-06-20 15:00:07 -070049import android.text.TextUtils;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080050import android.util.Log;
Gilles Debunnee78c1872011-06-20 15:00:07 -070051import android.view.LayoutInflater;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080052import android.view.View;
53import android.view.View.OnClickListener;
Gilles Debunnee78c1872011-06-20 15:00:07 -070054import android.view.ViewGroup;
55import android.widget.ArrayAdapter;
Amith Yamasaniea7b28c2012-06-20 13:51:40 -070056import android.widget.BaseAdapter;
Amith Yamasani9e3a4702011-01-11 09:09:26 -080057import android.widget.Button;
Gilles Debunnee78c1872011-06-20 15:00:07 -070058import android.widget.ImageView;
59import android.widget.ListAdapter;
60import android.widget.Switch;
61import android.widget.TextView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080062
Gilles Debunnee78c1872011-06-20 15:00:07 -070063import java.util.ArrayList;
Amith Yamasanid1ab8282012-05-18 09:50:08 -070064import java.util.Collections;
65import java.util.Comparator;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070066import java.util.HashMap;
Amith Yamasani02cf71a2010-09-21 15:48:52 -070067import java.util.List;
Amith Yamasanid7993472010-08-18 13:59:28 -070068
69/**
70 * Top-level settings activity to handle single pane and double pane UI layout.
71 */
Amith Yamasani56821db2012-06-05 13:20:45 -070072public class Settings extends PreferenceActivity
73 implements ButtonBarHandler, OnAccountsUpdateListener {
Amith Yamasanid7993472010-08-18 13:59:28 -070074
Gilles Debunnee78c1872011-06-20 15:00:07 -070075 private static final String LOG_TAG = "Settings";
Amith Yamasaniea7b28c2012-06-20 13:51:40 -070076
Amith Yamasani5203bdf2010-11-04 09:59:44 -070077 private static final String META_DATA_KEY_HEADER_ID =
Gilles Debunnee78c1872011-06-20 15:00:07 -070078 "com.android.settings.TOP_LEVEL_HEADER_ID";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070079 private static final String META_DATA_KEY_FRAGMENT_CLASS =
Gilles Debunnee78c1872011-06-20 15:00:07 -070080 "com.android.settings.FRAGMENT_CLASS";
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080081 private static final String META_DATA_KEY_PARENT_TITLE =
82 "com.android.settings.PARENT_FRAGMENT_TITLE";
83 private static final String META_DATA_KEY_PARENT_FRAGMENT_CLASS =
84 "com.android.settings.PARENT_FRAGMENT_CLASS";
85
Jeff Sharkey54d0af52011-08-11 18:26:57 -070086 private static final String EXTRA_CLEAR_UI_OPTIONS = "settings:remove_ui_options";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070087
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080088 private static final String SAVE_KEY_CURRENT_HEADER = "com.android.settings.CURRENT_HEADER";
89 private static final String SAVE_KEY_PARENT_HEADER = "com.android.settings.PARENT_HEADER";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070090
91 private String mFragmentClass;
92 private int mTopLevelHeaderId;
Amith Yamasani3965ae62010-11-15 14:45:19 -080093 private Header mFirstHeader;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080094 private Header mCurrentHeader;
95 private Header mParentHeader;
96 private boolean mInLocalHeaderSwitch;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070097
Amith Yamasanib810a0d2012-03-25 10:12:26 -070098 // Show only these settings for restricted users
99 private int[] SETTINGS_FOR_RESTRICTED = {
Amith Yamasani32630392012-08-10 19:31:39 -0700100 R.id.wireless_section,
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700101 R.id.wifi_settings,
102 R.id.bluetooth_settings,
Amith Yamasani32630392012-08-10 19:31:39 -0700103 R.id.device_section,
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700104 R.id.sound_settings,
105 R.id.display_settings,
Jeff Sharkey90c8b202012-08-30 15:20:10 -0700106 R.id.storage_settings,
Dianne Hackborn271c8b02012-08-20 17:24:39 -0700107 R.id.application_settings,
Dianne Hackborn52e56a22012-09-12 17:02:23 -0700108 R.id.battery_settings,
Amith Yamasani32630392012-08-10 19:31:39 -0700109 R.id.personal_section,
Amith Yamasani3deeeb72012-04-05 14:44:48 -0700110 R.id.security_settings,
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700111 R.id.user_settings,
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700112 R.id.account_settings,
Amith Yamasani32630392012-08-10 19:31:39 -0700113 R.id.account_add,
114 R.id.system_section,
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700115 R.id.about_settings
116 };
117
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700118 // TODO: Update Call Settings based on airplane mode state.
Amith Yamasanib61cf512010-09-12 08:17:50 -0700119
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700120 protected HashMap<Integer, Integer> mHeaderIndexMap = new HashMap<Integer, Integer>();
121
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700122 private AuthenticatorHelper mAuthenticatorHelper;
123 private Header mLastHeader;
Amith Yamasani86708a82012-06-06 20:23:08 -0700124 private boolean mListeningToAccountUpdates;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700125
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700126 @Override
127 protected void onCreate(Bundle savedInstanceState) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700128 if (getIntent().getBooleanExtra(EXTRA_CLEAR_UI_OPTIONS, false)) {
129 getWindow().setUiOptions(0);
130 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700131
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700132 mAuthenticatorHelper = new AuthenticatorHelper();
133 mAuthenticatorHelper.updateAuthDescriptions(this);
134 mAuthenticatorHelper.onAccountsUpdated(this, null);
135
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700136 getMetaData();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800137 mInLocalHeaderSwitch = true;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700138 super.onCreate(savedInstanceState);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800139 mInLocalHeaderSwitch = false;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700140
Gilles Debunne3661b622011-06-27 11:19:16 -0700141 if (!onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700142 highlightHeader(mTopLevelHeaderId);
Amith Yamasani72aa19d2010-12-09 06:07:12 -0800143 // Force the title so that it doesn't get overridden by a direct launch of
144 // a specific settings screen.
145 setTitle(R.string.settings_label);
146 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800147
148 // Retrieve any saved state
149 if (savedInstanceState != null) {
150 mCurrentHeader = savedInstanceState.getParcelable(SAVE_KEY_CURRENT_HEADER);
151 mParentHeader = savedInstanceState.getParcelable(SAVE_KEY_PARENT_HEADER);
152 }
153
154 // If the current header was saved, switch to it
155 if (savedInstanceState != null && mCurrentHeader != null) {
156 //switchToHeaderLocal(mCurrentHeader);
157 showBreadCrumbs(mCurrentHeader.title, null);
158 }
159
160 if (mParentHeader != null) {
161 setParentTitle(mParentHeader.title, null, new OnClickListener() {
162 public void onClick(View v) {
163 switchToParent(mParentHeader.fragment);
164 }
165 });
166 }
Gilles Debunnedc7101f2011-06-27 12:00:49 -0700167
Amith Yamasani3d384f42012-05-11 15:22:04 -0700168 // Override up navigation for multi-pane, since we handle it in the fragment breadcrumbs
169 if (onIsMultiPane()) {
170 getActionBar().setDisplayHomeAsUpEnabled(false);
171 getActionBar().setHomeButtonEnabled(false);
172 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800173 }
174
175 @Override
176 protected void onSaveInstanceState(Bundle outState) {
177 super.onSaveInstanceState(outState);
178
179 // Save the current fragment, if it is the same as originally launched
180 if (mCurrentHeader != null) {
181 outState.putParcelable(SAVE_KEY_CURRENT_HEADER, mCurrentHeader);
182 }
183 if (mParentHeader != null) {
184 outState.putParcelable(SAVE_KEY_PARENT_HEADER, mParentHeader);
185 }
186 }
187
Gilles Debunnee78c1872011-06-20 15:00:07 -0700188 @Override
189 public void onResume() {
190 super.onResume();
191
192 ListAdapter listAdapter = getListAdapter();
193 if (listAdapter instanceof HeaderAdapter) {
194 ((HeaderAdapter) listAdapter).resume();
195 }
Amith Yamasaniea7b28c2012-06-20 13:51:40 -0700196 invalidateHeaders();
Gilles Debunnee78c1872011-06-20 15:00:07 -0700197 }
198
199 @Override
200 public void onPause() {
201 super.onPause();
202
203 ListAdapter listAdapter = getListAdapter();
204 if (listAdapter instanceof HeaderAdapter) {
205 ((HeaderAdapter) listAdapter).pause();
Amith Yamasani86708a82012-06-06 20:23:08 -0700206 }
207 }
208
209 @Override
210 public void onDestroy() {
211 super.onDestroy();
212 if (mListeningToAccountUpdates) {
Amith Yamasani56821db2012-06-05 13:20:45 -0700213 AccountManager.get(this).removeOnAccountsUpdatedListener(this);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700214 }
215 }
216
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800217 private void switchToHeaderLocal(Header header) {
218 mInLocalHeaderSwitch = true;
219 switchToHeader(header);
220 mInLocalHeaderSwitch = false;
221 }
222
223 @Override
224 public void switchToHeader(Header header) {
225 if (!mInLocalHeaderSwitch) {
226 mCurrentHeader = null;
227 mParentHeader = null;
228 }
229 super.switchToHeader(header);
230 }
231
232 /**
233 * Switch to parent fragment and store the grand parent's info
Jake Hamby2748fc22011-01-12 15:06:28 -0800234 * @param className name of the activity wrapper for the parent fragment.
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800235 */
236 private void switchToParent(String className) {
237 final ComponentName cn = new ComponentName(this, className);
238 try {
239 final PackageManager pm = getPackageManager();
240 final ActivityInfo parentInfo = pm.getActivityInfo(cn, PackageManager.GET_META_DATA);
241
242 if (parentInfo != null && parentInfo.metaData != null) {
243 String fragmentClass = parentInfo.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
244 CharSequence fragmentTitle = parentInfo.loadLabel(pm);
245 Header parentHeader = new Header();
246 parentHeader.fragment = fragmentClass;
247 parentHeader.title = fragmentTitle;
248 mCurrentHeader = parentHeader;
249
250 switchToHeaderLocal(parentHeader);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700251 highlightHeader(mTopLevelHeaderId);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800252
253 mParentHeader = new Header();
254 mParentHeader.fragment
255 = parentInfo.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
256 mParentHeader.title = parentInfo.metaData.getString(META_DATA_KEY_PARENT_TITLE);
257 }
258 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700259 Log.w(LOG_TAG, "Could not find parent activity : " + className);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800260 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700261 }
262
Amith Yamasani3965ae62010-11-15 14:45:19 -0800263 @Override
264 public void onNewIntent(Intent intent) {
265 super.onNewIntent(intent);
266
267 // If it is not launched from history, then reset to top-level
268 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0
Gilles Debunne3661b622011-06-27 11:19:16 -0700269 && mFirstHeader != null && !onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800270 switchToHeaderLocal(mFirstHeader);
Amith Yamasani3965ae62010-11-15 14:45:19 -0800271 }
272 }
273
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700274 private void highlightHeader(int id) {
275 if (id != 0) {
276 Integer index = mHeaderIndexMap.get(id);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700277 if (index != null) {
278 getListView().setItemChecked(index, true);
Amith Yamasani990fb522011-09-02 09:47:18 -0700279 getListView().smoothScrollToPosition(index);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700280 }
281 }
282 }
283
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700284 @Override
285 public Intent getIntent() {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700286 Intent superIntent = super.getIntent();
287 String startingFragment = getStartingFragmentClass(superIntent);
Gilles Debunne3661b622011-06-27 11:19:16 -0700288 // This is called from super.onCreate, isMultiPane() is not yet reliable
289 // Do not use onIsHidingHeaders either, which relies itself on this method
290 if (startingFragment != null && !onIsMultiPane()) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700291 Intent modIntent = new Intent(superIntent);
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700292 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700293 Bundle args = superIntent.getExtras();
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700294 if (args != null) {
295 args = new Bundle(args);
296 } else {
297 args = new Bundle();
298 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700299 args.putParcelable("intent", superIntent);
300 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras());
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700301 return modIntent;
302 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700303 return superIntent;
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700304 }
305
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700306 /**
Amith Yamasani379d9b02010-09-27 12:03:59 -0700307 * Checks if the component name in the intent is different from the Settings class and
308 * returns the class name to load as a fragment.
309 */
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700310 protected String getStartingFragmentClass(Intent intent) {
311 if (mFragmentClass != null) return mFragmentClass;
312
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700313 String intentClass = intent.getComponent().getClassName();
Amith Yamasani379d9b02010-09-27 12:03:59 -0700314 if (intentClass.equals(getClass().getName())) return null;
315
Dianne Hackbornee293792010-11-01 12:32:33 -0700316 if ("com.android.settings.ManageApplications".equals(intentClass)
317 || "com.android.settings.RunningServices".equals(intentClass)
318 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700319 // Old names of manage apps.
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700320 intentClass = com.android.settings.applications.ManageApplications.class.getName();
321 }
322
Amith Yamasani379d9b02010-09-27 12:03:59 -0700323 return intentClass;
324 }
325
326 /**
327 * Override initial header when an activity-alias is causing Settings to be launched
328 * for a specific fragment encoded in the android:name parameter.
329 */
330 @Override
331 public Header onGetInitialHeader() {
332 String fragmentClass = getStartingFragmentClass(super.getIntent());
333 if (fragmentClass != null) {
334 Header header = new Header();
335 header.fragment = fragmentClass;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800336 header.title = getTitle();
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700337 header.fragmentArguments = getIntent().getExtras();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800338 mCurrentHeader = header;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700339 return header;
340 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700341
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700342 return mFirstHeader;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700343 }
344
Dianne Hackbornb7258182011-03-15 16:23:55 -0700345 @Override
Dianne Hackborn48147dc2011-03-18 12:29:41 -0700346 public Intent onBuildStartFragmentIntent(String fragmentName, Bundle args,
347 int titleRes, int shortTitleRes) {
348 Intent intent = super.onBuildStartFragmentIntent(fragmentName, args,
349 titleRes, shortTitleRes);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700350
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700351 // some fragments want to avoid split actionbar
Gilles Debunne162e5412011-07-11 14:13:31 -0700352 if (DataUsageSummary.class.getName().equals(fragmentName) ||
353 PowerUsageSummary.class.getName().equals(fragmentName) ||
Gilles Debunnecd8e5242011-07-25 11:36:15 -0700354 AccountSyncSettings.class.getName().equals(fragmentName) ||
Jeff Sharkey11d30122012-03-19 16:54:07 -0700355 UserDictionarySettings.class.getName().equals(fragmentName) ||
Adam Powellfaba7e42012-03-26 17:28:38 -0700356 Memory.class.getName().equals(fragmentName) ||
Jeff Sharkeya339cba2012-05-08 11:42:49 -0700357 ManageApplications.class.getName().equals(fragmentName) ||
358 WirelessSettings.class.getName().equals(fragmentName) ||
359 SoundSettings.class.getName().equals(fragmentName) ||
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700360 PrivacySettings.class.getName().equals(fragmentName) ||
Jeff Sharkey9fd7ac12012-08-25 00:06:08 -0700361 ManageAccountsSettings.class.getName().equals(fragmentName) ||
362 VpnSettings.class.getName().equals(fragmentName)) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700363 intent.putExtra(EXTRA_CLEAR_UI_OPTIONS, true);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700364 }
365
Dianne Hackbornb7258182011-03-15 16:23:55 -0700366 intent.setClass(this, SubSettings.class);
367 return intent;
368 }
Amith Yamasaniea7b28c2012-06-20 13:51:40 -0700369
Amith Yamasani379d9b02010-09-27 12:03:59 -0700370 /**
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700371 * Populate the activity with the top-level headers.
372 */
Amith Yamasanid7993472010-08-18 13:59:28 -0700373 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700374 public void onBuildHeaders(List<Header> headers) {
375 loadHeadersFromResource(R.xml.settings_headers, headers);
Amith Yamasanid7993472010-08-18 13:59:28 -0700376
Gilles Debunnee78c1872011-06-20 15:00:07 -0700377 updateHeaderList(headers);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700378 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700379
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700380 private void updateHeaderList(List<Header> target) {
381 int i = 0;
382 while (i < target.size()) {
383 Header header = target.get(i);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700384 // Ids are integers, so downcasting
385 int id = (int) header.id;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700386 if (id == R.id.dock_settings) {
387 if (!needsDockSettings())
388 target.remove(header);
389 } else if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
390 Utils.updateHeaderToSpecificActivityFromMetaDataOrRemove(this, target, header);
Gilles Debunne2454f492011-06-21 16:16:06 -0700391 } else if (id == R.id.wifi_settings) {
392 // Remove WiFi Settings if WiFi service is not available.
393 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
394 target.remove(header);
395 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700396 } else if (id == R.id.bluetooth_settings) {
397 // Remove Bluetooth Settings if Bluetooth service is not available.
Gilles Debunne2454f492011-06-21 16:16:06 -0700398 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700399 target.remove(header);
400 }
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700401 } else if (id == R.id.data_usage_settings) {
402 // Remove data usage when kernel module not enabled
403 final INetworkManagementService netManager = INetworkManagementService.Stub
404 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
405 try {
406 if (!netManager.isBandwidthControlEnabled()) {
407 target.remove(header);
408 }
409 } catch (RemoteException e) {
410 // ignored
411 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700412 } else if (id == R.id.account_settings) {
413 int headerIndex = i + 1;
414 i = insertAccountsHeaders(target, headerIndex);
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700415 } else if (id == R.id.user_settings) {
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700416 if (!UserHandle.MU_ENABLED
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700417 || !getResources().getBoolean(R.bool.enable_user_management)
418 || Utils.isMonkeyRunning()) {
419 target.remove(header);
420 }
421 }
Dianne Hackbornbb06a422012-08-16 11:01:57 -0700422 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700423 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
424 target.remove(header);
Amith Yamasanid7993472010-08-18 13:59:28 -0700425 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700426
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700427 // Increment if the current one wasn't removed by the Utils code.
428 if (target.get(i) == header) {
Amith Yamasani3965ae62010-11-15 14:45:19 -0800429 // Hold on to the first header, when we need to reset to the top-level
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700430 if (mFirstHeader == null &&
431 HeaderAdapter.getHeaderType(header) != HeaderAdapter.HEADER_TYPE_CATEGORY) {
432 mFirstHeader = header;
433 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700434 mHeaderIndexMap.put(id, i);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700435 i++;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700436 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700437 }
438 }
439
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700440 private int insertAccountsHeaders(List<Header> target, int headerIndex) {
441 String[] accountTypes = mAuthenticatorHelper.getEnabledAccountTypes();
442 List<Header> accountHeaders = new ArrayList<Header>(accountTypes.length);
443 for (String accountType : accountTypes) {
444 CharSequence label = mAuthenticatorHelper.getLabelForType(this, accountType);
Brian Muramatsuc28af522012-06-28 14:25:14 -0700445 if (label == null) {
446 continue;
447 }
448
Amith Yamasani3882c2e2012-06-07 17:03:20 -0700449 Account[] accounts = AccountManager.get(this).getAccountsByType(accountType);
450 boolean skipToAccount = accounts.length == 1
451 && !mAuthenticatorHelper.hasAccountPreferences(accountType);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700452 Header accHeader = new Header();
453 accHeader.title = label;
454 if (accHeader.extras == null) {
455 accHeader.extras = new Bundle();
456 }
Amith Yamasani3882c2e2012-06-07 17:03:20 -0700457 if (skipToAccount) {
458 accHeader.breadCrumbTitleRes = R.string.account_sync_settings_title;
459 accHeader.breadCrumbShortTitleRes = R.string.account_sync_settings_title;
460 accHeader.fragment = AccountSyncSettings.class.getName();
461 accHeader.fragmentArguments = new Bundle();
462 // Need this for the icon
463 accHeader.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
464 accHeader.extras.putParcelable(AccountSyncSettings.ACCOUNT_KEY, accounts[0]);
465 accHeader.fragmentArguments.putParcelable(AccountSyncSettings.ACCOUNT_KEY,
466 accounts[0]);
467 } else {
468 accHeader.breadCrumbTitle = label;
469 accHeader.breadCrumbShortTitle = label;
470 accHeader.fragment = ManageAccountsSettings.class.getName();
471 accHeader.fragmentArguments = new Bundle();
472 accHeader.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
473 accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE,
474 accountType);
475 if (!isMultiPane()) {
476 accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_LABEL,
477 label.toString());
478 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700479 }
480 accountHeaders.add(accHeader);
481 }
482
483 // Sort by label
484 Collections.sort(accountHeaders, new Comparator<Header>() {
485 @Override
486 public int compare(Header h1, Header h2) {
487 return h1.title.toString().compareTo(h2.title.toString());
488 }
489 });
490
491 for (Header header : accountHeaders) {
492 target.add(headerIndex++, header);
493 }
Amith Yamasani86708a82012-06-06 20:23:08 -0700494 if (!mListeningToAccountUpdates) {
495 AccountManager.get(this).addOnAccountsUpdatedListener(this, null, true);
496 mListeningToAccountUpdates = true;
497 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700498 return headerIndex;
499 }
500
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700501 private boolean needsDockSettings() {
502 return getResources().getBoolean(R.bool.has_dock_settings);
Amith Yamasanib61cf512010-09-12 08:17:50 -0700503 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700504
505 private void getMetaData() {
506 try {
507 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
508 PackageManager.GET_META_DATA);
509 if (ai == null || ai.metaData == null) return;
510 mTopLevelHeaderId = ai.metaData.getInt(META_DATA_KEY_HEADER_ID);
511 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700512
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800513 // Check if it has a parent specified and create a Header object
514 final int parentHeaderTitleRes = ai.metaData.getInt(META_DATA_KEY_PARENT_TITLE);
515 String parentFragmentClass = ai.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
516 if (parentFragmentClass != null) {
517 mParentHeader = new Header();
518 mParentHeader.fragment = parentFragmentClass;
519 if (parentHeaderTitleRes != 0) {
520 mParentHeader.title = getResources().getString(parentHeaderTitleRes);
521 }
522 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700523 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700524 // No recovery
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700525 }
526 }
527
Amith Yamasani9e3a4702011-01-11 09:09:26 -0800528 @Override
529 public boolean hasNextButton() {
530 return super.hasNextButton();
531 }
532
533 @Override
534 public Button getNextButton() {
535 return super.getNextButton();
536 }
537
Gilles Debunnee78c1872011-06-20 15:00:07 -0700538 private static class HeaderAdapter extends ArrayAdapter<Header> {
539 static final int HEADER_TYPE_CATEGORY = 0;
540 static final int HEADER_TYPE_NORMAL = 1;
541 static final int HEADER_TYPE_SWITCH = 2;
542 private static final int HEADER_TYPE_COUNT = HEADER_TYPE_SWITCH + 1;
543
544 private final WifiEnabler mWifiEnabler;
545 private final BluetoothEnabler mBluetoothEnabler;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700546 private AuthenticatorHelper mAuthHelper;
Gilles Debunnee78c1872011-06-20 15:00:07 -0700547
548 private static class HeaderViewHolder {
549 ImageView icon;
550 TextView title;
551 TextView summary;
552 Switch switch_;
553 }
554
555 private LayoutInflater mInflater;
556
557 static int getHeaderType(Header header) {
558 if (header.fragment == null && header.intent == null) {
559 return HEADER_TYPE_CATEGORY;
560 } else if (header.id == R.id.wifi_settings || header.id == R.id.bluetooth_settings) {
561 return HEADER_TYPE_SWITCH;
562 } else {
563 return HEADER_TYPE_NORMAL;
564 }
565 }
566
567 @Override
568 public int getItemViewType(int position) {
569 Header header = getItem(position);
570 return getHeaderType(header);
571 }
572
573 @Override
574 public boolean areAllItemsEnabled() {
575 return false; // because of categories
576 }
577
578 @Override
579 public boolean isEnabled(int position) {
580 return getItemViewType(position) != HEADER_TYPE_CATEGORY;
581 }
582
583 @Override
584 public int getViewTypeCount() {
585 return HEADER_TYPE_COUNT;
586 }
587
588 @Override
589 public boolean hasStableIds() {
590 return true;
591 }
592
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700593 public HeaderAdapter(Context context, List<Header> objects,
594 AuthenticatorHelper authenticatorHelper) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700595 super(context, 0, objects);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700596
597 mAuthHelper = authenticatorHelper;
Gilles Debunnee78c1872011-06-20 15:00:07 -0700598 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700599
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700600 // Temp Switches provided as placeholder until the adapter replaces these with actual
Gilles Debunnee78c1872011-06-20 15:00:07 -0700601 // Switches inflated from their layouts. Must be done before adapter is set in super
602 mWifiEnabler = new WifiEnabler(context, new Switch(context));
603 mBluetoothEnabler = new BluetoothEnabler(context, new Switch(context));
604 }
605
606 @Override
607 public View getView(int position, View convertView, ViewGroup parent) {
608 HeaderViewHolder holder;
609 Header header = getItem(position);
610 int headerType = getHeaderType(header);
611 View view = null;
612
613 if (convertView == null) {
614 holder = new HeaderViewHolder();
615 switch (headerType) {
616 case HEADER_TYPE_CATEGORY:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700617 view = new TextView(getContext(), null,
618 android.R.attr.listSeparatorTextViewStyle);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700619 holder.title = (TextView) view;
620 break;
621
622 case HEADER_TYPE_SWITCH:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700623 view = mInflater.inflate(R.layout.preference_header_switch_item, parent,
624 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700625 holder.icon = (ImageView) view.findViewById(R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700626 holder.title = (TextView)
627 view.findViewById(com.android.internal.R.id.title);
628 holder.summary = (TextView)
629 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700630 holder.switch_ = (Switch) view.findViewById(R.id.switchWidget);
631 break;
632
633 case HEADER_TYPE_NORMAL:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700634 view = mInflater.inflate(
Amith Yamasanic8a93172012-06-08 13:35:47 -0700635 R.layout.preference_header_item, parent,
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700636 false);
Amith Yamasanic8a93172012-06-08 13:35:47 -0700637 holder.icon = (ImageView) view.findViewById(R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700638 holder.title = (TextView)
639 view.findViewById(com.android.internal.R.id.title);
640 holder.summary = (TextView)
641 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700642 break;
643 }
644 view.setTag(holder);
645 } else {
646 view = convertView;
647 holder = (HeaderViewHolder) view.getTag();
648 }
649
650 // All view fields must be updated every time, because the view may be recycled
651 switch (headerType) {
652 case HEADER_TYPE_CATEGORY:
653 holder.title.setText(header.getTitle(getContext().getResources()));
654 break;
655
656 case HEADER_TYPE_SWITCH:
657 // Would need a different treatment if the main menu had more switches
658 if (header.id == R.id.wifi_settings) {
659 mWifiEnabler.setSwitch(holder.switch_);
660 } else {
661 mBluetoothEnabler.setSwitch(holder.switch_);
662 }
663 // No break, fall through on purpose to update common fields
664
665 //$FALL-THROUGH$
666 case HEADER_TYPE_NORMAL:
Amith Yamasani3882c2e2012-06-07 17:03:20 -0700667 if (header.extras != null
668 && header.extras.containsKey(ManageAccountsSettings.KEY_ACCOUNT_TYPE)) {
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700669 String accType = header.extras.getString(
670 ManageAccountsSettings.KEY_ACCOUNT_TYPE);
671 ViewGroup.LayoutParams lp = holder.icon.getLayoutParams();
672 lp.width = getContext().getResources().getDimensionPixelSize(
673 R.dimen.header_icon_width);
674 lp.height = lp.width;
675 holder.icon.setLayoutParams(lp);
676 Drawable icon = mAuthHelper.getDrawableForType(getContext(), accType);
677 holder.icon.setImageDrawable(icon);
678 } else {
679 holder.icon.setImageResource(header.iconRes);
680 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700681 holder.title.setText(header.getTitle(getContext().getResources()));
682 CharSequence summary = header.getSummary(getContext().getResources());
683 if (!TextUtils.isEmpty(summary)) {
684 holder.summary.setVisibility(View.VISIBLE);
685 holder.summary.setText(summary);
686 } else {
687 holder.summary.setVisibility(View.GONE);
688 }
689 break;
690 }
691
692 return view;
693 }
694
695 public void resume() {
696 mWifiEnabler.resume();
697 mBluetoothEnabler.resume();
698 }
Brian Muramatsuc28af522012-06-28 14:25:14 -0700699
Gilles Debunnee78c1872011-06-20 15:00:07 -0700700 public void pause() {
701 mWifiEnabler.pause();
702 mBluetoothEnabler.pause();
703 }
704 }
705
706 @Override
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700707 public void onHeaderClick(Header header, int position) {
708 boolean revert = false;
709 if (header.id == R.id.account_add) {
710 revert = true;
711 }
712
713 super.onHeaderClick(header, position);
714
715 if (revert && mLastHeader != null) {
716 highlightHeader((int) mLastHeader.id);
717 } else {
718 mLastHeader = header;
719 }
720 }
721
722 @Override
Amith Yamasania4379d62011-07-22 10:34:58 -0700723 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
724 // Override the fragment title for Wallpaper settings
Amith Yamasanidfb65432011-11-29 16:38:14 -0800725 int titleRes = pref.getTitleRes();
Amith Yamasania4379d62011-07-22 10:34:58 -0700726 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
Amith Yamasanidfb65432011-11-29 16:38:14 -0800727 titleRes = R.string.wallpaper_settings_fragment_title;
Amith Yamasania4379d62011-07-22 10:34:58 -0700728 }
Jean Chalard7dabe452012-05-10 18:08:07 +0900729 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
730 null, 0);
Amith Yamasania4379d62011-07-22 10:34:58 -0700731 return true;
732 }
733
Amith Yamasani3d384f42012-05-11 15:22:04 -0700734 public boolean shouldUpRecreateTask(Intent targetIntent) {
735 return super.shouldUpRecreateTask(new Intent(this, Settings.class));
736 }
737
Amith Yamasania4379d62011-07-22 10:34:58 -0700738 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700739 public void setListAdapter(ListAdapter adapter) {
Amith Yamasaniea7b28c2012-06-20 13:51:40 -0700740 if (adapter == null) {
741 super.setListAdapter(null);
742 } else {
743 super.setListAdapter(new HeaderAdapter(this, getHeaders(), mAuthenticatorHelper));
Gilles Debunnee78c1872011-06-20 15:00:07 -0700744 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700745 }
746
Amith Yamasani56821db2012-06-05 13:20:45 -0700747 @Override
748 public void onAccountsUpdated(Account[] accounts) {
749 mAuthenticatorHelper.onAccountsUpdated(this, accounts);
750 invalidateHeaders();
751 }
752
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700753 /*
754 * Settings subclasses for launching independently.
755 */
Gilles Debunnee78c1872011-06-20 15:00:07 -0700756 public static class BluetoothSettingsActivity extends Settings { /* empty */ }
757 public static class WirelessSettingsActivity extends Settings { /* empty */ }
758 public static class TetherSettingsActivity extends Settings { /* empty */ }
759 public static class VpnSettingsActivity extends Settings { /* empty */ }
760 public static class DateTimeSettingsActivity extends Settings { /* empty */ }
761 public static class StorageSettingsActivity extends Settings { /* empty */ }
762 public static class WifiSettingsActivity extends Settings { /* empty */ }
repo syncb98463f2011-06-30 10:58:43 -0700763 public static class WifiP2pSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700764 public static class InputMethodAndLanguageSettingsActivity extends Settings { /* empty */ }
Jeff Browne46c5f32012-04-05 11:42:18 -0700765 public static class KeyboardLayoutPickerActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700766 public static class InputMethodAndSubtypeEnablerActivity extends Settings { /* empty */ }
satoke077d2b2011-07-25 09:38:11 +0900767 public static class SpellCheckersSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700768 public static class LocalePickerActivity extends Settings { /* empty */ }
769 public static class UserDictionarySettingsActivity extends Settings { /* empty */ }
770 public static class SoundSettingsActivity extends Settings { /* empty */ }
771 public static class DisplaySettingsActivity extends Settings { /* empty */ }
772 public static class DeviceInfoSettingsActivity extends Settings { /* empty */ }
773 public static class ApplicationSettingsActivity extends Settings { /* empty */ }
774 public static class ManageApplicationsActivity extends Settings { /* empty */ }
775 public static class StorageUseActivity extends Settings { /* empty */ }
776 public static class DevelopmentSettingsActivity extends Settings { /* empty */ }
777 public static class AccessibilitySettingsActivity extends Settings { /* empty */ }
778 public static class SecuritySettingsActivity extends Settings { /* empty */ }
Gilles Debunnea6a8a142011-06-09 11:56:17 -0700779 public static class LocationSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700780 public static class PrivacySettingsActivity extends Settings { /* empty */ }
781 public static class DockSettingsActivity extends Settings { /* empty */ }
782 public static class RunningServicesActivity extends Settings { /* empty */ }
783 public static class ManageAccountsSettingsActivity extends Settings { /* empty */ }
784 public static class PowerUsageSummaryActivity extends Settings { /* empty */ }
785 public static class AccountSyncSettingsActivity extends Settings { /* empty */ }
786 public static class AccountSyncSettingsInAddAccountActivity extends Settings { /* empty */ }
787 public static class CryptKeeperSettingsActivity extends Settings { /* empty */ }
788 public static class DeviceAdminSettingsActivity extends Settings { /* empty */ }
789 public static class DataUsageSummaryActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700790 public static class AdvancedWifiSettingsActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700791 public static class TextToSpeechSettingsActivity extends Settings { /* empty */ }
Jeff Hamilton3d670de2011-09-21 16:44:36 -0500792 public static class AndroidBeamSettingsActivity extends Settings { /* empty */ }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800793}