@@ -437,32 +437,39 @@ public static boolean checkIfUserHasOptedIn(Context context) {
437
437
.getBoolean (USER_OPTED_IN_KEY , false );
438
438
}
439
439
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
+
440
464
/**
441
465
* Delete the cached results from the UrlDeviceDisoveryService.
442
466
* @param context The context for the service.
443
467
*/
444
468
public static void deleteCache (Context context ) {
445
- new ServiceConnection () {
446
- private Context mContext ;
447
-
469
+ new UrlDeviceDiscoveryServiceConnection () {
448
470
@ 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 ) {
453
472
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 );
466
473
}
467
474
}.connect (context );
468
475
}
@@ -472,27 +479,10 @@ public void connect(Context context) {
472
479
* @param context The context for the service.
473
480
*/
474
481
public static void startScan (Context context ) {
475
- new ServiceConnection () {
476
- private Context mContext ;
477
-
482
+ new UrlDeviceDiscoveryServiceConnection () {
478
483
@ 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 ) {
483
485
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 );
496
486
}
497
487
}.connect (context );
498
488
}
0 commit comments