Remove AppRTCUtils.NonThreadSafe in AppRTC Android Demo
ThreadUtils.ThreadChecker in WebRTC does the same thing. No point in having a duplicate implementation. Review-Url: https://codereview.webrtc.org/1992813007 Cr-Commit-Position: refs/heads/master@{#12824}
This commit is contained in:
@ -10,9 +10,6 @@
|
|||||||
|
|
||||||
package org.appspot.apprtc;
|
package org.appspot.apprtc;
|
||||||
|
|
||||||
import org.appspot.apprtc.util.AppRTCUtils;
|
|
||||||
import org.appspot.apprtc.util.AppRTCUtils.NonThreadSafe;
|
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.hardware.Sensor;
|
import android.hardware.Sensor;
|
||||||
import android.hardware.SensorEvent;
|
import android.hardware.SensorEvent;
|
||||||
@ -21,6 +18,9 @@ import android.hardware.SensorManager;
|
|||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import org.appspot.apprtc.util.AppRTCUtils;
|
||||||
|
import org.webrtc.ThreadUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AppRTCProximitySensor manages functions related to the proximity sensor in
|
* AppRTCProximitySensor manages functions related to the proximity sensor in
|
||||||
* the AppRTC demo.
|
* the AppRTC demo.
|
||||||
@ -36,7 +36,7 @@ public class AppRTCProximitySensor implements SensorEventListener {
|
|||||||
// This class should be created, started and stopped on one thread
|
// This class should be created, started and stopped on one thread
|
||||||
// (e.g. the main thread). We use |nonThreadSafe| to ensure that this is
|
// (e.g. the main thread). We use |nonThreadSafe| to ensure that this is
|
||||||
// the case. Only active when |DEBUG| is set to true.
|
// the case. Only active when |DEBUG| is set to true.
|
||||||
private final NonThreadSafe nonThreadSafe = new AppRTCUtils.NonThreadSafe();
|
private final ThreadUtils.ThreadChecker threadChecker = new ThreadUtils.ThreadChecker();
|
||||||
|
|
||||||
private final Runnable onSensorStateListener;
|
private final Runnable onSensorStateListener;
|
||||||
private final SensorManager sensorManager;
|
private final SensorManager sensorManager;
|
||||||
@ -61,7 +61,7 @@ public class AppRTCProximitySensor implements SensorEventListener {
|
|||||||
* first time.
|
* first time.
|
||||||
*/
|
*/
|
||||||
public boolean start() {
|
public boolean start() {
|
||||||
checkIfCalledOnValidThread();
|
threadChecker.checkIsOnValidThread();
|
||||||
Log.d(TAG, "start" + AppRTCUtils.getThreadInfo());
|
Log.d(TAG, "start" + AppRTCUtils.getThreadInfo());
|
||||||
if (!initDefaultSensor()) {
|
if (!initDefaultSensor()) {
|
||||||
// Proximity sensor is not supported on this device.
|
// Proximity sensor is not supported on this device.
|
||||||
@ -74,7 +74,7 @@ public class AppRTCProximitySensor implements SensorEventListener {
|
|||||||
|
|
||||||
/** Deactivate the proximity sensor. */
|
/** Deactivate the proximity sensor. */
|
||||||
public void stop() {
|
public void stop() {
|
||||||
checkIfCalledOnValidThread();
|
threadChecker.checkIsOnValidThread();
|
||||||
Log.d(TAG, "stop" + AppRTCUtils.getThreadInfo());
|
Log.d(TAG, "stop" + AppRTCUtils.getThreadInfo());
|
||||||
if (proximitySensor == null) {
|
if (proximitySensor == null) {
|
||||||
return;
|
return;
|
||||||
@ -84,13 +84,13 @@ public class AppRTCProximitySensor implements SensorEventListener {
|
|||||||
|
|
||||||
/** Getter for last reported state. Set to true if "near" is reported. */
|
/** Getter for last reported state. Set to true if "near" is reported. */
|
||||||
public boolean sensorReportsNearState() {
|
public boolean sensorReportsNearState() {
|
||||||
checkIfCalledOnValidThread();
|
threadChecker.checkIsOnValidThread();
|
||||||
return lastStateReportIsNear;
|
return lastStateReportIsNear;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void onAccuracyChanged(Sensor sensor, int accuracy) {
|
public final void onAccuracyChanged(Sensor sensor, int accuracy) {
|
||||||
checkIfCalledOnValidThread();
|
threadChecker.checkIsOnValidThread();
|
||||||
AppRTCUtils.assertIsTrue(sensor.getType() == Sensor.TYPE_PROXIMITY);
|
AppRTCUtils.assertIsTrue(sensor.getType() == Sensor.TYPE_PROXIMITY);
|
||||||
if (accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) {
|
if (accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) {
|
||||||
Log.e(TAG, "The values returned by this sensor cannot be trusted");
|
Log.e(TAG, "The values returned by this sensor cannot be trusted");
|
||||||
@ -99,7 +99,7 @@ public class AppRTCProximitySensor implements SensorEventListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void onSensorChanged(SensorEvent event) {
|
public final void onSensorChanged(SensorEvent event) {
|
||||||
checkIfCalledOnValidThread();
|
threadChecker.checkIsOnValidThread();
|
||||||
AppRTCUtils.assertIsTrue(event.sensor.getType() == Sensor.TYPE_PROXIMITY);
|
AppRTCUtils.assertIsTrue(event.sensor.getType() == Sensor.TYPE_PROXIMITY);
|
||||||
// As a best practice; do as little as possible within this method and
|
// As a best practice; do as little as possible within this method and
|
||||||
// avoid blocking.
|
// avoid blocking.
|
||||||
@ -168,13 +168,4 @@ public class AppRTCProximitySensor implements SensorEventListener {
|
|||||||
Log.d(TAG, info.toString());
|
Log.d(TAG, info.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper method for debugging purposes. Ensures that method is
|
|
||||||
* called on same thread as this object was created on.
|
|
||||||
*/
|
|
||||||
private void checkIfCalledOnValidThread() {
|
|
||||||
if (!nonThreadSafe.calledOnValidThread()) {
|
|
||||||
throw new IllegalStateException("Method is not called on valid thread");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,24 +21,6 @@ public final class AppRTCUtils {
|
|||||||
private AppRTCUtils() {
|
private AppRTCUtils() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* NonThreadSafe is a helper class used to help verify that methods of a
|
|
||||||
* class are called from the same thread.
|
|
||||||
*/
|
|
||||||
public static class NonThreadSafe {
|
|
||||||
private final Long threadId;
|
|
||||||
|
|
||||||
public NonThreadSafe() {
|
|
||||||
// Store thread ID of the creating thread.
|
|
||||||
threadId = Thread.currentThread().getId();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Checks if the method is called on the valid/creating thread. */
|
|
||||||
public boolean calledOnValidThread() {
|
|
||||||
return threadId.equals(Thread.currentThread().getId());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Helper method which throws an exception when an assertion has failed. */
|
/** Helper method which throws an exception when an assertion has failed. */
|
||||||
public static void assertIsTrue(boolean condition) {
|
public static void assertIsTrue(boolean condition) {
|
||||||
if (!condition) {
|
if (!condition) {
|
||||||
|
|||||||
Reference in New Issue
Block a user