1 /*
2  * Copyright (C) 2016 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.cts.deviceinfo;
17 
18 import android.content.Context;
19 import android.hardware.Sensor;
20 import android.hardware.SensorDirectChannel;
21 import android.hardware.SensorManager;
22 import android.os.Bundle;
23 
24 import com.android.compatibility.common.deviceinfo.DeviceInfo;
25 import com.android.compatibility.common.util.DeviceInfoStore;
26 
27 import java.lang.Exception;
28 import java.util.Arrays;
29 import java.util.ArrayList;
30 import java.util.List;
31 
32 /**
33  * Sensor device info collector.
34  */
35 public class SensorDeviceInfo extends DeviceInfo {
36 
37     private static final String SENSOR = "sensor";
38     private static final String REPORTING_MODE = "reporting_mode";
39     private static final String NAME = "name";
40     private static final String VENDOR = "vendor";
41     private static final String TYPE = "type";
42     private static final String VERSION = "version";
43     private static final String MAXIMUM_RANGE = "maximum_range";
44     private static final String RESOLUTION = "resolution";
45     private static final String POWER = "power";
46     private static final String MIN_DELAY = "min_delay";
47     private static final String FIFO_RESERVED_EVENT_COUNT =
48             "fifo_reserved_event_count";
49     private static final String FIFO_MAX_EVENT_COUNT = "fifo_max_event_count";
50     private static final String STRING_TYPE = "string_type";
51     private static final String ID = "id";
52     private static final String MAX_DELAY = "max_delay";
53     private static final String IS_WAKE_UP_SENSOR = "is_wake_up_sensor";
54     private static final String IS_DYNAMIC_SENSOR = "is_dynamic_sensor";
55     private static final String IS_ADDITONAL_INFO_SUPPORTED =
56             "is_additional_info_supported";
57     private static final String HIGHEST_DIRECT_REPORT_RATE_LEVEL =
58             "highest_direct_report_rate_level";
59     private static final String SUPPORTED_DIRECT_CHANNEL_TYPE =
60             "supported_direct_channel_type";
61     private static final int[] CHANNEL_TYPES = new int[] {
62             SensorDirectChannel.TYPE_MEMORY_FILE,
63             SensorDirectChannel.TYPE_HARDWARE_BUFFER };
64 
65     @Override
collectDeviceInfo(DeviceInfoStore store)66     protected void collectDeviceInfo(DeviceInfoStore store) throws Exception {
67         SensorManager sensorManager = (SensorManager)
68                 getContext().getSystemService(Context.SENSOR_SERVICE);
69         List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
70         store.startArray(SENSOR);
71         for (Sensor sensor : sensors) {
72             store.startGroup();
73             store.addResult(REPORTING_MODE, sensor.getReportingMode());
74             store.addResult(NAME, sensor.getName());
75             store.addResult(VENDOR, sensor.getVendor());
76             store.addResult(TYPE, sensor.getType());
77             store.addResult(VERSION, sensor.getVersion());
78             store.addResult(MAXIMUM_RANGE, sensor.getMaximumRange());
79             store.addResult(RESOLUTION, sensor.getResolution());
80             store.addResult(POWER, sensor.getPower());
81             store.addResult(MIN_DELAY, sensor.getMinDelay());
82             store.addResult(FIFO_RESERVED_EVENT_COUNT,
83                     sensor.getFifoReservedEventCount());
84             store.addResult(FIFO_MAX_EVENT_COUNT,
85                     sensor.getFifoMaxEventCount());
86             store.addResult(STRING_TYPE, sensor.getStringType());
87             store.addResult(ID, sensor.getId());
88             store.addResult(MAX_DELAY, sensor.getMaxDelay());
89             store.addResult(IS_WAKE_UP_SENSOR, sensor.isWakeUpSensor());
90             store.addResult(IS_DYNAMIC_SENSOR, sensor.isDynamicSensor());
91             store.addResult(IS_ADDITONAL_INFO_SUPPORTED,
92                     sensor.isAdditionalInfoSupported());
93             store.addResult(HIGHEST_DIRECT_REPORT_RATE_LEVEL,
94                     sensor.getHighestDirectReportRateLevel());
95 
96             List<Integer> supportedChannelType = new ArrayList<>();
97             for (int channelType : CHANNEL_TYPES) {
98                 if (sensor.isDirectChannelTypeSupported(channelType)) {
99                     supportedChannelType.add(channelType);
100                 }
101             }
102             store.addArrayResult(SUPPORTED_DIRECT_CHANNEL_TYPE,
103                     supportedChannelType.stream().mapToInt(i->i).toArray());
104             store.endGroup();
105         }
106         store.endArray(); // Sensor
107     }
108 }
109