blob: e8a8f462048555f4e662ddbda78540884bceea7a [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;
27import com.android.settings.wifi.WifiEnabler;
28
Amith Yamasani56821db2012-06-05 13:20:45 -070029import android.accounts.Account;
30import android.accounts.AccountManager;
31import android.accounts.OnAccountsUpdateListener;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080032import android.content.ComponentName;
Gilles Debunnee78c1872011-06-20 15:00:07 -070033import android.content.Context;
Amith Yamasani379d9b02010-09-27 12:03:59 -070034import android.content.Intent;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070035import android.content.pm.ActivityInfo;
36import android.content.pm.PackageManager;
37import android.content.pm.PackageManager.NameNotFoundException;
Amith Yamasanid1ab8282012-05-18 09:50:08 -070038import android.graphics.drawable.Drawable;
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -070039import android.os.Bundle;
Jeff Sharkey34e964d2012-04-21 15:41:48 -070040import android.os.INetworkManagementService;
41import android.os.RemoteException;
42import android.os.ServiceManager;
Amith Yamasanib810a0d2012-03-25 10:12:26 -070043import android.os.UserId;
Amith Yamasania4379d62011-07-22 10:34:58 -070044import android.preference.Preference;
Amith Yamasani02cf71a2010-09-21 15:48:52 -070045import android.preference.PreferenceActivity;
Amith Yamasanid1ab8282012-05-18 09:50:08 -070046import android.preference.PreferenceActivity.Header;
Amith Yamasania4379d62011-07-22 10:34:58 -070047import android.preference.PreferenceFragment;
Gilles Debunnee78c1872011-06-20 15:00:07 -070048import android.text.TextUtils;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080049import android.util.Log;
Gilles Debunnee78c1872011-06-20 15:00:07 -070050import android.view.LayoutInflater;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080051import android.view.View;
52import android.view.View.OnClickListener;
Gilles Debunnee78c1872011-06-20 15:00:07 -070053import android.view.ViewGroup;
54import android.widget.ArrayAdapter;
Amith Yamasaniea7b28c2012-06-20 13:51:40 -070055import android.widget.BaseAdapter;
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 = {
99 R.id.wifi_settings,
100 R.id.bluetooth_settings,
101 R.id.sound_settings,
102 R.id.display_settings,
Amith Yamasani3deeeb72012-04-05 14:44:48 -0700103 R.id.security_settings,
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700104 R.id.account_settings,
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700105 R.id.about_settings
106 };
107
Amith Yamasanifa3eea52012-03-28 10:25:08 -0700108 private boolean mEnableUserManagement = false;
109
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700110 // TODO: Update Call Settings based on airplane mode state.
Amith Yamasanib61cf512010-09-12 08:17:50 -0700111
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700112 protected HashMap<Integer, Integer> mHeaderIndexMap = new HashMap<Integer, Integer>();
113
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700114 private AuthenticatorHelper mAuthenticatorHelper;
115 private Header mLastHeader;
Amith Yamasani86708a82012-06-06 20:23:08 -0700116 private boolean mListeningToAccountUpdates;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700117
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700118 @Override
119 protected void onCreate(Bundle savedInstanceState) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700120 if (getIntent().getBooleanExtra(EXTRA_CLEAR_UI_OPTIONS, false)) {
121 getWindow().setUiOptions(0);
122 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700123
Amith Yamasanifa3eea52012-03-28 10:25:08 -0700124 if (android.provider.Settings.Secure.getInt(getContentResolver(), "multiuser_enabled", -1)
125 > 0) {
126 mEnableUserManagement = true;
127 }
128
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700129 mAuthenticatorHelper = new AuthenticatorHelper();
130 mAuthenticatorHelper.updateAuthDescriptions(this);
131 mAuthenticatorHelper.onAccountsUpdated(this, null);
132
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700133 getMetaData();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800134 mInLocalHeaderSwitch = true;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700135 super.onCreate(savedInstanceState);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800136 mInLocalHeaderSwitch = false;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700137
Gilles Debunne3661b622011-06-27 11:19:16 -0700138 if (!onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700139 highlightHeader(mTopLevelHeaderId);
Amith Yamasani72aa19d2010-12-09 06:07:12 -0800140 // Force the title so that it doesn't get overridden by a direct launch of
141 // a specific settings screen.
142 setTitle(R.string.settings_label);
143 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800144
145 // Retrieve any saved state
146 if (savedInstanceState != null) {
147 mCurrentHeader = savedInstanceState.getParcelable(SAVE_KEY_CURRENT_HEADER);
148 mParentHeader = savedInstanceState.getParcelable(SAVE_KEY_PARENT_HEADER);
149 }
150
151 // If the current header was saved, switch to it
152 if (savedInstanceState != null && mCurrentHeader != null) {
153 //switchToHeaderLocal(mCurrentHeader);
154 showBreadCrumbs(mCurrentHeader.title, null);
155 }
156
157 if (mParentHeader != null) {
158 setParentTitle(mParentHeader.title, null, new OnClickListener() {
159 public void onClick(View v) {
160 switchToParent(mParentHeader.fragment);
161 }
162 });
163 }
Gilles Debunnedc7101f2011-06-27 12:00:49 -0700164
Amith Yamasani3d384f42012-05-11 15:22:04 -0700165 // Override up navigation for multi-pane, since we handle it in the fragment breadcrumbs
166 if (onIsMultiPane()) {
167 getActionBar().setDisplayHomeAsUpEnabled(false);
168 getActionBar().setHomeButtonEnabled(false);
169 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800170 }
171
172 @Override
173 protected void onSaveInstanceState(Bundle outState) {
174 super.onSaveInstanceState(outState);
175
176 // Save the current fragment, if it is the same as originally launched
177 if (mCurrentHeader != null) {
178 outState.putParcelable(SAVE_KEY_CURRENT_HEADER, mCurrentHeader);
179 }
180 if (mParentHeader != null) {
181 outState.putParcelable(SAVE_KEY_PARENT_HEADER, mParentHeader);
182 }
183 }
184
Gilles Debunnee78c1872011-06-20 15:00:07 -0700185 @Override
186 public void onResume() {
187 super.onResume();
188
189 ListAdapter listAdapter = getListAdapter();
190 if (listAdapter instanceof HeaderAdapter) {
191 ((HeaderAdapter) listAdapter).resume();
192 }
Amith Yamasaniea7b28c2012-06-20 13:51:40 -0700193 invalidateHeaders();
Gilles Debunnee78c1872011-06-20 15:00:07 -0700194 }
195
196 @Override
197 public void onPause() {
198 super.onPause();
199
200 ListAdapter listAdapter = getListAdapter();
201 if (listAdapter instanceof HeaderAdapter) {
202 ((HeaderAdapter) listAdapter).pause();
Amith Yamasani86708a82012-06-06 20:23:08 -0700203 }
204 }
205
206 @Override
207 public void onDestroy() {
208 super.onDestroy();
209 if (mListeningToAccountUpdates) {
Amith Yamasani56821db2012-06-05 13:20:45 -0700210 AccountManager.get(this).removeOnAccountsUpdatedListener(this);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700211 }
212 }
213
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800214 private void switchToHeaderLocal(Header header) {
215 mInLocalHeaderSwitch = true;
216 switchToHeader(header);
217 mInLocalHeaderSwitch = false;
218 }
219
220 @Override
221 public void switchToHeader(Header header) {
222 if (!mInLocalHeaderSwitch) {
223 mCurrentHeader = null;
224 mParentHeader = null;
225 }
226 super.switchToHeader(header);
227 }
228
229 /**
230 * Switch to parent fragment and store the grand parent's info
Jake Hamby2748fc22011-01-12 15:06:28 -0800231 * @param className name of the activity wrapper for the parent fragment.
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800232 */
233 private void switchToParent(String className) {
234 final ComponentName cn = new ComponentName(this, className);
235 try {
236 final PackageManager pm = getPackageManager();
237 final ActivityInfo parentInfo = pm.getActivityInfo(cn, PackageManager.GET_META_DATA);
238
239 if (parentInfo != null && parentInfo.metaData != null) {
240 String fragmentClass = parentInfo.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
241 CharSequence fragmentTitle = parentInfo.loadLabel(pm);
242 Header parentHeader = new Header();
243 parentHeader.fragment = fragmentClass;
244 parentHeader.title = fragmentTitle;
245 mCurrentHeader = parentHeader;
246
247 switchToHeaderLocal(parentHeader);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700248 highlightHeader(mTopLevelHeaderId);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800249
250 mParentHeader = new Header();
251 mParentHeader.fragment
252 = parentInfo.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
253 mParentHeader.title = parentInfo.metaData.getString(META_DATA_KEY_PARENT_TITLE);
254 }
255 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700256 Log.w(LOG_TAG, "Could not find parent activity : " + className);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800257 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700258 }
259
Amith Yamasani3965ae62010-11-15 14:45:19 -0800260 @Override
261 public void onNewIntent(Intent intent) {
262 super.onNewIntent(intent);
263
264 // If it is not launched from history, then reset to top-level
265 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0
Gilles Debunne3661b622011-06-27 11:19:16 -0700266 && mFirstHeader != null && !onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800267 switchToHeaderLocal(mFirstHeader);
Amith Yamasani3965ae62010-11-15 14:45:19 -0800268 }
269 }
270
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700271 private void highlightHeader(int id) {
272 if (id != 0) {
273 Integer index = mHeaderIndexMap.get(id);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700274 if (index != null) {
275 getListView().setItemChecked(index, true);
Amith Yamasani990fb522011-09-02 09:47:18 -0700276 getListView().smoothScrollToPosition(index);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700277 }
278 }
279 }
280
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700281 @Override
282 public Intent getIntent() {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700283 Intent superIntent = super.getIntent();
284 String startingFragment = getStartingFragmentClass(superIntent);
Gilles Debunne3661b622011-06-27 11:19:16 -0700285 // This is called from super.onCreate, isMultiPane() is not yet reliable
286 // Do not use onIsHidingHeaders either, which relies itself on this method
287 if (startingFragment != null && !onIsMultiPane()) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700288 Intent modIntent = new Intent(superIntent);
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700289 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700290 Bundle args = superIntent.getExtras();
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700291 if (args != null) {
292 args = new Bundle(args);
293 } else {
294 args = new Bundle();
295 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700296 args.putParcelable("intent", superIntent);
297 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras());
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700298 return modIntent;
299 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700300 return superIntent;
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700301 }
302
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700303 /**
Amith Yamasani379d9b02010-09-27 12:03:59 -0700304 * Checks if the component name in the intent is different from the Settings class and
305 * returns the class name to load as a fragment.
306 */
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700307 protected String getStartingFragmentClass(Intent intent) {
308 if (mFragmentClass != null) return mFragmentClass;
309
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700310 String intentClass = intent.getComponent().getClassName();
Amith Yamasani379d9b02010-09-27 12:03:59 -0700311 if (intentClass.equals(getClass().getName())) return null;
312
Dianne Hackbornee293792010-11-01 12:32:33 -0700313 if ("com.android.settings.ManageApplications".equals(intentClass)
314 || "com.android.settings.RunningServices".equals(intentClass)
315 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700316 // Old names of manage apps.
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700317 intentClass = com.android.settings.applications.ManageApplications.class.getName();
318 }
319
Amith Yamasani379d9b02010-09-27 12:03:59 -0700320 return intentClass;
321 }
322
323 /**
324 * Override initial header when an activity-alias is causing Settings to be launched
325 * for a specific fragment encoded in the android:name parameter.
326 */
327 @Override
328 public Header onGetInitialHeader() {
329 String fragmentClass = getStartingFragmentClass(super.getIntent());
330 if (fragmentClass != null) {
331 Header header = new Header();
332 header.fragment = fragmentClass;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800333 header.title = getTitle();
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700334 header.fragmentArguments = getIntent().getExtras();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800335 mCurrentHeader = header;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700336 return header;
337 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700338
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700339 return mFirstHeader;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700340 }
341
Dianne Hackbornb7258182011-03-15 16:23:55 -0700342 @Override
Dianne Hackborn48147dc2011-03-18 12:29:41 -0700343 public Intent onBuildStartFragmentIntent(String fragmentName, Bundle args,
344 int titleRes, int shortTitleRes) {
345 Intent intent = super.onBuildStartFragmentIntent(fragmentName, args,
346 titleRes, shortTitleRes);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700347
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700348 // some fragments want to avoid split actionbar
Gilles Debunne162e5412011-07-11 14:13:31 -0700349 if (DataUsageSummary.class.getName().equals(fragmentName) ||
350 PowerUsageSummary.class.getName().equals(fragmentName) ||
Gilles Debunnecd8e5242011-07-25 11:36:15 -0700351 AccountSyncSettings.class.getName().equals(fragmentName) ||
Jeff Sharkey11d30122012-03-19 16:54:07 -0700352 UserDictionarySettings.class.getName().equals(fragmentName) ||
Adam Powellfaba7e42012-03-26 17:28:38 -0700353 Memory.class.getName().equals(fragmentName) ||
Jeff Sharkeya339cba2012-05-08 11:42:49 -0700354 ManageApplications.class.getName().equals(fragmentName) ||
355 WirelessSettings.class.getName().equals(fragmentName) ||
356 SoundSettings.class.getName().equals(fragmentName) ||
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700357 PrivacySettings.class.getName().equals(fragmentName) ||
358 ManageAccountsSettings.class.getName().equals(fragmentName)) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700359 intent.putExtra(EXTRA_CLEAR_UI_OPTIONS, true);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700360 }
361
Dianne Hackbornb7258182011-03-15 16:23:55 -0700362 intent.setClass(this, SubSettings.class);
363 return intent;
364 }
Amith Yamasaniea7b28c2012-06-20 13:51:40 -0700365
Amith Yamasani379d9b02010-09-27 12:03:59 -0700366 /**
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700367 * Populate the activity with the top-level headers.
368 */
Amith Yamasanid7993472010-08-18 13:59:28 -0700369 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700370 public void onBuildHeaders(List<Header> headers) {
371 loadHeadersFromResource(R.xml.settings_headers, headers);
Amith Yamasanid7993472010-08-18 13:59:28 -0700372
Gilles Debunnee78c1872011-06-20 15:00:07 -0700373 updateHeaderList(headers);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700374 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700375
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700376 private void updateHeaderList(List<Header> target) {
377 int i = 0;
378 while (i < target.size()) {
379 Header header = target.get(i);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700380 // Ids are integers, so downcasting
381 int id = (int) header.id;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700382 if (id == R.id.dock_settings) {
383 if (!needsDockSettings())
384 target.remove(header);
385 } else if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
386 Utils.updateHeaderToSpecificActivityFromMetaDataOrRemove(this, target, header);
Gilles Debunne2454f492011-06-21 16:16:06 -0700387 } else if (id == R.id.wifi_settings) {
388 // Remove WiFi Settings if WiFi service is not available.
389 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
390 target.remove(header);
391 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700392 } else if (id == R.id.bluetooth_settings) {
393 // Remove Bluetooth Settings if Bluetooth service is not available.
Gilles Debunne2454f492011-06-21 16:16:06 -0700394 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700395 target.remove(header);
396 }
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700397 } else if (id == R.id.data_usage_settings) {
398 // Remove data usage when kernel module not enabled
399 final INetworkManagementService netManager = INetworkManagementService.Stub
400 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
401 try {
402 if (!netManager.isBandwidthControlEnabled()) {
403 target.remove(header);
404 }
405 } catch (RemoteException e) {
406 // ignored
407 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700408 } else if (id == R.id.account_settings) {
409 int headerIndex = i + 1;
410 i = insertAccountsHeaders(target, headerIndex);
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700411 } else if (id == R.id.user_settings) {
Amith Yamasanifa3eea52012-03-28 10:25:08 -0700412 if (!mEnableUserManagement
413 || !UserId.MU_ENABLED || UserId.myUserId() != 0
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700414 || !getResources().getBoolean(R.bool.enable_user_management)
415 || Utils.isMonkeyRunning()) {
416 target.remove(header);
417 }
418 }
419 if (UserId.MU_ENABLED && UserId.myUserId() != 0
420 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
421 target.remove(header);
Amith Yamasanid7993472010-08-18 13:59:28 -0700422 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700423
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700424 // Increment if the current one wasn't removed by the Utils code.
425 if (target.get(i) == header) {
Amith Yamasani3965ae62010-11-15 14:45:19 -0800426 // Hold on to the first header, when we need to reset to the top-level
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700427 if (mFirstHeader == null &&
428 HeaderAdapter.getHeaderType(header) != HeaderAdapter.HEADER_TYPE_CATEGORY) {
429 mFirstHeader = header;
430 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700431 mHeaderIndexMap.put(id, i);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700432 i++;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700433 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700434 }
435 }
436
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700437 private int insertAccountsHeaders(List<Header> target, int headerIndex) {
438 String[] accountTypes = mAuthenticatorHelper.getEnabledAccountTypes();
439 List<Header> accountHeaders = new ArrayList<Header>(accountTypes.length);
440 for (String accountType : accountTypes) {
441 CharSequence label = mAuthenticatorHelper.getLabelForType(this, accountType);
Amith Yamasani3882c2e2012-06-07 17:03:20 -0700442 Account[] accounts = AccountManager.get(this).getAccountsByType(accountType);
443 boolean skipToAccount = accounts.length == 1
444 && !mAuthenticatorHelper.hasAccountPreferences(accountType);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700445 Header accHeader = new Header();
446 accHeader.title = label;
447 if (accHeader.extras == null) {
448 accHeader.extras = new Bundle();
449 }
Amith Yamasani3882c2e2012-06-07 17:03:20 -0700450 if (skipToAccount) {
451 accHeader.breadCrumbTitleRes = R.string.account_sync_settings_title;
452 accHeader.breadCrumbShortTitleRes = R.string.account_sync_settings_title;
453 accHeader.fragment = AccountSyncSettings.class.getName();
454 accHeader.fragmentArguments = new Bundle();
455 // Need this for the icon
456 accHeader.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
457 accHeader.extras.putParcelable(AccountSyncSettings.ACCOUNT_KEY, accounts[0]);
458 accHeader.fragmentArguments.putParcelable(AccountSyncSettings.ACCOUNT_KEY,
459 accounts[0]);
460 } else {
461 accHeader.breadCrumbTitle = label;
462 accHeader.breadCrumbShortTitle = label;
463 accHeader.fragment = ManageAccountsSettings.class.getName();
464 accHeader.fragmentArguments = new Bundle();
465 accHeader.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
466 accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE,
467 accountType);
468 if (!isMultiPane()) {
469 accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_LABEL,
470 label.toString());
471 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700472 }
473 accountHeaders.add(accHeader);
474 }
475
476 // Sort by label
477 Collections.sort(accountHeaders, new Comparator<Header>() {
478 @Override
479 public int compare(Header h1, Header h2) {
480 return h1.title.toString().compareTo(h2.title.toString());
481 }
482 });
483
484 for (Header header : accountHeaders) {
485 target.add(headerIndex++, header);
486 }
Amith Yamasani86708a82012-06-06 20:23:08 -0700487 if (!mListeningToAccountUpdates) {
488 AccountManager.get(this).addOnAccountsUpdatedListener(this, null, true);
489 mListeningToAccountUpdates = true;
490 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700491 return headerIndex;
492 }
493
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700494 private boolean needsDockSettings() {
495 return getResources().getBoolean(R.bool.has_dock_settings);
Amith Yamasanib61cf512010-09-12 08:17:50 -0700496 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700497
498 private void getMetaData() {
499 try {
500 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
501 PackageManager.GET_META_DATA);
502 if (ai == null || ai.metaData == null) return;
503 mTopLevelHeaderId = ai.metaData.getInt(META_DATA_KEY_HEADER_ID);
504 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700505
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800506 // Check if it has a parent specified and create a Header object
507 final int parentHeaderTitleRes = ai.metaData.getInt(META_DATA_KEY_PARENT_TITLE);
508 String parentFragmentClass = ai.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
509 if (parentFragmentClass != null) {
510 mParentHeader = new Header();
511 mParentHeader.fragment = parentFragmentClass;
512 if (parentHeaderTitleRes != 0) {
513 mParentHeader.title = getResources().getString(parentHeaderTitleRes);
514 }
515 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700516 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700517 // No recovery
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700518 }
519 }
520
Amith Yamasani9e3a4702011-01-11 09:09:26 -0800521 @Override
522 public boolean hasNextButton() {
523 return super.hasNextButton();
524 }
525
526 @Override
527 public Button getNextButton() {
528 return super.getNextButton();
529 }
530
Gilles Debunnee78c1872011-06-20 15:00:07 -0700531 private static class HeaderAdapter extends ArrayAdapter<Header> {
532 static final int HEADER_TYPE_CATEGORY = 0;
533 static final int HEADER_TYPE_NORMAL = 1;
534 static final int HEADER_TYPE_SWITCH = 2;
535 private static final int HEADER_TYPE_COUNT = HEADER_TYPE_SWITCH + 1;
536
537 private final WifiEnabler mWifiEnabler;
538 private final BluetoothEnabler mBluetoothEnabler;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700539 private AuthenticatorHelper mAuthHelper;
Gilles Debunnee78c1872011-06-20 15:00:07 -0700540
541 private static class HeaderViewHolder {
542 ImageView icon;
543 TextView title;
544 TextView summary;
545 Switch switch_;
546 }
547
548 private LayoutInflater mInflater;
549
550 static int getHeaderType(Header header) {
551 if (header.fragment == null && header.intent == null) {
552 return HEADER_TYPE_CATEGORY;
553 } else if (header.id == R.id.wifi_settings || header.id == R.id.bluetooth_settings) {
554 return HEADER_TYPE_SWITCH;
555 } else {
556 return HEADER_TYPE_NORMAL;
557 }
558 }
559
560 @Override
561 public int getItemViewType(int position) {
562 Header header = getItem(position);
563 return getHeaderType(header);
564 }
565
566 @Override
567 public boolean areAllItemsEnabled() {
568 return false; // because of categories
569 }
570
571 @Override
572 public boolean isEnabled(int position) {
573 return getItemViewType(position) != HEADER_TYPE_CATEGORY;
574 }
575
576 @Override
577 public int getViewTypeCount() {
578 return HEADER_TYPE_COUNT;
579 }
580
581 @Override
582 public boolean hasStableIds() {
583 return true;
584 }
585
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700586 public HeaderAdapter(Context context, List<Header> objects,
587 AuthenticatorHelper authenticatorHelper) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700588 super(context, 0, objects);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700589
590 mAuthHelper = authenticatorHelper;
Gilles Debunnee78c1872011-06-20 15:00:07 -0700591 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700592
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700593 // Temp Switches provided as placeholder until the adapter replaces these with actual
Gilles Debunnee78c1872011-06-20 15:00:07 -0700594 // Switches inflated from their layouts. Must be done before adapter is set in super
595 mWifiEnabler = new WifiEnabler(context, new Switch(context));
596 mBluetoothEnabler = new BluetoothEnabler(context, new Switch(context));
597 }
598
599 @Override
600 public View getView(int position, View convertView, ViewGroup parent) {
601 HeaderViewHolder holder;
602 Header header = getItem(position);
603 int headerType = getHeaderType(header);
604 View view = null;
605
606 if (convertView == null) {
607 holder = new HeaderViewHolder();
608 switch (headerType) {
609 case HEADER_TYPE_CATEGORY:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700610 view = new TextView(getContext(), null,
611 android.R.attr.listSeparatorTextViewStyle);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700612 holder.title = (TextView) view;
613 break;
614
615 case HEADER_TYPE_SWITCH:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700616 view = mInflater.inflate(R.layout.preference_header_switch_item, parent,
617 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700618 holder.icon = (ImageView) view.findViewById(R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700619 holder.title = (TextView)
620 view.findViewById(com.android.internal.R.id.title);
621 holder.summary = (TextView)
622 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700623 holder.switch_ = (Switch) view.findViewById(R.id.switchWidget);
624 break;
625
626 case HEADER_TYPE_NORMAL:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700627 view = mInflater.inflate(
Amith Yamasanic8a93172012-06-08 13:35:47 -0700628 R.layout.preference_header_item, parent,
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700629 false);
Amith Yamasanic8a93172012-06-08 13:35:47 -0700630 holder.icon = (ImageView) view.findViewById(R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700631 holder.title = (TextView)
632 view.findViewById(com.android.internal.R.id.title);
633 holder.summary = (TextView)
634 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700635 break;
636 }
637 view.setTag(holder);
638 } else {
639 view = convertView;
640 holder = (HeaderViewHolder) view.getTag();
641 }
642
643 // All view fields must be updated every time, because the view may be recycled
644 switch (headerType) {
645 case HEADER_TYPE_CATEGORY:
646 holder.title.setText(header.getTitle(getContext().getResources()));
647 break;
648
649 case HEADER_TYPE_SWITCH:
650 // Would need a different treatment if the main menu had more switches
651 if (header.id == R.id.wifi_settings) {
652 mWifiEnabler.setSwitch(holder.switch_);
653 } else {
654 mBluetoothEnabler.setSwitch(holder.switch_);
655 }
656 // No break, fall through on purpose to update common fields
657
658 //$FALL-THROUGH$
659 case HEADER_TYPE_NORMAL:
Amith Yamasani3882c2e2012-06-07 17:03:20 -0700660 if (header.extras != null
661 && header.extras.containsKey(ManageAccountsSettings.KEY_ACCOUNT_TYPE)) {
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700662 String accType = header.extras.getString(
663 ManageAccountsSettings.KEY_ACCOUNT_TYPE);
664 ViewGroup.LayoutParams lp = holder.icon.getLayoutParams();
665 lp.width = getContext().getResources().getDimensionPixelSize(
666 R.dimen.header_icon_width);
667 lp.height = lp.width;
668 holder.icon.setLayoutParams(lp);
669 Drawable icon = mAuthHelper.getDrawableForType(getContext(), accType);
670 holder.icon.setImageDrawable(icon);
671 } else {
672 holder.icon.setImageResource(header.iconRes);
673 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700674 holder.title.setText(header.getTitle(getContext().getResources()));
675 CharSequence summary = header.getSummary(getContext().getResources());
676 if (!TextUtils.isEmpty(summary)) {
677 holder.summary.setVisibility(View.VISIBLE);
678 holder.summary.setText(summary);
679 } else {
680 holder.summary.setVisibility(View.GONE);
681 }
682 break;
683 }
684
685 return view;
686 }
687
688 public void resume() {
689 mWifiEnabler.resume();
690 mBluetoothEnabler.resume();
691 }
692
693 public void pause() {
694 mWifiEnabler.pause();
695 mBluetoothEnabler.pause();
696 }
697 }
698
699 @Override
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700700 public void onHeaderClick(Header header, int position) {
701 boolean revert = false;
702 if (header.id == R.id.account_add) {
703 revert = true;
704 }
705
706 super.onHeaderClick(header, position);
707
708 if (revert && mLastHeader != null) {
709 highlightHeader((int) mLastHeader.id);
710 } else {
711 mLastHeader = header;
712 }
713 }
714
715 @Override
Amith Yamasania4379d62011-07-22 10:34:58 -0700716 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
717 // Override the fragment title for Wallpaper settings
Amith Yamasanidfb65432011-11-29 16:38:14 -0800718 int titleRes = pref.getTitleRes();
Amith Yamasania4379d62011-07-22 10:34:58 -0700719 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
Amith Yamasanidfb65432011-11-29 16:38:14 -0800720 titleRes = R.string.wallpaper_settings_fragment_title;
Amith Yamasania4379d62011-07-22 10:34:58 -0700721 }
Jean Chalard7dabe452012-05-10 18:08:07 +0900722 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
723 null, 0);
Amith Yamasania4379d62011-07-22 10:34:58 -0700724 return true;
725 }
726
Amith Yamasani3d384f42012-05-11 15:22:04 -0700727 public boolean shouldUpRecreateTask(Intent targetIntent) {
728 return super.shouldUpRecreateTask(new Intent(this, Settings.class));
729 }
730
Amith Yamasania4379d62011-07-22 10:34:58 -0700731 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700732 public void setListAdapter(ListAdapter adapter) {
Amith Yamasaniea7b28c2012-06-20 13:51:40 -0700733 if (adapter == null) {
734 super.setListAdapter(null);
735 } else {
736 super.setListAdapter(new HeaderAdapter(this, getHeaders(), mAuthenticatorHelper));
Gilles Debunnee78c1872011-06-20 15:00:07 -0700737 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700738 }
739
Amith Yamasani56821db2012-06-05 13:20:45 -0700740 @Override
741 public void onAccountsUpdated(Account[] accounts) {
742 mAuthenticatorHelper.onAccountsUpdated(this, accounts);
743 invalidateHeaders();
744 }
745
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700746 /*
747 * Settings subclasses for launching independently.
748 */
Gilles Debunnee78c1872011-06-20 15:00:07 -0700749 public static class BluetoothSettingsActivity extends Settings { /* empty */ }
750 public static class WirelessSettingsActivity extends Settings { /* empty */ }
751 public static class TetherSettingsActivity extends Settings { /* empty */ }
752 public static class VpnSettingsActivity extends Settings { /* empty */ }
753 public static class DateTimeSettingsActivity extends Settings { /* empty */ }
754 public static class StorageSettingsActivity extends Settings { /* empty */ }
755 public static class WifiSettingsActivity extends Settings { /* empty */ }
repo syncb98463f2011-06-30 10:58:43 -0700756 public static class WifiP2pSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700757 public static class InputMethodAndLanguageSettingsActivity extends Settings { /* empty */ }
Jeff Browne46c5f32012-04-05 11:42:18 -0700758 public static class KeyboardLayoutPickerActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700759 public static class InputMethodAndSubtypeEnablerActivity extends Settings { /* empty */ }
satoke077d2b2011-07-25 09:38:11 +0900760 public static class SpellCheckersSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700761 public static class LocalePickerActivity extends Settings { /* empty */ }
762 public static class UserDictionarySettingsActivity extends Settings { /* empty */ }
763 public static class SoundSettingsActivity extends Settings { /* empty */ }
764 public static class DisplaySettingsActivity extends Settings { /* empty */ }
765 public static class DeviceInfoSettingsActivity extends Settings { /* empty */ }
766 public static class ApplicationSettingsActivity extends Settings { /* empty */ }
767 public static class ManageApplicationsActivity extends Settings { /* empty */ }
768 public static class StorageUseActivity extends Settings { /* empty */ }
769 public static class DevelopmentSettingsActivity extends Settings { /* empty */ }
770 public static class AccessibilitySettingsActivity extends Settings { /* empty */ }
771 public static class SecuritySettingsActivity extends Settings { /* empty */ }
Gilles Debunnea6a8a142011-06-09 11:56:17 -0700772 public static class LocationSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700773 public static class PrivacySettingsActivity extends Settings { /* empty */ }
774 public static class DockSettingsActivity extends Settings { /* empty */ }
775 public static class RunningServicesActivity extends Settings { /* empty */ }
776 public static class ManageAccountsSettingsActivity extends Settings { /* empty */ }
777 public static class PowerUsageSummaryActivity extends Settings { /* empty */ }
778 public static class AccountSyncSettingsActivity extends Settings { /* empty */ }
779 public static class AccountSyncSettingsInAddAccountActivity extends Settings { /* empty */ }
780 public static class CryptKeeperSettingsActivity extends Settings { /* empty */ }
781 public static class DeviceAdminSettingsActivity extends Settings { /* empty */ }
782 public static class DataUsageSummaryActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700783 public static class AdvancedWifiSettingsActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700784 public static class TextToSpeechSettingsActivity extends Settings { /* empty */ }
Jeff Hamilton3d670de2011-09-21 16:44:36 -0500785 public static class AndroidBeamSettingsActivity extends Settings { /* empty */ }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800786}