Skip to content

Commit

Permalink
refactor: Use ScannerSearchOptions instead of explicit intent
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentKobz committed May 30, 2024
1 parent 9592c26 commit 4ba9e4f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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<BarcodeType> symbologySelection = defaultSymbology();

Expand Down Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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;
Expand All @@ -179,7 +171,7 @@ protected synchronized void initProviderDiscovery() {
client.onProviderDiscoveryEnded();
}

if (firstBind && startScannerSearchOnFirstBind) {
if (firstBind && scannerSearchOptions.startSearchOnServiceBind) {
this.initLaserScannerSearch();
}
firstBind = false;
Expand Down

0 comments on commit 4ba9e4f

Please sign in to comment.