From 4e991fb72927f3ed9162cfe26f8beff0b1129735 Mon Sep 17 00:00:00 2001 From: Martin Boonk Date: Sat, 7 Jan 2023 11:41:01 +0100 Subject: [PATCH] android - Handle connection events and switch GPS use over --- apps/android/boot.js | 57 ++++++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 15 deletions(-) diff --git a/apps/android/boot.js b/apps/android/boot.js index 814e35aa1..9ae75e228 100644 --- a/apps/android/boot.js +++ b/apps/android/boot.js @@ -141,7 +141,7 @@ Bangle.emit('GPS', event); }, "is_gps_active": function() { - gbSend({ t: "gps_power", status: Bangle._PWR && Bangle._PWR.GPS && Bangle._PWR.GPS.length>0 }); + gbSend({ t: "gps_power", status: Bangle.isGBGPSOn() }); } }; var h = HANDLERS[event.t]; @@ -207,27 +207,54 @@ }; // GPS overwrite logic if (settings.overwriteGps) { // if the overwrite option is set../ - // Replace set GPS power logic to suppress activation of gps (and instead request it from the phone) - Bangle.setGPSPower = (o => (isOn, appID) => { - // if not connected, use old logic - if (!NRF.getSecurityStatus().connected) return o(isOn, appID); - // Emulate old GPS power logic + const origIsGPSOn = Bangle.isGPSOn; + const origSetGPSPower = Bangle.setGPSPower; + // migrate all GPS clients to the other variant on connection events + let handleConnect = () => { + if (origIsGPSOn()){ + for (let c of Bangle._PWR.GPS){ + Bangle.setGBGPSPower(1, c); + origSetGPSPower(0, c); + } + } + }; + let handleDisconnect = () => { + if (Bangle.isGBGPSOn()){ + for (let c of Bangle._PWR.GBGPS){ + origSetGPSPower(1, c); + Bangle.setGBGPSPower(0, c); + } + } + }; + NRF.on('connect', handleConnect); + NRF.on('disconnect', handleDisconnect); + Bangle.setGBGPSPower = (isOn, appID) => { if (!Bangle._PWR) Bangle._PWR={}; - if (!Bangle._PWR.GPS) Bangle._PWR.GPS=[]; + if (!Bangle._PWR.GBGPS) Bangle._PWR.GBGPS=[]; 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; + if (isOn && !Bangle._PWR.GBGPS.includes(appID)) Bangle._PWR.GBGPS.push(appID); + if (!isOn && Bangle._PWR.GBGPS.includes(appID)) Bangle._PWR.GBGPS.splice(Bangle._PWR.GBGPS.indexOf(appID),1); + let pwr = Bangle._PWR.GBGPS.length>0; gbSend({ t: "gps_power", status: pwr }); return pwr; - })(Bangle.setGPSPower); - // Replace check if the GPS is on to check the _PWR variable + }; + // 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 internal GPS power function + if (!NRF.getSecurityStatus().connected) return origSetGPSPower(isOn, appID); + return Bangle.setGBGPSPower(isOn, appID); + }; + // Allow checking for GPS via GadgetBridge + Bangle.isGBGPSOn = () => { + return !!(Bangle._PWR && Bangle._PWR.GBGPS && Bangle._PWR.GBGPS.length>0); + }; + // Replace check if the GPS is on to check both internal and external GPS Bangle.isGPSOn = () => { - return Bangle._PWR && Bangle._PWR.GPS && Bangle._PWR.GPS.length>0; - } + return Bangle.isGBGPSOn() || origIsGPSOn(); + }; // stop GPS on boot if not activated setTimeout(()=>{ - if (!Bangle.isGPSOn()) gbSend({ t: "gps_power", status: false }); + if (!Bangle.isGBGPSOn()) gbSend({ t: "gps_power", status: false }); },3000); }