blob: 6f2002a9c3bbaab9c7e948002b982fcd4b5adae2 [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,
Amith Yamasani32630392012-08-10 19:31:39 -0700108 R.id.personal_section,
Amith Yamasani3deeeb72012-04-05 14:44:48 -0700109 R.id.security_settings,
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700110 R.id.user_settings,
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700111 R.id.account_settings,
Amith Yamasani32630392012-08-10 19:31:39 -0700112 R.id.account_add,
113 R.id.system_section,
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700114 R.id.about_settings
115 };
116
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700117 // TODO: Update Call Settings based on airplane mode state.
Amith Yamasanib61cf512010-09-12 08:17:50 -0700118
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700119 protected HashMap<Integer, Integer> mHeaderIndexMap = new HashMap<Integer, Integer>();
120
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700121 private AuthenticatorHelper mAuthenticatorHelper;
122 private Header mLastHeader;
Amith Yamasani86708a82012-06-06 20:23:08 -0700123 private boolean mListeningToAccountUpdates;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700124
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700125 @Override
126 protected void onCreate(Bundle savedInstanceState) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700127 if (getIntent().getBooleanExtra(EXTRA_CLEAR_UI_OPTIONS, false)) {
128 getWindow().setUiOptions(0);
129 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700130
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700131 mAuthenticatorHelper = new AuthenticatorHelper();
132 mAuthenticatorHelper.updateAuthDescriptions(this);
133 mAuthenticatorHelper.onAccountsUpdated(this, null);
134
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700135 getMetaData();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800136 mInLocalHeaderSwitch = true;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700137 super.onCreate(savedInstanceState);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800138 mInLocalHeaderSwitch = false;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700139
Gilles Debunne3661b622011-06-27 11:19:16 -0700140 if (!onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700141 highlightHeader(mTopLevelHeaderId);
Amith Yamasani72aa19d2010-12-09 06:07:12 -0800142 // Force the title so that it doesn't get overridden by a direct launch of
143 // a specific settings screen.
144 setTitle(R.string.settings_label);
145 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800146
147 // Retrieve any saved state
148 if (savedInstanceState != null) {
149 mCurrentHeader = savedInstanceState.getParcelable(SAVE_KEY_CURRENT_HEADER);
150 mParentHeader = savedInstanceState.getParcelable(SAVE_KEY_PARENT_HEADER);
151 }
152
153 // If the current header was saved, switch to it
154 if (savedInstanceState != null && mCurrentHeader != null) {
155 //switchToHeaderLocal(mCurrentHeader);
156 showBreadCrumbs(mCurrentHeader.title, null);
157 }
158
159 if (mParentHeader != null) {
160 setParentTitle(mParentHeader.title, null, new OnClickListener() {
161 public void onClick(View v) {
162 switchToParent(mParentHeader.fragment);
163 }
164 });
165 }
Gilles Debunnedc7101f2011-06-27 12:00:49 -0700166
Amith Yamasani3d384f42012-05-11 15:22:04 -0700167 // Override up navigation for multi-pane, since we handle it in the fragment breadcrumbs
168 if (onIsMultiPane()) {
169 getActionBar().setDisplayHomeAsUpEnabled(false);
170 getActionBar().setHomeButtonEnabled(false);
171 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800172 }
173
174 @Override
175 protected void onSaveInstanceState(Bundle outState) {
176 super.onSaveInstanceState(outState);
177
178 // Save the current fragment, if it is the same as originally launched
179 if (mCurrentHeader != null) {
180 outState.putParcelable(SAVE_KEY_CURRENT_HEADER, mCurrentHeader);
181 }
182 if (mParentHeader != null) {
183 outState.putParcelable(SAVE_KEY_PARENT_HEADER, mParentHeader);
184 }
185 }
186
Gilles Debunnee78c1872011-06-20 15:00:07 -0700187 @Override
188 public void onResume() {
189 super.onResume();
190
191 ListAdapter listAdapter = getListAdapter();
192 if (listAdapter instanceof HeaderAdapter) {
193 ((HeaderAdapter) listAdapter).resume();
194 }
Amith Yamasaniea7b28c2012-06-20 13:51:40 -0700195 invalidateHeaders();
Gilles Debunnee78c1872011-06-20 15:00:07 -0700196 }
197
198 @Override
199 public void onPause() {
200 super.onPause();
201
202 ListAdapter listAdapter = getListAdapter();
203 if (listAdapter instanceof HeaderAdapter) {
204 ((HeaderAdapter) listAdapter).pause();
Amith Yamasani86708a82012-06-06 20:23:08 -0700205 }
206 }
207
208 @Override
209 public void onDestroy() {
210 super.onDestroy();
211 if (mListeningToAccountUpdates) {
Amith Yamasani56821db2012-06-05 13:20:45 -0700212 AccountManager.get(this).removeOnAccountsUpdatedListener(this);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700213 }
214 }
215
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800216 private void switchToHeaderLocal(Header header) {
217 mInLocalHeaderSwitch = true;
218 switchToHeader(header);
219 mInLocalHeaderSwitch = false;
220 }
221
222 @Override
223 public void switchToHeader(Header header) {
224 if (!mInLocalHeaderSwitch) {
225 mCurrentHeader = null;
226 mParentHeader = null;
227 }
228 super.switchToHeader(header);
229 }
230
231 /**
232 * Switch to parent fragment and store the grand parent's info
Jake Hamby2748fc22011-01-12 15:06:28 -0800233 * @param className name of the activity wrapper for the parent fragment.
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800234 */
235 private void switchToParent(String className) {
236 final ComponentName cn = new ComponentName(this, className);
237 try {
238 final PackageManager pm = getPackageManager();
239 final ActivityInfo parentInfo = pm.getActivityInfo(cn, PackageManager.GET_META_DATA);
240
241 if (parentInfo != null && parentInfo.metaData != null) {
242 String fragmentClass = parentInfo.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
243 CharSequence fragmentTitle = parentInfo.loadLabel(pm);
244 Header parentHeader = new Header();
245 parentHeader.fragment = fragmentClass;
246 parentHeader.title = fragmentTitle;
247 mCurrentHeader = parentHeader;
248
249 switchToHeaderLocal(parentHeader);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700250 highlightHeader(mTopLevelHeaderId);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800251
252 mParentHeader = new Header();
253 mParentHeader.fragment
254 = parentInfo.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
255 mParentHeader.title = parentInfo.metaData.getString(META_DATA_KEY_PARENT_TITLE);
256 }
257 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700258 Log.w(LOG_TAG, "Could not find parent activity : " + className);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800259 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700260 }
261
Amith Yamasani3965ae62010-11-15 14:45:19 -0800262 @Override
263 public void onNewIntent(Intent intent) {
264 super.onNewIntent(intent);
265
266 // If it is not launched from history, then reset to top-level
267 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0
Gilles Debunne3661b622011-06-27 11:19:16 -0700268 && mFirstHeader != null && !onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800269 switchToHeaderLocal(mFirstHeader);
Amith Yamasani3965ae62010-11-15 14:45:19 -0800270 }
271 }
272
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700273 private void highlightHeader(int id) {
274 if (id != 0) {
275 Integer index = mHeaderIndexMap.get(id);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700276 if (index != null) {
277 getListView().setItemChecked(index, true);
Amith Yamasani990fb522011-09-02 09:47:18 -0700278 getListView().smoothScrollToPosition(index);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700279 }
280 }
281 }
282
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700283 @Override
284 public Intent getIntent() {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700285 Intent superIntent = super.getIntent();
286 String startingFragment = getStartingFragmentClass(superIntent);
Gilles Debunne3661b622011-06-27 11:19:16 -0700287 // This is called from super.onCreate, isMultiPane() is not yet reliable
288 // Do not use onIsHidingHeaders either, which relies itself on this method
289 if (startingFragment != null && !onIsMultiPane()) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700290 Intent modIntent = new Intent(superIntent);
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700291 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700292 Bundle args = superIntent.getExtras();
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700293 if (args != null) {
294 args = new Bundle(args);
295 } else {
296 args = new Bundle();
297 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700298 args.putParcelable("intent", superIntent);
299 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras());
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700300 return modIntent;
301 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700302 return superIntent;
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700303 }
304
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700305 /**
Amith Yamasani379d9b02010-09-27 12:03:59 -0700306 * Checks if the component name in the intent is different from the Settings class and
307 * returns the class name to load as a fragment.
308 */
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700309 protected String getStartingFragmentClass(Intent intent) {
310 if (mFragmentClass != null) return mFragmentClass;
311
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700312 String intentClass = intent.getComponent().getClassName();
Amith Yamasani379d9b02010-09-27 12:03:59 -0700313 if (intentClass.equals(getClass().getName())) return null;
314
Dianne Hackbornee293792010-11-01 12:32:33 -0700315 if ("com.android.settings.ManageApplications".equals(intentClass)
316 || "com.android.settings.RunningServices".equals(intentClass)
317 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700318 // Old names of manage apps.
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700319 intentClass = com.android.settings.applications.ManageApplications.class.getName();
320 }
321
Amith Yamasani379d9b02010-09-27 12:03:59 -0700322 return intentClass;
323 }
324
325 /**
326 * Override initial header when an activity-alias is causing Settings to be launched
327 * for a specific fragment encoded in the android:name parameter.
328 */
329 @Override
330 public Header onGetInitialHeader() {
331 String fragmentClass = getStartingFragmentClass(super.getIntent());
332 if (fragmentClass != null) {
333 Header header = new Header();
334 header.fragment = fragmentClass;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800335 header.title = getTitle();
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700336 header.fragmentArguments = getIntent().getExtras();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800337 mCurrentHeader = header;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700338 return header;
339 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700340
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700341 return mFirstHeader;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700342 }
343
Dianne Hackbornb7258182011-03-15 16:23:55 -0700344 @Override
Dianne Hackborn48147dc2011-03-18 12:29:41 -0700345 public Intent onBuildStartFragmentIntent(String fragmentName, Bundle args,
346 int titleRes, int shortTitleRes) {
347 Intent intent = super.onBuildStartFragmentIntent(fragmentName, args,
348 titleRes, shortTitleRes);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700349
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700350 // some fragments want to avoid split actionbar
Gilles Debunne162e5412011-07-11 14:13:31 -0700351 if (DataUsageSummary.class.getName().equals(fragmentName) ||
352 PowerUsageSummary.class.getName().equals(fragmentName) ||
Gilles Debunnecd8e5242011-07-25 11:36:15 -0700353 AccountSyncSettings.class.getName().equals(fragmentName) ||
Jeff Sharkey11d30122012-03-19 16:54:07 -0700354 UserDictionarySettings.class.getName().equals(fragmentName) ||
Adam Powellfaba7e42012-03-26 17:28:38 -0700355 Memory.class.getName().equals(fragmentName) ||
Jeff Sharkeya339cba2012-05-08 11:42:49 -0700356 ManageApplications.class.getName().equals(fragmentName) ||
357 WirelessSettings.class.getName().equals(fragmentName) ||
358 SoundSettings.class.getName().equals(fragmentName) ||
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700359 PrivacySettings.class.getName().equals(fragmentName) ||
Jeff Sharkey9fd7ac12012-08-25 00:06:08 -0700360 ManageAccountsSettings.class.getName().equals(fragmentName) ||
361 VpnSettings.class.getName().equals(fragmentName)) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700362 intent.putExtra(EXTRA_CLEAR_UI_OPTIONS, true);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700363 }
364
Dianne Hackbornb7258182011-03-15 16:23:55 -0700365 intent.setClass(this, SubSettings.class);
366 return intent;
367 }
Amith Yamasaniea7b28c2012-06-20 13:51:40 -0700368
Amith Yamasani379d9b02010-09-27 12:03:59 -0700369 /**
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700370 * Populate the activity with the top-level headers.
371 */
Amith Yamasanid7993472010-08-18 13:59:28 -0700372 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700373 public void onBuildHeaders(List<Header> headers) {
374 loadHeadersFromResource(R.xml.settings_headers, headers);
Amith Yamasanid7993472010-08-18 13:59:28 -0700375
Gilles Debunnee78c1872011-06-20 15:00:07 -0700376 updateHeaderList(headers);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700377 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700378
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700379 private void updateHeaderList(List<Header> target) {
380 int i = 0;
381 while (i < target.size()) {
382 Header header = target.get(i);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700383 // Ids are integers, so downcasting
384 int id = (int) header.id;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700385 if (id == R.id.dock_settings) {
386 if (!needsDockSettings())
387 target.remove(header);
388 } else if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
389 Utils.updateHeaderToSpecificActivityFromMetaDataOrRemove(this, target, header);
Gilles Debunne2454f492011-06-21 16:16:06 -0700390 } else if (id == R.id.wifi_settings) {
391 // Remove WiFi Settings if WiFi service is not available.
392 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
393 target.remove(header);
394 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700395 } else if (id == R.id.bluetooth_settings) {
396 // Remove Bluetooth Settings if Bluetooth service is not available.
Gilles Debunne2454f492011-06-21 16:16:06 -0700397 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700398 target.remove(header);
399 }
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700400 } else if (id == R.id.data_usage_settings) {
401 // Remove data usage when kernel module not enabled
402 final INetworkManagementService netManager = INetworkManagementService.Stub
403 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
404 try {
405 if (!netManager.isBandwidthControlEnabled()) {
406 target.remove(header);
407 }
408 } catch (RemoteException e) {
409 // ignored
410 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700411 } else if (id == R.id.account_settings) {
412 int headerIndex = i + 1;
413 i = insertAccountsHeaders(target, headerIndex);
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700414 } else if (id == R.id.user_settings) {
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700415 if (!UserHandle.MU_ENABLED
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700416 || !getResources().getBoolean(R.bool.enable_user_management)
417 || Utils.isMonkeyRunning()) {
418 target.remove(header);
419 }
420 }
Dianne Hackbornbb06a422012-08-16 11:01:57 -0700421 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700422 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
423 target.remove(header);
Amith Yamasanid7993472010-08-18 13:59:28 -0700424 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700425
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700426 // Increment if the current one wasn't removed by the Utils code.
427 if (target.get(i) == header) {
Amith Yamasani3965ae62010-11-15 14:45:19 -0800428 // Hold on to the first header, when we need to reset to the top-level
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700429 if (mFirstHeader == null &&
430 HeaderAdapter.getHeaderType(header) != HeaderAdapter.HEADER_TYPE_CATEGORY) {
431 mFirstHeader = header;
432 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700433 mHeaderIndexMap.put(id, i);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700434 i++;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700435 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700436 }
437 }
438
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700439 private int insertAccountsHeaders(List<Header> target, int headerIndex) {
440 String[] accountTypes = mAuthenticatorHelper.getEnabledAccountTypes();
441 List<Header> accountHeaders = new ArrayList<Header>(accountTypes.length);
442 for (String accountType : accountTypes) {
443 CharSequence label = mAuthenticatorHelper.getLabelForType(this, accountType);
Brian Muramatsuc28af522012-06-28 14:25:14 -0700444 if (label == null) {
445 continue;
446 }
447
Amith Yamasani3882c2e2012-06-07 17:03:20 -0700448 Account[] accounts = AccountManager.get(this).getAccountsByType(accountType);
449 boolean skipToAccount = accounts.length == 1
450 && !mAuthenticatorHelper.hasAccountPreferences(accountType);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700451 Header accHeader = new Header();
452 accHeader.title = label;
453 if (accHeader.extras == null) {
454 accHeader.extras = new Bundle();
455 }
Amith Yamasani3882c2e2012-06-07 17:03:20 -0700456 if (skipToAccount) {
457 accHeader.breadCrumbTitleRes = R.string.account_sync_settings_title;
458 accHeader.breadCrumbShortTitleRes = R.string.account_sync_settings_title;
459 accHeader.fragment = AccountSyncSettings.class.getName();
460 accHeader.fragmentArguments = new Bundle();
461 // Need this for the icon
462 accHeader.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
463 accHeader.extras.putParcelable(AccountSyncSettings.ACCOUNT_KEY, accounts[0]);
464 accHeader.fragmentArguments.putParcelable(AccountSyncSettings.ACCOUNT_KEY,
465 accounts[0]);
466 } else {
467 accHeader.breadCrumbTitle = label;
468 accHeader.breadCrumbShortTitle = label;
469 accHeader.fragment = ManageAccountsSettings.class.getName();
470 accHeader.fragmentArguments = new Bundle();
471 accHeader.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
472 accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE,
473 accountType);
474 if (!isMultiPane()) {
475 accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_LABEL,
476 label.toString());
477 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700478 }
479 accountHeaders.add(accHeader);
480 }
481
482 // Sort by label
483 Collections.sort(accountHeaders, new Comparator<Header>() {
484 @Override
485 public int compare(Header h1, Header h2) {
486 return h1.title.toString().compareTo(h2.title.toString());
487 }
488 });
489
490 for (Header header : accountHeaders) {
491 target.add(headerIndex++, header);
492 }
Amith Yamasani86708a82012-06-06 20:23:08 -0700493 if (!mListeningToAccountUpdates) {
494 AccountManager.get(this).addOnAccountsUpdatedListener(this, null, true);
495 mListeningToAccountUpdates = true;
496 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700497 return headerIndex;
498 }
499
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700500 private boolean needsDockSettings() {
501 return getResources().getBoolean(R.bool.has_dock_settings);
Amith Yamasanib61cf512010-09-12 08:17:50 -0700502 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700503
504 private void getMetaData() {
505 try {
506 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
507 PackageManager.GET_META_DATA);
508 if (ai == null || ai.metaData == null) return;
509 mTopLevelHeaderId = ai.metaData.getInt(META_DATA_KEY_HEADER_ID);
510 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700511
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800512 // Check if it has a parent specified and create a Header object
513 final int parentHeaderTitleRes = ai.metaData.getInt(META_DATA_KEY_PARENT_TITLE);
514 String parentFragmentClass = ai.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
515 if (parentFragmentClass != null) {
516 mParentHeader = new Header();
517 mParentHeader.fragment = parentFragmentClass;
518 if (parentHeaderTitleRes != 0) {
519 mParentHeader.title = getResources().getString(parentHeaderTitleRes);
520 }
521 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700522 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700523 // No recovery
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700524 }
525 }
526
Amith Yamasani9e3a4702011-01-11 09:09:26 -0800527 @Override
528 public boolean hasNextButton() {
529 return super.hasNextButton();
530 }
531
532 @Override
533 public Button getNextButton() {
534 return super.getNextButton();
535 }
536
Gilles Debunnee78c1872011-06-20 15:00:07 -0700537 private static class HeaderAdapter extends ArrayAdapter<Header> {
538 static final int HEADER_TYPE_CATEGORY = 0;
539 static final int HEADER_TYPE_NORMAL = 1;
540 static final int HEADER_TYPE_SWITCH = 2;
541 private static final int HEADER_TYPE_COUNT = HEADER_TYPE_SWITCH + 1;
542
543 private final WifiEnabler mWifiEnabler;
544 private final BluetoothEnabler mBluetoothEnabler;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700545 private AuthenticatorHelper mAuthHelper;
Gilles Debunnee78c1872011-06-20 15:00:07 -0700546
547 private static class HeaderViewHolder {
548 ImageView icon;
549 TextView title;
550 TextView summary;
551 Switch switch_;
552 }
553
554 private LayoutInflater mInflater;
555
556 static int getHeaderType(Header header) {
557 if (header.fragment == null && header.intent == null) {
558 return HEADER_TYPE_CATEGORY;
559 } else if (header.id == R.id.wifi_settings || header.id == R.id.bluetooth_settings) {
560 return HEADER_TYPE_SWITCH;
561 } else {
562 return HEADER_TYPE_NORMAL;
563 }
564 }
565
566 @Override
567 public int getItemViewType(int position) {
568 Header header = getItem(position);
569 return getHeaderType(header);
570 }
571
572 @Override
573 public boolean areAllItemsEnabled() {
574 return false; // because of categories
575 }
576
577 @Override
578 public boolean isEnabled(int position) {
579 return getItemViewType(position) != HEADER_TYPE_CATEGORY;
580 }
581
582 @Override
583 public int getViewTypeCount() {
584 return HEADER_TYPE_COUNT;
585 }
586
587 @Override
588 public boolean hasStableIds() {
589 return true;
590 }
591
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700592 public HeaderAdapter(Context context, List<Header> objects,
593 AuthenticatorHelper authenticatorHelper) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700594 super(context, 0, objects);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700595
596 mAuthHelper = authenticatorHelper;
Gilles Debunnee78c1872011-06-20 15:00:07 -0700597 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700598
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700599 // Temp Switches provided as placeholder until the adapter replaces these with actual
Gilles Debunnee78c1872011-06-20 15:00:07 -0700600 // Switches inflated from their layouts. Must be done before adapter is set in super
601 mWifiEnabler = new WifiEnabler(context, new Switch(context));
602 mBluetoothEnabler = new BluetoothEnabler(context, new Switch(context));
603 }
604
605 @Override
606 public View getView(int position, View convertView, ViewGroup parent) {
607 HeaderViewHolder holder;
608 Header header = getItem(position);
609 int headerType = getHeaderType(header);
610 View view = null;
611
612 if (convertView == null) {
613 holder = new HeaderViewHolder();
614 switch (headerType) {
615 case HEADER_TYPE_CATEGORY:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700616 view = new TextView(getContext(), null,
617 android.R.attr.listSeparatorTextViewStyle);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700618 holder.title = (TextView) view;
619 break;
620
621 case HEADER_TYPE_SWITCH:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700622 view = mInflater.inflate(R.layout.preference_header_switch_item, parent,
623 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700624 holder.icon = (ImageView) view.findViewById(R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700625 holder.title = (TextView)
626 view.findViewById(com.android.internal.R.id.title);
627 holder.summary = (TextView)
628 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700629 holder.switch_ = (Switch) view.findViewById(R.id.switchWidget);
630 break;
631
632 case HEADER_TYPE_NORMAL:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700633 view = mInflater.inflate(
Amith Yamasanic8a93172012-06-08 13:35:47 -0700634 R.layout.preference_header_item, parent,
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700635 false);
Amith Yamasanic8a93172012-06-08 13:35:47 -0700636 holder.icon = (ImageView) view.findViewById(R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700637 holder.title = (TextView)
638 view.findViewById(com.android.internal.R.id.title);
639 holder.summary = (TextView)
640 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700641 break;
642 }
643 view.setTag(holder);
644 } else {
645 view = convertView;
646 holder = (HeaderViewHolder) view.getTag();
647 }
648
649 // All view fields must be updated every time, because the view may be recycled
650 switch (headerType) {
651 case HEADER_TYPE_CATEGORY:
652 holder.title.setText(header.getTitle(getContext().getResources()));
653 break;
654
655 case HEADER_TYPE_SWITCH:
656 // Would need a different treatment if the main menu had more switches
657 if (header.id == R.id.wifi_settings) {
658 mWifiEnabler.setSwitch(holder.switch_);
659 } else {
660 mBluetoothEnabler.setSwitch(holder.switch_);
661 }
662 // No break, fall through on purpose to update common fields
663
664 //$FALL-THROUGH$
665 case HEADER_TYPE_NORMAL:
Amith Yamasani3882c2e2012-06-07 17:03:20 -0700666 if (header.extras != null
667 && header.extras.containsKey(ManageAccountsSettings.KEY_ACCOUNT_TYPE)) {
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700668 String accType = header.extras.getString(
669 ManageAccountsSettings.KEY_ACCOUNT_TYPE);
670 ViewGroup.LayoutParams lp = holder.icon.getLayoutParams();
671 lp.width = getContext().getResources().getDimensionPixelSize(
672 R.dimen.header_icon_width);
673 lp.height = lp.width;
674 holder.icon.setLayoutParams(lp);
675 Drawable icon = mAuthHelper.getDrawableForType(getContext(), accType);
676 holder.icon.setImageDrawable(icon);
677 } else {
678 holder.icon.setImageResource(header.iconRes);
679 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700680 holder.title.setText(header.getTitle(getContext().getResources()));
681 CharSequence summary = header.getSummary(getContext().getResources());
682 if (!TextUtils.isEmpty(summary)) {
683 holder.summary.setVisibility(View.VISIBLE);
684 holder.summary.setText(summary);
685 } else {
686 holder.summary.setVisibility(View.GONE);
687 }
688 break;
689 }
690
691 return view;
692 }
693
694 public void resume() {
695 mWifiEnabler.resume();
696 mBluetoothEnabler.resume();
697 }
Brian Muramatsuc28af522012-06-28 14:25:14 -0700698
Gilles Debunnee78c1872011-06-20 15:00:07 -0700699 public void pause() {
700 mWifiEnabler.pause();
701 mBluetoothEnabler.pause();
702 }
703 }
704
705 @Override
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700706 public void onHeaderClick(Header header, int position) {
707 boolean revert = false;
708 if (header.id == R.id.account_add) {
709 revert = true;
710 }
711
712 super.onHeaderClick(header, position);
713
714 if (revert && mLastHeader != null) {
715 highlightHeader((int) mLastHeader.id);
716 } else {
717 mLastHeader = header;
718 }
719 }
720
721 @Override
Amith Yamasania4379d62011-07-22 10:34:58 -0700722 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
723 // Override the fragment title for Wallpaper settings
Amith Yamasanidfb65432011-11-29 16:38:14 -0800724 int titleRes = pref.getTitleRes();
Amith Yamasania4379d62011-07-22 10:34:58 -0700725 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
Amith Yamasanidfb65432011-11-29 16:38:14 -0800726 titleRes = R.string.wallpaper_settings_fragment_title;
Amith Yamasania4379d62011-07-22 10:34:58 -0700727 }
Jean Chalard7dabe452012-05-10 18:08:07 +0900728 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
729 null, 0);
Amith Yamasania4379d62011-07-22 10:34:58 -0700730 return true;
731 }
732
Amith Yamasani3d384f42012-05-11 15:22:04 -0700733 public boolean shouldUpRecreateTask(Intent targetIntent) {
734 return super.shouldUpRecreateTask(new Intent(this, Settings.class));
735 }
736
Amith Yamasania4379d62011-07-22 10:34:58 -0700737 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700738 public void setListAdapter(ListAdapter adapter) {
Amith Yamasaniea7b28c2012-06-20 13:51:40 -0700739 if (adapter == null) {
740 super.setListAdapter(null);
741 } else {
742 super.setListAdapter(new HeaderAdapter(this, getHeaders(), mAuthenticatorHelper));
Gilles Debunnee78c1872011-06-20 15:00:07 -0700743 }
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}