The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settings; |
| 18 | |
Amith Yamasani | c9fdfa8 | 2010-12-14 14:38:16 -0800 | [diff] [blame] | 19 | import android.content.ComponentName; |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Amith Yamasani | 379d9b0 | 2010-09-27 12:03:59 -0700 | [diff] [blame] | 21 | import android.content.Intent; |
Amith Yamasani | 5203bdf | 2010-11-04 09:59:44 -0700 | [diff] [blame] | 22 | import android.content.pm.ActivityInfo; |
| 23 | import android.content.pm.PackageManager; |
| 24 | import android.content.pm.PackageManager.NameNotFoundException; |
Dianne Hackborn | f4eb85b | 2010-10-29 16:53:04 -0700 | [diff] [blame] | 25 | import android.os.Bundle; |
Amith Yamasani | 02cf71a | 2010-09-21 15:48:52 -0700 | [diff] [blame] | 26 | import android.preference.PreferenceActivity; |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 27 | import android.text.TextUtils; |
Amith Yamasani | c9fdfa8 | 2010-12-14 14:38:16 -0800 | [diff] [blame] | 28 | import android.util.Log; |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 29 | import android.view.LayoutInflater; |
Amith Yamasani | c9fdfa8 | 2010-12-14 14:38:16 -0800 | [diff] [blame] | 30 | import android.view.View; |
| 31 | import android.view.View.OnClickListener; |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 32 | import android.view.ViewGroup; |
| 33 | import android.widget.ArrayAdapter; |
Amith Yamasani | 9e3a470 | 2011-01-11 09:09:26 -0800 | [diff] [blame] | 34 | import android.widget.Button; |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 35 | import android.widget.ImageView; |
| 36 | import android.widget.ListAdapter; |
| 37 | import android.widget.Switch; |
| 38 | import android.widget.TextView; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 39 | |
Gilles Debunne | 162e541 | 2011-07-11 14:13:31 -0700 | [diff] [blame] | 40 | import com.android.settings.accounts.ManageAccountsSettings; |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 41 | import com.android.settings.bluetooth.BluetoothEnabler; |
Gilles Debunne | 162e541 | 2011-07-11 14:13:31 -0700 | [diff] [blame] | 42 | import com.android.settings.fuelgauge.PowerUsageSummary; |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 43 | import com.android.settings.wifi.WifiEnabler; |
| 44 | |
| 45 | import java.util.ArrayList; |
Amith Yamasani | 5203bdf | 2010-11-04 09:59:44 -0700 | [diff] [blame] | 46 | import java.util.HashMap; |
Amith Yamasani | 02cf71a | 2010-09-21 15:48:52 -0700 | [diff] [blame] | 47 | import java.util.List; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Top-level settings activity to handle single pane and double pane UI layout. |
| 51 | */ |
Daisuke Miyakawa | 79c5fd9 | 2011-01-15 14:58:00 -0800 | [diff] [blame] | 52 | public class Settings extends PreferenceActivity implements ButtonBarHandler { |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 53 | |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 54 | private static final String LOG_TAG = "Settings"; |
Amith Yamasani | 5203bdf | 2010-11-04 09:59:44 -0700 | [diff] [blame] | 55 | private static final String META_DATA_KEY_HEADER_ID = |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 56 | "com.android.settings.TOP_LEVEL_HEADER_ID"; |
Amith Yamasani | 5203bdf | 2010-11-04 09:59:44 -0700 | [diff] [blame] | 57 | private static final String META_DATA_KEY_FRAGMENT_CLASS = |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 58 | "com.android.settings.FRAGMENT_CLASS"; |
Amith Yamasani | c9fdfa8 | 2010-12-14 14:38:16 -0800 | [diff] [blame] | 59 | private static final String META_DATA_KEY_PARENT_TITLE = |
| 60 | "com.android.settings.PARENT_FRAGMENT_TITLE"; |
| 61 | private static final String META_DATA_KEY_PARENT_FRAGMENT_CLASS = |
| 62 | "com.android.settings.PARENT_FRAGMENT_CLASS"; |
| 63 | |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 64 | private static final String EXTRA_THEME = "settings:theme"; |
| 65 | |
Amith Yamasani | c9fdfa8 | 2010-12-14 14:38:16 -0800 | [diff] [blame] | 66 | private static final String SAVE_KEY_CURRENT_HEADER = "com.android.settings.CURRENT_HEADER"; |
| 67 | private static final String SAVE_KEY_PARENT_HEADER = "com.android.settings.PARENT_HEADER"; |
Amith Yamasani | 5203bdf | 2010-11-04 09:59:44 -0700 | [diff] [blame] | 68 | |
| 69 | private String mFragmentClass; |
| 70 | private int mTopLevelHeaderId; |
Amith Yamasani | 3965ae6 | 2010-11-15 14:45:19 -0800 | [diff] [blame] | 71 | private Header mFirstHeader; |
Amith Yamasani | c9fdfa8 | 2010-12-14 14:38:16 -0800 | [diff] [blame] | 72 | private Header mCurrentHeader; |
| 73 | private Header mParentHeader; |
| 74 | private boolean mInLocalHeaderSwitch; |
Amith Yamasani | 5203bdf | 2010-11-04 09:59:44 -0700 | [diff] [blame] | 75 | |
Amith Yamasani | 02cf71a | 2010-09-21 15:48:52 -0700 | [diff] [blame] | 76 | // TODO: Update Call Settings based on airplane mode state. |
Amith Yamasani | b61cf51 | 2010-09-12 08:17:50 -0700 | [diff] [blame] | 77 | |
Amith Yamasani | 5203bdf | 2010-11-04 09:59:44 -0700 | [diff] [blame] | 78 | protected HashMap<Integer, Integer> mHeaderIndexMap = new HashMap<Integer, Integer>(); |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 79 | private List<Header> mHeaders; |
Amith Yamasani | 5203bdf | 2010-11-04 09:59:44 -0700 | [diff] [blame] | 80 | |
| 81 | @Override |
| 82 | protected void onCreate(Bundle savedInstanceState) { |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 83 | final int theme = getIntent().getIntExtra( |
| 84 | EXTRA_THEME, android.R.style.Theme_Holo_SolidActionBar_SplitActionBarWhenNarrow); |
| 85 | setTheme(theme); |
| 86 | |
Amith Yamasani | 5203bdf | 2010-11-04 09:59:44 -0700 | [diff] [blame] | 87 | getMetaData(); |
Amith Yamasani | c9fdfa8 | 2010-12-14 14:38:16 -0800 | [diff] [blame] | 88 | mInLocalHeaderSwitch = true; |
Amith Yamasani | 5203bdf | 2010-11-04 09:59:44 -0700 | [diff] [blame] | 89 | super.onCreate(savedInstanceState); |
Amith Yamasani | c9fdfa8 | 2010-12-14 14:38:16 -0800 | [diff] [blame] | 90 | mInLocalHeaderSwitch = false; |
Amith Yamasani | 5203bdf | 2010-11-04 09:59:44 -0700 | [diff] [blame] | 91 | |
Gilles Debunne | 3661b62 | 2011-06-27 11:19:16 -0700 | [diff] [blame] | 92 | if (!onIsHidingHeaders() && onIsMultiPane()) { |
Amith Yamasani | 72aa19d | 2010-12-09 06:07:12 -0800 | [diff] [blame] | 93 | highlightHeader(); |
| 94 | // Force the title so that it doesn't get overridden by a direct launch of |
| 95 | // a specific settings screen. |
| 96 | setTitle(R.string.settings_label); |
| 97 | } |
Amith Yamasani | c9fdfa8 | 2010-12-14 14:38:16 -0800 | [diff] [blame] | 98 | |
| 99 | // Retrieve any saved state |
| 100 | if (savedInstanceState != null) { |
| 101 | mCurrentHeader = savedInstanceState.getParcelable(SAVE_KEY_CURRENT_HEADER); |
| 102 | mParentHeader = savedInstanceState.getParcelable(SAVE_KEY_PARENT_HEADER); |
| 103 | } |
| 104 | |
| 105 | // If the current header was saved, switch to it |
| 106 | if (savedInstanceState != null && mCurrentHeader != null) { |
| 107 | //switchToHeaderLocal(mCurrentHeader); |
| 108 | showBreadCrumbs(mCurrentHeader.title, null); |
| 109 | } |
| 110 | |
| 111 | if (mParentHeader != null) { |
| 112 | setParentTitle(mParentHeader.title, null, new OnClickListener() { |
| 113 | public void onClick(View v) { |
| 114 | switchToParent(mParentHeader.fragment); |
| 115 | } |
| 116 | }); |
| 117 | } |
Gilles Debunne | dc7101f | 2011-06-27 12:00:49 -0700 | [diff] [blame] | 118 | |
| 119 | // TODO Add support for android.R.id.home in all Setting's onOptionsItemSelected |
| 120 | // getActionBar().setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP, |
| 121 | // ActionBar.DISPLAY_HOME_AS_UP); |
Amith Yamasani | c9fdfa8 | 2010-12-14 14:38:16 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Override |
| 125 | protected void onSaveInstanceState(Bundle outState) { |
| 126 | super.onSaveInstanceState(outState); |
| 127 | |
| 128 | // Save the current fragment, if it is the same as originally launched |
| 129 | if (mCurrentHeader != null) { |
| 130 | outState.putParcelable(SAVE_KEY_CURRENT_HEADER, mCurrentHeader); |
| 131 | } |
| 132 | if (mParentHeader != null) { |
| 133 | outState.putParcelable(SAVE_KEY_PARENT_HEADER, mParentHeader); |
| 134 | } |
| 135 | } |
| 136 | |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 137 | @Override |
| 138 | public void onResume() { |
| 139 | super.onResume(); |
| 140 | |
| 141 | ListAdapter listAdapter = getListAdapter(); |
| 142 | if (listAdapter instanceof HeaderAdapter) { |
| 143 | ((HeaderAdapter) listAdapter).resume(); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void onPause() { |
| 149 | super.onPause(); |
| 150 | |
| 151 | ListAdapter listAdapter = getListAdapter(); |
| 152 | if (listAdapter instanceof HeaderAdapter) { |
| 153 | ((HeaderAdapter) listAdapter).pause(); |
| 154 | } |
| 155 | } |
| 156 | |
Amith Yamasani | c9fdfa8 | 2010-12-14 14:38:16 -0800 | [diff] [blame] | 157 | private void switchToHeaderLocal(Header header) { |
| 158 | mInLocalHeaderSwitch = true; |
| 159 | switchToHeader(header); |
| 160 | mInLocalHeaderSwitch = false; |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public void switchToHeader(Header header) { |
| 165 | if (!mInLocalHeaderSwitch) { |
| 166 | mCurrentHeader = null; |
| 167 | mParentHeader = null; |
| 168 | } |
| 169 | super.switchToHeader(header); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Switch to parent fragment and store the grand parent's info |
Jake Hamby | 2748fc2 | 2011-01-12 15:06:28 -0800 | [diff] [blame] | 174 | * @param className name of the activity wrapper for the parent fragment. |
Amith Yamasani | c9fdfa8 | 2010-12-14 14:38:16 -0800 | [diff] [blame] | 175 | */ |
| 176 | private void switchToParent(String className) { |
| 177 | final ComponentName cn = new ComponentName(this, className); |
| 178 | try { |
| 179 | final PackageManager pm = getPackageManager(); |
| 180 | final ActivityInfo parentInfo = pm.getActivityInfo(cn, PackageManager.GET_META_DATA); |
| 181 | |
| 182 | if (parentInfo != null && parentInfo.metaData != null) { |
| 183 | String fragmentClass = parentInfo.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS); |
| 184 | CharSequence fragmentTitle = parentInfo.loadLabel(pm); |
| 185 | Header parentHeader = new Header(); |
| 186 | parentHeader.fragment = fragmentClass; |
| 187 | parentHeader.title = fragmentTitle; |
| 188 | mCurrentHeader = parentHeader; |
| 189 | |
| 190 | switchToHeaderLocal(parentHeader); |
| 191 | |
| 192 | mParentHeader = new Header(); |
| 193 | mParentHeader.fragment |
| 194 | = parentInfo.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS); |
| 195 | mParentHeader.title = parentInfo.metaData.getString(META_DATA_KEY_PARENT_TITLE); |
| 196 | } |
| 197 | } catch (NameNotFoundException nnfe) { |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 198 | Log.w(LOG_TAG, "Could not find parent activity : " + className); |
Amith Yamasani | c9fdfa8 | 2010-12-14 14:38:16 -0800 | [diff] [blame] | 199 | } |
Amith Yamasani | 5203bdf | 2010-11-04 09:59:44 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Amith Yamasani | 3965ae6 | 2010-11-15 14:45:19 -0800 | [diff] [blame] | 202 | @Override |
| 203 | public void onNewIntent(Intent intent) { |
| 204 | super.onNewIntent(intent); |
| 205 | |
| 206 | // If it is not launched from history, then reset to top-level |
| 207 | if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0 |
Gilles Debunne | 3661b62 | 2011-06-27 11:19:16 -0700 | [diff] [blame] | 208 | && mFirstHeader != null && !onIsHidingHeaders() && onIsMultiPane()) { |
Amith Yamasani | c9fdfa8 | 2010-12-14 14:38:16 -0800 | [diff] [blame] | 209 | switchToHeaderLocal(mFirstHeader); |
Amith Yamasani | 3965ae6 | 2010-11-15 14:45:19 -0800 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
Amith Yamasani | 5203bdf | 2010-11-04 09:59:44 -0700 | [diff] [blame] | 213 | private void highlightHeader() { |
| 214 | if (mTopLevelHeaderId != 0) { |
| 215 | Integer index = mHeaderIndexMap.get(mTopLevelHeaderId); |
| 216 | if (index != null) { |
| 217 | getListView().setItemChecked(index, true); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
Amith Yamasani | e0e4fc2 | 2010-10-05 11:49:51 -0700 | [diff] [blame] | 222 | @Override |
| 223 | public Intent getIntent() { |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 224 | Intent superIntent = super.getIntent(); |
| 225 | String startingFragment = getStartingFragmentClass(superIntent); |
Gilles Debunne | 3661b62 | 2011-06-27 11:19:16 -0700 | [diff] [blame] | 226 | // This is called from super.onCreate, isMultiPane() is not yet reliable |
| 227 | // Do not use onIsHidingHeaders either, which relies itself on this method |
| 228 | if (startingFragment != null && !onIsMultiPane()) { |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 229 | Intent modIntent = new Intent(superIntent); |
Amith Yamasani | e0e4fc2 | 2010-10-05 11:49:51 -0700 | [diff] [blame] | 230 | modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment); |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 231 | Bundle args = superIntent.getExtras(); |
Dianne Hackborn | f4eb85b | 2010-10-29 16:53:04 -0700 | [diff] [blame] | 232 | if (args != null) { |
| 233 | args = new Bundle(args); |
| 234 | } else { |
| 235 | args = new Bundle(); |
| 236 | } |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 237 | args.putParcelable("intent", superIntent); |
| 238 | modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras()); |
Amith Yamasani | e0e4fc2 | 2010-10-05 11:49:51 -0700 | [diff] [blame] | 239 | return modIntent; |
| 240 | } |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 241 | return superIntent; |
Amith Yamasani | e0e4fc2 | 2010-10-05 11:49:51 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Amith Yamasani | 02cf71a | 2010-09-21 15:48:52 -0700 | [diff] [blame] | 244 | /** |
Amith Yamasani | 379d9b0 | 2010-09-27 12:03:59 -0700 | [diff] [blame] | 245 | * Checks if the component name in the intent is different from the Settings class and |
| 246 | * returns the class name to load as a fragment. |
| 247 | */ |
Amith Yamasani | 5203bdf | 2010-11-04 09:59:44 -0700 | [diff] [blame] | 248 | protected String getStartingFragmentClass(Intent intent) { |
| 249 | if (mFragmentClass != null) return mFragmentClass; |
| 250 | |
Dianne Hackborn | f4eb85b | 2010-10-29 16:53:04 -0700 | [diff] [blame] | 251 | String intentClass = intent.getComponent().getClassName(); |
Amith Yamasani | 379d9b0 | 2010-09-27 12:03:59 -0700 | [diff] [blame] | 252 | if (intentClass.equals(getClass().getName())) return null; |
| 253 | |
Dianne Hackborn | ee29379 | 2010-11-01 12:32:33 -0700 | [diff] [blame] | 254 | if ("com.android.settings.ManageApplications".equals(intentClass) |
| 255 | || "com.android.settings.RunningServices".equals(intentClass) |
| 256 | || "com.android.settings.applications.StorageUse".equals(intentClass)) { |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 257 | // Old names of manage apps. |
Dianne Hackborn | f4eb85b | 2010-10-29 16:53:04 -0700 | [diff] [blame] | 258 | intentClass = com.android.settings.applications.ManageApplications.class.getName(); |
| 259 | } |
| 260 | |
Amith Yamasani | 379d9b0 | 2010-09-27 12:03:59 -0700 | [diff] [blame] | 261 | return intentClass; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Override initial header when an activity-alias is causing Settings to be launched |
| 266 | * for a specific fragment encoded in the android:name parameter. |
| 267 | */ |
| 268 | @Override |
| 269 | public Header onGetInitialHeader() { |
| 270 | String fragmentClass = getStartingFragmentClass(super.getIntent()); |
| 271 | if (fragmentClass != null) { |
| 272 | Header header = new Header(); |
| 273 | header.fragment = fragmentClass; |
Amith Yamasani | c9fdfa8 | 2010-12-14 14:38:16 -0800 | [diff] [blame] | 274 | header.title = getTitle(); |
Amith Yamasani | e0e4fc2 | 2010-10-05 11:49:51 -0700 | [diff] [blame] | 275 | header.fragmentArguments = getIntent().getExtras(); |
Amith Yamasani | c9fdfa8 | 2010-12-14 14:38:16 -0800 | [diff] [blame] | 276 | mCurrentHeader = header; |
Amith Yamasani | 379d9b0 | 2010-09-27 12:03:59 -0700 | [diff] [blame] | 277 | return header; |
| 278 | } |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 279 | |
Gilles Debunne | b396c9b | 2011-06-22 16:07:29 -0700 | [diff] [blame] | 280 | return mFirstHeader; |
Amith Yamasani | 379d9b0 | 2010-09-27 12:03:59 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Dianne Hackborn | b725818 | 2011-03-15 16:23:55 -0700 | [diff] [blame] | 283 | @Override |
Dianne Hackborn | 48147dc | 2011-03-18 12:29:41 -0700 | [diff] [blame] | 284 | public Intent onBuildStartFragmentIntent(String fragmentName, Bundle args, |
| 285 | int titleRes, int shortTitleRes) { |
| 286 | Intent intent = super.onBuildStartFragmentIntent(fragmentName, args, |
| 287 | titleRes, shortTitleRes); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 288 | |
| 289 | // some fragments would like a custom activity theme |
Gilles Debunne | 162e541 | 2011-07-11 14:13:31 -0700 | [diff] [blame] | 290 | if (DataUsageSummary.class.getName().equals(fragmentName) || |
| 291 | PowerUsageSummary.class.getName().equals(fragmentName) || |
| 292 | UserDictionarySettings.class.getName().equals(fragmentName) || |
| 293 | ManageAccountsSettings.class.getName().equals(fragmentName)) { |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 294 | intent.putExtra(EXTRA_THEME, android.R.style.Theme_Holo_SolidActionBar); |
| 295 | } |
| 296 | |
Dianne Hackborn | b725818 | 2011-03-15 16:23:55 -0700 | [diff] [blame] | 297 | intent.setClass(this, SubSettings.class); |
| 298 | return intent; |
| 299 | } |
| 300 | |
Amith Yamasani | 379d9b0 | 2010-09-27 12:03:59 -0700 | [diff] [blame] | 301 | /** |
Amith Yamasani | 02cf71a | 2010-09-21 15:48:52 -0700 | [diff] [blame] | 302 | * Populate the activity with the top-level headers. |
| 303 | */ |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 304 | @Override |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 305 | public void onBuildHeaders(List<Header> headers) { |
| 306 | loadHeadersFromResource(R.xml.settings_headers, headers); |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 307 | |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 308 | updateHeaderList(headers); |
| 309 | |
| 310 | mHeaders = headers; |
Amith Yamasani | 02cf71a | 2010-09-21 15:48:52 -0700 | [diff] [blame] | 311 | } |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 312 | |
Amith Yamasani | 02cf71a | 2010-09-21 15:48:52 -0700 | [diff] [blame] | 313 | private void updateHeaderList(List<Header> target) { |
| 314 | int i = 0; |
| 315 | while (i < target.size()) { |
| 316 | Header header = target.get(i); |
Amith Yamasani | 5203bdf | 2010-11-04 09:59:44 -0700 | [diff] [blame] | 317 | // Ids are integers, so downcasting |
| 318 | int id = (int) header.id; |
Amith Yamasani | 02cf71a | 2010-09-21 15:48:52 -0700 | [diff] [blame] | 319 | if (id == R.id.dock_settings) { |
| 320 | if (!needsDockSettings()) |
| 321 | target.remove(header); |
| 322 | } else if (id == R.id.operator_settings || id == R.id.manufacturer_settings) { |
| 323 | Utils.updateHeaderToSpecificActivityFromMetaDataOrRemove(this, target, header); |
Gilles Debunne | 2454f49 | 2011-06-21 16:16:06 -0700 | [diff] [blame] | 324 | } else if (id == R.id.wifi_settings) { |
| 325 | // Remove WiFi Settings if WiFi service is not available. |
| 326 | if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) { |
| 327 | target.remove(header); |
| 328 | } |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 329 | } else if (id == R.id.bluetooth_settings) { |
| 330 | // Remove Bluetooth Settings if Bluetooth service is not available. |
Gilles Debunne | 2454f49 | 2011-06-21 16:16:06 -0700 | [diff] [blame] | 331 | if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) { |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 332 | target.remove(header); |
| 333 | } |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 334 | } |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 335 | |
Amith Yamasani | 5203bdf | 2010-11-04 09:59:44 -0700 | [diff] [blame] | 336 | // Increment if the current one wasn't removed by the Utils code. |
| 337 | if (target.get(i) == header) { |
Amith Yamasani | 3965ae6 | 2010-11-15 14:45:19 -0800 | [diff] [blame] | 338 | // Hold on to the first header, when we need to reset to the top-level |
Gilles Debunne | b396c9b | 2011-06-22 16:07:29 -0700 | [diff] [blame] | 339 | if (mFirstHeader == null && |
| 340 | HeaderAdapter.getHeaderType(header) != HeaderAdapter.HEADER_TYPE_CATEGORY) { |
| 341 | mFirstHeader = header; |
| 342 | } |
Amith Yamasani | 5203bdf | 2010-11-04 09:59:44 -0700 | [diff] [blame] | 343 | mHeaderIndexMap.put(id, i); |
Amith Yamasani | 02cf71a | 2010-09-21 15:48:52 -0700 | [diff] [blame] | 344 | i++; |
Amith Yamasani | 5203bdf | 2010-11-04 09:59:44 -0700 | [diff] [blame] | 345 | } |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
Amith Yamasani | 02cf71a | 2010-09-21 15:48:52 -0700 | [diff] [blame] | 349 | private boolean needsDockSettings() { |
| 350 | return getResources().getBoolean(R.bool.has_dock_settings); |
Amith Yamasani | b61cf51 | 2010-09-12 08:17:50 -0700 | [diff] [blame] | 351 | } |
Amith Yamasani | 5203bdf | 2010-11-04 09:59:44 -0700 | [diff] [blame] | 352 | |
| 353 | private void getMetaData() { |
| 354 | try { |
| 355 | ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(), |
| 356 | PackageManager.GET_META_DATA); |
| 357 | if (ai == null || ai.metaData == null) return; |
| 358 | mTopLevelHeaderId = ai.metaData.getInt(META_DATA_KEY_HEADER_ID); |
| 359 | mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS); |
Amith Yamasani | c9fdfa8 | 2010-12-14 14:38:16 -0800 | [diff] [blame] | 360 | |
| 361 | // Check if it has a parent specified and create a Header object |
| 362 | final int parentHeaderTitleRes = ai.metaData.getInt(META_DATA_KEY_PARENT_TITLE); |
| 363 | String parentFragmentClass = ai.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS); |
| 364 | if (parentFragmentClass != null) { |
| 365 | mParentHeader = new Header(); |
| 366 | mParentHeader.fragment = parentFragmentClass; |
| 367 | if (parentHeaderTitleRes != 0) { |
| 368 | mParentHeader.title = getResources().getString(parentHeaderTitleRes); |
| 369 | } |
| 370 | } |
Amith Yamasani | 5203bdf | 2010-11-04 09:59:44 -0700 | [diff] [blame] | 371 | } catch (NameNotFoundException nnfe) { |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 372 | // No recovery |
Amith Yamasani | 5203bdf | 2010-11-04 09:59:44 -0700 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
Amith Yamasani | 9e3a470 | 2011-01-11 09:09:26 -0800 | [diff] [blame] | 376 | @Override |
| 377 | public boolean hasNextButton() { |
| 378 | return super.hasNextButton(); |
| 379 | } |
| 380 | |
| 381 | @Override |
| 382 | public Button getNextButton() { |
| 383 | return super.getNextButton(); |
| 384 | } |
| 385 | |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 386 | private static class HeaderAdapter extends ArrayAdapter<Header> { |
| 387 | static final int HEADER_TYPE_CATEGORY = 0; |
| 388 | static final int HEADER_TYPE_NORMAL = 1; |
| 389 | static final int HEADER_TYPE_SWITCH = 2; |
| 390 | private static final int HEADER_TYPE_COUNT = HEADER_TYPE_SWITCH + 1; |
| 391 | |
| 392 | private final WifiEnabler mWifiEnabler; |
| 393 | private final BluetoothEnabler mBluetoothEnabler; |
| 394 | |
| 395 | private static class HeaderViewHolder { |
| 396 | ImageView icon; |
| 397 | TextView title; |
| 398 | TextView summary; |
| 399 | Switch switch_; |
| 400 | } |
| 401 | |
| 402 | private LayoutInflater mInflater; |
| 403 | |
| 404 | static int getHeaderType(Header header) { |
| 405 | if (header.fragment == null && header.intent == null) { |
| 406 | return HEADER_TYPE_CATEGORY; |
| 407 | } else if (header.id == R.id.wifi_settings || header.id == R.id.bluetooth_settings) { |
| 408 | return HEADER_TYPE_SWITCH; |
| 409 | } else { |
| 410 | return HEADER_TYPE_NORMAL; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | @Override |
| 415 | public int getItemViewType(int position) { |
| 416 | Header header = getItem(position); |
| 417 | return getHeaderType(header); |
| 418 | } |
| 419 | |
| 420 | @Override |
| 421 | public boolean areAllItemsEnabled() { |
| 422 | return false; // because of categories |
| 423 | } |
| 424 | |
| 425 | @Override |
| 426 | public boolean isEnabled(int position) { |
| 427 | return getItemViewType(position) != HEADER_TYPE_CATEGORY; |
| 428 | } |
| 429 | |
| 430 | @Override |
| 431 | public int getViewTypeCount() { |
| 432 | return HEADER_TYPE_COUNT; |
| 433 | } |
| 434 | |
| 435 | @Override |
| 436 | public boolean hasStableIds() { |
| 437 | return true; |
| 438 | } |
| 439 | |
| 440 | public HeaderAdapter(Context context, List<Header> objects) { |
| 441 | super(context, 0, objects); |
| 442 | mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 443 | |
Gilles Debunne | b396c9b | 2011-06-22 16:07:29 -0700 | [diff] [blame] | 444 | // Temp Switches provided as placeholder until the adapter replaces these with actual |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 445 | // Switches inflated from their layouts. Must be done before adapter is set in super |
| 446 | mWifiEnabler = new WifiEnabler(context, new Switch(context)); |
| 447 | mBluetoothEnabler = new BluetoothEnabler(context, new Switch(context)); |
| 448 | } |
| 449 | |
| 450 | @Override |
| 451 | public View getView(int position, View convertView, ViewGroup parent) { |
| 452 | HeaderViewHolder holder; |
| 453 | Header header = getItem(position); |
| 454 | int headerType = getHeaderType(header); |
| 455 | View view = null; |
| 456 | |
| 457 | if (convertView == null) { |
| 458 | holder = new HeaderViewHolder(); |
| 459 | switch (headerType) { |
| 460 | case HEADER_TYPE_CATEGORY: |
Gilles Debunne | b396c9b | 2011-06-22 16:07:29 -0700 | [diff] [blame] | 461 | view = new TextView(getContext(), null, |
| 462 | android.R.attr.listSeparatorTextViewStyle); |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 463 | holder.title = (TextView) view; |
| 464 | break; |
| 465 | |
| 466 | case HEADER_TYPE_SWITCH: |
Gilles Debunne | b396c9b | 2011-06-22 16:07:29 -0700 | [diff] [blame] | 467 | view = mInflater.inflate(R.layout.preference_header_switch_item, parent, |
| 468 | false); |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 469 | holder.icon = (ImageView) view.findViewById(R.id.icon); |
Gilles Debunne | b396c9b | 2011-06-22 16:07:29 -0700 | [diff] [blame] | 470 | holder.title = (TextView) |
| 471 | view.findViewById(com.android.internal.R.id.title); |
| 472 | holder.summary = (TextView) |
| 473 | view.findViewById(com.android.internal.R.id.summary); |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 474 | holder.switch_ = (Switch) view.findViewById(R.id.switchWidget); |
| 475 | break; |
| 476 | |
| 477 | case HEADER_TYPE_NORMAL: |
Gilles Debunne | b396c9b | 2011-06-22 16:07:29 -0700 | [diff] [blame] | 478 | view = mInflater.inflate( |
| 479 | com.android.internal.R.layout.preference_header_item, parent, |
| 480 | false); |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 481 | holder.icon = (ImageView) view.findViewById(com.android.internal.R.id.icon); |
Gilles Debunne | b396c9b | 2011-06-22 16:07:29 -0700 | [diff] [blame] | 482 | holder.title = (TextView) |
| 483 | view.findViewById(com.android.internal.R.id.title); |
| 484 | holder.summary = (TextView) |
| 485 | view.findViewById(com.android.internal.R.id.summary); |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 486 | break; |
| 487 | } |
| 488 | view.setTag(holder); |
| 489 | } else { |
| 490 | view = convertView; |
| 491 | holder = (HeaderViewHolder) view.getTag(); |
| 492 | } |
| 493 | |
| 494 | // All view fields must be updated every time, because the view may be recycled |
| 495 | switch (headerType) { |
| 496 | case HEADER_TYPE_CATEGORY: |
| 497 | holder.title.setText(header.getTitle(getContext().getResources())); |
| 498 | break; |
| 499 | |
| 500 | case HEADER_TYPE_SWITCH: |
| 501 | // Would need a different treatment if the main menu had more switches |
| 502 | if (header.id == R.id.wifi_settings) { |
| 503 | mWifiEnabler.setSwitch(holder.switch_); |
| 504 | } else { |
| 505 | mBluetoothEnabler.setSwitch(holder.switch_); |
| 506 | } |
| 507 | // No break, fall through on purpose to update common fields |
| 508 | |
| 509 | //$FALL-THROUGH$ |
| 510 | case HEADER_TYPE_NORMAL: |
| 511 | holder.icon.setImageResource(header.iconRes); |
| 512 | holder.title.setText(header.getTitle(getContext().getResources())); |
| 513 | CharSequence summary = header.getSummary(getContext().getResources()); |
| 514 | if (!TextUtils.isEmpty(summary)) { |
| 515 | holder.summary.setVisibility(View.VISIBLE); |
| 516 | holder.summary.setText(summary); |
| 517 | } else { |
| 518 | holder.summary.setVisibility(View.GONE); |
| 519 | } |
| 520 | break; |
| 521 | } |
| 522 | |
| 523 | return view; |
| 524 | } |
| 525 | |
| 526 | public void resume() { |
| 527 | mWifiEnabler.resume(); |
| 528 | mBluetoothEnabler.resume(); |
| 529 | } |
| 530 | |
| 531 | public void pause() { |
| 532 | mWifiEnabler.pause(); |
| 533 | mBluetoothEnabler.pause(); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | @Override |
| 538 | public void setListAdapter(ListAdapter adapter) { |
| 539 | if (mHeaders == null) { |
| 540 | mHeaders = new ArrayList<Header>(); |
| 541 | // When the saved state provides the list of headers, onBuildHeaders is not called |
| 542 | // Copy the list of Headers from the adapter, preserving their order |
| 543 | for (int i = 0; i < adapter.getCount(); i++) { |
| 544 | mHeaders.add((Header) adapter.getItem(i)); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | // Ignore the adapter provided by PreferenceActivity and substitute ours instead |
| 549 | super.setListAdapter(new HeaderAdapter(this, mHeaders)); |
| 550 | } |
| 551 | |
Amith Yamasani | 5203bdf | 2010-11-04 09:59:44 -0700 | [diff] [blame] | 552 | /* |
| 553 | * Settings subclasses for launching independently. |
| 554 | */ |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 555 | public static class BluetoothSettingsActivity extends Settings { /* empty */ } |
| 556 | public static class WirelessSettingsActivity extends Settings { /* empty */ } |
| 557 | public static class TetherSettingsActivity extends Settings { /* empty */ } |
| 558 | public static class VpnSettingsActivity extends Settings { /* empty */ } |
| 559 | public static class DateTimeSettingsActivity extends Settings { /* empty */ } |
| 560 | public static class StorageSettingsActivity extends Settings { /* empty */ } |
| 561 | public static class WifiSettingsActivity extends Settings { /* empty */ } |
| 562 | public static class InputMethodAndLanguageSettingsActivity extends Settings { /* empty */ } |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 563 | public static class InputMethodAndSubtypeEnablerActivity extends Settings { /* empty */ } |
satok | e077d2b | 2011-07-25 09:38:11 +0900 | [diff] [blame^] | 564 | public static class SpellCheckersSettingsActivity extends Settings { /* empty */ } |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 565 | public static class LocalePickerActivity extends Settings { /* empty */ } |
| 566 | public static class UserDictionarySettingsActivity extends Settings { /* empty */ } |
| 567 | public static class SoundSettingsActivity extends Settings { /* empty */ } |
| 568 | public static class DisplaySettingsActivity extends Settings { /* empty */ } |
| 569 | public static class DeviceInfoSettingsActivity extends Settings { /* empty */ } |
| 570 | public static class ApplicationSettingsActivity extends Settings { /* empty */ } |
| 571 | public static class ManageApplicationsActivity extends Settings { /* empty */ } |
| 572 | public static class StorageUseActivity extends Settings { /* empty */ } |
| 573 | public static class DevelopmentSettingsActivity extends Settings { /* empty */ } |
| 574 | public static class AccessibilitySettingsActivity extends Settings { /* empty */ } |
| 575 | public static class SecuritySettingsActivity extends Settings { /* empty */ } |
Gilles Debunne | a6a8a14 | 2011-06-09 11:56:17 -0700 | [diff] [blame] | 576 | public static class LocationSettingsActivity extends Settings { /* empty */ } |
Gilles Debunne | e78c187 | 2011-06-20 15:00:07 -0700 | [diff] [blame] | 577 | public static class PrivacySettingsActivity extends Settings { /* empty */ } |
| 578 | public static class DockSettingsActivity extends Settings { /* empty */ } |
| 579 | public static class RunningServicesActivity extends Settings { /* empty */ } |
| 580 | public static class ManageAccountsSettingsActivity extends Settings { /* empty */ } |
| 581 | public static class PowerUsageSummaryActivity extends Settings { /* empty */ } |
| 582 | public static class AccountSyncSettingsActivity extends Settings { /* empty */ } |
| 583 | public static class AccountSyncSettingsInAddAccountActivity extends Settings { /* empty */ } |
| 584 | public static class CryptKeeperSettingsActivity extends Settings { /* empty */ } |
| 585 | public static class DeviceAdminSettingsActivity extends Settings { /* empty */ } |
| 586 | public static class DataUsageSummaryActivity extends Settings { /* empty */ } |
Gilles Debunne | ab189bd | 2011-07-06 13:10:16 -0700 | [diff] [blame] | 587 | public static class AdvancedWifiSettingsActivity extends Settings { /* empty */ } |
Gilles Debunne | ab189bd | 2011-07-06 13:10:16 -0700 | [diff] [blame] | 588 | public static class TextToSpeechSettingsActivity extends Settings { /* empty */ } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 589 | } |