1 /*
2  * Copyright (C) 2014 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.util;
17 
18 import com.android.tradefed.build.IBuildInfo;
19 import com.android.tradefed.device.DeviceNotAvailableException;
20 import com.android.tradefed.device.DeviceProperties;
21 import com.android.tradefed.device.ITestDevice;
22 
23 /** A util class to help manipulate {@link IBuildInfo} */
24 public class BuildInfoUtil {
25 
26     /**
27      * Reads build attributes from device and use them to override the relevant build info fields
28      *
29      * <p>Note: because branch information is not stored on device as build attributes, the injected
30      * branch info will be the following fields concatenated via dashes:
31      *
32      * <ul>
33      *   <li><code>ro.product.brand</code>
34      *   <li><code>ro.product.name</code>
35      *   <li><code>ro.product.vendor.device</code> (maybe different on older API levels)
36      *   <li><code>ro.build.version.release</code>
37      * </ul>
38      *
39      * @param buildInfo the build info where device build attributes will be injected
40      * @param device the device to read build attributes from
41      * @param overrideBuildId instead of reading from device, override build id to this value;
42      *     <code>null</code> for no override
43      * @param overrideBuildFlavor instead of reading from device, override build flavor to this
44      *     value; <code>null</code> for no override
45      * @param overrideBuildBranch instead of concatenating device attributes as substitute for
46      *     branch, override it to this value; <code>null</code> for no override
47      * @param overrideBuildAlias instead of reading from device, override build alias to this value;
48      *     <code>null</code> for no override
49      * @throws DeviceNotAvailableException
50      */
bootstrapDeviceBuildAttributes( IBuildInfo buildInfo, ITestDevice device, String overrideBuildId, String overrideBuildFlavor, String overrideBuildBranch, String overrideBuildAlias)51     public static void bootstrapDeviceBuildAttributes(
52             IBuildInfo buildInfo,
53             ITestDevice device,
54             String overrideBuildId,
55             String overrideBuildFlavor,
56             String overrideBuildBranch,
57             String overrideBuildAlias)
58             throws DeviceNotAvailableException {
59         String buildId, buildAlias, buildFlavor, branch;
60         // inject build id
61         if (overrideBuildId != null) {
62             buildId = overrideBuildId;
63         } else {
64             buildId = device.getBuildId();
65         }
66         buildInfo.setBuildId(buildId);
67 
68         // inject build alias
69         if (overrideBuildAlias != null) {
70             buildAlias = overrideBuildAlias;
71         } else {
72             buildAlias = device.getBuildAlias();
73         }
74         buildInfo.addBuildAttribute("build_alias", buildAlias);
75 
76         // inject build flavor
77         if (overrideBuildFlavor != null) {
78             buildFlavor = overrideBuildFlavor;
79         } else {
80             buildFlavor = device.getBuildFlavor();
81         }
82         buildInfo.setBuildFlavor(buildFlavor);
83 
84         // generate branch information, either via parameter override, or via concatenating fields
85         if (overrideBuildBranch != null) {
86             branch = overrideBuildBranch;
87         } else {
88             branch =
89                     String.format(
90                             "%s-%s-%s-%s",
91                             device.getProperty(DeviceProperties.BRAND),
92                             device.getProperty(DeviceProperties.PRODUCT),
93                             device.getProductVariant(),
94                             device.getProperty(DeviceProperties.RELEASE_VERSION));
95         }
96         buildInfo.setBuildBranch(branch);
97     }
98 }
99