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