Home | History | Annotate | Download | only in getinfo
      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.telephony.TelephonyManager;
     29 import android.util.DisplayMetrics;
     30 import android.util.Log;
     31 import android.view.Display;
     32 import android.view.WindowManager;
     33 
     34 import java.io.IOException;
     35 import java.lang.reflect.Field;
     36 import java.util.ArrayList;
     37 import java.util.HashSet;
     38 import java.util.List;
     39 import java.util.Scanner;
     40 import java.util.Set;
     41 
     42 public class DeviceInfoInstrument extends Instrumentation implements DeviceInfoConstants {
     43 
     44     private static final String TAG = "DeviceInfoInstrument";
     45 
     46     private static Bundle mResults = new Bundle();
     47 
     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);
     64         addResult(BUILD_MANUFACTURER, Build.MANUFACTURER);
     65         addResult(BUILD_BRAND, Build.BRAND);
     66         addResult(BUILD_MODEL, Build.MODEL);
     67         addResult(BUILD_TYPE, Build.TYPE);
     68         addResult(BUILD_FINGERPRINT, Build.FINGERPRINT);
     69         addResult(BUILD_ABI, Build.CPU_ABI);
     70         addResult(BUILD_ABI2, Build.CPU_ABI2);
     71         addResult(SERIAL_NUMBER, Build.SERIAL);
     72 
     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();
     80         d.getMetrics(metrics);
     81         addResult(RESOLUTION, String.format("%sx%s", metrics.widthPixels, metrics.heightPixels));
     82         addResult(SCREEN_DENSITY, metrics.density);
     83         addResult(SCREEN_X_DENSITY, metrics.xdpi);
     84         addResult(SCREEN_Y_DENSITY, metrics.ydpi);
     85 
     86         String screenDensityBucket = getScreenDensityBucket(metrics);
     87         addResult(SCREEN_DENSITY_BUCKET, screenDensityBucket);
     88 
     89         String screenSize = getScreenSize();
     90         addResult(SCREEN_SIZE, screenSize);
     91 
     92         Intent intent = new Intent();
     93         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     94         intent.setClass(this.getContext(), DeviceInfoActivity.class);
     95 
     96         DeviceInfoActivity activity = (DeviceInfoActivity) startActivitySync(intent);
     97         waitForIdleSync();
     98         activity.waitForAcitityToFinish();
     99 
    100         TelephonyManager tm = (TelephonyManager) getContext().getSystemService(
    101                 Context.TELEPHONY_SERVICE);
    102         // network
    103         String network = tm.getNetworkOperatorName();
    104         addResult(NETWORK, network.trim());
    105 
    106         // imei
    107         String imei = tm.getDeviceId();
    108         addResult(IMEI, imei);
    109 
    110         // imsi
    111         String imsi = tm.getSubscriberId();
    112         addResult(IMSI, imsi);
    113 
    114         // phone number
    115         String phoneNumber = tm.getLine1Number();
    116         addResult(PHONE_NUMBER, phoneNumber);
    117 
    118         // features
    119         String features = getFeatures();
    120         addResult(FEATURES, features);
    121 
    122         // processes
    123         String processes = getProcesses();
    124         addResult(PROCESSES, processes);
    125 
    126         // OpenGL ES version
    127         String openGlEsVersion = getOpenGlEsVersion();
    128         addResult(OPEN_GL_ES_VERSION, openGlEsVersion);
    129 
    130         // partitions
    131         String partitions = getPartitions();
    132         addResult(PARTITIONS, partitions);
    133 
    134         // System libraries
    135         String sysLibraries = getSystemLibraries();
    136         addResult(SYS_LIBRARIES, sysLibraries);
    137 
    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     }
    170 
    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 
    188             case Configuration.SCREENLAYOUT_SIZE_UNDEFINED:
    189                 screenSize = "undefined";
    190                 break;
    191         }
    192         return screenSize;
    193     }
    194 
    195     private String getScreenDensityBucket(DisplayMetrics metrics) {
    196         switch (metrics.densityDpi) {
    197             case DisplayMetrics.DENSITY_LOW:
    198                 return "ldpi";
    199 
    200             case DisplayMetrics.DENSITY_MEDIUM:
    201                 return "mdpi";
    202 
    203             case DisplayMetrics.DENSITY_HIGH:
    204                 return "hdpi";
    205 
    206             case 320:
    207                 return "xdpi";
    208 
    209             default:
    210                 return "" + metrics.densityDpi;
    211         }
    212     }
    213 
    214     /**
    215      * Return a summary of the device's feature as a semi-colon-delimited list of colon separated
    216      * name and availability pairs like "feature1:sdk:true;feature2:sdk:false;feature3:other:true;".
    217      */
    218     private String getFeatures() {
    219         StringBuilder features = new StringBuilder();
    220 
    221         try {
    222             Set<String> checkedFeatures = new HashSet<String>();
    223 
    224             PackageManager packageManager = getContext().getPackageManager();
    225             for (String featureName : getPackageManagerFeatures()) {
    226                 checkedFeatures.add(featureName);
    227                 boolean hasFeature = packageManager.hasSystemFeature(featureName);
    228                 addFeature(features, featureName, "sdk", hasFeature);
    229             }
    230 
    231             FeatureInfo[] featureInfos = packageManager.getSystemAvailableFeatures();
    232             if (featureInfos != null) {
    233                 for (FeatureInfo featureInfo : featureInfos) {
    234                     if (featureInfo.name != null && !checkedFeatures.contains(featureInfo.name)) {
    235                         addFeature(features, featureInfo.name, "other", true);
    236                     }
    237                 }
    238             }
    239         } catch (Exception exception) {
    240             Log.e(TAG, "Error getting features: " + exception.getMessage(), exception);
    241         }
    242 
    243         return features.toString();
    244     }
    245 
    246     private static void addFeature(StringBuilder features, String name, String type,
    247             boolean available) {
    248         features.append(name).append(':').append(type).append(':').append(available).append(';');
    249     }
    250 
    251     /**
    252      * Use reflection to get the features defined by the SDK. If there are features that do not fit
    253      * the convention of starting with "FEATURE_" then they will still be shown under the
    254      * "Other Features" section.
    255      *
    256      * @return list of feature names from sdk
    257      */
    258     private List<String> getPackageManagerFeatures() {
    259         try {
    260             List<String> features = new ArrayList<String>();
    261             Field[] fields = PackageManager.class.getFields();
    262             for (Field field : fields) {
    263                 if (field.getName().startsWith("FEATURE_")) {
    264                     String feature = (String) field.get(null);
    265                     features.add(feature);
    266                 }
    267             }
    268             return features;
    269         } catch (IllegalAccessException illegalAccess) {
    270             throw new RuntimeException(illegalAccess);
    271         }
    272     }
    273 
    274     /**
    275      * Return a semi-colon-delimited list of the root processes that were running on the phone
    276      * or an error message.
    277      */
    278     private static String getProcesses() {
    279         StringBuilder builder = new StringBuilder();
    280 
    281         try {
    282             String[] rootProcesses = RootProcessScanner.getRootProcesses();
    283             for (String rootProcess : rootProcesses) {
    284                 builder.append(rootProcess).append(';');
    285             }
    286         } catch (Exception exception) {
    287             Log.e(TAG, "Error getting processes: " + exception.getMessage(), exception);
    288             builder.append(exception.getMessage());
    289         }
    290 
    291         return builder.toString();
    292     }
    293 
    294     /** @return a string containing the Open GL ES version number or an error message */
    295     private String getOpenGlEsVersion() {
    296         PackageManager packageManager = getContext().getPackageManager();
    297         FeatureInfo[] featureInfos = packageManager.getSystemAvailableFeatures();
    298         if (featureInfos != null && featureInfos.length > 0) {
    299             for (FeatureInfo featureInfo : featureInfos) {
    300                 // Null feature name means this feature is the open gl es version feature.
    301                 if (featureInfo.name == null) {
    302                     return featureInfo.getGlEsVersion();
    303                 }
    304             }
    305         }
    306         return "No feature for Open GL ES version.";
    307     }
    308 
    309     private String getPartitions() {
    310         try {
    311             StringBuilder builder = new StringBuilder();
    312             Process df = new ProcessBuilder("df").start();
    313             Scanner scanner = new Scanner(df.getInputStream());
    314             try {
    315                 while (scanner.hasNextLine()) {
    316                     builder.append(scanner.nextLine()).append(';');
    317                 }
    318                 return builder.toString();
    319             } finally {
    320                 scanner.close();
    321             }
    322         } catch (IOException e) {
    323             return "Not able to run df for partition information.";
    324         }
    325     }
    326 
    327     private String getSystemLibraries() {
    328         PackageManager pm = getContext().getPackageManager();
    329         String list[] = pm.getSystemSharedLibraryNames();
    330 
    331         StringBuilder builder = new StringBuilder();
    332         for (String lib : list) {
    333             builder.append(lib);
    334             builder.append(";");
    335         }
    336 
    337         return builder.toString();
    338     }
    339 
    340 }
    341