• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 com.android.car.settings.common;
18 
19 import android.car.Car;
20 import android.car.VehicleAreaType;
21 import android.car.VehiclePropertyIds;
22 import android.car.hardware.CarPropertyValue;
23 import android.car.hardware.property.CarPropertyManager;
24 import android.content.Context;
25 import android.os.Build;
26 import android.text.TextUtils;
27 
28 import androidx.annotation.ArrayRes;
29 
30 import com.android.car.settings.R;
31 
32 import java.util.Set;
33 
34 /**
35  * Contains utility functions for build info.
36  */
37 // TODO(b/371116800): move this class to car-apps-common library
38 public class BuildInfoUtil {
39 
BuildInfoUtil()40     private BuildInfoUtil() {
41     }
42 
43     /**
44      * Returns true for builds that are for testing for developers.
45      */
isDevTesting(Context context)46     public static boolean isDevTesting(Context context) {
47         return (Build.IS_ENG || Build.IS_USERDEBUG) && isSupportedBenchOrEmulator(context);
48     }
49 
50     /**
51      * Returns true for builds that are on benches or emulators.
52      */
isSupportedBenchOrEmulator(Context context)53     public static boolean isSupportedBenchOrEmulator(Context context) {
54         return isEmulator() || isSupportedDebugDevice(context) || isSupportedDebugDeviceExcludeCar(
55                 context);
56     }
57 
isEmulator()58     private static boolean isEmulator() {
59         return Build.IS_EMULATOR;
60     }
61 
isSupportedDebugDevice(Context context)62     private static boolean isSupportedDebugDevice(Context context) {
63         return isDebugDeviceIncluded(context, R.array.config_debug_support_devices);
64     }
65 
isDebugDeviceIncluded(Context context, @ArrayRes int resId)66     private static boolean isDebugDeviceIncluded(Context context, @ArrayRes int resId) {
67         Set<String> supportedDevices = Set.of(context.getResources().getStringArray(resId));
68         return supportedDevices.contains(Build.DEVICE);
69     }
70 
isSupportedDebugDeviceExcludeCar(Context context)71     private static boolean isSupportedDebugDeviceExcludeCar(Context context) {
72         return isDebugDeviceIncluded(context, R.array.config_debug_support_devices_exclude_car)
73                 && !isRealCar(context);
74 
75     }
76 
77     /**
78      * Please make sure the VIN numbers on the benches are reset before using this function,
79      * follow the instructions in b/267517048.
80      */
isRealCar(Context context)81     private static boolean isRealCar(Context context) {
82         Car car = Car.createCar(context);
83         CarPropertyManager carPropertyManager = null;
84         if (car != null) {
85             carPropertyManager = (CarPropertyManager) car.getCarManager(Car.PROPERTY_SERVICE);
86         }
87         if (carPropertyManager != null) {
88             try {
89                 CarPropertyValue carPropertyValue = carPropertyManager.getProperty(
90                         VehiclePropertyIds.INFO_VIN, VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL);
91                 if (carPropertyValue != null && carPropertyValue.getPropertyStatus()
92                         == CarPropertyValue.STATUS_AVAILABLE) {
93                     if (TextUtils.isDigitsOnly((CharSequence) carPropertyValue.getValue())) {
94                         return Long.valueOf((String) carPropertyValue.getValue()) != 0;
95                     } else {
96                         return true;
97                     }
98                 }
99             } catch (Exception e) {
100                 // For the situations where there are exceptions, the status of the device is
101                 // uncertain, so it will be treated as a real car in order to avoid showing the
102                 // debug only features.
103                 return true;
104             }
105         }
106         return false;
107     }
108 }
109