blob: bf31386b47e2d0f9be1b44c608df198ebbc4b148 [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,
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700103 R.id.data_usage_settings,
Amith Yamasani32630392012-08-10 19:31:39 -0700104 R.id.device_section,
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700105 R.id.sound_settings,
106 R.id.display_settings,
Jeff Sharkey90c8b202012-08-30 15:20:10 -0700107 R.id.storage_settings,
Dianne Hackborn271c8b02012-08-20 17:24:39 -0700108 R.id.application_settings,
Dianne Hackborn52e56a22012-09-12 17:02:23 -0700109 R.id.battery_settings,
Amith Yamasani32630392012-08-10 19:31:39 -0700110 R.id.personal_section,
Amith Yamasani3deeeb72012-04-05 14:44:48 -0700111 R.id.security_settings,
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700112 R.id.user_settings,
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700113 R.id.account_settings,
Amith Yamasani32630392012-08-10 19:31:39 -0700114 R.id.account_add,
115 R.id.system_section,
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700116 R.id.about_settings
117 };
118
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700119 // TODO: Update Call Settings based on airplane mode state.
Amith Yamasanib61cf512010-09-12 08:17:50 -0700120
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700121 protected HashMap<Integer, Integer> mHeaderIndexMap = new HashMap<Integer, Integer>();
122
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700123 private AuthenticatorHelper mAuthenticatorHelper;
124 private Header mLastHeader;
Amith Yamasani86708a82012-06-06 20:23:08 -0700125 private boolean mListeningToAccountUpdates;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700126
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700127 @Override
128 protected void onCreate(Bundle savedInstanceState) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700129 if (getIntent().getBooleanExtra(EXTRA_CLEAR_UI_OPTIONS, false)) {
130 getWindow().setUiOptions(0);
131 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700132
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700133 mAuthenticatorHelper = new AuthenticatorHelper();
134 mAuthenticatorHelper.updateAuthDescriptions(this);
135 mAuthenticatorHelper.onAccountsUpdated(this, null);
136
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700137 getMetaData();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800138 mInLocalHeaderSwitch = true;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700139 super.onCreate(savedInstanceState);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800140 mInLocalHeaderSwitch = false;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700141
Gilles Debunne3661b622011-06-27 11:19:16 -0700142 if (!onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700143 highlightHeader(mTopLevelHeaderId);
Amith Yamasani72aa19d2010-12-09 06:07:12 -0800144 // Force the title so that it doesn't get overridden by a direct launch of
145 // a specific settings screen.
146 setTitle(R.string.settings_label);
147 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800148
149 // Retrieve any saved state
150 if (savedInstanceState != null) {
151 mCurrentHeader = savedInstanceState.getParcelable(SAVE_KEY_CURRENT_HEADER);
152 mParentHeader = savedInstanceState.getParcelable(SAVE_KEY_PARENT_HEADER);
153 }
154
155 // If the current header was saved, switch to it
156 if (savedInstanceState != null && mCurrentHeader != null) {
157 //switchToHeaderLocal(mCurrentHeader);
158 showBreadCrumbs(mCurrentHeader.title, null);
159 }
160
161 if (mParentHeader != null) {
162 setParentTitle(mParentHeader.title, null, new OnClickListener() {
163 public void onClick(View v) {
164 switchToParent(mParentHeader.fragment);
165 }
166 });
167 }
Gilles Debunnedc7101f2011-06-27 12:00:49 -0700168
Amith Yamasani3d384f42012-05-11 15:22:04 -0700169 // Override up navigation for multi-pane, since we handle it in the fragment breadcrumbs
170 if (onIsMultiPane()) {
171 getActionBar().setDisplayHomeAsUpEnabled(false);
172 getActionBar().setHomeButtonEnabled(false);
173 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800174 }
175
176 @Override
177 protected void onSaveInstanceState(Bundle outState) {
178 super.onSaveInstanceState(outState);
179
180 // Save the current fragment, if it is the same as originally launched
181 if (mCurrentHeader != null) {
182 outState.putParcelable(SAVE_KEY_CURRENT_HEADER, mCurrentHeader);
183 }
184 if (mParentHeader != null) {
185 outState.putParcelable(SAVE_KEY_PARENT_HEADER, mParentHeader);
186 }
187 }
188
Gilles Debunnee78c1872011-06-20 15:00:07 -0700189 @Override
190 public void onResume() {
191 super.onResume();
192
193 ListAdapter listAdapter = getListAdapter();
194 if (listAdapter instanceof HeaderAdapter) {
195 ((HeaderAdapter) listAdapter).resume();
196 }
Amith Yamasaniea7b28c2012-06-20 13:51:40 -0700197 invalidateHeaders();
Gilles Debunnee78c1872011-06-20 15:00:07 -0700198 }
199
200 @Override
201 public void onPause() {
202 super.onPause();
203
204 ListAdapter listAdapter = getListAdapter();
205 if (listAdapter instanceof HeaderAdapter) {
206 ((HeaderAdapter) listAdapter).pause();
Amith Yamasani86708a82012-06-06 20:23:08 -0700207 }
208 }
209
210 @Override
211 public void onDestroy() {
212 super.onDestroy();
213 if (mListeningToAccountUpdates) {
Amith Yamasani56821db2012-06-05 13:20:45 -0700214 AccountManager.get(this).removeOnAccountsUpdatedListener(this);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700215 }
216 }
217
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800218 private void switchToHeaderLocal(Header header) {
219 mInLocalHeaderSwitch = true;
220 switchToHeader(header);
221 mInLocalHeaderSwitch = false;
222 }
223
224 @Override
225 public void switchToHeader(Header header) {
226 if (!mInLocalHeaderSwitch) {
227 mCurrentHeader = null;
228 mParentHeader = null;
229 }
230 super.switchToHeader(header);
231 }
232
233 /**
234 * Switch to parent fragment and store the grand parent's info
Jake Hamby2748fc22011-01-12 15:06:28 -0800235 * @param className name of the activity wrapper for the parent fragment.
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800236 */
237 private void switchToParent(String className) {
238 final ComponentName cn = new ComponentName(this, className);
239 try {
240 final PackageManager pm = getPackageManager();
241 final ActivityInfo parentInfo = pm.getActivityInfo(cn, PackageManager.GET_META_DATA);
242
243 if (parentInfo != null && parentInfo.metaData != null) {
244 String fragmentClass = parentInfo.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
245 CharSequence fragmentTitle = parentInfo.loadLabel(pm);
246 Header parentHeader = new Header();
247 parentHeader.fragment = fragmentClass;
248 parentHeader.title = fragmentTitle;
249 mCurrentHeader = parentHeader;
250
251 switchToHeaderLocal(parentHeader);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700252 highlightHeader(mTopLevelHeaderId);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800253
254 mParentHeader = new Header();
255 mParentHeader.fragment
256 = parentInfo.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
257 mParentHeader.title = parentInfo.metaData.getString(META_DATA_KEY_PARENT_TITLE);
258 }
259 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700260 Log.w(LOG_TAG, "Could not find parent activity : " + className);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800261 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700262 }
263
Amith Yamasani3965ae62010-11-15 14:45:19 -0800264 @Override
265 public void onNewIntent(Intent intent) {
266 super.onNewIntent(intent);
267
268 // If it is not launched from history, then reset to top-level
269 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0
Gilles Debunne3661b622011-06-27 11:19:16 -0700270 && mFirstHeader != null && !onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800271 switchToHeaderLocal(mFirstHeader);
Amith Yamasani3965ae62010-11-15 14:45:19 -0800272 }
273 }
274
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700275 private void highlightHeader(int id) {
276 if (id != 0) {
277 Integer index = mHeaderIndexMap.get(id);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700278 if (index != null) {
279 getListView().setItemChecked(index, true);
Amith Yamasani990fb522011-09-02 09:47:18 -0700280 getListView().smoothScrollToPosition(index);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700281 }
282 }
283 }
284
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700285 @Override
286 public Intent getIntent() {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700287 Intent superIntent = super.getIntent();
288 String startingFragment = getStartingFragmentClass(superIntent);
Gilles Debunne3661b622011-06-27 11:19:16 -0700289 // This is called from super.onCreate, isMultiPane() is not yet reliable
290 // Do not use onIsHidingHeaders either, which relies itself on this method
291 if (startingFragment != null && !onIsMultiPane()) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700292 Intent modIntent = new Intent(superIntent);
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700293 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700294 Bundle args = superIntent.getExtras();
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700295 if (args != null) {
296 args = new Bundle(args);
297 } else {
298 args = new Bundle();
299 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700300 args.putParcelable("intent", superIntent);
301 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras());
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700302 return modIntent;
303 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700304 return superIntent;
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700305 }
306
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700307 /**
Amith Yamasani379d9b02010-09-27 12:03:59 -0700308 * Checks if the component name in the intent is different from the Settings class and
309 * returns the class name to load as a fragment.
310 */
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700311 protected String getStartingFragmentClass(Intent intent) {
312 if (mFragmentClass != null) return mFragmentClass;
313
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700314 String intentClass = intent.getComponent().getClassName();
Amith Yamasani379d9b02010-09-27 12:03:59 -0700315 if (intentClass.equals(getClass().getName())) return null;
316
Dianne Hackbornee293792010-11-01 12:32:33 -0700317 if ("com.android.settings.ManageApplications".equals(intentClass)
318 || "com.android.settings.RunningServices".equals(intentClass)
319 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700320 // Old names of manage apps.
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700321 intentClass = com.android.settings.applications.ManageApplications.class.getName();
322 }
323
Amith Yamasani379d9b02010-09-27 12:03:59 -0700324 return intentClass;
325 }
326
327 /**
328 * Override initial header when an activity-alias is causing Settings to be launched
329 * for a specific fragment encoded in the android:name parameter.
330 */
331 @Override
332 public Header onGetInitialHeader() {
333 String fragmentClass = getStartingFragmentClass(super.getIntent());
334 if (fragmentClass != null) {
335 Header header = new Header();
336 header.fragment = fragmentClass;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800337 header.title = getTitle();
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700338 header.fragmentArguments = getIntent().getExtras();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800339 mCurrentHeader = header;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700340 return header;
341 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700342
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700343 return mFirstHeader;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700344 }
345
Dianne Hackbornb7258182011-03-15 16:23:55 -0700346 @Override
Dianne Hackborn48147dc2011-03-18 12:29:41 -0700347 public Intent onBuildStartFragmentIntent(String fragmentName, Bundle args,
348 int titleRes, int shortTitleRes) {
349 Intent intent = super.onBuildStartFragmentIntent(fragmentName, args,
350 titleRes, shortTitleRes);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700351
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700352 // some fragments want to avoid split actionbar
Gilles Debunne162e5412011-07-11 14:13:31 -0700353 if (DataUsageSummary.class.getName().equals(fragmentName) ||
354 PowerUsageSummary.class.getName().equals(fragmentName) ||
Gilles Debunnecd8e5242011-07-25 11:36:15 -0700355 AccountSyncSettings.class.getName().equals(fragmentName) ||
Jeff Sharkey11d30122012-03-19 16:54:07 -0700356 UserDictionarySettings.class.getName().equals(fragmentName) ||
Adam Powellfaba7e42012-03-26 17:28:38 -0700357 Memory.class.getName().equals(fragmentName) ||
Jeff Sharkeya339cba2012-05-08 11:42:49 -0700358 ManageApplications.class.getName().equals(fragmentName) ||
359 WirelessSettings.class.getName().equals(fragmentName) ||
360 SoundSettings.class.getName().equals(fragmentName) ||
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700361 PrivacySettings.class.getName().equals(fragmentName) ||
Jeff Sharkey9fd7ac12012-08-25 00:06:08 -0700362 ManageAccountsSettings.class.getName().equals(fragmentName) ||
363 VpnSettings.class.getName().equals(fragmentName)) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700364 intent.putExtra(EXTRA_CLEAR_UI_OPTIONS, true);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700365 }
366
Dianne Hackbornb7258182011-03-15 16:23:55 -0700367 intent.setClass(this, SubSettings.class);
368 return intent;
369 }
Amith Yamasaniea7b28c2012-06-20 13:51:40 -0700370
Amith Yamasani379d9b02010-09-27 12:03:59 -0700371 /**
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700372 * Populate the activity with the top-level headers.
373 */
Amith Yamasanid7993472010-08-18 13:59:28 -0700374 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700375 public void onBuildHeaders(List<Header> headers) {
376 loadHeadersFromResource(R.xml.settings_headers, headers);
Amith Yamasanid7993472010-08-18 13:59:28 -0700377
Gilles Debunnee78c1872011-06-20 15:00:07 -0700378 updateHeaderList(headers);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700379 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700380
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700381 private void updateHeaderList(List<Header> target) {
382 int i = 0;
383 while (i < target.size()) {
384 Header header = target.get(i);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700385 // Ids are integers, so downcasting
386 int id = (int) header.id;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700387 if (id == R.id.dock_settings) {
388 if (!needsDockSettings())
389 target.remove(header);
390 } else if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
391 Utils.updateHeaderToSpecificActivityFromMetaDataOrRemove(this, target, header);
Gilles Debunne2454f492011-06-21 16:16:06 -0700392 } else if (id == R.id.wifi_settings) {
393 // Remove WiFi Settings if WiFi service is not available.
394 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
395 target.remove(header);
396 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700397 } else if (id == R.id.bluetooth_settings) {
398 // Remove Bluetooth Settings if Bluetooth service is not available.
Gilles Debunne2454f492011-06-21 16:16:06 -0700399 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700400 target.remove(header);
401 }
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700402 } else if (id == R.id.data_usage_settings) {
403 // Remove data usage when kernel module not enabled
404 final INetworkManagementService netManager = INetworkManagementService.Stub
405 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
406 try {
407 if (!netManager.isBandwidthControlEnabled()) {
408 target.remove(header);
409 }
410 } catch (RemoteException e) {
411 // ignored
412 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700413 } else if (id == R.id.account_settings) {
414 int headerIndex = i + 1;
415 i = insertAccountsHeaders(target, headerIndex);
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700416 } else if (id == R.id.user_settings) {
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700417 if (!UserHandle.MU_ENABLED
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700418 || !getResources().getBoolean(R.bool.enable_user_management)
419 || Utils.isMonkeyRunning()) {
420 target.remove(header);
421 }
422 }
Dianne Hackbornbb06a422012-08-16 11:01:57 -0700423 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700424 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
425 target.remove(header);
Amith Yamasanid7993472010-08-18 13:59:28 -0700426 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700427
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700428 // Increment if the current one wasn't removed by the Utils code.
429 if (target.get(i) == header) {
Amith Yamasani3965ae62010-11-15 14:45:19 -0800430 // Hold on to the first header, when we need to reset to the top-level
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700431 if (mFirstHeader == null &&
432 HeaderAdapter.getHeaderType(header) != HeaderAdapter.HEADER_TYPE_CATEGORY) {
433 mFirstHeader = header;
434 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700435 mHeaderIndexMap.put(id, i);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700436 i++;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700437 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700438 }
439 }
440
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700441 private int insertAccountsHeaders(List<Header> target, int headerIndex) {
442 String[] accountTypes = mAuthenticatorHelper.getEnabledAccountTypes();
443 List<Header> accountHeaders = new ArrayList<Header>(accountTypes.length);
444 for (String accountType : accountTypes) {
445 CharSequence label = mAuthenticatorHelper.getLabelForType(this, accountType);
Brian Muramatsuc28af522012-06-28 14:25:14 -0700446 if (label == null) {
447 continue;
448 }
449
Amith Yamasani3882c2e2012-06-07 17:03:20 -0700450 Account[] accounts = AccountManager.get(this).getAccountsByType(accountType);
451 boolean skipToAccount = accounts.length == 1
452 && !mAuthenticatorHelper.hasAccountPreferences(accountType);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700453 Header accHeader = new Header();
454 accHeader.title = label;
455 if (accHeader.extras == null) {
456 accHeader.extras = new Bundle();
457 }
Amith Yamasani3882c2e2012-06-07 17:03:20 -0700458 if (skipToAccount) {
459 accHeader.breadCrumbTitleRes = R.string.account_sync_settings_title;
460 accHeader.breadCrumbShortTitleRes = R.string.account_sync_settings_title;
461 accHeader.fragment = AccountSyncSettings.class.getName();
462 accHeader.fragmentArguments = new Bundle();
463 // Need this for the icon
464 accHeader.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
465 accHeader.extras.putParcelable(AccountSyncSettings.ACCOUNT_KEY, accounts[0]);
466 accHeader.fragmentArguments.putParcelable(AccountSyncSettings.ACCOUNT_KEY,
467 accounts[0]);
468 } else {
469 accHeader.breadCrumbTitle = label;
470 accHeader.breadCrumbShortTitle = label;
471 accHeader.fragment = ManageAccountsSettings.class.getName();
472 accHeader.fragmentArguments = new Bundle();
473 accHeader.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
474 accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE,
475 accountType);
476 if (!isMultiPane()) {
477 accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_LABEL,
478 label.toString());
479 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700480 }
481 accountHeaders.add(accHeader);
482 }
483
484 // Sort by label
485 Collections.sort(accountHeaders, new Comparator<Header>() {
486 @Override
487 public int compare(Header h1, Header h2) {
488 return h1.title.toString().compareTo(h2.title.toString());
489 }
490 });
491
492 for (Header header : accountHeaders) {
493 target.add(headerIndex++, header);
494 }
Amith Yamasani86708a82012-06-06 20:23:08 -0700495 if (!mListeningToAccountUpdates) {
496 AccountManager.get(this).addOnAccountsUpdatedListener(this, null, true);
497 mListeningToAccountUpdates = true;
498 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700499 return headerIndex;
500 }
501
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700502 private boolean needsDockSettings() {
503 return getResources().getBoolean(R.bool.has_dock_settings);
Amith Yamasanib61cf512010-09-12 08:17:50 -0700504 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700505
506 private void getMetaData() {
507 try {
508 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
509 PackageManager.GET_META_DATA);
510 if (ai == null || ai.metaData == null) return;
511 mTopLevelHeaderId = ai.metaData.getInt(META_DATA_KEY_HEADER_ID);
512 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700513
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800514 // Check if it has a parent specified and create a Header object
515 final int parentHeaderTitleRes = ai.metaData.getInt(META_DATA_KEY_PARENT_TITLE);
516 String parentFragmentClass = ai.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
517 if (parentFragmentClass != null) {
518 mParentHeader = new Header();
519 mParentHeader.fragment = parentFragmentClass;
520 if (parentHeaderTitleRes != 0) {
521 mParentHeader.title = getResources().getString(parentHeaderTitleRes);
522 }
523 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700524 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700525 // No recovery
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700526 }
527 }
528
Amith Yamasani9e3a4702011-01-11 09:09:26 -0800529 @Override
530 public boolean hasNextButton() {
531 return super.hasNextButton();
532 }
533
534 @Override
535 public Button getNextButton() {
536 return super.getNextButton();
537 }
538
Gilles Debunnee78c1872011-06-20 15:00:07 -0700539 private static class HeaderAdapter extends ArrayAdapter<Header> {
540 static final int HEADER_TYPE_CATEGORY = 0;
541 static final int HEADER_TYPE_NORMAL = 1;
542 static final int HEADER_TYPE_SWITCH = 2;
543 private static final int HEADER_TYPE_COUNT = HEADER_TYPE_SWITCH + 1;
544
545 private final WifiEnabler mWifiEnabler;
546 private final BluetoothEnabler mBluetoothEnabler;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700547 private AuthenticatorHelper mAuthHelper;
Gilles Debunnee78c1872011-06-20 15:00:07 -0700548
549 private static class HeaderViewHolder {
550 ImageView icon;
551 TextView title;
552 TextView summary;
553 Switch switch_;
554 }
555
556 private LayoutInflater mInflater;
557
558 static int getHeaderType(Header header) {
559 if (header.fragment == null && header.intent == null) {
560 return HEADER_TYPE_CATEGORY;
561 } else if (header.id == R.id.wifi_settings || header.id == R.id.bluetooth_settings) {
562 return HEADER_TYPE_SWITCH;
563 } else {
564 return HEADER_TYPE_NORMAL;
565 }
566 }
567
568 @Override
569 public int getItemViewType(int position) {
570 Header header = getItem(position);
571 return getHeaderType(header);
572 }
573
574 @Override
575 public boolean areAllItemsEnabled() {
576 return false; // because of categories
577 }
578
579 @Override
580 public boolean isEnabled(int position) {
581 return getItemViewType(position) != HEADER_TYPE_CATEGORY;
582 }
583
584 @Override
585 public int getViewTypeCount() {
586 return HEADER_TYPE_COUNT;
587 }
588
589 @Override
590 public boolean hasStableIds() {
591 return true;
592 }
593
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700594 public HeaderAdapter(Context context, List<Header> objects,
595 AuthenticatorHelper authenticatorHelper) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700596 super(context, 0, objects);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700597
598 mAuthHelper = authenticatorHelper;
Gilles Debunnee78c1872011-06-20 15:00:07 -0700599 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700600
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700601 // Temp Switches provided as placeholder until the adapter replaces these with actual
Gilles Debunnee78c1872011-06-20 15:00:07 -0700602 // Switches inflated from their layouts. Must be done before adapter is set in super
603 mWifiEnabler = new WifiEnabler(context, new Switch(context));
604 mBluetoothEnabler = new BluetoothEnabler(context, new Switch(context));
605 }
606
607 @Override
608 public View getView(int position, View convertView, ViewGroup parent) {
609 HeaderViewHolder holder;
610 Header header = getItem(position);
611 int headerType = getHeaderType(header);
612 View view = null;
613
614 if (convertView == null) {
615 holder = new HeaderViewHolder();
616 switch (headerType) {
617 case HEADER_TYPE_CATEGORY:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700618 view = new TextView(getContext(), null,
619 android.R.attr.listSeparatorTextViewStyle);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700620 holder.title = (TextView) view;
621 break;
622
623 case HEADER_TYPE_SWITCH:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700624 view = mInflater.inflate(R.layout.preference_header_switch_item, parent,
625 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700626 holder.icon = (ImageView) view.findViewById(R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700627 holder.title = (TextView)
628 view.findViewById(com.android.internal.R.id.title);
629 holder.summary = (TextView)
630 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700631 holder.switch_ = (Switch) view.findViewById(R.id.switchWidget);
632 break;
633
634 case HEADER_TYPE_NORMAL:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700635 view = mInflater.inflate(
Amith Yamasanic8a93172012-06-08 13:35:47 -0700636 R.layout.preference_header_item, parent,
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700637 false);
Amith Yamasanic8a93172012-06-08 13:35:47 -0700638 holder.icon = (ImageView) view.findViewById(R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700639 holder.title = (TextView)
640 view.findViewById(com.android.internal.R.id.title);
641 holder.summary = (TextView)
642 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700643 break;
644 }
645 view.setTag(holder);
646 } else {
647 view = convertView;
648 holder = (HeaderViewHolder) view.getTag();
649 }
650
651 // All view fields must be updated every time, because the view may be recycled
652 switch (headerType) {
653 case HEADER_TYPE_CATEGORY:
654 holder.title.setText(header.getTitle(getContext().getResources()));
655 break;
656
657 case HEADER_TYPE_SWITCH:
658 // Would need a different treatment if the main menu had more switches
659 if (header.id == R.id.wifi_settings) {
660 mWifiEnabler.setSwitch(holder.switch_);
661 } else {
662 mBluetoothEnabler.setSwitch(holder.switch_);
663 }
664 // No break, fall through on purpose to update common fields
665
666 //$FALL-THROUGH$
667 case HEADER_TYPE_NORMAL:
Amith Yamasani3882c2e2012-06-07 17:03:20 -0700668 if (header.extras != null
669 && header.extras.containsKey(ManageAccountsSettings.KEY_ACCOUNT_TYPE)) {
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700670 String accType = header.extras.getString(
671 ManageAccountsSettings.KEY_ACCOUNT_TYPE);
672 ViewGroup.LayoutParams lp = holder.icon.getLayoutParams();
673 lp.width = getContext().getResources().getDimensionPixelSize(
674 R.dimen.header_icon_width);
675 lp.height = lp.width;
676 holder.icon.setLayoutParams(lp);
677 Drawable icon = mAuthHelper.getDrawableForType(getContext(), accType);
678 holder.icon.setImageDrawable(icon);
679 } else {
680 holder.icon.setImageResource(header.iconRes);
681 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700682 holder.title.setText(header.getTitle(getContext().getResources()));
683 CharSequence summary = header.getSummary(getContext().getResources());
684 if (!TextUtils.isEmpty(summary)) {
685 holder.summary.setVisibility(View.VISIBLE);
686 holder.summary.setText(summary);
687 } else {
688 holder.summary.setVisibility(View.GONE);
689 }
690 break;
691 }
692
693 return view;
694 }
695
696 public void resume() {
697 mWifiEnabler.resume();
698 mBluetoothEnabler.resume();
699 }
Brian Muramatsuc28af522012-06-28 14:25:14 -0700700
Gilles Debunnee78c1872011-06-20 15:00:07 -0700701 public void pause() {
702 mWifiEnabler.pause();
703 mBluetoothEnabler.pause();
704 }
705 }
706
707 @Override
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700708 public void onHeaderClick(Header header, int position) {
709 boolean revert = false;
710 if (header.id == R.id.account_add) {
711 revert = true;
712 }
713
714 super.onHeaderClick(header, position);
715
716 if (revert && mLastHeader != null) {
717 highlightHeader((int) mLastHeader.id);
718 } else {
719 mLastHeader = header;
720 }
721 }
722
723 @Override
Amith Yamasania4379d62011-07-22 10:34:58 -0700724 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
725 // Override the fragment title for Wallpaper settings
Amith Yamasanidfb65432011-11-29 16:38:14 -0800726 int titleRes = pref.getTitleRes();
Amith Yamasania4379d62011-07-22 10:34:58 -0700727 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
Amith Yamasanidfb65432011-11-29 16:38:14 -0800728 titleRes = R.string.wallpaper_settings_fragment_title;
Amith Yamasania4379d62011-07-22 10:34:58 -0700729 }
Jean Chalard7dabe452012-05-10 18:08:07 +0900730 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
731 null, 0);
Amith Yamasania4379d62011-07-22 10:34:58 -0700732 return true;
733 }
734
Amith Yamasani3d384f42012-05-11 15:22:04 -0700735 public boolean shouldUpRecreateTask(Intent targetIntent) {
736 return super.shouldUpRecreateTask(new Intent(this, Settings.class));
737 }
738
Amith Yamasania4379d62011-07-22 10:34:58 -0700739 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700740 public void setListAdapter(ListAdapter adapter) {
Amith Yamasaniea7b28c2012-06-20 13:51:40 -0700741 if (adapter == null) {
742 super.setListAdapter(null);
743 } else {
744 super.setListAdapter(new HeaderAdapter(this, getHeaders(), mAuthenticatorHelper));
Gilles Debunnee78c1872011-06-20 15:00:07 -0700745 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700746 }
747
Amith Yamasani56821db2012-06-05 13:20:45 -0700748 @Override
749 public void onAccountsUpdated(Account[] accounts) {
750 mAuthenticatorHelper.onAccountsUpdated(this, accounts);
751 invalidateHeaders();
752 }
753
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700754 /*
755 * Settings subclasses for launching independently.
756 */
Gilles Debunnee78c1872011-06-20 15:00:07 -0700757 public static class BluetoothSettingsActivity extends Settings { /* empty */ }
758 public static class WirelessSettingsActivity extends Settings { /* empty */ }
759 public static class TetherSettingsActivity extends Settings { /* empty */ }
760 public static class VpnSettingsActivity extends Settings { /* empty */ }
761 public static class DateTimeSettingsActivity extends Settings { /* empty */ }
762 public static class StorageSettingsActivity extends Settings { /* empty */ }
763 public static class WifiSettingsActivity extends Settings { /* empty */ }
repo syncb98463f2011-06-30 10:58:43 -0700764 public static class WifiP2pSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700765 public static class InputMethodAndLanguageSettingsActivity extends Settings { /* empty */ }
Jeff Browne46c5f32012-04-05 11:42:18 -0700766 public static class KeyboardLayoutPickerActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700767 public static class InputMethodAndSubtypeEnablerActivity extends Settings { /* empty */ }
satoke077d2b2011-07-25 09:38:11 +0900768 public static class SpellCheckersSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700769 public static class LocalePickerActivity extends Settings { /* empty */ }
770 public static class UserDictionarySettingsActivity extends Settings { /* empty */ }
771 public static class SoundSettingsActivity extends Settings { /* empty */ }
772 public static class DisplaySettingsActivity extends Settings { /* empty */ }
773 public static class DeviceInfoSettingsActivity extends Settings { /* empty */ }
774 public static class ApplicationSettingsActivity extends Settings { /* empty */ }
775 public static class ManageApplicationsActivity extends Settings { /* empty */ }
776 public static class StorageUseActivity extends Settings { /* empty */ }
777 public static class DevelopmentSettingsActivity extends Settings { /* empty */ }
778 public static class AccessibilitySettingsActivity extends Settings { /* empty */ }
779 public static class SecuritySettingsActivity extends Settings { /* empty */ }
Gilles Debunnea6a8a142011-06-09 11:56:17 -0700780 public static class LocationSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700781 public static class PrivacySettingsActivity extends Settings { /* empty */ }
782 public static class DockSettingsActivity extends Settings { /* empty */ }
783 public static class RunningServicesActivity extends Settings { /* empty */ }
784 public static class ManageAccountsSettingsActivity extends Settings { /* empty */ }
785 public static class PowerUsageSummaryActivity extends Settings { /* empty */ }
786 public static class AccountSyncSettingsActivity extends Settings { /* empty */ }
787 public static class AccountSyncSettingsInAddAccountActivity extends Settings { /* empty */ }
788 public static class CryptKeeperSettingsActivity extends Settings { /* empty */ }
789 public static class DeviceAdminSettingsActivity extends Settings { /* empty */ }
790 public static class DataUsageSummaryActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700791 public static class AdvancedWifiSettingsActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700792 public static class TextToSpeechSettingsActivity extends Settings { /* empty */ }
Jeff Hamilton3d670de2011-09-21 16:44:36 -0500793 public static class AndroidBeamSettingsActivity extends Settings { /* empty */ }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800794}