Home | History | Annotate | Download | only in build
      1 /*
      2  * Copyright (C) 2012 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 package com.android.tradefed.build;
     17 
     18 import com.android.tradefed.device.DeviceNotAvailableException;
     19 import com.android.tradefed.device.ITestDevice;
     20 
     21 /**
     22  * A wrapper class for a {@link IBuildInfo}, that contains helper methods to retrieve device
     23  * platform build information.
     24  * <p/>
     25  * Intended to be use for "unbundled" aka not device builds {@link IBuildInfo}, that desire
     26  * metadata about what device the build was run on.
     27  */
     28 public class DeviceBuildDescriptor {
     29 
     30     public static final String DEVICE_BUILD_ID = "device_build_id";
     31     public static final String DEVICE_BUILD_ALIAS = "device_build_alias";
     32     public static final String DEVICE_BUILD_FLAVOR = "device_build_flavor";
     33     public static final String DEVICE_DESC = "device_description";
     34     public static final String DEVICE_PRODUCT = "device_product";
     35 
     36     private final IBuildInfo mBuild;
     37 
     38     public DeviceBuildDescriptor(IBuildInfo build) {
     39         mBuild = build;
     40     }
     41 
     42     /**
     43      * Determines if given {@link IBuildInfo} contains device build metadata
     44      *
     45      * @param build
     46      * @return True if the {@link IBuildInfo} contains the device build metadata, false otherwise
     47      */
     48     public static boolean describesDeviceBuild(IBuildInfo build) {
     49         return build.getBuildAttributes().containsKey(DEVICE_BUILD_ID);
     50     }
     51 
     52     /**
     53      * Gets the device build ID. Maps to the ro.build.incremental.id property on device.
     54      */
     55     public String getDeviceBuildId() {
     56         return mBuild.getBuildAttributes().get(DEVICE_BUILD_ID);
     57     }
     58 
     59     /**
     60      * Gets the device build alias. Maps to the ro.build.id property on device. Typically follows
     61      * format IMM76.
     62      */
     63     public String getDeviceBuildAlias() {
     64         return mBuild.getBuildAttributes().get(DEVICE_BUILD_ALIAS);
     65     }
     66 
     67     /**
     68      * Gets the device build flavor eg yakju-userdebug.
     69      */
     70     public String getDeviceBuildFlavor() {
     71         return mBuild.getBuildAttributes().get(DEVICE_BUILD_FLAVOR);
     72     }
     73 
     74     /**
     75      * Gets a description of the device and build. This is typically a more end-user friendly
     76      * description compared with {@link #getDeviceBuildAlias()} and {@link #getDeviceBuildFlavor()}
     77      * but with the possible penalty of being less precise.
     78      * eg. it wouldn't be possible to distinguish the GSM (yakju) and CDMA (mysid) variants of
     79      * Google Galaxy Nexus using this string.
     80      */
     81     public String getDeviceUserDescription() {
     82         return mBuild.getBuildAttributes().get(DEVICE_DESC);
     83     }
     84 
     85     /**
     86      * Get the product and variant of the device, in product:variant format.
     87      */
     88     public String getDeviceProduct() {
     89         return mBuild.getBuildAttributes().get(DEVICE_PRODUCT);
     90     }
     91 
     92     /**
     93      * Inserts attributes from device into build.
     94      *
     95      * @param device
     96      * @throws DeviceNotAvailableException
     97      */
     98     public static void injectDeviceAttributes(ITestDevice device, IBuildInfo b)
     99             throws DeviceNotAvailableException {
    100         b.addBuildAttribute(DEVICE_BUILD_ID, device.getBuildId());
    101         b.addBuildAttribute(DEVICE_BUILD_ALIAS, device.getBuildAlias());
    102         String buildFlavor = String.format("%s-%s", device.getProperty("ro.product.name"),
    103                 device.getProperty("ro.build.type"));
    104         b.addBuildAttribute(DEVICE_BUILD_FLAVOR, buildFlavor);
    105         b.addBuildAttribute(DEVICE_DESC, generateDeviceDesc(device));
    106         b.addBuildAttribute(DEVICE_PRODUCT, generateDeviceProduct(device));
    107     }
    108 
    109     /**
    110      * Generate the device description string from device properties.
    111      * <p/>
    112      * Description should follow this format: eg
    113      * Google Galaxy Nexus 4.2
    114      *
    115      * @param device
    116      * @return The device description string
    117      * @throws DeviceNotAvailableException
    118      */
    119     public static String generateDeviceDesc(ITestDevice device)
    120             throws DeviceNotAvailableException {
    121         // brand is typically all lower case. Capitalize it
    122         String brand =  device.getProperty("ro.product.brand");
    123         if (brand.length() > 1) {
    124             brand = String.format("%s%s", brand.substring(0, 1).toUpperCase(), brand.substring(1));
    125         }
    126         return String.format("%s %s %s", brand, device.getProperty("ro.product.model"),
    127                 device.getProperty("ro.build.version.release"));
    128     }
    129 
    130     /**
    131      * Query the product and variant of the device, in product:variant format.
    132      * @throws DeviceNotAvailableException
    133      */
    134     public static String generateDeviceProduct(ITestDevice device)
    135             throws DeviceNotAvailableException {
    136         return String.format("%s:%s", device.getProductType(), device.getProductVariant());
    137     }
    138 }
    139