blob: 50019a33940e346ca643500a5597390abf85a7af [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;
Amith Yamasani86708a82012-06-06 20:23:08 -0700115 private boolean mListeningToAccountUpdates;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700116
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700117 @Override
118 protected void onCreate(Bundle savedInstanceState) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700119 if (getIntent().getBooleanExtra(EXTRA_CLEAR_UI_OPTIONS, false)) {
120 getWindow().setUiOptions(0);
121 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700122
Amith Yamasanifa3eea52012-03-28 10:25:08 -0700123 if (android.provider.Settings.Secure.getInt(getContentResolver(), "multiuser_enabled", -1)
124 > 0) {
125 mEnableUserManagement = true;
126 }
127
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700128 mAuthenticatorHelper = new AuthenticatorHelper();
129 mAuthenticatorHelper.updateAuthDescriptions(this);
130 mAuthenticatorHelper.onAccountsUpdated(this, null);
131
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700132 getMetaData();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800133 mInLocalHeaderSwitch = true;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700134 super.onCreate(savedInstanceState);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800135 mInLocalHeaderSwitch = false;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700136
Gilles Debunne3661b622011-06-27 11:19:16 -0700137 if (!onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700138 highlightHeader(mTopLevelHeaderId);
Amith Yamasani72aa19d2010-12-09 06:07:12 -0800139 // Force the title so that it doesn't get overridden by a direct launch of
140 // a specific settings screen.
141 setTitle(R.string.settings_label);
142 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800143
144 // Retrieve any saved state
145 if (savedInstanceState != null) {
146 mCurrentHeader = savedInstanceState.getParcelable(SAVE_KEY_CURRENT_HEADER);
147 mParentHeader = savedInstanceState.getParcelable(SAVE_KEY_PARENT_HEADER);
148 }
149
150 // If the current header was saved, switch to it
151 if (savedInstanceState != null && mCurrentHeader != null) {
152 //switchToHeaderLocal(mCurrentHeader);
153 showBreadCrumbs(mCurrentHeader.title, null);
154 }
155
156 if (mParentHeader != null) {
157 setParentTitle(mParentHeader.title, null, new OnClickListener() {
158 public void onClick(View v) {
159 switchToParent(mParentHeader.fragment);
160 }
161 });
162 }
Gilles Debunnedc7101f2011-06-27 12:00:49 -0700163
Amith Yamasani3d384f42012-05-11 15:22:04 -0700164 // Override up navigation for multi-pane, since we handle it in the fragment breadcrumbs
165 if (onIsMultiPane()) {
166 getActionBar().setDisplayHomeAsUpEnabled(false);
167 getActionBar().setHomeButtonEnabled(false);
168 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800169 }
170
171 @Override
172 protected void onSaveInstanceState(Bundle outState) {
173 super.onSaveInstanceState(outState);
174
175 // Save the current fragment, if it is the same as originally launched
176 if (mCurrentHeader != null) {
177 outState.putParcelable(SAVE_KEY_CURRENT_HEADER, mCurrentHeader);
178 }
179 if (mParentHeader != null) {
180 outState.putParcelable(SAVE_KEY_PARENT_HEADER, mParentHeader);
181 }
182 }
183
Gilles Debunnee78c1872011-06-20 15:00:07 -0700184 @Override
185 public void onResume() {
186 super.onResume();
187
188 ListAdapter listAdapter = getListAdapter();
189 if (listAdapter instanceof HeaderAdapter) {
190 ((HeaderAdapter) listAdapter).resume();
191 }
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 Yamasani86708a82012-06-06 20:23:08 -0700201 }
202 }
203
204 @Override
205 public void onDestroy() {
206 super.onDestroy();
207 if (mListeningToAccountUpdates) {
Amith Yamasani56821db2012-06-05 13:20:45 -0700208 AccountManager.get(this).removeOnAccountsUpdatedListener(this);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700209 }
210 }
211
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800212 private void switchToHeaderLocal(Header header) {
213 mInLocalHeaderSwitch = true;
214 switchToHeader(header);
215 mInLocalHeaderSwitch = false;
216 }
217
218 @Override
219 public void switchToHeader(Header header) {
220 if (!mInLocalHeaderSwitch) {
221 mCurrentHeader = null;
222 mParentHeader = null;
223 }
224 super.switchToHeader(header);
225 }
226
227 /**
228 * Switch to parent fragment and store the grand parent's info
Jake Hamby2748fc22011-01-12 15:06:28 -0800229 * @param className name of the activity wrapper for the parent fragment.
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800230 */
231 private void switchToParent(String className) {
232 final ComponentName cn = new ComponentName(this, className);
233 try {
234 final PackageManager pm = getPackageManager();
235 final ActivityInfo parentInfo = pm.getActivityInfo(cn, PackageManager.GET_META_DATA);
236
237 if (parentInfo != null && parentInfo.metaData != null) {
238 String fragmentClass = parentInfo.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
239 CharSequence fragmentTitle = parentInfo.loadLabel(pm);
240 Header parentHeader = new Header();
241 parentHeader.fragment = fragmentClass;
242 parentHeader.title = fragmentTitle;
243 mCurrentHeader = parentHeader;
244
245 switchToHeaderLocal(parentHeader);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700246 highlightHeader(mTopLevelHeaderId);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800247
248 mParentHeader = new Header();
249 mParentHeader.fragment
250 = parentInfo.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
251 mParentHeader.title = parentInfo.metaData.getString(META_DATA_KEY_PARENT_TITLE);
252 }
253 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700254 Log.w(LOG_TAG, "Could not find parent activity : " + className);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800255 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700256 }
257
Amith Yamasani3965ae62010-11-15 14:45:19 -0800258 @Override
259 public void onNewIntent(Intent intent) {
260 super.onNewIntent(intent);
261
262 // If it is not launched from history, then reset to top-level
263 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0
Gilles Debunne3661b622011-06-27 11:19:16 -0700264 && mFirstHeader != null && !onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800265 switchToHeaderLocal(mFirstHeader);
Amith Yamasani3965ae62010-11-15 14:45:19 -0800266 }
267 }
268
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700269 private void highlightHeader(int id) {
270 if (id != 0) {
271 Integer index = mHeaderIndexMap.get(id);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700272 if (index != null) {
273 getListView().setItemChecked(index, true);
Amith Yamasani990fb522011-09-02 09:47:18 -0700274 getListView().smoothScrollToPosition(index);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700275 }
276 }
277 }
278
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700279 @Override
280 public Intent getIntent() {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700281 Intent superIntent = super.getIntent();
282 String startingFragment = getStartingFragmentClass(superIntent);
Gilles Debunne3661b622011-06-27 11:19:16 -0700283 // This is called from super.onCreate, isMultiPane() is not yet reliable
284 // Do not use onIsHidingHeaders either, which relies itself on this method
285 if (startingFragment != null && !onIsMultiPane()) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700286 Intent modIntent = new Intent(superIntent);
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700287 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700288 Bundle args = superIntent.getExtras();
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700289 if (args != null) {
290 args = new Bundle(args);
291 } else {
292 args = new Bundle();
293 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700294 args.putParcelable("intent", superIntent);
295 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras());
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700296 return modIntent;
297 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700298 return superIntent;
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700299 }
300
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700301 /**
Amith Yamasani379d9b02010-09-27 12:03:59 -0700302 * Checks if the component name in the intent is different from the Settings class and
303 * returns the class name to load as a fragment.
304 */
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700305 protected String getStartingFragmentClass(Intent intent) {
306 if (mFragmentClass != null) return mFragmentClass;
307
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700308 String intentClass = intent.getComponent().getClassName();
Amith Yamasani379d9b02010-09-27 12:03:59 -0700309 if (intentClass.equals(getClass().getName())) return null;
310
Dianne Hackbornee293792010-11-01 12:32:33 -0700311 if ("com.android.settings.ManageApplications".equals(intentClass)
312 || "com.android.settings.RunningServices".equals(intentClass)
313 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700314 // Old names of manage apps.
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700315 intentClass = com.android.settings.applications.ManageApplications.class.getName();
316 }
317
Amith Yamasani379d9b02010-09-27 12:03:59 -0700318 return intentClass;
319 }
320
321 /**
322 * Override initial header when an activity-alias is causing Settings to be launched
323 * for a specific fragment encoded in the android:name parameter.
324 */
325 @Override
326 public Header onGetInitialHeader() {
327 String fragmentClass = getStartingFragmentClass(super.getIntent());
328 if (fragmentClass != null) {
329 Header header = new Header();
330 header.fragment = fragmentClass;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800331 header.title = getTitle();
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700332 header.fragmentArguments = getIntent().getExtras();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800333 mCurrentHeader = header;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700334 return header;
335 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700336
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700337 return mFirstHeader;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700338 }
339
Dianne Hackbornb7258182011-03-15 16:23:55 -0700340 @Override
Dianne Hackborn48147dc2011-03-18 12:29:41 -0700341 public Intent onBuildStartFragmentIntent(String fragmentName, Bundle args,
342 int titleRes, int shortTitleRes) {
343 Intent intent = super.onBuildStartFragmentIntent(fragmentName, args,
344 titleRes, shortTitleRes);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700345
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700346 // some fragments want to avoid split actionbar
Gilles Debunne162e5412011-07-11 14:13:31 -0700347 if (DataUsageSummary.class.getName().equals(fragmentName) ||
348 PowerUsageSummary.class.getName().equals(fragmentName) ||
Gilles Debunnecd8e5242011-07-25 11:36:15 -0700349 AccountSyncSettings.class.getName().equals(fragmentName) ||
Jeff Sharkey11d30122012-03-19 16:54:07 -0700350 UserDictionarySettings.class.getName().equals(fragmentName) ||
Adam Powellfaba7e42012-03-26 17:28:38 -0700351 Memory.class.getName().equals(fragmentName) ||
Jeff Sharkeya339cba2012-05-08 11:42:49 -0700352 ManageApplications.class.getName().equals(fragmentName) ||
353 WirelessSettings.class.getName().equals(fragmentName) ||
354 SoundSettings.class.getName().equals(fragmentName) ||
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700355 PrivacySettings.class.getName().equals(fragmentName) ||
356 ManageAccountsSettings.class.getName().equals(fragmentName)) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700357 intent.putExtra(EXTRA_CLEAR_UI_OPTIONS, true);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700358 }
359
Dianne Hackbornb7258182011-03-15 16:23:55 -0700360 intent.setClass(this, SubSettings.class);
361 return intent;
362 }
363
Amith Yamasani379d9b02010-09-27 12:03:59 -0700364 /**
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700365 * Populate the activity with the top-level headers.
366 */
Amith Yamasanid7993472010-08-18 13:59:28 -0700367 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700368 public void onBuildHeaders(List<Header> headers) {
369 loadHeadersFromResource(R.xml.settings_headers, headers);
Amith Yamasanid7993472010-08-18 13:59:28 -0700370
Gilles Debunnee78c1872011-06-20 15:00:07 -0700371 updateHeaderList(headers);
372
373 mHeaders = 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);
442 Header accHeader = new Header();
443 accHeader.title = label;
444 if (accHeader.extras == null) {
445 accHeader.extras = new Bundle();
446 }
447 accHeader.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
448 accHeader.breadCrumbTitle = label;
449 accHeader.breadCrumbShortTitle = label;
450 accHeader.fragment = ManageAccountsSettings.class.getName();
451 accHeader.fragmentArguments = new Bundle();
452 accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE,
453 accountType);
454 if (!isMultiPane()) {
455 accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_LABEL,
456 label.toString());
457 }
458 accountHeaders.add(accHeader);
459 }
460
461 // Sort by label
462 Collections.sort(accountHeaders, new Comparator<Header>() {
463 @Override
464 public int compare(Header h1, Header h2) {
465 return h1.title.toString().compareTo(h2.title.toString());
466 }
467 });
468
469 for (Header header : accountHeaders) {
470 target.add(headerIndex++, header);
471 }
Amith Yamasani86708a82012-06-06 20:23:08 -0700472 if (!mListeningToAccountUpdates) {
473 AccountManager.get(this).addOnAccountsUpdatedListener(this, null, true);
474 mListeningToAccountUpdates = true;
475 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700476 return headerIndex;
477 }
478
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700479 private boolean needsDockSettings() {
480 return getResources().getBoolean(R.bool.has_dock_settings);
Amith Yamasanib61cf512010-09-12 08:17:50 -0700481 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700482
483 private void getMetaData() {
484 try {
485 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
486 PackageManager.GET_META_DATA);
487 if (ai == null || ai.metaData == null) return;
488 mTopLevelHeaderId = ai.metaData.getInt(META_DATA_KEY_HEADER_ID);
489 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700490
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800491 // Check if it has a parent specified and create a Header object
492 final int parentHeaderTitleRes = ai.metaData.getInt(META_DATA_KEY_PARENT_TITLE);
493 String parentFragmentClass = ai.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
494 if (parentFragmentClass != null) {
495 mParentHeader = new Header();
496 mParentHeader.fragment = parentFragmentClass;
497 if (parentHeaderTitleRes != 0) {
498 mParentHeader.title = getResources().getString(parentHeaderTitleRes);
499 }
500 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700501 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700502 // No recovery
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700503 }
504 }
505
Amith Yamasani9e3a4702011-01-11 09:09:26 -0800506 @Override
507 public boolean hasNextButton() {
508 return super.hasNextButton();
509 }
510
511 @Override
512 public Button getNextButton() {
513 return super.getNextButton();
514 }
515
Gilles Debunnee78c1872011-06-20 15:00:07 -0700516 private static class HeaderAdapter extends ArrayAdapter<Header> {
517 static final int HEADER_TYPE_CATEGORY = 0;
518 static final int HEADER_TYPE_NORMAL = 1;
519 static final int HEADER_TYPE_SWITCH = 2;
520 private static final int HEADER_TYPE_COUNT = HEADER_TYPE_SWITCH + 1;
521
522 private final WifiEnabler mWifiEnabler;
523 private final BluetoothEnabler mBluetoothEnabler;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700524 private AuthenticatorHelper mAuthHelper;
Gilles Debunnee78c1872011-06-20 15:00:07 -0700525
526 private static class HeaderViewHolder {
527 ImageView icon;
528 TextView title;
529 TextView summary;
530 Switch switch_;
531 }
532
533 private LayoutInflater mInflater;
534
535 static int getHeaderType(Header header) {
536 if (header.fragment == null && header.intent == null) {
537 return HEADER_TYPE_CATEGORY;
538 } else if (header.id == R.id.wifi_settings || header.id == R.id.bluetooth_settings) {
539 return HEADER_TYPE_SWITCH;
540 } else {
541 return HEADER_TYPE_NORMAL;
542 }
543 }
544
545 @Override
546 public int getItemViewType(int position) {
547 Header header = getItem(position);
548 return getHeaderType(header);
549 }
550
551 @Override
552 public boolean areAllItemsEnabled() {
553 return false; // because of categories
554 }
555
556 @Override
557 public boolean isEnabled(int position) {
558 return getItemViewType(position) != HEADER_TYPE_CATEGORY;
559 }
560
561 @Override
562 public int getViewTypeCount() {
563 return HEADER_TYPE_COUNT;
564 }
565
566 @Override
567 public boolean hasStableIds() {
568 return true;
569 }
570
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700571 public HeaderAdapter(Context context, List<Header> objects,
572 AuthenticatorHelper authenticatorHelper) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700573 super(context, 0, objects);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700574
575 mAuthHelper = authenticatorHelper;
Gilles Debunnee78c1872011-06-20 15:00:07 -0700576 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700577
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700578 // Temp Switches provided as placeholder until the adapter replaces these with actual
Gilles Debunnee78c1872011-06-20 15:00:07 -0700579 // Switches inflated from their layouts. Must be done before adapter is set in super
580 mWifiEnabler = new WifiEnabler(context, new Switch(context));
581 mBluetoothEnabler = new BluetoothEnabler(context, new Switch(context));
582 }
583
584 @Override
585 public View getView(int position, View convertView, ViewGroup parent) {
586 HeaderViewHolder holder;
587 Header header = getItem(position);
588 int headerType = getHeaderType(header);
589 View view = null;
590
591 if (convertView == null) {
592 holder = new HeaderViewHolder();
593 switch (headerType) {
594 case HEADER_TYPE_CATEGORY:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700595 view = new TextView(getContext(), null,
596 android.R.attr.listSeparatorTextViewStyle);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700597 holder.title = (TextView) view;
598 break;
599
600 case HEADER_TYPE_SWITCH:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700601 view = mInflater.inflate(R.layout.preference_header_switch_item, parent,
602 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700603 holder.icon = (ImageView) view.findViewById(R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700604 holder.title = (TextView)
605 view.findViewById(com.android.internal.R.id.title);
606 holder.summary = (TextView)
607 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700608 holder.switch_ = (Switch) view.findViewById(R.id.switchWidget);
609 break;
610
611 case HEADER_TYPE_NORMAL:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700612 view = mInflater.inflate(
613 com.android.internal.R.layout.preference_header_item, parent,
614 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700615 holder.icon = (ImageView) view.findViewById(com.android.internal.R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700616 holder.title = (TextView)
617 view.findViewById(com.android.internal.R.id.title);
618 holder.summary = (TextView)
619 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700620 break;
621 }
622 view.setTag(holder);
623 } else {
624 view = convertView;
625 holder = (HeaderViewHolder) view.getTag();
626 }
627
628 // All view fields must be updated every time, because the view may be recycled
629 switch (headerType) {
630 case HEADER_TYPE_CATEGORY:
631 holder.title.setText(header.getTitle(getContext().getResources()));
632 break;
633
634 case HEADER_TYPE_SWITCH:
635 // Would need a different treatment if the main menu had more switches
636 if (header.id == R.id.wifi_settings) {
637 mWifiEnabler.setSwitch(holder.switch_);
638 } else {
639 mBluetoothEnabler.setSwitch(holder.switch_);
640 }
641 // No break, fall through on purpose to update common fields
642
643 //$FALL-THROUGH$
644 case HEADER_TYPE_NORMAL:
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700645 if (header.extras != null && header.extras.containsKey(
646 ManageAccountsSettings.KEY_ACCOUNT_TYPE)) {
647 String accType = header.extras.getString(
648 ManageAccountsSettings.KEY_ACCOUNT_TYPE);
649 ViewGroup.LayoutParams lp = holder.icon.getLayoutParams();
650 lp.width = getContext().getResources().getDimensionPixelSize(
651 R.dimen.header_icon_width);
652 lp.height = lp.width;
653 holder.icon.setLayoutParams(lp);
654 Drawable icon = mAuthHelper.getDrawableForType(getContext(), accType);
655 holder.icon.setImageDrawable(icon);
656 } else {
657 holder.icon.setImageResource(header.iconRes);
658 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700659 holder.title.setText(header.getTitle(getContext().getResources()));
660 CharSequence summary = header.getSummary(getContext().getResources());
661 if (!TextUtils.isEmpty(summary)) {
662 holder.summary.setVisibility(View.VISIBLE);
663 holder.summary.setText(summary);
664 } else {
665 holder.summary.setVisibility(View.GONE);
666 }
667 break;
668 }
669
670 return view;
671 }
672
673 public void resume() {
674 mWifiEnabler.resume();
675 mBluetoothEnabler.resume();
676 }
677
678 public void pause() {
679 mWifiEnabler.pause();
680 mBluetoothEnabler.pause();
681 }
682 }
683
684 @Override
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700685 public void onHeaderClick(Header header, int position) {
686 boolean revert = false;
687 if (header.id == R.id.account_add) {
688 revert = true;
689 }
690
691 super.onHeaderClick(header, position);
692
693 if (revert && mLastHeader != null) {
694 highlightHeader((int) mLastHeader.id);
695 } else {
696 mLastHeader = header;
697 }
698 }
699
700 @Override
Amith Yamasania4379d62011-07-22 10:34:58 -0700701 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
702 // Override the fragment title for Wallpaper settings
Amith Yamasanidfb65432011-11-29 16:38:14 -0800703 int titleRes = pref.getTitleRes();
Amith Yamasania4379d62011-07-22 10:34:58 -0700704 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
Amith Yamasanidfb65432011-11-29 16:38:14 -0800705 titleRes = R.string.wallpaper_settings_fragment_title;
Amith Yamasania4379d62011-07-22 10:34:58 -0700706 }
Jean Chalard7dabe452012-05-10 18:08:07 +0900707 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
708 null, 0);
Amith Yamasania4379d62011-07-22 10:34:58 -0700709 return true;
710 }
711
Amith Yamasani3d384f42012-05-11 15:22:04 -0700712 public boolean shouldUpRecreateTask(Intent targetIntent) {
713 return super.shouldUpRecreateTask(new Intent(this, Settings.class));
714 }
715
Amith Yamasania4379d62011-07-22 10:34:58 -0700716 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700717 public void setListAdapter(ListAdapter adapter) {
718 if (mHeaders == null) {
719 mHeaders = new ArrayList<Header>();
720 // When the saved state provides the list of headers, onBuildHeaders is not called
721 // Copy the list of Headers from the adapter, preserving their order
722 for (int i = 0; i < adapter.getCount(); i++) {
723 mHeaders.add((Header) adapter.getItem(i));
724 }
725 }
726
727 // Ignore the adapter provided by PreferenceActivity and substitute ours instead
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700728 super.setListAdapter(new HeaderAdapter(this, mHeaders, mAuthenticatorHelper));
Gilles Debunnee78c1872011-06-20 15:00:07 -0700729 }
730
Amith Yamasani56821db2012-06-05 13:20:45 -0700731 @Override
732 public void onAccountsUpdated(Account[] accounts) {
733 mAuthenticatorHelper.onAccountsUpdated(this, accounts);
734 invalidateHeaders();
735 }
736
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700737 /*
738 * Settings subclasses for launching independently.
739 */
Gilles Debunnee78c1872011-06-20 15:00:07 -0700740 public static class BluetoothSettingsActivity extends Settings { /* empty */ }
741 public static class WirelessSettingsActivity extends Settings { /* empty */ }
742 public static class TetherSettingsActivity extends Settings { /* empty */ }
743 public static class VpnSettingsActivity extends Settings { /* empty */ }
744 public static class DateTimeSettingsActivity extends Settings { /* empty */ }
745 public static class StorageSettingsActivity extends Settings { /* empty */ }
746 public static class WifiSettingsActivity extends Settings { /* empty */ }
repo syncb98463f2011-06-30 10:58:43 -0700747 public static class WifiP2pSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700748 public static class InputMethodAndLanguageSettingsActivity extends Settings { /* empty */ }
Jeff Browne46c5f32012-04-05 11:42:18 -0700749 public static class KeyboardLayoutPickerActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700750 public static class InputMethodAndSubtypeEnablerActivity extends Settings { /* empty */ }
satoke077d2b2011-07-25 09:38:11 +0900751 public static class SpellCheckersSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700752 public static class LocalePickerActivity extends Settings { /* empty */ }
753 public static class UserDictionarySettingsActivity extends Settings { /* empty */ }
754 public static class SoundSettingsActivity extends Settings { /* empty */ }
755 public static class DisplaySettingsActivity extends Settings { /* empty */ }
756 public static class DeviceInfoSettingsActivity extends Settings { /* empty */ }
757 public static class ApplicationSettingsActivity extends Settings { /* empty */ }
758 public static class ManageApplicationsActivity extends Settings { /* empty */ }
759 public static class StorageUseActivity extends Settings { /* empty */ }
760 public static class DevelopmentSettingsActivity extends Settings { /* empty */ }
761 public static class AccessibilitySettingsActivity extends Settings { /* empty */ }
762 public static class SecuritySettingsActivity extends Settings { /* empty */ }
Gilles Debunnea6a8a142011-06-09 11:56:17 -0700763 public static class LocationSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700764 public static class PrivacySettingsActivity extends Settings { /* empty */ }
765 public static class DockSettingsActivity extends Settings { /* empty */ }
766 public static class RunningServicesActivity extends Settings { /* empty */ }
767 public static class ManageAccountsSettingsActivity extends Settings { /* empty */ }
768 public static class PowerUsageSummaryActivity extends Settings { /* empty */ }
769 public static class AccountSyncSettingsActivity extends Settings { /* empty */ }
770 public static class AccountSyncSettingsInAddAccountActivity extends Settings { /* empty */ }
771 public static class CryptKeeperSettingsActivity extends Settings { /* empty */ }
772 public static class DeviceAdminSettingsActivity extends Settings { /* empty */ }
773 public static class DataUsageSummaryActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700774 public static class AdvancedWifiSettingsActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700775 public static class TextToSpeechSettingsActivity extends Settings { /* empty */ }
Jeff Hamilton3d670de2011-09-21 16:44:36 -0500776 public static class AndroidBeamSettingsActivity extends Settings { /* empty */ }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800777}