2016-11-14 17:51:33 +01:00
|
|
|
if (window.Storage){
|
2016-11-27 11:31:03 +01:00
|
|
|
var prefix = GBjs.getAppLocalstoragePrefix();
|
2016-11-14 17:51:33 +01:00
|
|
|
GBjs.gbLog("redefining local storage with prefix: " + prefix);
|
|
|
|
|
|
|
|
Storage.prototype.setItem = (function(key, value) {
|
|
|
|
this.call(localStorage,prefix + key, value);
|
|
|
|
}).bind(Storage.prototype.setItem);
|
|
|
|
|
|
|
|
Storage.prototype.getItem = (function(key) {
|
2016-11-18 12:33:12 +01:00
|
|
|
// console.log("I am about to return " + prefix + key);
|
|
|
|
var def = null;
|
|
|
|
if(key == 'clay-settings') {
|
|
|
|
def = '{}';
|
|
|
|
}
|
|
|
|
return this.call(localStorage,prefix + key) || def;
|
2016-11-14 17:51:33 +01:00
|
|
|
}).bind(Storage.prototype.getItem);
|
|
|
|
}
|
2016-06-18 18:40:57 +02:00
|
|
|
|
2016-02-28 22:25:21 +01:00
|
|
|
function loadScript(url, callback) {
|
|
|
|
// Adding the script tag to the head as suggested before
|
|
|
|
var head = document.getElementsByTagName('head')[0];
|
|
|
|
var script = document.createElement('script');
|
|
|
|
script.type = 'text/javascript';
|
|
|
|
script.src = url;
|
|
|
|
|
|
|
|
// Then bind the event to the callback function.
|
|
|
|
// There are several events for cross browser compatibility.
|
|
|
|
script.onreadystatechange = callback;
|
|
|
|
script.onload = callback;
|
|
|
|
|
|
|
|
// Fire the loading
|
|
|
|
head.appendChild(script);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getURLVariable(variable, defaultValue) {
|
|
|
|
// Find all URL parameters
|
|
|
|
var query = location.search.substring(1);
|
|
|
|
var vars = query.split('&');
|
|
|
|
for (var i = 0; i < vars.length; i++) {
|
|
|
|
var pair = vars[i].split('=');
|
|
|
|
|
|
|
|
// If the query variable parameter is found, decode it to use and return it for use
|
|
|
|
if (pair[0] === variable) {
|
|
|
|
return decodeURIComponent(pair[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return defaultValue || false;
|
|
|
|
}
|
|
|
|
|
2016-06-18 18:40:57 +02:00
|
|
|
function showStep(desiredStep) {
|
|
|
|
var steps = document.getElementsByClassName("step");
|
|
|
|
var testStep = null;
|
|
|
|
for (var i = 0; i < steps.length; i ++) {
|
|
|
|
if (steps[i].id == desiredStep)
|
|
|
|
testStep = steps[i].id;
|
|
|
|
}
|
|
|
|
if (testStep !== null) {
|
|
|
|
for (var i = 0; i < steps.length; i ++) {
|
|
|
|
steps[i].style.display = 'none';
|
|
|
|
}
|
|
|
|
document.getElementById(desiredStep).style.display="block";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-28 22:25:21 +01:00
|
|
|
function gbPebble() {
|
|
|
|
this.configurationURL = null;
|
|
|
|
this.configurationValues = null;
|
2016-06-18 18:40:57 +02:00
|
|
|
var self = this;
|
2016-11-24 18:03:47 +01:00
|
|
|
self.events = {};
|
|
|
|
//events processing: see http://stackoverflow.com/questions/10978311/implementing-events-in-my-own-object
|
|
|
|
self.addEventListener = function(name, handler) {
|
|
|
|
if (self.events.hasOwnProperty(name))
|
|
|
|
self.events[name].push(handler);
|
|
|
|
else
|
|
|
|
self.events[name] = [handler];
|
|
|
|
}
|
2016-02-28 22:25:21 +01:00
|
|
|
|
2016-11-24 18:03:47 +01:00
|
|
|
self.removeEventListener = function(name, handler) {
|
|
|
|
if (!self.events.hasOwnProperty(name))
|
|
|
|
return;
|
|
|
|
|
|
|
|
var index = self.events[name].indexOf(handler);
|
|
|
|
if (index != -1)
|
|
|
|
self.events[name].splice(index, 1);
|
2016-02-28 22:25:21 +01:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:03:47 +01:00
|
|
|
self.evaluate = function(name, args) {
|
|
|
|
if (!self.events.hasOwnProperty(name))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!args || !args.length)
|
|
|
|
args = [];
|
|
|
|
|
|
|
|
var evs = self.events[name], l = evs.length;
|
|
|
|
for (var i = 0; i < l; i++) {
|
|
|
|
evs[i].apply(null, args);
|
2016-03-04 17:44:42 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-24 18:03:47 +01:00
|
|
|
|
2016-02-28 22:25:21 +01:00
|
|
|
this.actuallyOpenURL = function() {
|
2016-06-18 18:40:57 +02:00
|
|
|
showStep("step1compat");
|
|
|
|
window.open(self.configurationURL.toString(), "config");
|
2016-02-28 22:25:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.actuallySendData = function() {
|
2016-06-18 18:40:57 +02:00
|
|
|
GBjs.sendAppMessage(self.configurationValues);
|
|
|
|
GBjs.closeActivity();
|
2016-02-28 22:25:21 +01:00
|
|
|
}
|
|
|
|
|
2016-06-26 11:55:08 +02:00
|
|
|
this.savePreset = function() {
|
|
|
|
GBjs.saveAppStoredPreset(self.configurationValues);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.loadPreset = function() {
|
|
|
|
showStep("step2");
|
|
|
|
var presetElements = document.getElementsByClassName("store_presets");
|
|
|
|
for (var i = 0; i < presetElements.length; i ++) {
|
|
|
|
presetElements[i].style.display = 'none';
|
|
|
|
}
|
2016-06-26 14:13:47 +02:00
|
|
|
self.configurationValues = GBjs.getAppStoredPreset();
|
|
|
|
document.getElementById("jsondata").innerHTML=self.configurationValues;
|
2016-06-26 11:55:08 +02:00
|
|
|
}
|
|
|
|
|
2016-02-28 22:25:21 +01:00
|
|
|
//needs to be called like this because of original Pebble function name
|
|
|
|
this.openURL = function(url) {
|
2016-03-08 17:45:11 +01:00
|
|
|
if (url.lastIndexOf("http", 0) === 0) {
|
|
|
|
document.getElementById("config_url").innerHTML=url;
|
|
|
|
var UUID = GBjs.getAppUUID();
|
2016-06-18 18:40:57 +02:00
|
|
|
self.configurationURL = new Uri(url).addQueryParam("return_to", "gadgetbridge://"+UUID+"?config=true&json=");
|
2016-03-08 17:45:11 +01:00
|
|
|
} else {
|
|
|
|
//TODO: add custom return_to
|
|
|
|
location.href = url;
|
|
|
|
}
|
|
|
|
|
2016-02-28 22:25:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.getActiveWatchInfo = function() {
|
|
|
|
return JSON.parse(GBjs.getActiveWatchInfo());
|
|
|
|
}
|
|
|
|
|
2016-03-04 17:44:42 +01:00
|
|
|
this.sendAppMessage = function (dict, callbackAck, callbackNack){
|
|
|
|
try {
|
2016-06-18 18:40:57 +02:00
|
|
|
self.configurationValues = JSON.stringify(dict);
|
2016-06-26 11:55:08 +02:00
|
|
|
document.getElementById("jsondata").innerHTML=self.configurationValues;
|
2016-03-04 17:44:42 +01:00
|
|
|
return callbackAck;
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
GBjs.gbLog("sendAppMessage failed");
|
|
|
|
return callbackNack;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.getAccountToken = function() {
|
|
|
|
return '';
|
2016-02-28 22:25:21 +01:00
|
|
|
}
|
|
|
|
|
2016-03-04 17:44:42 +01:00
|
|
|
this.getWatchToken = function() {
|
|
|
|
return GBjs.getWatchToken();
|
2016-02-28 22:25:21 +01:00
|
|
|
}
|
2016-03-04 17:44:42 +01:00
|
|
|
|
2016-06-18 18:40:57 +02:00
|
|
|
this.getTimelineToken = function() {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2016-03-04 17:44:42 +01:00
|
|
|
this.showSimpleNotificationOnPebble = function(title, body) {
|
|
|
|
GBjs.gbLog("app wanted to show: " + title + " body: "+ body);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-18 21:17:38 +02:00
|
|
|
this.showConfiguration = function() {
|
|
|
|
console.error("This watchapp doesn't support configuration");
|
|
|
|
GBjs.closeActivity();
|
|
|
|
}
|
|
|
|
|
2016-06-17 17:47:13 +02:00
|
|
|
this.parseReturnedPebbleJS = function() {
|
|
|
|
var str = document.getElementById('pastereturn').value;
|
|
|
|
var needle = "pebblejs://close#";
|
|
|
|
|
|
|
|
if (str.split(needle)[1] !== undefined) {
|
|
|
|
var t = new Object();
|
2016-11-24 18:03:47 +01:00
|
|
|
t.response = decodeURIComponent(str.split(needle)[1]);
|
|
|
|
self.evaluate('webviewclosed',[t]);
|
2016-06-18 18:40:57 +02:00
|
|
|
showStep("step2");
|
2016-06-17 17:47:13 +02:00
|
|
|
} else {
|
|
|
|
console.error("No valid configuration found in the entered string.");
|
|
|
|
}
|
|
|
|
}
|
2016-02-28 22:25:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var Pebble = new gbPebble();
|
|
|
|
|
|
|
|
var jsConfigFile = GBjs.getAppConfigurationFile();
|
2016-06-26 11:55:08 +02:00
|
|
|
var storedPreset = GBjs.getAppStoredPreset();
|
|
|
|
|
2016-06-18 18:40:57 +02:00
|
|
|
document.addEventListener('DOMContentLoaded', function(){
|
2016-02-28 22:25:21 +01:00
|
|
|
if (jsConfigFile != null) {
|
|
|
|
loadScript(jsConfigFile, function() {
|
2016-12-09 11:55:11 +01:00
|
|
|
Pebble.evaluate('ready');
|
2016-02-28 22:25:21 +01:00
|
|
|
if (getURLVariable('config') == 'true') {
|
2016-06-18 18:40:57 +02:00
|
|
|
showStep("step2");
|
2016-12-09 11:55:11 +01:00
|
|
|
var json_string = getURLVariable('json');
|
2016-02-28 22:25:21 +01:00
|
|
|
var t = new Object();
|
|
|
|
t.response = json_string;
|
2016-11-24 18:03:47 +01:00
|
|
|
if (json_string != '') {
|
|
|
|
Pebble.evaluate('webviewclosed',[t]);
|
|
|
|
}
|
|
|
|
|
2016-02-28 22:25:21 +01:00
|
|
|
} else {
|
2016-06-26 11:55:08 +02:00
|
|
|
if (storedPreset === undefined) {
|
|
|
|
var presetElements = document.getElementsByClassName("load_presets");
|
|
|
|
for (var i = 0; i < presetElements.length; i ++) {
|
|
|
|
presetElements[i].style.display = 'none';
|
|
|
|
}
|
|
|
|
}
|
2016-11-24 18:03:47 +01:00
|
|
|
Pebble.evaluate('showConfiguration');
|
2016-02-28 22:25:21 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-06-18 18:40:57 +02:00
|
|
|
}, false);
|