Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8a2e576b authored by Mohammed Althaf T's avatar Mohammed Althaf T 😊 Committed by Nishith Khanna
Browse files

camera: Fix duplicate QR actions

parent e8c4445f
Loading
Loading
Loading
Loading
+29 −12
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import net.sourceforge.opencamera.preview.ApplicationInterface.NoFreeStorageExce
import net.sourceforge.opencamera.preview.camerasurface.CameraSurface;
import net.sourceforge.opencamera.preview.camerasurface.MySurfaceView;
import net.sourceforge.opencamera.preview.camerasurface.MyTextureView;
import net.sourceforge.opencamera.qr.QrImageAnalyzer;

import java.io.File;
//import java.io.FileOutputStream;
@@ -455,6 +456,9 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
    public volatile boolean test_runtime_on_video_stop; // force throwing a RuntimeException when stopping video (this usually happens naturally when stopping video too soon)
    public volatile boolean test_burst_resolution;

    private static final long EXECUTION_DELAY_IN_MS = 500; // Execution delay
    private long lastExecutionTime = 0; // Track the last execution time

    public Preview(ApplicationInterface applicationInterface, ViewGroup parent) {
        if( MyDebug.LOG ) {
            Log.d(TAG, "new Preview");
@@ -1157,19 +1161,32 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
    public void onSurfaceTextureUpdated(@NonNull SurfaceTexture arg0) {
        refreshPreviewBitmap();

        if (!isQRCode()) return;

        MainActivity mainActivity = (MainActivity) this.getContext();
        QrImageAnalyzer qrImageAnalyzer = mainActivity.qrImageAnalyzer;

        // Check if the function is being called too quickly
        if (qrImageAnalyzer.isQrDialogShowing()
                || (System.currentTimeMillis() - lastExecutionTime) < EXECUTION_DELAY_IN_MS) {
            return; // Skip below code if it's within executionDelay
        }

        lastExecutionTime = System.currentTimeMillis();

        boolean previewBitmapWasEnabled = isPreviewBitmapEnabled();
        if (isQRCode()) {
        if (!previewBitmapWasEnabled) {
            enablePreviewBitmap();
        }

        TextureView textureView = (TextureView) this.cameraSurface;
        Bitmap bitmap = textureView.getBitmap(preview_bitmap);
            MainActivity mActivity = (MainActivity) this.getContext();
        int rotation = getDisplayRotationDegrees(false);
        if (bitmap != null) {
                mActivity.qrImageAnalyzer.readImage(bitmap, rotation);
            qrImageAnalyzer.readImage(bitmap, rotation);
        }
        } else if (!previewBitmapWasEnabled) {

        if (!previewBitmapWasEnabled) {
            disablePreviewBitmap();
        }
    }
+11 −1
Original line number Diff line number Diff line
@@ -85,6 +85,8 @@ class QrImageAnalyzer(private val activity: Activity, private val scope: Corouti
        activity.getSystemService(TextClassificationManager::class.java)
    }

    private var isAboutToShow = false

    // QR
    private val reader by lazy {
        BarcodeReader().apply {
@@ -132,12 +134,19 @@ class QrImageAnalyzer(private val activity: Activity, private val scope: Corouti
        } catch (ignored: Exception) { }
    }

    fun isQrDialogShowing(): Boolean {
        return bottomSheetDialog.isShowing || isAboutToShow
    }

    private fun showQrDialog(result: BarcodeReader.Result) {
        scope.launch(Dispatchers.Main) {
            if (bottomSheetDialog.isShowing) {
            if (isQrDialogShowing()) {
                return@launch
            }

            // showQrDialog is getting called even before its about to show.
            isAboutToShow = true

            val text = result.text ?: return@launch
            bottomSheetDialogData.text = text

@@ -269,6 +278,7 @@ class QrImageAnalyzer(private val activity: Activity, private val scope: Corouti
            bottomSheetDialog.setOnDismissListener {
                activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
            }
            isAboutToShow = false
        }
    }