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