From d1b93018e329c2ada95075a533c2a4c0245f1cce Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Tue, 4 Jun 2024 18:10:14 +0200 Subject: [PATCH] Show generic zxing errors on the camera error screen --- .../card_locker/CatimaCaptureManager.java | 12 +++++----- .../protect/card_locker/ScanActivity.java | 22 ++++++++++++++----- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/protect/card_locker/CatimaCaptureManager.java b/app/src/main/java/protect/card_locker/CatimaCaptureManager.java index 7507d8090..e083c48ff 100644 --- a/app/src/main/java/protect/card_locker/CatimaCaptureManager.java +++ b/app/src/main/java/protect/card_locker/CatimaCaptureManager.java @@ -4,22 +4,24 @@ import android.app.Activity; import android.content.Context; import android.widget.Toast; +import androidx.core.util.Consumer; + import com.journeyapps.barcodescanner.CaptureManager; import com.journeyapps.barcodescanner.DecoratedBarcodeView; public class CatimaCaptureManager extends CaptureManager { - private final Context mContext; + private final Consumer mErrorCallback; - public CatimaCaptureManager(Activity activity, DecoratedBarcodeView barcodeView) { + public CatimaCaptureManager(Activity activity, DecoratedBarcodeView barcodeView, Consumer errorCallback) { super(activity, barcodeView); - mContext = activity.getApplicationContext(); + mErrorCallback = errorCallback; } @Override protected void displayFrameworkBugMessageAndExit(String message) { // We don't want to exit, as we also have a enter from card image and add manually button here - // So we show a toast instead - Toast.makeText(mContext, message, Toast.LENGTH_LONG).show(); + // So, instead, we call our error callback + mErrorCallback.accept(message); } } diff --git a/app/src/main/java/protect/card_locker/ScanActivity.java b/app/src/main/java/protect/card_locker/ScanActivity.java index 977439ad2..ae24ac7ad 100644 --- a/app/src/main/java/protect/card_locker/ScanActivity.java +++ b/app/src/main/java/protect/card_locker/ScanActivity.java @@ -78,6 +78,7 @@ public class ScanActivity extends CatimaAppCompatActivity { static final String STATE_SCANNER_ACTIVE = "scannerActive"; private boolean mScannerActive = true; + private boolean mHasError = false; private void extractIntentFields(Intent intent) { final Bundle b = intent.getExtras(); @@ -141,7 +142,7 @@ public class ScanActivity extends CatimaAppCompatActivity { // Even though we do the actual decoding with the barcodeScannerView // CaptureManager needs to be running to show the camera and scanning bar - capture = new CatimaCaptureManager(this, barcodeScannerView); + capture = new CatimaCaptureManager(this, barcodeScannerView, this::onCaptureManagerError); Intent captureIntent = new Intent(); Bundle captureIntentBundle = new Bundle(); captureIntentBundle.putBoolean(Intents.Scan.BEEP_ENABLED, false); @@ -179,7 +180,7 @@ public class ScanActivity extends CatimaAppCompatActivity { } if (!Utils.deviceHasCamera(this)) { - showCameraError(R.string.noCameraFoundGuideText, false); + showCameraError(getString(R.string.noCameraFoundGuideText), false); } else if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { showCameraPermissionMissingText(); } else { @@ -407,11 +408,20 @@ public class ScanActivity extends CatimaAppCompatActivity { } } - private void showCameraPermissionMissingText() { - showCameraError(R.string.noCameraPermissionDirectToSystemSetting, true); + public void onCaptureManagerError(String errorMessage) { + if (mHasError) { + // We're already showing an error, ignore this new error + return; + } + + showCameraError(errorMessage, false); } - private void showCameraError(int message, boolean setOnClick) { + private void showCameraPermissionMissingText() { + showCameraError(getString(R.string.noCameraPermissionDirectToSystemSetting), true); + } + + private void showCameraError(String message, boolean setOnClick) { customBarcodeScannerBinding.cameraPermissionDeniedLayout.cameraPermissionDeniedMessage.setText(message); setCameraErrorState(true, setOnClick); @@ -422,6 +432,8 @@ public class ScanActivity extends CatimaAppCompatActivity { } private void setCameraErrorState(boolean visible, boolean setOnClick) { + mHasError = visible; + customBarcodeScannerBinding.cameraPermissionDeniedLayout.cameraPermissionDeniedClickableArea.setOnClickListener(visible && setOnClick ? v -> { navigateToSystemPermissionSetting(); } : null);