From dd20efa41cd7d1f2220e38a45e0d7413d19f5c09 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 may be useful to start a fresh app on a new virtual display: scrcpy --new-display --start-app=+org.mozilla.firefox PR #5370 --- 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 35abd0d1..802dab5e 100644 --- a/app/scrcpy.1 +++ b/app/scrcpy.1 @@ -498,6 +498,10 @@ Default is "lalt,lsuper" (left-Alt or left-Super). .BI "\-\-start\-app " name Start an Android app, by its exact package name. +Add a '+' prefix to force-stop before starting the app: + + scrcpy --new-display --start-app=+org.mozilla.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 ba272393..d715a385 100644 --- a/app/src/cli.c +++ b/app/src/cli.c @@ -811,7 +811,9 @@ static const struct sc_option options[] = { .longopt_id = OPT_START_APP, .longopt = "start-app", .argdesc = "name", - .text = "Start an Android app, by its exact package name.", + .text = "Start an Android app, by its exact package name.\n" + "Add a '+' prefix to force-stop before starting the app:\n" + " scrcpy --new-display --start-app=+org.mozilla.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 ccdb85e1..b3ab34c3 100644 --- a/server/src/main/java/com/genymobile/scrcpy/control/Controller.java +++ b/server/src/main/java/com/genymobile/scrcpy/control/Controller.java @@ -594,6 +594,11 @@ public class Controller implements AsyncProcessor, VirtualDisplayListener { } private void startApp(String name) { + boolean forceStopBeforeStart = name.startsWith("+"); + if (forceStopBeforeStart) { + name = name.substring(1); + } + DeviceApp app = Device.findByPackageName(name); if (app == null) { Ln.w("No app found for package \"" + name + "\""); @@ -607,7 +612,7 @@ public class Controller implements AsyncProcessor, VirtualDisplayListener { } 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 496865e4..f51a433e 100644 --- a/server/src/main/java/com/genymobile/scrcpy/device/Device.java +++ b/server/src/main/java/com/genymobile/scrcpy/device/Device.java @@ -264,7 +264,7 @@ public final class Device { return null; } - public static void startApp(String packageName, int displayId) { + public static void startApp(String packageName, int displayId, boolean forceStop) { PackageManager pm = FakeContext.get().getPackageManager(); Intent launchIntent = getLaunchIntent(pm, packageName); @@ -283,6 +283,9 @@ public final class Device { } ActivityManager am = ServiceManager.getActivityManager(); + if (forceStop) { + am.forceStopPackage(packageName); + } am.startActivity(launchIntent, options); } }