blob: 88fac0a2af671cf4daf45cbbd59244e1077cec3c [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) ||
324 ManageApplications.class.getName().equals(fragmentName)) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700325 intent.putExtra(EXTRA_CLEAR_UI_OPTIONS, true);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700326 }
327
Dianne Hackbornb7258182011-03-15 16:23:55 -0700328 intent.setClass(this, SubSettings.class);
329 return intent;
330 }
331
Amith Yamasani379d9b02010-09-27 12:03:59 -0700332 /**
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700333 * Populate the activity with the top-level headers.
334 */
Amith Yamasanid7993472010-08-18 13:59:28 -0700335 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700336 public void onBuildHeaders(List<Header> headers) {
337 loadHeadersFromResource(R.xml.settings_headers, headers);
Amith Yamasanid7993472010-08-18 13:59:28 -0700338
Gilles Debunnee78c1872011-06-20 15:00:07 -0700339 updateHeaderList(headers);
340
341 mHeaders = headers;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700342 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700343
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700344 private void updateHeaderList(List<Header> target) {
345 int i = 0;
346 while (i < target.size()) {
347 Header header = target.get(i);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700348 // Ids are integers, so downcasting
349 int id = (int) header.id;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700350 if (id == R.id.dock_settings) {
351 if (!needsDockSettings())
352 target.remove(header);
353 } else if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
354 Utils.updateHeaderToSpecificActivityFromMetaDataOrRemove(this, target, header);
Gilles Debunne2454f492011-06-21 16:16:06 -0700355 } else if (id == R.id.wifi_settings) {
356 // Remove WiFi Settings if WiFi service is not available.
357 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
358 target.remove(header);
359 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700360 } else if (id == R.id.bluetooth_settings) {
361 // Remove Bluetooth Settings if Bluetooth service is not available.
Gilles Debunne2454f492011-06-21 16:16:06 -0700362 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700363 target.remove(header);
364 }
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700365 } else if (id == R.id.data_usage_settings) {
366 // Remove data usage when kernel module not enabled
367 final INetworkManagementService netManager = INetworkManagementService.Stub
368 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
369 try {
370 if (!netManager.isBandwidthControlEnabled()) {
371 target.remove(header);
372 }
373 } catch (RemoteException e) {
374 // ignored
375 }
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700376 } else if (id == R.id.user_settings) {
Amith Yamasanifa3eea52012-03-28 10:25:08 -0700377 if (!mEnableUserManagement
378 || !UserId.MU_ENABLED || UserId.myUserId() != 0
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700379 || !getResources().getBoolean(R.bool.enable_user_management)
380 || Utils.isMonkeyRunning()) {
381 target.remove(header);
382 }
383 }
384 if (UserId.MU_ENABLED && UserId.myUserId() != 0
385 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
386 target.remove(header);
Amith Yamasanid7993472010-08-18 13:59:28 -0700387 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700388
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700389 // Increment if the current one wasn't removed by the Utils code.
390 if (target.get(i) == header) {
Amith Yamasani3965ae62010-11-15 14:45:19 -0800391 // Hold on to the first header, when we need to reset to the top-level
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700392 if (mFirstHeader == null &&
393 HeaderAdapter.getHeaderType(header) != HeaderAdapter.HEADER_TYPE_CATEGORY) {
394 mFirstHeader = header;
395 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700396 mHeaderIndexMap.put(id, i);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700397 i++;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700398 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700399 }
400 }
401
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700402 private boolean needsDockSettings() {
403 return getResources().getBoolean(R.bool.has_dock_settings);
Amith Yamasanib61cf512010-09-12 08:17:50 -0700404 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700405
406 private void getMetaData() {
407 try {
408 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
409 PackageManager.GET_META_DATA);
410 if (ai == null || ai.metaData == null) return;
411 mTopLevelHeaderId = ai.metaData.getInt(META_DATA_KEY_HEADER_ID);
412 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800413
414 // Check if it has a parent specified and create a Header object
415 final int parentHeaderTitleRes = ai.metaData.getInt(META_DATA_KEY_PARENT_TITLE);
416 String parentFragmentClass = ai.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
417 if (parentFragmentClass != null) {
418 mParentHeader = new Header();
419 mParentHeader.fragment = parentFragmentClass;
420 if (parentHeaderTitleRes != 0) {
421 mParentHeader.title = getResources().getString(parentHeaderTitleRes);
422 }
423 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700424 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700425 // No recovery
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700426 }
427 }
428
Amith Yamasani9e3a4702011-01-11 09:09:26 -0800429 @Override
430 public boolean hasNextButton() {
431 return super.hasNextButton();
432 }
433
434 @Override
435 public Button getNextButton() {
436 return super.getNextButton();
437 }
438
Gilles Debunnee78c1872011-06-20 15:00:07 -0700439 private static class HeaderAdapter extends ArrayAdapter<Header> {
440 static final int HEADER_TYPE_CATEGORY = 0;
441 static final int HEADER_TYPE_NORMAL = 1;
442 static final int HEADER_TYPE_SWITCH = 2;
443 private static final int HEADER_TYPE_COUNT = HEADER_TYPE_SWITCH + 1;
444
445 private final WifiEnabler mWifiEnabler;
446 private final BluetoothEnabler mBluetoothEnabler;
447
448 private static class HeaderViewHolder {
449 ImageView icon;
450 TextView title;
451 TextView summary;
452 Switch switch_;
453 }
454
455 private LayoutInflater mInflater;
456
457 static int getHeaderType(Header header) {
458 if (header.fragment == null && header.intent == null) {
459 return HEADER_TYPE_CATEGORY;
460 } else if (header.id == R.id.wifi_settings || header.id == R.id.bluetooth_settings) {
461 return HEADER_TYPE_SWITCH;
462 } else {
463 return HEADER_TYPE_NORMAL;
464 }
465 }
466
467 @Override
468 public int getItemViewType(int position) {
469 Header header = getItem(position);
470 return getHeaderType(header);
471 }
472
473 @Override
474 public boolean areAllItemsEnabled() {
475 return false; // because of categories
476 }
477
478 @Override
479 public boolean isEnabled(int position) {
480 return getItemViewType(position) != HEADER_TYPE_CATEGORY;
481 }
482
483 @Override
484 public int getViewTypeCount() {
485 return HEADER_TYPE_COUNT;
486 }
487
488 @Override
489 public boolean hasStableIds() {
490 return true;
491 }
492
493 public HeaderAdapter(Context context, List<Header> objects) {
494 super(context, 0, objects);
495 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
496
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700497 // Temp Switches provided as placeholder until the adapter replaces these with actual
Gilles Debunnee78c1872011-06-20 15:00:07 -0700498 // Switches inflated from their layouts. Must be done before adapter is set in super
499 mWifiEnabler = new WifiEnabler(context, new Switch(context));
500 mBluetoothEnabler = new BluetoothEnabler(context, new Switch(context));
501 }
502
503 @Override
504 public View getView(int position, View convertView, ViewGroup parent) {
505 HeaderViewHolder holder;
506 Header header = getItem(position);
507 int headerType = getHeaderType(header);
508 View view = null;
509
510 if (convertView == null) {
511 holder = new HeaderViewHolder();
512 switch (headerType) {
513 case HEADER_TYPE_CATEGORY:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700514 view = new TextView(getContext(), null,
515 android.R.attr.listSeparatorTextViewStyle);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700516 holder.title = (TextView) view;
517 break;
518
519 case HEADER_TYPE_SWITCH:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700520 view = mInflater.inflate(R.layout.preference_header_switch_item, parent,
521 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700522 holder.icon = (ImageView) view.findViewById(R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700523 holder.title = (TextView)
524 view.findViewById(com.android.internal.R.id.title);
525 holder.summary = (TextView)
526 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700527 holder.switch_ = (Switch) view.findViewById(R.id.switchWidget);
528 break;
529
530 case HEADER_TYPE_NORMAL:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700531 view = mInflater.inflate(
532 com.android.internal.R.layout.preference_header_item, parent,
533 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700534 holder.icon = (ImageView) view.findViewById(com.android.internal.R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700535 holder.title = (TextView)
536 view.findViewById(com.android.internal.R.id.title);
537 holder.summary = (TextView)
538 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700539 break;
540 }
541 view.setTag(holder);
542 } else {
543 view = convertView;
544 holder = (HeaderViewHolder) view.getTag();
545 }
546
547 // All view fields must be updated every time, because the view may be recycled
548 switch (headerType) {
549 case HEADER_TYPE_CATEGORY:
550 holder.title.setText(header.getTitle(getContext().getResources()));
551 break;
552
553 case HEADER_TYPE_SWITCH:
554 // Would need a different treatment if the main menu had more switches
555 if (header.id == R.id.wifi_settings) {
556 mWifiEnabler.setSwitch(holder.switch_);
557 } else {
558 mBluetoothEnabler.setSwitch(holder.switch_);
559 }
560 // No break, fall through on purpose to update common fields
561
562 //$FALL-THROUGH$
563 case HEADER_TYPE_NORMAL:
564 holder.icon.setImageResource(header.iconRes);
565 holder.title.setText(header.getTitle(getContext().getResources()));
566 CharSequence summary = header.getSummary(getContext().getResources());
567 if (!TextUtils.isEmpty(summary)) {
568 holder.summary.setVisibility(View.VISIBLE);
569 holder.summary.setText(summary);
570 } else {
571 holder.summary.setVisibility(View.GONE);
572 }
573 break;
574 }
575
576 return view;
577 }
578
579 public void resume() {
580 mWifiEnabler.resume();
581 mBluetoothEnabler.resume();
582 }
583
584 public void pause() {
585 mWifiEnabler.pause();
586 mBluetoothEnabler.pause();
587 }
588 }
589
590 @Override
Amith Yamasania4379d62011-07-22 10:34:58 -0700591 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
592 // Override the fragment title for Wallpaper settings
Amith Yamasanidfb65432011-11-29 16:38:14 -0800593 int titleRes = pref.getTitleRes();
Amith Yamasania4379d62011-07-22 10:34:58 -0700594 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
Amith Yamasanidfb65432011-11-29 16:38:14 -0800595 titleRes = R.string.wallpaper_settings_fragment_title;
Amith Yamasania4379d62011-07-22 10:34:58 -0700596 }
Amith Yamasanidfb65432011-11-29 16:38:14 -0800597 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, null, null, 0);
Amith Yamasania4379d62011-07-22 10:34:58 -0700598 return true;
599 }
600
601 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700602 public void setListAdapter(ListAdapter adapter) {
603 if (mHeaders == null) {
604 mHeaders = new ArrayList<Header>();
605 // When the saved state provides the list of headers, onBuildHeaders is not called
606 // Copy the list of Headers from the adapter, preserving their order
607 for (int i = 0; i < adapter.getCount(); i++) {
608 mHeaders.add((Header) adapter.getItem(i));
609 }
610 }
611
612 // Ignore the adapter provided by PreferenceActivity and substitute ours instead
613 super.setListAdapter(new HeaderAdapter(this, mHeaders));
614 }
615
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700616 /*
617 * Settings subclasses for launching independently.
618 */
Gilles Debunnee78c1872011-06-20 15:00:07 -0700619 public static class BluetoothSettingsActivity extends Settings { /* empty */ }
620 public static class WirelessSettingsActivity extends Settings { /* empty */ }
621 public static class TetherSettingsActivity extends Settings { /* empty */ }
622 public static class VpnSettingsActivity extends Settings { /* empty */ }
623 public static class DateTimeSettingsActivity extends Settings { /* empty */ }
624 public static class StorageSettingsActivity extends Settings { /* empty */ }
625 public static class WifiSettingsActivity extends Settings { /* empty */ }
repo syncb98463f2011-06-30 10:58:43 -0700626 public static class WifiP2pSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700627 public static class InputMethodAndLanguageSettingsActivity extends Settings { /* empty */ }
Jeff Browne46c5f32012-04-05 11:42:18 -0700628 public static class KeyboardLayoutPickerActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700629 public static class InputMethodAndSubtypeEnablerActivity extends Settings { /* empty */ }
satoke077d2b2011-07-25 09:38:11 +0900630 public static class SpellCheckersSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700631 public static class LocalePickerActivity extends Settings { /* empty */ }
632 public static class UserDictionarySettingsActivity extends Settings { /* empty */ }
633 public static class SoundSettingsActivity extends Settings { /* empty */ }
634 public static class DisplaySettingsActivity extends Settings { /* empty */ }
635 public static class DeviceInfoSettingsActivity extends Settings { /* empty */ }
636 public static class ApplicationSettingsActivity extends Settings { /* empty */ }
637 public static class ManageApplicationsActivity extends Settings { /* empty */ }
638 public static class StorageUseActivity extends Settings { /* empty */ }
639 public static class DevelopmentSettingsActivity extends Settings { /* empty */ }
640 public static class AccessibilitySettingsActivity extends Settings { /* empty */ }
641 public static class SecuritySettingsActivity extends Settings { /* empty */ }
Gilles Debunnea6a8a142011-06-09 11:56:17 -0700642 public static class LocationSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700643 public static class PrivacySettingsActivity extends Settings { /* empty */ }
644 public static class DockSettingsActivity extends Settings { /* empty */ }
645 public static class RunningServicesActivity extends Settings { /* empty */ }
646 public static class ManageAccountsSettingsActivity extends Settings { /* empty */ }
647 public static class PowerUsageSummaryActivity extends Settings { /* empty */ }
648 public static class AccountSyncSettingsActivity extends Settings { /* empty */ }
649 public static class AccountSyncSettingsInAddAccountActivity extends Settings { /* empty */ }
650 public static class CryptKeeperSettingsActivity extends Settings { /* empty */ }
651 public static class DeviceAdminSettingsActivity extends Settings { /* empty */ }
652 public static class DataUsageSummaryActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700653 public static class AdvancedWifiSettingsActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700654 public static class TextToSpeechSettingsActivity extends Settings { /* empty */ }
Jeff Hamilton3d670de2011-09-21 16:44:36 -0500655 public static class AndroidBeamSettingsActivity extends Settings { /* empty */ }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800656}