Skip to content

Commit

Permalink
Add a service connection helper
Browse files Browse the repository at this point in the history
Address issue 761
  • Loading branch information
hayesjordan committed Sep 28, 2016
1 parent 80c8028 commit 1f2c296
Showing 1 changed file with 28 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -437,32 +437,39 @@ public static boolean checkIfUserHasOptedIn(Context context) {
.getBoolean(USER_OPTED_IN_KEY, false);
}

public abstract static class UrlDeviceDiscoveryServiceConnection implements ServiceConnection {
private Context mContext;

@Override
public void onServiceConnected(ComponentName className, IBinder service) {
// Forward the service to the implementing class
serviceHandler((UrlDeviceDiscoveryService.LocalBinder) service);
mContext.unbindService(this);
}

@Override
public void onServiceDisconnected(ComponentName className) {
}

public void connect(Context context) {
mContext = context;
Intent intent = new Intent(mContext, UrlDeviceDiscoveryService.class);
mContext.startService(intent);
mContext.bindService(intent, this, Context.BIND_AUTO_CREATE);
}

public abstract void serviceHandler(UrlDeviceDiscoveryService.LocalBinder localBinder);
}

/**
* Delete the cached results from the UrlDeviceDisoveryService.
* @param context The context for the service.
*/
public static void deleteCache(Context context) {
new ServiceConnection() {
private Context mContext;

new UrlDeviceDiscoveryServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
// Get the service
UrlDeviceDiscoveryService.LocalBinder localBinder =
(UrlDeviceDiscoveryService.LocalBinder) service;
public void serviceHandler(UrlDeviceDiscoveryService.LocalBinder localBinder) {
localBinder.getServiceInstance().clearCache();
mContext.unbindService(this);
}

@Override
public void onServiceDisconnected(ComponentName className) {
}

public void connect(Context context) {
mContext = context;
Intent intent = new Intent(mContext, UrlDeviceDiscoveryService.class);
mContext.startService(intent);
mContext.bindService(intent, this, Context.BIND_AUTO_CREATE);
}
}.connect(context);
}
Expand All @@ -472,27 +479,10 @@ public void connect(Context context) {
* @param context The context for the service.
*/
public static void startScan(Context context) {
new ServiceConnection() {
private Context mContext;

new UrlDeviceDiscoveryServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
// Get the service
UrlDeviceDiscoveryService.LocalBinder localBinder =
(UrlDeviceDiscoveryService.LocalBinder) service;
public void serviceHandler(UrlDeviceDiscoveryService.LocalBinder localBinder) {
localBinder.getServiceInstance().restartScan();
mContext.unbindService(this);
}

@Override
public void onServiceDisconnected(ComponentName className) {
}

public void connect(Context context) {
mContext = context;
Intent intent = new Intent(mContext, UrlDeviceDiscoveryService.class);
mContext.startService(intent);
mContext.bindService(intent, this, Context.BIND_AUTO_CREATE);
}
}.connect(context);
}
Expand Down

0 comments on commit 1f2c296

Please sign in to comment.