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