Adapt clean up for installed APK

The cleanup process is slightly different when the server is installed
rather than pushed.
This commit is contained in:
Romain Vimont 2022-07-03 01:15:28 +02:00
commit b4e714f77d
4 changed files with 37 additions and 10 deletions

View file

@ -316,6 +316,10 @@ execute_server(struct sc_server *server,
// By default, power_on is true // By default, power_on is true
ADD_PARAM("power_on=false"); ADD_PARAM("power_on=false");
} }
if (params->install) {
// By default, installed is false
ADD_PARAM("installed=true");
}
#undef ADD_PARAM #undef ADD_PARAM

View file

@ -35,6 +35,8 @@ public final class CleanUp {
private static final int FLAG_RESTORE_NORMAL_POWER_MODE = 2; private static final int FLAG_RESTORE_NORMAL_POWER_MODE = 2;
private static final int FLAG_POWER_OFF_SCREEN = 4; private static final int FLAG_POWER_OFF_SCREEN = 4;
private boolean installed;
private int displayId; private int displayId;
// Restore the value (between 0 and 7), -1 to not restore // Restore the value (between 0 and 7), -1 to not restore
@ -50,6 +52,7 @@ public final class CleanUp {
} }
protected Config(Parcel in) { protected Config(Parcel in) {
installed = in.readInt() != 0;
displayId = in.readInt(); displayId = in.readInt();
restoreStayOn = in.readInt(); restoreStayOn = in.readInt();
byte options = in.readByte(); byte options = in.readByte();
@ -60,6 +63,7 @@ public final class CleanUp {
@Override @Override
public void writeToParcel(Parcel dest, int flags) { public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(installed ? 1 : 0);
dest.writeInt(displayId); dest.writeInt(displayId);
dest.writeInt(restoreStayOn); dest.writeInt(restoreStayOn);
byte options = 0; byte options = 0;
@ -114,9 +118,10 @@ public final class CleanUp {
// not instantiable // not instantiable
} }
public static void configure(int displayId, int restoreStayOn, boolean disableShowTouches, boolean restoreNormalPowerMode, boolean powerOffScreen) public static void configure(boolean installed, int displayId, int restoreStayOn, boolean disableShowTouches, boolean restoreNormalPowerMode,
throws IOException { boolean powerOffScreen) throws IOException {
Config config = new Config(); Config config = new Config();
config.installed = installed;
config.displayId = displayId; config.displayId = displayId;
config.disableShowTouches = disableShowTouches; config.disableShowTouches = disableShowTouches;
config.restoreStayOn = restoreStayOn; config.restoreStayOn = restoreStayOn;
@ -125,8 +130,9 @@ public final class CleanUp {
if (config.hasWork()) { if (config.hasWork()) {
startProcess(config); startProcess(config);
} else { } else if (!installed) {
// There is no additional clean up to do when scrcpy dies // There is no additional clean up to do when scrcpy dies.
// If the APK has been pushed to /data/local/tmp, remove it.
unlinkSelf(); unlinkSelf();
} }
} }
@ -135,7 +141,8 @@ public final class CleanUp {
String[] cmd = {"app_process", "/", CleanUp.class.getName(), config.toBase64()}; String[] cmd = {"app_process", "/", CleanUp.class.getName(), config.toBase64()};
ProcessBuilder builder = new ProcessBuilder(cmd); ProcessBuilder builder = new ProcessBuilder(cmd);
builder.environment().put("CLASSPATH", SERVER_PATH); String serverPath = config.installed ? Device.getInstalledApkPath() : SERVER_PATH;
builder.environment().put("CLASSPATH", serverPath);
builder.start(); builder.start();
} }
@ -148,7 +155,12 @@ public final class CleanUp {
} }
public static void main(String... args) { public static void main(String... args) {
unlinkSelf(); Config config = Config.fromBase64(args[0]);
if (!config.installed) {
// If the APK has been pushed to /data/local/tmp, remove it.
unlinkSelf();
}
try { try {
// Wait for the server to die // Wait for the server to die
@ -159,8 +171,6 @@ public final class CleanUp {
Ln.i("Cleaning up"); Ln.i("Cleaning up");
Config config = Config.fromBase64(args[0]);
if (config.disableShowTouches || config.restoreStayOn != -1) { if (config.disableShowTouches || config.restoreStayOn != -1) {
if (config.disableShowTouches) { if (config.disableShowTouches) {
Ln.i("Disabling \"show touches\""); Ln.i("Disabling \"show touches\"");

View file

@ -23,6 +23,7 @@ public class Options {
private boolean downsizeOnError = true; private boolean downsizeOnError = true;
private boolean cleanup = true; private boolean cleanup = true;
private boolean powerOn = true; private boolean powerOn = true;
private boolean installed = false;
// Options not used by the scrcpy client, but useful to use scrcpy-server directly // Options not used by the scrcpy client, but useful to use scrcpy-server directly
private boolean sendDeviceMeta = true; // send device name and size private boolean sendDeviceMeta = true; // send device name and size
@ -196,4 +197,12 @@ public class Options {
public void setSendDummyByte(boolean sendDummyByte) { public void setSendDummyByte(boolean sendDummyByte) {
this.sendDummyByte = sendDummyByte; this.sendDummyByte = sendDummyByte;
} }
public boolean getInstalled() {
return installed;
}
public void setInstalled(boolean installed) {
this.installed = installed;
}
} }

View file

@ -51,8 +51,8 @@ public final class Server {
if (options.getCleanup()) { if (options.getCleanup()) {
try { try {
CleanUp.configure(options.getDisplayId(), restoreStayOn, mustDisableShowTouchesOnCleanUp, restoreNormalPowerMode, CleanUp.configure(options.getInstalled(), options.getDisplayId(), restoreStayOn, mustDisableShowTouchesOnCleanUp,
options.getPowerOffScreenOnClose()); restoreNormalPowerMode, options.getPowerOffScreenOnClose());
} catch (IOException e) { } catch (IOException e) {
Ln.e("Could not configure cleanup", e); Ln.e("Could not configure cleanup", e);
} }
@ -251,6 +251,10 @@ public final class Server {
boolean powerOn = Boolean.parseBoolean(value); boolean powerOn = Boolean.parseBoolean(value);
options.setPowerOn(powerOn); options.setPowerOn(powerOn);
break; break;
case "installed":
boolean installed = Boolean.parseBoolean(value);
options.setInstalled(installed);
break;
case "send_device_meta": case "send_device_meta":
boolean sendDeviceMeta = Boolean.parseBoolean(value); boolean sendDeviceMeta = Boolean.parseBoolean(value);
options.setSendDeviceMeta(sendDeviceMeta); options.setSendDeviceMeta(sendDeviceMeta);