1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-28 04:46:51 +01:00

Pebble: background webview, address (most of) review comments

This commit is contained in:
Daniele Gobbetti 2017-10-13 21:57:22 +02:00
parent b0c723b68a
commit c4f36d1202
4 changed files with 28 additions and 13 deletions

View File

@ -71,6 +71,7 @@ public class ExternalPebbleJSActivity extends AbstractGBActivity {
} else if (extras.getBoolean(START_BG_WEBVIEW, false)) { } else if (extras.getBoolean(START_BG_WEBVIEW, false)) {
WebViewSingleton.ensureCreated(this); WebViewSingleton.ensureCreated(this);
finish(); finish();
return;
} }
} }
} else { } else {

View File

@ -76,7 +76,7 @@ public class GBWebClient extends WebViewClient {
return internetResponse; return internetResponse;
} catch (RemoteException | InterruptedException e) { } catch (RemoteException | InterruptedException e) {
e.printStackTrace(); LOG.warn("Error downloading data from " + requestedUri, e);
} }
} else { } else {
@ -163,7 +163,7 @@ public class GBWebClient extends WebViewClient {
return new WebResourceResponse("application/json", "utf-8", new ByteArrayInputStream(resp.toString().getBytes())); return new WebResourceResponse("application/json", "utf-8", new ByteArrayInputStream(resp.toString().getBytes()));
} }
} catch (JSONException e) { } catch (JSONException e) {
LOG.warn(e.getMessage()); LOG.warn("Error building the JSON weather message.", e);
} }
return null; return null;

View File

@ -98,7 +98,7 @@ public class JSInterface {
} }
} catch (JSONException e) { } catch (JSONException e) {
LOG.warn(e.getMessage()); LOG.warn("Error building the appmessage JSON object", e);
} }
return null; return null;
} }
@ -113,7 +113,7 @@ public class JSInterface {
//TODO: use real info //TODO: use real info
wi.put("language", "en"); wi.put("language", "en");
} catch (JSONException e) { } catch (JSONException e) {
LOG.warn(e.getMessage()); LOG.warn("Error building the ActiveWathcInfo JSON object", e);
} }
//Json not supported apparently, we need to cast back and forth //Json not supported apparently, we need to cast back and forth
return wi.toString(); return wi.toString();
@ -129,7 +129,7 @@ public class JSInterface {
return "file:///" + configurationFile.getAbsolutePath(); return "file:///" + configurationFile.getAbsolutePath();
} }
} catch (IOException e) { } catch (IOException e) {
LOG.warn(e.getMessage()); LOG.warn("Error loading config file", e);
} }
return null; return null;
} }
@ -144,7 +144,7 @@ public class JSInterface {
} }
} catch (IOException e) { } catch (IOException e) {
GB.toast("Error reading presets", Toast.LENGTH_LONG, GB.ERROR); GB.toast("Error reading presets", Toast.LENGTH_LONG, GB.ERROR);
LOG.warn(e.getMessage()); LOG.warn("Error reading presets", e);
} }
return null; return null;
} }
@ -162,7 +162,7 @@ public class JSInterface {
GB.toast("Presets stored", Toast.LENGTH_SHORT, GB.INFO); GB.toast("Presets stored", Toast.LENGTH_SHORT, GB.INFO);
} catch (IOException e) { } catch (IOException e) {
GB.toast("Error storing presets", Toast.LENGTH_LONG, GB.ERROR); GB.toast("Error storing presets", Toast.LENGTH_LONG, GB.ERROR);
LOG.warn(e.getMessage()); LOG.warn("Error storing presets", e);
} }
} }
@ -185,7 +185,7 @@ public class JSInterface {
} }
return sb.toString().toLowerCase(); return sb.toString().toLowerCase();
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
LOG.warn(e.getMessage()); LOG.warn("Error definining local storage prefix", e);
return prefix; return prefix;
} }
} }

View File

@ -29,6 +29,7 @@ import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
@ -95,6 +96,16 @@ public class WebViewSingleton {
//Internet helper inbound (responses) handler //Internet helper inbound (responses) handler
private static class IncomingHandler extends Handler { private static class IncomingHandler extends Handler {
private String getCharsetFromHeaders(String contentType) {
if (contentType != null && contentType.toLowerCase().trim().contains("charset=")) {
String[] parts = contentType.toLowerCase().trim().split("=");
if (parts.length > 0)
return parts[1];
}
return null;
}
@Override @Override
public void handleMessage(Message msg) { public void handleMessage(Message msg) {
Bundle data = msg.getData(); Bundle data = msg.getData();
@ -105,10 +116,10 @@ public class WebViewSingleton {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
internetResponse = new WebResourceResponse(data.getString("content-type"), data.getString("content-encoding"), 200, "OK", internetResponse = new WebResourceResponse(data.getString("content-type"), data.getString("content-encoding"), 200, "OK",
headers, headers,
new ByteArrayInputStream(data.getString("response").getBytes()) new ByteArrayInputStream(data.getString("response").getBytes(Charset.forName(getCharsetFromHeaders(data.getString("content-type")))))
); );
} else { } else {
internetResponse = new WebResourceResponse(data.getString("content-type"), data.getString("content-encoding"), new ByteArrayInputStream(data.getString("response").getBytes())); internetResponse = new WebResourceResponse(data.getString("content-type"), data.getString("content-encoding"), new ByteArrayInputStream(data.getString("response").getBytes(Charset.forName(getCharsetFromHeaders(data.getString("content-type"))))));
} }
latch.countDown(); latch.countDown();
@ -122,6 +133,9 @@ public class WebViewSingleton {
} }
public static void runJavascriptInterface(GBDevice device, UUID uuid) { public static void runJavascriptInterface(GBDevice device, UUID uuid) {
if (uuid == null && device == null) {
throw new RuntimeException("Javascript interface started without device and uuid");
}
if (uuid.equals(currentRunningUUID)) { if (uuid.equals(currentRunningUUID)) {
LOG.debug("WEBVIEW uuid not changed keeping the old context"); LOG.debug("WEBVIEW uuid not changed keeping the old context");
} else { } else {
@ -221,7 +235,7 @@ public class WebViewSingleton {
return json.getJSONObject("appKeys"); return json.getJSONObject("appKeys");
} }
} catch (IOException | JSONException e) { } catch (IOException | JSONException e) {
LOG.warn(e.getMessage()); LOG.warn("Unable to parse configuration JSON file", e);
} }
return null; return null;
} }
@ -232,7 +246,7 @@ public class WebViewSingleton {
JSONObject knownKeys = getAppConfigurationKeys(uuid); JSONObject knownKeys = getAppConfigurationKeys(uuid);
SparseArray<String> appKeysMap = new SparseArray<>(); SparseArray<String> appKeysMap = new SparseArray<>();
if (knownKeys == null) { if (knownKeys == null || msg == null) {
return "{}"; return "{}";
} }
@ -267,7 +281,7 @@ public class WebViewSingleton {
jsAppMessage.put("payload", outgoing); jsAppMessage.put("payload", outgoing);
} catch (Exception e) { } catch (Exception e) {
LOG.warn(e.getMessage()); LOG.warn("Unable to parse incoming app message", e);
} }
return jsAppMessage.toString(); return jsAppMessage.toString();
} }