From 2a4997d43a8a750efd1a1e9f46e21657ded136d1 Mon Sep 17 00:00:00 2001 From: Antoine Gonzalez Date: Wed, 17 Apr 2024 16:17:16 +0200 Subject: [PATCH] Fix/Optimize ZebraDwScanner lifecycle and intent handling (#173) --- .../scanner/sdk/zebra/dw/ZebraDwScanner.java | 26 ++++++++++++++----- .../sdk/zebra/dw/ZebraDwSymbology.java | 4 ++- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/enioka_scan_zebra_dw/src/main/java/com/enioka/scanner/sdk/zebra/dw/ZebraDwScanner.java b/enioka_scan_zebra_dw/src/main/java/com/enioka/scanner/sdk/zebra/dw/ZebraDwScanner.java index ccdde1fd..57399f24 100644 --- a/enioka_scan_zebra_dw/src/main/java/com/enioka/scanner/sdk/zebra/dw/ZebraDwScanner.java +++ b/enioka_scan_zebra_dw/src/main/java/com/enioka/scanner/sdk/zebra/dw/ZebraDwScanner.java @@ -33,6 +33,10 @@ public class ZebraDwScanner extends IntentScanner implements Scanner.Wit @Override public void onReceive(Context context, Intent intent) { + // Avoid re-config loops in case another enioka_scan instance is running in the foreground + if (paused) + return; + Log.d(LOG_TAG, "Received data from DW service"); // Barcode? @@ -41,6 +45,7 @@ public void onReceive(Context context, Intent intent) { List barcodes = new ArrayList<>(); barcodes.add(new Barcode(barcodeData, ZebraDwSymbology.getSymbology(intent.getStringExtra(ZebraDwIntents.DW_BARCODE_TYPE_EXTRA)).type)); + Log.d(LOG_TAG, "Received scan result: " + barcodes.get(0).getBarcode() + " (" + barcodes.get(0).getBarcodeType().toString() + ")"); if (dataCb != null) { dataCb.onData(this, barcodes); } @@ -113,10 +118,12 @@ public void onReceive(Context context, Intent intent) { Log.i(LOG_TAG, "PROFILE_SWITCH: profileName: " + b.getString("PROFILE_NAME") + ", profileEnabled: " + b.getBoolean("PROFILE_ENABLED")); currentlyActiveProfile = b.getString("PROFILE_NAME"); if (currentConfig != null && profileName.equals(currentlyActiveProfile)) { + Log.d(LOG_TAG, "Correct profile, configuring symbologies"); configureSymbologies(); } if (!paused && !profileName.equals(currentlyActiveProfile)) { // DW will change the profile between activities and apps. We must check each time a switch is done. + Log.d(LOG_TAG, "Incorrect profile " + currentlyActiveProfile + ", requesting profile change to " + profileName); switchToOurProfile(); } break; @@ -128,6 +135,10 @@ public void onReceive(Context context, Intent intent) { case ZebraDwIntents.DW_NOTIFICATION_CHANGE_WORKFLOW: Log.d(LOG_TAG, "WORKFLOW_STATUS: status: " + b.getString("STATUS") + ", profileName: " + b.getString("PROFILE_NAME")); break; + + default: + Log.d(LOG_TAG, "Other notification: " + notificationType + " - status: " + b.getString("STATUS") + ", profileName: " + b.getString("PROFILE_NAME")); + break; } } } @@ -252,8 +263,10 @@ private void queryActiveProfile() { if (currentlyActiveProfile.equals(profileName)) { // No need to create, just reconfigure + Log.d(LOG_TAG, "Correct profile, configuring profile"); configureProfile(); } else { + Log.d(LOG_TAG, "Incorrect profile " + currentlyActiveProfile + ", requesting profile change to " + profileName); queryProfileList(); } } @@ -343,7 +356,7 @@ private void configureSymbologies() { } private void switchToOurProfile() { - if (this.currentlyActiveProfile == null || this.currentlyActiveProfile.equals(this.profileName)) { + if (this.currentlyActiveProfile != null && this.currentlyActiveProfile.equals(this.profileName)) { Log.d(LOG_TAG, "Not switching profile - already active"); return; } @@ -419,12 +432,14 @@ protected void configureProvider(final Context applicationContext) { broadcastIntentFilters.add(ZebraDwIntents.DW_CONFIGURATION_CALLBACK_ACTION); broadcastIntentFilters.add(ZebraDwIntents.DW_NOTIFICATION_ACTION); - disableScanner = newIntent(ZebraDwIntents.DW_API_MAIN_ACTION, "com.symbol.datawedge.api.ENABLE_DATAWEDGE", false); - enableScanner = newIntent(ZebraDwIntents.DW_API_MAIN_ACTION, "com.symbol.datawedge.api.ENABLE_DATAWEDGE", true); + disableScanner = newIntent(ZebraDwIntents.DW_API_MAIN_ACTION, "com.symbol.datawedge.api.SCANNER_INPUT_PLUGIN", "DISABLE_PLUGIN"); + enableScanner = newIntent(ZebraDwIntents.DW_API_MAIN_ACTION, "com.symbol.datawedge.api.SCANNER_INPUT_PLUGIN", "ENABLE_PLUGIN"); } @Override protected void configureAfterInit(Context ctx) { + // Enable datawedge just in case. + broadcastIntent(newIntent(ZebraDwIntents.DW_API_MAIN_ACTION, "com.symbol.datawedge.api.ENABLE_DATAWEDGE", true)); super.configureAfterInit(ctx); // We want to know what is happening on the device, at least for logs @@ -433,13 +448,12 @@ protected void configureAfterInit(Context ctx) { // Create profile and configure it. // (this triggers a callback chain) queryActiveProfile(); - - // Enable datawedge just in case. - resume(); } @Override public void resume(@Nullable ScannerCommandCallbackProxy cb) { + // Enable datawedge just in case. + broadcastIntent(newIntent(ZebraDwIntents.DW_API_MAIN_ACTION, "com.symbol.datawedge.api.ENABLE_DATAWEDGE", true)); super.resume(cb); switchToOurProfile(); } diff --git a/enioka_scan_zebra_dw/src/main/java/com/enioka/scanner/sdk/zebra/dw/ZebraDwSymbology.java b/enioka_scan_zebra_dw/src/main/java/com/enioka/scanner/sdk/zebra/dw/ZebraDwSymbology.java index 0636a22e..4ee211e8 100644 --- a/enioka_scan_zebra_dw/src/main/java/com/enioka/scanner/sdk/zebra/dw/ZebraDwSymbology.java +++ b/enioka_scan_zebra_dw/src/main/java/com/enioka/scanner/sdk/zebra/dw/ZebraDwSymbology.java @@ -7,11 +7,13 @@ */ enum ZebraDwSymbology { // Items supported by the lib - CODE39("LABEL-TYPE-CODE39", "decoder_code39", BarcodeType.CODE39), CODE128("LABEL-TYPE-CODE128", "decoder_code128", BarcodeType.CODE128), + CODE39("LABEL-TYPE-CODE39", "decoder_code39", BarcodeType.CODE39), DIS25("LABEL-TYPE-D2OF5", "decoder_d2of5", BarcodeType.DIS25), INT25("LABEL-TYPE-I2OF5", "decoder_i2of5", BarcodeType.INT25), EAN13("LABEL-TYPE-EAN13", "decoder_ean13", BarcodeType.EAN13), + QRCODE("LABEL-TYPE-QRCODE", "decoder_qrcode", BarcodeType.QRCODE), + AZTEC("LABEL-TYPE-AZTEC", "decoder_aztec", BarcodeType.AZTEC), UNKNOWN("none", "none", BarcodeType.UNKNOWN); // Items not supported by the lib. Present because we need to be able to disable them.