blob: bec82415dee527e24da4c8368c47d3a20538348d [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;
41
Amith Yamasani02cf71a2010-09-21 15:48:52 -070042 // TODO: Update Call Settings based on airplane mode state.
Amith Yamasanib61cf512010-09-12 08:17:50 -070043
Amith Yamasani5203bdf2010-11-04 09:59:44 -070044 protected HashMap<Integer, Integer> mHeaderIndexMap = new HashMap<Integer, Integer>();
45
46 @Override
47 protected void onCreate(Bundle savedInstanceState) {
48 getMetaData();
49 super.onCreate(savedInstanceState);
50
51 // TODO: Do this only if 2-pane mode
52 highlightHeader();
53 }
54
55 private void highlightHeader() {
56 if (mTopLevelHeaderId != 0) {
57 Integer index = mHeaderIndexMap.get(mTopLevelHeaderId);
58 if (index != null) {
59 getListView().setItemChecked(index, true);
60 }
61 }
62 }
63
Amith Yamasanie0e4fc22010-10-05 11:49:51 -070064 @Override
65 public Intent getIntent() {
66 String startingFragment = getStartingFragmentClass(super.getIntent());
67 if (startingFragment != null && !onIsMultiPane()) {
68 Intent modIntent = new Intent(super.getIntent());
69 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -070070 Bundle args = super.getIntent().getExtras();
71 if (args != null) {
72 args = new Bundle(args);
73 } else {
74 args = new Bundle();
75 }
76 args.putParcelable("intent", super.getIntent());
Amith Yamasanie0e4fc22010-10-05 11:49:51 -070077 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, super.getIntent().getExtras());
78 return modIntent;
79 }
80 return super.getIntent();
81 }
82
Amith Yamasani02cf71a2010-09-21 15:48:52 -070083 /**
Amith Yamasani379d9b02010-09-27 12:03:59 -070084 * Checks if the component name in the intent is different from the Settings class and
85 * returns the class name to load as a fragment.
86 */
Amith Yamasani5203bdf2010-11-04 09:59:44 -070087 protected String getStartingFragmentClass(Intent intent) {
88 if (mFragmentClass != null) return mFragmentClass;
89
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -070090 String intentClass = intent.getComponent().getClassName();
Amith Yamasani379d9b02010-09-27 12:03:59 -070091 if (intentClass.equals(getClass().getName())) return null;
92
Dianne Hackbornee293792010-11-01 12:32:33 -070093 if ("com.android.settings.ManageApplications".equals(intentClass)
94 || "com.android.settings.RunningServices".equals(intentClass)
95 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
Dianne Hackbornf4eb85b2010-10-29 16:53:04 -070096 // Old name of manage apps.
97 intentClass = com.android.settings.applications.ManageApplications.class.getName();
98 }
99
Amith Yamasani379d9b02010-09-27 12:03:59 -0700100 return intentClass;
101 }
102
103 /**
104 * Override initial header when an activity-alias is causing Settings to be launched
105 * for a specific fragment encoded in the android:name parameter.
106 */
107 @Override
108 public Header onGetInitialHeader() {
109 String fragmentClass = getStartingFragmentClass(super.getIntent());
110 if (fragmentClass != null) {
111 Header header = new Header();
112 header.fragment = fragmentClass;
Amith Yamasanie0e4fc22010-10-05 11:49:51 -0700113 header.fragmentArguments = getIntent().getExtras();
Amith Yamasani379d9b02010-09-27 12:03:59 -0700114 return header;
115 }
116 return super.onGetInitialHeader();
117 }
118
119 /**
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700120 * Populate the activity with the top-level headers.
121 */
Amith Yamasanid7993472010-08-18 13:59:28 -0700122 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700123 public void onBuildHeaders(List<Header> target) {
124 loadHeadersFromResource(R.xml.settings_headers, target);
Amith Yamasanid7993472010-08-18 13:59:28 -0700125
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700126 updateHeaderList(target);
127 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700128
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700129 private void updateHeaderList(List<Header> target) {
130 int i = 0;
131 while (i < target.size()) {
132 Header header = target.get(i);
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700133 // Ids are integers, so downcasting
134 int id = (int) header.id;
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700135 if (id == R.id.dock_settings) {
136 if (!needsDockSettings())
137 target.remove(header);
138 } else if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
139 Utils.updateHeaderToSpecificActivityFromMetaDataOrRemove(this, target, header);
140 } else if (id == R.id.call_settings) {
141 if (!Utils.isVoiceCapable(this))
142 target.remove(header);
Amith Yamasanid7993472010-08-18 13:59:28 -0700143 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700144 // Increment if the current one wasn't removed by the Utils code.
145 if (target.get(i) == header) {
146 mHeaderIndexMap.put(id, i);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700147 i++;
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700148 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700149 }
150 }
151
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700152 private boolean needsDockSettings() {
153 return getResources().getBoolean(R.bool.has_dock_settings);
Amith Yamasanib61cf512010-09-12 08:17:50 -0700154 }
Amith Yamasani5203bdf2010-11-04 09:59:44 -0700155
156 private void getMetaData() {
157 try {
158 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
159 PackageManager.GET_META_DATA);
160 if (ai == null || ai.metaData == null) return;
161 mTopLevelHeaderId = ai.metaData.getInt(META_DATA_KEY_HEADER_ID);
162 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
163 } catch (NameNotFoundException nnfe) {
164 }
165 }
166
167 /*
168 * Settings subclasses for launching independently.
169 */
170
171 public static class BluetoothSettingsActivity extends Settings { }
172 public static class WirelessSettingsActivity extends Settings { }
173 public static class TetherSettingsActivity extends Settings { }
174 public static class VpnSettingsActivity extends Settings { }
175 public static class DateTimeSettingsActivity extends Settings { }
176 public static class StorageSettingsActivity extends Settings { }
177 public static class WifiSettingsActivity extends Settings { }
178 public static class InputMethodAndLanguageSettingsActivity extends Settings { }
179 public static class InputMethodAndSubtypeEnablerActivity extends Settings { }
180 public static class LocalePickerActivity extends Settings { }
181 public static class UserDictionarySettingsActivity extends Settings { }
182 public static class SoundSettingsActivity extends Settings { }
183 public static class DisplaySettingsActivity extends Settings { }
184 public static class DeviceInfoSettingsActivity extends Settings { }
185 public static class ApplicationSettingsActivity extends Settings { }
186 public static class ManageApplicationsActivity extends Settings { }
187 public static class StorageUseActivity extends Settings { }
188 public static class DevelopmentSettingsActivity extends Settings { }
189 public static class AccessibilitySettingsActivity extends Settings { }
190 public static class SecuritySettingsActivity extends Settings { }
191 public static class PrivacySettingsActivity extends Settings { }
192 public static class DockSettingsActivity extends Settings { }
193 public static class RunningServicesActivity extends Settings { }
194 public static class VoiceInputOutputSettingsActivity extends Settings { }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800195}