Compare commits
11 Commits
pr1313
...
name_param
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
870ced088e | ||
|
|
a845ea0794 | ||
|
|
a0d98fe4ae | ||
|
|
74ece9b45b | ||
|
|
c77024314d | ||
|
|
828327365a | ||
|
|
4668638ee1 | ||
|
|
dbb0df607c | ||
|
|
2f74ec2518 | ||
|
|
8c6799297b | ||
|
|
62c0c1321f |
23
README.md
23
README.md
@@ -396,6 +396,18 @@ The list of display ids can be retrieved by:
|
|||||||
adb shell dumpsys display # search "mDisplayId=" in the output
|
adb shell dumpsys display # search "mDisplayId=" in the output
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Stay awake
|
||||||
|
|
||||||
|
To prevent the device to sleep after some delay:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
scrcpy --stay-awake
|
||||||
|
scrcpy -w
|
||||||
|
```
|
||||||
|
|
||||||
|
The initial state is restored when scrcpy is closed.
|
||||||
|
|
||||||
|
|
||||||
#### Turn screen off
|
#### Turn screen off
|
||||||
|
|
||||||
It is possible to turn the device screen off while mirroring on start with a
|
It is possible to turn the device screen off while mirroring on start with a
|
||||||
@@ -410,6 +422,14 @@ Or by pressing `Ctrl`+`o` at any time.
|
|||||||
|
|
||||||
To turn it back on, press `POWER` (or `Ctrl`+`p`).
|
To turn it back on, press `POWER` (or `Ctrl`+`p`).
|
||||||
|
|
||||||
|
It can be useful to also prevent the device to sleep:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
scrcpy --turn-screen-off --stay-awake
|
||||||
|
scrcpy -Sw
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
#### Render expired frames
|
#### Render expired frames
|
||||||
|
|
||||||
By default, to minimize latency, _scrcpy_ always renders the last decoded frame
|
By default, to minimize latency, _scrcpy_ always renders the last decoded frame
|
||||||
@@ -429,7 +449,8 @@ device).
|
|||||||
|
|
||||||
Android provides this feature in _Developers options_.
|
Android provides this feature in _Developers options_.
|
||||||
|
|
||||||
_Scrcpy_ provides an option to enable this feature on start and disable on exit:
|
_Scrcpy_ provides an option to enable this feature on start and restore the
|
||||||
|
initial value on exit:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
scrcpy --show-touches
|
scrcpy --show-touches
|
||||||
|
|||||||
@@ -136,7 +136,7 @@ Turn the device screen off immediately.
|
|||||||
|
|
||||||
.TP
|
.TP
|
||||||
.B \-t, \-\-show\-touches
|
.B \-t, \-\-show\-touches
|
||||||
Enable "show touches" on start, disable on quit.
|
Enable "show touches" on start, restore the initial value on exit..
|
||||||
|
|
||||||
It only shows physical touches (not clicks from scrcpy).
|
It only shows physical touches (not clicks from scrcpy).
|
||||||
|
|
||||||
@@ -144,6 +144,10 @@ It only shows physical touches (not clicks from scrcpy).
|
|||||||
.B \-v, \-\-version
|
.B \-v, \-\-version
|
||||||
Print the version of scrcpy.
|
Print the version of scrcpy.
|
||||||
|
|
||||||
|
.TP
|
||||||
|
.B \-w, \-\-stay-awake
|
||||||
|
Keep the device on while scrcpy is running.
|
||||||
|
|
||||||
.TP
|
.TP
|
||||||
.B \-\-window\-borderless
|
.B \-\-window\-borderless
|
||||||
Disable window decorations (display borderless window).
|
Disable window decorations (display borderless window).
|
||||||
|
|||||||
@@ -130,12 +130,16 @@ scrcpy_print_usage(const char *arg0) {
|
|||||||
" Turn the device screen off immediately.\n"
|
" Turn the device screen off immediately.\n"
|
||||||
"\n"
|
"\n"
|
||||||
" -t, --show-touches\n"
|
" -t, --show-touches\n"
|
||||||
" Enable \"show touches\" on start, disable on quit.\n"
|
" Enable \"show touches\" on start, restore the initial value\n"
|
||||||
|
" on exit.\n"
|
||||||
" It only shows physical touches (not clicks from scrcpy).\n"
|
" It only shows physical touches (not clicks from scrcpy).\n"
|
||||||
"\n"
|
"\n"
|
||||||
" -v, --version\n"
|
" -v, --version\n"
|
||||||
" Print the version of scrcpy.\n"
|
" Print the version of scrcpy.\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
" -w, --stay-awake\n"
|
||||||
|
" Keep the device on while scrcpy is running.\n"
|
||||||
|
"\n"
|
||||||
" --window-borderless\n"
|
" --window-borderless\n"
|
||||||
" Disable window decorations (display borderless window).\n"
|
" Disable window decorations (display borderless window).\n"
|
||||||
"\n"
|
"\n"
|
||||||
@@ -486,6 +490,7 @@ scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) {
|
|||||||
{"no-display", no_argument, NULL, 'N'},
|
{"no-display", no_argument, NULL, 'N'},
|
||||||
{"no-mipmaps", no_argument, NULL, OPT_NO_MIPMAPS},
|
{"no-mipmaps", no_argument, NULL, OPT_NO_MIPMAPS},
|
||||||
{"port", required_argument, NULL, 'p'},
|
{"port", required_argument, NULL, 'p'},
|
||||||
|
{"prefer-text", no_argument, NULL, OPT_PREFER_TEXT},
|
||||||
{"push-target", required_argument, NULL, OPT_PUSH_TARGET},
|
{"push-target", required_argument, NULL, OPT_PUSH_TARGET},
|
||||||
{"record", required_argument, NULL, 'r'},
|
{"record", required_argument, NULL, 'r'},
|
||||||
{"record-format", required_argument, NULL, OPT_RECORD_FORMAT},
|
{"record-format", required_argument, NULL, OPT_RECORD_FORMAT},
|
||||||
@@ -495,8 +500,8 @@ scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) {
|
|||||||
{"rotation", required_argument, NULL, OPT_ROTATION},
|
{"rotation", required_argument, NULL, OPT_ROTATION},
|
||||||
{"serial", required_argument, NULL, 's'},
|
{"serial", required_argument, NULL, 's'},
|
||||||
{"show-touches", no_argument, NULL, 't'},
|
{"show-touches", no_argument, NULL, 't'},
|
||||||
|
{"stay-awake", no_argument, NULL, 'w'},
|
||||||
{"turn-screen-off", no_argument, NULL, 'S'},
|
{"turn-screen-off", no_argument, NULL, 'S'},
|
||||||
{"prefer-text", no_argument, NULL, OPT_PREFER_TEXT},
|
|
||||||
{"version", no_argument, NULL, 'v'},
|
{"version", no_argument, NULL, 'v'},
|
||||||
{"window-title", required_argument, NULL, OPT_WINDOW_TITLE},
|
{"window-title", required_argument, NULL, OPT_WINDOW_TITLE},
|
||||||
{"window-x", required_argument, NULL, OPT_WINDOW_X},
|
{"window-x", required_argument, NULL, OPT_WINDOW_X},
|
||||||
@@ -513,7 +518,7 @@ scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) {
|
|||||||
optind = 0; // reset to start from the first argument in tests
|
optind = 0; // reset to start from the first argument in tests
|
||||||
|
|
||||||
int c;
|
int c;
|
||||||
while ((c = getopt_long(argc, argv, "b:c:fF:hm:nNp:r:s:StTv", long_options,
|
while ((c = getopt_long(argc, argv, "b:c:fF:hm:nNp:r:s:StTvw", long_options,
|
||||||
NULL)) != -1) {
|
NULL)) != -1) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'b':
|
case 'b':
|
||||||
@@ -593,6 +598,9 @@ scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) {
|
|||||||
case 'v':
|
case 'v':
|
||||||
args->version = true;
|
args->version = true;
|
||||||
break;
|
break;
|
||||||
|
case 'w':
|
||||||
|
opts->stay_awake = true;
|
||||||
|
break;
|
||||||
case OPT_RENDER_EXPIRED_FRAMES:
|
case OPT_RENDER_EXPIRED_FRAMES:
|
||||||
opts->render_expired_frames = true;
|
opts->render_expired_frames = true;
|
||||||
break;
|
break;
|
||||||
@@ -675,5 +683,10 @@ scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!opts->control && opts->stay_awake) {
|
||||||
|
LOGE("Could not request to stay awake if control is disabled");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -229,21 +229,6 @@ event_loop(bool display, bool control) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static process_t
|
|
||||||
set_show_touches_enabled(const char *serial, bool enabled) {
|
|
||||||
const char *value = enabled ? "1" : "0";
|
|
||||||
const char *const adb_cmd[] = {
|
|
||||||
"shell", "settings", "put", "system", "show_touches", value
|
|
||||||
};
|
|
||||||
return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
wait_show_touches(process_t process) {
|
|
||||||
// reap the process, ignore the result
|
|
||||||
process_check_success(process, "show_touches");
|
|
||||||
}
|
|
||||||
|
|
||||||
static SDL_LogPriority
|
static SDL_LogPriority
|
||||||
sdl_priority_from_av_level(int level) {
|
sdl_priority_from_av_level(int level) {
|
||||||
switch (level) {
|
switch (level) {
|
||||||
@@ -292,19 +277,13 @@ scrcpy(const struct scrcpy_options *options) {
|
|||||||
.lock_video_orientation = options->lock_video_orientation,
|
.lock_video_orientation = options->lock_video_orientation,
|
||||||
.control = options->control,
|
.control = options->control,
|
||||||
.display_id = options->display_id,
|
.display_id = options->display_id,
|
||||||
|
.show_touches = options->show_touches,
|
||||||
|
.stay_awake = options->stay_awake,
|
||||||
};
|
};
|
||||||
if (!server_start(&server, options->serial, ¶ms)) {
|
if (!server_start(&server, options->serial, ¶ms)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
process_t proc_show_touches = PROCESS_NONE;
|
|
||||||
bool show_touches_waited;
|
|
||||||
if (options->show_touches) {
|
|
||||||
LOGI("Enable show_touches");
|
|
||||||
proc_show_touches = set_show_touches_enabled(options->serial, true);
|
|
||||||
show_touches_waited = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
|
|
||||||
bool fps_counter_initialized = false;
|
bool fps_counter_initialized = false;
|
||||||
@@ -421,11 +400,6 @@ scrcpy(const struct scrcpy_options *options) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options->show_touches) {
|
|
||||||
wait_show_touches(proc_show_touches);
|
|
||||||
show_touches_waited = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
input_manager.prefer_text = options->prefer_text;
|
input_manager.prefer_text = options->prefer_text;
|
||||||
|
|
||||||
ret = event_loop(options->display, options->control);
|
ret = event_loop(options->display, options->control);
|
||||||
@@ -482,16 +456,6 @@ end:
|
|||||||
fps_counter_destroy(&fps_counter);
|
fps_counter_destroy(&fps_counter);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options->show_touches) {
|
|
||||||
if (!show_touches_waited) {
|
|
||||||
// wait the process which enabled "show touches"
|
|
||||||
wait_show_touches(proc_show_touches);
|
|
||||||
}
|
|
||||||
LOGI("Disable show_touches");
|
|
||||||
proc_show_touches = set_show_touches_enabled(options->serial, false);
|
|
||||||
wait_show_touches(proc_show_touches);
|
|
||||||
}
|
|
||||||
|
|
||||||
server_destroy(&server);
|
server_destroy(&server);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ struct scrcpy_options {
|
|||||||
bool prefer_text;
|
bool prefer_text;
|
||||||
bool window_borderless;
|
bool window_borderless;
|
||||||
bool mipmaps;
|
bool mipmaps;
|
||||||
|
bool stay_awake;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define SCRCPY_OPTIONS_DEFAULT { \
|
#define SCRCPY_OPTIONS_DEFAULT { \
|
||||||
@@ -72,6 +73,7 @@ struct scrcpy_options {
|
|||||||
.prefer_text = false, \
|
.prefer_text = false, \
|
||||||
.window_borderless = false, \
|
.window_borderless = false, \
|
||||||
.mipmaps = true, \
|
.mipmaps = true, \
|
||||||
|
.stay_awake = false, \
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
|
|||||||
@@ -231,22 +231,17 @@ enable_tunnel_any_port(struct server *server, struct port_range port_range) {
|
|||||||
|
|
||||||
static process_t
|
static process_t
|
||||||
execute_server(struct server *server, const struct server_params *params) {
|
execute_server(struct server *server, const struct server_params *params) {
|
||||||
char max_size_string[6];
|
process_t result = PROCESS_NONE;
|
||||||
char bit_rate_string[11];
|
|
||||||
char max_fps_string[6];
|
char *cmd[128];
|
||||||
char lock_video_orientation_string[3];
|
int i = 0;
|
||||||
char display_id_string[6];
|
cmd[i++] = "shell";
|
||||||
sprintf(max_size_string, "%"PRIu16, params->max_size);
|
cmd[i++] = "CLASSPATH=" DEVICE_SERVER_PATH;
|
||||||
sprintf(bit_rate_string, "%"PRIu32, params->bit_rate);
|
cmd[i++] = "app_process";
|
||||||
sprintf(max_fps_string, "%"PRIu16, params->max_fps);
|
|
||||||
sprintf(lock_video_orientation_string, "%"PRIi8, params->lock_video_orientation);
|
|
||||||
sprintf(display_id_string, "%"PRIu16, params->display_id);
|
|
||||||
const char *const cmd[] = {
|
|
||||||
"shell",
|
|
||||||
"CLASSPATH=" DEVICE_SERVER_PATH,
|
|
||||||
"app_process",
|
|
||||||
#ifdef SERVER_DEBUGGER
|
#ifdef SERVER_DEBUGGER
|
||||||
# define SERVER_DEBUGGER_PORT "5005"
|
# define SERVER_DEBUGGER_PORT "5005"
|
||||||
|
cmd[i++] =
|
||||||
# ifdef SERVER_DEBUGGER_METHOD_NEW
|
# ifdef SERVER_DEBUGGER_METHOD_NEW
|
||||||
/* Android 9 and above */
|
/* Android 9 and above */
|
||||||
"-XjdwpProvider:internal -XjdwpOptions:transport=dt_socket,suspend=y,server=y,address="
|
"-XjdwpProvider:internal -XjdwpOptions:transport=dt_socket,suspend=y,server=y,address="
|
||||||
@@ -254,21 +249,38 @@ execute_server(struct server *server, const struct server_params *params) {
|
|||||||
/* Android 8 and below */
|
/* Android 8 and below */
|
||||||
"-agentlib:jdwp=transport=dt_socket,suspend=y,server=y,address="
|
"-agentlib:jdwp=transport=dt_socket,suspend=y,server=y,address="
|
||||||
# endif
|
# endif
|
||||||
SERVER_DEBUGGER_PORT,
|
SERVER_DEBUGGER_PORT;
|
||||||
#endif
|
#endif
|
||||||
"/", // unused
|
|
||||||
"com.genymobile.scrcpy.Server",
|
cmd[i++] = "/"; // unused
|
||||||
SCRCPY_VERSION,
|
cmd[i++] = "com.genymobile.scrcpy.Server";
|
||||||
max_size_string,
|
cmd[i++] = SCRCPY_VERSION;
|
||||||
bit_rate_string,
|
|
||||||
max_fps_string,
|
int dyn_index = i; // from there, the strings are allocated
|
||||||
lock_video_orientation_string,
|
#define ADD_PARAM(fmt, ...) \
|
||||||
server->tunnel_forward ? "true" : "false",
|
cmd[i] = sc_asprintf(fmt, ## __VA_ARGS__); \
|
||||||
params->crop ? params->crop : "-",
|
if (!cmd[i++]) { \
|
||||||
"true", // always send frame meta (packet boundaries + timestamp)
|
goto end; \
|
||||||
params->control ? "true" : "false",
|
}
|
||||||
display_id_string,
|
|
||||||
};
|
#define STRBOOL(p) (p ? "true" : "false")
|
||||||
|
|
||||||
|
ADD_PARAM("max_size=%"PRIu16, params->max_size);
|
||||||
|
ADD_PARAM("bit_rate=%"PRIu32, params->bit_rate);
|
||||||
|
ADD_PARAM("max_fps=%"PRIu16, params->max_fps);
|
||||||
|
ADD_PARAM("lock_video_orientation=%"PRIi8, params->lock_video_orientation);
|
||||||
|
ADD_PARAM("tunnel_forward=%s", STRBOOL(server->tunnel_forward));
|
||||||
|
ADD_PARAM("crop=%s", params->crop ? params->crop : "");
|
||||||
|
// always send frame meta (packet boundaries + timestamp)
|
||||||
|
ADD_PARAM("send_frame_meta=true");
|
||||||
|
ADD_PARAM("control=%s", STRBOOL(params->control));
|
||||||
|
ADD_PARAM("display_id=%"PRIu16, params->display_id);
|
||||||
|
ADD_PARAM("show_touches=%s", STRBOOL(params->show_touches));
|
||||||
|
ADD_PARAM("stay_awake=%s", STRBOOL(params->stay_awake));
|
||||||
|
|
||||||
|
#undef ADD_PARAM
|
||||||
|
#undef STRBOOL
|
||||||
|
|
||||||
#ifdef SERVER_DEBUGGER
|
#ifdef SERVER_DEBUGGER
|
||||||
LOGI("Server debugger waiting for a client on device port "
|
LOGI("Server debugger waiting for a client on device port "
|
||||||
SERVER_DEBUGGER_PORT "...");
|
SERVER_DEBUGGER_PORT "...");
|
||||||
@@ -280,7 +292,14 @@ execute_server(struct server *server, const struct server_params *params) {
|
|||||||
// Port: 5005
|
// Port: 5005
|
||||||
// Then click on "Debug"
|
// Then click on "Debug"
|
||||||
#endif
|
#endif
|
||||||
return adb_execute(server->serial, cmd, sizeof(cmd) / sizeof(cmd[0]));
|
result = adb_execute(server->serial, (const char **) cmd, i);
|
||||||
|
|
||||||
|
end:
|
||||||
|
for (int j = i; j > dyn_index; --j) {
|
||||||
|
free(cmd[j - 1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static socket_t
|
static socket_t
|
||||||
|
|||||||
@@ -51,6 +51,8 @@ struct server_params {
|
|||||||
int8_t lock_video_orientation;
|
int8_t lock_video_orientation;
|
||||||
bool control;
|
bool control;
|
||||||
uint16_t display_id;
|
uint16_t display_id;
|
||||||
|
bool show_touches;
|
||||||
|
bool stay_awake;
|
||||||
};
|
};
|
||||||
|
|
||||||
// init default values
|
// init default values
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
#include "str_util.h"
|
#include "str_util.h"
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
#include <stdarg.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@@ -195,3 +197,33 @@ utf8_from_wide_char(const wchar_t *ws) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
char *
|
||||||
|
sc_asprintf(const char *fmt, ...) {
|
||||||
|
va_list va;
|
||||||
|
va_start(va, fmt);
|
||||||
|
char *s = sc_vasprintf(fmt, va);
|
||||||
|
va_end(va);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *
|
||||||
|
sc_vasprintf(const char *fmt, va_list ap) {
|
||||||
|
va_list va;
|
||||||
|
va_copy(va, ap);
|
||||||
|
int len = vsnprintf(NULL, 0, fmt, va);
|
||||||
|
va_end(va);
|
||||||
|
|
||||||
|
char *str = malloc(len + 1);
|
||||||
|
if (!str) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
va_copy(va, ap);
|
||||||
|
int len2 = vsprintf(str, fmt, va);
|
||||||
|
(void) len2;
|
||||||
|
assert(len == len2);
|
||||||
|
va_end(va);
|
||||||
|
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
#ifndef STRUTIL_H
|
#ifndef STRUTIL_H
|
||||||
#define STRUTIL_H
|
#define STRUTIL_H
|
||||||
|
|
||||||
|
#include <stdarg.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
@@ -57,4 +59,13 @@ char *
|
|||||||
utf8_from_wide_char(const wchar_t *s);
|
utf8_from_wide_char(const wchar_t *s);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// compatibility function similar to asprintf()
|
||||||
|
// (but returning the resulting string for convenience)
|
||||||
|
char *
|
||||||
|
sc_asprintf(const char *fmt, ...)
|
||||||
|
__attribute__((format(printf, 1, 2)));
|
||||||
|
|
||||||
|
char *
|
||||||
|
sc_vasprintf(const char *fmt, va_list ap);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
77
server/src/main/java/com/genymobile/scrcpy/CleanUp.java
Normal file
77
server/src/main/java/com/genymobile/scrcpy/CleanUp.java
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
package com.genymobile.scrcpy;
|
||||||
|
|
||||||
|
import com.genymobile.scrcpy.wrappers.ContentProvider;
|
||||||
|
import com.genymobile.scrcpy.wrappers.ServiceManager;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the cleanup of scrcpy, even if the main process is killed.
|
||||||
|
* <p>
|
||||||
|
* This is useful to restore some state when scrcpy is closed, even on device disconnection (which kills the scrcpy process).
|
||||||
|
*/
|
||||||
|
public final class CleanUp {
|
||||||
|
|
||||||
|
public static final String SERVER_PATH = "/data/local/tmp/scrcpy-server.jar";
|
||||||
|
|
||||||
|
private CleanUp() {
|
||||||
|
// not instantiable
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void configure(boolean disableShowTouches, int restoreStayOn) throws IOException {
|
||||||
|
boolean needProcess = disableShowTouches || restoreStayOn != -1;
|
||||||
|
if (needProcess) {
|
||||||
|
startProcess(disableShowTouches, restoreStayOn);
|
||||||
|
} else {
|
||||||
|
// There is no additional clean up to do when scrcpy dies
|
||||||
|
unlinkSelf();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void startProcess(boolean disableShowTouches, int restoreStayOn) throws IOException {
|
||||||
|
String[] cmd = {"app_process", "/", CleanUp.class.getName(), String.valueOf(disableShowTouches), String.valueOf(restoreStayOn)};
|
||||||
|
|
||||||
|
ProcessBuilder builder = new ProcessBuilder(cmd);
|
||||||
|
builder.environment().put("CLASSPATH", SERVER_PATH);
|
||||||
|
builder.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void unlinkSelf() {
|
||||||
|
try {
|
||||||
|
new File(SERVER_PATH).delete();
|
||||||
|
} catch (Exception e) {
|
||||||
|
Ln.e("Could not unlink server", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String... args) {
|
||||||
|
unlinkSelf();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Wait for the server to die
|
||||||
|
System.in.read();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// Expected when the server is dead
|
||||||
|
}
|
||||||
|
|
||||||
|
Ln.i("Cleaning up");
|
||||||
|
|
||||||
|
boolean disableShowTouches = Boolean.parseBoolean(args[0]);
|
||||||
|
int restoreStayOn = Integer.parseInt(args[1]);
|
||||||
|
|
||||||
|
if (disableShowTouches || restoreStayOn != -1) {
|
||||||
|
ServiceManager serviceManager = new ServiceManager();
|
||||||
|
try (ContentProvider settings = serviceManager.getActivityManager().createSettingsProvider()) {
|
||||||
|
if (disableShowTouches) {
|
||||||
|
Ln.i("Disabling \"show touches\"");
|
||||||
|
settings.putValue(ContentProvider.TABLE_SYSTEM, "show_touches", "0");
|
||||||
|
}
|
||||||
|
if (restoreStayOn != -1) {
|
||||||
|
Ln.i("Restoring \"stay awake\"");
|
||||||
|
settings.putValue(ContentProvider.TABLE_GLOBAL, "stay_on_while_plugged_in", String.valueOf(restoreStayOn));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.genymobile.scrcpy;
|
package com.genymobile.scrcpy;
|
||||||
|
|
||||||
|
import com.genymobile.scrcpy.wrappers.ContentProvider;
|
||||||
import com.genymobile.scrcpy.wrappers.InputManager;
|
import com.genymobile.scrcpy.wrappers.InputManager;
|
||||||
import com.genymobile.scrcpy.wrappers.ServiceManager;
|
import com.genymobile.scrcpy.wrappers.ServiceManager;
|
||||||
import com.genymobile.scrcpy.wrappers.SurfaceControl;
|
import com.genymobile.scrcpy.wrappers.SurfaceControl;
|
||||||
@@ -199,4 +200,8 @@ public final class Device {
|
|||||||
wm.thawRotation();
|
wm.thawRotation();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ContentProvider createSettingsProvider() {
|
||||||
|
return serviceManager.getActivityManager().createSettingsProvider();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,12 +6,14 @@ public class Options {
|
|||||||
private int maxSize;
|
private int maxSize;
|
||||||
private int bitRate;
|
private int bitRate;
|
||||||
private int maxFps;
|
private int maxFps;
|
||||||
private int lockedVideoOrientation;
|
private int lockedVideoOrientation = -1;
|
||||||
private boolean tunnelForward;
|
private boolean tunnelForward;
|
||||||
private Rect crop;
|
private Rect crop;
|
||||||
private boolean sendFrameMeta; // send PTS so that the client may record properly
|
private boolean sendFrameMeta; // send PTS so that the client may record properly
|
||||||
private boolean control;
|
private boolean control;
|
||||||
private int displayId;
|
private int displayId;
|
||||||
|
private boolean showTouches;
|
||||||
|
private boolean stayAwake;
|
||||||
|
|
||||||
public int getMaxSize() {
|
public int getMaxSize() {
|
||||||
return maxSize;
|
return maxSize;
|
||||||
@@ -84,4 +86,20 @@ public class Options {
|
|||||||
public void setDisplayId(int displayId) {
|
public void setDisplayId(int displayId) {
|
||||||
this.displayId = displayId;
|
this.displayId = displayId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean getShowTouches() {
|
||||||
|
return showTouches;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setShowTouches(boolean showTouches) {
|
||||||
|
this.showTouches = showTouches;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean getStayAwake() {
|
||||||
|
return stayAwake;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStayAwake(boolean stayAwake) {
|
||||||
|
this.stayAwake = stayAwake;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,9 +47,21 @@ public class ScreenEncoder implements Device.RotationListener {
|
|||||||
|
|
||||||
public void streamScreen(Device device, FileDescriptor fd) throws IOException {
|
public void streamScreen(Device device, FileDescriptor fd) throws IOException {
|
||||||
Workarounds.prepareMainLooper();
|
Workarounds.prepareMainLooper();
|
||||||
Workarounds.fillAppInfo();
|
|
||||||
|
|
||||||
MediaFormat format = createFormat(bitRate, maxFps, DEFAULT_I_FRAME_INTERVAL);
|
try {
|
||||||
|
internalStreamScreen(device, fd);
|
||||||
|
} catch (NullPointerException e) {
|
||||||
|
// Retry with workarounds enabled:
|
||||||
|
// <https://github.com/Genymobile/scrcpy/issues/365>
|
||||||
|
// <https://github.com/Genymobile/scrcpy/issues/940>
|
||||||
|
Ln.d("Applying workarounds to avoid NullPointerException");
|
||||||
|
Workarounds.fillAppInfo();
|
||||||
|
internalStreamScreen(device, fd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void internalStreamScreen(Device device, FileDescriptor fd) throws IOException {
|
||||||
|
MediaFormat format = createFormat(bitRate, maxFps);
|
||||||
device.setRotationListener(this);
|
device.setRotationListener(this);
|
||||||
boolean alive;
|
boolean alive;
|
||||||
try {
|
try {
|
||||||
@@ -139,14 +151,14 @@ public class ScreenEncoder implements Device.RotationListener {
|
|||||||
return MediaCodec.createEncoderByType(MediaFormat.MIMETYPE_VIDEO_AVC);
|
return MediaCodec.createEncoderByType(MediaFormat.MIMETYPE_VIDEO_AVC);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static MediaFormat createFormat(int bitRate, int maxFps, int iFrameInterval) {
|
private static MediaFormat createFormat(int bitRate, int maxFps) {
|
||||||
MediaFormat format = new MediaFormat();
|
MediaFormat format = new MediaFormat();
|
||||||
format.setString(MediaFormat.KEY_MIME, MediaFormat.MIMETYPE_VIDEO_AVC);
|
format.setString(MediaFormat.KEY_MIME, MediaFormat.MIMETYPE_VIDEO_AVC);
|
||||||
format.setInteger(MediaFormat.KEY_BIT_RATE, bitRate);
|
format.setInteger(MediaFormat.KEY_BIT_RATE, bitRate);
|
||||||
// must be present to configure the encoder, but does not impact the actual frame rate, which is variable
|
// must be present to configure the encoder, but does not impact the actual frame rate, which is variable
|
||||||
format.setInteger(MediaFormat.KEY_FRAME_RATE, 60);
|
format.setInteger(MediaFormat.KEY_FRAME_RATE, 60);
|
||||||
format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
|
format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
|
||||||
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, iFrameInterval);
|
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, DEFAULT_I_FRAME_INTERVAL);
|
||||||
// display the very first frame, and recover from bad quality when no new frames
|
// display the very first frame, and recover from bad quality when no new frames
|
||||||
format.setLong(MediaFormat.KEY_REPEAT_PREVIOUS_FRAME_AFTER, REPEAT_FRAME_DELAY_US); // µs
|
format.setLong(MediaFormat.KEY_REPEAT_PREVIOUS_FRAME_AFTER, REPEAT_FRAME_DELAY_US); // µs
|
||||||
if (maxFps > 0) {
|
if (maxFps > 0) {
|
||||||
|
|||||||
@@ -1,15 +1,16 @@
|
|||||||
package com.genymobile.scrcpy;
|
package com.genymobile.scrcpy;
|
||||||
|
|
||||||
|
import com.genymobile.scrcpy.wrappers.ContentProvider;
|
||||||
|
|
||||||
import android.graphics.Rect;
|
import android.graphics.Rect;
|
||||||
import android.media.MediaCodec;
|
import android.media.MediaCodec;
|
||||||
|
import android.os.BatteryManager;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public final class Server {
|
public final class Server {
|
||||||
|
|
||||||
private static final String SERVER_PATH = "/data/local/tmp/scrcpy-server.jar";
|
|
||||||
|
|
||||||
private Server() {
|
private Server() {
|
||||||
// not instantiable
|
// not instantiable
|
||||||
@@ -18,6 +19,35 @@ public final class Server {
|
|||||||
private static void scrcpy(Options options) throws IOException {
|
private static void scrcpy(Options options) throws IOException {
|
||||||
Ln.i("Device: " + Build.MANUFACTURER + " " + Build.MODEL + " (Android " + Build.VERSION.RELEASE + ")");
|
Ln.i("Device: " + Build.MANUFACTURER + " " + Build.MODEL + " (Android " + Build.VERSION.RELEASE + ")");
|
||||||
final Device device = new Device(options);
|
final Device device = new Device(options);
|
||||||
|
|
||||||
|
boolean mustDisableShowTouchesOnCleanUp = false;
|
||||||
|
int restoreStayOn = -1;
|
||||||
|
if (options.getShowTouches() || options.getStayAwake()) {
|
||||||
|
try (ContentProvider settings = device.createSettingsProvider()) {
|
||||||
|
if (options.getShowTouches()) {
|
||||||
|
String oldValue = settings.getAndPutValue(ContentProvider.TABLE_SYSTEM, "show_touches", "1");
|
||||||
|
// If "show touches" was disabled, it must be disabled back on clean up
|
||||||
|
mustDisableShowTouchesOnCleanUp = !"1".equals(oldValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.getStayAwake()) {
|
||||||
|
int stayOn = BatteryManager.BATTERY_PLUGGED_AC | BatteryManager.BATTERY_PLUGGED_USB | BatteryManager.BATTERY_PLUGGED_WIRELESS;
|
||||||
|
String oldValue = settings.getAndPutValue(ContentProvider.TABLE_GLOBAL, "stay_on_while_plugged_in", String.valueOf(stayOn));
|
||||||
|
try {
|
||||||
|
restoreStayOn = Integer.parseInt(oldValue);
|
||||||
|
if (restoreStayOn == stayOn) {
|
||||||
|
// No need to restore
|
||||||
|
restoreStayOn = -1;
|
||||||
|
}
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
restoreStayOn = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CleanUp.configure(mustDisableShowTouchesOnCleanUp, restoreStayOn);
|
||||||
|
|
||||||
boolean tunnelForward = options.isTunnelForward();
|
boolean tunnelForward = options.isTunnelForward();
|
||||||
try (DesktopConnection connection = DesktopConnection.open(device, tunnelForward)) {
|
try (DesktopConnection connection = DesktopConnection.open(device, tunnelForward)) {
|
||||||
ScreenEncoder screenEncoder = new ScreenEncoder(options.getSendFrameMeta(), options.getBitRate(), options.getMaxFps());
|
ScreenEncoder screenEncoder = new ScreenEncoder(options.getSendFrameMeta(), options.getBitRate(), options.getMaxFps());
|
||||||
@@ -79,45 +109,73 @@ public final class Server {
|
|||||||
"The server version (" + BuildConfig.VERSION_NAME + ") does not match the client " + "(" + clientVersion + ")");
|
"The server version (" + BuildConfig.VERSION_NAME + ") does not match the client " + "(" + clientVersion + ")");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args.length != 10) {
|
|
||||||
throw new IllegalArgumentException("Expecting 10 parameters");
|
|
||||||
}
|
|
||||||
|
|
||||||
Options options = new Options();
|
Options options = new Options();
|
||||||
|
|
||||||
int maxSize = Integer.parseInt(args[1]) & ~7; // multiple of 8
|
for (int i = 1; i < args.length; ++i) {
|
||||||
options.setMaxSize(maxSize);
|
String arg = args[i];
|
||||||
|
int equalIndex = arg.indexOf('=');
|
||||||
|
if (equalIndex == -1) {
|
||||||
|
throw new IllegalArgumentException("Invalid key=value pair: \"" + arg + "\"");
|
||||||
|
}
|
||||||
|
String key = arg.substring(0, equalIndex);
|
||||||
|
String value = arg.substring(equalIndex + 1);
|
||||||
|
|
||||||
int bitRate = Integer.parseInt(args[2]);
|
switch (key) {
|
||||||
options.setBitRate(bitRate);
|
case "max_size":
|
||||||
|
int maxSize = Integer.parseInt(value) & ~7; // multiple of 8
|
||||||
int maxFps = Integer.parseInt(args[3]);
|
options.setMaxSize(maxSize);
|
||||||
options.setMaxFps(maxFps);
|
break;
|
||||||
|
case "bit_rate":
|
||||||
int lockedVideoOrientation = Integer.parseInt(args[4]);
|
int bitRate = Integer.parseInt(value);
|
||||||
options.setLockedVideoOrientation(lockedVideoOrientation);
|
options.setBitRate(bitRate);
|
||||||
|
break;
|
||||||
// use "adb forward" instead of "adb tunnel"? (so the server must listen)
|
case "max_fps":
|
||||||
boolean tunnelForward = Boolean.parseBoolean(args[5]);
|
int maxFps = Integer.parseInt(value);
|
||||||
options.setTunnelForward(tunnelForward);
|
options.setMaxFps(maxFps);
|
||||||
|
break;
|
||||||
Rect crop = parseCrop(args[6]);
|
case "lock_video_orientation":
|
||||||
options.setCrop(crop);
|
int lockedVideoOrientation = Integer.parseInt(value);
|
||||||
|
options.setLockedVideoOrientation(lockedVideoOrientation);
|
||||||
boolean sendFrameMeta = Boolean.parseBoolean(args[7]);
|
break;
|
||||||
options.setSendFrameMeta(sendFrameMeta);
|
case "tunnel_forward":
|
||||||
|
// use "adb forward" instead of "adb tunnel"? (so the server must listen)
|
||||||
boolean control = Boolean.parseBoolean(args[8]);
|
boolean tunnelForward = Boolean.parseBoolean(value);
|
||||||
options.setControl(control);
|
options.setTunnelForward(tunnelForward);
|
||||||
|
break;
|
||||||
int displayId = Integer.parseInt(args[9]);
|
case "crop":
|
||||||
options.setDisplayId(displayId);
|
Rect crop = parseCrop(value);
|
||||||
|
options.setCrop(crop);
|
||||||
|
break;
|
||||||
|
case "send_frame_meta":
|
||||||
|
boolean sendFrameMeta = Boolean.parseBoolean(value);
|
||||||
|
options.setSendFrameMeta(sendFrameMeta);
|
||||||
|
break;
|
||||||
|
case "control":
|
||||||
|
boolean control = Boolean.parseBoolean(value);
|
||||||
|
options.setControl(control);
|
||||||
|
break;
|
||||||
|
case "display_id":
|
||||||
|
int displayId = Integer.parseInt(value);
|
||||||
|
options.setDisplayId(displayId);
|
||||||
|
break;
|
||||||
|
case "show_touches":
|
||||||
|
boolean showTouches = Boolean.parseBoolean(value);
|
||||||
|
options.setShowTouches(showTouches);
|
||||||
|
break;
|
||||||
|
case "stay_awake":
|
||||||
|
boolean stayAwake = Boolean.parseBoolean(value);
|
||||||
|
options.setStayAwake(stayAwake);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("Unknown parameter: " + key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Rect parseCrop(String crop) {
|
private static Rect parseCrop(String crop) {
|
||||||
if ("-".equals(crop)) {
|
if (crop.isEmpty()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
// input format: "width:height:x:y"
|
// input format: "width:height:x:y"
|
||||||
@@ -132,14 +190,6 @@ public final class Server {
|
|||||||
return new Rect(x, y, x + width, y + height);
|
return new Rect(x, y, x + width, y + height);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void unlinkSelf() {
|
|
||||||
try {
|
|
||||||
new File(SERVER_PATH).delete();
|
|
||||||
} catch (Exception e) {
|
|
||||||
Ln.e("Could not unlink server", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void suggestFix(Throwable e) {
|
private static void suggestFix(Throwable e) {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||||
if (e instanceof MediaCodec.CodecException) {
|
if (e instanceof MediaCodec.CodecException) {
|
||||||
@@ -172,7 +222,6 @@ public final class Server {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
unlinkSelf();
|
|
||||||
Options options = createOptions(args);
|
Options options = createOptions(args);
|
||||||
scrcpy(options);
|
scrcpy(options);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,87 @@
|
|||||||
|
package com.genymobile.scrcpy.wrappers;
|
||||||
|
|
||||||
|
import com.genymobile.scrcpy.Ln;
|
||||||
|
|
||||||
|
import android.os.Binder;
|
||||||
|
import android.os.IBinder;
|
||||||
|
import android.os.IInterface;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
public class ActivityManager {
|
||||||
|
|
||||||
|
private final IInterface manager;
|
||||||
|
private Method getContentProviderExternalMethod;
|
||||||
|
private boolean getContentProviderExternalMethodLegacy;
|
||||||
|
private Method removeContentProviderExternalMethod;
|
||||||
|
|
||||||
|
public ActivityManager(IInterface manager) {
|
||||||
|
this.manager = manager;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Method getGetContentProviderExternalMethod() throws NoSuchMethodException {
|
||||||
|
if (getContentProviderExternalMethod == null) {
|
||||||
|
try {
|
||||||
|
getContentProviderExternalMethod = manager.getClass()
|
||||||
|
.getMethod("getContentProviderExternal", String.class, int.class, IBinder.class, String.class);
|
||||||
|
} catch (NoSuchMethodException e) {
|
||||||
|
// old version
|
||||||
|
getContentProviderExternalMethod = manager.getClass().getMethod("getContentProviderExternal", String.class, int.class, IBinder.class);
|
||||||
|
getContentProviderExternalMethodLegacy = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return getContentProviderExternalMethod;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Method getRemoveContentProviderExternalMethod() throws NoSuchMethodException {
|
||||||
|
if (removeContentProviderExternalMethod == null) {
|
||||||
|
removeContentProviderExternalMethod = manager.getClass().getMethod("removeContentProviderExternal", String.class, IBinder.class);
|
||||||
|
}
|
||||||
|
return removeContentProviderExternalMethod;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ContentProvider getContentProviderExternal(String name, IBinder token) {
|
||||||
|
try {
|
||||||
|
Method method = getGetContentProviderExternalMethod();
|
||||||
|
Object[] args;
|
||||||
|
if (!getContentProviderExternalMethodLegacy) {
|
||||||
|
// new version
|
||||||
|
args = new Object[]{name, ServiceManager.USER_ID, token, null};
|
||||||
|
} else {
|
||||||
|
// old version
|
||||||
|
args = new Object[]{name, ServiceManager.USER_ID, token};
|
||||||
|
}
|
||||||
|
// ContentProviderHolder providerHolder = getContentProviderExternal(...);
|
||||||
|
Object providerHolder = method.invoke(manager, args);
|
||||||
|
if (providerHolder == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
// IContentProvider provider = providerHolder.provider;
|
||||||
|
Field providerField = providerHolder.getClass().getDeclaredField("provider");
|
||||||
|
providerField.setAccessible(true);
|
||||||
|
Object provider = providerField.get(providerHolder);
|
||||||
|
if (provider == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return new ContentProvider(this, provider, name, token);
|
||||||
|
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException | NoSuchFieldException e) {
|
||||||
|
Ln.e("Could not invoke method", e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void removeContentProviderExternal(String name, IBinder token) {
|
||||||
|
try {
|
||||||
|
Method method = getRemoveContentProviderExternalMethod();
|
||||||
|
method.invoke(manager, name, token);
|
||||||
|
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
|
||||||
|
Ln.e("Could not invoke method", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ContentProvider createSettingsProvider() {
|
||||||
|
return getContentProviderExternal("settings", new Binder());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,10 +10,6 @@ import java.lang.reflect.InvocationTargetException;
|
|||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
public class ClipboardManager {
|
public class ClipboardManager {
|
||||||
|
|
||||||
private static final String PACKAGE_NAME = "com.android.shell";
|
|
||||||
private static final int USER_ID = 0;
|
|
||||||
|
|
||||||
private final IInterface manager;
|
private final IInterface manager;
|
||||||
private Method getPrimaryClipMethod;
|
private Method getPrimaryClipMethod;
|
||||||
private Method setPrimaryClipMethod;
|
private Method setPrimaryClipMethod;
|
||||||
@@ -46,17 +42,17 @@ public class ClipboardManager {
|
|||||||
|
|
||||||
private static ClipData getPrimaryClip(Method method, IInterface manager) throws InvocationTargetException, IllegalAccessException {
|
private static ClipData getPrimaryClip(Method method, IInterface manager) throws InvocationTargetException, IllegalAccessException {
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
|
||||||
return (ClipData) method.invoke(manager, PACKAGE_NAME);
|
return (ClipData) method.invoke(manager, ServiceManager.PACKAGE_NAME);
|
||||||
}
|
}
|
||||||
return (ClipData) method.invoke(manager, PACKAGE_NAME, USER_ID);
|
return (ClipData) method.invoke(manager, ServiceManager.PACKAGE_NAME, ServiceManager.USER_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void setPrimaryClip(Method method, IInterface manager, ClipData clipData)
|
private static void setPrimaryClip(Method method, IInterface manager, ClipData clipData)
|
||||||
throws InvocationTargetException, IllegalAccessException {
|
throws InvocationTargetException, IllegalAccessException {
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
|
||||||
method.invoke(manager, clipData, PACKAGE_NAME);
|
method.invoke(manager, clipData, ServiceManager.PACKAGE_NAME);
|
||||||
} else {
|
} else {
|
||||||
method.invoke(manager, clipData, PACKAGE_NAME, USER_ID);
|
method.invoke(manager, clipData, ServiceManager.PACKAGE_NAME, ServiceManager.USER_ID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,132 @@
|
|||||||
|
package com.genymobile.scrcpy.wrappers;
|
||||||
|
|
||||||
|
import com.genymobile.scrcpy.Ln;
|
||||||
|
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.os.IBinder;
|
||||||
|
|
||||||
|
import java.io.Closeable;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
public class ContentProvider implements Closeable {
|
||||||
|
|
||||||
|
public static final String TABLE_SYSTEM = "system";
|
||||||
|
public static final String TABLE_SECURE = "secure";
|
||||||
|
public static final String TABLE_GLOBAL = "global";
|
||||||
|
|
||||||
|
// See android/providerHolder/Settings.java
|
||||||
|
private static final String CALL_METHOD_GET_SYSTEM = "GET_system";
|
||||||
|
private static final String CALL_METHOD_GET_SECURE = "GET_secure";
|
||||||
|
private static final String CALL_METHOD_GET_GLOBAL = "GET_global";
|
||||||
|
|
||||||
|
private static final String CALL_METHOD_PUT_SYSTEM = "PUT_system";
|
||||||
|
private static final String CALL_METHOD_PUT_SECURE = "PUT_secure";
|
||||||
|
private static final String CALL_METHOD_PUT_GLOBAL = "PUT_global";
|
||||||
|
|
||||||
|
private static final String CALL_METHOD_USER_KEY = "_user";
|
||||||
|
|
||||||
|
private static final String NAME_VALUE_TABLE_VALUE = "value";
|
||||||
|
|
||||||
|
private final ActivityManager manager;
|
||||||
|
// android.content.IContentProvider
|
||||||
|
private final Object provider;
|
||||||
|
private final String name;
|
||||||
|
private final IBinder token;
|
||||||
|
|
||||||
|
private Method callMethod;
|
||||||
|
private boolean callMethodLegacy;
|
||||||
|
|
||||||
|
ContentProvider(ActivityManager manager, Object provider, String name, IBinder token) {
|
||||||
|
this.manager = manager;
|
||||||
|
this.provider = provider;
|
||||||
|
this.name = name;
|
||||||
|
this.token = token;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Method getCallMethod() throws NoSuchMethodException {
|
||||||
|
if (callMethod == null) {
|
||||||
|
try {
|
||||||
|
callMethod = provider.getClass().getMethod("call", String.class, String.class, String.class, String.class, Bundle.class);
|
||||||
|
} catch (NoSuchMethodException e) {
|
||||||
|
// old version
|
||||||
|
callMethod = provider.getClass().getMethod("call", String.class, String.class, String.class, Bundle.class);
|
||||||
|
callMethodLegacy = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return callMethod;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Bundle call(String callMethod, String arg, Bundle extras) {
|
||||||
|
try {
|
||||||
|
Method method = getCallMethod();
|
||||||
|
Object[] args;
|
||||||
|
if (!callMethodLegacy) {
|
||||||
|
args = new Object[]{ServiceManager.PACKAGE_NAME, "settings", callMethod, arg, extras};
|
||||||
|
} else {
|
||||||
|
args = new Object[]{ServiceManager.PACKAGE_NAME, callMethod, arg, extras};
|
||||||
|
}
|
||||||
|
return (Bundle) method.invoke(provider, args);
|
||||||
|
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
|
||||||
|
Ln.e("Could not invoke method", e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void close() {
|
||||||
|
manager.removeContentProviderExternal(name, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String getGetMethod(String table) {
|
||||||
|
switch (table) {
|
||||||
|
case TABLE_SECURE:
|
||||||
|
return CALL_METHOD_GET_SECURE;
|
||||||
|
case TABLE_SYSTEM:
|
||||||
|
return CALL_METHOD_GET_SYSTEM;
|
||||||
|
case TABLE_GLOBAL:
|
||||||
|
return CALL_METHOD_GET_GLOBAL;
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("Invalid table: " + table);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String getPutMethod(String table) {
|
||||||
|
switch (table) {
|
||||||
|
case TABLE_SECURE:
|
||||||
|
return CALL_METHOD_PUT_SECURE;
|
||||||
|
case TABLE_SYSTEM:
|
||||||
|
return CALL_METHOD_PUT_SYSTEM;
|
||||||
|
case TABLE_GLOBAL:
|
||||||
|
return CALL_METHOD_PUT_GLOBAL;
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("Invalid table: " + table);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getValue(String table, String key) {
|
||||||
|
String method = getGetMethod(table);
|
||||||
|
Bundle arg = new Bundle();
|
||||||
|
arg.putInt(CALL_METHOD_USER_KEY, ServiceManager.USER_ID);
|
||||||
|
Bundle bundle = call(method, key, arg);
|
||||||
|
if (bundle == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return bundle.getString("value");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void putValue(String table, String key, String value) {
|
||||||
|
String method = getPutMethod(table);
|
||||||
|
Bundle arg = new Bundle();
|
||||||
|
arg.putInt(CALL_METHOD_USER_KEY, ServiceManager.USER_ID);
|
||||||
|
arg.putString(NAME_VALUE_TABLE_VALUE, value);
|
||||||
|
call(method, key, arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAndPutValue(String table, String key, String value) {
|
||||||
|
String oldValue = getValue(table, key);
|
||||||
|
if (!value.equals(oldValue)) {
|
||||||
|
putValue(table, key, value);
|
||||||
|
}
|
||||||
|
return oldValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,6 +8,10 @@ import java.lang.reflect.Method;
|
|||||||
|
|
||||||
@SuppressLint("PrivateApi,DiscouragedPrivateApi")
|
@SuppressLint("PrivateApi,DiscouragedPrivateApi")
|
||||||
public final class ServiceManager {
|
public final class ServiceManager {
|
||||||
|
|
||||||
|
public static final String PACKAGE_NAME = "com.android.shell";
|
||||||
|
public static final int USER_ID = 0;
|
||||||
|
|
||||||
private final Method getServiceMethod;
|
private final Method getServiceMethod;
|
||||||
|
|
||||||
private WindowManager windowManager;
|
private WindowManager windowManager;
|
||||||
@@ -16,6 +20,7 @@ public final class ServiceManager {
|
|||||||
private PowerManager powerManager;
|
private PowerManager powerManager;
|
||||||
private StatusBarManager statusBarManager;
|
private StatusBarManager statusBarManager;
|
||||||
private ClipboardManager clipboardManager;
|
private ClipboardManager clipboardManager;
|
||||||
|
private ActivityManager activityManager;
|
||||||
|
|
||||||
public ServiceManager() {
|
public ServiceManager() {
|
||||||
try {
|
try {
|
||||||
@@ -76,4 +81,21 @@ public final class ServiceManager {
|
|||||||
}
|
}
|
||||||
return clipboardManager;
|
return clipboardManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ActivityManager getActivityManager() {
|
||||||
|
if (activityManager == null) {
|
||||||
|
try {
|
||||||
|
// On old Android versions, the ActivityManager is not exposed via AIDL,
|
||||||
|
// so use ActivityManagerNative.getDefault()
|
||||||
|
Class<?> cls = Class.forName("android.app.ActivityManagerNative");
|
||||||
|
Method getDefaultMethod = cls.getDeclaredMethod("getDefault");
|
||||||
|
IInterface am = (IInterface) getDefaultMethod.invoke(null);
|
||||||
|
activityManager = new ActivityManager(am);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new AssertionError(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return activityManager;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user