blob: 694bc1fe4549b6963f4459623d6e5e8555790c55 [file] [log] [blame]
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -08001/**
2 * Copyright (C) 2007 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 */
16
17package com.android.settings;
18
Amith Yamasani60133dd2010-09-11 14:17:31 -070019import java.util.List;
20
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080021import android.content.Context;
22import android.content.Intent;
23import android.content.pm.ApplicationInfo;
24import android.content.pm.PackageManager;
25import android.content.pm.ResolveInfo;
Anders Hammar1b2dd9032010-04-08 10:03:50 +020026import android.content.pm.PackageManager.NameNotFoundException;
27import android.content.res.Resources;
28import android.content.res.Resources.NotFoundException;
29import android.graphics.drawable.Drawable;
30import android.os.Bundle;
Amith Yamasani60133dd2010-09-11 14:17:31 -070031import android.os.SystemProperties;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080032import android.preference.Preference;
33import android.preference.PreferenceGroup;
Amith Yamasani60133dd2010-09-11 14:17:31 -070034import android.telephony.TelephonyManager;
Anders Hammar1b2dd9032010-04-08 10:03:50 +020035import android.text.TextUtils;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080036
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080037public class Utils {
38
39 /**
40 * Set the preference's title to the matching activity's label.
41 */
42 public static final int UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY = 1;
43
44 /**
Anders Hammar1b2dd9032010-04-08 10:03:50 +020045 * Name of the meta-data item that should be set in the AndroidManifest.xml
46 * to specify the icon that should be displayed for the preference.
47 */
48 private static final String META_DATA_PREFERENCE_ICON = "com.android.settings.icon";
49
50 /**
51 * Name of the meta-data item that should be set in the AndroidManifest.xml
52 * to specify the title that should be displayed for the preference.
53 */
54 private static final String META_DATA_PREFERENCE_TITLE = "com.android.settings.title";
55
56 /**
57 * Name of the meta-data item that should be set in the AndroidManifest.xml
58 * to specify the summary text that should be displayed for the preference.
59 */
60 private static final String META_DATA_PREFERENCE_SUMMARY = "com.android.settings.summary";
61
62 /**
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080063 * Finds a matching activity for a preference's intent. If a matching
64 * activity is not found, it will remove the preference.
Ying Wanga7188322010-01-04 18:45:10 -080065 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080066 * @param context The context.
67 * @param parentPreferenceGroup The preference group that contains the
68 * preference whose intent is being resolved.
69 * @param preferenceKey The key of the preference whose intent is being
70 * resolved.
71 * @param flags 0 or one or more of
72 * {@link #UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY}
73 * .
74 * @return Whether an activity was found. If false, the preference was
75 * removed.
76 */
77 public static boolean updatePreferenceToSpecificActivityOrRemove(Context context,
78 PreferenceGroup parentPreferenceGroup, String preferenceKey, int flags) {
Ying Wanga7188322010-01-04 18:45:10 -080079
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080080 Preference preference = parentPreferenceGroup.findPreference(preferenceKey);
81 if (preference == null) {
82 return false;
83 }
Ying Wanga7188322010-01-04 18:45:10 -080084
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080085 Intent intent = preference.getIntent();
86 if (intent != null) {
87 // Find the activity that is in the system image
88 PackageManager pm = context.getPackageManager();
89 List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
90 int listSize = list.size();
91 for (int i = 0; i < listSize; i++) {
92 ResolveInfo resolveInfo = list.get(i);
93 if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
94 != 0) {
Ying Wanga7188322010-01-04 18:45:10 -080095
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080096 // Replace the intent with this specific activity
97 preference.setIntent(new Intent().setClassName(
98 resolveInfo.activityInfo.packageName,
99 resolveInfo.activityInfo.name));
100
101 if ((flags & UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY) != 0) {
102 // Set the preference title to the activity's label
103 preference.setTitle(resolveInfo.loadLabel(pm));
104 }
Ying Wanga7188322010-01-04 18:45:10 -0800105
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800106 return true;
107 }
108 }
109 }
110
111 // Did not find a matching activity, so remove the preference
112 parentPreferenceGroup.removePreference(preference);
Ying Wanga7188322010-01-04 18:45:10 -0800113
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800114 return true;
115 }
Ying Wanga7188322010-01-04 18:45:10 -0800116
117 /**
Anders Hammar1b2dd9032010-04-08 10:03:50 +0200118 * Finds a matching activity for a preference's intent. If a matching
119 * activity is not found, it will remove the preference. The icon, title and
120 * summary of the preference will also be updated with the values retrieved
121 * from the activity's meta-data elements. If no meta-data elements are
122 * specified then the preference title will be set to match the label of the
123 * activity, an icon and summary text will not be displayed.
124 *
125 * @param context The context.
126 * @param parentPreferenceGroup The preference group that contains the
127 * preference whose intent is being resolved.
128 * @param preferenceKey The key of the preference whose intent is being
129 * resolved.
130 *
131 * @return Whether an activity was found. If false, the preference was
132 * removed.
133 *
134 * @see {@link #META_DATA_PREFERENCE_ICON}
135 * {@link #META_DATA_PREFERENCE_TITLE}
136 * {@link #META_DATA_PREFERENCE_SUMMARY}
137 */
138 public static boolean updatePreferenceToSpecificActivityFromMetaDataOrRemove(Context context,
139 PreferenceGroup parentPreferenceGroup, String preferenceKey) {
140
141 IconPreferenceScreen preference = (IconPreferenceScreen)parentPreferenceGroup
142 .findPreference(preferenceKey);
143 if (preference == null) {
144 return false;
145 }
146
147 Intent intent = preference.getIntent();
148 if (intent != null) {
149 // Find the activity that is in the system image
150 PackageManager pm = context.getPackageManager();
151 List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA);
152 int listSize = list.size();
153 for (int i = 0; i < listSize; i++) {
154 ResolveInfo resolveInfo = list.get(i);
155 if ((resolveInfo.activityInfo.applicationInfo.flags
156 & ApplicationInfo.FLAG_SYSTEM) != 0) {
157 Drawable icon = null;
158 String title = null;
159 String summary = null;
160
161 // Get the activity's meta-data
162 try {
163 Resources res = pm
164 .getResourcesForApplication(resolveInfo.activityInfo.packageName);
165 Bundle metaData = resolveInfo.activityInfo.metaData;
166
167 if (res != null && metaData != null) {
168 icon = res.getDrawable(metaData.getInt(META_DATA_PREFERENCE_ICON));
169 title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE));
170 summary = res.getString(metaData.getInt(META_DATA_PREFERENCE_SUMMARY));
171 }
172 } catch (NameNotFoundException e) {
173 // Ignore
174 } catch (NotFoundException e) {
175 // Ignore
176 }
177
178 // Set the preference title to the activity's label if no
179 // meta-data is found
180 if (TextUtils.isEmpty(title)) {
181 title = resolveInfo.loadLabel(pm).toString();
182 }
183
184 // Set icon, title and summary for the preference
185 preference.setIcon(icon);
186 preference.setTitle(title);
187 preference.setSummary(summary);
188
189 // Replace the intent with this specific activity
190 preference.setIntent(new Intent().setClassName(
191 resolveInfo.activityInfo.packageName,
192 resolveInfo.activityInfo.name));
193
194 return true;
195 }
196 }
197 }
198
199 // Did not find a matching activity, so remove the preference
200 parentPreferenceGroup.removePreference(preference);
201
202 return false;
203 }
204
205 /**
Ying Wanga7188322010-01-04 18:45:10 -0800206 * Returns true if Monkey is running.
207 */
208 public static boolean isMonkeyRunning() {
209 return SystemProperties.getBoolean("ro.monkey", false);
210 }
Amith Yamasani60133dd2010-09-11 14:17:31 -0700211
212 /**
213 * Returns whether the device is voice-capable (meaning, it is also a phone).
214 */
215 public static boolean isVoiceCapable(Context context) {
216 TelephonyManager telephony =
217 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
218 return telephony != null && telephony.isVoiceCapable();
219 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800220}