Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(185)

Side by Side Diff: webrtc/examples/androidapp/src/org/appspot/apprtc/AppRTCProximitySensor.java

Issue 1992813007: Remove AppRTCUtils.NonThreadSafe in AppRTC Android Demo (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Reorder imports Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | webrtc/examples/androidapp/src/org/appspot/apprtc/util/AppRTCUtils.java » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2014 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2014 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 package org.appspot.apprtc; 11 package org.appspot.apprtc;
12 12
13 import org.appspot.apprtc.util.AppRTCUtils;
14 import org.appspot.apprtc.util.AppRTCUtils.NonThreadSafe;
15
16 import android.content.Context; 13 import android.content.Context;
17 import android.hardware.Sensor; 14 import android.hardware.Sensor;
18 import android.hardware.SensorEvent; 15 import android.hardware.SensorEvent;
19 import android.hardware.SensorEventListener; 16 import android.hardware.SensorEventListener;
20 import android.hardware.SensorManager; 17 import android.hardware.SensorManager;
21 import android.os.Build; 18 import android.os.Build;
22 import android.util.Log; 19 import android.util.Log;
23 20
21 import org.appspot.apprtc.util.AppRTCUtils;
22 import org.webrtc.ThreadUtils;
23
24 /** 24 /**
25 * AppRTCProximitySensor manages functions related to the proximity sensor in 25 * AppRTCProximitySensor manages functions related to the proximity sensor in
26 * the AppRTC demo. 26 * the AppRTC demo.
27 * On most device, the proximity sensor is implemented as a boolean-sensor. 27 * On most device, the proximity sensor is implemented as a boolean-sensor.
28 * It returns just two values "NEAR" or "FAR". Thresholding is done on the LUX 28 * It returns just two values "NEAR" or "FAR". Thresholding is done on the LUX
29 * value i.e. the LUX value of the light sensor is compared with a threshold. 29 * value i.e. the LUX value of the light sensor is compared with a threshold.
30 * A LUX-value more than the threshold means the proximity sensor returns "FAR". 30 * A LUX-value more than the threshold means the proximity sensor returns "FAR".
31 * Anything less than the threshold value and the sensor returns "NEAR". 31 * Anything less than the threshold value and the sensor returns "NEAR".
32 */ 32 */
33 public class AppRTCProximitySensor implements SensorEventListener { 33 public class AppRTCProximitySensor implements SensorEventListener {
34 private static final String TAG = "AppRTCProximitySensor"; 34 private static final String TAG = "AppRTCProximitySensor";
35 35
36 // This class should be created, started and stopped on one thread 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 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. 38 // the case. Only active when |DEBUG| is set to true.
39 private final NonThreadSafe nonThreadSafe = new AppRTCUtils.NonThreadSafe(); 39 private final ThreadUtils.ThreadChecker threadChecker = new ThreadUtils.Thread Checker();
40 40
41 private final Runnable onSensorStateListener; 41 private final Runnable onSensorStateListener;
42 private final SensorManager sensorManager; 42 private final SensorManager sensorManager;
43 private Sensor proximitySensor = null; 43 private Sensor proximitySensor = null;
44 private boolean lastStateReportIsNear = false; 44 private boolean lastStateReportIsNear = false;
45 45
46 /** Construction */ 46 /** Construction */
47 static AppRTCProximitySensor create(Context context, 47 static AppRTCProximitySensor create(Context context,
48 Runnable sensorStateListener) { 48 Runnable sensorStateListener) {
49 return new AppRTCProximitySensor(context, sensorStateListener); 49 return new AppRTCProximitySensor(context, sensorStateListener);
50 } 50 }
51 51
52 private AppRTCProximitySensor(Context context, Runnable sensorStateListener) { 52 private AppRTCProximitySensor(Context context, Runnable sensorStateListener) {
53 Log.d(TAG, "AppRTCProximitySensor" + AppRTCUtils.getThreadInfo()); 53 Log.d(TAG, "AppRTCProximitySensor" + AppRTCUtils.getThreadInfo());
54 onSensorStateListener = sensorStateListener; 54 onSensorStateListener = sensorStateListener;
55 sensorManager = ((SensorManager) context.getSystemService( 55 sensorManager = ((SensorManager) context.getSystemService(
56 Context.SENSOR_SERVICE)); 56 Context.SENSOR_SERVICE));
57 } 57 }
58 58
59 /** 59 /**
60 * Activate the proximity sensor. Also do initializtion if called for the 60 * Activate the proximity sensor. Also do initializtion if called for the
61 * first time. 61 * first time.
62 */ 62 */
63 public boolean start() { 63 public boolean start() {
64 checkIfCalledOnValidThread(); 64 threadChecker.checkIsOnValidThread();
65 Log.d(TAG, "start" + AppRTCUtils.getThreadInfo()); 65 Log.d(TAG, "start" + AppRTCUtils.getThreadInfo());
66 if (!initDefaultSensor()) { 66 if (!initDefaultSensor()) {
67 // Proximity sensor is not supported on this device. 67 // Proximity sensor is not supported on this device.
68 return false; 68 return false;
69 } 69 }
70 sensorManager.registerListener( 70 sensorManager.registerListener(
71 this, proximitySensor, SensorManager.SENSOR_DELAY_NORMAL); 71 this, proximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
72 return true; 72 return true;
73 } 73 }
74 74
75 /** Deactivate the proximity sensor. */ 75 /** Deactivate the proximity sensor. */
76 public void stop() { 76 public void stop() {
77 checkIfCalledOnValidThread(); 77 threadChecker.checkIsOnValidThread();
78 Log.d(TAG, "stop" + AppRTCUtils.getThreadInfo()); 78 Log.d(TAG, "stop" + AppRTCUtils.getThreadInfo());
79 if (proximitySensor == null) { 79 if (proximitySensor == null) {
80 return; 80 return;
81 } 81 }
82 sensorManager.unregisterListener(this, proximitySensor); 82 sensorManager.unregisterListener(this, proximitySensor);
83 } 83 }
84 84
85 /** Getter for last reported state. Set to true if "near" is reported. */ 85 /** Getter for last reported state. Set to true if "near" is reported. */
86 public boolean sensorReportsNearState() { 86 public boolean sensorReportsNearState() {
87 checkIfCalledOnValidThread(); 87 threadChecker.checkIsOnValidThread();
88 return lastStateReportIsNear; 88 return lastStateReportIsNear;
89 } 89 }
90 90
91 @Override 91 @Override
92 public final void onAccuracyChanged(Sensor sensor, int accuracy) { 92 public final void onAccuracyChanged(Sensor sensor, int accuracy) {
93 checkIfCalledOnValidThread(); 93 threadChecker.checkIsOnValidThread();
94 AppRTCUtils.assertIsTrue(sensor.getType() == Sensor.TYPE_PROXIMITY); 94 AppRTCUtils.assertIsTrue(sensor.getType() == Sensor.TYPE_PROXIMITY);
95 if (accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) { 95 if (accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) {
96 Log.e(TAG, "The values returned by this sensor cannot be trusted"); 96 Log.e(TAG, "The values returned by this sensor cannot be trusted");
97 } 97 }
98 } 98 }
99 99
100 @Override 100 @Override
101 public final void onSensorChanged(SensorEvent event) { 101 public final void onSensorChanged(SensorEvent event) {
102 checkIfCalledOnValidThread(); 102 threadChecker.checkIsOnValidThread();
103 AppRTCUtils.assertIsTrue(event.sensor.getType() == Sensor.TYPE_PROXIMITY); 103 AppRTCUtils.assertIsTrue(event.sensor.getType() == Sensor.TYPE_PROXIMITY);
104 // As a best practice; do as little as possible within this method and 104 // As a best practice; do as little as possible within this method and
105 // avoid blocking. 105 // avoid blocking.
106 float distanceInCentimeters = event.values[0]; 106 float distanceInCentimeters = event.values[0];
107 if (distanceInCentimeters < proximitySensor.getMaximumRange()) { 107 if (distanceInCentimeters < proximitySensor.getMaximumRange()) {
108 Log.d(TAG, "Proximity sensor => NEAR state"); 108 Log.d(TAG, "Proximity sensor => NEAR state");
109 lastStateReportIsNear = true; 109 lastStateReportIsNear = true;
110 } else { 110 } else {
111 Log.d(TAG, "Proximity sensor => FAR state"); 111 Log.d(TAG, "Proximity sensor => FAR state");
112 lastStateReportIsNear = false; 112 lastStateReportIsNear = false;
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 } 161 }
162 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 162 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
163 // Added in API level 21. 163 // Added in API level 21.
164 info.append(", max delay: " + proximitySensor.getMaxDelay()); 164 info.append(", max delay: " + proximitySensor.getMaxDelay());
165 info.append(", reporting mode: " + proximitySensor.getReportingMode()); 165 info.append(", reporting mode: " + proximitySensor.getReportingMode());
166 info.append(", isWakeUpSensor: " + proximitySensor.isWakeUpSensor()); 166 info.append(", isWakeUpSensor: " + proximitySensor.isWakeUpSensor());
167 } 167 }
168 Log.d(TAG, info.toString()); 168 Log.d(TAG, info.toString());
169 } 169 }
170 170
171 /**
172 * Helper method for debugging purposes. Ensures that method is
173 * called on same thread as this object was created on.
174 */
175 private void checkIfCalledOnValidThread() {
176 if (!nonThreadSafe.calledOnValidThread()) {
177 throw new IllegalStateException("Method is not called on valid thread");
178 }
179 }
180 } 171 }
OLDNEW
« no previous file with comments | « no previous file | webrtc/examples/androidapp/src/org/appspot/apprtc/util/AppRTCUtils.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698