From 8f85b58612e60cd79f5729b73ae03d300ab65fdf Mon Sep 17 00:00:00 2001 From: Alex Hart Date: Tue, 5 Jul 2022 12:36:33 -0300 Subject: [PATCH] Utilize debouncer instead of animator timeout for video capture end time. --- .../mediasend/CameraXVideoCaptureHelper.java | 53 +++++++------------ 1 file changed, 19 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/mediasend/CameraXVideoCaptureHelper.java b/app/src/main/java/org/thoughtcrime/securesms/mediasend/CameraXVideoCaptureHelper.java index d017543dc45..9cdb7f663fd 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/mediasend/CameraXVideoCaptureHelper.java +++ b/app/src/main/java/org/thoughtcrime/securesms/mediasend/CameraXVideoCaptureHelper.java @@ -1,7 +1,6 @@ package org.thoughtcrime.securesms.mediasend; import android.Manifest; -import android.animation.Animator; import android.animation.ValueAnimator; import android.annotation.SuppressLint; import android.content.Context; @@ -23,11 +22,13 @@ import org.signal.core.util.logging.Log; import org.thoughtcrime.securesms.R; import org.thoughtcrime.securesms.permissions.Permissions; +import org.thoughtcrime.securesms.util.Debouncer; import org.thoughtcrime.securesms.util.MemoryFileDescriptor; import org.thoughtcrime.securesms.video.VideoUtil; import java.io.FileDescriptor; import java.io.IOException; +import java.util.concurrent.TimeUnit; @RequiresApi(26) class CameraXVideoCaptureHelper implements CameraButtonView.VideoCaptureListener { @@ -41,9 +42,10 @@ class CameraXVideoCaptureHelper implements CameraButtonView.VideoCaptureListener private final @NonNull Callback callback; private final @NonNull MemoryFileDescriptor memoryFileDescriptor; private final @NonNull ValueAnimator updateProgressAnimator; + private final @NonNull Debouncer debouncer; - private boolean isRecording; - private ValueAnimator cameraMetricsAnimator; + private boolean isRecording; + private ValueAnimator cameraMetricsAnimator; private final VideoCapture.OnVideoSavedCallback videoSavedListener = new VideoCapture.OnVideoSavedCallback() { @SuppressLint("RestrictedApi") @@ -51,6 +53,7 @@ class CameraXVideoCaptureHelper implements CameraButtonView.VideoCaptureListener public void onVideoSaved(@NonNull VideoCapture.OutputFileResults outputFileResults) { try { isRecording = false; + debouncer.clear(); camera.setZoomRatio(camera.getMinZoomRatio()); memoryFileDescriptor.seek(0); callback.onVideoSaved(memoryFileDescriptor.getFileDescriptor()); @@ -63,6 +66,7 @@ public void onVideoSaved(@NonNull VideoCapture.OutputFileResults outputFileResul @Override public void onError(int videoCaptureError, @NonNull String message, @Nullable Throwable cause) { isRecording = false; + debouncer.clear(); callback.onVideoError(cause); } }; @@ -71,23 +75,18 @@ public void onError(int videoCaptureError, @NonNull String message, @Nullable Th @NonNull CameraButtonView captureButton, @NonNull SignalCameraView camera, @NonNull MemoryFileDescriptor memoryFileDescriptor, - int maxVideoDurationSec, + int maxVideoDurationSec, @NonNull Callback callback) { this.fragment = fragment; this.camera = camera; this.memoryFileDescriptor = memoryFileDescriptor; this.callback = callback; - this.updateProgressAnimator = ValueAnimator.ofFloat(0f, 1f).setDuration(maxVideoDurationSec * 1000); + this.updateProgressAnimator = ValueAnimator.ofFloat(0f, 1f).setDuration(TimeUnit.SECONDS.toMillis(maxVideoDurationSec)); + this.debouncer = new Debouncer(TimeUnit.SECONDS.toMillis(maxVideoDurationSec)); updateProgressAnimator.setInterpolator(new LinearInterpolator()); updateProgressAnimator.addUpdateListener(anim -> captureButton.setProgress(anim.getAnimatedFraction())); - updateProgressAnimator.addListener(new AnimationEndCallback() { - @Override - public void onAnimationEnd(Animator animation) { - if (isRecording) onVideoCaptureComplete(); - } - }); } @Override @@ -126,14 +125,15 @@ private void beginCameraRecording() { camera.startRecording(options, Executors.mainThreadExecutor(), videoSavedListener); updateProgressAnimator.start(); + debouncer.publish(this::onVideoCaptureComplete); } private void shrinkCaptureArea() { - Size screenSize = getScreenSize(); - Size videoRecordingSize = VideoUtil.getVideoRecordingSize(); - float scale = getSurfaceScaleForRecording(); - float targetWidthForAnimation = videoRecordingSize.getWidth() * scale; - float scaleX = targetWidthForAnimation / screenSize.getWidth(); + Size screenSize = getScreenSize(); + Size videoRecordingSize = VideoUtil.getVideoRecordingSize(); + float scale = getSurfaceScaleForRecording(); + float targetWidthForAnimation = videoRecordingSize.getWidth() * scale; + float scaleX = targetWidthForAnimation / screenSize.getWidth(); if (scaleX == 1f) { float targetHeightForAnimation = videoRecordingSize.getHeight() * scale; @@ -190,6 +190,7 @@ public void onVideoCaptureComplete() { } updateProgressAnimator.cancel(); + debouncer.clear(); } @Override @@ -206,27 +207,11 @@ static MemoryFileDescriptor createFileDescriptor(@NonNull Context context) throw ); } - private static abstract class AnimationEndCallback implements Animator.AnimatorListener { - - @Override - public final void onAnimationStart(Animator animation) { - - } - - @Override - public final void onAnimationCancel(Animator animation) { - - } - - @Override - public final void onAnimationRepeat(Animator animation) { - - } - } - interface Callback { void onVideoRecordStarted(); + void onVideoSaved(@NonNull FileDescriptor fd); + void onVideoError(@Nullable Throwable cause); } }