Run a main looper in the cleanup process

Since a main looper is explicitly run in the main process, the
initialization of workarounds no longer calls
Looper.prepareMainLooper(), leading to a crash:

    java.lang.RuntimeException: Can't create handler inside thread
    Thread[main,5,main] that has not called Looper.prepare()

As a result, --power-off-on-close was broken.

Refs 283326b2f6
Fixes #6146 <https://github.com/Genymobile/scrcpy/issues/6146>
This commit is contained in:
Romain Vimont 2025-06-12 09:03:39 +02:00
commit 4e1cf13a50

View file

@ -7,6 +7,7 @@ import com.genymobile.scrcpy.util.SettingsException;
import com.genymobile.scrcpy.wrappers.ServiceManager;
import android.os.BatteryManager;
import android.os.Looper;
import android.system.ErrnoException;
import android.system.Os;
@ -179,6 +180,11 @@ public final class CleanUp {
}
}
@SuppressWarnings("deprecation")
private static void prepareMainLooper() {
Looper.prepareMainLooper();
}
public static void main(String... args) {
try {
// Start a new session to avoid being terminated along with the server process on some devices
@ -188,6 +194,9 @@ public final class CleanUp {
}
unlinkSelf();
// Needed for workarounds
prepareMainLooper();
int displayId = Integer.parseInt(args[0]);
int restoreStayOn = Integer.parseInt(args[1]);
boolean disableShowTouches = Boolean.parseBoolean(args[2]);