Skip to content

Commit

Permalink
Merge pull request #3438 from deltachat/adb/webxdc-deeplink
Browse files Browse the repository at this point in the history
allow to open deep-link to webxdc
  • Loading branch information
adbenitez authored Nov 23, 2024
2 parents 1a5e062 + ea89d37 commit 0b2a26e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 13 deletions.
9 changes: 9 additions & 0 deletions jni/dc_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -1565,6 +1565,15 @@ JNIEXPORT jstring Java_com_b44t_messenger_DcMsg_getWebxdcInfoJson(JNIEnv *env, j
}


JNIEXPORT jstring Java_com_b44t_messenger_DcMsg_getWebxdcHref(JNIEnv *env, jobject obj)
{
char* temp = dc_msg_get_webxdc_href(get_dc_msg(env, obj));
jstring ret = JSTRING_NEW(temp);
dc_str_unref(temp);
return ret;
}


JNIEXPORT jboolean Java_com_b44t_messenger_DcMsg_isForwarded(JNIEnv *env, jobject obj)
{
return dc_msg_is_forwarded(get_dc_msg(env, obj))!=0;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/b44t/messenger/DcMsg.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public JSONObject getWebxdcInfo () {
return new JSONObject();
}
}
public native String getWebxdcHref ();
public native boolean isForwarded ();
public native boolean isInfo ();
public native boolean isSetupMessage ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ else if(DozeReminder.isDozeReminderMsg(getContext(), messageRecord)) {
DozeReminder.dozeReminderTapped(getContext());
}
else if(messageRecord.getInfoType() == DcMsg.DC_INFO_WEBXDC_INFO_MESSAGE) {
scrollMaybeSmoothToMsgId(messageRecord.getParent().getId());
WebxdcActivity.openWebxdcActivity(getContext(), messageRecord.getParent(), messageRecord.getWebxdcHref());
}
else {
String self_mail = dcContext.getConfig("configured_mail_user");
Expand Down
31 changes: 24 additions & 7 deletions src/main/java/org/thoughtcrime/securesms/WebxdcActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.b44t.messenger.DcMsg;
import com.b44t.messenger.rpc.Rpc;
import com.b44t.messenger.rpc.RpcException;
import com.google.common.base.Charsets;

import org.json.JSONObject;
import org.thoughtcrime.securesms.connect.AccountManager;
Expand All @@ -53,6 +54,8 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -62,6 +65,7 @@ public class WebxdcActivity extends WebViewActivity implements DcEventCenter.DcE
private static final String EXTRA_ACCOUNT_ID = "accountId";
private static final String EXTRA_APP_MSG_ID = "appMessageId";
private static final String EXTRA_HIDE_ACTION_BAR = "hideActionBar";
private static final String EXTRA_HREF = "href";
private static final int REQUEST_CODE_FILE_PICKER = 51426;

private ValueCallback<Uri[]> filePathCallback;
Expand Down Expand Up @@ -91,29 +95,34 @@ public static void openMaps(Context context, int chatId) {
return;
}
}
openWebxdcActivity(context, msgId, true);
openWebxdcActivity(context, msgId, true, "");
}

public static void openWebxdcActivity(Context context, DcMsg instance) {
openWebxdcActivity(context, instance.getId(), false);
openWebxdcActivity(context, instance, "");
}

public static void openWebxdcActivity(Context context, int msgId, boolean hideActionBar) {
public static void openWebxdcActivity(Context context, DcMsg instance, String href) {
openWebxdcActivity(context, instance.getId(), false, href);
}

public static void openWebxdcActivity(Context context, int msgId, boolean hideActionBar, String href) {
if (!Util.isClickedRecently()) {
if (Prefs.isDeveloperModeEnabled(context)) {
WebView.setWebContentsDebuggingEnabled(true);
}
context.startActivity(getWebxdcIntent(context, msgId, hideActionBar));
context.startActivity(getWebxdcIntent(context, msgId, hideActionBar, href));
}
}

private static Intent getWebxdcIntent(Context context, int msgId, boolean hideActionBar) {
private static Intent getWebxdcIntent(Context context, int msgId, boolean hideActionBar, String href) {
DcContext dcContext = DcHelper.getContext(context);
Intent intent = new Intent(context, WebxdcActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.putExtra(EXTRA_ACCOUNT_ID, dcContext.getAccountId());
intent.putExtra(EXTRA_APP_MSG_ID, msgId);
intent.putExtra(EXTRA_HIDE_ACTION_BAR, hideActionBar);
intent.putExtra(EXTRA_HREF, href);
return intent;
}

Expand All @@ -124,7 +133,7 @@ private static Intent[] getWebxdcIntentWithParentStack(Context context, int msgI
.putExtra(ConversationActivity.CHAT_ID_EXTRA, dcContext.getMsg(msgId).getChatId())
.setAction(Intent.ACTION_VIEW);

final Intent webxdcIntent = getWebxdcIntent(context, msgId, false);
final Intent webxdcIntent = getWebxdcIntent(context, msgId, false, "");

return TaskStackBuilder.create(context)
.addNextIntentWithParentStack(chatIntent)
Expand Down Expand Up @@ -205,7 +214,15 @@ public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathC
webView.setNetworkAvailable(internetAccess); // this does not block network but sets `window.navigator.isOnline` in js land
webView.addJavascriptInterface(new InternalJSApi(), "InternalJSApi");

webView.loadUrl(this.baseURL + (internetAccess? "/index.html" : "/webxdc_bootstrap324567869.html"));
String href = b.getString(EXTRA_HREF, "");
String encodedHref = "";
try {
encodedHref = URLEncoder.encode(href, Charsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

webView.loadUrl(this.baseURL + "/webxdc_bootstrap324567869.html?i=" + (internetAccess? "1" : "0") + "&href=" + encodedHref);

Util.runOnAnyBackgroundThread(() -> {
final DcChat chat = dcContext.getChat(dcAppMsg.getChatId());
Expand Down
28 changes: 23 additions & 5 deletions src/main/res/raw/webxdc_wrapper.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@
style="display: none"
></iframe>
<script>
const thisUnchangable = (async () => {
const loadingDiv = document.getElementById("loading");
const iframe = document.getElementById("frame");

let fill500 = async (href) => {
const connections = [];
const loadingProgress = document.getElementById("progress");
const loadingDiv = document.getElementById("loading");
const iframe = document.getElementById("frame");
const isolatedContextTest = document.getElementById(
"test-isolated-sandbox-context"
);
Expand Down Expand Up @@ -131,7 +132,7 @@
} else {
loadingDiv.innerHTML = "";
iframe.style.display = 'block';
iframe.src = "index.html";
iframe.src = href;
}
}

Expand All @@ -140,7 +141,24 @@
return connections.length;
},
});
})();
};

const params = new URLSearchParams(document.location.search);
const internetAccess = (params.get("i") || "0") === "1";
let href = params.get("href") || "";
if (!href || href.startsWith("#")) {
href = "index.html" + href;
}

if (internetAccess) { // fill500 not needed, just load the frame
fill500 = (href) => {
loadingDiv.innerHTML = "";
iframe.style.display = 'block';
iframe.src = href;
}
}

const thisUnchangable = fill500(href)
</script>
</body>
</html>

0 comments on commit 0b2a26e

Please sign in to comment.