1/*
2 * Copyright 2014 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10package eu.siacs.conversations.services;
11
12import android.content.Context;
13import android.hardware.Sensor;
14import android.hardware.SensorEvent;
15import android.hardware.SensorEventListener;
16import android.hardware.SensorManager;
17import android.os.Build;
18import android.util.Log;
19
20import androidx.annotation.Nullable;
21
22import org.webrtc.ThreadUtils;
23
24import eu.siacs.conversations.Config;
25import eu.siacs.conversations.utils.AppRTCUtils;
26
27/**
28 * AppRTCProximitySensor manages functions related to the proximity sensor in
29 * the AppRTC demo.
30 * On most device, the proximity sensor is implemented as a boolean-sensor.
31 * It returns just two values "NEAR" or "FAR". Thresholding is done on the LUX
32 * value i.e. the LUX value of the light sensor is compared with a threshold.
33 * A LUX-value more than the threshold means the proximity sensor returns "FAR".
34 * Anything less than the threshold value and the sensor returns "NEAR".
35 */
36public class AppRTCProximitySensor implements SensorEventListener {
37 // This class should be created, started and stopped on one thread
38 // (e.g. the main thread). We use |nonThreadSafe| to ensure that this is
39 // the case. Only active when |DEBUG| is set to true.
40 private final ThreadUtils.ThreadChecker threadChecker = new ThreadUtils.ThreadChecker();
41 private final Runnable onSensorStateListener;
42 private final SensorManager sensorManager;
43 @Nullable
44 private Sensor proximitySensor;
45 private boolean lastStateReportIsNear;
46
47 private AppRTCProximitySensor(Context context, Runnable sensorStateListener) {
48 Log.d(Config.LOGTAG, "AppRTCProximitySensor" + AppRTCUtils.getThreadInfo());
49 onSensorStateListener = sensorStateListener;
50 sensorManager = ((SensorManager) context.getSystemService(Context.SENSOR_SERVICE));
51 }
52
53 /**
54 * Construction
55 */
56 static AppRTCProximitySensor create(Context context, Runnable sensorStateListener) {
57 return new AppRTCProximitySensor(context, sensorStateListener);
58 }
59
60 /**
61 * Activate the proximity sensor. Also do initialization if called for the
62 * first time.
63 */
64 public boolean start() {
65 threadChecker.checkIsOnValidThread();
66 Log.d(Config.LOGTAG, "start" + AppRTCUtils.getThreadInfo());
67 if (!initDefaultSensor()) {
68 // Proximity sensor is not supported on this device.
69 return false;
70 }
71 sensorManager.registerListener(this, proximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
72 return true;
73 }
74
75 /**
76 * Deactivate the proximity sensor.
77 */
78 public void stop() {
79 threadChecker.checkIsOnValidThread();
80 Log.d(Config.LOGTAG, "stop" + AppRTCUtils.getThreadInfo());
81 if (proximitySensor == null) {
82 return;
83 }
84 sensorManager.unregisterListener(this, proximitySensor);
85 }
86
87 /**
88 * Getter for last reported state. Set to true if "near" is reported.
89 */
90 public boolean sensorReportsNearState() {
91 threadChecker.checkIsOnValidThread();
92 return lastStateReportIsNear;
93 }
94
95 @Override
96 public final void onAccuracyChanged(Sensor sensor, int accuracy) {
97 threadChecker.checkIsOnValidThread();
98 AppRTCUtils.assertIsTrue(sensor.getType() == Sensor.TYPE_PROXIMITY);
99 if (accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) {
100 Log.e(Config.LOGTAG, "The values returned by this sensor cannot be trusted");
101 }
102 }
103
104 @Override
105 public final void onSensorChanged(SensorEvent event) {
106 threadChecker.checkIsOnValidThread();
107 AppRTCUtils.assertIsTrue(event.sensor.getType() == Sensor.TYPE_PROXIMITY);
108 // As a best practice; do as little as possible within this method and
109 // avoid blocking.
110 float distanceInCentimeters = event.values[0];
111 if (distanceInCentimeters < proximitySensor.getMaximumRange()) {
112 Log.d(Config.LOGTAG, "Proximity sensor => NEAR state");
113 lastStateReportIsNear = true;
114 } else {
115 Log.d(Config.LOGTAG, "Proximity sensor => FAR state");
116 lastStateReportIsNear = false;
117 }
118 // Report about new state to listening client. Client can then call
119 // sensorReportsNearState() to query the current state (NEAR or FAR).
120 if (onSensorStateListener != null) {
121 onSensorStateListener.run();
122 }
123 Log.d(Config.LOGTAG, "onSensorChanged" + AppRTCUtils.getThreadInfo() + ": "
124 + "accuracy=" + event.accuracy + ", timestamp=" + event.timestamp + ", distance="
125 + event.values[0]);
126 }
127
128 /**
129 * Get default proximity sensor if it exists. Tablet devices (e.g. Nexus 7)
130 * does not support this type of sensor and false will be returned in such
131 * cases.
132 */
133 private boolean initDefaultSensor() {
134 if (proximitySensor != null) {
135 return true;
136 }
137 proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
138 if (proximitySensor == null) {
139 return false;
140 }
141 logProximitySensorInfo();
142 return true;
143 }
144
145 /**
146 * Helper method for logging information about the proximity sensor.
147 */
148 private void logProximitySensorInfo() {
149 if (proximitySensor == null) {
150 return;
151 }
152 StringBuilder info = new StringBuilder("Proximity sensor: ");
153 info.append("name=").append(proximitySensor.getName());
154 info.append(", vendor: ").append(proximitySensor.getVendor());
155 info.append(", power: ").append(proximitySensor.getPower());
156 info.append(", resolution: ").append(proximitySensor.getResolution());
157 info.append(", max range: ").append(proximitySensor.getMaximumRange());
158 info.append(", min delay: ").append(proximitySensor.getMinDelay());
159 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
160 // Added in API level 20.
161 info.append(", type: ").append(proximitySensor.getStringType());
162 }
163 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
164 // Added in API level 21.
165 info.append(", max delay: ").append(proximitySensor.getMaxDelay());
166 info.append(", reporting mode: ").append(proximitySensor.getReportingMode());
167 info.append(", isWakeUpSensor: ").append(proximitySensor.isWakeUpSensor());
168 }
169 Log.d(Config.LOGTAG, info.toString());
170 }
171}