Skip to content

Commit

Permalink
Defer start and configure until mService != null
Browse files Browse the repository at this point in the history
Defer `stop` too

PR: #7
By: @danielgindi
  • Loading branch information
danielgindi authored and mauron85 committed Aug 16, 2018
1 parent 515f621 commit 00e1314
Showing 1 changed file with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public class BackgroundGeolocationFacade {
private final Object mLock = new Object();
private final PluginDelegate mDelegate;

private boolean mShouldStartService = false;
private boolean mShouldStopService = false;
private Config mNextConfiguration = null;

private BackgroundLocation mStationaryLocation;

private org.slf4j.Logger logger;
Expand Down Expand Up @@ -102,6 +106,21 @@ public void onServiceConnected(ComponentName className, IBinder service) {
LocationService.LocalBinder binder = (LocationService.LocalBinder) service;
mService = binder.getService();
mIsBound = true;

if (mNextConfiguration != null)
{
mService.configure(mNextConfiguration);
mNextConfiguration = null;
}

if (mShouldStartService)
{
start();
}
else if (mShouldStopService)
{
stop();
}
}

public void onServiceDisconnected(ComponentName className) {
Expand Down Expand Up @@ -209,7 +228,17 @@ private synchronized void unregisterServiceBroadcast() {
}

public void start() {
if (mService == null)
{
logger.debug("Should start service, but mService is not bound yet.");
mShouldStartService = true;
mShouldStopService = false;
return;
}

logger.debug("Starting service");
mShouldStartService = false;
mShouldStopService = false;

PermissionManager permissionManager = PermissionManager.getInstance(getContext());
permissionManager.checkPermissions(Arrays.asList(PERMISSIONS), new PermissionManager.PermissionRequestListener() {
Expand All @@ -233,7 +262,18 @@ public void onPermissionDenied() {
}

public void stop() {
if (mService == null)
{
logger.debug("Should stop service, but mService is not bound yet.");
mShouldStartService = false;
mShouldStopService = true;
return;
}

logger.debug("Stopping service");
mShouldStartService = false;
mShouldStopService = false;

stopBackgroundService();
unregisterLocationModeChangeReceiver();
// unregisterServiceBroadcast();
Expand Down Expand Up @@ -361,11 +401,21 @@ public void sendCommand(final int commandId) {

public void configure(Config config) throws PluginException {
synchronized (mLock) {
try {
try
{
Config newConfig = Config.merge(getConfig(), config);
persistConfiguration(newConfig);
logger.debug("Service configured with: {}", newConfig.toString());
mService.configure(newConfig);

if (mService != null)
{
mService.configure(newConfig);
}
else
{
mNextConfiguration = newConfig;
}

} catch (Exception e) {
logger.error("Configuration error: {}", e.getMessage());
throw new PluginException("Configuration error", e, PluginException.CONFIGURE_ERROR);
Expand Down

0 comments on commit 00e1314

Please sign in to comment.