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