From 84327a5b866aee745d44b3c565b7a4735d2f4604 Mon Sep 17 00:00:00 2001 From: Daniele Gobbetti Date: Sun, 27 Nov 2016 11:31:03 +0100 Subject: [PATCH] Pebble: use also the device address as seed to store app configuration This is not needed as long as one GB instance is used to manage a single pebble device, if multiple devices are managed the stored watchapp configuration could be messed up. --- .../app_config/js/gadgetbridge_boilerplate.js | 3 +-- .../activities/ExternalPebbleJSActivity.java | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/app/src/main/assets/app_config/js/gadgetbridge_boilerplate.js b/app/src/main/assets/app_config/js/gadgetbridge_boilerplate.js index 0c51b1dd..50bab12d 100644 --- a/app/src/main/assets/app_config/js/gadgetbridge_boilerplate.js +++ b/app/src/main/assets/app_config/js/gadgetbridge_boilerplate.js @@ -1,5 +1,5 @@ if (window.Storage){ - var prefix = GBjs.getAppUUID(); + var prefix = GBjs.getAppLocalstoragePrefix(); GBjs.gbLog("redefining local storage with prefix: " + prefix); Storage.prototype.setItem = (function(key, value) { @@ -85,7 +85,6 @@ function gbPebble() { } self.evaluate = function(name, args) { - console.log(args); if (!self.events.hasOwnProperty(name)) return; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ExternalPebbleJSActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ExternalPebbleJSActivity.java index d26691b7..4904a181 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ExternalPebbleJSActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ExternalPebbleJSActivity.java @@ -24,7 +24,10 @@ import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.io.Writer; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.util.Iterator; import java.util.Scanner; import java.util.UUID; @@ -274,6 +277,25 @@ public class ExternalPebbleJSActivity extends GBActivity { return appUuid.toString(); } + @JavascriptInterface + public String getAppLocalstoragePrefix() { + String prefix = mGBDevice.getAddress() + appUuid.toString(); + try { + MessageDigest digest = MessageDigest.getInstance("SHA-1"); + byte[] bytes = prefix.getBytes("UTF-8"); + digest.update(bytes, 0, bytes.length); + bytes = digest.digest(); + final StringBuilder sb = new StringBuilder(); + for (int i = 0; i < bytes.length; i++) { + sb.append(String.format("%02X", bytes[i])); + } + return sb.toString().toLowerCase(); + } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { + e.printStackTrace(); + return prefix; + } + } + @JavascriptInterface public String getWatchToken() { //specification says: A string that is is guaranteed to be identical for each Pebble device for the same app across different mobile devices. The token is unique to your app and cannot be used to track Pebble devices across applications. see https://developer.pebble.com/docs/js/Pebble/