From 3aa30899a53543cec217057c7a79d838ea9faa52 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Tue, 17 May 2022 16:10:53 +0200 Subject: [PATCH] add 'Source Code' item to webxdc menu the code is a tiny bit more complicated as we keep the call to get_webxdc_info() in background and also avoid a second call to get_webxdc_info(). --- res/menu/webxdc.xml | 8 ++++++ res/values/strings.xml | 1 + .../securesms/WebxdcActivity.java | 28 +++++++++++++++++-- 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 res/menu/webxdc.xml diff --git a/res/menu/webxdc.xml b/res/menu/webxdc.xml new file mode 100644 index 000000000..d119a5c72 --- /dev/null +++ b/res/menu/webxdc.xml @@ -0,0 +1,8 @@ + + + + + + + diff --git a/res/values/strings.xml b/res/values/strings.xml index 0c5778a0f..93695152c 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -258,6 +258,7 @@ Unpin Original message not found Reply Privately + Source Code Mute for 1 hour Mute for 2 hours diff --git a/src/org/thoughtcrime/securesms/WebxdcActivity.java b/src/org/thoughtcrime/securesms/WebxdcActivity.java index 90176dc3d..f72ea2e66 100644 --- a/src/org/thoughtcrime/securesms/WebxdcActivity.java +++ b/src/org/thoughtcrime/securesms/WebxdcActivity.java @@ -8,6 +8,7 @@ import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.Menu; +import android.view.MenuItem; import android.webkit.JavascriptInterface; import android.webkit.MimeTypeMap; import android.webkit.WebResourceResponse; @@ -37,6 +38,7 @@ public class WebxdcActivity extends WebViewActivity implements DcEventCenter.DcE private DcContext dcContext; private DcMsg dcAppMsg; private String baseURL; + private boolean hasSourceCodeUrl; public static void openWebxdcActivity(Context context, DcMsg instance) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { @@ -82,7 +84,7 @@ public class WebxdcActivity extends WebViewActivity implements DcEventCenter.DcE webView.addJavascriptInterface(new InternalJSApi(), "InternalJSApi"); webView.loadUrl(this.baseURL + "/index.html"); - updateTitle(); + updateTitleAndMenu(); } @Override @@ -94,9 +96,23 @@ public class WebxdcActivity extends WebViewActivity implements DcEventCenter.DcE @Override public boolean onPrepareOptionsMenu(Menu menu) { // do not call super.onPrepareOptionsMenu() as the default "Search" menu is not needed + menu.clear(); + this.getMenuInflater().inflate(R.menu.webxdc, menu); + menu.findItem(R.id.source_code).setVisible(hasSourceCodeUrl); return true; } + @Override + public boolean onOptionsItemSelected(MenuItem item) { + super.onOptionsItemSelected(item); + switch (item.getItemId()) { + case R.id.source_code: + openUrlInBrowser(this, JsonUtils.optString(dcAppMsg.getWebxdcInfo(), "source_code_url")); + return true; + } + return false; + } + @Override protected boolean openOnlineUrl(String url) { if (url.startsWith(baseURL +"/")) { @@ -149,18 +165,24 @@ 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())) { - updateTitle(); + updateTitleAndMenu(); } } - private void updateTitle() { + private void updateTitleAndMenu() { Util.runOnAnyBackgroundThread(() -> { final JSONObject info = this.dcAppMsg.getWebxdcInfo(); 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 boolean currHasSourceCodeUrl = !JsonUtils.optString(info, "source_code_url").isEmpty(); + Util.runOnMain(() -> { getSupportActionBar().setTitle((docName.isEmpty() ? xdcName : docName) + " – " + chatName); + if (hasSourceCodeUrl != currHasSourceCodeUrl) { + hasSourceCodeUrl = currHasSourceCodeUrl; + invalidateOptionsMenu(); + } }); }); }