blob: b36364d6f8c6671186f440fd765432f9d8979f0e [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;
Adam Powellfaba7e42012-03-26 17:28:38 -070021import com.android.settings.applications.ManageApplications;
Gilles Debunnecd8e5242011-07-25 11:36:15 -070022import com.android.settings.bluetooth.BluetoothEnabler;
Jeff Sharkey11d30122012-03-19 16:54:07 -070023import com.android.settings.deviceinfo.Memory;
Gilles Debunnecd8e5242011-07-25 11:36:15 -070024import com.android.settings.fuelgauge.PowerUsageSummary;
25import com.android.settings.wifi.WifiEnabler;
26
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080027import android.content.ComponentName;
Gilles Debunnee78c1872011-06-20 15:00:07 -070028import android.content.Context;
Amith Yamasani379d9b02010-09-27 12:03:59 -070029import android.content.Intent;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070030import android.content.pm.ActivityInfo;
31import android.content.pm.PackageManager;
32import android.content.pm.PackageManager.NameNotFoundException;
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -070033import android.os.Bundle;
Jeff Sharkey34e964d2012-04-21 15:41:48 -070034import android.os.INetworkManagementService;
35import android.os.RemoteException;
36import android.os.ServiceManager;
Amith Yamasanib810a0d2012-03-25 10:12:26 -070037import android.os.UserId;
Amith Yamasania4379d62011-07-22 10:34:58 -070038import android.preference.Preference;
Amith Yamasani02cf71a2010-09-21 15:48:52 -070039import android.preference.PreferenceActivity;
Amith Yamasania4379d62011-07-22 10:34:58 -070040import android.preference.PreferenceFragment;
Gilles Debunnee78c1872011-06-20 15:00:07 -070041import android.text.TextUtils;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080042import android.util.Log;
Gilles Debunnee78c1872011-06-20 15:00:07 -070043import android.view.LayoutInflater;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080044import android.view.View;
45import android.view.View.OnClickListener;
Gilles Debunnee78c1872011-06-20 15:00:07 -070046import android.view.ViewGroup;
47import android.widget.ArrayAdapter;
Amith Yamasani9e3a4702011-01-11 09:09:26 -080048import android.widget.Button;
Gilles Debunnee78c1872011-06-20 15:00:07 -070049import android.widget.ImageView;
50import android.widget.ListAdapter;
51import android.widget.Switch;
52import android.widget.TextView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080053
Gilles Debunnee78c1872011-06-20 15:00:07 -070054import java.util.ArrayList;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070055import java.util.HashMap;
Amith Yamasani02cf71a2010-09-21 15:48:52 -070056import java.util.List;
Amith Yamasanid7993472010-08-18 13:59:28 -070057
58/**
59 * Top-level settings activity to handle single pane and double pane UI layout.
60 */
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -080061public class Settings extends PreferenceActivity implements ButtonBarHandler {
Amith Yamasanid7993472010-08-18 13:59:28 -070062
Gilles Debunnee78c1872011-06-20 15:00:07 -070063 private static final String LOG_TAG = "Settings";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070064 private static final String META_DATA_KEY_HEADER_ID =
Gilles Debunnee78c1872011-06-20 15:00:07 -070065 "com.android.settings.TOP_LEVEL_HEADER_ID";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070066 private static final String META_DATA_KEY_FRAGMENT_CLASS =
Gilles Debunnee78c1872011-06-20 15:00:07 -070067 "com.android.settings.FRAGMENT_CLASS";
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080068 private static final String META_DATA_KEY_PARENT_TITLE =
69 "com.android.settings.PARENT_FRAGMENT_TITLE";
70 private static final String META_DATA_KEY_PARENT_FRAGMENT_CLASS =
71 "com.android.settings.PARENT_FRAGMENT_CLASS";
72
Jeff Sharkey54d0af52011-08-11 18:26:57 -070073 private static final String EXTRA_CLEAR_UI_OPTIONS = "settings:remove_ui_options";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070074
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080075 private static final String SAVE_KEY_CURRENT_HEADER = "com.android.settings.CURRENT_HEADER";
76 private static final String SAVE_KEY_PARENT_HEADER = "com.android.settings.PARENT_HEADER";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070077
78 private String mFragmentClass;
79 private int mTopLevelHeaderId;
Amith Yamasani3965ae62010-11-15 14:45:19 -080080 private Header mFirstHeader;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080081 private Header mCurrentHeader;
82 private Header mParentHeader;
83 private boolean mInLocalHeaderSwitch;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070084
Amith Yamasanib810a0d2012-03-25 10:12:26 -070085 // Show only these settings for restricted users
86 private int[] SETTINGS_FOR_RESTRICTED = {
87 R.id.wifi_settings,
88 R.id.bluetooth_settings,
89 R.id.sound_settings,
90 R.id.display_settings,
Amith Yamasani3deeeb72012-04-05 14:44:48 -070091 R.id.security_settings,
Amith Yamasanib810a0d2012-03-25 10:12:26 -070092 R.id.sync_settings,
93 R.id.about_settings
94 };
95
Amith Yamasanifa3eea52012-03-28 10:25:08 -070096 private boolean mEnableUserManagement = false;
97
Amith Yamasani02cf71a2010-09-21 15:48:52 -070098 // TODO: Update Call Settings based on airplane mode state.
Amith Yamasanib61cf512010-09-12 08:17:50 -070099
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700100 protected HashMap<Integer, Integer> mHeaderIndexMap = new HashMap<Integer, Integer>();
Gilles Debunnee78c1872011-06-20 15:00:07 -0700101 private List<Header> mHeaders;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700102
103 @Override
104 protected void onCreate(Bundle savedInstanceState) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700105 if (getIntent().getBooleanExtra(EXTRA_CLEAR_UI_OPTIONS, false)) {
106 getWindow().setUiOptions(0);
107 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700108
Amith Yamasanifa3eea52012-03-28 10:25:08 -0700109 if (android.provider.Settings.Secure.getInt(getContentResolver(), "multiuser_enabled", -1)
110 > 0) {
111 mEnableUserManagement = true;
112 }
113
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700114 getMetaData();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800115 mInLocalHeaderSwitch = true;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700116 super.onCreate(savedInstanceState);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800117 mInLocalHeaderSwitch = false;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700118
Gilles Debunne3661b622011-06-27 11:19:16 -0700119 if (!onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasani72aa19d2010-12-09 06:07:12 -0800120 highlightHeader();
121 // Force the title so that it doesn't get overridden by a direct launch of
122 // a specific settings screen.
123 setTitle(R.string.settings_label);
124 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800125
126 // Retrieve any saved state
127 if (savedInstanceState != null) {
128 mCurrentHeader = savedInstanceState.getParcelable(SAVE_KEY_CURRENT_HEADER);
129 mParentHeader = savedInstanceState.getParcelable(SAVE_KEY_PARENT_HEADER);
130 }
131
132 // If the current header was saved, switch to it
133 if (savedInstanceState != null && mCurrentHeader != null) {
134 //switchToHeaderLocal(mCurrentHeader);
135 showBreadCrumbs(mCurrentHeader.title, null);
136 }
137
138 if (mParentHeader != null) {
139 setParentTitle(mParentHeader.title, null, new OnClickListener() {
140 public void onClick(View v) {
141 switchToParent(mParentHeader.fragment);
142 }
143 });
144 }
Gilles Debunnedc7101f2011-06-27 12:00:49 -0700145
Amith Yamasani3d384f42012-05-11 15:22:04 -0700146 // Override up navigation for multi-pane, since we handle it in the fragment breadcrumbs
147 if (onIsMultiPane()) {
148 getActionBar().setDisplayHomeAsUpEnabled(false);
149 getActionBar().setHomeButtonEnabled(false);
150 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800151 }
152
153 @Override
154 protected void onSaveInstanceState(Bundle outState) {
155 super.onSaveInstanceState(outState);
156
157 // Save the current fragment, if it is the same as originally launched
158 if (mCurrentHeader != null) {
159 outState.putParcelable(SAVE_KEY_CURRENT_HEADER, mCurrentHeader);
160 }
161 if (mParentHeader != null) {
162 outState.putParcelable(SAVE_KEY_PARENT_HEADER, mParentHeader);
163 }
164 }
165
Gilles Debunnee78c1872011-06-20 15:00:07 -0700166 @Override
167 public void onResume() {
168 super.onResume();
169
170 ListAdapter listAdapter = getListAdapter();
171 if (listAdapter instanceof HeaderAdapter) {
172 ((HeaderAdapter) listAdapter).resume();
173 }
174 }
175
176 @Override
177 public void onPause() {
178 super.onPause();
179
180 ListAdapter listAdapter = getListAdapter();
181 if (listAdapter instanceof HeaderAdapter) {
182 ((HeaderAdapter) listAdapter).pause();
183 }
184 }
185
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800186 private void switchToHeaderLocal(Header header) {
187 mInLocalHeaderSwitch = true;
188 switchToHeader(header);
189 mInLocalHeaderSwitch = false;
190 }
191
192 @Override
193 public void switchToHeader(Header header) {
194 if (!mInLocalHeaderSwitch) {
195 mCurrentHeader = null;
196 mParentHeader = null;
197 }
198 super.switchToHeader(header);
199 }
200
201 /**
202 * Switch to parent fragment and store the grand parent's info
Jake Hamby2748fc22011-01-12 15:06:28 -0800203 * @param className name of the activity wrapper for the parent fragment.
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800204 */
205 private void switchToParent(String className) {
206 final ComponentName cn = new ComponentName(this, className);
207 try {
208 final PackageManager pm = getPackageManager();
209 final ActivityInfo parentInfo = pm.getActivityInfo(cn, PackageManager.GET_META_DATA);
210
211 if (parentInfo != null && parentInfo.metaData != null) {
212 String fragmentClass = parentInfo.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
213 CharSequence fragmentTitle = parentInfo.loadLabel(pm);
214 Header parentHeader = new Header();
215 parentHeader.fragment = fragmentClass;
216 parentHeader.title = fragmentTitle;
217 mCurrentHeader = parentHeader;
218
219 switchToHeaderLocal(parentHeader);
Amith Yamasani990fb522011-09-02 09:47:18 -0700220 highlightHeader();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800221
222 mParentHeader = new Header();
223 mParentHeader.fragment
224 = parentInfo.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
225 mParentHeader.title = parentInfo.metaData.getString(META_DATA_KEY_PARENT_TITLE);
226 }
227 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700228 Log.w(LOG_TAG, "Could not find parent activity : " + className);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800229 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700230 }
231
Amith Yamasani3965ae62010-11-15 14:45:19 -0800232 @Override
233 public void onNewIntent(Intent intent) {
234 super.onNewIntent(intent);
235
236 // If it is not launched from history, then reset to top-level
237 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0
Gilles Debunne3661b622011-06-27 11:19:16 -0700238 && mFirstHeader != null && !onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800239 switchToHeaderLocal(mFirstHeader);
Amith Yamasani3965ae62010-11-15 14:45:19 -0800240 }
241 }
242
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700243 private void highlightHeader() {
244 if (mTopLevelHeaderId != 0) {
245 Integer index = mHeaderIndexMap.get(mTopLevelHeaderId);
246 if (index != null) {
247 getListView().setItemChecked(index, true);
Amith Yamasani990fb522011-09-02 09:47:18 -0700248 getListView().smoothScrollToPosition(index);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700249 }
250 }
251 }
252
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700253 @Override
254 public Intent getIntent() {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700255 Intent superIntent = super.getIntent();
256 String startingFragment = getStartingFragmentClass(superIntent);
Gilles Debunne3661b622011-06-27 11:19:16 -0700257 // This is called from super.onCreate, isMultiPane() is not yet reliable
258 // Do not use onIsHidingHeaders either, which relies itself on this method
259 if (startingFragment != null && !onIsMultiPane()) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700260 Intent modIntent = new Intent(superIntent);
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700261 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700262 Bundle args = superIntent.getExtras();
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700263 if (args != null) {
264 args = new Bundle(args);
265 } else {
266 args = new Bundle();
267 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700268 args.putParcelable("intent", superIntent);
269 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras());
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700270 return modIntent;
271 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700272 return superIntent;
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700273 }
274
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700275 /**
Amith Yamasani379d9b02010-09-27 12:03:59 -0700276 * Checks if the component name in the intent is different from the Settings class and
277 * returns the class name to load as a fragment.
278 */
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700279 protected String getStartingFragmentClass(Intent intent) {
280 if (mFragmentClass != null) return mFragmentClass;
281
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700282 String intentClass = intent.getComponent().getClassName();
Amith Yamasani379d9b02010-09-27 12:03:59 -0700283 if (intentClass.equals(getClass().getName())) return null;
284
Dianne Hackbornee293792010-11-01 12:32:33 -0700285 if ("com.android.settings.ManageApplications".equals(intentClass)
286 || "com.android.settings.RunningServices".equals(intentClass)
287 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700288 // Old names of manage apps.
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700289 intentClass = com.android.settings.applications.ManageApplications.class.getName();
290 }
291
Amith Yamasani379d9b02010-09-27 12:03:59 -0700292 return intentClass;
293 }
294
295 /**
296 * Override initial header when an activity-alias is causing Settings to be launched
297 * for a specific fragment encoded in the android:name parameter.
298 */
299 @Override
300 public Header onGetInitialHeader() {
301 String fragmentClass = getStartingFragmentClass(super.getIntent());
302 if (fragmentClass != null) {
303 Header header = new Header();
304 header.fragment = fragmentClass;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800305 header.title = getTitle();
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700306 header.fragmentArguments = getIntent().getExtras();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800307 mCurrentHeader = header;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700308 return header;
309 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700310
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700311 return mFirstHeader;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700312 }
313
Dianne Hackbornb7258182011-03-15 16:23:55 -0700314 @Override
Dianne Hackborn48147dc2011-03-18 12:29:41 -0700315 public Intent onBuildStartFragmentIntent(String fragmentName, Bundle args,
316 int titleRes, int shortTitleRes) {
317 Intent intent = super.onBuildStartFragmentIntent(fragmentName, args,
318 titleRes, shortTitleRes);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700319
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700320 // some fragments want to avoid split actionbar
Gilles Debunne162e5412011-07-11 14:13:31 -0700321 if (DataUsageSummary.class.getName().equals(fragmentName) ||
322 PowerUsageSummary.class.getName().equals(fragmentName) ||
Gilles Debunnecd8e5242011-07-25 11:36:15 -0700323 AccountSyncSettings.class.getName().equals(fragmentName) ||
Jeff Sharkey11d30122012-03-19 16:54:07 -0700324 UserDictionarySettings.class.getName().equals(fragmentName) ||
Adam Powellfaba7e42012-03-26 17:28:38 -0700325 Memory.class.getName().equals(fragmentName) ||
Jeff Sharkeya339cba2012-05-08 11:42:49 -0700326 ManageApplications.class.getName().equals(fragmentName) ||
327 WirelessSettings.class.getName().equals(fragmentName) ||
328 SoundSettings.class.getName().equals(fragmentName) ||
329 PrivacySettings.class.getName().equals(fragmentName)) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700330 intent.putExtra(EXTRA_CLEAR_UI_OPTIONS, true);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700331 }
332
Dianne Hackbornb7258182011-03-15 16:23:55 -0700333 intent.setClass(this, SubSettings.class);
334 return intent;
335 }
336
Amith Yamasani379d9b02010-09-27 12:03:59 -0700337 /**
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700338 * Populate the activity with the top-level headers.
339 */
Amith Yamasanid7993472010-08-18 13:59:28 -0700340 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700341 public void onBuildHeaders(List<Header> headers) {
342 loadHeadersFromResource(R.xml.settings_headers, headers);
Amith Yamasanid7993472010-08-18 13:59:28 -0700343
Gilles Debunnee78c1872011-06-20 15:00:07 -0700344 updateHeaderList(headers);
345
346 mHeaders = headers;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700347 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700348
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700349 private void updateHeaderList(List<Header> target) {
350 int i = 0;
351 while (i < target.size()) {
352 Header header = target.get(i);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700353 // Ids are integers, so downcasting
354 int id = (int) header.id;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700355 if (id == R.id.dock_settings) {
356 if (!needsDockSettings())
357 target.remove(header);
358 } else if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
359 Utils.updateHeaderToSpecificActivityFromMetaDataOrRemove(this, target, header);
Gilles Debunne2454f492011-06-21 16:16:06 -0700360 } else if (id == R.id.wifi_settings) {
361 // Remove WiFi Settings if WiFi service is not available.
362 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
363 target.remove(header);
364 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700365 } else if (id == R.id.bluetooth_settings) {
366 // Remove Bluetooth Settings if Bluetooth service is not available.
Gilles Debunne2454f492011-06-21 16:16:06 -0700367 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700368 target.remove(header);
369 }
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700370 } else if (id == R.id.data_usage_settings) {
371 // Remove data usage when kernel module not enabled
372 final INetworkManagementService netManager = INetworkManagementService.Stub
373 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
374 try {
375 if (!netManager.isBandwidthControlEnabled()) {
376 target.remove(header);
377 }
378 } catch (RemoteException e) {
379 // ignored
380 }
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700381 } else if (id == R.id.user_settings) {
Amith Yamasanifa3eea52012-03-28 10:25:08 -0700382 if (!mEnableUserManagement
383 || !UserId.MU_ENABLED || UserId.myUserId() != 0
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700384 || !getResources().getBoolean(R.bool.enable_user_management)
385 || Utils.isMonkeyRunning()) {
386 target.remove(header);
387 }
388 }
389 if (UserId.MU_ENABLED && UserId.myUserId() != 0
390 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
391 target.remove(header);
Amith Yamasanid7993472010-08-18 13:59:28 -0700392 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700393
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700394 // Increment if the current one wasn't removed by the Utils code.
395 if (target.get(i) == header) {
Amith Yamasani3965ae62010-11-15 14:45:19 -0800396 // Hold on to the first header, when we need to reset to the top-level
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700397 if (mFirstHeader == null &&
398 HeaderAdapter.getHeaderType(header) != HeaderAdapter.HEADER_TYPE_CATEGORY) {
399 mFirstHeader = header;
400 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700401 mHeaderIndexMap.put(id, i);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700402 i++;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700403 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700404 }
405 }
406
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700407 private boolean needsDockSettings() {
408 return getResources().getBoolean(R.bool.has_dock_settings);
Amith Yamasanib61cf512010-09-12 08:17:50 -0700409 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700410
411 private void getMetaData() {
412 try {
413 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
414 PackageManager.GET_META_DATA);
415 if (ai == null || ai.metaData == null) return;
416 mTopLevelHeaderId = ai.metaData.getInt(META_DATA_KEY_HEADER_ID);
417 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800418
419 // Check if it has a parent specified and create a Header object
420 final int parentHeaderTitleRes = ai.metaData.getInt(META_DATA_KEY_PARENT_TITLE);
421 String parentFragmentClass = ai.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
422 if (parentFragmentClass != null) {
423 mParentHeader = new Header();
424 mParentHeader.fragment = parentFragmentClass;
425 if (parentHeaderTitleRes != 0) {
426 mParentHeader.title = getResources().getString(parentHeaderTitleRes);
427 }
428 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700429 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700430 // No recovery
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700431 }
432 }
433
Amith Yamasani9e3a4702011-01-11 09:09:26 -0800434 @Override
435 public boolean hasNextButton() {
436 return super.hasNextButton();
437 }
438
439 @Override
440 public Button getNextButton() {
441 return super.getNextButton();
442 }
443
Gilles Debunnee78c1872011-06-20 15:00:07 -0700444 private static class HeaderAdapter extends ArrayAdapter<Header> {
445 static final int HEADER_TYPE_CATEGORY = 0;
446 static final int HEADER_TYPE_NORMAL = 1;
447 static final int HEADER_TYPE_SWITCH = 2;
448 private static final int HEADER_TYPE_COUNT = HEADER_TYPE_SWITCH + 1;
449
450 private final WifiEnabler mWifiEnabler;
451 private final BluetoothEnabler mBluetoothEnabler;
452
453 private static class HeaderViewHolder {
454 ImageView icon;
455 TextView title;
456 TextView summary;
457 Switch switch_;
458 }
459
460 private LayoutInflater mInflater;
461
462 static int getHeaderType(Header header) {
463 if (header.fragment == null && header.intent == null) {
464 return HEADER_TYPE_CATEGORY;
465 } else if (header.id == R.id.wifi_settings || header.id == R.id.bluetooth_settings) {
466 return HEADER_TYPE_SWITCH;
467 } else {
468 return HEADER_TYPE_NORMAL;
469 }
470 }
471
472 @Override
473 public int getItemViewType(int position) {
474 Header header = getItem(position);
475 return getHeaderType(header);
476 }
477
478 @Override
479 public boolean areAllItemsEnabled() {
480 return false; // because of categories
481 }
482
483 @Override
484 public boolean isEnabled(int position) {
485 return getItemViewType(position) != HEADER_TYPE_CATEGORY;
486 }
487
488 @Override
489 public int getViewTypeCount() {
490 return HEADER_TYPE_COUNT;
491 }
492
493 @Override
494 public boolean hasStableIds() {
495 return true;
496 }
497
498 public HeaderAdapter(Context context, List<Header> objects) {
499 super(context, 0, objects);
500 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
501
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700502 // Temp Switches provided as placeholder until the adapter replaces these with actual
Gilles Debunnee78c1872011-06-20 15:00:07 -0700503 // Switches inflated from their layouts. Must be done before adapter is set in super
504 mWifiEnabler = new WifiEnabler(context, new Switch(context));
505 mBluetoothEnabler = new BluetoothEnabler(context, new Switch(context));
506 }
507
508 @Override
509 public View getView(int position, View convertView, ViewGroup parent) {
510 HeaderViewHolder holder;
511 Header header = getItem(position);
512 int headerType = getHeaderType(header);
513 View view = null;
514
515 if (convertView == null) {
516 holder = new HeaderViewHolder();
517 switch (headerType) {
518 case HEADER_TYPE_CATEGORY:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700519 view = new TextView(getContext(), null,
520 android.R.attr.listSeparatorTextViewStyle);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700521 holder.title = (TextView) view;
522 break;
523
524 case HEADER_TYPE_SWITCH:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700525 view = mInflater.inflate(R.layout.preference_header_switch_item, parent,
526 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700527 holder.icon = (ImageView) view.findViewById(R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700528 holder.title = (TextView)
529 view.findViewById(com.android.internal.R.id.title);
530 holder.summary = (TextView)
531 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700532 holder.switch_ = (Switch) view.findViewById(R.id.switchWidget);
533 break;
534
535 case HEADER_TYPE_NORMAL:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700536 view = mInflater.inflate(
537 com.android.internal.R.layout.preference_header_item, parent,
538 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700539 holder.icon = (ImageView) view.findViewById(com.android.internal.R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700540 holder.title = (TextView)
541 view.findViewById(com.android.internal.R.id.title);
542 holder.summary = (TextView)
543 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700544 break;
545 }
546 view.setTag(holder);
547 } else {
548 view = convertView;
549 holder = (HeaderViewHolder) view.getTag();
550 }
551
552 // All view fields must be updated every time, because the view may be recycled
553 switch (headerType) {
554 case HEADER_TYPE_CATEGORY:
555 holder.title.setText(header.getTitle(getContext().getResources()));
556 break;
557
558 case HEADER_TYPE_SWITCH:
559 // Would need a different treatment if the main menu had more switches
560 if (header.id == R.id.wifi_settings) {
561 mWifiEnabler.setSwitch(holder.switch_);
562 } else {
563 mBluetoothEnabler.setSwitch(holder.switch_);
564 }
565 // No break, fall through on purpose to update common fields
566
567 //$FALL-THROUGH$
568 case HEADER_TYPE_NORMAL:
569 holder.icon.setImageResource(header.iconRes);
570 holder.title.setText(header.getTitle(getContext().getResources()));
571 CharSequence summary = header.getSummary(getContext().getResources());
572 if (!TextUtils.isEmpty(summary)) {
573 holder.summary.setVisibility(View.VISIBLE);
574 holder.summary.setText(summary);
575 } else {
576 holder.summary.setVisibility(View.GONE);
577 }
578 break;
579 }
580
581 return view;
582 }
583
584 public void resume() {
585 mWifiEnabler.resume();
586 mBluetoothEnabler.resume();
587 }
588
589 public void pause() {
590 mWifiEnabler.pause();
591 mBluetoothEnabler.pause();
592 }
593 }
594
595 @Override
Amith Yamasania4379d62011-07-22 10:34:58 -0700596 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
597 // Override the fragment title for Wallpaper settings
Amith Yamasanidfb65432011-11-29 16:38:14 -0800598 int titleRes = pref.getTitleRes();
Amith Yamasania4379d62011-07-22 10:34:58 -0700599 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
Amith Yamasanidfb65432011-11-29 16:38:14 -0800600 titleRes = R.string.wallpaper_settings_fragment_title;
Amith Yamasania4379d62011-07-22 10:34:58 -0700601 }
Jean Chalard7dabe452012-05-10 18:08:07 +0900602 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
603 null, 0);
Amith Yamasania4379d62011-07-22 10:34:58 -0700604 return true;
605 }
606
Amith Yamasani3d384f42012-05-11 15:22:04 -0700607 public boolean shouldUpRecreateTask(Intent targetIntent) {
608 return super.shouldUpRecreateTask(new Intent(this, Settings.class));
609 }
610
Amith Yamasania4379d62011-07-22 10:34:58 -0700611 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700612 public void setListAdapter(ListAdapter adapter) {
613 if (mHeaders == null) {
614 mHeaders = new ArrayList<Header>();
615 // When the saved state provides the list of headers, onBuildHeaders is not called
616 // Copy the list of Headers from the adapter, preserving their order
617 for (int i = 0; i < adapter.getCount(); i++) {
618 mHeaders.add((Header) adapter.getItem(i));
619 }
620 }
621
622 // Ignore the adapter provided by PreferenceActivity and substitute ours instead
623 super.setListAdapter(new HeaderAdapter(this, mHeaders));
624 }
625
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700626 /*
627 * Settings subclasses for launching independently.
628 */
Gilles Debunnee78c1872011-06-20 15:00:07 -0700629 public static class BluetoothSettingsActivity extends Settings { /* empty */ }
630 public static class WirelessSettingsActivity extends Settings { /* empty */ }
631 public static class TetherSettingsActivity extends Settings { /* empty */ }
632 public static class VpnSettingsActivity extends Settings { /* empty */ }
633 public static class DateTimeSettingsActivity extends Settings { /* empty */ }
634 public static class StorageSettingsActivity extends Settings { /* empty */ }
635 public static class WifiSettingsActivity extends Settings { /* empty */ }
repo syncb98463f2011-06-30 10:58:43 -0700636 public static class WifiP2pSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700637 public static class InputMethodAndLanguageSettingsActivity extends Settings { /* empty */ }
Jeff Browne46c5f32012-04-05 11:42:18 -0700638 public static class KeyboardLayoutPickerActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700639 public static class InputMethodAndSubtypeEnablerActivity extends Settings { /* empty */ }
satoke077d2b2011-07-25 09:38:11 +0900640 public static class SpellCheckersSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700641 public static class LocalePickerActivity extends Settings { /* empty */ }
642 public static class UserDictionarySettingsActivity extends Settings { /* empty */ }
643 public static class SoundSettingsActivity extends Settings { /* empty */ }
644 public static class DisplaySettingsActivity extends Settings { /* empty */ }
645 public static class DeviceInfoSettingsActivity extends Settings { /* empty */ }
646 public static class ApplicationSettingsActivity extends Settings { /* empty */ }
647 public static class ManageApplicationsActivity extends Settings { /* empty */ }
648 public static class StorageUseActivity extends Settings { /* empty */ }
649 public static class DevelopmentSettingsActivity extends Settings { /* empty */ }
650 public static class AccessibilitySettingsActivity extends Settings { /* empty */ }
651 public static class SecuritySettingsActivity extends Settings { /* empty */ }
Gilles Debunnea6a8a142011-06-09 11:56:17 -0700652 public static class LocationSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700653 public static class PrivacySettingsActivity extends Settings { /* empty */ }
654 public static class DockSettingsActivity extends Settings { /* empty */ }
655 public static class RunningServicesActivity extends Settings { /* empty */ }
656 public static class ManageAccountsSettingsActivity extends Settings { /* empty */ }
657 public static class PowerUsageSummaryActivity extends Settings { /* empty */ }
658 public static class AccountSyncSettingsActivity extends Settings { /* empty */ }
659 public static class AccountSyncSettingsInAddAccountActivity extends Settings { /* empty */ }
660 public static class CryptKeeperSettingsActivity extends Settings { /* empty */ }
661 public static class DeviceAdminSettingsActivity extends Settings { /* empty */ }
662 public static class DataUsageSummaryActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700663 public static class AdvancedWifiSettingsActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700664 public static class TextToSpeechSettingsActivity extends Settings { /* empty */ }
Jeff Hamilton3d670de2011-09-21 16:44:36 -0500665 public static class AndroidBeamSettingsActivity extends Settings { /* empty */ }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800666}