Implement watchapp configuration presets.

This is a quick & dirty solution that may be improved by storing multiple presets in the DB in the future.
master
Daniele Gobbetti 2016-06-26 11:55:08 +02:00
parent 248e38b5ef
commit 1430619c30
6 changed files with 75 additions and 2 deletions

View File

@ -4,6 +4,7 @@
####Version 0.11.0
* Pebble: new App Manager (keeps track of installed apps and allows app sorting on FW 3.x)
* Pebble: call dismissal with canned SMS (FW 3.x)
* Pebble: watchapp configuration presets
* Pebble: fix regression with FW 2.x (almost everything was broken in 0.10.2)
####Version 0.10.2

View File

@ -60,6 +60,11 @@
<button class="btn" name="open config" value="open config" onclick="Pebble.actuallyOpenURL()">
Open configuration website
</button>
<h2 class="load_presets">App presets:</h2>
<button class="btn load_presets" name="read config" value="read config"
onclick="Pebble.loadPreset()">
Load saved configuration
</button>
</div>
<div id="step1compat" class="step">
<p>In case of "network error" after saving settings in the watchhapp, copy the "network error"
@ -75,5 +80,11 @@
<button class="btn" name="send config" value="send config" onclick="Pebble.actuallySendData()">
Send data to pebble
</button>
<h2 class="store_presets">App Presets:</h2>
<button class="btn store_presets" name="store config" value="store config"
onclick="Pebble.savePreset()">
Store incoming configuration
</button>
<p class="store_presets">Existing presets will be deleted.</p>
</div>
</body>

View File

@ -91,6 +91,23 @@ function gbPebble() {
GBjs.closeActivity();
}
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';
}
var json_string = GBjs.getAppStoredPreset();
var t = new Object();
t.response = json_string;
if (json_string != '')
Pebble.parseconfig(t);
}
//needs to be called like this because of original Pebble function name
this.openURL = function(url) {
if (url.lastIndexOf("http", 0) === 0) {
@ -111,7 +128,7 @@ function gbPebble() {
this.sendAppMessage = function (dict, callbackAck, callbackNack){
try {
self.configurationValues = JSON.stringify(dict);
document.getElementById("jsondata").innerHTML=this.configurationValues;
document.getElementById("jsondata").innerHTML=self.configurationValues;
return callbackAck;
}
catch (e) {
@ -162,6 +179,8 @@ function gbPebble() {
var Pebble = new gbPebble();
var jsConfigFile = GBjs.getAppConfigurationFile();
var storedPreset = GBjs.getAppStoredPreset();
document.addEventListener('DOMContentLoaded', function(){
if (jsConfigFile != null) {
loadScript(jsConfigFile, function() {
@ -173,6 +192,12 @@ if (jsConfigFile != null) {
if (json_string != '')
Pebble.parseconfig(t);
} else {
if (storedPreset === undefined) {
var presetElements = document.getElementsByClassName("load_presets");
for (var i = 0; i < presetElements.length; i ++) {
presetElements[i].style.display = 'none';
}
}
Pebble.ready();
Pebble.showConfiguration();
}

View File

@ -20,8 +20,11 @@ import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import java.util.Scanner;
import java.util.UUID;
@ -233,6 +236,38 @@ public class ExternalPebbleJSActivity extends GBActivity {
return null;
}
@JavascriptInterface
public String getAppStoredPreset() {
try {
File destDir = new File(FileUtils.getExternalFilesDir() + "/pbw-cache");
File configurationFile = new File(destDir, appUuid.toString() + "_preset.json");
if (configurationFile.exists()) {
return FileUtils.getStringFromFile(configurationFile);
}
} catch (IOException e) {
GB.toast("Error reading presets", Toast.LENGTH_LONG, GB.ERROR);
e.printStackTrace();
}
return null;
}
@JavascriptInterface
public void saveAppStoredPreset(String msg) {
Writer writer;
try {
File destDir = new File(FileUtils.getExternalFilesDir() + "/pbw-cache");
File presetsFile = new File(destDir, appUuid.toString() + "_preset.json");
writer = new BufferedWriter(new FileWriter(presetsFile));
writer.write(msg);
writer.close();
GB.toast("Presets stored", Toast.LENGTH_SHORT, GB.INFO);
} catch (IOException e) {
GB.toast("Error storing presets", Toast.LENGTH_LONG, GB.ERROR);
e.printStackTrace();
}
}
@JavascriptInterface
public String getAppUUID() {
return appUuid.toString();

View File

@ -312,7 +312,7 @@ public abstract class AbstractAppManagerFragment extends Fragment {
return true;
}
String[] suffixToDelete = new String[]{".pbw", ".json", "_config.js"};
String[] suffixToDelete = new String[]{".pbw", ".json", "_config.js", "_preset.json"};
for (String suffix : suffixToDelete) {
File fileToDelete = new File(baseName + suffix);

View File

@ -3,6 +3,7 @@
<release version="0.11.0" versioncode="56">
<change>Pebble: new App Manager (keeps track of installed apps and allows app sorting on FW 3.x)</change>
<change>Pebble: call dismissal with canned SMS (FW 3.x)</change>
<change>Pebble: watchapp configuration presets</change>
<change>Pebble: fix regression with FW 2.x (almost everything was broken in 0.10.2)</change>
</release>
<release version="0.10.2" versioncode="55">