blob: 353bc3a699a277300163ce068533c6f9792be02c [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 Yamasani9e3a4702011-01-11 09:09:26 -080055import android.widget.Button;
Gilles Debunnee78c1872011-06-20 15:00:07 -070056import android.widget.ImageView;
57import android.widget.ListAdapter;
58import android.widget.Switch;
59import android.widget.TextView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080060
Gilles Debunnee78c1872011-06-20 15:00:07 -070061import java.util.ArrayList;
Amith Yamasanid1ab8282012-05-18 09:50:08 -070062import java.util.Collections;
63import java.util.Comparator;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070064import java.util.HashMap;
Amith Yamasani02cf71a2010-09-21 15:48:52 -070065import java.util.List;
Amith Yamasanid7993472010-08-18 13:59:28 -070066
67/**
68 * Top-level settings activity to handle single pane and double pane UI layout.
69 */
Amith Yamasani56821db2012-06-05 13:20:45 -070070public class Settings extends PreferenceActivity
71 implements ButtonBarHandler, OnAccountsUpdateListener {
Amith Yamasanid7993472010-08-18 13:59:28 -070072
Gilles Debunnee78c1872011-06-20 15:00:07 -070073 private static final String LOG_TAG = "Settings";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070074 private static final String META_DATA_KEY_HEADER_ID =
Gilles Debunnee78c1872011-06-20 15:00:07 -070075 "com.android.settings.TOP_LEVEL_HEADER_ID";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070076 private static final String META_DATA_KEY_FRAGMENT_CLASS =
Gilles Debunnee78c1872011-06-20 15:00:07 -070077 "com.android.settings.FRAGMENT_CLASS";
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080078 private static final String META_DATA_KEY_PARENT_TITLE =
79 "com.android.settings.PARENT_FRAGMENT_TITLE";
80 private static final String META_DATA_KEY_PARENT_FRAGMENT_CLASS =
81 "com.android.settings.PARENT_FRAGMENT_CLASS";
82
Jeff Sharkey54d0af52011-08-11 18:26:57 -070083 private static final String EXTRA_CLEAR_UI_OPTIONS = "settings:remove_ui_options";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070084
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080085 private static final String SAVE_KEY_CURRENT_HEADER = "com.android.settings.CURRENT_HEADER";
86 private static final String SAVE_KEY_PARENT_HEADER = "com.android.settings.PARENT_HEADER";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070087
88 private String mFragmentClass;
89 private int mTopLevelHeaderId;
Amith Yamasani3965ae62010-11-15 14:45:19 -080090 private Header mFirstHeader;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080091 private Header mCurrentHeader;
92 private Header mParentHeader;
93 private boolean mInLocalHeaderSwitch;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070094
Amith Yamasanib810a0d2012-03-25 10:12:26 -070095 // Show only these settings for restricted users
96 private int[] SETTINGS_FOR_RESTRICTED = {
97 R.id.wifi_settings,
98 R.id.bluetooth_settings,
99 R.id.sound_settings,
100 R.id.display_settings,
Amith Yamasani3deeeb72012-04-05 14:44:48 -0700101 R.id.security_settings,
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700102 R.id.account_settings,
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700103 R.id.about_settings
104 };
105
Amith Yamasanifa3eea52012-03-28 10:25:08 -0700106 private boolean mEnableUserManagement = false;
107
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700108 // TODO: Update Call Settings based on airplane mode state.
Amith Yamasanib61cf512010-09-12 08:17:50 -0700109
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700110 protected HashMap<Integer, Integer> mHeaderIndexMap = new HashMap<Integer, Integer>();
Gilles Debunnee78c1872011-06-20 15:00:07 -0700111 private List<Header> mHeaders;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700112
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700113 private AuthenticatorHelper mAuthenticatorHelper;
114 private Header mLastHeader;
115
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700116 @Override
117 protected void onCreate(Bundle savedInstanceState) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700118 if (getIntent().getBooleanExtra(EXTRA_CLEAR_UI_OPTIONS, false)) {
119 getWindow().setUiOptions(0);
120 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700121
Amith Yamasanifa3eea52012-03-28 10:25:08 -0700122 if (android.provider.Settings.Secure.getInt(getContentResolver(), "multiuser_enabled", -1)
123 > 0) {
124 mEnableUserManagement = true;
125 }
126
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700127 mAuthenticatorHelper = new AuthenticatorHelper();
128 mAuthenticatorHelper.updateAuthDescriptions(this);
129 mAuthenticatorHelper.onAccountsUpdated(this, null);
130
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700131 getMetaData();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800132 mInLocalHeaderSwitch = true;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700133 super.onCreate(savedInstanceState);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800134 mInLocalHeaderSwitch = false;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700135
Gilles Debunne3661b622011-06-27 11:19:16 -0700136 if (!onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700137 highlightHeader(mTopLevelHeaderId);
Amith Yamasani72aa19d2010-12-09 06:07:12 -0800138 // Force the title so that it doesn't get overridden by a direct launch of
139 // a specific settings screen.
140 setTitle(R.string.settings_label);
141 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800142
143 // Retrieve any saved state
144 if (savedInstanceState != null) {
145 mCurrentHeader = savedInstanceState.getParcelable(SAVE_KEY_CURRENT_HEADER);
146 mParentHeader = savedInstanceState.getParcelable(SAVE_KEY_PARENT_HEADER);
147 }
148
149 // If the current header was saved, switch to it
150 if (savedInstanceState != null && mCurrentHeader != null) {
151 //switchToHeaderLocal(mCurrentHeader);
152 showBreadCrumbs(mCurrentHeader.title, null);
153 }
154
155 if (mParentHeader != null) {
156 setParentTitle(mParentHeader.title, null, new OnClickListener() {
157 public void onClick(View v) {
158 switchToParent(mParentHeader.fragment);
159 }
160 });
161 }
Gilles Debunnedc7101f2011-06-27 12:00:49 -0700162
Amith Yamasani3d384f42012-05-11 15:22:04 -0700163 // Override up navigation for multi-pane, since we handle it in the fragment breadcrumbs
164 if (onIsMultiPane()) {
165 getActionBar().setDisplayHomeAsUpEnabled(false);
166 getActionBar().setHomeButtonEnabled(false);
167 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800168 }
169
170 @Override
171 protected void onSaveInstanceState(Bundle outState) {
172 super.onSaveInstanceState(outState);
173
174 // Save the current fragment, if it is the same as originally launched
175 if (mCurrentHeader != null) {
176 outState.putParcelable(SAVE_KEY_CURRENT_HEADER, mCurrentHeader);
177 }
178 if (mParentHeader != null) {
179 outState.putParcelable(SAVE_KEY_PARENT_HEADER, mParentHeader);
180 }
181 }
182
Gilles Debunnee78c1872011-06-20 15:00:07 -0700183 @Override
184 public void onResume() {
185 super.onResume();
186
187 ListAdapter listAdapter = getListAdapter();
188 if (listAdapter instanceof HeaderAdapter) {
189 ((HeaderAdapter) listAdapter).resume();
Amith Yamasani56821db2012-06-05 13:20:45 -0700190 AccountManager.get(this).addOnAccountsUpdatedListener(this, null, true);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700191 }
192 }
193
194 @Override
195 public void onPause() {
196 super.onPause();
197
198 ListAdapter listAdapter = getListAdapter();
199 if (listAdapter instanceof HeaderAdapter) {
200 ((HeaderAdapter) listAdapter).pause();
Amith Yamasani56821db2012-06-05 13:20:45 -0700201 AccountManager.get(this).removeOnAccountsUpdatedListener(this);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700202 }
203 }
204
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800205 private void switchToHeaderLocal(Header header) {
206 mInLocalHeaderSwitch = true;
207 switchToHeader(header);
208 mInLocalHeaderSwitch = false;
209 }
210
211 @Override
212 public void switchToHeader(Header header) {
213 if (!mInLocalHeaderSwitch) {
214 mCurrentHeader = null;
215 mParentHeader = null;
216 }
217 super.switchToHeader(header);
218 }
219
220 /**
221 * Switch to parent fragment and store the grand parent's info
Jake Hamby2748fc22011-01-12 15:06:28 -0800222 * @param className name of the activity wrapper for the parent fragment.
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800223 */
224 private void switchToParent(String className) {
225 final ComponentName cn = new ComponentName(this, className);
226 try {
227 final PackageManager pm = getPackageManager();
228 final ActivityInfo parentInfo = pm.getActivityInfo(cn, PackageManager.GET_META_DATA);
229
230 if (parentInfo != null && parentInfo.metaData != null) {
231 String fragmentClass = parentInfo.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
232 CharSequence fragmentTitle = parentInfo.loadLabel(pm);
233 Header parentHeader = new Header();
234 parentHeader.fragment = fragmentClass;
235 parentHeader.title = fragmentTitle;
236 mCurrentHeader = parentHeader;
237
238 switchToHeaderLocal(parentHeader);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700239 highlightHeader(mTopLevelHeaderId);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800240
241 mParentHeader = new Header();
242 mParentHeader.fragment
243 = parentInfo.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
244 mParentHeader.title = parentInfo.metaData.getString(META_DATA_KEY_PARENT_TITLE);
245 }
246 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700247 Log.w(LOG_TAG, "Could not find parent activity : " + className);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800248 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700249 }
250
Amith Yamasani3965ae62010-11-15 14:45:19 -0800251 @Override
252 public void onNewIntent(Intent intent) {
253 super.onNewIntent(intent);
254
255 // If it is not launched from history, then reset to top-level
256 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0
Gilles Debunne3661b622011-06-27 11:19:16 -0700257 && mFirstHeader != null && !onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800258 switchToHeaderLocal(mFirstHeader);
Amith Yamasani3965ae62010-11-15 14:45:19 -0800259 }
260 }
261
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700262 private void highlightHeader(int id) {
263 if (id != 0) {
264 Integer index = mHeaderIndexMap.get(id);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700265 if (index != null) {
266 getListView().setItemChecked(index, true);
Amith Yamasani990fb522011-09-02 09:47:18 -0700267 getListView().smoothScrollToPosition(index);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700268 }
269 }
270 }
271
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700272 @Override
273 public Intent getIntent() {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700274 Intent superIntent = super.getIntent();
275 String startingFragment = getStartingFragmentClass(superIntent);
Gilles Debunne3661b622011-06-27 11:19:16 -0700276 // This is called from super.onCreate, isMultiPane() is not yet reliable
277 // Do not use onIsHidingHeaders either, which relies itself on this method
278 if (startingFragment != null && !onIsMultiPane()) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700279 Intent modIntent = new Intent(superIntent);
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700280 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700281 Bundle args = superIntent.getExtras();
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700282 if (args != null) {
283 args = new Bundle(args);
284 } else {
285 args = new Bundle();
286 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700287 args.putParcelable("intent", superIntent);
288 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras());
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700289 return modIntent;
290 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700291 return superIntent;
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700292 }
293
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700294 /**
Amith Yamasani379d9b02010-09-27 12:03:59 -0700295 * Checks if the component name in the intent is different from the Settings class and
296 * returns the class name to load as a fragment.
297 */
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700298 protected String getStartingFragmentClass(Intent intent) {
299 if (mFragmentClass != null) return mFragmentClass;
300
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700301 String intentClass = intent.getComponent().getClassName();
Amith Yamasani379d9b02010-09-27 12:03:59 -0700302 if (intentClass.equals(getClass().getName())) return null;
303
Dianne Hackbornee293792010-11-01 12:32:33 -0700304 if ("com.android.settings.ManageApplications".equals(intentClass)
305 || "com.android.settings.RunningServices".equals(intentClass)
306 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700307 // Old names of manage apps.
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700308 intentClass = com.android.settings.applications.ManageApplications.class.getName();
309 }
310
Amith Yamasani379d9b02010-09-27 12:03:59 -0700311 return intentClass;
312 }
313
314 /**
315 * Override initial header when an activity-alias is causing Settings to be launched
316 * for a specific fragment encoded in the android:name parameter.
317 */
318 @Override
319 public Header onGetInitialHeader() {
320 String fragmentClass = getStartingFragmentClass(super.getIntent());
321 if (fragmentClass != null) {
322 Header header = new Header();
323 header.fragment = fragmentClass;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800324 header.title = getTitle();
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700325 header.fragmentArguments = getIntent().getExtras();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800326 mCurrentHeader = header;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700327 return header;
328 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700329
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700330 return mFirstHeader;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700331 }
332
Dianne Hackbornb7258182011-03-15 16:23:55 -0700333 @Override
Dianne Hackborn48147dc2011-03-18 12:29:41 -0700334 public Intent onBuildStartFragmentIntent(String fragmentName, Bundle args,
335 int titleRes, int shortTitleRes) {
336 Intent intent = super.onBuildStartFragmentIntent(fragmentName, args,
337 titleRes, shortTitleRes);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700338
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700339 // some fragments want to avoid split actionbar
Gilles Debunne162e5412011-07-11 14:13:31 -0700340 if (DataUsageSummary.class.getName().equals(fragmentName) ||
341 PowerUsageSummary.class.getName().equals(fragmentName) ||
Gilles Debunnecd8e5242011-07-25 11:36:15 -0700342 AccountSyncSettings.class.getName().equals(fragmentName) ||
Jeff Sharkey11d30122012-03-19 16:54:07 -0700343 UserDictionarySettings.class.getName().equals(fragmentName) ||
Adam Powellfaba7e42012-03-26 17:28:38 -0700344 Memory.class.getName().equals(fragmentName) ||
Jeff Sharkeya339cba2012-05-08 11:42:49 -0700345 ManageApplications.class.getName().equals(fragmentName) ||
346 WirelessSettings.class.getName().equals(fragmentName) ||
347 SoundSettings.class.getName().equals(fragmentName) ||
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700348 PrivacySettings.class.getName().equals(fragmentName) ||
349 ManageAccountsSettings.class.getName().equals(fragmentName)) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700350 intent.putExtra(EXTRA_CLEAR_UI_OPTIONS, true);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700351 }
352
Dianne Hackbornb7258182011-03-15 16:23:55 -0700353 intent.setClass(this, SubSettings.class);
354 return intent;
355 }
356
Amith Yamasani379d9b02010-09-27 12:03:59 -0700357 /**
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700358 * Populate the activity with the top-level headers.
359 */
Amith Yamasanid7993472010-08-18 13:59:28 -0700360 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700361 public void onBuildHeaders(List<Header> headers) {
362 loadHeadersFromResource(R.xml.settings_headers, headers);
Amith Yamasanid7993472010-08-18 13:59:28 -0700363
Gilles Debunnee78c1872011-06-20 15:00:07 -0700364 updateHeaderList(headers);
365
366 mHeaders = headers;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700367 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700368
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700369 private void updateHeaderList(List<Header> target) {
370 int i = 0;
371 while (i < target.size()) {
372 Header header = target.get(i);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700373 // Ids are integers, so downcasting
374 int id = (int) header.id;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700375 if (id == R.id.dock_settings) {
376 if (!needsDockSettings())
377 target.remove(header);
378 } else if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
379 Utils.updateHeaderToSpecificActivityFromMetaDataOrRemove(this, target, header);
Gilles Debunne2454f492011-06-21 16:16:06 -0700380 } else if (id == R.id.wifi_settings) {
381 // Remove WiFi Settings if WiFi service is not available.
382 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
383 target.remove(header);
384 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700385 } else if (id == R.id.bluetooth_settings) {
386 // Remove Bluetooth Settings if Bluetooth service is not available.
Gilles Debunne2454f492011-06-21 16:16:06 -0700387 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700388 target.remove(header);
389 }
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700390 } else if (id == R.id.data_usage_settings) {
391 // Remove data usage when kernel module not enabled
392 final INetworkManagementService netManager = INetworkManagementService.Stub
393 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
394 try {
395 if (!netManager.isBandwidthControlEnabled()) {
396 target.remove(header);
397 }
398 } catch (RemoteException e) {
399 // ignored
400 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700401 } else if (id == R.id.account_settings) {
402 int headerIndex = i + 1;
403 i = insertAccountsHeaders(target, headerIndex);
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700404 } else if (id == R.id.user_settings) {
Amith Yamasanifa3eea52012-03-28 10:25:08 -0700405 if (!mEnableUserManagement
406 || !UserId.MU_ENABLED || UserId.myUserId() != 0
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700407 || !getResources().getBoolean(R.bool.enable_user_management)
408 || Utils.isMonkeyRunning()) {
409 target.remove(header);
410 }
411 }
412 if (UserId.MU_ENABLED && UserId.myUserId() != 0
413 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
414 target.remove(header);
Amith Yamasanid7993472010-08-18 13:59:28 -0700415 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700416
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700417 // Increment if the current one wasn't removed by the Utils code.
418 if (target.get(i) == header) {
Amith Yamasani3965ae62010-11-15 14:45:19 -0800419 // Hold on to the first header, when we need to reset to the top-level
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700420 if (mFirstHeader == null &&
421 HeaderAdapter.getHeaderType(header) != HeaderAdapter.HEADER_TYPE_CATEGORY) {
422 mFirstHeader = header;
423 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700424 mHeaderIndexMap.put(id, i);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700425 i++;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700426 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700427 }
428 }
429
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700430 private int insertAccountsHeaders(List<Header> target, int headerIndex) {
431 String[] accountTypes = mAuthenticatorHelper.getEnabledAccountTypes();
432 List<Header> accountHeaders = new ArrayList<Header>(accountTypes.length);
433 for (String accountType : accountTypes) {
434 CharSequence label = mAuthenticatorHelper.getLabelForType(this, accountType);
435 Header accHeader = new Header();
436 accHeader.title = label;
437 if (accHeader.extras == null) {
438 accHeader.extras = new Bundle();
439 }
440 accHeader.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
441 accHeader.breadCrumbTitle = label;
442 accHeader.breadCrumbShortTitle = label;
443 accHeader.fragment = ManageAccountsSettings.class.getName();
444 accHeader.fragmentArguments = new Bundle();
445 accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE,
446 accountType);
447 if (!isMultiPane()) {
448 accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_LABEL,
449 label.toString());
450 }
451 accountHeaders.add(accHeader);
452 }
453
454 // Sort by label
455 Collections.sort(accountHeaders, new Comparator<Header>() {
456 @Override
457 public int compare(Header h1, Header h2) {
458 return h1.title.toString().compareTo(h2.title.toString());
459 }
460 });
461
462 for (Header header : accountHeaders) {
463 target.add(headerIndex++, header);
464 }
465 return headerIndex;
466 }
467
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700468 private boolean needsDockSettings() {
469 return getResources().getBoolean(R.bool.has_dock_settings);
Amith Yamasanib61cf512010-09-12 08:17:50 -0700470 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700471
472 private void getMetaData() {
473 try {
474 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
475 PackageManager.GET_META_DATA);
476 if (ai == null || ai.metaData == null) return;
477 mTopLevelHeaderId = ai.metaData.getInt(META_DATA_KEY_HEADER_ID);
478 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700479
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800480 // Check if it has a parent specified and create a Header object
481 final int parentHeaderTitleRes = ai.metaData.getInt(META_DATA_KEY_PARENT_TITLE);
482 String parentFragmentClass = ai.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
483 if (parentFragmentClass != null) {
484 mParentHeader = new Header();
485 mParentHeader.fragment = parentFragmentClass;
486 if (parentHeaderTitleRes != 0) {
487 mParentHeader.title = getResources().getString(parentHeaderTitleRes);
488 }
489 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700490 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700491 // No recovery
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700492 }
493 }
494
Amith Yamasani9e3a4702011-01-11 09:09:26 -0800495 @Override
496 public boolean hasNextButton() {
497 return super.hasNextButton();
498 }
499
500 @Override
501 public Button getNextButton() {
502 return super.getNextButton();
503 }
504
Gilles Debunnee78c1872011-06-20 15:00:07 -0700505 private static class HeaderAdapter extends ArrayAdapter<Header> {
506 static final int HEADER_TYPE_CATEGORY = 0;
507 static final int HEADER_TYPE_NORMAL = 1;
508 static final int HEADER_TYPE_SWITCH = 2;
509 private static final int HEADER_TYPE_COUNT = HEADER_TYPE_SWITCH + 1;
510
511 private final WifiEnabler mWifiEnabler;
512 private final BluetoothEnabler mBluetoothEnabler;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700513 private AuthenticatorHelper mAuthHelper;
Gilles Debunnee78c1872011-06-20 15:00:07 -0700514
515 private static class HeaderViewHolder {
516 ImageView icon;
517 TextView title;
518 TextView summary;
519 Switch switch_;
520 }
521
522 private LayoutInflater mInflater;
523
524 static int getHeaderType(Header header) {
525 if (header.fragment == null && header.intent == null) {
526 return HEADER_TYPE_CATEGORY;
527 } else if (header.id == R.id.wifi_settings || header.id == R.id.bluetooth_settings) {
528 return HEADER_TYPE_SWITCH;
529 } else {
530 return HEADER_TYPE_NORMAL;
531 }
532 }
533
534 @Override
535 public int getItemViewType(int position) {
536 Header header = getItem(position);
537 return getHeaderType(header);
538 }
539
540 @Override
541 public boolean areAllItemsEnabled() {
542 return false; // because of categories
543 }
544
545 @Override
546 public boolean isEnabled(int position) {
547 return getItemViewType(position) != HEADER_TYPE_CATEGORY;
548 }
549
550 @Override
551 public int getViewTypeCount() {
552 return HEADER_TYPE_COUNT;
553 }
554
555 @Override
556 public boolean hasStableIds() {
557 return true;
558 }
559
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700560 public HeaderAdapter(Context context, List<Header> objects,
561 AuthenticatorHelper authenticatorHelper) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700562 super(context, 0, objects);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700563
564 mAuthHelper = authenticatorHelper;
Gilles Debunnee78c1872011-06-20 15:00:07 -0700565 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700566
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700567 // Temp Switches provided as placeholder until the adapter replaces these with actual
Gilles Debunnee78c1872011-06-20 15:00:07 -0700568 // Switches inflated from their layouts. Must be done before adapter is set in super
569 mWifiEnabler = new WifiEnabler(context, new Switch(context));
570 mBluetoothEnabler = new BluetoothEnabler(context, new Switch(context));
571 }
572
573 @Override
574 public View getView(int position, View convertView, ViewGroup parent) {
575 HeaderViewHolder holder;
576 Header header = getItem(position);
577 int headerType = getHeaderType(header);
578 View view = null;
579
580 if (convertView == null) {
581 holder = new HeaderViewHolder();
582 switch (headerType) {
583 case HEADER_TYPE_CATEGORY:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700584 view = new TextView(getContext(), null,
585 android.R.attr.listSeparatorTextViewStyle);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700586 holder.title = (TextView) view;
587 break;
588
589 case HEADER_TYPE_SWITCH:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700590 view = mInflater.inflate(R.layout.preference_header_switch_item, parent,
591 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700592 holder.icon = (ImageView) view.findViewById(R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700593 holder.title = (TextView)
594 view.findViewById(com.android.internal.R.id.title);
595 holder.summary = (TextView)
596 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700597 holder.switch_ = (Switch) view.findViewById(R.id.switchWidget);
598 break;
599
600 case HEADER_TYPE_NORMAL:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700601 view = mInflater.inflate(
602 com.android.internal.R.layout.preference_header_item, parent,
603 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700604 holder.icon = (ImageView) view.findViewById(com.android.internal.R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700605 holder.title = (TextView)
606 view.findViewById(com.android.internal.R.id.title);
607 holder.summary = (TextView)
608 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700609 break;
610 }
611 view.setTag(holder);
612 } else {
613 view = convertView;
614 holder = (HeaderViewHolder) view.getTag();
615 }
616
617 // All view fields must be updated every time, because the view may be recycled
618 switch (headerType) {
619 case HEADER_TYPE_CATEGORY:
620 holder.title.setText(header.getTitle(getContext().getResources()));
621 break;
622
623 case HEADER_TYPE_SWITCH:
624 // Would need a different treatment if the main menu had more switches
625 if (header.id == R.id.wifi_settings) {
626 mWifiEnabler.setSwitch(holder.switch_);
627 } else {
628 mBluetoothEnabler.setSwitch(holder.switch_);
629 }
630 // No break, fall through on purpose to update common fields
631
632 //$FALL-THROUGH$
633 case HEADER_TYPE_NORMAL:
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700634 if (header.extras != null && header.extras.containsKey(
635 ManageAccountsSettings.KEY_ACCOUNT_TYPE)) {
636 String accType = header.extras.getString(
637 ManageAccountsSettings.KEY_ACCOUNT_TYPE);
638 ViewGroup.LayoutParams lp = holder.icon.getLayoutParams();
639 lp.width = getContext().getResources().getDimensionPixelSize(
640 R.dimen.header_icon_width);
641 lp.height = lp.width;
642 holder.icon.setLayoutParams(lp);
643 Drawable icon = mAuthHelper.getDrawableForType(getContext(), accType);
644 holder.icon.setImageDrawable(icon);
645 } else {
646 holder.icon.setImageResource(header.iconRes);
647 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700648 holder.title.setText(header.getTitle(getContext().getResources()));
649 CharSequence summary = header.getSummary(getContext().getResources());
650 if (!TextUtils.isEmpty(summary)) {
651 holder.summary.setVisibility(View.VISIBLE);
652 holder.summary.setText(summary);
653 } else {
654 holder.summary.setVisibility(View.GONE);
655 }
656 break;
657 }
658
659 return view;
660 }
661
662 public void resume() {
663 mWifiEnabler.resume();
664 mBluetoothEnabler.resume();
665 }
666
667 public void pause() {
668 mWifiEnabler.pause();
669 mBluetoothEnabler.pause();
670 }
671 }
672
673 @Override
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700674 public void onHeaderClick(Header header, int position) {
675 boolean revert = false;
676 if (header.id == R.id.account_add) {
677 revert = true;
678 }
679
680 super.onHeaderClick(header, position);
681
682 if (revert && mLastHeader != null) {
683 highlightHeader((int) mLastHeader.id);
684 } else {
685 mLastHeader = header;
686 }
687 }
688
689 @Override
Amith Yamasania4379d62011-07-22 10:34:58 -0700690 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
691 // Override the fragment title for Wallpaper settings
Amith Yamasanidfb65432011-11-29 16:38:14 -0800692 int titleRes = pref.getTitleRes();
Amith Yamasania4379d62011-07-22 10:34:58 -0700693 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
Amith Yamasanidfb65432011-11-29 16:38:14 -0800694 titleRes = R.string.wallpaper_settings_fragment_title;
Amith Yamasania4379d62011-07-22 10:34:58 -0700695 }
Jean Chalard7dabe452012-05-10 18:08:07 +0900696 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
697 null, 0);
Amith Yamasania4379d62011-07-22 10:34:58 -0700698 return true;
699 }
700
Amith Yamasani3d384f42012-05-11 15:22:04 -0700701 public boolean shouldUpRecreateTask(Intent targetIntent) {
702 return super.shouldUpRecreateTask(new Intent(this, Settings.class));
703 }
704
Amith Yamasania4379d62011-07-22 10:34:58 -0700705 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700706 public void setListAdapter(ListAdapter adapter) {
707 if (mHeaders == null) {
708 mHeaders = new ArrayList<Header>();
709 // When the saved state provides the list of headers, onBuildHeaders is not called
710 // Copy the list of Headers from the adapter, preserving their order
711 for (int i = 0; i < adapter.getCount(); i++) {
712 mHeaders.add((Header) adapter.getItem(i));
713 }
714 }
715
716 // Ignore the adapter provided by PreferenceActivity and substitute ours instead
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700717 super.setListAdapter(new HeaderAdapter(this, mHeaders, mAuthenticatorHelper));
Gilles Debunnee78c1872011-06-20 15:00:07 -0700718 }
719
Amith Yamasani56821db2012-06-05 13:20:45 -0700720 @Override
721 public void onAccountsUpdated(Account[] accounts) {
722 mAuthenticatorHelper.onAccountsUpdated(this, accounts);
723 invalidateHeaders();
724 }
725
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700726 /*
727 * Settings subclasses for launching independently.
728 */
Gilles Debunnee78c1872011-06-20 15:00:07 -0700729 public static class BluetoothSettingsActivity extends Settings { /* empty */ }
730 public static class WirelessSettingsActivity extends Settings { /* empty */ }
731 public static class TetherSettingsActivity extends Settings { /* empty */ }
732 public static class VpnSettingsActivity extends Settings { /* empty */ }
733 public static class DateTimeSettingsActivity extends Settings { /* empty */ }
734 public static class StorageSettingsActivity extends Settings { /* empty */ }
735 public static class WifiSettingsActivity extends Settings { /* empty */ }
repo syncb98463f2011-06-30 10:58:43 -0700736 public static class WifiP2pSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700737 public static class InputMethodAndLanguageSettingsActivity extends Settings { /* empty */ }
Jeff Browne46c5f32012-04-05 11:42:18 -0700738 public static class KeyboardLayoutPickerActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700739 public static class InputMethodAndSubtypeEnablerActivity extends Settings { /* empty */ }
satoke077d2b2011-07-25 09:38:11 +0900740 public static class SpellCheckersSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700741 public static class LocalePickerActivity extends Settings { /* empty */ }
742 public static class UserDictionarySettingsActivity extends Settings { /* empty */ }
743 public static class SoundSettingsActivity extends Settings { /* empty */ }
744 public static class DisplaySettingsActivity extends Settings { /* empty */ }
745 public static class DeviceInfoSettingsActivity extends Settings { /* empty */ }
746 public static class ApplicationSettingsActivity extends Settings { /* empty */ }
747 public static class ManageApplicationsActivity extends Settings { /* empty */ }
748 public static class StorageUseActivity extends Settings { /* empty */ }
749 public static class DevelopmentSettingsActivity extends Settings { /* empty */ }
750 public static class AccessibilitySettingsActivity extends Settings { /* empty */ }
751 public static class SecuritySettingsActivity extends Settings { /* empty */ }
Gilles Debunnea6a8a142011-06-09 11:56:17 -0700752 public static class LocationSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700753 public static class PrivacySettingsActivity extends Settings { /* empty */ }
754 public static class DockSettingsActivity extends Settings { /* empty */ }
755 public static class RunningServicesActivity extends Settings { /* empty */ }
756 public static class ManageAccountsSettingsActivity extends Settings { /* empty */ }
757 public static class PowerUsageSummaryActivity extends Settings { /* empty */ }
758 public static class AccountSyncSettingsActivity extends Settings { /* empty */ }
759 public static class AccountSyncSettingsInAddAccountActivity extends Settings { /* empty */ }
760 public static class CryptKeeperSettingsActivity extends Settings { /* empty */ }
761 public static class DeviceAdminSettingsActivity extends Settings { /* empty */ }
762 public static class DataUsageSummaryActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700763 public static class AdvancedWifiSettingsActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700764 public static class TextToSpeechSettingsActivity extends Settings { /* empty */ }
Jeff Hamilton3d670de2011-09-21 16:44:36 -0500765 public static class AndroidBeamSettingsActivity extends Settings { /* empty */ }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800766}