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:
sakal
2016-05-20 03:15:58 -07:00
committed by Commit bot
parent 7cf11476b9
commit 2d285ca150
2 changed files with 9 additions and 36 deletions

View File

@ -10,9 +10,6 @@
package org.appspot.apprtc;
import org.appspot.apprtc.util.AppRTCUtils;
import org.appspot.apprtc.util.AppRTCUtils.NonThreadSafe;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
@ -21,6 +18,9 @@ import android.hardware.SensorManager;
import android.os.Build;
import android.util.Log;
import org.appspot.apprtc.util.AppRTCUtils;
import org.webrtc.ThreadUtils;
/**
* AppRTCProximitySensor manages functions related to the proximity sensor in
* the AppRTC demo.
@ -36,7 +36,7 @@ public class AppRTCProximitySensor implements SensorEventListener {
// This class should be created, started and stopped on one thread
// (e.g. the main thread). We use |nonThreadSafe| to ensure that this is
// 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 SensorManager sensorManager;
@ -61,7 +61,7 @@ public class AppRTCProximitySensor implements SensorEventListener {
* first time.
*/
public boolean start() {
checkIfCalledOnValidThread();
threadChecker.checkIsOnValidThread();
Log.d(TAG, "start" + AppRTCUtils.getThreadInfo());
if (!initDefaultSensor()) {
// Proximity sensor is not supported on this device.
@ -74,7 +74,7 @@ public class AppRTCProximitySensor implements SensorEventListener {
/** Deactivate the proximity sensor. */
public void stop() {
checkIfCalledOnValidThread();
threadChecker.checkIsOnValidThread();
Log.d(TAG, "stop" + AppRTCUtils.getThreadInfo());
if (proximitySensor == null) {
return;
@ -84,13 +84,13 @@ public class AppRTCProximitySensor implements SensorEventListener {
/** Getter for last reported state. Set to true if "near" is reported. */
public boolean sensorReportsNearState() {
checkIfCalledOnValidThread();
threadChecker.checkIsOnValidThread();
return lastStateReportIsNear;
}
@Override
public final void onAccuracyChanged(Sensor sensor, int accuracy) {
checkIfCalledOnValidThread();
threadChecker.checkIsOnValidThread();
AppRTCUtils.assertIsTrue(sensor.getType() == Sensor.TYPE_PROXIMITY);
if (accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) {
Log.e(TAG, "The values returned by this sensor cannot be trusted");
@ -99,7 +99,7 @@ public class AppRTCProximitySensor implements SensorEventListener {
@Override
public final void onSensorChanged(SensorEvent event) {
checkIfCalledOnValidThread();
threadChecker.checkIsOnValidThread();
AppRTCUtils.assertIsTrue(event.sensor.getType() == Sensor.TYPE_PROXIMITY);
// As a best practice; do as little as possible within this method and
// avoid blocking.
@ -168,13 +168,4 @@ public class AppRTCProximitySensor implements SensorEventListener {
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");
}
}
}

View File

@ -21,24 +21,6 @@ public final class 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. */
public static void assertIsTrue(boolean condition) {
if (!condition) {