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