From 2f35908d0cdcb13140893fba86a587f3a60c1d1e Mon Sep 17 00:00:00 2001 From: Vendicated Date: Fri, 25 Nov 2022 13:05:02 +0100 Subject: [PATCH] Make back gesture close modals and similar --- .../dev/vendicated/vencord/HttpClient.java | 17 ++++++++++--- .../dev/vendicated/vencord/MainActivity.java | 10 +++++--- .../vendicated/vencord/VWebviewClient.java | 3 ++- .../dev/vendicated/vencord/VencordNative.java | 25 +++++++++++++++++++ app/src/main/res/raw/vencord_mobile.js | 12 +++++++++ 5 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/dev/vendicated/vencord/VencordNative.java create mode 100644 app/src/main/res/raw/vencord_mobile.js diff --git a/app/src/main/java/dev/vendicated/vencord/HttpClient.java b/app/src/main/java/dev/vendicated/vencord/HttpClient.java index a58f7e6..352de20 100644 --- a/app/src/main/java/dev/vendicated/vencord/HttpClient.java +++ b/app/src/main/java/dev/vendicated/vencord/HttpClient.java @@ -1,5 +1,8 @@ package dev.vendicated.vencord; +import android.app.Activity; +import android.content.res.Resources; + import androidx.annotation.NonNull; import java.io.*; @@ -39,14 +42,20 @@ public class HttpClient { } } - public static String vencord; + public static String VencordRuntime; + public static String VencordMobileRuntime; - public static void fetchVencord() throws IOException { - if (vencord != null) return; + public static void fetchVencord(Activity activity) throws IOException { + if (VencordRuntime != null) return; + + var res = activity.getResources(); + try (var is = res.openRawResource(R.raw.vencord_mobile)) { + VencordMobileRuntime = readAsText(is); + } var conn = fetch(VENCORD_BUNDLE_URL); try (var is = conn.getInputStream()) { - vencord = readAsText(is); + VencordRuntime = readAsText(is); } } diff --git a/app/src/main/java/dev/vendicated/vencord/MainActivity.java b/app/src/main/java/dev/vendicated/vencord/MainActivity.java index 04cfda3..2813d86 100644 --- a/app/src/main/java/dev/vendicated/vencord/MainActivity.java +++ b/app/src/main/java/dev/vendicated/vencord/MainActivity.java @@ -39,9 +39,11 @@ public class MainActivity extends Activity { s.setJavaScriptEnabled(true); s.setDomStorageEnabled(true); s.setAllowFileAccess(true); - + + wv.addJavascriptInterface(new VencordNative(this, wv), "VencordMobileNative"); + try { - HttpClient.fetchVencord(); + HttpClient.fetchVencord(this); } catch (IOException ex) { Logger.e("Failed to fetch Vencord", ex); return; @@ -52,8 +54,8 @@ public class MainActivity extends Activity { @Override public boolean onKeyDown(int keyCode, KeyEvent event) { - if (keyCode == KeyEvent.KEYCODE_BACK && wv != null && wv.canGoBack()) { - wv.goBack(); + if (keyCode == KeyEvent.KEYCODE_BACK && wv != null) { + runOnUiThread(() -> wv.evaluateJavascript("VencordMobile.onBackPress()", null)); return true; } return super.onKeyDown(keyCode, event); diff --git a/app/src/main/java/dev/vendicated/vencord/VWebviewClient.java b/app/src/main/java/dev/vendicated/vencord/VWebviewClient.java index 2bff9cd..1cd46c9 100644 --- a/app/src/main/java/dev/vendicated/vencord/VWebviewClient.java +++ b/app/src/main/java/dev/vendicated/vencord/VWebviewClient.java @@ -18,7 +18,8 @@ public class VWebviewClient extends WebViewClient { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { - view.evaluateJavascript(HttpClient.vencord, null); + view.evaluateJavascript(HttpClient.VencordRuntime, null); + view.evaluateJavascript(HttpClient.VencordMobileRuntime, null); } @Nullable diff --git a/app/src/main/java/dev/vendicated/vencord/VencordNative.java b/app/src/main/java/dev/vendicated/vencord/VencordNative.java new file mode 100644 index 0000000..485b372 --- /dev/null +++ b/app/src/main/java/dev/vendicated/vencord/VencordNative.java @@ -0,0 +1,25 @@ +package dev.vendicated.vencord; + +import android.app.Activity; +import android.webkit.JavascriptInterface; +import android.webkit.WebView; + +public class VencordNative { + private final WebView wv; + private final Activity activity; + + public VencordNative(Activity activity, WebView wv) { + this.activity = activity; + this.wv = wv; + } + + @JavascriptInterface + public void goBack() { + activity.runOnUiThread(() -> { + if (wv.canGoBack()) + wv.goBack(); + else + activity.getActionBar(); + }); + } +} diff --git a/app/src/main/res/raw/vencord_mobile.js b/app/src/main/res/raw/vencord_mobile.js new file mode 100644 index 0000000..d43c572 --- /dev/null +++ b/app/src/main/res/raw/vencord_mobile.js @@ -0,0 +1,12 @@ +!(() => { +const ModalEscapeHandler = Vencord.Util.lazyWebpack(m => m.binds?.[0] === "esc" && m.binds.length === 1); +const EscapeHandler = Vencord.Util.lazyWebpack(m => m.binds?.[0] === "esc" && m.binds[1] === "shift+pagedown"); +window.VencordMobile = { + onBackPress() { + // false implies modal closed + if (ModalEscapeHandler.action() === false) return; + + EscapeHandler.action({target:document.activeElement}); + } +} +})(); \ No newline at end of file