diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraControllerIntegrationTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/IntegrationTest.java similarity index 94% rename from cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraControllerIntegrationTest.java rename to cameraview/src/androidTest/java/com/otaliastudios/cameraview/IntegrationTest.java index 192e8f80..d34f950f 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraControllerIntegrationTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/IntegrationTest.java @@ -1,30 +1,19 @@ package com.otaliastudios.cameraview; -import android.content.Context; import android.graphics.Bitmap; -import android.graphics.BitmapFactory; import android.graphics.PointF; -import android.support.test.InstrumentationRegistry; -import android.support.test.espresso.Espresso; import android.support.test.filters.MediumTest; import android.support.test.rule.ActivityTestRule; import android.support.test.runner.AndroidJUnit4; -import android.view.ViewGroup; -import android.widget.FrameLayout; import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; import java.io.File; -import java.util.ArrayList; -import java.util.List; -import java.util.Set; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -35,7 +24,7 @@ import static org.junit.Assert.*; // TODO: won't work well in a 23+ emulator. Permissions will be asked. @RunWith(AndroidJUnit4.class) @MediumTest -public class CameraControllerIntegrationTest extends BaseTest { +public class IntegrationTest extends BaseTest { @Rule public ActivityTestRule rule = new ActivityTestRule<>(TestActivity.class); @@ -68,17 +57,8 @@ public class CameraControllerIntegrationTest extends BaseTest { @After public void tearDown() throws Exception { camera.stopCapturingVideo(); - /* int state = controller.getState(); - if (state >= CameraController.STATE_STARTING) { - // Enqueue a stop and wait. - camera.stop(); - waitForClose(true); - } else if (state == CameraController.STATE_STOPPING) { - // Wait for incoming stop. - waitForClose(true); - } */ camera.destroy(); - WorkerHandler.clearCache(); + WorkerHandler.destroy(); } private CameraOptions waitForOpen(boolean expectSuccess) { @@ -88,7 +68,6 @@ public class CameraControllerIntegrationTest extends BaseTest { CameraOptions result = open.await(4000); if (expectSuccess) { assertNotNull("Can open", result); - // try { Thread.sleep(600); } catch (Exception e) {} } else { assertNull("Should not open", result); } @@ -102,7 +81,6 @@ public class CameraControllerIntegrationTest extends BaseTest { Boolean result = close.await(4000); if (expectSuccess) { assertNotNull("Can close", result); - // try { Thread.sleep(600); } catch (Exception e) {} } else { assertNull("Should not close", result); } diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java index bf6d070b..95935b4a 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java @@ -53,7 +53,7 @@ abstract class CameraController implements Preview.SurfaceCallback { public void uncaughtException(Thread thread, Throwable throwable) { // Something went wrong. Thread is terminated (about to?). // Move to other thread and stop resources. - WorkerHandler.clearCache(); + thread.interrupt(); mHandler = WorkerHandler.get("CameraViewController"); mHandler.post(new Runnable() { @Override diff --git a/cameraview/src/main/utils/com/otaliastudios/cameraview/WorkerHandler.java b/cameraview/src/main/utils/com/otaliastudios/cameraview/WorkerHandler.java index ad48f62b..817dbd34 100644 --- a/cameraview/src/main/utils/com/otaliastudios/cameraview/WorkerHandler.java +++ b/cameraview/src/main/utils/com/otaliastudios/cameraview/WorkerHandler.java @@ -65,7 +65,15 @@ class WorkerHandler { return mThread; } - public static void clearCache() { + public static void destroy() { + for (String key : sCache.keySet()) { + WeakReference ref = sCache.get(key); + WorkerHandler handler = ref.get(); + if (handler != null && handler.getThread().isAlive()) { + handler.getThread().interrupt(); + } + ref.clear(); + } sCache.clear(); } }