blob: d7580feb260ce105a0045dc484d22e55d8c3dfbd [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 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 Yamasani9e3a4702011-01-11 09:09:26 -080056import android.widget.Button;
Gilles Debunnee78c1872011-06-20 15:00:07 -070057import android.widget.ImageView;
58import android.widget.ListAdapter;
59import android.widget.Switch;
60import android.widget.TextView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080061
Gilles Debunnee78c1872011-06-20 15:00:07 -070062import java.util.ArrayList;
Amith Yamasanid1ab8282012-05-18 09:50:08 -070063import java.util.Collections;
64import java.util.Comparator;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070065import java.util.HashMap;
Amith Yamasani02cf71a2010-09-21 15:48:52 -070066import java.util.List;
Amith Yamasanid7993472010-08-18 13:59:28 -070067
68/**
69 * Top-level settings activity to handle single pane and double pane UI layout.
70 */
Amith Yamasani56821db2012-06-05 13:20:45 -070071public class Settings extends PreferenceActivity
72 implements ButtonBarHandler, OnAccountsUpdateListener {
Amith Yamasanid7993472010-08-18 13:59:28 -070073
Gilles Debunnee78c1872011-06-20 15:00:07 -070074 private static final String LOG_TAG = "Settings";
Amith Yamasaniea7b28c2012-06-20 13:51:40 -070075
Amith Yamasani5203bdf2010-11-04 09:59:44 -070076 private static final String META_DATA_KEY_HEADER_ID =
Gilles Debunnee78c1872011-06-20 15:00:07 -070077 "com.android.settings.TOP_LEVEL_HEADER_ID";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070078 private static final String META_DATA_KEY_FRAGMENT_CLASS =
Gilles Debunnee78c1872011-06-20 15:00:07 -070079 "com.android.settings.FRAGMENT_CLASS";
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080080 private static final String META_DATA_KEY_PARENT_TITLE =
81 "com.android.settings.PARENT_FRAGMENT_TITLE";
82 private static final String META_DATA_KEY_PARENT_FRAGMENT_CLASS =
83 "com.android.settings.PARENT_FRAGMENT_CLASS";
84
Jeff Sharkey54d0af52011-08-11 18:26:57 -070085 private static final String EXTRA_CLEAR_UI_OPTIONS = "settings:remove_ui_options";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070086
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080087 private static final String SAVE_KEY_CURRENT_HEADER = "com.android.settings.CURRENT_HEADER";
88 private static final String SAVE_KEY_PARENT_HEADER = "com.android.settings.PARENT_HEADER";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070089
90 private String mFragmentClass;
91 private int mTopLevelHeaderId;
Amith Yamasani3965ae62010-11-15 14:45:19 -080092 private Header mFirstHeader;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080093 private Header mCurrentHeader;
94 private Header mParentHeader;
95 private boolean mInLocalHeaderSwitch;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070096
Amith Yamasanib810a0d2012-03-25 10:12:26 -070097 // Show only these settings for restricted users
98 private int[] SETTINGS_FOR_RESTRICTED = {
Amith Yamasani32630392012-08-10 19:31:39 -070099 R.id.wireless_section,
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700100 R.id.wifi_settings,
101 R.id.bluetooth_settings,
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700102 R.id.data_usage_settings,
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700103 R.id.wireless_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 Yamasani9627a8e2012-09-23 12:54:14 -0700111 R.id.location_settings,
Amith Yamasani3deeeb72012-04-05 14:44:48 -0700112 R.id.security_settings,
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700113 R.id.language_settings,
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700114 R.id.user_settings,
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700115 R.id.account_settings,
Amith Yamasani32630392012-08-10 19:31:39 -0700116 R.id.account_add,
117 R.id.system_section,
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700118 R.id.date_time_settings,
Jeff Brown9e143f52012-09-19 20:46:07 -0700119 R.id.about_settings,
Svetoslav Ganov46045d82012-09-20 15:04:55 -0700120 R.id.accessibility_settings
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700121 };
122
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700123 // TODO: Update Call Settings based on airplane mode state.
Amith Yamasanib61cf512010-09-12 08:17:50 -0700124
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700125 protected HashMap<Integer, Integer> mHeaderIndexMap = new HashMap<Integer, Integer>();
126
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700127 private AuthenticatorHelper mAuthenticatorHelper;
128 private Header mLastHeader;
Amith Yamasani86708a82012-06-06 20:23:08 -0700129 private boolean mListeningToAccountUpdates;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700130
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700131 @Override
132 protected void onCreate(Bundle savedInstanceState) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700133 if (getIntent().getBooleanExtra(EXTRA_CLEAR_UI_OPTIONS, false)) {
134 getWindow().setUiOptions(0);
135 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700136
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700137 mAuthenticatorHelper = new AuthenticatorHelper();
138 mAuthenticatorHelper.updateAuthDescriptions(this);
139 mAuthenticatorHelper.onAccountsUpdated(this, null);
140
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700141 getMetaData();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800142 mInLocalHeaderSwitch = true;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700143 super.onCreate(savedInstanceState);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800144 mInLocalHeaderSwitch = false;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700145
Gilles Debunne3661b622011-06-27 11:19:16 -0700146 if (!onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700147 highlightHeader(mTopLevelHeaderId);
Amith Yamasani72aa19d2010-12-09 06:07:12 -0800148 // Force the title so that it doesn't get overridden by a direct launch of
149 // a specific settings screen.
150 setTitle(R.string.settings_label);
151 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800152
153 // Retrieve any saved state
154 if (savedInstanceState != null) {
155 mCurrentHeader = savedInstanceState.getParcelable(SAVE_KEY_CURRENT_HEADER);
156 mParentHeader = savedInstanceState.getParcelable(SAVE_KEY_PARENT_HEADER);
157 }
158
159 // If the current header was saved, switch to it
160 if (savedInstanceState != null && mCurrentHeader != null) {
161 //switchToHeaderLocal(mCurrentHeader);
162 showBreadCrumbs(mCurrentHeader.title, null);
163 }
164
165 if (mParentHeader != null) {
166 setParentTitle(mParentHeader.title, null, new OnClickListener() {
167 public void onClick(View v) {
168 switchToParent(mParentHeader.fragment);
169 }
170 });
171 }
Gilles Debunnedc7101f2011-06-27 12:00:49 -0700172
Amith Yamasani3d384f42012-05-11 15:22:04 -0700173 // Override up navigation for multi-pane, since we handle it in the fragment breadcrumbs
174 if (onIsMultiPane()) {
175 getActionBar().setDisplayHomeAsUpEnabled(false);
176 getActionBar().setHomeButtonEnabled(false);
177 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800178 }
179
180 @Override
181 protected void onSaveInstanceState(Bundle outState) {
182 super.onSaveInstanceState(outState);
183
184 // Save the current fragment, if it is the same as originally launched
185 if (mCurrentHeader != null) {
186 outState.putParcelable(SAVE_KEY_CURRENT_HEADER, mCurrentHeader);
187 }
188 if (mParentHeader != null) {
189 outState.putParcelable(SAVE_KEY_PARENT_HEADER, mParentHeader);
190 }
191 }
192
Gilles Debunnee78c1872011-06-20 15:00:07 -0700193 @Override
194 public void onResume() {
195 super.onResume();
196
197 ListAdapter listAdapter = getListAdapter();
198 if (listAdapter instanceof HeaderAdapter) {
199 ((HeaderAdapter) listAdapter).resume();
200 }
Amith Yamasaniea7b28c2012-06-20 13:51:40 -0700201 invalidateHeaders();
Gilles Debunnee78c1872011-06-20 15:00:07 -0700202 }
203
204 @Override
205 public void onPause() {
206 super.onPause();
207
208 ListAdapter listAdapter = getListAdapter();
209 if (listAdapter instanceof HeaderAdapter) {
210 ((HeaderAdapter) listAdapter).pause();
Amith Yamasani86708a82012-06-06 20:23:08 -0700211 }
212 }
213
214 @Override
215 public void onDestroy() {
216 super.onDestroy();
217 if (mListeningToAccountUpdates) {
Amith Yamasani56821db2012-06-05 13:20:45 -0700218 AccountManager.get(this).removeOnAccountsUpdatedListener(this);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700219 }
220 }
221
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800222 private void switchToHeaderLocal(Header header) {
223 mInLocalHeaderSwitch = true;
224 switchToHeader(header);
225 mInLocalHeaderSwitch = false;
226 }
227
228 @Override
229 public void switchToHeader(Header header) {
230 if (!mInLocalHeaderSwitch) {
231 mCurrentHeader = null;
232 mParentHeader = null;
233 }
234 super.switchToHeader(header);
235 }
236
237 /**
238 * Switch to parent fragment and store the grand parent's info
Jake Hamby2748fc22011-01-12 15:06:28 -0800239 * @param className name of the activity wrapper for the parent fragment.
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800240 */
241 private void switchToParent(String className) {
242 final ComponentName cn = new ComponentName(this, className);
243 try {
244 final PackageManager pm = getPackageManager();
245 final ActivityInfo parentInfo = pm.getActivityInfo(cn, PackageManager.GET_META_DATA);
246
247 if (parentInfo != null && parentInfo.metaData != null) {
248 String fragmentClass = parentInfo.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
249 CharSequence fragmentTitle = parentInfo.loadLabel(pm);
250 Header parentHeader = new Header();
251 parentHeader.fragment = fragmentClass;
252 parentHeader.title = fragmentTitle;
253 mCurrentHeader = parentHeader;
254
255 switchToHeaderLocal(parentHeader);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700256 highlightHeader(mTopLevelHeaderId);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800257
258 mParentHeader = new Header();
259 mParentHeader.fragment
260 = parentInfo.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
261 mParentHeader.title = parentInfo.metaData.getString(META_DATA_KEY_PARENT_TITLE);
262 }
263 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700264 Log.w(LOG_TAG, "Could not find parent activity : " + className);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800265 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700266 }
267
Amith Yamasani3965ae62010-11-15 14:45:19 -0800268 @Override
269 public void onNewIntent(Intent intent) {
270 super.onNewIntent(intent);
271
272 // If it is not launched from history, then reset to top-level
273 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0
Gilles Debunne3661b622011-06-27 11:19:16 -0700274 && mFirstHeader != null && !onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800275 switchToHeaderLocal(mFirstHeader);
Amith Yamasani3965ae62010-11-15 14:45:19 -0800276 }
277 }
278
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700279 private void highlightHeader(int id) {
280 if (id != 0) {
281 Integer index = mHeaderIndexMap.get(id);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700282 if (index != null) {
283 getListView().setItemChecked(index, true);
Amith Yamasani990fb522011-09-02 09:47:18 -0700284 getListView().smoothScrollToPosition(index);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700285 }
286 }
287 }
288
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700289 @Override
290 public Intent getIntent() {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700291 Intent superIntent = super.getIntent();
292 String startingFragment = getStartingFragmentClass(superIntent);
Gilles Debunne3661b622011-06-27 11:19:16 -0700293 // This is called from super.onCreate, isMultiPane() is not yet reliable
294 // Do not use onIsHidingHeaders either, which relies itself on this method
295 if (startingFragment != null && !onIsMultiPane()) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700296 Intent modIntent = new Intent(superIntent);
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700297 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700298 Bundle args = superIntent.getExtras();
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700299 if (args != null) {
300 args = new Bundle(args);
301 } else {
302 args = new Bundle();
303 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700304 args.putParcelable("intent", superIntent);
305 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras());
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700306 return modIntent;
307 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700308 return superIntent;
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700309 }
310
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700311 /**
Amith Yamasani379d9b02010-09-27 12:03:59 -0700312 * Checks if the component name in the intent is different from the Settings class and
313 * returns the class name to load as a fragment.
314 */
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700315 protected String getStartingFragmentClass(Intent intent) {
316 if (mFragmentClass != null) return mFragmentClass;
317
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700318 String intentClass = intent.getComponent().getClassName();
Amith Yamasani379d9b02010-09-27 12:03:59 -0700319 if (intentClass.equals(getClass().getName())) return null;
320
Dianne Hackbornee293792010-11-01 12:32:33 -0700321 if ("com.android.settings.ManageApplications".equals(intentClass)
322 || "com.android.settings.RunningServices".equals(intentClass)
323 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700324 // Old names of manage apps.
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700325 intentClass = com.android.settings.applications.ManageApplications.class.getName();
326 }
327
Amith Yamasani379d9b02010-09-27 12:03:59 -0700328 return intentClass;
329 }
330
331 /**
332 * Override initial header when an activity-alias is causing Settings to be launched
333 * for a specific fragment encoded in the android:name parameter.
334 */
335 @Override
336 public Header onGetInitialHeader() {
337 String fragmentClass = getStartingFragmentClass(super.getIntent());
338 if (fragmentClass != null) {
339 Header header = new Header();
340 header.fragment = fragmentClass;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800341 header.title = getTitle();
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700342 header.fragmentArguments = getIntent().getExtras();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800343 mCurrentHeader = header;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700344 return header;
345 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700346
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700347 return mFirstHeader;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700348 }
349
Dianne Hackbornb7258182011-03-15 16:23:55 -0700350 @Override
Dianne Hackborn48147dc2011-03-18 12:29:41 -0700351 public Intent onBuildStartFragmentIntent(String fragmentName, Bundle args,
352 int titleRes, int shortTitleRes) {
353 Intent intent = super.onBuildStartFragmentIntent(fragmentName, args,
354 titleRes, shortTitleRes);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700355
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700356 // some fragments want to avoid split actionbar
Gilles Debunne162e5412011-07-11 14:13:31 -0700357 if (DataUsageSummary.class.getName().equals(fragmentName) ||
358 PowerUsageSummary.class.getName().equals(fragmentName) ||
Gilles Debunnecd8e5242011-07-25 11:36:15 -0700359 AccountSyncSettings.class.getName().equals(fragmentName) ||
Jeff Sharkey11d30122012-03-19 16:54:07 -0700360 UserDictionarySettings.class.getName().equals(fragmentName) ||
Adam Powellfaba7e42012-03-26 17:28:38 -0700361 Memory.class.getName().equals(fragmentName) ||
Jeff Sharkeya339cba2012-05-08 11:42:49 -0700362 ManageApplications.class.getName().equals(fragmentName) ||
363 WirelessSettings.class.getName().equals(fragmentName) ||
364 SoundSettings.class.getName().equals(fragmentName) ||
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700365 PrivacySettings.class.getName().equals(fragmentName) ||
Jeff Sharkey9fd7ac12012-08-25 00:06:08 -0700366 ManageAccountsSettings.class.getName().equals(fragmentName) ||
367 VpnSettings.class.getName().equals(fragmentName)) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700368 intent.putExtra(EXTRA_CLEAR_UI_OPTIONS, true);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700369 }
370
Dianne Hackbornb7258182011-03-15 16:23:55 -0700371 intent.setClass(this, SubSettings.class);
372 return intent;
373 }
Amith Yamasaniea7b28c2012-06-20 13:51:40 -0700374
Amith Yamasani379d9b02010-09-27 12:03:59 -0700375 /**
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700376 * Populate the activity with the top-level headers.
377 */
Amith Yamasanid7993472010-08-18 13:59:28 -0700378 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700379 public void onBuildHeaders(List<Header> headers) {
380 loadHeadersFromResource(R.xml.settings_headers, headers);
Amith Yamasanid7993472010-08-18 13:59:28 -0700381
Gilles Debunnee78c1872011-06-20 15:00:07 -0700382 updateHeaderList(headers);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700383 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700384
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700385 private void updateHeaderList(List<Header> target) {
386 int i = 0;
387 while (i < target.size()) {
388 Header header = target.get(i);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700389 // Ids are integers, so downcasting
390 int id = (int) header.id;
Chris Wren2bc32ae2012-09-24 14:23:46 -0400391 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700392 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 Yamasani5203bdf2010-11-04 09:59:44 -0700503 private void getMetaData() {
504 try {
505 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
506 PackageManager.GET_META_DATA);
507 if (ai == null || ai.metaData == null) return;
508 mTopLevelHeaderId = ai.metaData.getInt(META_DATA_KEY_HEADER_ID);
509 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700510
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800511 // Check if it has a parent specified and create a Header object
512 final int parentHeaderTitleRes = ai.metaData.getInt(META_DATA_KEY_PARENT_TITLE);
513 String parentFragmentClass = ai.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
514 if (parentFragmentClass != null) {
515 mParentHeader = new Header();
516 mParentHeader.fragment = parentFragmentClass;
517 if (parentHeaderTitleRes != 0) {
518 mParentHeader.title = getResources().getString(parentHeaderTitleRes);
519 }
520 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700521 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700522 // No recovery
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700523 }
524 }
525
Amith Yamasani9e3a4702011-01-11 09:09:26 -0800526 @Override
527 public boolean hasNextButton() {
528 return super.hasNextButton();
529 }
530
531 @Override
532 public Button getNextButton() {
533 return super.getNextButton();
534 }
535
Gilles Debunnee78c1872011-06-20 15:00:07 -0700536 private static class HeaderAdapter extends ArrayAdapter<Header> {
537 static final int HEADER_TYPE_CATEGORY = 0;
538 static final int HEADER_TYPE_NORMAL = 1;
539 static final int HEADER_TYPE_SWITCH = 2;
540 private static final int HEADER_TYPE_COUNT = HEADER_TYPE_SWITCH + 1;
541
542 private final WifiEnabler mWifiEnabler;
543 private final BluetoothEnabler mBluetoothEnabler;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700544 private AuthenticatorHelper mAuthHelper;
Gilles Debunnee78c1872011-06-20 15:00:07 -0700545
546 private static class HeaderViewHolder {
547 ImageView icon;
548 TextView title;
549 TextView summary;
550 Switch switch_;
551 }
552
553 private LayoutInflater mInflater;
554
555 static int getHeaderType(Header header) {
556 if (header.fragment == null && header.intent == null) {
557 return HEADER_TYPE_CATEGORY;
558 } else if (header.id == R.id.wifi_settings || header.id == R.id.bluetooth_settings) {
559 return HEADER_TYPE_SWITCH;
560 } else {
561 return HEADER_TYPE_NORMAL;
562 }
563 }
564
565 @Override
566 public int getItemViewType(int position) {
567 Header header = getItem(position);
568 return getHeaderType(header);
569 }
570
571 @Override
572 public boolean areAllItemsEnabled() {
573 return false; // because of categories
574 }
575
576 @Override
577 public boolean isEnabled(int position) {
578 return getItemViewType(position) != HEADER_TYPE_CATEGORY;
579 }
580
581 @Override
582 public int getViewTypeCount() {
583 return HEADER_TYPE_COUNT;
584 }
585
586 @Override
587 public boolean hasStableIds() {
588 return true;
589 }
590
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700591 public HeaderAdapter(Context context, List<Header> objects,
592 AuthenticatorHelper authenticatorHelper) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700593 super(context, 0, objects);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700594
595 mAuthHelper = authenticatorHelper;
Gilles Debunnee78c1872011-06-20 15:00:07 -0700596 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700597
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700598 // Temp Switches provided as placeholder until the adapter replaces these with actual
Gilles Debunnee78c1872011-06-20 15:00:07 -0700599 // Switches inflated from their layouts. Must be done before adapter is set in super
600 mWifiEnabler = new WifiEnabler(context, new Switch(context));
601 mBluetoothEnabler = new BluetoothEnabler(context, new Switch(context));
602 }
603
604 @Override
605 public View getView(int position, View convertView, ViewGroup parent) {
606 HeaderViewHolder holder;
607 Header header = getItem(position);
608 int headerType = getHeaderType(header);
609 View view = null;
610
611 if (convertView == null) {
612 holder = new HeaderViewHolder();
613 switch (headerType) {
614 case HEADER_TYPE_CATEGORY:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700615 view = new TextView(getContext(), null,
616 android.R.attr.listSeparatorTextViewStyle);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700617 holder.title = (TextView) view;
618 break;
619
620 case HEADER_TYPE_SWITCH:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700621 view = mInflater.inflate(R.layout.preference_header_switch_item, parent,
622 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700623 holder.icon = (ImageView) view.findViewById(R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700624 holder.title = (TextView)
625 view.findViewById(com.android.internal.R.id.title);
626 holder.summary = (TextView)
627 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700628 holder.switch_ = (Switch) view.findViewById(R.id.switchWidget);
629 break;
630
631 case HEADER_TYPE_NORMAL:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700632 view = mInflater.inflate(
Amith Yamasanic8a93172012-06-08 13:35:47 -0700633 R.layout.preference_header_item, parent,
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700634 false);
Amith Yamasanic8a93172012-06-08 13:35:47 -0700635 holder.icon = (ImageView) view.findViewById(R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700636 holder.title = (TextView)
637 view.findViewById(com.android.internal.R.id.title);
638 holder.summary = (TextView)
639 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700640 break;
641 }
642 view.setTag(holder);
643 } else {
644 view = convertView;
645 holder = (HeaderViewHolder) view.getTag();
646 }
647
648 // All view fields must be updated every time, because the view may be recycled
649 switch (headerType) {
650 case HEADER_TYPE_CATEGORY:
651 holder.title.setText(header.getTitle(getContext().getResources()));
652 break;
653
654 case HEADER_TYPE_SWITCH:
655 // Would need a different treatment if the main menu had more switches
656 if (header.id == R.id.wifi_settings) {
657 mWifiEnabler.setSwitch(holder.switch_);
658 } else {
659 mBluetoothEnabler.setSwitch(holder.switch_);
660 }
661 // No break, fall through on purpose to update common fields
662
663 //$FALL-THROUGH$
664 case HEADER_TYPE_NORMAL:
Amith Yamasani3882c2e2012-06-07 17:03:20 -0700665 if (header.extras != null
666 && header.extras.containsKey(ManageAccountsSettings.KEY_ACCOUNT_TYPE)) {
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700667 String accType = header.extras.getString(
668 ManageAccountsSettings.KEY_ACCOUNT_TYPE);
669 ViewGroup.LayoutParams lp = holder.icon.getLayoutParams();
670 lp.width = getContext().getResources().getDimensionPixelSize(
671 R.dimen.header_icon_width);
672 lp.height = lp.width;
673 holder.icon.setLayoutParams(lp);
674 Drawable icon = mAuthHelper.getDrawableForType(getContext(), accType);
675 holder.icon.setImageDrawable(icon);
676 } else {
677 holder.icon.setImageResource(header.iconRes);
678 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700679 holder.title.setText(header.getTitle(getContext().getResources()));
680 CharSequence summary = header.getSummary(getContext().getResources());
681 if (!TextUtils.isEmpty(summary)) {
682 holder.summary.setVisibility(View.VISIBLE);
683 holder.summary.setText(summary);
684 } else {
685 holder.summary.setVisibility(View.GONE);
686 }
687 break;
688 }
689
690 return view;
691 }
692
693 public void resume() {
694 mWifiEnabler.resume();
695 mBluetoothEnabler.resume();
696 }
Brian Muramatsuc28af522012-06-28 14:25:14 -0700697
Gilles Debunnee78c1872011-06-20 15:00:07 -0700698 public void pause() {
699 mWifiEnabler.pause();
700 mBluetoothEnabler.pause();
701 }
702 }
703
704 @Override
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700705 public void onHeaderClick(Header header, int position) {
706 boolean revert = false;
707 if (header.id == R.id.account_add) {
708 revert = true;
709 }
710
711 super.onHeaderClick(header, position);
712
713 if (revert && mLastHeader != null) {
714 highlightHeader((int) mLastHeader.id);
715 } else {
716 mLastHeader = header;
717 }
718 }
719
720 @Override
Amith Yamasania4379d62011-07-22 10:34:58 -0700721 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
722 // Override the fragment title for Wallpaper settings
Amith Yamasanidfb65432011-11-29 16:38:14 -0800723 int titleRes = pref.getTitleRes();
Amith Yamasania4379d62011-07-22 10:34:58 -0700724 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
Amith Yamasanidfb65432011-11-29 16:38:14 -0800725 titleRes = R.string.wallpaper_settings_fragment_title;
Amith Yamasani8666b9e2012-09-27 14:50:13 -0700726 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
727 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
728 titleRes = R.string.user_info_settings_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 */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700782 public static class RunningServicesActivity extends Settings { /* empty */ }
783 public static class ManageAccountsSettingsActivity extends Settings { /* empty */ }
784 public static class PowerUsageSummaryActivity extends Settings { /* empty */ }
785 public static class AccountSyncSettingsActivity extends Settings { /* empty */ }
786 public static class AccountSyncSettingsInAddAccountActivity extends Settings { /* empty */ }
787 public static class CryptKeeperSettingsActivity extends Settings { /* empty */ }
788 public static class DeviceAdminSettingsActivity extends Settings { /* empty */ }
789 public static class DataUsageSummaryActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700790 public static class AdvancedWifiSettingsActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700791 public static class TextToSpeechSettingsActivity extends Settings { /* empty */ }
Jeff Hamilton3d670de2011-09-21 16:44:36 -0500792 public static class AndroidBeamSettingsActivity extends Settings { /* empty */ }
Jeff Brown9e143f52012-09-19 20:46:07 -0700793 public static class WifiDisplaySettingsActivity extends Settings { /* empty */ }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800794}