diff --git a/app/src/main/java/net/sourceforge/opencamera/preview/Preview.java b/app/src/main/java/net/sourceforge/opencamera/preview/Preview.java index 224cd7f7e227f4bee081f461f5a7c11bd0ce23c6..46a38360318b33ae954a0a93ff5e17f1b9adfb4a 100644 --- a/app/src/main/java/net/sourceforge/opencamera/preview/Preview.java +++ b/app/src/main/java/net/sourceforge/opencamera/preview/Preview.java @@ -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; @@ -444,6 +445,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"); @@ -1120,19 +1124,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); - } - } else if (!previewBitmapWasEnabled) { + if (!previewBitmapWasEnabled) { + enablePreviewBitmap(); + } + + TextureView textureView = (TextureView) this.cameraSurface; + Bitmap bitmap = textureView.getBitmap(preview_bitmap); + int rotation = getDisplayRotationDegrees(false); + if (bitmap != null) { + qrImageAnalyzer.readImage(bitmap, rotation); + } + + if (!previewBitmapWasEnabled) { disablePreviewBitmap(); } } diff --git a/app/src/main/java/net/sourceforge/opencamera/qr/QrImageAnalyzer.kt b/app/src/main/java/net/sourceforge/opencamera/qr/QrImageAnalyzer.kt index 0b475dc310a048772ff59c058f892bde679648f3..1d95a9d1d8d5c0b429084678b599c68ca2ccef14 100644 --- a/app/src/main/java/net/sourceforge/opencamera/qr/QrImageAnalyzer.kt +++ b/app/src/main/java/net/sourceforge/opencamera/qr/QrImageAnalyzer.kt @@ -18,6 +18,8 @@ import android.graphics.Rect import android.os.Build import android.text.method.LinkMovementMethod import android.util.Log +import android.view.ContextThemeWrapper +import android.view.LayoutInflater import android.view.textclassifier.TextClassificationManager import android.widget.ImageButton import android.widget.ImageView @@ -83,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 { @@ -130,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 @@ -267,16 +278,20 @@ class QrImageAnalyzer(private val activity: Activity, private val scope: Corouti bottomSheetDialog.setOnDismissListener { activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED } + isAboutToShow = false } } - private fun inflateButton() = MaterialButton::class.cast( - activity.layoutInflater.inflate( - R.layout.qr_bottom_sheet_action_button, - bottomSheetDialogActionsLayout, - false - ) - ).apply { - layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT) + private fun inflateButton(): MaterialButton { + val materialContext = ContextThemeWrapper(activity, R.style.Theme_MaterialComponents_Light_Dialog) + return MaterialButton::class.cast( + LayoutInflater.from(materialContext).inflate( + R.layout.qr_bottom_sheet_action_button, + bottomSheetDialogActionsLayout, + false + ) + ).apply { + layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT) + } } } diff --git a/app/src/main/res/layout/qr_bottom_sheet_action_button.xml b/app/src/main/res/layout/qr_bottom_sheet_action_button.xml index 6154163e10d0237aeb4bc7343f77a0588aacf37b..98928137c4961eed4a33385fb34cd0aa35199bab 100644 --- a/app/src/main/res/layout/qr_bottom_sheet_action_button.xml +++ b/app/src/main/res/layout/qr_bottom_sheet_action_button.xml @@ -11,10 +11,10 @@ android:letterSpacing="0.0" android:textAlignment="center" android:textAllCaps="false" - android:textColor="@color/qrcodeColorOnSurface" + android:textColor="@color/e_primary_text_color" android:textSize="15sp" android:textStyle="normal" android:typeface="normal" app:cornerRadius="8dp" - app:strokeColor="@color/qrcodeColorOnSurfaceVariant" + app:strokeColor="@color/e_accent" app:strokeWidth="1dp" /> diff --git a/app/src/main/res/layout/qr_bottom_sheet_dialog.xml b/app/src/main/res/layout/qr_bottom_sheet_dialog.xml index 8ee7345a60227fba4c315cc89a13ad4f14b8f2db..5e117530a987e3fee6fd222229924e53c43ae711 100644 --- a/app/src/main/res/layout/qr_bottom_sheet_dialog.xml +++ b/app/src/main/res/layout/qr_bottom_sheet_dialog.xml @@ -8,6 +8,7 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" + android:background="@color/e_floating_background" android:paddingHorizontal="16dp">