From 5925a7e7cbbd9c615253c234e66154d435b4f414 Mon Sep 17 00:00:00 2001 From: Twaik Yont <9674930+twaik@users.noreply.github.com> Date: Wed, 29 Jan 2025 09:48:24 +0200 Subject: [PATCH] fix: keep splashimage shown until being connected if request is sent --- app/src/main/cpp/lorie/activity.c | 7 ++++-- .../main/java/com/termux/x11/LorieView.java | 2 +- .../java/com/termux/x11/MainActivity.java | 25 +++++++++++++++---- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/app/src/main/cpp/lorie/activity.c b/app/src/main/cpp/lorie/activity.c index b4425f022..8daa99c21 100644 --- a/app/src/main/cpp/lorie/activity.c +++ b/app/src/main/cpp/lorie/activity.c @@ -70,8 +70,9 @@ static jclass FindMethodOrDie(JNIEnv *env, jclass clazz, const char* name, const return method; } -static void requestConnection(__unused JNIEnv *env, __unused jclass clazz) { +static jboolean requestConnection(__unused JNIEnv *env, __unused jclass clazz) { #define check(cond, fmt, ...) if ((cond)) do { __android_log_print(ANDROID_LOG_ERROR, "requestConnection", fmt, ## __VA_ARGS__); goto end; } while (0) + bool sent = JNI_FALSE; // We do not want to block GUI thread for a long time so we will set timeout to 20 msec. struct sockaddr_in server = { .sin_family = AF_INET, .sin_port = htons(PORT), .sin_addr.s_addr = inet_addr("127.0.0.1") }; int so_error, sock = socket(AF_INET, SOCK_STREAM, 0); @@ -91,12 +92,14 @@ static void requestConnection(__unused JNIEnv *env, __unused jclass clazz) { check(so_error != 0, "Connection failed: %s", strerror(so_error)); check(write(sock, MAGIC, sizeof(MAGIC)) < 0, "failed to send message: %s", strerror(errno)); + sent = JNI_TRUE; goto end; } check(1, "something went wrong: %s, %s", strerror(errno), strerror(r)); end: if (sock >= 0) close(sock); + return sent; #undef errorReturn } @@ -383,7 +386,7 @@ JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) { {"requestStylusEnabled", "(Z)V", (void *)&requestStylusEnabled}, {"sendKeyEvent", "(IIZ)Z", (void *)&sendKeyEvent}, {"sendTextEvent", "([B)V", (void *)&sendTextEvent}, - {"requestConnection", "()V", (void *)&requestConnection}, + {"requestConnection", "()Z", (void *)&requestConnection}, }; (*vm)->AttachCurrentThread(vm, &env, NULL); jclass cls = (*env)->FindClass(env, "com/termux/x11/LorieView"); diff --git a/app/src/main/java/com/termux/x11/LorieView.java b/app/src/main/java/com/termux/x11/LorieView.java index d3022c288..7d743f309 100644 --- a/app/src/main/java/com/termux/x11/LorieView.java +++ b/app/src/main/java/com/termux/x11/LorieView.java @@ -361,7 +361,7 @@ public boolean commitText(CharSequence text, int newCursorPosition) { @FastNative static public native void requestStylusEnabled(boolean enabled); @FastNative public native boolean sendKeyEvent(int scanCode, int keyCode, boolean keyDown); @FastNative public native void sendTextEvent(byte[] text); - @CriticalNative public static native void requestConnection(); + @CriticalNative public static native boolean requestConnection(); static { System.loadLibrary("Xlorie"); diff --git a/app/src/main/java/com/termux/x11/MainActivity.java b/app/src/main/java/com/termux/x11/MainActivity.java index 09b0ac4f5..ccd1ad52b 100644 --- a/app/src/main/java/com/termux/x11/MainActivity.java +++ b/app/src/main/java/com/termux/x11/MainActivity.java @@ -45,6 +45,7 @@ import android.view.View; import android.view.ViewConfiguration; import android.view.ViewGroup; +import android.view.ViewTreeObserver; import android.view.Window; import android.view.WindowInsets; import android.view.inputmethod.InputMethodManager; @@ -123,6 +124,15 @@ public void onReceive(Context context, Intent intent) { } }; + ViewTreeObserver.OnPreDrawListener mOnPredrawListener = new ViewTreeObserver.OnPreDrawListener() { + @Override + public boolean onPreDraw() { + if (LorieView.connected()) + handler.post(() -> findViewById(android.R.id.content).getViewTreeObserver().removeOnPreDrawListener(mOnPredrawListener)); + return false; + } + }; + @SuppressLint("StaticFieldLeak") private static MainActivity instance; @@ -221,7 +231,11 @@ else if (SamsungDexUtils.checkDeXEnabled(this)) mNotification = buildNotification(); mNotificationManager.notify(mNotificationId, mNotification); - tryConnect(); + if (tryConnect()) { + final View content = findViewById(android.R.id.content); + content.getViewTreeObserver().addOnPreDrawListener(mOnPredrawListener); + handler.postDelayed(() -> content.getViewTreeObserver().removeOnPreDrawListener(mOnPredrawListener), 500); + } onPreferencesChanged(""); toggleExtraKeys(false, false); @@ -527,14 +541,14 @@ void onReceiveConnection(Intent intent) { } } - void tryConnect() { + boolean tryConnect() { if (LorieView.connected()) - return; + return false; if (service == null) { - LorieView.requestConnection(); + boolean sent = LorieView.requestConnection(); handler.postDelayed(this::tryConnect, 250); - return; + return true; } try { @@ -553,6 +567,7 @@ void tryConnect() { handler.postDelayed(this::tryConnect, 250); } + return false; } void onPreferencesChanged(String key) {