Skip to content

Commit

Permalink
fix: keep splashimage shown until being connected if request is sent
Browse files Browse the repository at this point in the history
  • Loading branch information
twaik committed Jan 29, 2025
1 parent 904e24d commit 5925a7e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
7 changes: 5 additions & 2 deletions app/src/main/cpp/lorie/activity.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
}

Expand Down Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/termux/x11/LorieView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
25 changes: 20 additions & 5 deletions app/src/main/java/com/termux/x11/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand All @@ -553,6 +567,7 @@ void tryConnect() {

handler.postDelayed(this::tryConnect, 250);
}
return false;
}

void onPreferencesChanged(String key) {
Expand Down

0 comments on commit 5925a7e

Please sign in to comment.