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.server.sensors;
18 
19 import android.annotation.NonNull;
20 import android.hardware.SensorDirectChannel;
21 import android.os.ParcelFileDescriptor;
22 
23 import java.util.concurrent.Executor;
24 
25 /**
26  * Local system service interface for sensors.
27  *
28  * @hide Only for use within system server.
29  */
30 public abstract class SensorManagerInternal {
31     /**
32      * Adds a listener for changes in proximity sensor state.
33      * @param executor The {@link Executor} to {@link Executor#execute invoke} the listener on.
34      * @param listener The listener to add.
35      *
36      * @throws IllegalArgumentException when adding a listener that is already listening
37      */
addProximityActiveListener(@onNull Executor executor, @NonNull ProximityActiveListener listener)38     public abstract void addProximityActiveListener(@NonNull Executor executor,
39             @NonNull ProximityActiveListener listener);
40 
41     /**
42      * Removes a previously registered listener of proximity sensor state changes.
43      * @param listener The listener to remove.
44      */
removeProximityActiveListener(@onNull ProximityActiveListener listener)45     public abstract void removeProximityActiveListener(@NonNull ProximityActiveListener listener);
46 
47     /**
48      * Creates a sensor that is registered at runtime by the system with the sensor service.
49      *
50      * The runtime sensors created here are different from the
51      * <a href="https://source.android.com/docs/core/interaction/sensors/sensors-hal2#dynamic-sensors">
52      * dynamic sensor support in the HAL</a>. These sensors have no HAL dependency and correspond to
53      * sensors that belong to an external (virtual) device.
54      *
55      * @param deviceId The identifier of the device this sensor is associated with.
56      * @param type The generic type of the sensor.
57      * @param name The name of the sensor.
58      * @param vendor The vendor string of the sensor.
59      * @param callback The callback to get notified when the sensor listeners have changed.
60      * @return The sensor handle.
61      */
createRuntimeSensor(int deviceId, int type, @NonNull String name, @NonNull String vendor, float maximumRange, float resolution, float power, int minDelay, int maxDelay, int flags, @NonNull RuntimeSensorCallback callback)62     public abstract int createRuntimeSensor(int deviceId, int type, @NonNull String name,
63             @NonNull String vendor, float maximumRange, float resolution, float power,
64             int minDelay, int maxDelay, int flags, @NonNull RuntimeSensorCallback callback);
65 
66     /**
67      * Unregisters the sensor with the given handle from the framework.
68      */
removeRuntimeSensor(int handle)69     public abstract void removeRuntimeSensor(int handle);
70 
71     /**
72      * Sends an event for the runtime sensor with the given handle to the framework.
73      *
74      * Only relevant for sending runtime sensor events. @see #createRuntimeSensor.
75      *
76      * @param handle The sensor handle.
77      * @param type The type of the sensor.
78      * @param timestampNanos When the event occurred.
79      * @param values The values of the event.
80      * @return Whether the event injection was successful.
81      */
sendSensorEvent(int handle, int type, long timestampNanos, @NonNull float[] values)82     public abstract boolean sendSensorEvent(int handle, int type, long timestampNanos,
83             @NonNull float[] values);
84 
85     /**
86      * Listener for proximity sensor state changes.
87      */
88     public interface ProximityActiveListener {
89         /**
90          * Callback invoked when the proximity sensor state changes
91          * @param isActive whether the sensor is being enabled or disabled.
92          */
onProximityActive(boolean isActive)93         void onProximityActive(boolean isActive);
94     }
95 
96     /**
97      * Callback for runtime sensor state changes. Only relevant to sensors created via
98      * {@link #createRuntimeSensor}, i.e. the dynamic sensors created via the dynamic sensor HAL are
99      * not covered.
100      */
101     public interface RuntimeSensorCallback {
102         /**
103          * Invoked when the listeners of the runtime sensor have changed.
104          * Returns zero on success, negative error code otherwise.
105          */
onConfigurationChanged(int handle, boolean enabled, int samplingPeriodMicros, int batchReportLatencyMicros)106         int onConfigurationChanged(int handle, boolean enabled, int samplingPeriodMicros,
107                 int batchReportLatencyMicros);
108 
109         /**
110          * Invoked when a direct sensor channel has been created.
111          * Wraps the file descriptor in a {@link android.os.SharedMemory} object and passes it to
112          * the client process.
113          * Returns a positive identifier of the channel on success, negative error code otherwise.
114          */
onDirectChannelCreated(ParcelFileDescriptor fd)115         int onDirectChannelCreated(ParcelFileDescriptor fd);
116 
117         /**
118          * Invoked when a direct sensor channel has been destroyed.
119          */
onDirectChannelDestroyed(int channelHandle)120         void onDirectChannelDestroyed(int channelHandle);
121 
122         /**
123          * Invoked when a direct sensor channel has been configured for a sensor.
124          * If the invocation is unsuccessful, a negative error code is returned.
125          * On success, the return value is zero if the rate level is {@code RATE_STOP}, and a
126          * positive report token otherwise.
127          */
onDirectChannelConfigured(int channelHandle, int sensorHandle, @SensorDirectChannel.RateLevel int rateLevel)128         int onDirectChannelConfigured(int channelHandle, int sensorHandle,
129                 @SensorDirectChannel.RateLevel int rateLevel);
130     }
131 }
132