WorkerHandler changes

pull/37/head
Mattia Iavarone 8 years ago
parent da1576fdf8
commit 9daa144e2d
  1. 26
      cameraview/src/androidTest/java/com/otaliastudios/cameraview/IntegrationTest.java
  2. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java
  3. 10
      cameraview/src/main/utils/com/otaliastudios/cameraview/WorkerHandler.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<TestActivity> 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);
}

@ -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

@ -65,7 +65,15 @@ class WorkerHandler {
return mThread;
}
public static void clearCache() {
public static void destroy() {
for (String key : sCache.keySet()) {
WeakReference<WorkerHandler> ref = sCache.get(key);
WorkerHandler handler = ref.get();
if (handler != null && handler.getThread().isAlive()) {
handler.getThread().interrupt();
}
ref.clear();
}
sCache.clear();
}
}

Loading…
Cancel
Save