blob: b29ec06f588eb107eed9fff39b47d6c1b1c3c6b7 [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
19import android.content.Context;
20import android.content.Intent;
21import android.content.pm.ApplicationInfo;
22import android.content.pm.PackageManager;
23import android.content.pm.ResolveInfo;
Ying Wanga7188322010-01-04 18:45:10 -080024import android.os.SystemProperties;
Anders Hammar1b2dd9032010-04-08 10:03:50 +020025import android.content.pm.PackageManager.NameNotFoundException;
26import android.content.res.Resources;
27import android.content.res.Resources.NotFoundException;
28import android.graphics.drawable.Drawable;
29import android.os.Bundle;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080030import android.preference.Preference;
31import android.preference.PreferenceGroup;
Anders Hammar1b2dd9032010-04-08 10:03:50 +020032import android.text.TextUtils;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080033
34import java.util.List;
35
36public class Utils {
37
38 /**
39 * Set the preference's title to the matching activity's label.
40 */
41 public static final int UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY = 1;
42
43 /**
Anders Hammar1b2dd9032010-04-08 10:03:50 +020044 * Name of the meta-data item that should be set in the AndroidManifest.xml
45 * to specify the icon that should be displayed for the preference.
46 */
47 private static final String META_DATA_PREFERENCE_ICON = "com.android.settings.icon";
48
49 /**
50 * Name of the meta-data item that should be set in the AndroidManifest.xml
51 * to specify the title that should be displayed for the preference.
52 */
53 private static final String META_DATA_PREFERENCE_TITLE = "com.android.settings.title";
54
55 /**
56 * Name of the meta-data item that should be set in the AndroidManifest.xml
57 * to specify the summary text that should be displayed for the preference.
58 */
59 private static final String META_DATA_PREFERENCE_SUMMARY = "com.android.settings.summary";
60
61 /**
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080062 * Finds a matching activity for a preference's intent. If a matching
63 * activity is not found, it will remove the preference.
Ying Wanga7188322010-01-04 18:45:10 -080064 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080065 * @param context The context.
66 * @param parentPreferenceGroup The preference group that contains the
67 * preference whose intent is being resolved.
68 * @param preferenceKey The key of the preference whose intent is being
69 * resolved.
70 * @param flags 0 or one or more of
71 * {@link #UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY}
72 * .
73 * @return Whether an activity was found. If false, the preference was
74 * removed.
75 */
76 public static boolean updatePreferenceToSpecificActivityOrRemove(Context context,
77 PreferenceGroup parentPreferenceGroup, String preferenceKey, int flags) {
Ying Wanga7188322010-01-04 18:45:10 -080078
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080079 Preference preference = parentPreferenceGroup.findPreference(preferenceKey);
80 if (preference == null) {
81 return false;
82 }
Ying Wanga7188322010-01-04 18:45:10 -080083
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080084 Intent intent = preference.getIntent();
85 if (intent != null) {
86 // Find the activity that is in the system image
87 PackageManager pm = context.getPackageManager();
88 List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
89 int listSize = list.size();
90 for (int i = 0; i < listSize; i++) {
91 ResolveInfo resolveInfo = list.get(i);
92 if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
93 != 0) {
Ying Wanga7188322010-01-04 18:45:10 -080094
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080095 // Replace the intent with this specific activity
96 preference.setIntent(new Intent().setClassName(
97 resolveInfo.activityInfo.packageName,
98 resolveInfo.activityInfo.name));
99
100 if ((flags & UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY) != 0) {
101 // Set the preference title to the activity's label
102 preference.setTitle(resolveInfo.loadLabel(pm));
103 }
Ying Wanga7188322010-01-04 18:45:10 -0800104
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800105 return true;
106 }
107 }
108 }
109
110 // Did not find a matching activity, so remove the preference
111 parentPreferenceGroup.removePreference(preference);
Ying Wanga7188322010-01-04 18:45:10 -0800112
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800113 return true;
114 }
Ying Wanga7188322010-01-04 18:45:10 -0800115
116 /**
Anders Hammar1b2dd9032010-04-08 10:03:50 +0200117 * Finds a matching activity for a preference's intent. If a matching
118 * activity is not found, it will remove the preference. The icon, title and
119 * summary of the preference will also be updated with the values retrieved
120 * from the activity's meta-data elements. If no meta-data elements are
121 * specified then the preference title will be set to match the label of the
122 * activity, an icon and summary text will not be displayed.
123 *
124 * @param context The context.
125 * @param parentPreferenceGroup The preference group that contains the
126 * preference whose intent is being resolved.
127 * @param preferenceKey The key of the preference whose intent is being
128 * resolved.
129 *
130 * @return Whether an activity was found. If false, the preference was
131 * removed.
132 *
133 * @see {@link #META_DATA_PREFERENCE_ICON}
134 * {@link #META_DATA_PREFERENCE_TITLE}
135 * {@link #META_DATA_PREFERENCE_SUMMARY}
136 */
137 public static boolean updatePreferenceToSpecificActivityFromMetaDataOrRemove(Context context,
138 PreferenceGroup parentPreferenceGroup, String preferenceKey) {
139
140 IconPreferenceScreen preference = (IconPreferenceScreen)parentPreferenceGroup
141 .findPreference(preferenceKey);
142 if (preference == null) {
143 return false;
144 }
145
146 Intent intent = preference.getIntent();
147 if (intent != null) {
148 // Find the activity that is in the system image
149 PackageManager pm = context.getPackageManager();
150 List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA);
151 int listSize = list.size();
152 for (int i = 0; i < listSize; i++) {
153 ResolveInfo resolveInfo = list.get(i);
154 if ((resolveInfo.activityInfo.applicationInfo.flags
155 & ApplicationInfo.FLAG_SYSTEM) != 0) {
156 Drawable icon = null;
157 String title = null;
158 String summary = null;
159
160 // Get the activity's meta-data
161 try {
162 Resources res = pm
163 .getResourcesForApplication(resolveInfo.activityInfo.packageName);
164 Bundle metaData = resolveInfo.activityInfo.metaData;
165
166 if (res != null && metaData != null) {
167 icon = res.getDrawable(metaData.getInt(META_DATA_PREFERENCE_ICON));
168 title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE));
169 summary = res.getString(metaData.getInt(META_DATA_PREFERENCE_SUMMARY));
170 }
171 } catch (NameNotFoundException e) {
172 // Ignore
173 } catch (NotFoundException e) {
174 // Ignore
175 }
176
177 // Set the preference title to the activity's label if no
178 // meta-data is found
179 if (TextUtils.isEmpty(title)) {
180 title = resolveInfo.loadLabel(pm).toString();
181 }
182
183 // Set icon, title and summary for the preference
184 preference.setIcon(icon);
185 preference.setTitle(title);
186 preference.setSummary(summary);
187
188 // Replace the intent with this specific activity
189 preference.setIntent(new Intent().setClassName(
190 resolveInfo.activityInfo.packageName,
191 resolveInfo.activityInfo.name));
192
193 return true;
194 }
195 }
196 }
197
198 // Did not find a matching activity, so remove the preference
199 parentPreferenceGroup.removePreference(preference);
200
201 return false;
202 }
203
204 /**
Ying Wanga7188322010-01-04 18:45:10 -0800205 * Returns true if Monkey is running.
206 */
207 public static boolean isMonkeyRunning() {
208 return SystemProperties.getBoolean("ro.monkey", false);
209 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800210}