From 15b7c52b660baea8c1a5b1f6f3c2e3c56a8a0179 Mon Sep 17 00:00:00 2001 From: jacques Date: Fri, 11 Mar 2022 10:14:28 +0100 Subject: [PATCH] Add more cancel case in scan return --- .../foucry/pilldroid/DrugListActivity.java | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java b/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java index a19e48f..e2672cd 100644 --- a/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java @@ -296,6 +296,30 @@ public class DrugListActivity extends AppCompatActivity { super.onResume(); } + /** Register the launcher and result handler + * ActivityResultLauncher + */ + private final ActivityResultLauncher barcodeLauncher = registerForActivityResult(new ScanContract(), + result -> { + if(result.getContents() == null) { + Intent originalIntent = result.getOriginalIntent(); + if (originalIntent == null { + Log.d(TAG, "Cancelled Scan"); + Toast.makeText(DrugListActivity.this, "Cancelled", Toast.LENGTH_LONG).show(); + } else if (originalIntent.hasExtra(Intents.Scan.MISSING_CAMERA_PERMISSION)) { + Log.d(TAG, "Cancelled scan due missing camera permission"); + Toast.makeText(DrugListaActivity.this, "Cancelled due missing camera persmission", + Toast.LENGTH_LONG.show()); + } else if (originalIntent.hasExtra(Intents.Scan.TIMEOUT)) { + Log.d(TAG, "Cancelled due timeout"); + Toast,makeText(DrugListActivity.this, "Cancelled due timeout", Toast.LENGTH_LONG.show()); + } + } else { + Log.d(TAG, "Scanned"); + Toast.makeText(DrugListActivity.this, "Scanned: " + result.getContents(), + Toast.LENGTH_LONG).show(); + } + }); // Launch scan public void onButtonClick() { @@ -306,7 +330,7 @@ public class DrugListActivity extends AppCompatActivity { options.setCameraId(0); // Use a specific camera of the device options.setBeepEnabled(true); options.setBarcodeImageEnabled(true); - //options.setTimeout(3600); + options.setTimeout(60); options.setCaptureActivity(CustomScannerActivity.class); options.setBeepEnabled(true); options.addExtra(Intents.Scan.SCAN_TYPE, Intents.Scan.MIXED_SCAN);