Add intents capability and handling to the server

This commit is contained in:
brunoais 2021-06-19 12:46:35 +01:00
parent 538320add9
commit 02d9df5d71
5 changed files with 63 additions and 4 deletions

View file

@ -294,6 +294,7 @@ execute_server(struct server *server, const struct server_params *params) {
params->codec_options ? params->codec_options : "-",
params->encoder_name ? params->encoder_name : "-",
params->power_off_on_close ? "true" : "false",
params->intent_broadcast ? "true" : "false",
};
#ifdef SERVER_DEBUGGER
LOGI("Server debugger waiting for a client on device port "

View file

@ -49,6 +49,7 @@ struct server_params {
bool stay_awake;
bool force_adb_forward;
bool power_off_on_close;
bool intent_broadcast;
};
// init default values

View file

@ -1,5 +1,7 @@
package com.genymobile.scrcpy;
import android.content.Intent;
import android.net.Uri;
import com.genymobile.scrcpy.wrappers.ContentProvider;
import com.genymobile.scrcpy.wrappers.ServiceManager;
@ -37,6 +39,7 @@ public final class CleanUp {
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 static final int FLAG_BROADCAST_STOPPED = 1 << 3;
private int displayId;
@ -47,6 +50,7 @@ public final class CleanUp {
private boolean disableShowTouches;
private boolean restoreNormalPowerMode;
private boolean powerOffScreen;
private boolean broadcastStopped;
public Config() {
// Default constructor, the fields are initialized by CleanUp.configure()
@ -59,6 +63,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;
broadcastStopped = (options & FLAG_BROADCAST_STOPPED) != 0;
}
@Override
@ -75,11 +80,14 @@ public final class CleanUp {
if (powerOffScreen) {
options |= FLAG_POWER_OFF_SCREEN;
}
if (broadcastStopped) {
options |= FLAG_BROADCAST_STOPPED;
}
dest.writeByte(options);
}
private boolean hasWork() {
return disableShowTouches || restoreStayOn != -1 || restoreNormalPowerMode || powerOffScreen;
return disableShowTouches || restoreStayOn != -1 || restoreNormalPowerMode || powerOffScreen || broadcastStopped;
}
@Override
@ -117,7 +125,9 @@ 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, boolean broadcastStopped
)
throws IOException {
Config config = new Config();
config.displayId = displayId;
@ -125,6 +135,7 @@ public final class CleanUp {
config.restoreStayOn = restoreStayOn;
config.restoreNormalPowerMode = restoreNormalPowerMode;
config.powerOffScreen = powerOffScreen;
config.broadcastStopped = broadcastStopped;
if (config.hasWork()) {
startProcess(config);
@ -187,5 +198,17 @@ public final class CleanUp {
Device.setScreenPowerMode(Device.POWER_MODE_NORMAL);
}
}
if(config.broadcastStopped){
Ln.i("Announce stopped");
announceScrcpyStopped();
}
}
private static void announceScrcpyStopped() {
Intent cleaned = new Intent(Server.scrcpyPrefix("STOPPED"));
cleaned.setData(Uri.parse("scrcpy-status:stopped"));
Device.sendBroadcast(cleaned);
}
}

View file

@ -2,6 +2,9 @@ package com.genymobile.scrcpy;
import android.graphics.Rect;
import java.util.BitSet;
import java.util.EnumSet;
public class Options {
private Ln.Level logLevel;
private int maxSize;
@ -18,6 +21,7 @@ public class Options {
private String codecOptions;
private String encoderName;
private boolean powerOffScreenOnClose;
private boolean broadcastIntents;
public Ln.Level getLogLevel() {
return logLevel;
@ -83,6 +87,10 @@ public class Options {
this.sendFrameMeta = sendFrameMeta;
}
public void setBroadcastIntents(boolean broadcastIntents) {
this.broadcastIntents = broadcastIntents;
}
public boolean getControl() {
return control;
}
@ -138,4 +146,8 @@ public class Options {
public boolean getPowerOffScreenOnClose() {
return this.powerOffScreenOnClose;
}
public boolean getBroadcastIntents() {
return broadcastIntents;
}
}

View file

@ -1,5 +1,7 @@
package com.genymobile.scrcpy;
import android.content.Intent;
import android.net.Uri;
import com.genymobile.scrcpy.wrappers.ContentProvider;
import android.graphics.Rect;
@ -15,6 +17,8 @@ import java.util.Locale;
public final class Server {
public static final String SCRCPY_PREFIX = "com.genymobile.scrcpy.";
private Server() {
// not instantiable
}
@ -50,7 +54,8 @@ public final class Server {
}
}
CleanUp.configure(options.getDisplayId(), restoreStayOn, mustDisableShowTouchesOnCleanUp, true, options.getPowerOffScreenOnClose());
CleanUp.configure(options.getDisplayId(), restoreStayOn, mustDisableShowTouchesOnCleanUp, true, options.getPowerOffScreenOnClose(),
options.getBroadcastIntents());
boolean tunnelForward = options.isTunnelForward();
@ -75,6 +80,10 @@ public final class Server {
});
}
if(options.getBroadcastIntents()){
announceScrcpyStarting();
}
try {
// synchronous
screenEncoder.streamScreen(device, connection.getVideoFd());
@ -92,6 +101,12 @@ public final class Server {
}
}
private static void announceScrcpyStarting() {
Intent starting = new Intent(scrcpyPrefix("STARTED"));
starting.setData(Uri.parse("scrcpy-status:started"));
Device.sendBroadcast(starting);
}
private static Thread startController(final Controller controller) {
Thread thread = new Thread(new Runnable() {
@Override
@ -135,7 +150,7 @@ public final class Server {
"The server version (" + BuildConfig.VERSION_NAME + ") does not match the client " + "(" + clientVersion + ")");
}
final int expectedParameters = 16;
final int expectedParameters = 17;
if (args.length != expectedParameters) {
throw new IllegalArgumentException("Expecting " + expectedParameters + " parameters");
}
@ -188,6 +203,9 @@ public final class Server {
boolean powerOffScreenOnClose = Boolean.parseBoolean(args[15]);
options.setPowerOffScreenOnClose(powerOffScreenOnClose);
boolean broadcastIntents = Boolean.parseBoolean(args[16]);
options.setBroadcastIntents(broadcastIntents);
return options;
}
@ -254,4 +272,8 @@ public final class Server {
scrcpy(options);
}
public static String scrcpyPrefix(String unprefixed){
return SCRCPY_PREFIX + unprefixed;
}
}