Merge branch '509-tap-viewfinder-to-auto-focus' into 'maintenance-0.16'

Backport: Tap viewfinder to restart auto focus

See merge request akwizgran/briar!701
This commit is contained in:
akwizgran
2018-02-19 16:20:16 +00:00

View File

@@ -13,6 +13,7 @@ import android.util.AttributeSet;
import android.view.Surface; import android.view.Surface;
import android.view.SurfaceHolder; import android.view.SurfaceHolder;
import android.view.SurfaceView; import android.view.SurfaceView;
import android.view.View;
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault; import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault; import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
@@ -38,18 +39,21 @@ import static java.util.logging.Level.WARNING;
@MethodsNotNullByDefault @MethodsNotNullByDefault
@ParametersNotNullByDefault @ParametersNotNullByDefault
public class CameraView extends SurfaceView implements SurfaceHolder.Callback, public class CameraView extends SurfaceView implements SurfaceHolder.Callback,
AutoFocusCallback { AutoFocusCallback, View.OnClickListener {
private static final int AUTO_FOCUS_RETRY_DELAY = 5000; // Milliseconds private static final int AUTO_FOCUS_RETRY_DELAY = 5000; // Milliseconds
private static final Logger LOG = private static final Logger LOG =
Logger.getLogger(CameraView.class.getName()); Logger.getLogger(CameraView.class.getName());
private final Runnable autoFocusRetry = this::retryAutoFocus;
@Nullable @Nullable
private Camera camera = null; private Camera camera = null;
private PreviewConsumer previewConsumer = null; private PreviewConsumer previewConsumer = null;
private Surface surface = null; private Surface surface = null;
private int displayOrientation = 0, surfaceWidth = 0, surfaceHeight = 0; private int displayOrientation = 0, surfaceWidth = 0, surfaceHeight = 0;
private boolean previewStarted = false, autoFocus = false; private boolean previewStarted = false;
private boolean autoFocusSupported = false, autoFocusRunning = false;
public CameraView(Context context) { public CameraView(Context context) {
super(context); super(context);
@@ -74,6 +78,7 @@ public class CameraView extends SurfaceView implements SurfaceHolder.Callback,
super.onAttachedToWindow(); super.onAttachedToWindow();
setKeepScreenOn(true); setKeepScreenOn(true);
getHolder().addCallback(this); getHolder().addCallback(this);
setOnClickListener(this);
} }
@Override @Override
@@ -157,27 +162,41 @@ public class CameraView extends SurfaceView implements SurfaceHolder.Callback,
@UiThread @UiThread
private void startConsumer() throws CameraException { private void startConsumer() throws CameraException {
if (camera == null) throw new CameraException("Camera is null"); if (camera == null) throw new CameraException("Camera is null");
if (autoFocus) { startAutoFocus();
previewConsumer.start(camera);
}
@UiThread
private void startAutoFocus() throws CameraException {
if (camera != null && autoFocusSupported && !autoFocusRunning) {
try { try {
removeCallbacks(autoFocusRetry);
camera.autoFocus(this); camera.autoFocus(this);
autoFocusRunning = true;
} catch (RuntimeException e) { } catch (RuntimeException e) {
throw new CameraException(e); throw new CameraException(e);
} }
} }
previewConsumer.start(camera);
} }
@UiThread @UiThread
private void stopConsumer() throws CameraException { private void stopConsumer() throws CameraException {
if (camera == null) throw new CameraException("Camera is null"); if (camera == null) throw new CameraException("Camera is null");
if (autoFocus) { cancelAutoFocus();
previewConsumer.stop();
}
@UiThread
private void cancelAutoFocus() throws CameraException {
if (camera != null && autoFocusSupported && autoFocusRunning) {
try { try {
removeCallbacks(autoFocusRetry);
camera.cancelAutoFocus(); camera.cancelAutoFocus();
autoFocusRunning = false;
} catch (RuntimeException e) { } catch (RuntimeException e) {
throw new CameraException(e); throw new CameraException(e);
} }
} }
previewConsumer.stop();
} }
@UiThread @UiThread
@@ -325,7 +344,7 @@ public class CameraView extends SurfaceView implements SurfaceHolder.Callback,
@UiThread @UiThread
private void enableAutoFocus(String focusMode) { private void enableAutoFocus(String focusMode) {
autoFocus = FOCUS_MODE_AUTO.equals(focusMode) || autoFocusSupported = FOCUS_MODE_AUTO.equals(focusMode) ||
FOCUS_MODE_MACRO.equals(focusMode); FOCUS_MODE_MACRO.equals(focusMode);
} }
@@ -427,16 +446,23 @@ public class CameraView extends SurfaceView implements SurfaceHolder.Callback,
@Override @Override
public void onAutoFocus(boolean success, Camera camera) { public void onAutoFocus(boolean success, Camera camera) {
LOG.info("Auto focus succeeded: " + success); if (LOG.isLoggable(INFO))
postDelayed(this::retryAutoFocus, AUTO_FOCUS_RETRY_DELAY); LOG.info("Auto focus succeeded: " + success);
autoFocusRunning = false;
postDelayed(autoFocusRetry, AUTO_FOCUS_RETRY_DELAY);
} }
@UiThread @UiThread
private void retryAutoFocus() { private void retryAutoFocus() {
try { try {
if (camera != null) camera.autoFocus(this); startAutoFocus();
} catch (RuntimeException e) { } catch (CameraException e) {
LOG.log(WARNING, "Error retrying auto focus", e); LOG.log(WARNING, e.toString(), e);
} }
} }
@Override
public void onClick(View v) {
retryAutoFocus();
}
} }