Utilize debouncer instead of animator timeout for video capture end time.

fork-5.53.8
Alex Hart 2022-07-05 12:36:33 -03:00
rodzic 6aa4706e9b
commit 8f85b58612
1 zmienionych plików z 19 dodań i 34 usunięć

Wyświetl plik

@ -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 com.bumptech.glide.util.Executors;
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 @@ class CameraXVideoCaptureHelper implements CameraButtonView.VideoCaptureListener
@Override
public void onError(int videoCaptureError, @NonNull String message, @Nullable Throwable cause) {
isRecording = false;
debouncer.clear();
callback.onVideoError(cause);
}
};
@ -71,23 +75,18 @@ class CameraXVideoCaptureHelper implements CameraButtonView.VideoCaptureListener
@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 @@ class CameraXVideoCaptureHelper implements CameraButtonView.VideoCaptureListener
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 @@ class CameraXVideoCaptureHelper implements CameraButtonView.VideoCaptureListener
}
updateProgressAnimator.cancel();
debouncer.clear();
}
@Override
@ -206,27 +207,11 @@ class CameraXVideoCaptureHelper implements CameraButtonView.VideoCaptureListener
);
}
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);
}
}