From 8e3a3528acffbc5344bffb90fc53267354c9dd97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Katzer?= Date: Sun, 1 Jan 2017 18:48:06 +0100 Subject: [PATCH] Enable geo location tracking in background [Fixes #186] --- src/android/BackgroundMode.java | 78 ++++++++++++++++++++++++++------- www/background-mode.js | 9 ++++ 2 files changed, 72 insertions(+), 15 deletions(-) diff --git a/src/android/BackgroundMode.java b/src/android/BackgroundMode.java index e236459..a0df8b8 100644 --- a/src/android/BackgroundMode.java +++ b/src/android/BackgroundMode.java @@ -27,6 +27,7 @@ import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; +import android.view.View; import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaPlugin; @@ -34,6 +35,8 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import java.lang.reflect.Method; + public class BackgroundMode extends CordovaPlugin { // Event types for callbacks @@ -62,7 +65,6 @@ public class BackgroundMode extends CordovaPlugin { // Used to (un)bind the service to with the activity private final ServiceConnection connection = new ServiceConnection() { - @Override public void onServiceConnected(ComponentName name, IBinder service) { ForegroundService.ForegroundBinder binder = @@ -96,14 +98,14 @@ public class BackgroundMode extends CordovaPlugin { if (action.equalsIgnoreCase("configure")) { JSONObject settings = args.getJSONObject(0); - boolean update = args.getBoolean(1); + boolean update = args.getBoolean(1); - if (update) { - updateNotification(settings); - } else { - setDefaultSettings(settings); - } + configure(settings, update); + return true; + } + if (action.equalsIgnoreCase("enableGeoLocation")) { + enableGeoLocation(); return true; } @@ -174,11 +176,24 @@ public class BackgroundMode extends CordovaPlugin { isDisabled = true; } + /** + * Update the default settings and configure the notification. + * + * @param settings The settings + * @param update A truthy value means to update the running service. + */ + private void configure(JSONObject settings, boolean update) { + if (update) { + updateNotification(settings); + } else { + setDefaultSettings(settings); + } + } + /** * Update the default settings for the notification. * - * @param settings - * The new default settings + * @param settings The new default settings */ private void setDefaultSettings(JSONObject settings) { defaultSettings = settings; @@ -197,8 +212,7 @@ public class BackgroundMode extends CordovaPlugin { /** * Update the notification. * - * @param settings - * The config settings + * @param settings The config settings */ private void updateNotification(JSONObject settings) { if (isBind) { @@ -254,13 +268,46 @@ public class BackgroundMode extends CordovaPlugin { isBind = false; } + /** + * Enable GPS position tracking while in background. + */ + private void enableGeoLocation() { + Thread thread = new Thread(){ + public void run() { + try { + Thread.sleep(1000); + cordova.getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + View view = webView.getEngine().getView(); + + try { + Class xWalkCls = Class.forName( + "org.crosswalk.engine.XWalkCordovaView"); + + Method onShowMethod = + xWalkCls.getMethod("onShow"); + + onShowMethod.invoke(view); + } catch (Exception e){ + view.dispatchWindowVisibilityChanged(View.VISIBLE); + } + } + }); + } catch (InterruptedException e) { + // do nothing + } + } + }; + + thread.start(); + } + /** * Fire vent with some parameters inside the web view. * - * @param event - * The name of the event - * @param params - * Optional arguments for the event + * @param event The name of the event + * @param params Optional arguments for the event */ private void fireEvent (Event event, String params) { String eventName; @@ -294,4 +341,5 @@ public class BackgroundMode extends CordovaPlugin { } }); } + } diff --git a/www/background-mode.js b/www/background-mode.js index d22628e..7b1344a 100644 --- a/www/background-mode.js +++ b/www/background-mode.js @@ -90,6 +90,15 @@ exports.configure = function (options) { } }; +/** + * Enable GPS-tracking in background (Android). + */ +exports.enableGeoLocation = function () { + if (device.platform == 'Android') { + cordova.exec(null, null, 'BackgroundMode', 'enableGeoLocation', []); + } +}; + /** * If the mode is enabled or disabled. *