blob: 3d15f556dd09c59d28faaf3489d3c55812334790 [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;
Amith Yamasanib810a0d2012-03-25 10:12:26 -070034import android.os.UserId;
Amith Yamasania4379d62011-07-22 10:34:58 -070035import android.preference.Preference;
Amith Yamasani02cf71a2010-09-21 15:48:52 -070036import android.preference.PreferenceActivity;
Amith Yamasania4379d62011-07-22 10:34:58 -070037import android.preference.PreferenceFragment;
Gilles Debunnee78c1872011-06-20 15:00:07 -070038import android.text.TextUtils;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080039import android.util.Log;
Gilles Debunnee78c1872011-06-20 15:00:07 -070040import android.view.LayoutInflater;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080041import android.view.View;
42import android.view.View.OnClickListener;
Gilles Debunnee78c1872011-06-20 15:00:07 -070043import android.view.ViewGroup;
44import android.widget.ArrayAdapter;
Amith Yamasani9e3a4702011-01-11 09:09:26 -080045import android.widget.Button;
Gilles Debunnee78c1872011-06-20 15:00:07 -070046import android.widget.ImageView;
47import android.widget.ListAdapter;
48import android.widget.Switch;
49import android.widget.TextView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080050
Gilles Debunnee78c1872011-06-20 15:00:07 -070051import java.util.ArrayList;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070052import java.util.HashMap;
Amith Yamasani02cf71a2010-09-21 15:48:52 -070053import java.util.List;
Amith Yamasanid7993472010-08-18 13:59:28 -070054
55/**
56 * Top-level settings activity to handle single pane and double pane UI layout.
57 */
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -080058public class Settings extends PreferenceActivity implements ButtonBarHandler {
Amith Yamasanid7993472010-08-18 13:59:28 -070059
Gilles Debunnee78c1872011-06-20 15:00:07 -070060 private static final String LOG_TAG = "Settings";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070061 private static final String META_DATA_KEY_HEADER_ID =
Gilles Debunnee78c1872011-06-20 15:00:07 -070062 "com.android.settings.TOP_LEVEL_HEADER_ID";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070063 private static final String META_DATA_KEY_FRAGMENT_CLASS =
Gilles Debunnee78c1872011-06-20 15:00:07 -070064 "com.android.settings.FRAGMENT_CLASS";
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080065 private static final String META_DATA_KEY_PARENT_TITLE =
66 "com.android.settings.PARENT_FRAGMENT_TITLE";
67 private static final String META_DATA_KEY_PARENT_FRAGMENT_CLASS =
68 "com.android.settings.PARENT_FRAGMENT_CLASS";
69
Jeff Sharkey54d0af52011-08-11 18:26:57 -070070 private static final String EXTRA_CLEAR_UI_OPTIONS = "settings:remove_ui_options";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070071
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080072 private static final String SAVE_KEY_CURRENT_HEADER = "com.android.settings.CURRENT_HEADER";
73 private static final String SAVE_KEY_PARENT_HEADER = "com.android.settings.PARENT_HEADER";
Amith Yamasani5203bdf2010-11-04 09:59:44 -070074
75 private String mFragmentClass;
76 private int mTopLevelHeaderId;
Amith Yamasani3965ae62010-11-15 14:45:19 -080077 private Header mFirstHeader;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -080078 private Header mCurrentHeader;
79 private Header mParentHeader;
80 private boolean mInLocalHeaderSwitch;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070081
Amith Yamasanib810a0d2012-03-25 10:12:26 -070082 // Show only these settings for restricted users
83 private int[] SETTINGS_FOR_RESTRICTED = {
84 R.id.wifi_settings,
85 R.id.bluetooth_settings,
86 R.id.sound_settings,
87 R.id.display_settings,
88 //R.id.security_settings,
89 R.id.sync_settings,
90 R.id.about_settings
91 };
92
Amith Yamasanifa3eea52012-03-28 10:25:08 -070093 private boolean mEnableUserManagement = false;
94
Amith Yamasani02cf71a2010-09-21 15:48:52 -070095 // TODO: Update Call Settings based on airplane mode state.
Amith Yamasanib61cf512010-09-12 08:17:50 -070096
Amith Yamasani5203bdf2010-11-04 09:59:44 -070097 protected HashMap<Integer, Integer> mHeaderIndexMap = new HashMap<Integer, Integer>();
Gilles Debunnee78c1872011-06-20 15:00:07 -070098 private List<Header> mHeaders;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070099
100 @Override
101 protected void onCreate(Bundle savedInstanceState) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700102 if (getIntent().getBooleanExtra(EXTRA_CLEAR_UI_OPTIONS, false)) {
103 getWindow().setUiOptions(0);
104 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700105
Amith Yamasanifa3eea52012-03-28 10:25:08 -0700106 if (android.provider.Settings.Secure.getInt(getContentResolver(), "multiuser_enabled", -1)
107 > 0) {
108 mEnableUserManagement = true;
109 }
110
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700111 getMetaData();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800112 mInLocalHeaderSwitch = true;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700113 super.onCreate(savedInstanceState);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800114 mInLocalHeaderSwitch = false;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700115
Gilles Debunne3661b622011-06-27 11:19:16 -0700116 if (!onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasani72aa19d2010-12-09 06:07:12 -0800117 highlightHeader();
118 // Force the title so that it doesn't get overridden by a direct launch of
119 // a specific settings screen.
120 setTitle(R.string.settings_label);
121 }
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800122
123 // Retrieve any saved state
124 if (savedInstanceState != null) {
125 mCurrentHeader = savedInstanceState.getParcelable(SAVE_KEY_CURRENT_HEADER);
126 mParentHeader = savedInstanceState.getParcelable(SAVE_KEY_PARENT_HEADER);
127 }
128
129 // If the current header was saved, switch to it
130 if (savedInstanceState != null && mCurrentHeader != null) {
131 //switchToHeaderLocal(mCurrentHeader);
132 showBreadCrumbs(mCurrentHeader.title, null);
133 }
134
135 if (mParentHeader != null) {
136 setParentTitle(mParentHeader.title, null, new OnClickListener() {
137 public void onClick(View v) {
138 switchToParent(mParentHeader.fragment);
139 }
140 });
141 }
Gilles Debunnedc7101f2011-06-27 12:00:49 -0700142
143 // TODO Add support for android.R.id.home in all Setting's onOptionsItemSelected
144 // getActionBar().setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP,
145 // ActionBar.DISPLAY_HOME_AS_UP);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800146 }
147
148 @Override
149 protected void onSaveInstanceState(Bundle outState) {
150 super.onSaveInstanceState(outState);
151
152 // Save the current fragment, if it is the same as originally launched
153 if (mCurrentHeader != null) {
154 outState.putParcelable(SAVE_KEY_CURRENT_HEADER, mCurrentHeader);
155 }
156 if (mParentHeader != null) {
157 outState.putParcelable(SAVE_KEY_PARENT_HEADER, mParentHeader);
158 }
159 }
160
Gilles Debunnee78c1872011-06-20 15:00:07 -0700161 @Override
162 public void onResume() {
163 super.onResume();
164
165 ListAdapter listAdapter = getListAdapter();
166 if (listAdapter instanceof HeaderAdapter) {
167 ((HeaderAdapter) listAdapter).resume();
168 }
169 }
170
171 @Override
172 public void onPause() {
173 super.onPause();
174
175 ListAdapter listAdapter = getListAdapter();
176 if (listAdapter instanceof HeaderAdapter) {
177 ((HeaderAdapter) listAdapter).pause();
178 }
179 }
180
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800181 private void switchToHeaderLocal(Header header) {
182 mInLocalHeaderSwitch = true;
183 switchToHeader(header);
184 mInLocalHeaderSwitch = false;
185 }
186
187 @Override
188 public void switchToHeader(Header header) {
189 if (!mInLocalHeaderSwitch) {
190 mCurrentHeader = null;
191 mParentHeader = null;
192 }
193 super.switchToHeader(header);
194 }
195
196 /**
197 * Switch to parent fragment and store the grand parent's info
Jake Hamby2748fc22011-01-12 15:06:28 -0800198 * @param className name of the activity wrapper for the parent fragment.
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800199 */
200 private void switchToParent(String className) {
201 final ComponentName cn = new ComponentName(this, className);
202 try {
203 final PackageManager pm = getPackageManager();
204 final ActivityInfo parentInfo = pm.getActivityInfo(cn, PackageManager.GET_META_DATA);
205
206 if (parentInfo != null && parentInfo.metaData != null) {
207 String fragmentClass = parentInfo.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
208 CharSequence fragmentTitle = parentInfo.loadLabel(pm);
209 Header parentHeader = new Header();
210 parentHeader.fragment = fragmentClass;
211 parentHeader.title = fragmentTitle;
212 mCurrentHeader = parentHeader;
213
214 switchToHeaderLocal(parentHeader);
Amith Yamasani990fb522011-09-02 09:47:18 -0700215 highlightHeader();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800216
217 mParentHeader = new Header();
218 mParentHeader.fragment
219 = parentInfo.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
220 mParentHeader.title = parentInfo.metaData.getString(META_DATA_KEY_PARENT_TITLE);
221 }
222 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700223 Log.w(LOG_TAG, "Could not find parent activity : " + className);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800224 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700225 }
226
Amith Yamasani3965ae62010-11-15 14:45:19 -0800227 @Override
228 public void onNewIntent(Intent intent) {
229 super.onNewIntent(intent);
230
231 // If it is not launched from history, then reset to top-level
232 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0
Gilles Debunne3661b622011-06-27 11:19:16 -0700233 && mFirstHeader != null && !onIsHidingHeaders() && onIsMultiPane()) {
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800234 switchToHeaderLocal(mFirstHeader);
Amith Yamasani3965ae62010-11-15 14:45:19 -0800235 }
236 }
237
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700238 private void highlightHeader() {
239 if (mTopLevelHeaderId != 0) {
240 Integer index = mHeaderIndexMap.get(mTopLevelHeaderId);
241 if (index != null) {
242 getListView().setItemChecked(index, true);
Amith Yamasani990fb522011-09-02 09:47:18 -0700243 getListView().smoothScrollToPosition(index);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700244 }
245 }
246 }
247
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700248 @Override
249 public Intent getIntent() {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700250 Intent superIntent = super.getIntent();
251 String startingFragment = getStartingFragmentClass(superIntent);
Gilles Debunne3661b622011-06-27 11:19:16 -0700252 // This is called from super.onCreate, isMultiPane() is not yet reliable
253 // Do not use onIsHidingHeaders either, which relies itself on this method
254 if (startingFragment != null && !onIsMultiPane()) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700255 Intent modIntent = new Intent(superIntent);
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700256 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700257 Bundle args = superIntent.getExtras();
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700258 if (args != null) {
259 args = new Bundle(args);
260 } else {
261 args = new Bundle();
262 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700263 args.putParcelable("intent", superIntent);
264 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras());
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700265 return modIntent;
266 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700267 return superIntent;
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700268 }
269
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700270 /**
Amith Yamasani379d9b02010-09-27 12:03:59 -0700271 * Checks if the component name in the intent is different from the Settings class and
272 * returns the class name to load as a fragment.
273 */
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700274 protected String getStartingFragmentClass(Intent intent) {
275 if (mFragmentClass != null) return mFragmentClass;
276
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700277 String intentClass = intent.getComponent().getClassName();
Amith Yamasani379d9b02010-09-27 12:03:59 -0700278 if (intentClass.equals(getClass().getName())) return null;
279
Dianne Hackbornee293792010-11-01 12:32:33 -0700280 if ("com.android.settings.ManageApplications".equals(intentClass)
281 || "com.android.settings.RunningServices".equals(intentClass)
282 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700283 // Old names of manage apps.
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700284 intentClass = com.android.settings.applications.ManageApplications.class.getName();
285 }
286
Amith Yamasani379d9b02010-09-27 12:03:59 -0700287 return intentClass;
288 }
289
290 /**
291 * Override initial header when an activity-alias is causing Settings to be launched
292 * for a specific fragment encoded in the android:name parameter.
293 */
294 @Override
295 public Header onGetInitialHeader() {
296 String fragmentClass = getStartingFragmentClass(super.getIntent());
297 if (fragmentClass != null) {
298 Header header = new Header();
299 header.fragment = fragmentClass;
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800300 header.title = getTitle();
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700301 header.fragmentArguments = getIntent().getExtras();
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800302 mCurrentHeader = header;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700303 return header;
304 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700305
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700306 return mFirstHeader;
Amith Yamasani379d9b02010-09-27 12:03:59 -0700307 }
308
Dianne Hackbornb7258182011-03-15 16:23:55 -0700309 @Override
Dianne Hackborn48147dc2011-03-18 12:29:41 -0700310 public Intent onBuildStartFragmentIntent(String fragmentName, Bundle args,
311 int titleRes, int shortTitleRes) {
312 Intent intent = super.onBuildStartFragmentIntent(fragmentName, args,
313 titleRes, shortTitleRes);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700314
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700315 // some fragments want to avoid split actionbar
Gilles Debunne162e5412011-07-11 14:13:31 -0700316 if (DataUsageSummary.class.getName().equals(fragmentName) ||
317 PowerUsageSummary.class.getName().equals(fragmentName) ||
Gilles Debunnecd8e5242011-07-25 11:36:15 -0700318 AccountSyncSettings.class.getName().equals(fragmentName) ||
Jeff Sharkey11d30122012-03-19 16:54:07 -0700319 UserDictionarySettings.class.getName().equals(fragmentName) ||
Adam Powellfaba7e42012-03-26 17:28:38 -0700320 Memory.class.getName().equals(fragmentName) ||
321 ManageApplications.class.getName().equals(fragmentName)) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700322 intent.putExtra(EXTRA_CLEAR_UI_OPTIONS, true);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700323 }
324
Dianne Hackbornb7258182011-03-15 16:23:55 -0700325 intent.setClass(this, SubSettings.class);
326 return intent;
327 }
328
Amith Yamasani379d9b02010-09-27 12:03:59 -0700329 /**
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700330 * Populate the activity with the top-level headers.
331 */
Amith Yamasanid7993472010-08-18 13:59:28 -0700332 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700333 public void onBuildHeaders(List<Header> headers) {
334 loadHeadersFromResource(R.xml.settings_headers, headers);
Amith Yamasanid7993472010-08-18 13:59:28 -0700335
Gilles Debunnee78c1872011-06-20 15:00:07 -0700336 updateHeaderList(headers);
337
338 mHeaders = headers;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700339 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700340
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700341 private void updateHeaderList(List<Header> target) {
342 int i = 0;
343 while (i < target.size()) {
344 Header header = target.get(i);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700345 // Ids are integers, so downcasting
346 int id = (int) header.id;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700347 if (id == R.id.dock_settings) {
348 if (!needsDockSettings())
349 target.remove(header);
350 } else if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
351 Utils.updateHeaderToSpecificActivityFromMetaDataOrRemove(this, target, header);
Gilles Debunne2454f492011-06-21 16:16:06 -0700352 } else if (id == R.id.wifi_settings) {
353 // Remove WiFi Settings if WiFi service is not available.
354 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
355 target.remove(header);
356 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700357 } else if (id == R.id.bluetooth_settings) {
358 // Remove Bluetooth Settings if Bluetooth service is not available.
Gilles Debunne2454f492011-06-21 16:16:06 -0700359 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700360 target.remove(header);
361 }
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700362 } else if (id == R.id.user_settings) {
Amith Yamasanifa3eea52012-03-28 10:25:08 -0700363 if (!mEnableUserManagement
364 || !UserId.MU_ENABLED || UserId.myUserId() != 0
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700365 || !getResources().getBoolean(R.bool.enable_user_management)
366 || Utils.isMonkeyRunning()) {
367 target.remove(header);
368 }
369 }
370 if (UserId.MU_ENABLED && UserId.myUserId() != 0
371 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
372 target.remove(header);
Amith Yamasanid7993472010-08-18 13:59:28 -0700373 }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700374
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700375 // Increment if the current one wasn't removed by the Utils code.
376 if (target.get(i) == header) {
Amith Yamasani3965ae62010-11-15 14:45:19 -0800377 // Hold on to the first header, when we need to reset to the top-level
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700378 if (mFirstHeader == null &&
379 HeaderAdapter.getHeaderType(header) != HeaderAdapter.HEADER_TYPE_CATEGORY) {
380 mFirstHeader = header;
381 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700382 mHeaderIndexMap.put(id, i);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700383 i++;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700384 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700385 }
386 }
387
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700388 private boolean needsDockSettings() {
389 return getResources().getBoolean(R.bool.has_dock_settings);
Amith Yamasanib61cf512010-09-12 08:17:50 -0700390 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700391
392 private void getMetaData() {
393 try {
394 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
395 PackageManager.GET_META_DATA);
396 if (ai == null || ai.metaData == null) return;
397 mTopLevelHeaderId = ai.metaData.getInt(META_DATA_KEY_HEADER_ID);
398 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
Amith Yamasanic9fdfa82010-12-14 14:38:16 -0800399
400 // Check if it has a parent specified and create a Header object
401 final int parentHeaderTitleRes = ai.metaData.getInt(META_DATA_KEY_PARENT_TITLE);
402 String parentFragmentClass = ai.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
403 if (parentFragmentClass != null) {
404 mParentHeader = new Header();
405 mParentHeader.fragment = parentFragmentClass;
406 if (parentHeaderTitleRes != 0) {
407 mParentHeader.title = getResources().getString(parentHeaderTitleRes);
408 }
409 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700410 } catch (NameNotFoundException nnfe) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700411 // No recovery
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700412 }
413 }
414
Amith Yamasani9e3a4702011-01-11 09:09:26 -0800415 @Override
416 public boolean hasNextButton() {
417 return super.hasNextButton();
418 }
419
420 @Override
421 public Button getNextButton() {
422 return super.getNextButton();
423 }
424
Gilles Debunnee78c1872011-06-20 15:00:07 -0700425 private static class HeaderAdapter extends ArrayAdapter<Header> {
426 static final int HEADER_TYPE_CATEGORY = 0;
427 static final int HEADER_TYPE_NORMAL = 1;
428 static final int HEADER_TYPE_SWITCH = 2;
429 private static final int HEADER_TYPE_COUNT = HEADER_TYPE_SWITCH + 1;
430
431 private final WifiEnabler mWifiEnabler;
432 private final BluetoothEnabler mBluetoothEnabler;
433
434 private static class HeaderViewHolder {
435 ImageView icon;
436 TextView title;
437 TextView summary;
438 Switch switch_;
439 }
440
441 private LayoutInflater mInflater;
442
443 static int getHeaderType(Header header) {
444 if (header.fragment == null && header.intent == null) {
445 return HEADER_TYPE_CATEGORY;
446 } else if (header.id == R.id.wifi_settings || header.id == R.id.bluetooth_settings) {
447 return HEADER_TYPE_SWITCH;
448 } else {
449 return HEADER_TYPE_NORMAL;
450 }
451 }
452
453 @Override
454 public int getItemViewType(int position) {
455 Header header = getItem(position);
456 return getHeaderType(header);
457 }
458
459 @Override
460 public boolean areAllItemsEnabled() {
461 return false; // because of categories
462 }
463
464 @Override
465 public boolean isEnabled(int position) {
466 return getItemViewType(position) != HEADER_TYPE_CATEGORY;
467 }
468
469 @Override
470 public int getViewTypeCount() {
471 return HEADER_TYPE_COUNT;
472 }
473
474 @Override
475 public boolean hasStableIds() {
476 return true;
477 }
478
479 public HeaderAdapter(Context context, List<Header> objects) {
480 super(context, 0, objects);
481 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
482
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700483 // Temp Switches provided as placeholder until the adapter replaces these with actual
Gilles Debunnee78c1872011-06-20 15:00:07 -0700484 // Switches inflated from their layouts. Must be done before adapter is set in super
485 mWifiEnabler = new WifiEnabler(context, new Switch(context));
486 mBluetoothEnabler = new BluetoothEnabler(context, new Switch(context));
487 }
488
489 @Override
490 public View getView(int position, View convertView, ViewGroup parent) {
491 HeaderViewHolder holder;
492 Header header = getItem(position);
493 int headerType = getHeaderType(header);
494 View view = null;
495
496 if (convertView == null) {
497 holder = new HeaderViewHolder();
498 switch (headerType) {
499 case HEADER_TYPE_CATEGORY:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700500 view = new TextView(getContext(), null,
501 android.R.attr.listSeparatorTextViewStyle);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700502 holder.title = (TextView) view;
503 break;
504
505 case HEADER_TYPE_SWITCH:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700506 view = mInflater.inflate(R.layout.preference_header_switch_item, parent,
507 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700508 holder.icon = (ImageView) view.findViewById(R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700509 holder.title = (TextView)
510 view.findViewById(com.android.internal.R.id.title);
511 holder.summary = (TextView)
512 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700513 holder.switch_ = (Switch) view.findViewById(R.id.switchWidget);
514 break;
515
516 case HEADER_TYPE_NORMAL:
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700517 view = mInflater.inflate(
518 com.android.internal.R.layout.preference_header_item, parent,
519 false);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700520 holder.icon = (ImageView) view.findViewById(com.android.internal.R.id.icon);
Gilles Debunneb396c9b2011-06-22 16:07:29 -0700521 holder.title = (TextView)
522 view.findViewById(com.android.internal.R.id.title);
523 holder.summary = (TextView)
524 view.findViewById(com.android.internal.R.id.summary);
Gilles Debunnee78c1872011-06-20 15:00:07 -0700525 break;
526 }
527 view.setTag(holder);
528 } else {
529 view = convertView;
530 holder = (HeaderViewHolder) view.getTag();
531 }
532
533 // All view fields must be updated every time, because the view may be recycled
534 switch (headerType) {
535 case HEADER_TYPE_CATEGORY:
536 holder.title.setText(header.getTitle(getContext().getResources()));
537 break;
538
539 case HEADER_TYPE_SWITCH:
540 // Would need a different treatment if the main menu had more switches
541 if (header.id == R.id.wifi_settings) {
542 mWifiEnabler.setSwitch(holder.switch_);
543 } else {
544 mBluetoothEnabler.setSwitch(holder.switch_);
545 }
546 // No break, fall through on purpose to update common fields
547
548 //$FALL-THROUGH$
549 case HEADER_TYPE_NORMAL:
550 holder.icon.setImageResource(header.iconRes);
551 holder.title.setText(header.getTitle(getContext().getResources()));
552 CharSequence summary = header.getSummary(getContext().getResources());
553 if (!TextUtils.isEmpty(summary)) {
554 holder.summary.setVisibility(View.VISIBLE);
555 holder.summary.setText(summary);
556 } else {
557 holder.summary.setVisibility(View.GONE);
558 }
559 break;
560 }
561
562 return view;
563 }
564
565 public void resume() {
566 mWifiEnabler.resume();
567 mBluetoothEnabler.resume();
568 }
569
570 public void pause() {
571 mWifiEnabler.pause();
572 mBluetoothEnabler.pause();
573 }
574 }
575
576 @Override
Amith Yamasania4379d62011-07-22 10:34:58 -0700577 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
578 // Override the fragment title for Wallpaper settings
Amith Yamasanidfb65432011-11-29 16:38:14 -0800579 int titleRes = pref.getTitleRes();
Amith Yamasania4379d62011-07-22 10:34:58 -0700580 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
Amith Yamasanidfb65432011-11-29 16:38:14 -0800581 titleRes = R.string.wallpaper_settings_fragment_title;
Amith Yamasania4379d62011-07-22 10:34:58 -0700582 }
Amith Yamasanidfb65432011-11-29 16:38:14 -0800583 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, null, null, 0);
Amith Yamasania4379d62011-07-22 10:34:58 -0700584 return true;
585 }
586
587 @Override
Gilles Debunnee78c1872011-06-20 15:00:07 -0700588 public void setListAdapter(ListAdapter adapter) {
589 if (mHeaders == null) {
590 mHeaders = new ArrayList<Header>();
591 // When the saved state provides the list of headers, onBuildHeaders is not called
592 // Copy the list of Headers from the adapter, preserving their order
593 for (int i = 0; i < adapter.getCount(); i++) {
594 mHeaders.add((Header) adapter.getItem(i));
595 }
596 }
597
598 // Ignore the adapter provided by PreferenceActivity and substitute ours instead
599 super.setListAdapter(new HeaderAdapter(this, mHeaders));
600 }
601
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700602 /*
603 * Settings subclasses for launching independently.
604 */
Gilles Debunnee78c1872011-06-20 15:00:07 -0700605 public static class BluetoothSettingsActivity extends Settings { /* empty */ }
606 public static class WirelessSettingsActivity extends Settings { /* empty */ }
607 public static class TetherSettingsActivity extends Settings { /* empty */ }
608 public static class VpnSettingsActivity extends Settings { /* empty */ }
609 public static class DateTimeSettingsActivity extends Settings { /* empty */ }
610 public static class StorageSettingsActivity extends Settings { /* empty */ }
611 public static class WifiSettingsActivity extends Settings { /* empty */ }
repo syncb98463f2011-06-30 10:58:43 -0700612 public static class WifiP2pSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700613 public static class InputMethodAndLanguageSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700614 public static class InputMethodAndSubtypeEnablerActivity extends Settings { /* empty */ }
satoke077d2b2011-07-25 09:38:11 +0900615 public static class SpellCheckersSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700616 public static class LocalePickerActivity extends Settings { /* empty */ }
617 public static class UserDictionarySettingsActivity extends Settings { /* empty */ }
618 public static class SoundSettingsActivity extends Settings { /* empty */ }
619 public static class DisplaySettingsActivity extends Settings { /* empty */ }
620 public static class DeviceInfoSettingsActivity extends Settings { /* empty */ }
621 public static class ApplicationSettingsActivity extends Settings { /* empty */ }
622 public static class ManageApplicationsActivity extends Settings { /* empty */ }
623 public static class StorageUseActivity extends Settings { /* empty */ }
624 public static class DevelopmentSettingsActivity extends Settings { /* empty */ }
625 public static class AccessibilitySettingsActivity extends Settings { /* empty */ }
626 public static class SecuritySettingsActivity extends Settings { /* empty */ }
Gilles Debunnea6a8a142011-06-09 11:56:17 -0700627 public static class LocationSettingsActivity extends Settings { /* empty */ }
Gilles Debunnee78c1872011-06-20 15:00:07 -0700628 public static class PrivacySettingsActivity extends Settings { /* empty */ }
629 public static class DockSettingsActivity extends Settings { /* empty */ }
630 public static class RunningServicesActivity extends Settings { /* empty */ }
631 public static class ManageAccountsSettingsActivity extends Settings { /* empty */ }
632 public static class PowerUsageSummaryActivity extends Settings { /* empty */ }
633 public static class AccountSyncSettingsActivity extends Settings { /* empty */ }
634 public static class AccountSyncSettingsInAddAccountActivity extends Settings { /* empty */ }
635 public static class CryptKeeperSettingsActivity extends Settings { /* empty */ }
636 public static class DeviceAdminSettingsActivity extends Settings { /* empty */ }
637 public static class DataUsageSummaryActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700638 public static class AdvancedWifiSettingsActivity extends Settings { /* empty */ }
Gilles Debunneab189bd2011-07-06 13:10:16 -0700639 public static class TextToSpeechSettingsActivity extends Settings { /* empty */ }
Jeff Hamilton3d670de2011-09-21 16:44:36 -0500640 public static class AndroidBeamSettingsActivity extends Settings { /* empty */ }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800641}