1 /*
2  * Copyright (C) 2018 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.systemui.plugins;
18 
19 import android.hardware.SensorListener;
20 
21 import com.android.systemui.plugins.annotations.ProvidesInterface;
22 
23 /**
24  * Allows for additional sensors to be retrieved from
25  * {@link com.android.systemui.util.sensors.AsyncSensorManager}.
26  */
27 @ProvidesInterface(action = SensorManagerPlugin.ACTION, version = SensorManagerPlugin.VERSION)
28 public interface SensorManagerPlugin extends Plugin {
29     String ACTION = "com.android.systemui.action.PLUGIN_SENSOR_MANAGER";
30     int VERSION = 1;
31 
32     /**
33      * Registers for sensor events. Events will be sent until the listener is unregistered.
34      * @param sensor
35      * @param listener
36      * @see android.hardware.SensorManager#registerListener(SensorListener, int)
37      */
registerListener(Sensor sensor, SensorEventListener listener)38     void registerListener(Sensor sensor, SensorEventListener listener);
39 
40     /**
41      * Unregisters events from the sensor.
42      * @param sensor
43      * @param listener
44      */
unregisterListener(Sensor sensor, SensorEventListener listener)45     void unregisterListener(Sensor sensor, SensorEventListener listener);
46 
47     /**
48      * Listener triggered whenever the Sensor has new data.
49      */
50     interface SensorEventListener {
onSensorChanged(SensorEvent event)51         void onSensorChanged(SensorEvent event);
52     }
53 
54     /**
55      * Sensor that can be defined in a plugin.
56      */
57     class Sensor {
58         public static final int TYPE_WAKE_LOCK_SCREEN = 1;
59         public static final int TYPE_WAKE_DISPLAY = 2;
60         public static final int TYPE_SWIPE = 3;
61         public static final int TYPE_SKIP_STATUS = 4;
62 
63         private int mType;
64 
Sensor(int type)65         public Sensor(int type) {
66             mType = type;
67         }
getType()68         public int getType() {
69             return mType;
70         }
toString()71         public String toString() {
72             return "{PluginSensor type=\"" + mType + "\"}";
73         }
74     }
75 
76     /**
77      * Event sent by a {@link Sensor}.
78      */
79     class SensorEvent {
80         Sensor mSensor;
81         int mVendorType;
82         float[] mValues;
83 
84         /**
85          * Creates a sensor event.
86          * @param sensor The type of sensor, e.g. TYPE_WAKE_LOCK_SCREEN
87          * @param vendorType The vendor type, which should be unique for each type of sensor,
88          *                   e.g. SINGLE_TAP = 1, DOUBLE_TAP = 2, etc.
89          */
SensorEvent(Sensor sensor, int vendorType)90         public SensorEvent(Sensor sensor, int vendorType) {
91             this(sensor, vendorType, null);
92         }
93 
94         /**
95          * Creates a sensor event.
96          * @param sensor The type of sensor, e.g. TYPE_WAKE_LOCK_SCREEN
97          * @param vendorType The vendor type, which should be unique for each type of sensor,
98          *                   e.g. SINGLE_TAP = 1, DOUBLE_TAP = 2, etc.
99          * @param values Values captured by the sensor.
100          */
SensorEvent(Sensor sensor, int vendorType, float[] values)101         public SensorEvent(Sensor sensor, int vendorType, float[] values) {
102             mSensor = sensor;
103             mVendorType = vendorType;
104             mValues = values;
105         }
106 
getSensor()107         public Sensor getSensor() {
108             return mSensor;
109         }
110 
getValues()111         public float[] getValues() {
112             return mValues;
113         }
114 
getVendorType()115         public int getVendorType() {
116             return mVendorType;
117         }
118     }
119 }
120