blob: fa30f09e147ebce1eef2218f3f4f4a10a3a7e838 [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;
Jeff Sharkey44202462012-09-19 13:13:45 -070045import android.os.UserManager;
Amith Yamasania4379d62011-07-22 10:34:58 -070046import android.preference.Preference;
Amith Yamasani02cf71a2010-09-21 15:48:52 -070047import android.preference.PreferenceActivity;
Amith Yamasanid1ab8282012-05-18 09:50:08 -070048import android.preference.PreferenceActivity.Header;
Amith Yamasania4379d62011-07-22 10:34:58 -070049import android.preference.PreferenceFragment;
Gilles Debunnee78c1872011-06-20 15:00:07 -070050import android.text.TextUtils;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080051import android.util.Log;
Gilles Debunnee78c1872011-06-20 15:00:07 -070052import android.view.LayoutInflater;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080053import android.view.View;
54import android.view.View.OnClickListener;
Gilles Debunnee78c1872011-06-20 15:00:07 -070055import android.view.ViewGroup;
56import android.widget.ArrayAdapter;
Amith Yamasaniea7b28c2012-06-20 13:51:40 -070057import android.widget.BaseAdapter;
Amith Yamasani9e3a4702011-01-11 09:09:26 -080058import android.widget.Button;
Gilles Debunnee78c1872011-06-20 15:00:07 -070059import android.widget.ImageView;
60import android.widget.ListAdapter;
61import android.widget.Switch;
62import android.widget.TextView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080063
Gilles Debunnee78c1872011-06-20 15:00:07 -070064import java.util.ArrayList;
Amith Yamasanid1ab8282012-05-18 09:50:08 -070065import java.util.Collections;
66import java.util.Comparator;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070067import java.util.HashMap;
Amith Yamasani02cf71a2010-09-21 15:48:52 -070068import java.util.List;
Amith Yamasanid7993472010-08-18 13:59:28 -070069
70/**
71 * Top-level settings activity to handle single pane and double pane UI layout.
72 */
Amith Yamasani56821db2012-06-05 13:20:45 -070073public class Settings extends PreferenceActivity
74 implements ButtonBarHandler, OnAccountsUpdateListener {
Amith Yamasanid7993472010-08-18 13:59:28 -070075
Gilles Debunnee78c1872011-06-20 15:00:07 -070076 private static final String LOG_TAG = "Settings";
Amith Yamasaniea7b28c2012-06-20 13:51:40 -070077
Amith Yamasani5203bdf2010-11-04 09:59:44 -070078 private static final String META_DATA_KEY_HEADER_ID =
Gilles Debunnee78c1872011-06-20 15:00:07 -070079 "com.android.settings.TOP_LEVEL_HEADER_ID";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070080 private static final String META_DATA_KEY_FRAGMENT_CLASS =
Gilles Debunnee78c1872011-06-20 15:00:07 -070081 "com.android.settings.FRAGMENT_CLASS";
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080082 private static final String META_DATA_KEY_PARENT_TITLE =
83 "com.android.settings.PARENT_FRAGMENT_TITLE";
84 private static final String META_DATA_KEY_PARENT_FRAGMENT_CLASS =
85 "com.android.settings.PARENT_FRAGMENT_CLASS";
86
Jeff Sharkey54d0af52011-08-11 18:26:57 -070087 private static final String EXTRA_CLEAR_UI_OPTIONS = "settings:remove_ui_options";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070088
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080089 private static final String SAVE_KEY_CURRENT_HEADER = "com.android.settings.CURRENT_HEADER";
90 private static final String SAVE_KEY_PARENT_HEADER = "com.android.settings.PARENT_HEADER";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070091
92 private String mFragmentClass;
93 private int mTopLevelHeaderId;
Amith Yamasani3965ae62010-11-15 14:45:19 -080094 private Header mFirstHeader;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080095 private Header mCurrentHeader;
96 private Header mParentHeader;
97 private boolean mInLocalHeaderSwitch;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070098
Amith Yamasanib810a0d2012-03-25 10:12:26 -070099 // Show only these settings for restricted users
100 private int[] SETTINGS_FOR_RESTRICTED = {
Amith Yamasani32630392012-08-10 19:31:39 -0700101 R.id.wireless_section,
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700102 R.id.wifi_settings,
103 R.id.bluetooth_settings,
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700104 R.id.data_usage_settings,
Amith Yamasani32630392012-08-10 19:31:39 -0700105 R.id.device_section,
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700106 R.id.sound_settings,
107 R.id.display_settings,
Jeff Sharkey90c8b202012-08-30 15:20:10 -0700108 R.id.storage_settings,
Dianne Hackborn271c8b02012-08-20 17:24:39 -0700109 R.id.application_settings,
Dianne Hackborn52e56a22012-09-12 17:02:23 -0700110 R.id.battery_settings,
Amith Yamasani32630392012-08-10 19:31:39 -0700111 R.id.personal_section,
Amith Yamasani3deeeb72012-04-05 14:44:48 -0700112 R.id.security_settings,
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700113 R.id.user_settings,
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700114 R.id.account_settings,
Amith Yamasani32630392012-08-10 19:31:39 -0700115 R.id.account_add,
116 R.id.system_section,
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700117 R.id.about_settings
118 };
119
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700120 // TODO: Update Call Settings based on airplane mode state.
Amith Yamasanib61cf512010-09-12 08:17:50 -0700121
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700122 protected HashMap<Integer, Integer> mHeaderIndexMap = new HashMap<Integer, Integer>();
123
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700124 private AuthenticatorHelper mAuthenticatorHelper;
125 private Header mLastHeader;
Amith Yamasani86708a82012-06-06 20:23:08 -0700126 private boolean mListeningToAccountUpdates;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700127
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700128 @Override
129 protected void onCreate(Bundle savedInstanceState) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700130 if (getIntent().getBooleanExtra(EXTRA_CLEAR_UI_OPTIONS, false)) {
131 getWindow().setUiOptions(0);
132 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700133
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700134 mAuthenticatorHelper = new AuthenticatorHelper();
135 mAuthenticatorHelper.updateAuthDescriptions(this);
136 mAuthenticatorHelper.onAccountsUpdated(this, null);
137
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700138 getMetaData();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800139 mInLocalHeaderSwitch = true;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700140 super.onCreate(savedInstanceState);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800141 mInLocalHeaderSwitch = false;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700142
Gilles Debunne3661b622011-06-27 11:19:16 -0700143 if (!onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700144 highlightHeader(mTopLevelHeaderId);
Amith Yamasani72aa19d2010-12-09 06:07:12 -0800145 // Force the title so that it doesn't get overridden by a direct launch of
146 // a specific settings screen.
147 setTitle(R.string.settings_label);
148 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800149
150 // Retrieve any saved state
151 if (savedInstanceState != null) {
152 mCurrentHeader = savedInstanceState.getParcelable(SAVE_KEY_CURRENT_HEADER);
153 mParentHeader = savedInstanceState.getParcelable(SAVE_KEY_PARENT_HEADER);
154 }
155
156 // If the current header was saved, switch to it
157 if (savedInstanceState != null && mCurrentHeader != null) {
158 //switchToHeaderLocal(mCurrentHeader);
159 showBreadCrumbs(mCurrentHeader.title, null);
160 }
161
162 if (mParentHeader != null) {
163 setParentTitle(mParentHeader.title, null, new OnClickListener() {
164 public void onClick(View v) {
165 switchToParent(mParentHeader.fragment);
166 }
167 });
168 }
Gilles Debunnedc7101f2011-06-27 12:00:49 -0700169
Amith Yamasani3d384f42012-05-11 15:22:04 -0700170 // Override up navigation for multi-pane, since we handle it in the fragment breadcrumbs
171 if (onIsMultiPane()) {
172 getActionBar().setDisplayHomeAsUpEnabled(false);
173 getActionBar().setHomeButtonEnabled(false);
174 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800175 }
176
177 @Override
178 protected void onSaveInstanceState(Bundle outState) {
179 super.onSaveInstanceState(outState);
180
181 // Save the current fragment, if it is the same as originally launched
182 if (mCurrentHeader != null) {
183 outState.putParcelable(SAVE_KEY_CURRENT_HEADER, mCurrentHeader);
184 }
185 if (mParentHeader != null) {
186 outState.putParcelable(SAVE_KEY_PARENT_HEADER, mParentHeader);
187 }
188 }
189
Gilles Debunnee78c1872011-06-20 15:00:07 -0700190 @Override
191 public void onResume() {
192 super.onResume();
193
194 ListAdapter listAdapter = getListAdapter();
195 if (listAdapter instanceof HeaderAdapter) {
196 ((HeaderAdapter) listAdapter).resume();
197 }
Amith Yamasaniea7b28c2012-06-20 13:51:40 -0700198 invalidateHeaders();
Gilles Debunnee78c1872011-06-20 15:00:07 -0700199 }
200
201 @Override
202 public void onPause() {
203 super.onPause();
204
205 ListAdapter listAdapter = getListAdapter();
206 if (listAdapter instanceof HeaderAdapter) {
207 ((HeaderAdapter) listAdapter).pause();
Amith Yamasani86708a82012-06-06 20:23:08 -0700208 }
209 }
210
211 @Override
212 public void onDestroy() {
213 super.onDestroy();
214 if (mListeningToAccountUpdates) {
Amith Yamasani56821db2012-06-05 13:20:45 -0700215 AccountManager.get(this).removeOnAccountsUpdatedListener(this);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700216 }
217 }
218
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800219 private void switchToHeaderLocal(Header header) {
220 mInLocalHeaderSwitch = true;
221 switchToHeader(header);
222 mInLocalHeaderSwitch = false;
223 }
224
225 @Override
226 public void switchToHeader(Header header) {
227 if (!mInLocalHeaderSwitch) {
228 mCurrentHeader = null;
229 mParentHeader = null;
230 }
231 super.switchToHeader(header);
232 }
233
234 /**
235 * Switch to parent fragment and store the grand parent's info
Jake Hamby2748fc22011-01-12 15:06:28 -0800236 * @param className name of the activity wrapper for the parent fragment.
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800237 */
238 private void switchToParent(String className) {
239 final ComponentName cn = new ComponentName(this, className);
240 try {
241 final PackageManager pm = getPackageManager();
242 final ActivityInfo parentInfo = pm.getActivityInfo(cn, PackageManager.GET_META_DATA);
243
244 if (parentInfo != null && parentInfo.metaData != null) {
245 String fragmentClass = parentInfo.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
246 CharSequence fragmentTitle = parentInfo.loadLabel(pm);
247 Header parentHeader = new Header();
248 parentHeader.fragment = fragmentClass;
249 parentHeader.title = fragmentTitle;
250 mCurrentHeader = parentHeader;
251
252 switchToHeaderLocal(parentHeader);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700253 highlightHeader(mTopLevelHeaderId);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800254
255 mParentHeader = new Header();
256 mParentHeader.fragment
257 = parentInfo.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
258 mParentHeader.title = parentInfo.metaData.getString(META_DATA_KEY_PARENT_TITLE);
259 }
260 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700261 Log.w(LOG_TAG, "Could not find parent activity : " + className);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800262 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700263 }
264
Amith Yamasani3965ae62010-11-15 14:45:19 -0800265 @Override
266 public void onNewIntent(Intent intent) {
267 super.onNewIntent(intent);
268
269 // If it is not launched from history, then reset to top-level
270 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0
Gilles Debunne3661b622011-06-27 11:19:16 -0700271 && mFirstHeader != null && !onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800272 switchToHeaderLocal(mFirstHeader);
Amith Yamasani3965ae62010-11-15 14:45:19 -0800273 }
274 }
275
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700276 private void highlightHeader(int id) {
277 if (id != 0) {
278 Integer index = mHeaderIndexMap.get(id);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700279 if (index != null) {
280 getListView().setItemChecked(index, true);
Amith Yamasani990fb522011-09-02 09:47:18 -0700281 getListView().smoothScrollToPosition(index);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700282 }
283 }
284 }
285
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700286 @Override
287 public Intent getIntent() {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700288 Intent superIntent = super.getIntent();
289 String startingFragment = getStartingFragmentClass(superIntent);
Gilles Debunne3661b622011-06-27 11:19:16 -0700290 // This is called from super.onCreate, isMultiPane() is not yet reliable
291 // Do not use onIsHidingHeaders either, which relies itself on this method
292 if (startingFragment != null && !onIsMultiPane()) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700293 Intent modIntent = new Intent(superIntent);
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700294 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700295 Bundle args = superIntent.getExtras();
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700296 if (args != null) {
297 args = new Bundle(args);
298 } else {
299 args = new Bundle();
300 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700301 args.putParcelable("intent", superIntent);
302 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras());
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700303 return modIntent;
304 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700305 return superIntent;
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700306 }
307
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700308 /**
Amith Yamasani379d9b02010-09-27 12:03:59 -0700309 * Checks if the component name in the intent is different from the Settings class and
310 * returns the class name to load as a fragment.
311 */
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700312 protected String getStartingFragmentClass(Intent intent) {
313 if (mFragmentClass != null) return mFragmentClass;
314
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700315 String intentClass = intent.getComponent().getClassName();
Amith Yamasani379d9b02010-09-27 12:03:59 -0700316 if (intentClass.equals(getClass().getName())) return null;
317
Dianne Hackbornee293792010-11-01 12:32:33 -0700318 if ("com.android.settings.ManageApplications".equals(intentClass)
319 || "com.android.settings.RunningServices".equals(intentClass)
320 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700321 // Old names of manage apps.
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700322 intentClass = com.android.settings.applications.ManageApplications.class.getName();
323 }
324
Amith Yamasani379d9b02010-09-27 12:03:59 -0700325 return intentClass;
326 }
327
328 /**
329 * Override initial header when an activity-alias is causing Settings to be launched
330 * for a specific fragment encoded in the android:name parameter.
331 */
332 @Override
333 public Header onGetInitialHeader() {
334 String fragmentClass = getStartingFragmentClass(super.getIntent());
335 if (fragmentClass != null) {
336 Header header = new Header();
337 header.fragment = fragmentClass;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800338 header.title = getTitle();
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700339 header.fragmentArguments = getIntent().getExtras();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800340 mCurrentHeader = header;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700341 return header;
342 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700343
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700344 return mFirstHeader;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700345 }
346
Dianne Hackbornb7258182011-03-15 16:23:55 -0700347 @Override
Dianne Hackborn48147dc2011-03-18 12:29:41 -0700348 public Intent onBuildStartFragmentIntent(String fragmentName, Bundle args,
349 int titleRes, int shortTitleRes) {
350 Intent intent = super.onBuildStartFragmentIntent(fragmentName, args,
351 titleRes, shortTitleRes);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700352
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700353 // some fragments want to avoid split actionbar
Gilles Debunne162e5412011-07-11 14:13:31 -0700354 if (DataUsageSummary.class.getName().equals(fragmentName) ||
355 PowerUsageSummary.class.getName().equals(fragmentName) ||
Gilles Debunnecd8e5242011-07-25 11:36:15 -0700356 AccountSyncSettings.class.getName().equals(fragmentName) ||
Jeff Sharkey11d30122012-03-19 16:54:07 -0700357 UserDictionarySettings.class.getName().equals(fragmentName) ||
Adam Powellfaba7e42012-03-26 17:28:38 -0700358 Memory.class.getName().equals(fragmentName) ||
Jeff Sharkeya339cba2012-05-08 11:42:49 -0700359 ManageApplications.class.getName().equals(fragmentName) ||
360 WirelessSettings.class.getName().equals(fragmentName) ||
361 SoundSettings.class.getName().equals(fragmentName) ||
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700362 PrivacySettings.class.getName().equals(fragmentName) ||
Jeff Sharkey9fd7ac12012-08-25 00:06:08 -0700363 ManageAccountsSettings.class.getName().equals(fragmentName) ||
364 VpnSettings.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 Yamasanidc6bfa62012-09-06 18:04:06 -0700418 if (!UserHandle.MU_ENABLED
Jeff Sharkey44202462012-09-19 13:13:45 -0700419 || !UserManager.supportsMultipleUsers()
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700420 || Utils.isMonkeyRunning()) {
421 target.remove(header);
422 }
423 }
Dianne Hackbornbb06a422012-08-16 11:01:57 -0700424 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700425 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
426 target.remove(header);
Amith Yamasanid7993472010-08-18 13:59:28 -0700427 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700428
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700429 // Increment if the current one wasn't removed by the Utils code.
430 if (target.get(i) == header) {
Amith Yamasani3965ae62010-11-15 14:45:19 -0800431 // Hold on to the first header, when we need to reset to the top-level
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700432 if (mFirstHeader == null &&
433 HeaderAdapter.getHeaderType(header) != HeaderAdapter.HEADER_TYPE_CATEGORY) {
434 mFirstHeader = header;
435 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700436 mHeaderIndexMap.put(id, i);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700437 i++;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700438 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700439 }
440 }
441
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700442 private int insertAccountsHeaders(List<Header> target, int headerIndex) {
443 String[] accountTypes = mAuthenticatorHelper.getEnabledAccountTypes();
444 List<Header> accountHeaders = new ArrayList<Header>(accountTypes.length);
445 for (String accountType : accountTypes) {
446 CharSequence label = mAuthenticatorHelper.getLabelForType(this, accountType);
Brian Muramatsuc28af522012-06-28 14:25:14 -0700447 if (label == null) {
448 continue;
449 }
450
Amith Yamasani3882c2e2012-06-07 17:03:20 -0700451 Account[] accounts = AccountManager.get(this).getAccountsByType(accountType);
452 boolean skipToAccount = accounts.length == 1
453 && !mAuthenticatorHelper.hasAccountPreferences(accountType);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700454 Header accHeader = new Header();
455 accHeader.title = label;
456 if (accHeader.extras == null) {
457 accHeader.extras = new Bundle();
458 }
Amith Yamasani3882c2e2012-06-07 17:03:20 -0700459 if (skipToAccount) {
460 accHeader.breadCrumbTitleRes = R.string.account_sync_settings_title;
461 accHeader.breadCrumbShortTitleRes = R.string.account_sync_settings_title;
462 accHeader.fragment = AccountSyncSettings.class.getName();
463 accHeader.fragmentArguments = new Bundle();
464 // Need this for the icon
465 accHeader.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
466 accHeader.extras.putParcelable(AccountSyncSettings.ACCOUNT_KEY, accounts[0]);
467 accHeader.fragmentArguments.putParcelable(AccountSyncSettings.ACCOUNT_KEY,
468 accounts[0]);
469 } else {
470 accHeader.breadCrumbTitle = label;
471 accHeader.breadCrumbShortTitle = label;
472 accHeader.fragment = ManageAccountsSettings.class.getName();
473 accHeader.fragmentArguments = new Bundle();
474 accHeader.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
475 accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE,
476 accountType);
477 if (!isMultiPane()) {
478 accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_LABEL,
479 label.toString());
480 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700481 }
482 accountHeaders.add(accHeader);
483 }
484
485 // Sort by label
486 Collections.sort(accountHeaders, new Comparator<Header>() {
487 @Override
488 public int compare(Header h1, Header h2) {
489 return h1.title.toString().compareTo(h2.title.toString());
490 }
491 });
492
493 for (Header header : accountHeaders) {
494 target.add(headerIndex++, header);
495 }
Amith Yamasani86708a82012-06-06 20:23:08 -0700496 if (!mListeningToAccountUpdates) {
497 AccountManager.get(this).addOnAccountsUpdatedListener(this, null, true);
498 mListeningToAccountUpdates = true;
499 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700500 return headerIndex;
501 }
502
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700503 private boolean needsDockSettings() {
504 return getResources().getBoolean(R.bool.has_dock_settings);
Amith Yamasanib61cf512010-09-12 08:17:50 -0700505 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700506
507 private void getMetaData() {
508 try {
509 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
510 PackageManager.GET_META_DATA);
511 if (ai == null || ai.metaData == null) return;
512 mTopLevelHeaderId = ai.metaData.getInt(META_DATA_KEY_HEADER_ID);
513 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700514
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800515 // Check if it has a parent specified and create a Header object
516 final int parentHeaderTitleRes = ai.metaData.getInt(META_DATA_KEY_PARENT_TITLE);
517 String parentFragmentClass = ai.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
518 if (parentFragmentClass != null) {
519 mParentHeader = new Header();
520 mParentHeader.fragment = parentFragmentClass;
521 if (parentHeaderTitleRes != 0) {
522 mParentHeader.title = getResources().getString(parentHeaderTitleRes);
523 }
524 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700525 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700526 // No recovery
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700527 }
528 }
529
Amith Yamasani9e3a4702011-01-11 09:09:26 -0800530 @Override
531 public boolean hasNextButton() {
532 return super.hasNextButton();
533 }
534
535 @Override
536 public Button getNextButton() {
537 return super.getNextButton();
538 }
539
Gilles Debunnee78c1872011-06-20 15:00:07 -0700540 private static class HeaderAdapter extends ArrayAdapter<Header> {
541 static final int HEADER_TYPE_CATEGORY = 0;
542 static final int HEADER_TYPE_NORMAL = 1;
543 static final int HEADER_TYPE_SWITCH = 2;
544 private static final int HEADER_TYPE_COUNT = HEADER_TYPE_SWITCH + 1;
545
546 private final WifiEnabler mWifiEnabler;
547 private final BluetoothEnabler mBluetoothEnabler;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700548 private AuthenticatorHelper mAuthHelper;
Gilles Debunnee78c1872011-06-20 15:00:07 -0700549
550 private static class HeaderViewHolder {
551 ImageView icon;
552 TextView title;
553 TextView summary;
554 Switch switch_;
555 }
556
557 private LayoutInflater mInflater;
558
559 static int getHeaderType(Header header) {
560 if (header.fragment == null && header.intent == null) {
561 return HEADER_TYPE_CATEGORY;
562 } else if (header.id == R.id.wifi_settings || header.id == R.id.bluetooth_settings) {
563 return HEADER_TYPE_SWITCH;
564 } else {
565 return HEADER_TYPE_NORMAL;
566 }
567 }
568
569 @Override
570 public int getItemViewType(int position) {
571 Header header = getItem(position);
572 return getHeaderType(header);
573 }
574
575 @Override
576 public boolean areAllItemsEnabled() {
577 return false; // because of categories
578 }
579
580 @Override
581 public boolean isEnabled(int position) {
582 return getItemViewType(position) != HEADER_TYPE_CATEGORY;
583 }
584
585 @Override
586 public int getViewTypeCount() {
587 return HEADER_TYPE_COUNT;
588 }
589
590 @Override
591 public boolean hasStableIds() {
592 return true;
593 }
594
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700595 public HeaderAdapter(Context context, List<Header> objects,
596 AuthenticatorHelper authenticatorHelper) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700597 super(context, 0, objects);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700598
599 mAuthHelper = authenticatorHelper;
Gilles Debunnee78c1872011-06-20 15:00:07 -0700600 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700601
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700602 // Temp Switches provided as placeholder until the adapter replaces these with actual
Gilles Debunnee78c1872011-06-20 15:00:07 -0700603 // Switches inflated from their layouts. Must be done before adapter is set in super
604 mWifiEnabler = new WifiEnabler(context, new Switch(context));
605 mBluetoothEnabler = new BluetoothEnabler(context, new Switch(context));
606 }
607
608 @Override
609 public View getView(int position, View convertView, ViewGroup parent) {
610 HeaderViewHolder holder;
611 Header header = getItem(position);
612 int headerType = getHeaderType(header);
613 View view = null;
614
615 if (convertView == null) {
616 holder = new HeaderViewHolder();
617 switch (headerType) {
618 case HEADER_TYPE_CATEGORY:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700619 view = new TextView(getContext(), null,
620 android.R.attr.listSeparatorTextViewStyle);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700621 holder.title = (TextView) view;
622 break;
623
624 case HEADER_TYPE_SWITCH:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700625 view = mInflater.inflate(R.layout.preference_header_switch_item, parent,
626 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700627 holder.icon = (ImageView) view.findViewById(R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700628 holder.title = (TextView)
629 view.findViewById(com.android.internal.R.id.title);
630 holder.summary = (TextView)
631 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700632 holder.switch_ = (Switch) view.findViewById(R.id.switchWidget);
633 break;
634
635 case HEADER_TYPE_NORMAL:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700636 view = mInflater.inflate(
Amith Yamasanic8a93172012-06-08 13:35:47 -0700637 R.layout.preference_header_item, parent,
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700638 false);
Amith Yamasanic8a93172012-06-08 13:35:47 -0700639 holder.icon = (ImageView) view.findViewById(R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700640 holder.title = (TextView)
641 view.findViewById(com.android.internal.R.id.title);
642 holder.summary = (TextView)
643 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700644 break;
645 }
646 view.setTag(holder);
647 } else {
648 view = convertView;
649 holder = (HeaderViewHolder) view.getTag();
650 }
651
652 // All view fields must be updated every time, because the view may be recycled
653 switch (headerType) {
654 case HEADER_TYPE_CATEGORY:
655 holder.title.setText(header.getTitle(getContext().getResources()));
656 break;
657
658 case HEADER_TYPE_SWITCH:
659 // Would need a different treatment if the main menu had more switches
660 if (header.id == R.id.wifi_settings) {
661 mWifiEnabler.setSwitch(holder.switch_);
662 } else {
663 mBluetoothEnabler.setSwitch(holder.switch_);
664 }
665 // No break, fall through on purpose to update common fields
666
667 //$FALL-THROUGH$
668 case HEADER_TYPE_NORMAL:
Amith Yamasani3882c2e2012-06-07 17:03:20 -0700669 if (header.extras != null
670 && header.extras.containsKey(ManageAccountsSettings.KEY_ACCOUNT_TYPE)) {
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700671 String accType = header.extras.getString(
672 ManageAccountsSettings.KEY_ACCOUNT_TYPE);
673 ViewGroup.LayoutParams lp = holder.icon.getLayoutParams();
674 lp.width = getContext().getResources().getDimensionPixelSize(
675 R.dimen.header_icon_width);
676 lp.height = lp.width;
677 holder.icon.setLayoutParams(lp);
678 Drawable icon = mAuthHelper.getDrawableForType(getContext(), accType);
679 holder.icon.setImageDrawable(icon);
680 } else {
681 holder.icon.setImageResource(header.iconRes);
682 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700683 holder.title.setText(header.getTitle(getContext().getResources()));
684 CharSequence summary = header.getSummary(getContext().getResources());
685 if (!TextUtils.isEmpty(summary)) {
686 holder.summary.setVisibility(View.VISIBLE);
687 holder.summary.setText(summary);
688 } else {
689 holder.summary.setVisibility(View.GONE);
690 }
691 break;
692 }
693
694 return view;
695 }
696
697 public void resume() {
698 mWifiEnabler.resume();
699 mBluetoothEnabler.resume();
700 }
Brian Muramatsuc28af522012-06-28 14:25:14 -0700701
Gilles Debunnee78c1872011-06-20 15:00:07 -0700702 public void pause() {
703 mWifiEnabler.pause();
704 mBluetoothEnabler.pause();
705 }
706 }
707
708 @Override
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700709 public void onHeaderClick(Header header, int position) {
710 boolean revert = false;
711 if (header.id == R.id.account_add) {
712 revert = true;
713 }
714
715 super.onHeaderClick(header, position);
716
717 if (revert && mLastHeader != null) {
718 highlightHeader((int) mLastHeader.id);
719 } else {
720 mLastHeader = header;
721 }
722 }
723
724 @Override
Amith Yamasania4379d62011-07-22 10:34:58 -0700725 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
726 // Override the fragment title for Wallpaper settings
Amith Yamasanidfb65432011-11-29 16:38:14 -0800727 int titleRes = pref.getTitleRes();
Amith Yamasania4379d62011-07-22 10:34:58 -0700728 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
Amith Yamasanidfb65432011-11-29 16:38:14 -0800729 titleRes = R.string.wallpaper_settings_fragment_title;
Amith Yamasania4379d62011-07-22 10:34:58 -0700730 }
Jean Chalard7dabe452012-05-10 18:08:07 +0900731 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
732 null, 0);
Amith Yamasania4379d62011-07-22 10:34:58 -0700733 return true;
734 }
735
Amith Yamasani3d384f42012-05-11 15:22:04 -0700736 public boolean shouldUpRecreateTask(Intent targetIntent) {
737 return super.shouldUpRecreateTask(new Intent(this, Settings.class));
738 }
739
Amith Yamasania4379d62011-07-22 10:34:58 -0700740 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700741 public void setListAdapter(ListAdapter adapter) {
Amith Yamasaniea7b28c2012-06-20 13:51:40 -0700742 if (adapter == null) {
743 super.setListAdapter(null);
744 } else {
745 super.setListAdapter(new HeaderAdapter(this, getHeaders(), mAuthenticatorHelper));
Gilles Debunnee78c1872011-06-20 15:00:07 -0700746 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700747 }
748
Amith Yamasani56821db2012-06-05 13:20:45 -0700749 @Override
750 public void onAccountsUpdated(Account[] accounts) {
751 mAuthenticatorHelper.onAccountsUpdated(this, accounts);
752 invalidateHeaders();
753 }
754
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700755 /*
756 * Settings subclasses for launching independently.
757 */
Gilles Debunnee78c1872011-06-20 15:00:07 -0700758 public static class BluetoothSettingsActivity extends Settings { /* empty */ }
759 public static class WirelessSettingsActivity extends Settings { /* empty */ }
760 public static class TetherSettingsActivity extends Settings { /* empty */ }
761 public static class VpnSettingsActivity extends Settings { /* empty */ }
762 public static class DateTimeSettingsActivity extends Settings { /* empty */ }
763 public static class StorageSettingsActivity extends Settings { /* empty */ }
764 public static class WifiSettingsActivity extends Settings { /* empty */ }
repo syncb98463f2011-06-30 10:58:43 -0700765 public static class WifiP2pSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700766 public static class InputMethodAndLanguageSettingsActivity extends Settings { /* empty */ }
Jeff Browne46c5f32012-04-05 11:42:18 -0700767 public static class KeyboardLayoutPickerActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700768 public static class InputMethodAndSubtypeEnablerActivity extends Settings { /* empty */ }
satoke077d2b2011-07-25 09:38:11 +0900769 public static class SpellCheckersSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700770 public static class LocalePickerActivity extends Settings { /* empty */ }
771 public static class UserDictionarySettingsActivity extends Settings { /* empty */ }
772 public static class SoundSettingsActivity extends Settings { /* empty */ }
773 public static class DisplaySettingsActivity extends Settings { /* empty */ }
774 public static class DeviceInfoSettingsActivity extends Settings { /* empty */ }
775 public static class ApplicationSettingsActivity extends Settings { /* empty */ }
776 public static class ManageApplicationsActivity extends Settings { /* empty */ }
777 public static class StorageUseActivity extends Settings { /* empty */ }
778 public static class DevelopmentSettingsActivity extends Settings { /* empty */ }
779 public static class AccessibilitySettingsActivity extends Settings { /* empty */ }
780 public static class SecuritySettingsActivity extends Settings { /* empty */ }
Gilles Debunnea6a8a142011-06-09 11:56:17 -0700781 public static class LocationSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700782 public static class PrivacySettingsActivity extends Settings { /* empty */ }
783 public static class DockSettingsActivity extends Settings { /* empty */ }
784 public static class RunningServicesActivity extends Settings { /* empty */ }
785 public static class ManageAccountsSettingsActivity extends Settings { /* empty */ }
786 public static class PowerUsageSummaryActivity extends Settings { /* empty */ }
787 public static class AccountSyncSettingsActivity extends Settings { /* empty */ }
788 public static class AccountSyncSettingsInAddAccountActivity extends Settings { /* empty */ }
789 public static class CryptKeeperSettingsActivity extends Settings { /* empty */ }
790 public static class DeviceAdminSettingsActivity extends Settings { /* empty */ }
791 public static class DataUsageSummaryActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700792 public static class AdvancedWifiSettingsActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700793 public static class TextToSpeechSettingsActivity extends Settings { /* empty */ }
Jeff Hamilton3d670de2011-09-21 16:44:36 -0500794 public static class AndroidBeamSettingsActivity extends Settings { /* empty */ }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800795}