From 11eca93ea2c3e34746d0db3252b38b8f58a4707c Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sat, 19 Oct 2024 19:01:54 +0200 Subject: [PATCH] Add option to force-stop app before starting The previous commit introduced: scrcpy --start-app=name By adding a '+' prefix, the app is stopped beforehand: scrcpy --start-app=+name This is especially useful to start it on a new virtual display: scrcpy --new-display --start-app=+firefox --- app/scrcpy.1 | 4 ++++ app/src/cli.c | 4 +++- .../java/com/genymobile/scrcpy/control/Controller.java | 7 ++++++- .../src/main/java/com/genymobile/scrcpy/device/Device.java | 5 ++++- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/scrcpy.1 b/app/scrcpy.1 index 01d7b2ff8c..1137703a38 100644 --- a/app/scrcpy.1 +++ b/app/scrcpy.1 @@ -497,6 +497,10 @@ Default is "lalt,lsuper" (left-Alt or left-Super). .BI "\-\-start\-app " name Start an Android app, by its exact package name or the start of its name (case-insensitive). +Add a '+' prefix to force-stop before starting the app: + + scrcpy --new-display --start-app=+firefox + .TP .B \-t, \-\-show\-touches Enable "show touches" on start, restore the initial value on exit. diff --git a/app/src/cli.c b/app/src/cli.c index 9f650d9204..088e7cc5dc 100644 --- a/app/src/cli.c +++ b/app/src/cli.c @@ -811,7 +811,9 @@ static const struct sc_option options[] = { .longopt = "start-app", .argdesc = "name", .text = "Start an Android app, by its exact package name or the start " - "of its name (case-insensitive).", + "of its name (case-insensitive).\n" + "Add a '+' prefix to force-stop before starting the app:\n" + " scrcpy --new-display --start-app=+firefox", }, { .shortopt = 't', diff --git a/server/src/main/java/com/genymobile/scrcpy/control/Controller.java b/server/src/main/java/com/genymobile/scrcpy/control/Controller.java index 3305f4da42..b32a4ca330 100644 --- a/server/src/main/java/com/genymobile/scrcpy/control/Controller.java +++ b/server/src/main/java/com/genymobile/scrcpy/control/Controller.java @@ -584,6 +584,11 @@ private int getActionDisplayId() { } private void startApp(String name) { + boolean forceStopBeforeStart = name.startsWith("+"); + if (forceStopBeforeStart) { + name = name.substring(1); + } + List apps = Device.findApps(name); if (apps.isEmpty()) { Ln.w("No app found for name \"" + name + "\""); @@ -604,7 +609,7 @@ private void startApp(String name) { } Ln.i("Starting app \"" + app.getName() + "\" [" + app.getPackageName() + "] on display " + startAppDisplayId + "..."); - Device.startApp(app.getPackageName(), startAppDisplayId); + Device.startApp(app.getPackageName(), startAppDisplayId, forceStopBeforeStart); } private int getStartAppDisplayId() { diff --git a/server/src/main/java/com/genymobile/scrcpy/device/Device.java b/server/src/main/java/com/genymobile/scrcpy/device/Device.java index f9acb91e7a..789d7fe024 100644 --- a/server/src/main/java/com/genymobile/scrcpy/device/Device.java +++ b/server/src/main/java/com/genymobile/scrcpy/device/Device.java @@ -270,7 +270,7 @@ private static List findStartingByName(PackageManager pm, List