1 /* 2 * Copyright (C) 2008 The Android Open Source Project 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; 23 import android.content.pm.FeatureInfo; 24 import android.content.pm.PackageManager; 25 import android.content.res.Configuration; 26 import android.os.Build; 27 import android.os.Bundle; 28 import android.os.Environment; 29 import android.os.UserManager; 30 import android.telephony.TelephonyManager; 31 import android.text.TextUtils; 32 import android.util.DisplayMetrics; 33 import android.util.Log; 34 import android.view.Display; 35 import android.view.WindowManager; 36 37 import java.io.IOException; 38 import java.lang.reflect.Field; 39 import java.lang.reflect.InvocationTargetException; 40 import java.lang.reflect.Method; 41 import java.util.ArrayList; 42 import java.util.Arrays; 43 import java.util.HashSet; 44 import java.util.List; 45 import java.util.Scanner; 46 import java.util.Set; 47 48 public class DeviceInfoInstrument extends Instrumentation implements DeviceInfoConstants { 49 50 private static final String TAG = "DeviceInfoInstrument"; 51 52 private static Bundle mResults = new Bundle(); 53 DeviceInfoInstrument()54 public DeviceInfoInstrument() { 55 super(); 56 } 57 58 @Override onCreate(Bundle arguments)59 public void onCreate(Bundle arguments) { 60 start(); 61 } 62 63 @Override onStart()64 public void onStart() { 65 addResult(BUILD_ID, Build.ID); 66 addResult(PRODUCT_NAME, Build.PRODUCT); 67 addResult(BUILD_DEVICE, Build.DEVICE); 68 addResult(BUILD_BOARD, Build.BOARD); 69 addResult(BUILD_MANUFACTURER, Build.MANUFACTURER); 70 addResult(BUILD_BRAND, Build.BRAND); 71 addResult(BUILD_MODEL, Build.MODEL); 72 addResult(BUILD_TYPE, Build.TYPE); 73 addResult(BUILD_FINGERPRINT, Build.FINGERPRINT); 74 addResult(BUILD_ABI, Build.CPU_ABI); 75 addResult(BUILD_ABI2, Build.CPU_ABI2); 76 addResult(BUILD_ABIS, TextUtils.join(",", Build.SUPPORTED_ABIS)); 77 addResult(BUILD_ABIS_32, TextUtils.join(",", Build.SUPPORTED_32_BIT_ABIS)); 78 addResult(BUILD_ABIS_64, TextUtils.join(",", Build.SUPPORTED_64_BIT_ABIS)); 79 addResult(SERIAL_NUMBER, Build.SERIAL); 80 81 addResult(VERSION_RELEASE, Build.VERSION.RELEASE); 82 addResult(VERSION_SDK, Build.VERSION.SDK); 83 84 DisplayMetrics metrics = new DisplayMetrics(); 85 WindowManager wm = (WindowManager) getContext().getSystemService( 86 Context.WINDOW_SERVICE); 87 Display d = wm.getDefaultDisplay(); 88 d.getRealMetrics(metrics); 89 addResult(RESOLUTION, String.format("%sx%s", metrics.widthPixels, metrics.heightPixels)); 90 addResult(SCREEN_DENSITY, metrics.density); 91 addResult(SCREEN_X_DENSITY, metrics.xdpi); 92 addResult(SCREEN_Y_DENSITY, metrics.ydpi); 93 94 String screenDensityBucket = getScreenDensityBucket(metrics); 95 addResult(SCREEN_DENSITY_BUCKET, screenDensityBucket); 96 97 String screenSize = getScreenSize(); 98 addResult(SCREEN_SIZE, screenSize); 99 100 Intent intent = new Intent(); 101 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 102 intent.setClass(this.getContext(), DeviceInfoActivity.class); 103 104 DeviceInfoActivity activity = (DeviceInfoActivity) startActivitySync(intent); 105 waitForIdleSync(); 106 activity.waitForAcitityToFinish(); 107 108 TelephonyManager tm = (TelephonyManager) getContext().getSystemService( 109 Context.TELEPHONY_SERVICE); 110 // network 111 String network = tm.getNetworkOperatorName(); 112 addResult(NETWORK, network.trim()); 113 114 // imei 115 String imei = tm.getDeviceId(); 116 addResult(IMEI, imei); 117 118 // imsi 119 String imsi = tm.getSubscriberId(); 120 addResult(IMSI, imsi); 121 122 // phone number 123 String phoneNumber = tm.getLine1Number(); 124 addResult(PHONE_NUMBER, phoneNumber); 125 126 // features 127 String features = getFeatures(); 128 addResult(FEATURES, features); 129 130 // processes 131 String processes = getProcesses(); 132 addResult(PROCESSES, processes); 133 134 // OpenGL ES version 135 String openGlEsVersion = getOpenGlEsVersion(); 136 addResult(OPEN_GL_ES_VERSION, openGlEsVersion); 137 138 // partitions 139 String partitions = getPartitions(); 140 addResult(PARTITIONS, partitions); 141 142 // System libraries 143 String sysLibraries = getSystemLibraries(); 144 addResult(SYS_LIBRARIES, sysLibraries); 145 146 // Storage devices 147 addResult(STORAGE_DEVICES, getStorageDevices()); 148 149 // Multi-user support 150 addResult(MULTI_USER, getMultiUserInfo()); 151 152 finish(Activity.RESULT_OK, mResults); 153 } 154 155 /** 156 * Add string result. 157 * 158 * @param key the string of the key name. 159 * @param value string value. 160 */ addResult(final String key, final String value)161 static void addResult(final String key, final String value){ 162 mResults.putString(key, value); 163 } 164 165 /** 166 * Add integer result. 167 * 168 * @param key the string of the key name. 169 * @param value integer value. 170 */ addResult(final String key, final int value)171 static void addResult(final String key, final int value){ 172 mResults.putInt(key, value); 173 } 174 175 /** 176 * Add float result. 177 * 178 * @param key the string of the key name. 179 * @param value float value. 180 */ addResult(final String key, final float value)181 static void addResult(final String key, final float value){ 182 mResults.putFloat(key, value); 183 } 184 getScreenSize()185 private String getScreenSize() { 186 Configuration config = getContext().getResources().getConfiguration(); 187 int screenLayout = config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK; 188 String screenSize = String.format("0x%x", screenLayout); 189 switch (screenLayout) { 190 case Configuration.SCREENLAYOUT_SIZE_SMALL: 191 screenSize = "small"; 192 break; 193 194 case Configuration.SCREENLAYOUT_SIZE_NORMAL: 195 screenSize = "normal"; 196 break; 197 198 case Configuration.SCREENLAYOUT_SIZE_LARGE: 199 screenSize = "large"; 200 break; 201 202 case Configuration.SCREENLAYOUT_SIZE_XLARGE: 203 screenSize = "xlarge"; 204 break; 205 206 case Configuration.SCREENLAYOUT_SIZE_UNDEFINED: 207 screenSize = "undefined"; 208 break; 209 } 210 return screenSize; 211 } 212 getScreenDensityBucket(DisplayMetrics metrics)213 private String getScreenDensityBucket(DisplayMetrics metrics) { 214 switch (metrics.densityDpi) { 215 case DisplayMetrics.DENSITY_LOW: 216 return "ldpi"; 217 218 case DisplayMetrics.DENSITY_MEDIUM: 219 return "mdpi"; 220 221 case DisplayMetrics.DENSITY_TV: 222 return "tvdpi"; 223 224 case DisplayMetrics.DENSITY_HIGH: 225 return "hdpi"; 226 227 case DisplayMetrics.DENSITY_XHIGH: 228 return "xdpi"; 229 230 default: 231 return "" + metrics.densityDpi; 232 } 233 } 234 235 /** 236 * Return a summary of the device's feature as a semi-colon-delimited list of colon separated 237 * name and availability pairs like "feature1:sdk:true;feature2:sdk:false;feature3:other:true;". 238 */ getFeatures()239 private String getFeatures() { 240 StringBuilder features = new StringBuilder(); 241 242 try { 243 Set<String> checkedFeatures = new HashSet<String>(); 244 245 PackageManager packageManager = getContext().getPackageManager(); 246 for (String featureName : getPackageManagerFeatures()) { 247 checkedFeatures.add(featureName); 248 boolean hasFeature = packageManager.hasSystemFeature(featureName); 249 addFeature(features, featureName, "sdk", hasFeature); 250 } 251 252 FeatureInfo[] featureInfos = packageManager.getSystemAvailableFeatures(); 253 if (featureInfos != null) { 254 for (FeatureInfo featureInfo : featureInfos) { 255 if (featureInfo.name != null && !checkedFeatures.contains(featureInfo.name)) { 256 addFeature(features, featureInfo.name, "other", true); 257 } 258 } 259 } 260 } catch (Exception exception) { 261 Log.e(TAG, "Error getting features: " + exception.getMessage(), exception); 262 } 263 264 return features.toString(); 265 } 266 addFeature(StringBuilder features, String name, String type, boolean available)267 private static void addFeature(StringBuilder features, String name, String type, 268 boolean available) { 269 features.append(name).append(':').append(type).append(':').append(available).append(';'); 270 } 271 272 /** 273 * Use reflection to get the features defined by the SDK. If there are features that do not fit 274 * the convention of starting with "FEATURE_" then they will still be shown under the 275 * "Other Features" section. 276 * 277 * @return list of feature names from sdk 278 */ getPackageManagerFeatures()279 private List<String> getPackageManagerFeatures() { 280 try { 281 List<String> features = new ArrayList<String>(); 282 Field[] fields = PackageManager.class.getFields(); 283 for (Field field : fields) { 284 if (field.getName().startsWith("FEATURE_")) { 285 String feature = (String) field.get(null); 286 features.add(feature); 287 } 288 } 289 return features; 290 } catch (IllegalAccessException illegalAccess) { 291 throw new RuntimeException(illegalAccess); 292 } 293 } 294 295 /** 296 * Return a semi-colon-delimited list of the root processes that were running on the phone 297 * or an error message. 298 */ getProcesses()299 private static String getProcesses() { 300 StringBuilder builder = new StringBuilder(); 301 302 try { 303 String[] rootProcesses = RootProcessScanner.getRootProcesses(); 304 for (String rootProcess : rootProcesses) { 305 builder.append(rootProcess).append(':').append(0).append(';'); 306 } 307 } catch (Exception exception) { 308 Log.e(TAG, "Error getting processes: " + exception.getMessage(), exception); 309 builder.append(exception.getMessage()); 310 } 311 312 return builder.toString(); 313 } 314 315 /** @return a string containing the Open GL ES version number or an error message */ getOpenGlEsVersion()316 private String getOpenGlEsVersion() { 317 PackageManager packageManager = getContext().getPackageManager(); 318 FeatureInfo[] featureInfos = packageManager.getSystemAvailableFeatures(); 319 if (featureInfos != null && featureInfos.length > 0) { 320 for (FeatureInfo featureInfo : featureInfos) { 321 // Null feature name means this feature is the open gl es version feature. 322 if (featureInfo.name == null) { 323 return featureInfo.getGlEsVersion(); 324 } 325 } 326 } 327 return "No feature for Open GL ES version."; 328 } 329 getPartitions()330 private String getPartitions() { 331 try { 332 StringBuilder builder = new StringBuilder(); 333 Process df = new ProcessBuilder("df").start(); 334 Scanner scanner = new Scanner(df.getInputStream()); 335 try { 336 while (scanner.hasNextLine()) { 337 builder.append(scanner.nextLine()).append(';'); 338 } 339 return builder.toString(); 340 } finally { 341 scanner.close(); 342 } 343 } catch (IOException e) { 344 return "Not able to run df for partition information."; 345 } 346 } 347 getSystemLibraries()348 private String getSystemLibraries() { 349 PackageManager pm = getContext().getPackageManager(); 350 String list[] = pm.getSystemSharedLibraryNames(); 351 352 StringBuilder builder = new StringBuilder(); 353 for (String lib : list) { 354 builder.append(lib); 355 builder.append(";"); 356 } 357 358 return builder.toString(); 359 } 360 getStorageDevices()361 private String getStorageDevices() { 362 int count = 0; 363 count = Math.max(count, getContext().getExternalCacheDirs().length); 364 count = Math.max(count, getContext().getExternalFilesDirs(null).length); 365 count = Math.max( 366 count, getContext().getExternalFilesDirs(Environment.DIRECTORY_PICTURES).length); 367 count = Math.max(count, getContext().getObbDirs().length); 368 369 if (Environment.isExternalStorageEmulated()) { 370 if (count == 1) { 371 return "1 emulated"; 372 } else { 373 return "1 emulated, " + (count - 1) + " physical media"; 374 } 375 } else { 376 return count + " physical media"; 377 } 378 } 379 getMultiUserInfo()380 private String getMultiUserInfo() { 381 try { 382 final Method method = UserManager.class.getMethod("getMaxSupportedUsers"); 383 final Integer maxUsers = (Integer) method.invoke(null); 384 if (maxUsers == 1) { 385 return "single user"; 386 } else { 387 return maxUsers + " users supported"; 388 } 389 } catch (ClassCastException e) { 390 } catch (NoSuchMethodException e) { 391 } catch (InvocationTargetException e) { 392 } catch (IllegalAccessException e) { 393 } 394 395 return "unknown"; 396 } 397 } 398