Utils.java revision a2633d0232adefd2767484add759a46906e00bcc
1/** 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.PackageManager.NameNotFoundException; 24import android.content.pm.ResolveInfo; 25import android.content.res.Resources; 26import android.content.res.Resources.NotFoundException; 27import android.graphics.drawable.Drawable; 28import android.os.Bundle; 29import android.os.SystemProperties; 30import android.preference.Preference; 31import android.preference.PreferenceGroup; 32import android.telephony.TelephonyManager; 33import android.text.TextUtils; 34import android.util.Log; 35 36import java.util.List; 37 38public class Utils { 39 40 /** 41 * Set the preference's title to the matching activity's label. 42 */ 43 public static final int UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY = 1; 44 45 /** 46 * Name of the meta-data item that should be set in the AndroidManifest.xml 47 * to specify the icon that should be displayed for the preference. 48 */ 49 private static final String META_DATA_PREFERENCE_ICON = "com.android.settings.icon"; 50 51 /** 52 * Name of the meta-data item that should be set in the AndroidManifest.xml 53 * to specify the title that should be displayed for the preference. 54 */ 55 private static final String META_DATA_PREFERENCE_TITLE = "com.android.settings.title"; 56 57 /** 58 * Name of the meta-data item that should be set in the AndroidManifest.xml 59 * to specify the summary text that should be displayed for the preference. 60 */ 61 private static final String META_DATA_PREFERENCE_SUMMARY = "com.android.settings.summary"; 62 63 /** 64 * Finds a matching activity for a preference's intent. If a matching 65 * activity is not found, it will remove the preference. 66 * 67 * @param context The context. 68 * @param parentPreferenceGroup The preference group that contains the 69 * preference whose intent is being resolved. 70 * @param preferenceKey The key of the preference whose intent is being 71 * resolved. 72 * @param flags 0 or one or more of 73 * {@link #UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY} 74 * . 75 * @return Whether an activity was found. If false, the preference was 76 * removed. 77 */ 78 public static boolean updatePreferenceToSpecificActivityOrRemove(Context context, 79 PreferenceGroup parentPreferenceGroup, String preferenceKey, int flags) { 80 81 Preference preference = parentPreferenceGroup.findPreference(preferenceKey); 82 if (preference == null) { 83 return false; 84 } 85 86 Intent intent = preference.getIntent(); 87 if (intent != null) { 88 // Find the activity that is in the system image 89 PackageManager pm = context.getPackageManager(); 90 List<ResolveInfo> list = pm.queryIntentActivities(intent, 0); 91 int listSize = list.size(); 92 for (int i = 0; i < listSize; i++) { 93 ResolveInfo resolveInfo = list.get(i); 94 if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) 95 != 0) { 96 97 // Replace the intent with this specific activity 98 preference.setIntent(new Intent().setClassName( 99 resolveInfo.activityInfo.packageName, 100 resolveInfo.activityInfo.name)); 101 102 if ((flags & UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY) != 0) { 103 // Set the preference title to the activity's label 104 preference.setTitle(resolveInfo.loadLabel(pm)); 105 } 106 107 return true; 108 } 109 } 110 } 111 112 // Did not find a matching activity, so remove the preference 113 parentPreferenceGroup.removePreference(preference); 114 115 return true; 116 } 117 118 /** 119 * Finds a matching activity for a preference's intent. If a matching 120 * activity is not found, it will remove the preference. The icon, title and 121 * summary of the preference will also be updated with the values retrieved 122 * from the activity's meta-data elements. If no meta-data elements are 123 * specified then the preference title will be set to match the label of the 124 * activity, an icon and summary text will not be displayed. 125 * 126 * @param context The context. 127 * @param parentPreferenceGroup The preference group that contains the 128 * preference whose intent is being resolved. 129 * @param preferenceKey The key of the preference whose intent is being 130 * resolved. 131 * 132 * @return Whether an activity was found. If false, the preference was 133 * removed. 134 * 135 * @see {@link #META_DATA_PREFERENCE_ICON} 136 * {@link #META_DATA_PREFERENCE_TITLE} 137 * {@link #META_DATA_PREFERENCE_SUMMARY} 138 */ 139 public static boolean updatePreferenceToSpecificActivityFromMetaDataOrRemove(Context context, 140 PreferenceGroup parentPreferenceGroup, String preferenceKey) { 141 142 IconPreferenceScreen preference = (IconPreferenceScreen)parentPreferenceGroup 143 .findPreference(preferenceKey); 144 if (preference == null) { 145 return false; 146 } 147 148 Intent intent = preference.getIntent(); 149 if (intent != null) { 150 // Find the activity that is in the system image 151 PackageManager pm = context.getPackageManager(); 152 List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA); 153 int listSize = list.size(); 154 for (int i = 0; i < listSize; i++) { 155 ResolveInfo resolveInfo = list.get(i); 156 if ((resolveInfo.activityInfo.applicationInfo.flags 157 & ApplicationInfo.FLAG_SYSTEM) != 0) { 158 Drawable icon = null; 159 String title = null; 160 String summary = null; 161 162 // Get the activity's meta-data 163 try { 164 Resources res = pm 165 .getResourcesForApplication(resolveInfo.activityInfo.packageName); 166 Bundle metaData = resolveInfo.activityInfo.metaData; 167 168 if (res != null && metaData != null) { 169 icon = res.getDrawable(metaData.getInt(META_DATA_PREFERENCE_ICON)); 170 title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE)); 171 summary = res.getString(metaData.getInt(META_DATA_PREFERENCE_SUMMARY)); 172 } 173 } catch (NameNotFoundException e) { 174 // Ignore 175 } catch (NotFoundException e) { 176 // Ignore 177 } 178 179 // Set the preference title to the activity's label if no 180 // meta-data is found 181 if (TextUtils.isEmpty(title)) { 182 title = resolveInfo.loadLabel(pm).toString(); 183 } 184 185 // Set icon, title and summary for the preference 186 preference.setIcon(icon); 187 preference.setTitle(title); 188 preference.setSummary(summary); 189 190 // Replace the intent with this specific activity 191 preference.setIntent(new Intent().setClassName( 192 resolveInfo.activityInfo.packageName, 193 resolveInfo.activityInfo.name)); 194 195 return true; 196 } 197 } 198 } 199 200 // Did not find a matching activity, so remove the preference 201 parentPreferenceGroup.removePreference(preference); 202 203 return false; 204 } 205 206 /** 207 * Returns true if Monkey is running. 208 */ 209 public static boolean isMonkeyRunning() { 210 return SystemProperties.getBoolean("ro.monkey", false); 211 } 212 213 /** 214 * Returns whether the device is voice-capable (meaning, it is also a phone). 215 */ 216 public static boolean isVoiceCapable(Context context) { 217 TelephonyManager telephony = 218 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 219 return telephony != null && telephony.isVoiceCapable(); 220 } 221} 222