From 4ba9e4f45ffb8764a86cae329062ea4316adfd6b Mon Sep 17 00:00:00 2001 From: VincentKobz Date: Mon, 27 May 2024 14:00:58 +0200 Subject: [PATCH] refactor: Use ScannerSearchOptions instead of explicit intent --- .../enioka/scanner/demo/WelcomeActivity.java | 8 +++---- .../service/ScannerServiceAndroidTest.java | 2 +- .../scanner/service/ScannerService.java | 22 ++++++------------- 3 files changed, 11 insertions(+), 21 deletions(-) diff --git a/demoscannerapp/src/main/java/com/enioka/scanner/demo/WelcomeActivity.java b/demoscannerapp/src/main/java/com/enioka/scanner/demo/WelcomeActivity.java index 8f0f63ea..393e7105 100644 --- a/demoscannerapp/src/main/java/com/enioka/scanner/demo/WelcomeActivity.java +++ b/demoscannerapp/src/main/java/com/enioka/scanner/demo/WelcomeActivity.java @@ -85,14 +85,12 @@ public void onClickBt1(View v) { options.allowIntentDevices = preferences.getBoolean(ScannerServiceApi.EXTRA_SEARCH_ALLOW_INTENT_BOOLEAN, options.allowIntentDevices); options.allowedProviderKeys = preferences.getStringSet(ScannerServiceApi.EXTRA_SEARCH_ALLOWED_PROVIDERS_STRING_ARRAY, options.allowedProviderKeys); options.excludedProviderKeys = preferences.getStringSet(ScannerServiceApi.EXTRA_SEARCH_EXCLUDED_PROVIDERS_STRING_ARRAY, options.excludedProviderKeys); + options.symbologySelection = preferences.getStringSet(ScannerServiceApi.EXTRA_SYMBOLOGY_SELECTION, ScannerService.defaultSymbologyByName()); options.toIntentExtras(intent); - // add symbology - final String[] symbologies = preferences.getStringSet(ScannerServiceApi.EXTRA_SYMBOLOGY_SELECTION, ScannerService.defaultSymbologyByName()).toArray(new String[0]); - intent.putExtra(ScannerServiceApi.EXTRA_SYMBOLOGY_SELECTION, symbologies); - // add logging intent extra + + // Add extra settings for scanner activity (logging and camera fallback) intent.putExtra(SettingsActivity.ENABLE_LOGGING_KEY, preferences.getBoolean(SettingsActivity.ENABLE_LOGGING_KEY, false)); - // add allow camera fallback intent extra intent.putExtra(SettingsActivity.ALLOW_CAMERA_FALLBACK_KEY, preferences.getBoolean(SettingsActivity.ALLOW_CAMERA_FALLBACK_KEY, false)); // add enable keep aspect ratio intent extra intent.putExtra(SettingsActivity.ENABLE_KEEP_ASPECT_RATIO_KEY, preferences.getBoolean(SettingsActivity.ENABLE_KEEP_ASPECT_RATIO_KEY, false) ? 1 : 0); diff --git a/enioka_scan/src/androidTest/java/com/enioka/scanner/service/ScannerServiceAndroidTest.java b/enioka_scan/src/androidTest/java/com/enioka/scanner/service/ScannerServiceAndroidTest.java index c3c529d7..baf3814a 100644 --- a/enioka_scan/src/androidTest/java/com/enioka/scanner/service/ScannerServiceAndroidTest.java +++ b/enioka_scan/src/androidTest/java/com/enioka/scanner/service/ScannerServiceAndroidTest.java @@ -63,10 +63,10 @@ public void testScannerSearchDelayedAfterBind() { options.useBlueTooth = false; options.allowedProviderKeys = new HashSet<>(); options.allowedProviderKeys.add(MockProvider.PROVIDER_KEY); + options.startSearchOnServiceBind = false; final Intent serviceIntent = new Intent(ctx, ScannerService.class); options.toIntentExtras(serviceIntent); - serviceIntent.putExtra(ScannerServiceApi.EXTRA_START_SEARCH_ON_SERVICE_BIND, false); ctx.bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE); // Wait for binding with ScannerService diff --git a/enioka_scan/src/main/java/com/enioka/scanner/service/ScannerService.java b/enioka_scan/src/main/java/com/enioka/scanner/service/ScannerService.java index 33d16c40..92f13617 100644 --- a/enioka_scan/src/main/java/com/enioka/scanner/service/ScannerService.java +++ b/enioka_scan/src/main/java/com/enioka/scanner/service/ScannerService.java @@ -38,7 +38,6 @@ public class ScannerService extends Service implements ScannerConnectionHandler, protected final static String LOG_TAG = "ScannerService"; - private boolean startScannerSearchOnFirstBind = true; private boolean firstBind = true; /** @@ -72,7 +71,7 @@ public class ScannerService extends Service implements ScannerConnectionHandler, private ScannerSearchOptions scannerSearchOptions = ScannerSearchOptions.defaultOptions(); /** - * Option to set wanted symbology. By default EAN13 and CODE128 is set + * Option to set wanted symbology. By default, all symbologies are enabled. */ private Set symbologySelection = defaultSymbology(); @@ -113,17 +112,10 @@ public IBinder onBind(Intent intent) { Log.i(LOG_TAG, "ScannerService is receiving a new bind request - onBind"); scannerSearchOptions.fromIntentExtras(intent); - final Bundle extras = intent.getExtras(); - if (extras != null) { - startScannerSearchOnFirstBind = extras.getBoolean(EXTRA_START_SEARCH_ON_SERVICE_BIND, startScannerSearchOnFirstBind); - - // FIXME enable all symbology if null ? - String[] symbologySelectionArray = extras.getStringArray(ScannerServiceApi.EXTRA_SYMBOLOGY_SELECTION); - if (symbologySelectionArray != null && symbologySelectionArray.length > 0) { - symbologySelection = new HashSet<>(); - for (String symbology : symbologySelectionArray) { - symbologySelection.add(BarcodeType.valueOf(symbology)); - } + if (scannerSearchOptions.symbologySelection != null) { + symbologySelection = new HashSet<>(); + for (String symbology : scannerSearchOptions.symbologySelection) { + symbologySelection.add(BarcodeType.valueOf(symbology)); } } @@ -167,7 +159,7 @@ protected synchronized void initProviderDiscovery() { Log.i(LOG_TAG, "(re)starting provider discovery!"); if (!LaserScanner.getProviderCache().isEmpty()) { Log.d(LOG_TAG, "Cached providers are used"); - if (firstBind && startScannerSearchOnFirstBind) { + if (firstBind && scannerSearchOptions.startSearchOnServiceBind) { this.initLaserScannerSearch(); } return; @@ -179,7 +171,7 @@ protected synchronized void initProviderDiscovery() { client.onProviderDiscoveryEnded(); } - if (firstBind && startScannerSearchOnFirstBind) { + if (firstBind && scannerSearchOptions.startSearchOnServiceBind) { this.initLaserScannerSearch(); } firstBind = false;