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().
This commit is contained in:
B. Petersen
2022-05-17 16:10:53 +02:00
committed by bjoern
parent cae4536d20
commit 3aa30899a5
3 changed files with 34 additions and 3 deletions
+8
View File
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="@string/source_code"
android:id="@+id/source_code" />
</menu>
+1
View File
@@ -258,6 +258,7 @@
<string name="unpin">Unpin</string>
<string name="ConversationFragment_quoted_message_not_found">Original message not found</string>
<string name="reply_privately">Reply Privately</string>
<string name="source_code">Source Code</string>
<string name="mute_for_one_hour">Mute for 1 hour</string>
<string name="mute_for_two_hours">Mute for 2 hours</string>
@@ -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();
}
});
});
}