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