Merge remote-tracking branch 'upstream/master'

This commit is contained in:
adbenitez
2022-09-11 00:06:54 -04:00
3 changed files with 38 additions and 20 deletions
@@ -48,8 +48,7 @@ public class WebxdcActivity extends WebViewActivity implements DcEventCenter.DcE
private DcMsg dcAppMsg;
private String baseURL;
private String sourceCodeUrl = "";
private boolean internetAccess = false;
public static void openWebxdcActivity(Context context, DcMsg instance) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
@@ -116,10 +115,13 @@ public class WebxdcActivity extends WebViewActivity implements DcEventCenter.DcE
// also a random-id is not that useful for debugging)
this.baseURL = "https://acc" + dcContext.getAccountId() + "-msg" + appMessageId + ".localhost";
final JSONObject info = this.dcAppMsg.getWebxdcInfo();
internetAccess = JsonUtils.optBoolean(info, "internet_access");
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowFileAccess(false);
webSettings.setBlockNetworkLoads(true);
webSettings.setBlockNetworkLoads(!internetAccess);
webSettings.setAllowContentAccess(false);
webSettings.setGeolocationEnabled(false);
webSettings.setAllowFileAccessFromFileURLs(false);
@@ -129,7 +131,13 @@ public class WebxdcActivity extends WebViewActivity implements DcEventCenter.DcE
webView.addJavascriptInterface(new InternalJSApi(), "InternalJSApi");
webView.loadUrl(this.baseURL + "/index.html");
updateTitleAndMenu();
Util.runOnAnyBackgroundThread(() -> {
final DcChat chat = dcContext.getChat(dcAppMsg.getChatId());
Util.runOnMain(() -> {
updateTitleAndMenu(info, chat);
});
});
}
@Override
@@ -189,6 +197,9 @@ public class WebxdcActivity extends WebViewActivity implements DcEventCenter.DcE
} else {
byte[] blob = this.dcAppMsg.getWebxdcBlob(path);
if (blob == null) {
if (internetAccess) {
return null; // do not intercept request
}
throw new Exception("\"" + path + "\" not found");
}
String ext = MediaUtil.getFileExtensionFromUrl(path);
@@ -217,26 +228,25 @@ public class WebxdcActivity extends WebViewActivity implements DcEventCenter.DcE
Log.i(TAG, "handleEvent");
webView.loadUrl("javascript:window.__webxdcUpdate();");
} else if ((eventId == DcContext.DC_EVENT_MSGS_CHANGED && event.getData2Int() == dcAppMsg.getId())) {
updateTitleAndMenu();
Util.runOnAnyBackgroundThread(() -> {
final JSONObject info = dcAppMsg.getWebxdcInfo();
final DcChat chat = dcContext.getChat(dcAppMsg.getChatId());
Util.runOnMain(() -> {
updateTitleAndMenu(info, chat);
});
});
}
}
private void updateTitleAndMenu() {
Util.runOnAnyBackgroundThread(() -> {
final JSONObject info = this.dcAppMsg.getWebxdcInfo();
private void updateTitleAndMenu(JSONObject info, DcChat chat) {
final String docName = JsonUtils.optString(info, "document");
final String xdcName = JsonUtils.optString(info, "name");
final String chatName = WebxdcActivity.this.dcContext.getChat(WebxdcActivity.this.dcAppMsg.getChatId()).getName();
final String currSourceCodeUrl = JsonUtils.optString(info, "source_code_url");
Util.runOnMain(() -> {
getSupportActionBar().setTitle((docName.isEmpty() ? xdcName : docName) + " " + chatName);
if (!sourceCodeUrl.equals(currSourceCodeUrl)) {
sourceCodeUrl = currSourceCodeUrl;
invalidateOptionsMenu();
}
});
});
getSupportActionBar().setTitle((docName.isEmpty() ? xdcName : docName) + " " + chat.getName());
if (!sourceCodeUrl.equals(currSourceCodeUrl)) {
sourceCodeUrl = currSourceCodeUrl;
invalidateOptionsMenu();
}
}
public static void addToHomeScreen(Activity activity, int msgId) {
@@ -255,7 +265,7 @@ public class WebxdcActivity extends WebViewActivity implements DcEventCenter.DcE
ShortcutInfoCompat shortcutInfoCompat = new ShortcutInfoCompat.Builder(context, "xdc-" + dcContext.getAccountId() + "-" + msgId)
.setShortLabel(docName.isEmpty() ? xdcName : docName)
.setIcon(IconCompat.createWithAdaptiveBitmap(bitmap))
.setIcon(IconCompat.createWithBitmap(bitmap)) // createWithAdaptiveBitmap() removes decorations but cuts out a too small circle and defamiliarize the icon too much
.setIntents(getWebxdcIntentWithParentStack(context, msgId))
.build();
@@ -54,6 +54,14 @@ public class JsonUtils {
}
}
public static boolean optBoolean(JSONObject obj, String name) {
try {
return obj.optBoolean(name);
} catch(Exception e) {
return false;
}
}
public static class SaneJSONObject {
private final JSONObject delegate;