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