Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit 1f2c296

Browse files
committed
Add a service connection helper
Address issue 761
1 parent 80c8028 commit 1f2c296

File tree

1 file changed

+28
-38
lines changed
  • android/PhysicalWeb/app/src/main/java/org/physical_web/physicalweb

1 file changed

+28
-38
lines changed

android/PhysicalWeb/app/src/main/java/org/physical_web/physicalweb/Utils.java

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -437,32 +437,39 @@ public static boolean checkIfUserHasOptedIn(Context context) {
437437
.getBoolean(USER_OPTED_IN_KEY, false);
438438
}
439439

440+
public abstract static class UrlDeviceDiscoveryServiceConnection implements ServiceConnection {
441+
private Context mContext;
442+
443+
@Override
444+
public void onServiceConnected(ComponentName className, IBinder service) {
445+
// Forward the service to the implementing class
446+
serviceHandler((UrlDeviceDiscoveryService.LocalBinder) service);
447+
mContext.unbindService(this);
448+
}
449+
450+
@Override
451+
public void onServiceDisconnected(ComponentName className) {
452+
}
453+
454+
public void connect(Context context) {
455+
mContext = context;
456+
Intent intent = new Intent(mContext, UrlDeviceDiscoveryService.class);
457+
mContext.startService(intent);
458+
mContext.bindService(intent, this, Context.BIND_AUTO_CREATE);
459+
}
460+
461+
public abstract void serviceHandler(UrlDeviceDiscoveryService.LocalBinder localBinder);
462+
}
463+
440464
/**
441465
* Delete the cached results from the UrlDeviceDisoveryService.
442466
* @param context The context for the service.
443467
*/
444468
public static void deleteCache(Context context) {
445-
new ServiceConnection() {
446-
private Context mContext;
447-
469+
new UrlDeviceDiscoveryServiceConnection() {
448470
@Override
449-
public void onServiceConnected(ComponentName className, IBinder service) {
450-
// Get the service
451-
UrlDeviceDiscoveryService.LocalBinder localBinder =
452-
(UrlDeviceDiscoveryService.LocalBinder) service;
471+
public void serviceHandler(UrlDeviceDiscoveryService.LocalBinder localBinder) {
453472
localBinder.getServiceInstance().clearCache();
454-
mContext.unbindService(this);
455-
}
456-
457-
@Override
458-
public void onServiceDisconnected(ComponentName className) {
459-
}
460-
461-
public void connect(Context context) {
462-
mContext = context;
463-
Intent intent = new Intent(mContext, UrlDeviceDiscoveryService.class);
464-
mContext.startService(intent);
465-
mContext.bindService(intent, this, Context.BIND_AUTO_CREATE);
466473
}
467474
}.connect(context);
468475
}
@@ -472,27 +479,10 @@ public void connect(Context context) {
472479
* @param context The context for the service.
473480
*/
474481
public static void startScan(Context context) {
475-
new ServiceConnection() {
476-
private Context mContext;
477-
482+
new UrlDeviceDiscoveryServiceConnection() {
478483
@Override
479-
public void onServiceConnected(ComponentName className, IBinder service) {
480-
// Get the service
481-
UrlDeviceDiscoveryService.LocalBinder localBinder =
482-
(UrlDeviceDiscoveryService.LocalBinder) service;
484+
public void serviceHandler(UrlDeviceDiscoveryService.LocalBinder localBinder) {
483485
localBinder.getServiceInstance().restartScan();
484-
mContext.unbindService(this);
485-
}
486-
487-
@Override
488-
public void onServiceDisconnected(ComponentName className) {
489-
}
490-
491-
public void connect(Context context) {
492-
mContext = context;
493-
Intent intent = new Intent(mContext, UrlDeviceDiscoveryService.class);
494-
mContext.startService(intent);
495-
mContext.bindService(intent, this, Context.BIND_AUTO_CREATE);
496486
}
497487
}.connect(context);
498488
}

0 commit comments

Comments
 (0)