Add hookScript ability to the server

This commit is contained in:
brunoais 2022-04-16 14:46:52 +01:00
parent 3e69f696c1
commit d5156dfb4f
3 changed files with 50 additions and 5 deletions

View file

@ -33,9 +33,9 @@ public final class CleanUp {
}
};
private static final int FLAG_DISABLE_SHOW_TOUCHES = 1;
private static final int FLAG_RESTORE_NORMAL_POWER_MODE = 2;
private static final int FLAG_POWER_OFF_SCREEN = 4;
private static final int FLAG_DISABLE_SHOW_TOUCHES = 1 << 0;
private static final int FLAG_RESTORE_NORMAL_POWER_MODE = 1 << 1;
private static final int FLAG_POWER_OFF_SCREEN = 1 << 2;
private int displayId;
@ -46,6 +46,7 @@ public final class CleanUp {
private boolean disableShowTouches;
private boolean restoreNormalPowerMode;
private boolean powerOffScreen;
public String hookScript;
public Config() {
// Default constructor, the fields are initialized by CleanUp.configure()
@ -58,6 +59,7 @@ public final class CleanUp {
disableShowTouches = (options & FLAG_DISABLE_SHOW_TOUCHES) != 0;
restoreNormalPowerMode = (options & FLAG_RESTORE_NORMAL_POWER_MODE) != 0;
powerOffScreen = (options & FLAG_POWER_OFF_SCREEN) != 0;
hookScript = in.readString();
}
@Override
@ -75,6 +77,7 @@ public final class CleanUp {
options |= FLAG_POWER_OFF_SCREEN;
}
dest.writeByte(options);
dest.writeString(hookScript);
}
private boolean hasWork() {
@ -116,7 +119,10 @@ public final class CleanUp {
// not instantiable
}
public static void configure(int displayId, int restoreStayOn, boolean disableShowTouches, boolean restoreNormalPowerMode, boolean powerOffScreen)
public static void configure(
int displayId, int restoreStayOn, boolean disableShowTouches, boolean restoreNormalPowerMode, boolean powerOffScreen,
String hookScript
)
throws IOException {
Config config = new Config();
config.displayId = displayId;
@ -124,6 +130,7 @@ public final class CleanUp {
config.restoreStayOn = restoreStayOn;
config.restoreNormalPowerMode = restoreNormalPowerMode;
config.powerOffScreen = powerOffScreen;
config.hookScript = hookScript == null ? "" : hookScript;
if (config.hasWork()) {
startProcess(config);
@ -193,5 +200,15 @@ public final class CleanUp {
Device.setScreenPowerMode(Device.POWER_MODE_NORMAL);
}
}
if (!config.hookScript.isEmpty()) {
try {
Command.execShellScript(config.hookScript, "stop");
} catch (IOException e) {
Ln.e("Something failed while trying to run the stop hook", e);
} catch (InterruptedException e) {
Ln.e("Got interrupted while running the start hook", e);
}
}
}
}

View file

@ -18,6 +18,7 @@ public class Options {
private boolean stayAwake;
private List<CodecOption> codecOptions;
private String encoderName;
private String hookScript;
private boolean powerOffScreenOnClose;
private boolean clipboardAutosync = true;
private boolean downsizeOnError = true;
@ -132,6 +133,14 @@ public class Options {
this.encoderName = encoderName;
}
public void setHookScript(String hookScript) {
this.hookScript = hookScript;
}
public String getHookScript() {
return this.hookScript;
}
public void setPowerOffScreenOnClose(boolean powerOffScreenOnClose) {
this.powerOffScreenOnClose = powerOffScreenOnClose;
}

View file

@ -6,6 +6,7 @@ import android.os.BatteryManager;
import android.os.Build;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
@ -53,11 +54,22 @@ public final class Server {
if (options.getCleanup()) {
try {
CleanUp.configure(options.getDisplayId(), restoreStayOn, mustDisableShowTouchesOnCleanUp, restoreNormalPowerMode,
options.getPowerOffScreenOnClose());
options.getPowerOffScreenOnClose(), options.getHookScript());
} catch (IOException e) {
Ln.e("Could not configure cleanup", e);
}
}
try {
String hookScript = options.getHookScript();
if(hookScript != null && !hookScript.isEmpty()){
Command.execShellScript(hookScript, "start", "--pid", String.valueOf(android.os.Process.myPid()));
}
} catch (IOException e) {
Ln.e("Something failed while trying to run the start hook", e);
} catch (InterruptedException e) {
Ln.e("Got interrupted while running the start hook", e);
}
}
private static void scrcpy(Options options) throws IOException {
@ -170,6 +182,8 @@ public final class Server {
Options options = new Options();
Ln.e("Args are these: " + Arrays.toString(args));
for (int i = 1; i < args.length; ++i) {
String arg = args[i];
int equalIndex = arg.indexOf('=');
@ -232,6 +246,11 @@ public final class Server {
options.setEncoderName(value);
}
break;
case "hook_script":
if (!value.isEmpty()) {
options.setHookScript(value);
}
break;
case "power_off_on_close":
boolean powerOffScreenOnClose = Boolean.parseBoolean(value);
options.setPowerOffScreenOnClose(powerOffScreenOnClose);