The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 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 | |
| 17 | package com.android.settings; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.Intent; |
| 21 | import android.content.pm.ApplicationInfo; |
| 22 | import android.content.pm.PackageManager; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 23 | import android.content.pm.PackageManager.NameNotFoundException; |
Daisuke Miyakawa | a2633d0 | 2010-09-15 20:09:12 -0700 | [diff] [blame^] | 24 | import android.content.pm.ResolveInfo; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 25 | import android.content.res.Resources; |
| 26 | import android.content.res.Resources.NotFoundException; |
| 27 | import android.graphics.drawable.Drawable; |
| 28 | import android.os.Bundle; |
Amith Yamasani | 60133dd | 2010-09-11 14:17:31 -0700 | [diff] [blame] | 29 | import android.os.SystemProperties; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 30 | import android.preference.Preference; |
| 31 | import android.preference.PreferenceGroup; |
Amith Yamasani | 60133dd | 2010-09-11 14:17:31 -0700 | [diff] [blame] | 32 | import android.telephony.TelephonyManager; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 33 | import android.text.TextUtils; |
Daisuke Miyakawa | a2633d0 | 2010-09-15 20:09:12 -0700 | [diff] [blame^] | 34 | import android.util.Log; |
| 35 | |
| 36 | import java.util.List; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 37 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 38 | public 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 | /** |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 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 | /** |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 64 | * Finds a matching activity for a preference's intent. If a matching |
| 65 | * activity is not found, it will remove the preference. |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 66 | * |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 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) { |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 80 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 81 | Preference preference = parentPreferenceGroup.findPreference(preferenceKey); |
| 82 | if (preference == null) { |
| 83 | return false; |
| 84 | } |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 85 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 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) { |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 96 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 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 | } |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 106 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 107 | return true; |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Did not find a matching activity, so remove the preference |
| 113 | parentPreferenceGroup.removePreference(preference); |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 114 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 115 | return true; |
| 116 | } |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 117 | |
| 118 | /** |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 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 | /** |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 207 | * Returns true if Monkey is running. |
| 208 | */ |
| 209 | public static boolean isMonkeyRunning() { |
| 210 | return SystemProperties.getBoolean("ro.monkey", false); |
| 211 | } |
Amith Yamasani | 60133dd | 2010-09-11 14:17:31 -0700 | [diff] [blame] | 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 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 221 | } |