1 /*
2  * Copyright (C) 2017 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.compatibility.common.util;
18 
19 import com.android.tradefed.device.DeviceNotAvailableException;
20 import com.android.tradefed.device.ITestDevice;
21 
22 import java.util.HashSet;
23 import java.util.Set;
24 
25 /**
26  * Host-side utility class for detecting system features
27  */
28 public class FeatureUtil {
29 
30     public static final String AUTOMOTIVE_FEATURE = "android.hardware.type.automotive";
31     public static final String LEANBACK_FEATURE = "android.software.leanback";
32     public static final String LOW_RAM_FEATURE = "android.hardware.ram.low";
33     public static final String TELEPHONY_FEATURE = "android.hardware.telephony";
34     public static final String TV_FEATURE = "android.hardware.type.television";
35     public static final String WATCH_FEATURE = "android.hardware.type.watch";
36     public static final String FEATURE_MICROPHONE = "android.hardware.microphone";
37 
38     /** Returns true if the device has a given system feature */
hasSystemFeature(ITestDevice device, String feature)39     public static boolean hasSystemFeature(ITestDevice device, String feature)
40             throws DeviceNotAvailableException {
41         return device.hasFeature(feature);
42     }
43 
44     /** Returns true if the device has any feature in a given collection of system features */
hasAnySystemFeature(ITestDevice device, String... features)45     public static boolean hasAnySystemFeature(ITestDevice device, String... features)
46             throws DeviceNotAvailableException {
47         for (String feature : features) {
48             if (device.hasFeature(feature)) {
49                 return true;
50             }
51         }
52         return false;
53     }
54 
55     /** Returns true if the device has all features in a given collection of system features */
hasAllSystemFeatures(ITestDevice device, String... features)56     public static boolean hasAllSystemFeatures(ITestDevice device, String... features)
57             throws DeviceNotAvailableException {
58         for (String feature : features) {
59             if (!device.hasFeature(feature)) {
60                 return false;
61             }
62         }
63         return true;
64     }
65 
66     /** Returns all system features of the device */
getAllFeatures(ITestDevice device)67     public static Set<String> getAllFeatures(ITestDevice device)
68             throws DeviceNotAvailableException {
69         Set<String> allFeatures = new HashSet<String>();
70         String output = device.executeShellCommand("pm list features");
71         for (String feature : output.split("[\\r?\\n]+")) {
72             allFeatures.add(feature.substring("feature:".length()));
73         }
74         return allFeatures;
75     }
76 
77     /** Returns true if the device has feature TV_FEATURE or feature LEANBACK_FEATURE */
isTV(ITestDevice device)78     public static boolean isTV(ITestDevice device) throws DeviceNotAvailableException {
79         return hasAnySystemFeature(device, TV_FEATURE, LEANBACK_FEATURE);
80     }
81 
82     /** Returns true if the device has feature WATCH_FEATURE */
isWatch(ITestDevice device)83     public static boolean isWatch(ITestDevice device) throws DeviceNotAvailableException {
84         return hasSystemFeature(device, WATCH_FEATURE);
85     }
86 
87     /** Returns true if the device has feature AUTOMOTIVE_FEATURE */
isAutomotive(ITestDevice device)88     public static boolean isAutomotive(ITestDevice device) throws DeviceNotAvailableException {
89         return hasSystemFeature(device, AUTOMOTIVE_FEATURE);
90     }
91 
92     /** Returns true if the device is a low ram device:
93      *  1. API level &gt;= O
94      *  2. device has feature LOW_RAM_FEATURE
95      */
isLowRam(ITestDevice device)96     public static boolean isLowRam(ITestDevice device) throws DeviceNotAvailableException {
97         return ApiLevelUtil.isAtLeast(device, VersionCodes.O) &&
98                 hasSystemFeature(device, LOW_RAM_FEATURE);
99     }
100 
101     /** Returns true if the device has feature TELEPHONY_FEATURE */
hasTelephony(ITestDevice device)102     public static boolean hasTelephony(ITestDevice device) throws DeviceNotAvailableException {
103         return hasSystemFeature(device, TELEPHONY_FEATURE);
104     }
105 
106     /** Returns true if the device has feature FEATURE_MICROPHONE */
hasMicrophone(ITestDevice device)107     public static boolean hasMicrophone(ITestDevice device) throws DeviceNotAvailableException {
108         return hasSystemFeature(device, FEATURE_MICROPHONE);
109     }
110 }
111