mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-11-24 10:56:50 +01:00
Pebble: background webview, address (most of) review comments
This commit is contained in:
parent
b0c723b68a
commit
c4f36d1202
@ -71,6 +71,7 @@ public class ExternalPebbleJSActivity extends AbstractGBActivity {
|
||||
} else if (extras.getBoolean(START_BG_WEBVIEW, false)) {
|
||||
WebViewSingleton.ensureCreated(this);
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
@ -76,7 +76,7 @@ public class GBWebClient extends WebViewClient {
|
||||
return internetResponse;
|
||||
|
||||
} catch (RemoteException | InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
LOG.warn("Error downloading data from " + requestedUri, e);
|
||||
}
|
||||
|
||||
} else {
|
||||
@ -163,7 +163,7 @@ public class GBWebClient extends WebViewClient {
|
||||
return new WebResourceResponse("application/json", "utf-8", new ByteArrayInputStream(resp.toString().getBytes()));
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
LOG.warn(e.getMessage());
|
||||
LOG.warn("Error building the JSON weather message.", e);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
@ -98,7 +98,7 @@ public class JSInterface {
|
||||
}
|
||||
|
||||
} catch (JSONException e) {
|
||||
LOG.warn(e.getMessage());
|
||||
LOG.warn("Error building the appmessage JSON object", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -113,7 +113,7 @@ public class JSInterface {
|
||||
//TODO: use real info
|
||||
wi.put("language", "en");
|
||||
} 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
|
||||
return wi.toString();
|
||||
@ -129,7 +129,7 @@ public class JSInterface {
|
||||
return "file:///" + configurationFile.getAbsolutePath();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
LOG.warn(e.getMessage());
|
||||
LOG.warn("Error loading config file", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -144,7 +144,7 @@ public class JSInterface {
|
||||
}
|
||||
} catch (IOException e) {
|
||||
GB.toast("Error reading presets", Toast.LENGTH_LONG, GB.ERROR);
|
||||
LOG.warn(e.getMessage());
|
||||
LOG.warn("Error reading presets", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -162,7 +162,7 @@ public class JSInterface {
|
||||
GB.toast("Presets stored", Toast.LENGTH_SHORT, GB.INFO);
|
||||
} catch (IOException e) {
|
||||
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();
|
||||
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
|
||||
LOG.warn(e.getMessage());
|
||||
LOG.warn("Error definining local storage prefix", e);
|
||||
return prefix;
|
||||
}
|
||||
}
|
||||
|
@ -29,6 +29,7 @@ import org.slf4j.LoggerFactory;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
@ -95,6 +96,16 @@ public class WebViewSingleton {
|
||||
|
||||
//Internet helper inbound (responses) 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
|
||||
public void handleMessage(Message msg) {
|
||||
Bundle data = msg.getData();
|
||||
@ -105,10 +116,10 @@ public class WebViewSingleton {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
internetResponse = new WebResourceResponse(data.getString("content-type"), data.getString("content-encoding"), 200, "OK",
|
||||
headers,
|
||||
new ByteArrayInputStream(data.getString("response").getBytes())
|
||||
new ByteArrayInputStream(data.getString("response").getBytes(Charset.forName(getCharsetFromHeaders(data.getString("content-type")))))
|
||||
);
|
||||
} 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();
|
||||
@ -122,6 +133,9 @@ public class WebViewSingleton {
|
||||
}
|
||||
|
||||
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)) {
|
||||
LOG.debug("WEBVIEW uuid not changed keeping the old context");
|
||||
} else {
|
||||
@ -221,7 +235,7 @@ public class WebViewSingleton {
|
||||
return json.getJSONObject("appKeys");
|
||||
}
|
||||
} catch (IOException | JSONException e) {
|
||||
LOG.warn(e.getMessage());
|
||||
LOG.warn("Unable to parse configuration JSON file", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -232,7 +246,7 @@ public class WebViewSingleton {
|
||||
JSONObject knownKeys = getAppConfigurationKeys(uuid);
|
||||
SparseArray<String> appKeysMap = new SparseArray<>();
|
||||
|
||||
if (knownKeys == null) {
|
||||
if (knownKeys == null || msg == null) {
|
||||
return "{}";
|
||||
}
|
||||
|
||||
@ -267,7 +281,7 @@ public class WebViewSingleton {
|
||||
jsAppMessage.put("payload", outgoing);
|
||||
|
||||
} catch (Exception e) {
|
||||
LOG.warn(e.getMessage());
|
||||
LOG.warn("Unable to parse incoming app message", e);
|
||||
}
|
||||
return jsAppMessage.toString();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user