blob: 320e527030e15fb200bc7402f2aa8bf7eb8cd4f6 [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
146 // TODO Add support for android.R.id.home in all Setting's onOptionsItemSelected
147 // getActionBar().setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP,
148 // ActionBar.DISPLAY_HOME_AS_UP);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800149 }
150
151 @Override
152 protected void onSaveInstanceState(Bundle outState) {
153 super.onSaveInstanceState(outState);
154
155 // Save the current fragment, if it is the same as originally launched
156 if (mCurrentHeader != null) {
157 outState.putParcelable(SAVE_KEY_CURRENT_HEADER, mCurrentHeader);
158 }
159 if (mParentHeader != null) {
160 outState.putParcelable(SAVE_KEY_PARENT_HEADER, mParentHeader);
161 }
162 }
163
Gilles Debunnee78c1872011-06-20 15:00:07 -0700164 @Override
165 public void onResume() {
166 super.onResume();
167
168 ListAdapter listAdapter = getListAdapter();
169 if (listAdapter instanceof HeaderAdapter) {
170 ((HeaderAdapter) listAdapter).resume();
171 }
172 }
173
174 @Override
175 public void onPause() {
176 super.onPause();
177
178 ListAdapter listAdapter = getListAdapter();
179 if (listAdapter instanceof HeaderAdapter) {
180 ((HeaderAdapter) listAdapter).pause();
181 }
182 }
183
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800184 private void switchToHeaderLocal(Header header) {
185 mInLocalHeaderSwitch = true;
186 switchToHeader(header);
187 mInLocalHeaderSwitch = false;
188 }
189
190 @Override
191 public void switchToHeader(Header header) {
192 if (!mInLocalHeaderSwitch) {
193 mCurrentHeader = null;
194 mParentHeader = null;
195 }
196 super.switchToHeader(header);
197 }
198
199 /**
200 * Switch to parent fragment and store the grand parent's info
Jake Hamby2748fc22011-01-12 15:06:28 -0800201 * @param className name of the activity wrapper for the parent fragment.
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800202 */
203 private void switchToParent(String className) {
204 final ComponentName cn = new ComponentName(this, className);
205 try {
206 final PackageManager pm = getPackageManager();
207 final ActivityInfo parentInfo = pm.getActivityInfo(cn, PackageManager.GET_META_DATA);
208
209 if (parentInfo != null && parentInfo.metaData != null) {
210 String fragmentClass = parentInfo.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
211 CharSequence fragmentTitle = parentInfo.loadLabel(pm);
212 Header parentHeader = new Header();
213 parentHeader.fragment = fragmentClass;
214 parentHeader.title = fragmentTitle;
215 mCurrentHeader = parentHeader;
216
217 switchToHeaderLocal(parentHeader);
Amith Yamasani990fb522011-09-02 09:47:18 -0700218 highlightHeader();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800219
220 mParentHeader = new Header();
221 mParentHeader.fragment
222 = parentInfo.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
223 mParentHeader.title = parentInfo.metaData.getString(META_DATA_KEY_PARENT_TITLE);
224 }
225 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700226 Log.w(LOG_TAG, "Could not find parent activity : " + className);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800227 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700228 }
229
Amith Yamasani3965ae62010-11-15 14:45:19 -0800230 @Override
231 public void onNewIntent(Intent intent) {
232 super.onNewIntent(intent);
233
234 // If it is not launched from history, then reset to top-level
235 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0
Gilles Debunne3661b622011-06-27 11:19:16 -0700236 && mFirstHeader != null && !onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800237 switchToHeaderLocal(mFirstHeader);
Amith Yamasani3965ae62010-11-15 14:45:19 -0800238 }
239 }
240
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700241 private void highlightHeader() {
242 if (mTopLevelHeaderId != 0) {
243 Integer index = mHeaderIndexMap.get(mTopLevelHeaderId);
244 if (index != null) {
245 getListView().setItemChecked(index, true);
Amith Yamasani990fb522011-09-02 09:47:18 -0700246 getListView().smoothScrollToPosition(index);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700247 }
248 }
249 }
250
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700251 @Override
252 public Intent getIntent() {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700253 Intent superIntent = super.getIntent();
254 String startingFragment = getStartingFragmentClass(superIntent);
Gilles Debunne3661b622011-06-27 11:19:16 -0700255 // This is called from super.onCreate, isMultiPane() is not yet reliable
256 // Do not use onIsHidingHeaders either, which relies itself on this method
257 if (startingFragment != null && !onIsMultiPane()) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700258 Intent modIntent = new Intent(superIntent);
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700259 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700260 Bundle args = superIntent.getExtras();
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700261 if (args != null) {
262 args = new Bundle(args);
263 } else {
264 args = new Bundle();
265 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700266 args.putParcelable("intent", superIntent);
267 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras());
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700268 return modIntent;
269 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700270 return superIntent;
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700271 }
272
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700273 /**
Amith Yamasani379d9b02010-09-27 12:03:59 -0700274 * Checks if the component name in the intent is different from the Settings class and
275 * returns the class name to load as a fragment.
276 */
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700277 protected String getStartingFragmentClass(Intent intent) {
278 if (mFragmentClass != null) return mFragmentClass;
279
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700280 String intentClass = intent.getComponent().getClassName();
Amith Yamasani379d9b02010-09-27 12:03:59 -0700281 if (intentClass.equals(getClass().getName())) return null;
282
Dianne Hackbornee293792010-11-01 12:32:33 -0700283 if ("com.android.settings.ManageApplications".equals(intentClass)
284 || "com.android.settings.RunningServices".equals(intentClass)
285 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700286 // Old names of manage apps.
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700287 intentClass = com.android.settings.applications.ManageApplications.class.getName();
288 }
289
Amith Yamasani379d9b02010-09-27 12:03:59 -0700290 return intentClass;
291 }
292
293 /**
294 * Override initial header when an activity-alias is causing Settings to be launched
295 * for a specific fragment encoded in the android:name parameter.
296 */
297 @Override
298 public Header onGetInitialHeader() {
299 String fragmentClass = getStartingFragmentClass(super.getIntent());
300 if (fragmentClass != null) {
301 Header header = new Header();
302 header.fragment = fragmentClass;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800303 header.title = getTitle();
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700304 header.fragmentArguments = getIntent().getExtras();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800305 mCurrentHeader = header;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700306 return header;
307 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700308
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700309 return mFirstHeader;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700310 }
311
Dianne Hackbornb7258182011-03-15 16:23:55 -0700312 @Override
Dianne Hackborn48147dc2011-03-18 12:29:41 -0700313 public Intent onBuildStartFragmentIntent(String fragmentName, Bundle args,
314 int titleRes, int shortTitleRes) {
315 Intent intent = super.onBuildStartFragmentIntent(fragmentName, args,
316 titleRes, shortTitleRes);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700317
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700318 // some fragments want to avoid split actionbar
Gilles Debunne162e5412011-07-11 14:13:31 -0700319 if (DataUsageSummary.class.getName().equals(fragmentName) ||
320 PowerUsageSummary.class.getName().equals(fragmentName) ||
Gilles Debunnecd8e5242011-07-25 11:36:15 -0700321 AccountSyncSettings.class.getName().equals(fragmentName) ||
Jeff Sharkey11d30122012-03-19 16:54:07 -0700322 UserDictionarySettings.class.getName().equals(fragmentName) ||
Adam Powellfaba7e42012-03-26 17:28:38 -0700323 Memory.class.getName().equals(fragmentName) ||
Jeff Sharkeya339cba2012-05-08 11:42:49 -0700324 ManageApplications.class.getName().equals(fragmentName) ||
325 WirelessSettings.class.getName().equals(fragmentName) ||
326 SoundSettings.class.getName().equals(fragmentName) ||
327 PrivacySettings.class.getName().equals(fragmentName)) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700328 intent.putExtra(EXTRA_CLEAR_UI_OPTIONS, true);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700329 }
330
Dianne Hackbornb7258182011-03-15 16:23:55 -0700331 intent.setClass(this, SubSettings.class);
332 return intent;
333 }
334
Amith Yamasani379d9b02010-09-27 12:03:59 -0700335 /**
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700336 * Populate the activity with the top-level headers.
337 */
Amith Yamasanid7993472010-08-18 13:59:28 -0700338 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700339 public void onBuildHeaders(List<Header> headers) {
340 loadHeadersFromResource(R.xml.settings_headers, headers);
Amith Yamasanid7993472010-08-18 13:59:28 -0700341
Gilles Debunnee78c1872011-06-20 15:00:07 -0700342 updateHeaderList(headers);
343
344 mHeaders = headers;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700345 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700346
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700347 private void updateHeaderList(List<Header> target) {
348 int i = 0;
349 while (i < target.size()) {
350 Header header = target.get(i);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700351 // Ids are integers, so downcasting
352 int id = (int) header.id;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700353 if (id == R.id.dock_settings) {
354 if (!needsDockSettings())
355 target.remove(header);
356 } else if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
357 Utils.updateHeaderToSpecificActivityFromMetaDataOrRemove(this, target, header);
Gilles Debunne2454f492011-06-21 16:16:06 -0700358 } else if (id == R.id.wifi_settings) {
359 // Remove WiFi Settings if WiFi service is not available.
360 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
361 target.remove(header);
362 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700363 } else if (id == R.id.bluetooth_settings) {
364 // Remove Bluetooth Settings if Bluetooth service is not available.
Gilles Debunne2454f492011-06-21 16:16:06 -0700365 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700366 target.remove(header);
367 }
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700368 } else if (id == R.id.data_usage_settings) {
369 // Remove data usage when kernel module not enabled
370 final INetworkManagementService netManager = INetworkManagementService.Stub
371 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
372 try {
373 if (!netManager.isBandwidthControlEnabled()) {
374 target.remove(header);
375 }
376 } catch (RemoteException e) {
377 // ignored
378 }
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700379 } else if (id == R.id.user_settings) {
Amith Yamasanifa3eea52012-03-28 10:25:08 -0700380 if (!mEnableUserManagement
381 || !UserId.MU_ENABLED || UserId.myUserId() != 0
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700382 || !getResources().getBoolean(R.bool.enable_user_management)
383 || Utils.isMonkeyRunning()) {
384 target.remove(header);
385 }
386 }
387 if (UserId.MU_ENABLED && UserId.myUserId() != 0
388 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
389 target.remove(header);
Amith Yamasanid7993472010-08-18 13:59:28 -0700390 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700391
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700392 // Increment if the current one wasn't removed by the Utils code.
393 if (target.get(i) == header) {
Amith Yamasani3965ae62010-11-15 14:45:19 -0800394 // Hold on to the first header, when we need to reset to the top-level
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700395 if (mFirstHeader == null &&
396 HeaderAdapter.getHeaderType(header) != HeaderAdapter.HEADER_TYPE_CATEGORY) {
397 mFirstHeader = header;
398 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700399 mHeaderIndexMap.put(id, i);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700400 i++;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700401 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700402 }
403 }
404
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700405 private boolean needsDockSettings() {
406 return getResources().getBoolean(R.bool.has_dock_settings);
Amith Yamasanib61cf512010-09-12 08:17:50 -0700407 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700408
409 private void getMetaData() {
410 try {
411 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
412 PackageManager.GET_META_DATA);
413 if (ai == null || ai.metaData == null) return;
414 mTopLevelHeaderId = ai.metaData.getInt(META_DATA_KEY_HEADER_ID);
415 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800416
417 // Check if it has a parent specified and create a Header object
418 final int parentHeaderTitleRes = ai.metaData.getInt(META_DATA_KEY_PARENT_TITLE);
419 String parentFragmentClass = ai.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
420 if (parentFragmentClass != null) {
421 mParentHeader = new Header();
422 mParentHeader.fragment = parentFragmentClass;
423 if (parentHeaderTitleRes != 0) {
424 mParentHeader.title = getResources().getString(parentHeaderTitleRes);
425 }
426 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700427 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700428 // No recovery
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700429 }
430 }
431
Amith Yamasani9e3a4702011-01-11 09:09:26 -0800432 @Override
433 public boolean hasNextButton() {
434 return super.hasNextButton();
435 }
436
437 @Override
438 public Button getNextButton() {
439 return super.getNextButton();
440 }
441
Gilles Debunnee78c1872011-06-20 15:00:07 -0700442 private static class HeaderAdapter extends ArrayAdapter<Header> {
443 static final int HEADER_TYPE_CATEGORY = 0;
444 static final int HEADER_TYPE_NORMAL = 1;
445 static final int HEADER_TYPE_SWITCH = 2;
446 private static final int HEADER_TYPE_COUNT = HEADER_TYPE_SWITCH + 1;
447
448 private final WifiEnabler mWifiEnabler;
449 private final BluetoothEnabler mBluetoothEnabler;
450
451 private static class HeaderViewHolder {
452 ImageView icon;
453 TextView title;
454 TextView summary;
455 Switch switch_;
456 }
457
458 private LayoutInflater mInflater;
459
460 static int getHeaderType(Header header) {
461 if (header.fragment == null && header.intent == null) {
462 return HEADER_TYPE_CATEGORY;
463 } else if (header.id == R.id.wifi_settings || header.id == R.id.bluetooth_settings) {
464 return HEADER_TYPE_SWITCH;
465 } else {
466 return HEADER_TYPE_NORMAL;
467 }
468 }
469
470 @Override
471 public int getItemViewType(int position) {
472 Header header = getItem(position);
473 return getHeaderType(header);
474 }
475
476 @Override
477 public boolean areAllItemsEnabled() {
478 return false; // because of categories
479 }
480
481 @Override
482 public boolean isEnabled(int position) {
483 return getItemViewType(position) != HEADER_TYPE_CATEGORY;
484 }
485
486 @Override
487 public int getViewTypeCount() {
488 return HEADER_TYPE_COUNT;
489 }
490
491 @Override
492 public boolean hasStableIds() {
493 return true;
494 }
495
496 public HeaderAdapter(Context context, List<Header> objects) {
497 super(context, 0, objects);
498 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
499
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700500 // Temp Switches provided as placeholder until the adapter replaces these with actual
Gilles Debunnee78c1872011-06-20 15:00:07 -0700501 // Switches inflated from their layouts. Must be done before adapter is set in super
502 mWifiEnabler = new WifiEnabler(context, new Switch(context));
503 mBluetoothEnabler = new BluetoothEnabler(context, new Switch(context));
504 }
505
506 @Override
507 public View getView(int position, View convertView, ViewGroup parent) {
508 HeaderViewHolder holder;
509 Header header = getItem(position);
510 int headerType = getHeaderType(header);
511 View view = null;
512
513 if (convertView == null) {
514 holder = new HeaderViewHolder();
515 switch (headerType) {
516 case HEADER_TYPE_CATEGORY:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700517 view = new TextView(getContext(), null,
518 android.R.attr.listSeparatorTextViewStyle);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700519 holder.title = (TextView) view;
520 break;
521
522 case HEADER_TYPE_SWITCH:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700523 view = mInflater.inflate(R.layout.preference_header_switch_item, parent,
524 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700525 holder.icon = (ImageView) view.findViewById(R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700526 holder.title = (TextView)
527 view.findViewById(com.android.internal.R.id.title);
528 holder.summary = (TextView)
529 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700530 holder.switch_ = (Switch) view.findViewById(R.id.switchWidget);
531 break;
532
533 case HEADER_TYPE_NORMAL:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700534 view = mInflater.inflate(
535 com.android.internal.R.layout.preference_header_item, parent,
536 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700537 holder.icon = (ImageView) view.findViewById(com.android.internal.R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700538 holder.title = (TextView)
539 view.findViewById(com.android.internal.R.id.title);
540 holder.summary = (TextView)
541 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700542 break;
543 }
544 view.setTag(holder);
545 } else {
546 view = convertView;
547 holder = (HeaderViewHolder) view.getTag();
548 }
549
550 // All view fields must be updated every time, because the view may be recycled
551 switch (headerType) {
552 case HEADER_TYPE_CATEGORY:
553 holder.title.setText(header.getTitle(getContext().getResources()));
554 break;
555
556 case HEADER_TYPE_SWITCH:
557 // Would need a different treatment if the main menu had more switches
558 if (header.id == R.id.wifi_settings) {
559 mWifiEnabler.setSwitch(holder.switch_);
560 } else {
561 mBluetoothEnabler.setSwitch(holder.switch_);
562 }
563 // No break, fall through on purpose to update common fields
564
565 //$FALL-THROUGH$
566 case HEADER_TYPE_NORMAL:
567 holder.icon.setImageResource(header.iconRes);
568 holder.title.setText(header.getTitle(getContext().getResources()));
569 CharSequence summary = header.getSummary(getContext().getResources());
570 if (!TextUtils.isEmpty(summary)) {
571 holder.summary.setVisibility(View.VISIBLE);
572 holder.summary.setText(summary);
573 } else {
574 holder.summary.setVisibility(View.GONE);
575 }
576 break;
577 }
578
579 return view;
580 }
581
582 public void resume() {
583 mWifiEnabler.resume();
584 mBluetoothEnabler.resume();
585 }
586
587 public void pause() {
588 mWifiEnabler.pause();
589 mBluetoothEnabler.pause();
590 }
591 }
592
593 @Override
Amith Yamasania4379d62011-07-22 10:34:58 -0700594 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
595 // Override the fragment title for Wallpaper settings
Amith Yamasanidfb65432011-11-29 16:38:14 -0800596 int titleRes = pref.getTitleRes();
Amith Yamasania4379d62011-07-22 10:34:58 -0700597 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
Amith Yamasanidfb65432011-11-29 16:38:14 -0800598 titleRes = R.string.wallpaper_settings_fragment_title;
Amith Yamasania4379d62011-07-22 10:34:58 -0700599 }
Jean Chalard7dabe452012-05-10 18:08:07 +0900600 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
601 null, 0);
Amith Yamasania4379d62011-07-22 10:34:58 -0700602 return true;
603 }
604
605 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700606 public void setListAdapter(ListAdapter adapter) {
607 if (mHeaders == null) {
608 mHeaders = new ArrayList<Header>();
609 // When the saved state provides the list of headers, onBuildHeaders is not called
610 // Copy the list of Headers from the adapter, preserving their order
611 for (int i = 0; i < adapter.getCount(); i++) {
612 mHeaders.add((Header) adapter.getItem(i));
613 }
614 }
615
616 // Ignore the adapter provided by PreferenceActivity and substitute ours instead
617 super.setListAdapter(new HeaderAdapter(this, mHeaders));
618 }
619
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700620 /*
621 * Settings subclasses for launching independently.
622 */
Gilles Debunnee78c1872011-06-20 15:00:07 -0700623 public static class BluetoothSettingsActivity extends Settings { /* empty */ }
624 public static class WirelessSettingsActivity extends Settings { /* empty */ }
625 public static class TetherSettingsActivity extends Settings { /* empty */ }
626 public static class VpnSettingsActivity extends Settings { /* empty */ }
627 public static class DateTimeSettingsActivity extends Settings { /* empty */ }
628 public static class StorageSettingsActivity extends Settings { /* empty */ }
629 public static class WifiSettingsActivity extends Settings { /* empty */ }
repo syncb98463f2011-06-30 10:58:43 -0700630 public static class WifiP2pSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700631 public static class InputMethodAndLanguageSettingsActivity extends Settings { /* empty */ }
Jeff Browne46c5f32012-04-05 11:42:18 -0700632 public static class KeyboardLayoutPickerActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700633 public static class InputMethodAndSubtypeEnablerActivity extends Settings { /* empty */ }
satoke077d2b2011-07-25 09:38:11 +0900634 public static class SpellCheckersSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700635 public static class LocalePickerActivity extends Settings { /* empty */ }
636 public static class UserDictionarySettingsActivity extends Settings { /* empty */ }
637 public static class SoundSettingsActivity extends Settings { /* empty */ }
638 public static class DisplaySettingsActivity extends Settings { /* empty */ }
639 public static class DeviceInfoSettingsActivity extends Settings { /* empty */ }
640 public static class ApplicationSettingsActivity extends Settings { /* empty */ }
641 public static class ManageApplicationsActivity extends Settings { /* empty */ }
642 public static class StorageUseActivity extends Settings { /* empty */ }
643 public static class DevelopmentSettingsActivity extends Settings { /* empty */ }
644 public static class AccessibilitySettingsActivity extends Settings { /* empty */ }
645 public static class SecuritySettingsActivity extends Settings { /* empty */ }
Gilles Debunnea6a8a142011-06-09 11:56:17 -0700646 public static class LocationSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700647 public static class PrivacySettingsActivity extends Settings { /* empty */ }
648 public static class DockSettingsActivity extends Settings { /* empty */ }
649 public static class RunningServicesActivity extends Settings { /* empty */ }
650 public static class ManageAccountsSettingsActivity extends Settings { /* empty */ }
651 public static class PowerUsageSummaryActivity extends Settings { /* empty */ }
652 public static class AccountSyncSettingsActivity extends Settings { /* empty */ }
653 public static class AccountSyncSettingsInAddAccountActivity extends Settings { /* empty */ }
654 public static class CryptKeeperSettingsActivity extends Settings { /* empty */ }
655 public static class DeviceAdminSettingsActivity extends Settings { /* empty */ }
656 public static class DataUsageSummaryActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700657 public static class AdvancedWifiSettingsActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700658 public static class TextToSpeechSettingsActivity extends Settings { /* empty */ }
Jeff Hamilton3d670de2011-09-21 16:44:36 -0500659 public static class AndroidBeamSettingsActivity extends Settings { /* empty */ }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800660}