From 00e131478ad4e37576eb85581bb663b65302a4e0 Mon Sep 17 00:00:00 2001 From: Daniel Cohen Gindi Date: Wed, 27 Jun 2018 21:17:51 +0300 Subject: [PATCH] Defer start and configure until `mService != null` Defer `stop` too PR: https://github.com/mauron85/background-geolocation-android/pull/7 By: @danielgindi --- .../bgloc/BackgroundGeolocationFacade.java | 54 ++++++++++++++++++- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/marianhello/bgloc/BackgroundGeolocationFacade.java b/src/main/java/com/marianhello/bgloc/BackgroundGeolocationFacade.java index 3b495145..bbf808c8 100644 --- a/src/main/java/com/marianhello/bgloc/BackgroundGeolocationFacade.java +++ b/src/main/java/com/marianhello/bgloc/BackgroundGeolocationFacade.java @@ -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; @@ -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) { @@ -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() { @@ -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(); @@ -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);