From d5e68798979d54b9ad1c8cdd6d33ea21d49021b9 Mon Sep 17 00:00:00 2001 From: Lukas Date: Sun, 6 Nov 2022 12:31:13 +0100 Subject: [PATCH 1/5] add handling of gps events --- apps/android/boot.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/apps/android/boot.js b/apps/android/boot.js index 0d1edae99..8bcc1ba0d 100644 --- a/apps/android/boot.js +++ b/apps/android/boot.js @@ -130,6 +130,13 @@ request.j(event.err); //r = reJect function else request.r(event); //r = resolve function + }, + "gps": function() { + delete event.t; + event.satellites = NaN; + event.course = NaN; + event.fix = 1; + Bangle.emit('gps', event); } }; var h = HANDLERS[event.t]; From 31bed21581242731de263bad8b5f43becbaccb3a Mon Sep 17 00:00:00 2001 From: Lukas Date: Sun, 6 Nov 2022 13:41:49 +0100 Subject: [PATCH 2/5] add setting to set overwrite of internal gps --- apps/android/boot.js | 28 ++++++++++++++++++++++++++++ apps/android/settings.js | 13 +++++++++++++ 2 files changed, 41 insertions(+) diff --git a/apps/android/boot.js b/apps/android/boot.js index 8bcc1ba0d..a8455cf35 100644 --- a/apps/android/boot.js +++ b/apps/android/boot.js @@ -197,6 +197,34 @@ if (isFinite(msg.id)) return gbSend({ t: "notify", n:response?"OPEN":"DISMISS", id: msg.id }); // error/warn here? }; + + // GPS overwrite logic + // Save current logic + const originalSetGpsPower = Bangle.setGPSPower; + const originalIsGpsOn = Bangle.isGPSOn; + + // Replace set GPS power logic to suppress activation of gps, if the overwrite option is active + Bangle.setGPSPower = (isOn, appID) => { + const currentSettings = require("Storage").readJSON("android.settings.json",1)||{}; + if (!currentSettings.overwriteGps) { + originalSetGpsPower(isOn, appID); + } else { + const logMessage = 'Ignore gps power change due to the gps overwrite from android integration app'; + console.log(logMessage); + Bluetooth.println(logMessage); + } + } + + // Replace check if the GPS is on, to show it as always active, if the overwrite option is set + Bangle.isGPSOn = () => { + const currentSettings = require("Storage").readJSON("android.settings.json",1)||{}; + if (!currentSettings.overwriteGps) { + return originalIsGpsOn(); + } else { + return true; + } + } + // remove settings object so it's not taking up RAM delete settings; })(); diff --git a/apps/android/settings.js b/apps/android/settings.js index c7c34a76f..94a1eba0b 100644 --- a/apps/android/settings.js +++ b/apps/android/settings.js @@ -1,4 +1,13 @@ (function(back) { + + function onGpsOverwriteChange(newValue) { + if (newValue) { + Bangle.setGPSPower(false, 'android'); + } + settings.overwriteGps = newValue; + updateSettings(); + } + function gb(j) { Bluetooth.println(JSON.stringify(j)); } @@ -23,6 +32,10 @@ updateSettings(); } }, + /*LANG*/"Overwrite GPS" : { + value : !!settings.overwriteGps, + onchange: onGpsOverwriteChange + }, /*LANG*/"Messages" : ()=>load("messages.app.js"), }; E.showMenu(mainmenu); From c68c14a9ed82502d906c2e2274a02441b7878cf6 Mon Sep 17 00:00:00 2001 From: Lukas Date: Sun, 6 Nov 2022 19:25:25 +0100 Subject: [PATCH 3/5] add check if gps data should be overwritten, before the gps data is emitted --- apps/android/boot.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/apps/android/boot.js b/apps/android/boot.js index a8455cf35..5cdc1f044 100644 --- a/apps/android/boot.js +++ b/apps/android/boot.js @@ -132,6 +132,8 @@ request.r(event); //r = resolve function }, "gps": function() { + const settings = require("Storage").readJSON("android.settings.json",1)||{}; + if (!settings.overwriteGps) return; delete event.t; event.satellites = NaN; event.course = NaN; From 7561f8d7c245ac68dc222e2c87355693ce8e7f26 Mon Sep 17 00:00:00 2001 From: Lukas Date: Mon, 21 Nov 2022 20:39:41 +0100 Subject: [PATCH 4/5] add gps request handling --- apps/android/boot.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/apps/android/boot.js b/apps/android/boot.js index 5cdc1f044..7973456cd 100644 --- a/apps/android/boot.js +++ b/apps/android/boot.js @@ -139,6 +139,10 @@ event.course = NaN; event.fix = 1; Bangle.emit('gps', event); + }, + "is_gps_active": function() { + const gpsActive = originalIsGpsOn(); + sendGPSPowerStatus(gpsActive); } }; var h = HANDLERS[event.t]; @@ -205,12 +209,15 @@ const originalSetGpsPower = Bangle.setGPSPower; const originalIsGpsOn = Bangle.isGPSOn; + function sendGPSPowerStatus(status) { gbSend({ t: "gps_power", status: status }); } + // Replace set GPS power logic to suppress activation of gps, if the overwrite option is active Bangle.setGPSPower = (isOn, appID) => { const currentSettings = require("Storage").readJSON("android.settings.json",1)||{}; if (!currentSettings.overwriteGps) { originalSetGpsPower(isOn, appID); } else { + sendGPSPowerStatus(Bangle.isGPSOn()); const logMessage = 'Ignore gps power change due to the gps overwrite from android integration app'; console.log(logMessage); Bluetooth.println(logMessage); From 8678d0cc2137c0d2344f71f92912df46daa2302f Mon Sep 17 00:00:00 2001 From: Gordon Williams Date: Mon, 28 Nov 2022 14:22:35 +0000 Subject: [PATCH 5/5] slight tweaks to https://github.com/espruino/BangleApps/pull/2304/ --- apps/android/ChangeLog | 1 + apps/android/README.md | 2 ++ apps/android/boot.js | 48 ++++++++++++++++---------------------- apps/android/metadata.json | 2 +- apps/android/settings.js | 16 ++++++------- 5 files changed, 32 insertions(+), 37 deletions(-) diff --git a/apps/android/ChangeLog b/apps/android/ChangeLog index fcb139c94..f71e94cf7 100644 --- a/apps/android/ChangeLog +++ b/apps/android/ChangeLog @@ -15,3 +15,4 @@ 0.15: Allow method/body/headers to be specified for `http` (needs Gadgetbridge 0.68.0b or later) 0.16: Bangle.http now fails immediately if there is no Bluetooth connection (fix #2152) 0.17: Now kick off Calendar sync as soon as connected to Gadgetbridge +0.18: If connected to Gadgetbridge, allow GPS forwarding from phone (Gadgetbridge code still not merged) diff --git a/apps/android/README.md b/apps/android/README.md index f9ab73699..c76e6e528 100644 --- a/apps/android/README.md +++ b/apps/android/README.md @@ -20,6 +20,8 @@ It contains: of Gadgetbridge - making your phone make noise so you can find it. * `Keep Msgs` - default is `Off`. When Gadgetbridge disconnects, should Bangle.js keep any messages it has received, or should it delete them? +* `Overwrite GPS` - when GPS is requested by an app, this doesn't use Bangle.js's GPS +but instead asks Gadgetbridge on the phone to use the phone's GPS * `Messages` - launches the messages app, showing a list of messages ## How it works diff --git a/apps/android/boot.js b/apps/android/boot.js index 8d9270b2a..8e75241e7 100644 --- a/apps/android/boot.js +++ b/apps/android/boot.js @@ -137,8 +137,7 @@ Bangle.emit('gps', event); }, "is_gps_active": function() { - const gpsActive = originalIsGpsOn(); - sendGPSPowerStatus(gpsActive); + gbSend({ t: "gps_power", status: Bangle._PWR && Bangle._PWR.GPS && Bangle._PWR.GPS.length>0 }); } }; var h = HANDLERS[event.t]; @@ -202,34 +201,27 @@ if (isFinite(msg.id)) return gbSend({ t: "notify", n:response?"OPEN":"DISMISS", id: msg.id }); // error/warn here? }; - // GPS overwrite logic - // Save current logic - const originalSetGpsPower = Bangle.setGPSPower; - const originalIsGpsOn = Bangle.isGPSOn; - - function sendGPSPowerStatus(status) { gbSend({ t: "gps_power", status: status }); } - - // Replace set GPS power logic to suppress activation of gps, if the overwrite option is active - Bangle.setGPSPower = (isOn, appID) => { - const currentSettings = require("Storage").readJSON("android.settings.json",1)||{}; - if (!currentSettings.overwriteGps) { - originalSetGpsPower(isOn, appID); - } else { - sendGPSPowerStatus(Bangle.isGPSOn()); - const logMessage = 'Ignore gps power change due to the gps overwrite from android integration app'; - console.log(logMessage); - Bluetooth.println(logMessage); + if (settings.overwriteGps) { // if the overwrite option is set../ + // Save current logic + const originalSetGpsPower = Bangle.setGPSPower; + // Replace set GPS power logic to suppress activation of gps (and instead request it from the phone) + Bangle.setGPSPower = (isOn, appID) => { + // if not connected, use old logic + if (!NRF.getSecurityStatus().connected) return originalSetGpsPower(isOn, appID); + // Emulate old GPS power logic + if (!Bangle._PWR) Bangle._PWR={}; + if (!Bangle._PWR.GPS) Bangle._PWR.GPS=[]; + if (!appID) appID="?"; + if (isOn && !Bangle._PWR.GPS.includes(appID)) Bangle._PWR.GPS.push(appID); + if (!isOn && Bangle._PWR.GPS.includes(appID)) Bangle._PWR.GPS.splice(Bangle._PWR.GPS.indexOf(appID),1); + let pwr = Bangle._PWR.GPS.length>0; + gbSend({ t: "gps_power", status: pwr }); + return pwr; } - } - - // Replace check if the GPS is on, to show it as always active, if the overwrite option is set - Bangle.isGPSOn = () => { - const currentSettings = require("Storage").readJSON("android.settings.json",1)||{}; - if (!currentSettings.overwriteGps) { - return originalIsGpsOn(); - } else { - return true; + // Replace check if the GPS is on to check the _PWR variable + Bangle.isGPSOn = () => { + return Bangle._PWR && Bangle._PWR.GPS && Bangle._PWR.GPS.length>0; } } diff --git a/apps/android/metadata.json b/apps/android/metadata.json index ac47449d6..ad5f09243 100644 --- a/apps/android/metadata.json +++ b/apps/android/metadata.json @@ -2,7 +2,7 @@ "id": "android", "name": "Android Integration", "shortName": "Android", - "version": "0.17", + "version": "0.18", "description": "Display notifications/music/etc sent from the Gadgetbridge app on Android. This replaces the old 'Gadgetbridge' Bangle.js widget.", "icon": "app.png", "tags": "tool,system,messages,notifications,gadgetbridge", diff --git a/apps/android/settings.js b/apps/android/settings.js index 94a1eba0b..b3057e888 100644 --- a/apps/android/settings.js +++ b/apps/android/settings.js @@ -1,12 +1,6 @@ (function(back) { - function onGpsOverwriteChange(newValue) { - if (newValue) { - Bangle.setGPSPower(false, 'android'); - } - settings.overwriteGps = newValue; - updateSettings(); - } + function gb(j) { Bluetooth.println(JSON.stringify(j)); @@ -34,7 +28,13 @@ }, /*LANG*/"Overwrite GPS" : { value : !!settings.overwriteGps, - onchange: onGpsOverwriteChange + onchange: newValue => { + if (newValue) { + Bangle.setGPSPower(false, 'android'); + } + settings.overwriteGps = newValue; + updateSettings(); + } }, /*LANG*/"Messages" : ()=>load("messages.app.js"), };