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 public static final String XR_FEATURE = "android.software.xr.immersive"; 38 39 /** Returns true if the device has a given system feature */ hasSystemFeature(ITestDevice device, String feature)40 public static boolean hasSystemFeature(ITestDevice device, String feature) 41 throws DeviceNotAvailableException { 42 return device.hasFeature(feature); 43 } 44 45 /** Returns true if the device has any feature in a given collection of system features */ hasAnySystemFeature(ITestDevice device, String... features)46 public static boolean hasAnySystemFeature(ITestDevice device, String... features) 47 throws DeviceNotAvailableException { 48 for (String feature : features) { 49 if (device.hasFeature(feature)) { 50 return true; 51 } 52 } 53 return false; 54 } 55 56 /** Returns true if the device has all features in a given collection of system features */ hasAllSystemFeatures(ITestDevice device, String... features)57 public static boolean hasAllSystemFeatures(ITestDevice device, String... features) 58 throws DeviceNotAvailableException { 59 for (String feature : features) { 60 if (!device.hasFeature(feature)) { 61 return false; 62 } 63 } 64 return true; 65 } 66 67 /** Returns all system features of the device */ getAllFeatures(ITestDevice device)68 public static Set<String> getAllFeatures(ITestDevice device) 69 throws DeviceNotAvailableException { 70 Set<String> allFeatures = new HashSet<String>(); 71 String output = device.executeShellCommand("pm list features"); 72 for (String feature : output.split("[\\r?\\n]+")) { 73 allFeatures.add(feature.substring("feature:".length())); 74 } 75 return allFeatures; 76 } 77 78 /** Returns true if the device has feature TV_FEATURE or feature LEANBACK_FEATURE */ isTV(ITestDevice device)79 public static boolean isTV(ITestDevice device) throws DeviceNotAvailableException { 80 return hasAnySystemFeature(device, TV_FEATURE, LEANBACK_FEATURE); 81 } 82 83 /** Returns true if the device has feature WATCH_FEATURE */ isWatch(ITestDevice device)84 public static boolean isWatch(ITestDevice device) throws DeviceNotAvailableException { 85 return hasSystemFeature(device, WATCH_FEATURE); 86 } 87 88 /** Returns true if the device has feature AUTOMOTIVE_FEATURE */ isAutomotive(ITestDevice device)89 public static boolean isAutomotive(ITestDevice device) throws DeviceNotAvailableException { 90 return hasSystemFeature(device, AUTOMOTIVE_FEATURE); 91 } 92 93 /** Returns true if the device has feature XR_FEATURE */ isXrHeadset(ITestDevice device)94 public static boolean isXrHeadset(ITestDevice device) throws DeviceNotAvailableException { 95 return hasSystemFeature(device, XR_FEATURE); 96 } 97 98 /** Returns true if the device is a low ram device: 99 * 1. API level >= O 100 * 2. device has feature LOW_RAM_FEATURE 101 */ isLowRam(ITestDevice device)102 public static boolean isLowRam(ITestDevice device) throws DeviceNotAvailableException { 103 return ApiLevelUtil.isAtLeast(device, VersionCodes.O) && 104 hasSystemFeature(device, LOW_RAM_FEATURE); 105 } 106 107 /** Returns true if the device has feature TELEPHONY_FEATURE */ hasTelephony(ITestDevice device)108 public static boolean hasTelephony(ITestDevice device) throws DeviceNotAvailableException { 109 return hasSystemFeature(device, TELEPHONY_FEATURE); 110 } 111 112 /** Returns true if the device has feature FEATURE_MICROPHONE */ hasMicrophone(ITestDevice device)113 public static boolean hasMicrophone(ITestDevice device) throws DeviceNotAvailableException { 114 return hasSystemFeature(device, FEATURE_MICROPHONE); 115 } 116 } 117