Add hookScript ability to the server

This commit is contained in:
brunoais 2022-04-16 14:46:52 +01:00
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_DISABLE_SHOW_TOUCHES = 1 << 0;
private static final int FLAG_RESTORE_NORMAL_POWER_MODE = 2; private static final int FLAG_RESTORE_NORMAL_POWER_MODE = 1 << 1;
private static final int FLAG_POWER_OFF_SCREEN = 4; private static final int FLAG_POWER_OFF_SCREEN = 1 << 2;
private int displayId; private int displayId;
@ -46,6 +46,7 @@ public final class CleanUp {
private boolean disableShowTouches; private boolean disableShowTouches;
private boolean restoreNormalPowerMode; private boolean restoreNormalPowerMode;
private boolean powerOffScreen; private boolean powerOffScreen;
public String hookScript;
public Config() { public Config() {
// Default constructor, the fields are initialized by CleanUp.configure() // Default constructor, the fields are initialized by CleanUp.configure()
@ -58,6 +59,7 @@ public final class CleanUp {
disableShowTouches = (options & FLAG_DISABLE_SHOW_TOUCHES) != 0; disableShowTouches = (options & FLAG_DISABLE_SHOW_TOUCHES) != 0;
restoreNormalPowerMode = (options & FLAG_RESTORE_NORMAL_POWER_MODE) != 0; restoreNormalPowerMode = (options & FLAG_RESTORE_NORMAL_POWER_MODE) != 0;
powerOffScreen = (options & FLAG_POWER_OFF_SCREEN) != 0; powerOffScreen = (options & FLAG_POWER_OFF_SCREEN) != 0;
hookScript = in.readString();
} }
@Override @Override
@ -75,6 +77,7 @@ public final class CleanUp {
options |= FLAG_POWER_OFF_SCREEN; options |= FLAG_POWER_OFF_SCREEN;
} }
dest.writeByte(options); dest.writeByte(options);
dest.writeString(hookScript);
} }
private boolean hasWork() { private boolean hasWork() {
@ -116,7 +119,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(
int displayId, int restoreStayOn, boolean disableShowTouches, boolean restoreNormalPowerMode, boolean powerOffScreen,
String hookScript
)
throws IOException { throws IOException {
Config config = new Config(); Config config = new Config();
config.displayId = displayId; config.displayId = displayId;
@ -124,6 +130,7 @@ public final class CleanUp {
config.restoreStayOn = restoreStayOn; config.restoreStayOn = restoreStayOn;
config.restoreNormalPowerMode = restoreNormalPowerMode; config.restoreNormalPowerMode = restoreNormalPowerMode;
config.powerOffScreen = powerOffScreen; config.powerOffScreen = powerOffScreen;
config.hookScript = hookScript == null ? "" : hookScript;
if (config.hasWork()) { if (config.hasWork()) {
startProcess(config); startProcess(config);
@ -193,5 +200,15 @@ public final class CleanUp {
Device.setScreenPowerMode(Device.POWER_MODE_NORMAL); 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 boolean stayAwake;
private List<CodecOption> codecOptions; private List<CodecOption> codecOptions;
private String encoderName; private String encoderName;
private String hookScript;
private boolean powerOffScreenOnClose; private boolean powerOffScreenOnClose;
private boolean clipboardAutosync = true; private boolean clipboardAutosync = true;
private boolean downsizeOnError = true; private boolean downsizeOnError = true;
@ -132,6 +133,14 @@ public class Options {
this.encoderName = encoderName; this.encoderName = encoderName;
} }
public void setHookScript(String hookScript) {
this.hookScript = hookScript;
}
public String getHookScript() {
return this.hookScript;
}
public void setPowerOffScreenOnClose(boolean powerOffScreenOnClose) { public void setPowerOffScreenOnClose(boolean powerOffScreenOnClose) {
this.powerOffScreenOnClose = powerOffScreenOnClose; this.powerOffScreenOnClose = powerOffScreenOnClose;
} }

View file

@ -6,6 +6,7 @@ import android.os.BatteryManager;
import android.os.Build; import android.os.Build;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -53,11 +54,22 @@ public final class Server {
if (options.getCleanup()) { if (options.getCleanup()) {
try { try {
CleanUp.configure(options.getDisplayId(), restoreStayOn, mustDisableShowTouchesOnCleanUp, restoreNormalPowerMode, CleanUp.configure(options.getDisplayId(), restoreStayOn, mustDisableShowTouchesOnCleanUp, restoreNormalPowerMode,
options.getPowerOffScreenOnClose()); options.getPowerOffScreenOnClose(), options.getHookScript());
} catch (IOException e) { } catch (IOException e) {
Ln.e("Could not configure cleanup", 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 { private static void scrcpy(Options options) throws IOException {
@ -170,6 +182,8 @@ public final class Server {
Options options = new Options(); Options options = new Options();
Ln.e("Args are these: " + Arrays.toString(args));
for (int i = 1; i < args.length; ++i) { for (int i = 1; i < args.length; ++i) {
String arg = args[i]; String arg = args[i];
int equalIndex = arg.indexOf('='); int equalIndex = arg.indexOf('=');
@ -232,6 +246,11 @@ public final class Server {
options.setEncoderName(value); options.setEncoderName(value);
} }
break; break;
case "hook_script":
if (!value.isEmpty()) {
options.setHookScript(value);
}
break;
case "power_off_on_close": case "power_off_on_close":
boolean powerOffScreenOnClose = Boolean.parseBoolean(value); boolean powerOffScreenOnClose = Boolean.parseBoolean(value);
options.setPowerOffScreenOnClose(powerOffScreenOnClose); options.setPowerOffScreenOnClose(powerOffScreenOnClose);