1 /*
2  * Copyright (C) 2015 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.compatibility.common.deviceinfo;
17 
18 import android.os.Build;
19 import android.os.Bundle;
20 import android.os.Environment;
21 import android.os.SystemProperties;
22 import android.os.UserManager;
23 import android.telephony.TelephonyManager;
24 import android.text.TextUtils;
25 
26 import java.lang.Integer;
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.util.Map.Entry;
30 
31 import com.android.compatibility.common.deviceinfo.DeviceInfo;
32 import com.android.compatibility.common.util.ApiLevelUtil;
33 import com.android.compatibility.common.util.DeviceInfoStore;
34 import com.android.compatibility.common.util.ShellIdentityUtils;
35 
36 /**
37  * Generic device info collector.
38  */
39 public class GenericDeviceInfo extends DeviceInfo {
40 
41     public static final String BUILD_ID = "build_id";
42     public static final String BUILD_PRODUCT = "build_product";
43     public static final String BUILD_DEVICE = "build_device";
44     public static final String BUILD_BOARD = "build_board";
45     public static final String BUILD_MANUFACTURER = "build_manufacturer";
46     public static final String BUILD_BRAND = "build_brand";
47     public static final String BUILD_MODEL = "build_model";
48     public static final String BUILD_TYPE = "build_type";
49     public static final String BUILD_FINGERPRINT = "build_fingerprint";
50     public static final String BUILD_ABI = "build_abi";
51     public static final String BUILD_ABI2 = "build_abi2";
52     public static final String BUILD_ABIS = "build_abis";
53     public static final String BUILD_ABIS_32 = "build_abis_32";
54     public static final String BUILD_ABIS_64 = "build_abis_64";
55     public static final String BUILD_SERIAL = "build_serial";
56     public static final String BUILD_VERSION_RELEASE = "build_version_release";
57     public static final String BUILD_VERSION_SDK = "build_version_sdk";
58     public static final String BUILD_VERSION_SDK_INT = "build_version_sdk_int";
59     public static final String BUILD_VERSION_BASE_OS = "build_version_base_os";
60     public static final String BUILD_VERSION_SECURITY_PATCH = "build_version_security_patch";
61     public static final String BUILD_REFERENCE_FINGERPRINT = "build_reference_fingerprint";
62 
63     private final Map<String, String> mDeviceInfo = new HashMap<>();
64 
65     @Override
collectDeviceInfo(DeviceInfoStore store)66     protected void collectDeviceInfo(DeviceInfoStore store) throws Exception {
67         store.addResult(BUILD_ID, Build.ID);
68         store.addResult(BUILD_PRODUCT, Build.PRODUCT);
69         store.addResult(BUILD_DEVICE, Build.DEVICE);
70         store.addResult(BUILD_BOARD, Build.BOARD);
71         store.addResult(BUILD_MANUFACTURER, Build.MANUFACTURER);
72         store.addResult(BUILD_BRAND, Build.BRAND);
73         store.addResult(BUILD_MODEL, Build.MODEL);
74         store.addResult(BUILD_TYPE, Build.TYPE);
75         store.addResult(BUILD_FINGERPRINT, Build.FINGERPRINT);
76         store.addResult(BUILD_ABI, Build.CPU_ABI);
77         store.addResult(BUILD_ABI2, Build.CPU_ABI2);
78         if (ApiLevelUtil.isAtLeast(Build.VERSION_CODES.Q)) {
79             store.addResult(BUILD_SERIAL,
80                     ShellIdentityUtils.invokeStaticMethodWithShellPermissions(Build::getSerial));
81         } else if (ApiLevelUtil.isAtLeast(Build.VERSION_CODES.O)) {
82             store.addResult(BUILD_SERIAL, Build.getSerial());
83         } else {
84             store.addResult(BUILD_SERIAL, Build.SERIAL); // deprecated in O
85         }
86         store.addResult(BUILD_VERSION_RELEASE, Build.VERSION.RELEASE);
87         store.addResult(BUILD_VERSION_SDK, Build.VERSION.SDK);
88         store.addResult(BUILD_REFERENCE_FINGERPRINT,
89                 SystemProperties.get("ro.build.reference.fingerprint", ""));
90 
91         // Collect build fields available in API level 21
92         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
93             store.addResult(BUILD_ABIS, TextUtils.join(",", Build.SUPPORTED_ABIS));
94             store.addResult(BUILD_ABIS_32, TextUtils.join(",", Build.SUPPORTED_32_BIT_ABIS));
95             store.addResult(BUILD_ABIS_64, TextUtils.join(",", Build.SUPPORTED_64_BIT_ABIS));
96         }
97 
98         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
99             store.addResult(BUILD_VERSION_BASE_OS, Build.VERSION.BASE_OS);
100             store.addResult(BUILD_VERSION_SECURITY_PATCH, Build.VERSION.SECURITY_PATCH);
101         } else {
102             // Access system properties directly because Build.Version.BASE_OS and
103             // Build.Version.SECURITY_PATCH are not defined pre-M.
104             store.addResult(BUILD_VERSION_BASE_OS,
105                     SystemProperties.get("ro.build.version.base_os", ""));
106             store.addResult(BUILD_VERSION_SECURITY_PATCH,
107                     SystemProperties.get("ro.build.version.security_patch", ""));
108         }
109     }
110 }
111