1 /*
2  * Copyright (C) 2021 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.cts.verifier.audio.audiolib;
18 
19 import android.content.Context;
20 import android.content.pm.PackageManager;
21 import android.util.DisplayMetrics;
22 
23 public class AudioSystemFlags {
24     static final String TAG = AudioSystemFlags.class.getName();
25 
26     private static final float MIN_TV_DIMENSION = 20;
27 
claimsOutput(Context context)28     public static boolean claimsOutput(Context context) {
29         return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUDIO_OUTPUT);
30     }
31 
claimsInput(Context context)32     public static boolean claimsInput(Context context) {
33         return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_MICROPHONE);
34     }
35 
claimsProAudio(Context context)36     public static boolean claimsProAudio(Context context) {
37         return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUDIO_PRO);
38     }
39 
claimsLowLatencyAudio(Context context)40     public static boolean claimsLowLatencyAudio(Context context) {
41         // CDD Section C-1-1: android.hardware.audio.low_latency
42         return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUDIO_LOW_LATENCY);
43     }
44 
claimsMIDI(Context context)45     public static boolean claimsMIDI(Context context) {
46         // CDD Section C-1-4: android.software.midi
47         return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_MIDI);
48     }
49 
claimsUSBHostMode(Context context)50     public static boolean claimsUSBHostMode(Context context) {
51         return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_USB_HOST);
52     }
53 
claimsUSBPeripheralMode(Context context)54     public static boolean claimsUSBPeripheralMode(Context context) {
55         return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_USB_ACCESSORY);
56     }
57 
58     /**
59      * Determines if the device has declared the FEATURE_AUDIO_SPATIAL_HEADTRACKING_LOW_LATENCY
60      * flag
61      * @param context The application context
62      * @return true if the PackageManager declares the system feature
63      *   FEATURE_AUDIO_SPATIAL_HEADTRACKING_LOW_LATENCY
64      */
claimsHeadTrackingLowLatency(Context context)65     public static boolean claimsHeadTrackingLowLatency(Context context) {
66         // Do we need to test FLAG_FEATURE_SPATIAL_AUDIO_HEADTRACKING_LOW_LATENCY?
67         // what namespace is this flag defined in?
68 //        if (!Flags.featureSpatialAudioHeadtrackingLowLatency()) {
69 //            return false;
70 //        }
71         return context.getPackageManager().hasSystemFeature(
72                 PackageManager.FEATURE_AUDIO_SPATIAL_HEADTRACKING_LOW_LATENCY);
73     }
74 
75     /**
76      * @param context The Context of the application.
77      * @return true if the device is a watch
78      */
isWatch(Context context)79     public static boolean isWatch(Context context) {
80         return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH);
81     }
82 
83     /**
84      * @param context The Context of the application.
85      * @return true if the device is Android Auto
86      */
isAutomobile(Context context)87     public static boolean isAutomobile(Context context) {
88         return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE);
89     }
90 
91     /**
92      * @param context The Context of the application.
93      * @return true if the device is a TV
94      */
isTV(Context context)95     public static boolean isTV(Context context) {
96         return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_LEANBACK);
97     }
98 
99     /**
100      * @param context The Context of the application.
101      * @return true if the device is a handheld (Phone or tablet)
102      */
isHandheld(Context context)103     public static boolean isHandheld(Context context) {
104         DisplayMetrics metrics = context.getResources().getDisplayMetrics();
105         float widthInInches = metrics.widthPixels / metrics.xdpi;
106         float heightInInches = metrics.heightPixels / metrics.ydpi;
107 
108         // it is handheld if
109         // 1. it is not identified as any of those special devices
110         // 2. and it is small enough to actually hold in your hand.
111         return !(isWatch(context) || isAutomobile(context) || isTV(context))
112                 && (widthInInches < MIN_TV_DIMENSION && heightInInches < MIN_TV_DIMENSION);
113     }
114 }
115