The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 1 | /* |
Brian Muramatsu | 40bdc5e | 2010-08-13 16:20:15 -0700 | [diff] [blame] | 2 | * Copyright (C) 2008 The Android Open Source Project |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy 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, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.tests.getinfo; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.app.Instrumentation; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
Brian Muramatsu | 4af6c4e | 2010-06-21 16:57:12 -0700 | [diff] [blame] | 23 | import android.content.pm.FeatureInfo; |
Brian Muramatsu | a61d323 | 2010-06-08 12:29:36 -0700 | [diff] [blame] | 24 | import android.content.pm.PackageManager; |
Brian Muramatsu | e6b12dc | 2010-11-08 14:56:40 -0800 | [diff] [blame] | 25 | import android.content.res.Configuration; |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 26 | import android.os.Build; |
| 27 | import android.os.Bundle; |
Brett Chabot | 62a17ce | 2009-11-12 13:35:13 -0800 | [diff] [blame] | 28 | import android.telephony.TelephonyManager; |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 29 | import android.util.DisplayMetrics; |
Brian Muramatsu | 7379989 | 2010-06-23 17:42:52 -0700 | [diff] [blame] | 30 | import android.util.Log; |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 31 | import android.view.Display; |
| 32 | import android.view.WindowManager; |
| 33 | |
Brian Muramatsu | 570acf7 | 2011-03-10 17:21:07 -0800 | [diff] [blame] | 34 | import java.io.IOException; |
Brian Muramatsu | 7379989 | 2010-06-23 17:42:52 -0700 | [diff] [blame] | 35 | import java.lang.reflect.Field; |
| 36 | import java.util.ArrayList; |
Brian Muramatsu | 4af6c4e | 2010-06-21 16:57:12 -0700 | [diff] [blame] | 37 | import java.util.HashSet; |
Brian Muramatsu | 7379989 | 2010-06-23 17:42:52 -0700 | [diff] [blame] | 38 | import java.util.List; |
Brian Muramatsu | 570acf7 | 2011-03-10 17:21:07 -0800 | [diff] [blame] | 39 | import java.util.Scanner; |
Brian Muramatsu | 4af6c4e | 2010-06-21 16:57:12 -0700 | [diff] [blame] | 40 | import java.util.Set; |
Brian Muramatsu | 5579039 | 2010-06-14 14:45:42 -0700 | [diff] [blame] | 41 | |
Brett Chabot | 2f903ad | 2011-02-02 18:22:18 -0800 | [diff] [blame] | 42 | public class DeviceInfoInstrument extends Instrumentation implements DeviceInfoConstants { |
Brian Muramatsu | a61d323 | 2010-06-08 12:29:36 -0700 | [diff] [blame] | 43 | |
Brian Muramatsu | 7379989 | 2010-06-23 17:42:52 -0700 | [diff] [blame] | 44 | private static final String TAG = "DeviceInfoInstrument"; |
Brian Muramatsu | a61d323 | 2010-06-08 12:29:36 -0700 | [diff] [blame] | 45 | |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 46 | private static Bundle mResults = new Bundle(); |
| 47 | |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 48 | public DeviceInfoInstrument() { |
| 49 | super(); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public void onCreate(Bundle arguments) { |
| 54 | start(); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public void onStart() { |
| 59 | |
| 60 | addResult(BUILD_ID, Build.ID); |
| 61 | addResult(PRODUCT_NAME, Build.PRODUCT); |
| 62 | addResult(BUILD_DEVICE, Build.DEVICE); |
| 63 | addResult(BUILD_BOARD, Build.BOARD); |
Brian Muramatsu | d6445ae | 2010-10-27 13:31:28 -0700 | [diff] [blame] | 64 | addResult(BUILD_MANUFACTURER, Build.MANUFACTURER); |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 65 | addResult(BUILD_BRAND, Build.BRAND); |
| 66 | addResult(BUILD_MODEL, Build.MODEL); |
| 67 | addResult(BUILD_TYPE, Build.TYPE); |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 68 | addResult(BUILD_FINGERPRINT, Build.FINGERPRINT); |
Brian Muramatsu | 722008e | 2010-05-25 16:44:48 -0700 | [diff] [blame] | 69 | addResult(BUILD_ABI, Build.CPU_ABI); |
| 70 | addResult(BUILD_ABI2, Build.CPU_ABI2); |
Brett Chabot | 770358e | 2011-10-28 17:27:35 -0700 | [diff] [blame] | 71 | addResult(SERIAL_NUMBER, Build.SERIAL); |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 72 | |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 73 | addResult(VERSION_RELEASE, Build.VERSION.RELEASE); |
| 74 | addResult(VERSION_SDK, Build.VERSION.SDK); |
| 75 | |
| 76 | DisplayMetrics metrics = new DisplayMetrics(); |
| 77 | WindowManager wm = (WindowManager) getContext().getSystemService( |
| 78 | Context.WINDOW_SERVICE); |
| 79 | Display d = wm.getDefaultDisplay(); |
Madan Ankapura | 56c756f | 2013-05-10 10:10:27 -0700 | [diff] [blame] | 80 | d.getRealMetrics(metrics); |
Brett Chabot | 770358e | 2011-10-28 17:27:35 -0700 | [diff] [blame] | 81 | addResult(RESOLUTION, String.format("%sx%s", metrics.widthPixels, metrics.heightPixels)); |
Brian Muramatsu | 647277e | 2010-11-16 13:58:42 -0800 | [diff] [blame] | 82 | addResult(SCREEN_DENSITY, metrics.density); |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 83 | addResult(SCREEN_X_DENSITY, metrics.xdpi); |
| 84 | addResult(SCREEN_Y_DENSITY, metrics.ydpi); |
| 85 | |
Brian Muramatsu | e6b12dc | 2010-11-08 14:56:40 -0800 | [diff] [blame] | 86 | String screenDensityBucket = getScreenDensityBucket(metrics); |
| 87 | addResult(SCREEN_DENSITY_BUCKET, screenDensityBucket); |
| 88 | |
| 89 | String screenSize = getScreenSize(); |
| 90 | addResult(SCREEN_SIZE, screenSize); |
| 91 | |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 92 | Intent intent = new Intent(); |
| 93 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 94 | intent.setClass(this.getContext(), DeviceInfoActivity.class); |
| 95 | |
Bill Napier | 6c12b46 | 2009-05-14 11:07:39 -0700 | [diff] [blame] | 96 | DeviceInfoActivity activity = (DeviceInfoActivity) startActivitySync(intent); |
| 97 | waitForIdleSync(); |
| 98 | activity.waitForAcitityToFinish(); |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 99 | |
Brett Chabot | 62a17ce | 2009-11-12 13:35:13 -0800 | [diff] [blame] | 100 | TelephonyManager tm = (TelephonyManager) getContext().getSystemService( |
| 101 | Context.TELEPHONY_SERVICE); |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 102 | // network |
Brett Chabot | 62a17ce | 2009-11-12 13:35:13 -0800 | [diff] [blame] | 103 | String network = tm.getNetworkOperatorName(); |
Brett Chabot | 51b9caa | 2011-09-07 18:44:22 -0700 | [diff] [blame] | 104 | addResult(NETWORK, network.trim()); |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 105 | |
| 106 | // imei |
Brett Chabot | 62a17ce | 2009-11-12 13:35:13 -0800 | [diff] [blame] | 107 | String imei = tm.getDeviceId(); |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 108 | addResult(IMEI, imei); |
| 109 | |
| 110 | // imsi |
Brett Chabot | 62a17ce | 2009-11-12 13:35:13 -0800 | [diff] [blame] | 111 | String imsi = tm.getSubscriberId(); |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 112 | addResult(IMSI, imsi); |
| 113 | |
| 114 | // phone number |
Brett Chabot | 62a17ce | 2009-11-12 13:35:13 -0800 | [diff] [blame] | 115 | String phoneNumber = tm.getLine1Number(); |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 116 | addResult(PHONE_NUMBER, phoneNumber); |
| 117 | |
Brian Muramatsu | a61d323 | 2010-06-08 12:29:36 -0700 | [diff] [blame] | 118 | // features |
| 119 | String features = getFeatures(); |
| 120 | addResult(FEATURES, features); |
| 121 | |
Brian Muramatsu | 5579039 | 2010-06-14 14:45:42 -0700 | [diff] [blame] | 122 | // processes |
| 123 | String processes = getProcesses(); |
| 124 | addResult(PROCESSES, processes); |
| 125 | |
Brian Muramatsu | d89a48b | 2010-07-29 12:03:49 -0700 | [diff] [blame] | 126 | // OpenGL ES version |
| 127 | String openGlEsVersion = getOpenGlEsVersion(); |
| 128 | addResult(OPEN_GL_ES_VERSION, openGlEsVersion); |
| 129 | |
Brian Muramatsu | 570acf7 | 2011-03-10 17:21:07 -0800 | [diff] [blame] | 130 | // partitions |
| 131 | String partitions = getPartitions(); |
| 132 | addResult(PARTITIONS, partitions); |
Brian Muramatsu | d89a48b | 2010-07-29 12:03:49 -0700 | [diff] [blame] | 133 | |
Keun young Park | abed886 | 2011-12-09 10:57:33 -0800 | [diff] [blame] | 134 | // System libraries |
| 135 | String sysLibraries = getSystemLibraries(); |
| 136 | addResult(SYS_LIBRARIES, sysLibraries); |
| 137 | |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 138 | finish(Activity.RESULT_OK, mResults); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Add string result. |
| 143 | * |
| 144 | * @param key the string of the key name. |
| 145 | * @param value string value. |
| 146 | */ |
| 147 | static void addResult(final String key, final String value){ |
| 148 | mResults.putString(key, value); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Add integer result. |
| 153 | * |
| 154 | * @param key the string of the key name. |
| 155 | * @param value integer value. |
| 156 | */ |
| 157 | private void addResult(final String key, final int value){ |
| 158 | mResults.putInt(key, value); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Add float result. |
| 163 | * |
| 164 | * @param key the string of the key name. |
| 165 | * @param value float value. |
| 166 | */ |
| 167 | private void addResult(final String key, final float value){ |
| 168 | mResults.putFloat(key, value); |
| 169 | } |
Brian Muramatsu | a61d323 | 2010-06-08 12:29:36 -0700 | [diff] [blame] | 170 | |
Brian Muramatsu | e6b12dc | 2010-11-08 14:56:40 -0800 | [diff] [blame] | 171 | private String getScreenSize() { |
| 172 | Configuration config = getContext().getResources().getConfiguration(); |
| 173 | int screenLayout = config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK; |
| 174 | String screenSize = String.format("0x%x", screenLayout); |
| 175 | switch (screenLayout) { |
| 176 | case Configuration.SCREENLAYOUT_SIZE_SMALL: |
| 177 | screenSize = "small"; |
| 178 | break; |
| 179 | |
| 180 | case Configuration.SCREENLAYOUT_SIZE_NORMAL: |
| 181 | screenSize = "normal"; |
| 182 | break; |
| 183 | |
| 184 | case Configuration.SCREENLAYOUT_SIZE_LARGE: |
| 185 | screenSize = "large"; |
| 186 | break; |
| 187 | |
Guru Nagarajan | 63c5983 | 2012-01-13 21:33:05 -0800 | [diff] [blame] | 188 | case Configuration.SCREENLAYOUT_SIZE_XLARGE: |
| 189 | screenSize = "xlarge"; |
| 190 | break; |
| 191 | |
Brian Muramatsu | e6b12dc | 2010-11-08 14:56:40 -0800 | [diff] [blame] | 192 | case Configuration.SCREENLAYOUT_SIZE_UNDEFINED: |
| 193 | screenSize = "undefined"; |
| 194 | break; |
| 195 | } |
| 196 | return screenSize; |
| 197 | } |
| 198 | |
| 199 | private String getScreenDensityBucket(DisplayMetrics metrics) { |
| 200 | switch (metrics.densityDpi) { |
| 201 | case DisplayMetrics.DENSITY_LOW: |
| 202 | return "ldpi"; |
| 203 | |
| 204 | case DisplayMetrics.DENSITY_MEDIUM: |
| 205 | return "mdpi"; |
| 206 | |
Guru Nagarajan | 63c5983 | 2012-01-13 21:33:05 -0800 | [diff] [blame] | 207 | case DisplayMetrics.DENSITY_TV: |
| 208 | return "tvdpi"; |
| 209 | |
Brian Muramatsu | e6b12dc | 2010-11-08 14:56:40 -0800 | [diff] [blame] | 210 | case DisplayMetrics.DENSITY_HIGH: |
| 211 | return "hdpi"; |
| 212 | |
Guru Nagarajan | 63c5983 | 2012-01-13 21:33:05 -0800 | [diff] [blame] | 213 | case DisplayMetrics.DENSITY_XHIGH: |
Brian Muramatsu | e6b12dc | 2010-11-08 14:56:40 -0800 | [diff] [blame] | 214 | return "xdpi"; |
| 215 | |
| 216 | default: |
| 217 | return "" + metrics.densityDpi; |
| 218 | } |
| 219 | } |
| 220 | |
Brian Muramatsu | a61d323 | 2010-06-08 12:29:36 -0700 | [diff] [blame] | 221 | /** |
| 222 | * Return a summary of the device's feature as a semi-colon-delimited list of colon separated |
Brian Muramatsu | 4af6c4e | 2010-06-21 16:57:12 -0700 | [diff] [blame] | 223 | * name and availability pairs like "feature1:sdk:true;feature2:sdk:false;feature3:other:true;". |
Brian Muramatsu | a61d323 | 2010-06-08 12:29:36 -0700 | [diff] [blame] | 224 | */ |
| 225 | private String getFeatures() { |
Brian Muramatsu | 4af6c4e | 2010-06-21 16:57:12 -0700 | [diff] [blame] | 226 | StringBuilder features = new StringBuilder(); |
| 227 | |
Brian Muramatsu | 7379989 | 2010-06-23 17:42:52 -0700 | [diff] [blame] | 228 | try { |
| 229 | Set<String> checkedFeatures = new HashSet<String>(); |
Brian Muramatsu | a61d323 | 2010-06-08 12:29:36 -0700 | [diff] [blame] | 230 | |
Brian Muramatsu | 7379989 | 2010-06-23 17:42:52 -0700 | [diff] [blame] | 231 | PackageManager packageManager = getContext().getPackageManager(); |
| 232 | for (String featureName : getPackageManagerFeatures()) { |
| 233 | checkedFeatures.add(featureName); |
| 234 | boolean hasFeature = packageManager.hasSystemFeature(featureName); |
| 235 | addFeature(features, featureName, "sdk", hasFeature); |
Brian Muramatsu | 4af6c4e | 2010-06-21 16:57:12 -0700 | [diff] [blame] | 236 | } |
Brian Muramatsu | 7379989 | 2010-06-23 17:42:52 -0700 | [diff] [blame] | 237 | |
| 238 | FeatureInfo[] featureInfos = packageManager.getSystemAvailableFeatures(); |
| 239 | if (featureInfos != null) { |
| 240 | for (FeatureInfo featureInfo : featureInfos) { |
| 241 | if (featureInfo.name != null && !checkedFeatures.contains(featureInfo.name)) { |
| 242 | addFeature(features, featureInfo.name, "other", true); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | } catch (Exception exception) { |
| 247 | Log.e(TAG, "Error getting features: " + exception.getMessage(), exception); |
Brian Muramatsu | 4af6c4e | 2010-06-21 16:57:12 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | return features.toString(); |
| 251 | } |
| 252 | |
| 253 | private static void addFeature(StringBuilder features, String name, String type, |
| 254 | boolean available) { |
| 255 | features.append(name).append(':').append(type).append(':').append(available).append(';'); |
Brian Muramatsu | a61d323 | 2010-06-08 12:29:36 -0700 | [diff] [blame] | 256 | } |
Brian Muramatsu | 5579039 | 2010-06-14 14:45:42 -0700 | [diff] [blame] | 257 | |
| 258 | /** |
Brian Muramatsu | 7379989 | 2010-06-23 17:42:52 -0700 | [diff] [blame] | 259 | * Use reflection to get the features defined by the SDK. If there are features that do not fit |
| 260 | * the convention of starting with "FEATURE_" then they will still be shown under the |
| 261 | * "Other Features" section. |
| 262 | * |
| 263 | * @return list of feature names from sdk |
| 264 | */ |
| 265 | private List<String> getPackageManagerFeatures() { |
| 266 | try { |
| 267 | List<String> features = new ArrayList<String>(); |
| 268 | Field[] fields = PackageManager.class.getFields(); |
| 269 | for (Field field : fields) { |
| 270 | if (field.getName().startsWith("FEATURE_")) { |
| 271 | String feature = (String) field.get(null); |
| 272 | features.add(feature); |
| 273 | } |
| 274 | } |
| 275 | return features; |
| 276 | } catch (IllegalAccessException illegalAccess) { |
| 277 | throw new RuntimeException(illegalAccess); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /** |
Brian Muramatsu | 5579039 | 2010-06-14 14:45:42 -0700 | [diff] [blame] | 282 | * Return a semi-colon-delimited list of the root processes that were running on the phone |
| 283 | * or an error message. |
| 284 | */ |
| 285 | private static String getProcesses() { |
| 286 | StringBuilder builder = new StringBuilder(); |
| 287 | |
| 288 | try { |
| 289 | String[] rootProcesses = RootProcessScanner.getRootProcesses(); |
| 290 | for (String rootProcess : rootProcesses) { |
Brian Muramatsu | 1d67e19 | 2012-01-18 15:24:23 -0800 | [diff] [blame] | 291 | builder.append(rootProcess).append(':').append(0).append(';'); |
Brian Muramatsu | 5579039 | 2010-06-14 14:45:42 -0700 | [diff] [blame] | 292 | } |
Brian Muramatsu | 7379989 | 2010-06-23 17:42:52 -0700 | [diff] [blame] | 293 | } catch (Exception exception) { |
| 294 | Log.e(TAG, "Error getting processes: " + exception.getMessage(), exception); |
| 295 | builder.append(exception.getMessage()); |
Brian Muramatsu | 5579039 | 2010-06-14 14:45:42 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | return builder.toString(); |
| 299 | } |
Brian Muramatsu | d89a48b | 2010-07-29 12:03:49 -0700 | [diff] [blame] | 300 | |
| 301 | /** @return a string containing the Open GL ES version number or an error message */ |
| 302 | private String getOpenGlEsVersion() { |
| 303 | PackageManager packageManager = getContext().getPackageManager(); |
| 304 | FeatureInfo[] featureInfos = packageManager.getSystemAvailableFeatures(); |
| 305 | if (featureInfos != null && featureInfos.length > 0) { |
| 306 | for (FeatureInfo featureInfo : featureInfos) { |
| 307 | // Null feature name means this feature is the open gl es version feature. |
| 308 | if (featureInfo.name == null) { |
| 309 | return featureInfo.getGlEsVersion(); |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | return "No feature for Open GL ES version."; |
| 314 | } |
Brian Muramatsu | 570acf7 | 2011-03-10 17:21:07 -0800 | [diff] [blame] | 315 | |
| 316 | private String getPartitions() { |
| 317 | try { |
| 318 | StringBuilder builder = new StringBuilder(); |
| 319 | Process df = new ProcessBuilder("df").start(); |
| 320 | Scanner scanner = new Scanner(df.getInputStream()); |
| 321 | try { |
| 322 | while (scanner.hasNextLine()) { |
| 323 | builder.append(scanner.nextLine()).append(';'); |
| 324 | } |
| 325 | return builder.toString(); |
| 326 | } finally { |
| 327 | scanner.close(); |
| 328 | } |
| 329 | } catch (IOException e) { |
| 330 | return "Not able to run df for partition information."; |
| 331 | } |
| 332 | } |
Keun young Park | abed886 | 2011-12-09 10:57:33 -0800 | [diff] [blame] | 333 | |
| 334 | private String getSystemLibraries() { |
| 335 | PackageManager pm = getContext().getPackageManager(); |
| 336 | String list[] = pm.getSystemSharedLibraryNames(); |
| 337 | |
| 338 | StringBuilder builder = new StringBuilder(); |
| 339 | for (String lib : list) { |
| 340 | builder.append(lib); |
| 341 | builder.append(";"); |
| 342 | } |
| 343 | |
| 344 | return builder.toString(); |
| 345 | } |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 346 | } |