blob: c94a0e16852bb6785c1e693d8118b32d27efaadc [file] [log] [blame]
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -08001/*
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
17package com.android.settings;
18
Amith Yamasani379d9b02010-09-27 12:03:59 -070019import android.content.Intent;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070020import android.content.pm.ActivityInfo;
21import android.content.pm.PackageManager;
22import android.content.pm.PackageManager.NameNotFoundException;
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -070023import android.os.Bundle;
Amith Yamasani02cf71a2010-09-21 15:48:52 -070024import android.preference.PreferenceActivity;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080025
Amith Yamasani5203bdf2010-11-04 09:59:44 -070026import java.util.HashMap;
Amith Yamasani02cf71a2010-09-21 15:48:52 -070027import java.util.List;
Amith Yamasanid7993472010-08-18 13:59:28 -070028
29/**
30 * Top-level settings activity to handle single pane and double pane UI layout.
31 */
Daisuke Miyakawa25af1502010-09-24 11:29:31 -070032public class Settings extends PreferenceActivity {
Amith Yamasanid7993472010-08-18 13:59:28 -070033
Amith Yamasani5203bdf2010-11-04 09:59:44 -070034 private static final String META_DATA_KEY_HEADER_ID =
35 "com.android.settings.TOP_LEVEL_HEADER_ID";
36 private static final String META_DATA_KEY_FRAGMENT_CLASS =
37 "com.android.settings.FRAGMENT_CLASS";
38
39 private String mFragmentClass;
40 private int mTopLevelHeaderId;
Amith Yamasani3965ae62010-11-15 14:45:19 -080041 private Header mFirstHeader;
Amith Yamasani5203bdf2010-11-04 09:59:44 -070042
Amith Yamasani02cf71a2010-09-21 15:48:52 -070043 // TODO: Update Call Settings based on airplane mode state.
Amith Yamasanib61cf512010-09-12 08:17:50 -070044
Amith Yamasani5203bdf2010-11-04 09:59:44 -070045 protected HashMap<Integer, Integer> mHeaderIndexMap = new HashMap<Integer, Integer>();
46
47 @Override
48 protected void onCreate(Bundle savedInstanceState) {
49 getMetaData();
50 super.onCreate(savedInstanceState);
51
52 // TODO: Do this only if 2-pane mode
53 highlightHeader();
54 }
55
Amith Yamasani3965ae62010-11-15 14:45:19 -080056 @Override
57 public void onNewIntent(Intent intent) {
58 super.onNewIntent(intent);
59
60 // If it is not launched from history, then reset to top-level
61 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0
62 && mFirstHeader != null) {
63 switchToHeader(mFirstHeader);
64 }
65 }
66
Amith Yamasani5203bdf2010-11-04 09:59:44 -070067 private void highlightHeader() {
68 if (mTopLevelHeaderId != 0) {
69 Integer index = mHeaderIndexMap.get(mTopLevelHeaderId);
70 if (index != null) {
71 getListView().setItemChecked(index, true);
72 }
73 }
74 }
75
Amith Yamasanie0e4fc22010-10-05 11:49:51 -070076 @Override
77 public Intent getIntent() {
78 String startingFragment = getStartingFragmentClass(super.getIntent());
79 if (startingFragment != null && !onIsMultiPane()) {
80 Intent modIntent = new Intent(super.getIntent());
81 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -070082 Bundle args = super.getIntent().getExtras();
83 if (args != null) {
84 args = new Bundle(args);
85 } else {
86 args = new Bundle();
87 }
88 args.putParcelable("intent", super.getIntent());
Amith Yamasanie0e4fc22010-10-05 11:49:51 -070089 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, super.getIntent().getExtras());
90 return modIntent;
91 }
92 return super.getIntent();
93 }
94
Amith Yamasani02cf71a2010-09-21 15:48:52 -070095 /**
Amith Yamasani379d9b02010-09-27 12:03:59 -070096 * Checks if the component name in the intent is different from the Settings class and
97 * returns the class name to load as a fragment.
98 */
Amith Yamasani5203bdf2010-11-04 09:59:44 -070099 protected String getStartingFragmentClass(Intent intent) {
100 if (mFragmentClass != null) return mFragmentClass;
101
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700102 String intentClass = intent.getComponent().getClassName();
Amith Yamasani379d9b02010-09-27 12:03:59 -0700103 if (intentClass.equals(getClass().getName())) return null;
104
Dianne Hackbornee293792010-11-01 12:32:33 -0700105 if ("com.android.settings.ManageApplications".equals(intentClass)
106 || "com.android.settings.RunningServices".equals(intentClass)
107 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -0700108 // Old name of manage apps.
109 intentClass = com.android.settings.applications.ManageApplications.class.getName();
110 }
111
Amith Yamasani379d9b02010-09-27 12:03:59 -0700112 return intentClass;
113 }
114
115 /**
116 * Override initial header when an activity-alias is causing Settings to be launched
117 * for a specific fragment encoded in the android:name parameter.
118 */
119 @Override
120 public Header onGetInitialHeader() {
121 String fragmentClass = getStartingFragmentClass(super.getIntent());
122 if (fragmentClass != null) {
123 Header header = new Header();
124 header.fragment = fragmentClass;
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700125 header.fragmentArguments = getIntent().getExtras();
Amith Yamasani379d9b02010-09-27 12:03:59 -0700126 return header;
127 }
128 return super.onGetInitialHeader();
129 }
130
131 /**
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700132 * Populate the activity with the top-level headers.
133 */
Amith Yamasanid7993472010-08-18 13:59:28 -0700134 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700135 public void onBuildHeaders(List<Header> target) {
136 loadHeadersFromResource(R.xml.settings_headers, target);
Amith Yamasanid7993472010-08-18 13:59:28 -0700137
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700138 updateHeaderList(target);
139 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700140
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700141 private void updateHeaderList(List<Header> target) {
142 int i = 0;
143 while (i < target.size()) {
144 Header header = target.get(i);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700145 // Ids are integers, so downcasting
146 int id = (int) header.id;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700147 if (id == R.id.dock_settings) {
148 if (!needsDockSettings())
149 target.remove(header);
150 } else if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
151 Utils.updateHeaderToSpecificActivityFromMetaDataOrRemove(this, target, header);
152 } else if (id == R.id.call_settings) {
153 if (!Utils.isVoiceCapable(this))
154 target.remove(header);
Amith Yamasanid7993472010-08-18 13:59:28 -0700155 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700156 // Increment if the current one wasn't removed by the Utils code.
157 if (target.get(i) == header) {
Amith Yamasani3965ae62010-11-15 14:45:19 -0800158 // Hold on to the first header, when we need to reset to the top-level
159 if (i == 0) mFirstHeader = header;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700160 mHeaderIndexMap.put(id, i);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700161 i++;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700162 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700163 }
164 }
165
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700166 private boolean needsDockSettings() {
167 return getResources().getBoolean(R.bool.has_dock_settings);
Amith Yamasanib61cf512010-09-12 08:17:50 -0700168 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700169
170 private void getMetaData() {
171 try {
172 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
173 PackageManager.GET_META_DATA);
174 if (ai == null || ai.metaData == null) return;
175 mTopLevelHeaderId = ai.metaData.getInt(META_DATA_KEY_HEADER_ID);
176 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
177 } catch (NameNotFoundException nnfe) {
178 }
179 }
180
181 /*
182 * Settings subclasses for launching independently.
183 */
184
185 public static class BluetoothSettingsActivity extends Settings { }
186 public static class WirelessSettingsActivity extends Settings { }
187 public static class TetherSettingsActivity extends Settings { }
188 public static class VpnSettingsActivity extends Settings { }
189 public static class DateTimeSettingsActivity extends Settings { }
190 public static class StorageSettingsActivity extends Settings { }
191 public static class WifiSettingsActivity extends Settings { }
192 public static class InputMethodAndLanguageSettingsActivity extends Settings { }
193 public static class InputMethodAndSubtypeEnablerActivity extends Settings { }
194 public static class LocalePickerActivity extends Settings { }
195 public static class UserDictionarySettingsActivity extends Settings { }
196 public static class SoundSettingsActivity extends Settings { }
197 public static class DisplaySettingsActivity extends Settings { }
198 public static class DeviceInfoSettingsActivity extends Settings { }
199 public static class ApplicationSettingsActivity extends Settings { }
200 public static class ManageApplicationsActivity extends Settings { }
201 public static class StorageUseActivity extends Settings { }
202 public static class DevelopmentSettingsActivity extends Settings { }
203 public static class AccessibilitySettingsActivity extends Settings { }
204 public static class SecuritySettingsActivity extends Settings { }
205 public static class PrivacySettingsActivity extends Settings { }
206 public static class DockSettingsActivity extends Settings { }
207 public static class RunningServicesActivity extends Settings { }
208 public static class VoiceInputOutputSettingsActivity extends Settings { }
Amith Yamasaniea071652010-11-08 14:28:05 -0800209 public static class ManageAccountsSettingsActivity extends Settings { }
Dianne Hackborn59a48602010-11-10 23:34:52 -0800210 public static class PowerUsageSummaryActivity extends Settings { }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800211}