Skip to content

Commit

Permalink
Fix/Optimize ZebraDwScanner lifecycle and intent handling (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaSpood authored Apr 17, 2024
1 parent 0e142e3 commit 2a4997d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public class ZebraDwScanner extends IntentScanner<String> 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?
Expand All @@ -41,6 +45,7 @@ public void onReceive(Context context, Intent intent) {
List<Barcode> 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);
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}
}
Expand Down Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 2a4997d

Please sign in to comment.