2 * Copyright (C) 2007 Google Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 package com.android.settings;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.pm.ApplicationInfo;
22 import android.content.pm.PackageManager;
23 import android.content.pm.ResolveInfo;
24 import android.content.pm.PackageManager.NameNotFoundException;
25 import android.content.res.Resources;
26 import android.content.res.Resources.NotFoundException;
27 import android.graphics.drawable.Drawable;
28 import android.net.ConnectivityManager;
29 import android.net.LinkProperties;
30 import android.os.Bundle;
31 import android.os.SystemProperties;
32 import android.preference.Preference;
33 import android.preference.PreferenceGroup;
34 import android.preference.PreferenceActivity.Header;
35 import android.telephony.TelephonyManager;
36 import android.text.TextUtils;
38 import java.net.InetAddress;
39 import java.util.Iterator;
40 import java.util.List;
45 * Set the preference's title to the matching activity's label.
47 public static final int UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY = 1;
50 * Name of the meta-data item that should be set in the AndroidManifest.xml
51 * to specify the icon that should be displayed for the preference.
53 private static final String META_DATA_PREFERENCE_ICON = "com.android.settings.icon";
56 * Name of the meta-data item that should be set in the AndroidManifest.xml
57 * to specify the title that should be displayed for the preference.
59 private static final String META_DATA_PREFERENCE_TITLE = "com.android.settings.title";
62 * Name of the meta-data item that should be set in the AndroidManifest.xml
63 * to specify the summary text that should be displayed for the preference.
65 private static final String META_DATA_PREFERENCE_SUMMARY = "com.android.settings.summary";
68 * Finds a matching activity for a preference's intent. If a matching
69 * activity is not found, it will remove the preference.
71 * @param context The context.
72 * @param parentPreferenceGroup The preference group that contains the
73 * preference whose intent is being resolved.
74 * @param preferenceKey The key of the preference whose intent is being
76 * @param flags 0 or one or more of
77 * {@link #UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY}
79 * @return Whether an activity was found. If false, the preference was
82 public static boolean updatePreferenceToSpecificActivityOrRemove(Context context,
83 PreferenceGroup parentPreferenceGroup, String preferenceKey, int flags) {
85 Preference preference = parentPreferenceGroup.findPreference(preferenceKey);
86 if (preference == null) {
90 Intent intent = preference.getIntent();
92 // Find the activity that is in the system image
93 PackageManager pm = context.getPackageManager();
94 List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
95 int listSize = list.size();
96 for (int i = 0; i < listSize; i++) {
97 ResolveInfo resolveInfo = list.get(i);
98 if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
101 // Replace the intent with this specific activity
102 preference.setIntent(new Intent().setClassName(
103 resolveInfo.activityInfo.packageName,
104 resolveInfo.activityInfo.name));
106 if ((flags & UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY) != 0) {
107 // Set the preference title to the activity's label
108 preference.setTitle(resolveInfo.loadLabel(pm));
116 // Did not find a matching activity, so remove the preference
117 parentPreferenceGroup.removePreference(preference);
123 * Finds a matching activity for a preference's intent. If a matching
124 * activity is not found, it will remove the preference. The icon, title and
125 * summary of the preference will also be updated with the values retrieved
126 * from the activity's meta-data elements. If no meta-data elements are
127 * specified then the preference title will be set to match the label of the
128 * activity, an icon and summary text will not be displayed.
130 * @param context The context.
131 * @param parentPreferenceGroup The preference group that contains the
132 * preference whose intent is being resolved.
133 * @param preferenceKey The key of the preference whose intent is being
136 * @return Whether an activity was found. If false, the preference was
139 * @see {@link #META_DATA_PREFERENCE_ICON}
140 * {@link #META_DATA_PREFERENCE_TITLE}
141 * {@link #META_DATA_PREFERENCE_SUMMARY}
143 public static boolean updatePreferenceToSpecificActivityFromMetaDataOrRemove(Context context,
144 PreferenceGroup parentPreferenceGroup, String preferenceKey) {
146 IconPreferenceScreen preference = (IconPreferenceScreen)parentPreferenceGroup
147 .findPreference(preferenceKey);
148 if (preference == null) {
152 Intent intent = preference.getIntent();
153 if (intent != null) {
154 // Find the activity that is in the system image
155 PackageManager pm = context.getPackageManager();
156 List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA);
157 int listSize = list.size();
158 for (int i = 0; i < listSize; i++) {
159 ResolveInfo resolveInfo = list.get(i);
160 if ((resolveInfo.activityInfo.applicationInfo.flags
161 & ApplicationInfo.FLAG_SYSTEM) != 0) {
162 Drawable icon = null;
164 String summary = null;
166 // Get the activity's meta-data
169 .getResourcesForApplication(resolveInfo.activityInfo.packageName);
170 Bundle metaData = resolveInfo.activityInfo.metaData;
172 if (res != null && metaData != null) {
173 icon = res.getDrawable(metaData.getInt(META_DATA_PREFERENCE_ICON));
174 title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE));
175 summary = res.getString(metaData.getInt(META_DATA_PREFERENCE_SUMMARY));
177 } catch (NameNotFoundException e) {
179 } catch (NotFoundException e) {
183 // Set the preference title to the activity's label if no
184 // meta-data is found
185 if (TextUtils.isEmpty(title)) {
186 title = resolveInfo.loadLabel(pm).toString();
189 // Set icon, title and summary for the preference
190 preference.setIcon(icon);
191 preference.setTitle(title);
192 preference.setSummary(summary);
194 // Replace the intent with this specific activity
195 preference.setIntent(new Intent().setClassName(
196 resolveInfo.activityInfo.packageName,
197 resolveInfo.activityInfo.name));
204 // Did not find a matching activity, so remove the preference
205 parentPreferenceGroup.removePreference(preference);
210 public static boolean updateHeaderToSpecificActivityFromMetaDataOrRemove(Context context,
211 List<Header> target, Header header) {
213 Intent intent = header.intent;
214 if (intent != null) {
215 // Find the activity that is in the system image
216 PackageManager pm = context.getPackageManager();
217 List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA);
218 int listSize = list.size();
219 for (int i = 0; i < listSize; i++) {
220 ResolveInfo resolveInfo = list.get(i);
221 if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
223 Drawable icon = null;
225 String summary = null;
227 // Get the activity's meta-data
229 Resources res = pm.getResourcesForApplication(
230 resolveInfo.activityInfo.packageName);
231 Bundle metaData = resolveInfo.activityInfo.metaData;
233 if (res != null && metaData != null) {
234 icon = res.getDrawable(metaData.getInt(META_DATA_PREFERENCE_ICON));
235 title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE));
236 summary = res.getString(metaData.getInt(META_DATA_PREFERENCE_SUMMARY));
238 } catch (NameNotFoundException e) {
240 } catch (NotFoundException e) {
244 // Set the preference title to the activity's label if no
245 // meta-data is found
246 if (TextUtils.isEmpty(title)) {
247 title = resolveInfo.loadLabel(pm).toString();
250 // Set icon, title and summary for the preference
252 //header.icon = icon;
253 header.title = title;
254 header.summary = summary;
255 // Replace the intent with this specific activity
256 header.intent = new Intent().setClassName(resolveInfo.activityInfo.packageName,
257 resolveInfo.activityInfo.name);
264 // Did not find a matching activity, so remove the preference
265 if (target.remove(header)) System.err.println("Removed " + header.id);
271 * Returns true if Monkey is running.
273 public static boolean isMonkeyRunning() {
274 return SystemProperties.getBoolean("ro.monkey", false);
278 * Returns whether the device is voice-capable (meaning, it is also a phone).
280 public static boolean isVoiceCapable(Context context) {
281 TelephonyManager telephony =
282 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
283 return telephony != null && telephony.isVoiceCapable();
286 public static boolean isWifiOnly() {
287 return "wifi-only".equals(SystemProperties.get("ro.carrier"));
291 * Returns the WIFI IP Addresses, if any, taking into account IPv4 and IPv6 style addresses.
292 * @param context the application context
293 * @return the formatted and comma-separated IP addresses, or null if none.
295 public static String getWifiIpAddresses(Context context) {
296 ConnectivityManager cm = (ConnectivityManager)
297 context.getSystemService(Context.CONNECTIVITY_SERVICE);
298 LinkProperties prop = cm.getLinkProperties(ConnectivityManager.TYPE_WIFI);
299 if (prop == null) return null;
300 Iterator<InetAddress> iter = prop.getAddresses().iterator();
301 // If there are no entries, return null
302 if (!iter.hasNext()) return null;
303 // Concatenate all available addresses, comma separated
304 String addresses = "";
305 while (iter.hasNext()) {
306 addresses += iter.next().getHostAddress();
307 if (iter.hasNext()) addresses += ", ";