1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
/**
* Copyright (C) 2007 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package com.android.settings;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.LinkProperties;
import android.os.Bundle;
import android.os.SystemProperties;
import android.preference.Preference;
import android.preference.PreferenceGroup;
import android.preference.PreferenceActivity.Header;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
public class Utils {
/**
* Set the preference's title to the matching activity's label.
*/
public static final int UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY = 1;
/**
* Name of the meta-data item that should be set in the AndroidManifest.xml
* to specify the icon that should be displayed for the preference.
*/
private static final String META_DATA_PREFERENCE_ICON = "com.android.settings.icon";
/**
* Name of the meta-data item that should be set in the AndroidManifest.xml
* to specify the title that should be displayed for the preference.
*/
private static final String META_DATA_PREFERENCE_TITLE = "com.android.settings.title";
/**
* Name of the meta-data item that should be set in the AndroidManifest.xml
* to specify the summary text that should be displayed for the preference.
*/
private static final String META_DATA_PREFERENCE_SUMMARY = "com.android.settings.summary";
/**
* Finds a matching activity for a preference's intent. If a matching
* activity is not found, it will remove the preference.
*
* @param context The context.
* @param parentPreferenceGroup The preference group that contains the
* preference whose intent is being resolved.
* @param preferenceKey The key of the preference whose intent is being
* resolved.
* @param flags 0 or one or more of
* {@link #UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY}
* .
* @return Whether an activity was found. If false, the preference was
* removed.
*/
public static boolean updatePreferenceToSpecificActivityOrRemove(Context context,
PreferenceGroup parentPreferenceGroup, String preferenceKey, int flags) {
Preference preference = parentPreferenceGroup.findPreference(preferenceKey);
if (preference == null) {
return false;
}
Intent intent = preference.getIntent();
if (intent != null) {
// Find the activity that is in the system image
PackageManager pm = context.getPackageManager();
List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
int listSize = list.size();
for (int i = 0; i < listSize; i++) {
ResolveInfo resolveInfo = list.get(i);
if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
!= 0) {
// Replace the intent with this specific activity
preference.setIntent(new Intent().setClassName(
resolveInfo.activityInfo.packageName,
resolveInfo.activityInfo.name));
if ((flags & UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY) != 0) {
// Set the preference title to the activity's label
preference.setTitle(resolveInfo.loadLabel(pm));
}
return true;
}
}
}
// Did not find a matching activity, so remove the preference
parentPreferenceGroup.removePreference(preference);
return true;
}
/**
* Finds a matching activity for a preference's intent. If a matching
* activity is not found, it will remove the preference. The icon, title and
* summary of the preference will also be updated with the values retrieved
* from the activity's meta-data elements. If no meta-data elements are
* specified then the preference title will be set to match the label of the
* activity, an icon and summary text will not be displayed.
*
* @param context The context.
* @param parentPreferenceGroup The preference group that contains the
* preference whose intent is being resolved.
* @param preferenceKey The key of the preference whose intent is being
* resolved.
*
* @return Whether an activity was found. If false, the preference was
* removed.
*
* @see {@link #META_DATA_PREFERENCE_ICON}
* {@link #META_DATA_PREFERENCE_TITLE}
* {@link #META_DATA_PREFERENCE_SUMMARY}
*/
public static boolean updatePreferenceToSpecificActivityFromMetaDataOrRemove(Context context,
PreferenceGroup parentPreferenceGroup, String preferenceKey) {
IconPreferenceScreen preference = (IconPreferenceScreen)parentPreferenceGroup
.findPreference(preferenceKey);
if (preference == null) {
return false;
}
Intent intent = preference.getIntent();
if (intent != null) {
// Find the activity that is in the system image
PackageManager pm = context.getPackageManager();
List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA);
int listSize = list.size();
for (int i = 0; i < listSize; i++) {
ResolveInfo resolveInfo = list.get(i);
if ((resolveInfo.activityInfo.applicationInfo.flags
& ApplicationInfo.FLAG_SYSTEM) != 0) {
Drawable icon = null;
String title = null;
String summary = null;
// Get the activity's meta-data
try {
Resources res = pm
.getResourcesForApplication(resolveInfo.activityInfo.packageName);
Bundle metaData = resolveInfo.activityInfo.metaData;
if (res != null && metaData != null) {
icon = res.getDrawable(metaData.getInt(META_DATA_PREFERENCE_ICON));
title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE));
summary = res.getString(metaData.getInt(META_DATA_PREFERENCE_SUMMARY));
}
} catch (NameNotFoundException e) {
// Ignore
} catch (NotFoundException e) {
// Ignore
}
// Set the preference title to the activity's label if no
// meta-data is found
if (TextUtils.isEmpty(title)) {
title = resolveInfo.loadLabel(pm).toString();
}
// Set icon, title and summary for the preference
preference.setIcon(icon);
preference.setTitle(title);
preference.setSummary(summary);
// Replace the intent with this specific activity
preference.setIntent(new Intent().setClassName(
resolveInfo.activityInfo.packageName,
resolveInfo.activityInfo.name));
return true;
}
}
}
// Did not find a matching activity, so remove the preference
parentPreferenceGroup.removePreference(preference);
return false;
}
public static boolean updateHeaderToSpecificActivityFromMetaDataOrRemove(Context context,
List<Header> target, Header header) {
Intent intent = header.intent;
if (intent != null) {
// Find the activity that is in the system image
PackageManager pm = context.getPackageManager();
List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA);
int listSize = list.size();
for (int i = 0; i < listSize; i++) {
ResolveInfo resolveInfo = list.get(i);
if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
!= 0) {
Drawable icon = null;
String title = null;
String summary = null;
// Get the activity's meta-data
try {
Resources res = pm.getResourcesForApplication(
resolveInfo.activityInfo.packageName);
Bundle metaData = resolveInfo.activityInfo.metaData;
if (res != null && metaData != null) {
icon = res.getDrawable(metaData.getInt(META_DATA_PREFERENCE_ICON));
title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE));
summary = res.getString(metaData.getInt(META_DATA_PREFERENCE_SUMMARY));
}
} catch (NameNotFoundException e) {
// Ignore
} catch (NotFoundException e) {
// Ignore
}
// Set the preference title to the activity's label if no
// meta-data is found
if (TextUtils.isEmpty(title)) {
title = resolveInfo.loadLabel(pm).toString();
}
// Set icon, title and summary for the preference
// TODO:
//header.icon = icon;
header.title = title;
header.summary = summary;
// Replace the intent with this specific activity
header.intent = new Intent().setClassName(resolveInfo.activityInfo.packageName,
resolveInfo.activityInfo.name);
return true;
}
}
}
// Did not find a matching activity, so remove the preference
if (target.remove(header)) System.err.println("Removed " + header.id);
return false;
}
/**
* Returns true if Monkey is running.
*/
public static boolean isMonkeyRunning() {
return SystemProperties.getBoolean("ro.monkey", false);
}
/**
* Returns whether the device is voice-capable (meaning, it is also a phone).
*/
public static boolean isVoiceCapable(Context context) {
TelephonyManager telephony =
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return telephony != null && telephony.isVoiceCapable();
}
public static boolean isWifiOnly() {
return "wifi-only".equals(SystemProperties.get("ro.carrier"));
}
/**
* Returns the WIFI IP Addresses, if any, taking into account IPv4 and IPv6 style addresses.
* @param context the application context
* @return the formatted and comma-separated IP addresses, or null if none.
*/
public static String getWifiIpAddresses(Context context) {
ConnectivityManager cm = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
LinkProperties prop = cm.getLinkProperties(ConnectivityManager.TYPE_WIFI);
if (prop == null) return null;
Iterator<InetAddress> iter = prop.getAddresses().iterator();
// If there are no entries, return null
if (!iter.hasNext()) return null;
// Concatenate all available addresses, comma separated
String addresses = "";
while (iter.hasNext()) {
addresses += iter.next().getHostAddress();
if (iter.hasNext()) addresses += ", ";
}
return addresses;
}
public static Locale createLocaleFromString(String localeStr) {
// TODO: is there a better way to actually construct a locale that will match?
// The main problem is, on top of Java specs, locale.toString() and
// new Locale(locale.toString()).toString() do not return equal() strings in
// many cases, because the constructor takes the only string as the language
// code. So : new Locale("en", "US").toString() => "en_US"
// And : new Locale("en_US").toString() => "en_us"
if (null == localeStr)
return Locale.getDefault();
String[] brokenDownLocale = localeStr.split("_", 3);
// split may not return a 0-length array.
if (1 == brokenDownLocale.length) {
return new Locale(brokenDownLocale[0]);
} else if (2 == brokenDownLocale.length) {
return new Locale(brokenDownLocale[0], brokenDownLocale[1]);
} else {
return new Locale(brokenDownLocale[0], brokenDownLocale[1], brokenDownLocale[2]);
}
}
}
|