Compare commits
12 commits
Author | SHA1 | Date | |
---|---|---|---|
ee93d2aac1 | |||
c29a0bf675 | |||
411bb0d18e | |||
cc0902b13c | |||
48b572c272 | |||
94feae71f2 | |||
67170437f1 | |||
9cb14b5166 | |||
9cb8766220 | |||
fd4ec784e0 | |||
52cebe1597 | |||
6a27062f48 |
|
@ -234,6 +234,12 @@ connect_to_server(struct sc_server *server, uint32_t attempts, sc_tick delay) {
|
||||||
|
|
||||||
net_close(socket);
|
net_close(socket);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (sc_intr_is_interrupted(&server->intr)) {
|
||||||
|
// Stop immediately
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (attempts) {
|
if (attempts) {
|
||||||
sc_mutex_lock(&server->mutex);
|
sc_mutex_lock(&server->mutex);
|
||||||
sc_tick deadline = sc_tick_now() + delay;
|
sc_tick deadline = sc_tick_now() + delay;
|
||||||
|
|
|
@ -1,5 +1,10 @@
|
||||||
|
// <https://devblogs.microsoft.com/oldnewthing/20111216-00/?p=8873>
|
||||||
|
#define _WIN32_WINNT 0x0600 // For extended process API
|
||||||
|
|
||||||
#include "util/process.h"
|
#include "util/process.h"
|
||||||
|
|
||||||
|
#include <processthreadsapi.h>
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
|
@ -26,6 +31,9 @@ sc_process_execute_p(const char *const argv[], HANDLE *handle,
|
||||||
HANDLE *pin, HANDLE *pout, HANDLE *perr) {
|
HANDLE *pin, HANDLE *pout, HANDLE *perr) {
|
||||||
enum sc_process_result ret = SC_PROCESS_ERROR_GENERIC;
|
enum sc_process_result ret = SC_PROCESS_ERROR_GENERIC;
|
||||||
|
|
||||||
|
// Add 1 per non-NULL pointer
|
||||||
|
unsigned handle_count = !!pin + !!pout + !!perr;
|
||||||
|
|
||||||
SECURITY_ATTRIBUTES sa;
|
SECURITY_ATTRIBUTES sa;
|
||||||
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
|
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
|
||||||
sa.lpSecurityDescriptor = NULL;
|
sa.lpSecurityDescriptor = NULL;
|
||||||
|
@ -65,45 +73,94 @@ sc_process_execute_p(const char *const argv[], HANDLE *handle,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
STARTUPINFOW si;
|
STARTUPINFOEXW si;
|
||||||
PROCESS_INFORMATION pi;
|
PROCESS_INFORMATION pi;
|
||||||
memset(&si, 0, sizeof(si));
|
memset(&si, 0, sizeof(si));
|
||||||
si.cb = sizeof(si);
|
si.StartupInfo.cb = sizeof(si);
|
||||||
if (pin || pout || perr) {
|
HANDLE handles[3];
|
||||||
si.dwFlags = STARTF_USESTDHANDLES;
|
|
||||||
|
LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList = NULL;
|
||||||
|
if (handle_count) {
|
||||||
|
si.StartupInfo.dwFlags = STARTF_USESTDHANDLES;
|
||||||
if (pin) {
|
if (pin) {
|
||||||
si.hStdInput = stdin_read_handle;
|
si.StartupInfo.hStdInput = stdin_read_handle;
|
||||||
}
|
}
|
||||||
if (pout) {
|
if (pout) {
|
||||||
si.hStdOutput = stdout_write_handle;
|
si.StartupInfo.hStdOutput = stdout_write_handle;
|
||||||
}
|
}
|
||||||
if (perr) {
|
if (perr) {
|
||||||
si.hStdError = stderr_write_handle;
|
si.StartupInfo.hStdError = stderr_write_handle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SIZE_T size;
|
||||||
|
// Call it once to know the required buffer size
|
||||||
|
BOOL ok =
|
||||||
|
InitializeProcThreadAttributeList(NULL, 1, 0, &size)
|
||||||
|
|| GetLastError() == ERROR_INSUFFICIENT_BUFFER;
|
||||||
|
if (!ok) {
|
||||||
|
goto error_close_stderr;
|
||||||
|
}
|
||||||
|
|
||||||
|
lpAttributeList = malloc(size);
|
||||||
|
if (!lpAttributeList) {
|
||||||
|
goto error_close_stderr;
|
||||||
|
}
|
||||||
|
|
||||||
|
ok = InitializeProcThreadAttributeList(lpAttributeList, 1, 0, &size);
|
||||||
|
if (!ok) {
|
||||||
|
free(lpAttributeList);
|
||||||
|
goto error_close_stderr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Explicitly pass the HANDLEs that must be inherited
|
||||||
|
unsigned i = 0;
|
||||||
|
if (pin) {
|
||||||
|
handles[i++] = stdin_read_handle;
|
||||||
|
}
|
||||||
|
if (pout) {
|
||||||
|
handles[i++] = stdout_write_handle;
|
||||||
|
}
|
||||||
|
if (perr) {
|
||||||
|
handles[i++] = stderr_write_handle;
|
||||||
|
}
|
||||||
|
ok = UpdateProcThreadAttribute(lpAttributeList, 0,
|
||||||
|
PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
|
||||||
|
handles, handle_count * sizeof(HANDLE),
|
||||||
|
NULL, NULL);
|
||||||
|
if (!ok) {
|
||||||
|
goto error_free_attribute_list;
|
||||||
|
}
|
||||||
|
|
||||||
|
si.lpAttributeList = lpAttributeList;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *cmd = malloc(CMD_MAX_LEN);
|
char *cmd = malloc(CMD_MAX_LEN);
|
||||||
if (!cmd || !build_cmd(cmd, CMD_MAX_LEN, argv)) {
|
if (!cmd || !build_cmd(cmd, CMD_MAX_LEN, argv)) {
|
||||||
*handle = NULL;
|
goto error_free_attribute_list;
|
||||||
goto error_close_stderr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wchar_t *wide = sc_str_to_wchars(cmd);
|
wchar_t *wide = sc_str_to_wchars(cmd);
|
||||||
free(cmd);
|
free(cmd);
|
||||||
if (!wide) {
|
if (!wide) {
|
||||||
LOGC("Could not allocate wide char string");
|
LOGC("Could not allocate wide char string");
|
||||||
goto error_close_stderr;
|
goto error_free_attribute_list;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!CreateProcessW(NULL, wide, NULL, NULL, TRUE, 0, NULL, NULL, &si,
|
BOOL bInheritHandles = handle_count > 0;
|
||||||
&pi)) {
|
DWORD dwCreationFlags = handle_count > 0 ? EXTENDED_STARTUPINFO_PRESENT : 0;
|
||||||
free(wide);
|
BOOL ok = CreateProcessW(NULL, wide, NULL, NULL, bInheritHandles,
|
||||||
*handle = NULL;
|
dwCreationFlags, NULL, NULL, &si.StartupInfo, &pi);
|
||||||
|
free(wide);
|
||||||
|
if (!ok) {
|
||||||
if (GetLastError() == ERROR_FILE_NOT_FOUND) {
|
if (GetLastError() == ERROR_FILE_NOT_FOUND) {
|
||||||
ret = SC_PROCESS_ERROR_MISSING_BINARY;
|
ret = SC_PROCESS_ERROR_MISSING_BINARY;
|
||||||
}
|
}
|
||||||
goto error_close_stderr;
|
goto error_free_attribute_list;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lpAttributeList) {
|
||||||
|
DeleteProcThreadAttributeList(lpAttributeList);
|
||||||
|
free(lpAttributeList);
|
||||||
}
|
}
|
||||||
|
|
||||||
// These handles are used by the child process, close them for this process
|
// These handles are used by the child process, close them for this process
|
||||||
|
@ -117,11 +174,15 @@ sc_process_execute_p(const char *const argv[], HANDLE *handle,
|
||||||
CloseHandle(stderr_write_handle);
|
CloseHandle(stderr_write_handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(wide);
|
|
||||||
*handle = pi.hProcess;
|
*handle = pi.hProcess;
|
||||||
|
|
||||||
return SC_PROCESS_SUCCESS;
|
return SC_PROCESS_SUCCESS;
|
||||||
|
|
||||||
|
error_free_attribute_list:
|
||||||
|
if (lpAttributeList) {
|
||||||
|
DeleteProcThreadAttributeList(lpAttributeList);
|
||||||
|
free(lpAttributeList);
|
||||||
|
}
|
||||||
error_close_stderr:
|
error_close_stderr:
|
||||||
if (perr) {
|
if (perr) {
|
||||||
CloseHandle(*perr);
|
CloseHandle(*perr);
|
||||||
|
@ -168,7 +229,7 @@ sc_process_close(HANDLE handle) {
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t
|
ssize_t
|
||||||
sc_read_pipe(HANDLE pipe, char *data, size_t len) {
|
sc_pipe_read(HANDLE pipe, char *data, size_t len) {
|
||||||
DWORD r;
|
DWORD r;
|
||||||
if (!ReadFile(pipe, data, len, &r, NULL)) {
|
if (!ReadFile(pipe, data, len, &r, NULL)) {
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -177,7 +238,7 @@ sc_read_pipe(HANDLE pipe, char *data, size_t len) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
sc_close_pipe(HANDLE pipe) {
|
sc_pipe_close(HANDLE pipe) {
|
||||||
if (!CloseHandle(pipe)) {
|
if (!CloseHandle(pipe)) {
|
||||||
LOGW("Cannot close pipe");
|
LOGW("Cannot close pipe");
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package com.genymobile.scrcpy;
|
package com.genymobile.scrcpy;
|
||||||
|
|
||||||
import com.genymobile.scrcpy.wrappers.ContentProvider;
|
|
||||||
import com.genymobile.scrcpy.wrappers.ServiceManager;
|
import com.genymobile.scrcpy.wrappers.ServiceManager;
|
||||||
|
|
||||||
import android.os.Parcel;
|
import android.os.Parcel;
|
||||||
|
@ -166,14 +165,21 @@ public final class CleanUp {
|
||||||
|
|
||||||
if (config.disableShowTouches || config.restoreStayOn != -1) {
|
if (config.disableShowTouches || config.restoreStayOn != -1) {
|
||||||
ServiceManager serviceManager = new ServiceManager();
|
ServiceManager serviceManager = new ServiceManager();
|
||||||
try (ContentProvider settings = serviceManager.getActivityManager().createSettingsProvider()) {
|
Settings settings = new Settings(serviceManager);
|
||||||
if (config.disableShowTouches) {
|
if (config.disableShowTouches) {
|
||||||
Ln.i("Disabling \"show touches\"");
|
Ln.i("Disabling \"show touches\"");
|
||||||
settings.putValue(ContentProvider.TABLE_SYSTEM, "show_touches", "0");
|
try {
|
||||||
|
settings.putValue(Settings.TABLE_SYSTEM, "show_touches", "0");
|
||||||
|
} catch (SettingsException e) {
|
||||||
|
Ln.e("Could not restore \"show_touches\"", e);
|
||||||
}
|
}
|
||||||
if (config.restoreStayOn != -1) {
|
}
|
||||||
Ln.i("Restoring \"stay awake\"");
|
if (config.restoreStayOn != -1) {
|
||||||
settings.putValue(ContentProvider.TABLE_GLOBAL, "stay_on_while_plugged_in", String.valueOf(config.restoreStayOn));
|
Ln.i("Restoring \"stay awake\"");
|
||||||
|
try {
|
||||||
|
settings.putValue(Settings.TABLE_GLOBAL, "stay_on_while_plugged_in", String.valueOf(config.restoreStayOn));
|
||||||
|
} catch (SettingsException e) {
|
||||||
|
Ln.e("Could not restore \"stay_on_while_plugged_in\"", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
33
server/src/main/java/com/genymobile/scrcpy/Command.java
Normal file
33
server/src/main/java/com/genymobile/scrcpy/Command.java
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
package com.genymobile.scrcpy;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public final class Command {
|
||||||
|
private Command() {
|
||||||
|
// not instantiable
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void exec(String... cmd) throws IOException, InterruptedException {
|
||||||
|
Process process = Runtime.getRuntime().exec(cmd);
|
||||||
|
int exitCode = process.waitFor();
|
||||||
|
if (exitCode != 0) {
|
||||||
|
throw new IOException("Command " + Arrays.toString(cmd) + " returned with value " + exitCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String execReadLine(String... cmd) throws IOException, InterruptedException {
|
||||||
|
String result = null;
|
||||||
|
Process process = Runtime.getRuntime().exec(cmd);
|
||||||
|
Scanner scanner = new Scanner(process.getInputStream());
|
||||||
|
if (scanner.hasNextLine()) {
|
||||||
|
result = scanner.nextLine();
|
||||||
|
}
|
||||||
|
int exitCode = process.waitFor();
|
||||||
|
if (exitCode != 0) {
|
||||||
|
throw new IOException("Command " + Arrays.toString(cmd) + " returned with value " + exitCode);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,7 +1,6 @@
|
||||||
package com.genymobile.scrcpy;
|
package com.genymobile.scrcpy;
|
||||||
|
|
||||||
import com.genymobile.scrcpy.wrappers.ClipboardManager;
|
import com.genymobile.scrcpy.wrappers.ClipboardManager;
|
||||||
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;
|
||||||
|
@ -29,6 +28,7 @@ public final class Device {
|
||||||
public static final int LOCK_VIDEO_ORIENTATION_INITIAL = -2;
|
public static final int LOCK_VIDEO_ORIENTATION_INITIAL = -2;
|
||||||
|
|
||||||
private static final ServiceManager SERVICE_MANAGER = new ServiceManager();
|
private static final ServiceManager SERVICE_MANAGER = new ServiceManager();
|
||||||
|
private static final Settings SETTINGS = new Settings(SERVICE_MANAGER);
|
||||||
|
|
||||||
public interface RotationListener {
|
public interface RotationListener {
|
||||||
void onRotationChanged(int rotation);
|
void onRotationChanged(int rotation);
|
||||||
|
@ -296,7 +296,7 @@ public final class Device {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ContentProvider createSettingsProvider() {
|
public static Settings getSettings() {
|
||||||
return SERVICE_MANAGER.getActivityManager().createSettingsProvider();
|
return SETTINGS;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,13 +57,20 @@ public final class Ln {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void w(String message) {
|
public static void w(String message, Throwable throwable) {
|
||||||
if (isEnabled(Level.WARN)) {
|
if (isEnabled(Level.WARN)) {
|
||||||
Log.w(TAG, message);
|
Log.w(TAG, message, throwable);
|
||||||
System.out.println(PREFIX + "WARN: " + message);
|
System.out.println(PREFIX + "WARN: " + message);
|
||||||
|
if (throwable != null) {
|
||||||
|
throwable.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void w(String message) {
|
||||||
|
w(message, null);
|
||||||
|
}
|
||||||
|
|
||||||
public static void e(String message, Throwable throwable) {
|
public static void e(String message, Throwable throwable) {
|
||||||
if (isEnabled(Level.ERROR)) {
|
if (isEnabled(Level.ERROR)) {
|
||||||
Log.e(TAG, message, throwable);
|
Log.e(TAG, message, throwable);
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
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.media.MediaCodecInfo;
|
import android.media.MediaCodecInfo;
|
||||||
|
@ -19,24 +17,25 @@ public final class Server {
|
||||||
// not instantiable
|
// not instantiable
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void scrcpy(Options options) throws IOException {
|
private static void initAndCleanUp(Options options) {
|
||||||
Ln.i("Device: " + Build.MANUFACTURER + " " + Build.MODEL + " (Android " + Build.VERSION.RELEASE + ")");
|
|
||||||
final Device device = new Device(options);
|
|
||||||
List<CodecOption> codecOptions = CodecOption.parse(options.getCodecOptions());
|
|
||||||
|
|
||||||
boolean mustDisableShowTouchesOnCleanUp = false;
|
boolean mustDisableShowTouchesOnCleanUp = false;
|
||||||
int restoreStayOn = -1;
|
int restoreStayOn = -1;
|
||||||
if (options.getShowTouches() || options.getStayAwake()) {
|
if (options.getShowTouches() || options.getStayAwake()) {
|
||||||
try (ContentProvider settings = Device.createSettingsProvider()) {
|
Settings settings = Device.getSettings();
|
||||||
if (options.getShowTouches()) {
|
if (options.getShowTouches()) {
|
||||||
String oldValue = settings.getAndPutValue(ContentProvider.TABLE_SYSTEM, "show_touches", "1");
|
try {
|
||||||
|
String oldValue = settings.getAndPutValue(Settings.TABLE_SYSTEM, "show_touches", "1");
|
||||||
// If "show touches" was disabled, it must be disabled back on clean up
|
// If "show touches" was disabled, it must be disabled back on clean up
|
||||||
mustDisableShowTouchesOnCleanUp = !"1".equals(oldValue);
|
mustDisableShowTouchesOnCleanUp = !"1".equals(oldValue);
|
||||||
|
} catch (SettingsException e) {
|
||||||
|
Ln.e("Could not change \"show_touches\"", e);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (options.getStayAwake()) {
|
if (options.getStayAwake()) {
|
||||||
int stayOn = BatteryManager.BATTERY_PLUGGED_AC | BatteryManager.BATTERY_PLUGGED_USB | BatteryManager.BATTERY_PLUGGED_WIRELESS;
|
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 {
|
||||||
|
String oldValue = settings.getAndPutValue(Settings.TABLE_GLOBAL, "stay_on_while_plugged_in", String.valueOf(stayOn));
|
||||||
try {
|
try {
|
||||||
restoreStayOn = Integer.parseInt(oldValue);
|
restoreStayOn = Integer.parseInt(oldValue);
|
||||||
if (restoreStayOn == stayOn) {
|
if (restoreStayOn == stayOn) {
|
||||||
|
@ -46,11 +45,25 @@ public final class Server {
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
restoreStayOn = 0;
|
restoreStayOn = 0;
|
||||||
}
|
}
|
||||||
|
} catch (SettingsException e) {
|
||||||
|
Ln.e("Could not change \"stay_on_while_plugged_in\"", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CleanUp.configure(options.getDisplayId(), restoreStayOn, mustDisableShowTouchesOnCleanUp, true, options.getPowerOffScreenOnClose());
|
try {
|
||||||
|
CleanUp.configure(options.getDisplayId(), restoreStayOn, mustDisableShowTouchesOnCleanUp, true, options.getPowerOffScreenOnClose());
|
||||||
|
} catch (IOException e) {
|
||||||
|
Ln.e("Could not configure cleanup", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void scrcpy(Options options) throws IOException {
|
||||||
|
Ln.i("Device: " + Build.MANUFACTURER + " " + Build.MODEL + " (Android " + Build.VERSION.RELEASE + ")");
|
||||||
|
final Device device = new Device(options);
|
||||||
|
List<CodecOption> codecOptions = CodecOption.parse(options.getCodecOptions());
|
||||||
|
|
||||||
|
Thread initThread = startInitThread(options);
|
||||||
|
|
||||||
boolean tunnelForward = options.isTunnelForward();
|
boolean tunnelForward = options.isTunnelForward();
|
||||||
|
|
||||||
|
@ -82,6 +95,7 @@ public final class Server {
|
||||||
// this is expected on close
|
// this is expected on close
|
||||||
Ln.d("Screen streaming stopped");
|
Ln.d("Screen streaming stopped");
|
||||||
} finally {
|
} finally {
|
||||||
|
initThread.interrupt();
|
||||||
if (controllerThread != null) {
|
if (controllerThread != null) {
|
||||||
controllerThread.interrupt();
|
controllerThread.interrupt();
|
||||||
}
|
}
|
||||||
|
@ -92,6 +106,17 @@ public final class Server {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Thread startInitThread(final Options options) {
|
||||||
|
Thread thread = new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
initAndCleanUp(options);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
thread.start();
|
||||||
|
return thread;
|
||||||
|
}
|
||||||
|
|
||||||
private static Thread startController(final Controller controller) {
|
private static Thread startController(final Controller controller) {
|
||||||
Thread thread = new Thread(new Runnable() {
|
Thread thread = new Thread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
|
|
84
server/src/main/java/com/genymobile/scrcpy/Settings.java
Normal file
84
server/src/main/java/com/genymobile/scrcpy/Settings.java
Normal file
|
@ -0,0 +1,84 @@
|
||||||
|
package com.genymobile.scrcpy;
|
||||||
|
|
||||||
|
import com.genymobile.scrcpy.wrappers.ContentProvider;
|
||||||
|
import com.genymobile.scrcpy.wrappers.ServiceManager;
|
||||||
|
|
||||||
|
import android.os.Build;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class Settings {
|
||||||
|
|
||||||
|
public static final String TABLE_SYSTEM = ContentProvider.TABLE_SYSTEM;
|
||||||
|
public static final String TABLE_SECURE = ContentProvider.TABLE_SECURE;
|
||||||
|
public static final String TABLE_GLOBAL = ContentProvider.TABLE_GLOBAL;
|
||||||
|
|
||||||
|
private final ServiceManager serviceManager;
|
||||||
|
|
||||||
|
public Settings(ServiceManager serviceManager) {
|
||||||
|
this.serviceManager = serviceManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void execSettingsPut(String table, String key, String value) throws SettingsException {
|
||||||
|
try {
|
||||||
|
Command.exec("settings", "put", table, key, value);
|
||||||
|
} catch (IOException | InterruptedException e) {
|
||||||
|
throw new SettingsException("put", table, key, value, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String execSettingsGet(String table, String key) throws SettingsException {
|
||||||
|
try {
|
||||||
|
return Command.execReadLine("settings", "get", table, key);
|
||||||
|
} catch (IOException | InterruptedException e) {
|
||||||
|
throw new SettingsException("get", table, key, null, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getValue(String table, String key) throws SettingsException {
|
||||||
|
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.R) {
|
||||||
|
// on Android >= 12, it always fails: <https://github.com/Genymobile/scrcpy/issues/2788>
|
||||||
|
try (ContentProvider provider = serviceManager.getActivityManager().createSettingsProvider()) {
|
||||||
|
return provider.getValue(table, key);
|
||||||
|
} catch (SettingsException e) {
|
||||||
|
Ln.w("Could not get settings value via ContentProvider, fallback to settings process", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return execSettingsGet(table, key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void putValue(String table, String key, String value) throws SettingsException {
|
||||||
|
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.R) {
|
||||||
|
// on Android >= 12, it always fails: <https://github.com/Genymobile/scrcpy/issues/2788>
|
||||||
|
try (ContentProvider provider = serviceManager.getActivityManager().createSettingsProvider()) {
|
||||||
|
provider.putValue(table, key, value);
|
||||||
|
} catch (SettingsException e) {
|
||||||
|
Ln.w("Could not put settings value via ContentProvider, fallback to settings process", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
execSettingsPut(table, key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAndPutValue(String table, String key, String value) throws SettingsException {
|
||||||
|
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.R) {
|
||||||
|
// on Android >= 12, it always fails: <https://github.com/Genymobile/scrcpy/issues/2788>
|
||||||
|
try (ContentProvider provider = serviceManager.getActivityManager().createSettingsProvider()) {
|
||||||
|
String oldValue = provider.getValue(table, key);
|
||||||
|
if (!value.equals(oldValue)) {
|
||||||
|
provider.putValue(table, key, value);
|
||||||
|
}
|
||||||
|
return oldValue;
|
||||||
|
} catch (SettingsException e) {
|
||||||
|
Ln.w("Could not get and put settings value via ContentProvider, fallback to settings process", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String oldValue = getValue(table, key);
|
||||||
|
if (!value.equals(oldValue)) {
|
||||||
|
putValue(table, key, value);
|
||||||
|
}
|
||||||
|
return oldValue;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.genymobile.scrcpy;
|
||||||
|
|
||||||
|
public class SettingsException extends Exception {
|
||||||
|
private static String createMessage(String method, String table, String key, String value) {
|
||||||
|
return "Could not access settings: " + method + " " + table + " " + key + (value != null ? " " + value : "");
|
||||||
|
}
|
||||||
|
|
||||||
|
public SettingsException(String method, String table, String key, String value, Throwable cause) {
|
||||||
|
super(createMessage(method, table, key, value), cause);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
package com.genymobile.scrcpy.wrappers;
|
package com.genymobile.scrcpy.wrappers;
|
||||||
|
|
||||||
import com.genymobile.scrcpy.Ln;
|
import com.genymobile.scrcpy.Ln;
|
||||||
|
import com.genymobile.scrcpy.SettingsException;
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
@ -87,7 +88,8 @@ public class ContentProvider implements Closeable {
|
||||||
return attributionSource;
|
return attributionSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Bundle call(String callMethod, String arg, Bundle extras) {
|
private Bundle call(String callMethod, String arg, Bundle extras)
|
||||||
|
throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
|
||||||
try {
|
try {
|
||||||
Method method = getCallMethod();
|
Method method = getCallMethod();
|
||||||
Object[] args;
|
Object[] args;
|
||||||
|
@ -108,7 +110,7 @@ public class ContentProvider implements Closeable {
|
||||||
return (Bundle) method.invoke(provider, args);
|
return (Bundle) method.invoke(provider, args);
|
||||||
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException | ClassNotFoundException | InstantiationException e) {
|
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException | ClassNotFoundException | InstantiationException e) {
|
||||||
Ln.e("Could not invoke method", e);
|
Ln.e("Could not invoke method", e);
|
||||||
return null;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -142,30 +144,31 @@ public class ContentProvider implements Closeable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getValue(String table, String key) {
|
public String getValue(String table, String key) throws SettingsException {
|
||||||
String method = getGetMethod(table);
|
String method = getGetMethod(table);
|
||||||
Bundle arg = new Bundle();
|
Bundle arg = new Bundle();
|
||||||
arg.putInt(CALL_METHOD_USER_KEY, ServiceManager.USER_ID);
|
arg.putInt(CALL_METHOD_USER_KEY, ServiceManager.USER_ID);
|
||||||
Bundle bundle = call(method, key, arg);
|
try {
|
||||||
if (bundle == null) {
|
Bundle bundle = call(method, key, arg);
|
||||||
return null;
|
if (bundle == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return bundle.getString("value");
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new SettingsException(table, "get", key, null, e);
|
||||||
}
|
}
|
||||||
return bundle.getString("value");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void putValue(String table, String key, String value) {
|
public void putValue(String table, String key, String value) throws SettingsException {
|
||||||
String method = getPutMethod(table);
|
String method = getPutMethod(table);
|
||||||
Bundle arg = new Bundle();
|
Bundle arg = new Bundle();
|
||||||
arg.putInt(CALL_METHOD_USER_KEY, ServiceManager.USER_ID);
|
arg.putInt(CALL_METHOD_USER_KEY, ServiceManager.USER_ID);
|
||||||
arg.putString(NAME_VALUE_TABLE_VALUE, value);
|
arg.putString(NAME_VALUE_TABLE_VALUE, value);
|
||||||
call(method, key, arg);
|
try {
|
||||||
}
|
call(method, key, arg);
|
||||||
|
} catch (Exception e) {
|
||||||
public String getAndPutValue(String table, String key, String value) {
|
throw new SettingsException(table, "put", key, value, e);
|
||||||
String oldValue = getValue(table, key);
|
|
||||||
if (!value.equals(oldValue)) {
|
|
||||||
putValue(table, key, value);
|
|
||||||
}
|
}
|
||||||
return oldValue;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue