Display and control your Android device
Wrap settings management into a Settings class
Romain Vimont67170437f1eb
diff --git a/server/src/main/java/com/genymobile/scrcpy/CleanUp.java b/server/src/main/java/com/genymobile/scrcpy/CleanUp.javaindex ec61a1c0..9001eef7 100644--- a/server/src/main/java/com/genymobile/scrcpy/CleanUp.java+++ b/server/src/main/java/com/genymobile/scrcpy/CleanUp.java@@ -1,6 +1,5 @@package com.genymobile.scrcpy;-import com.genymobile.scrcpy.wrappers.ContentProvider;import com.genymobile.scrcpy.wrappers.ServiceManager;import android.os.Parcel;@@ -166,15 +165,14 @@ public final class CleanUp {if (config.disableShowTouches || config.restoreStayOn != -1) {ServiceManager serviceManager = new ServiceManager();- try (ContentProvider settings = serviceManager.getActivityManager().createSettingsProvider()) {- if (config.disableShowTouches) {- Ln.i("Disabling \"show touches\"");- settings.putValue(ContentProvider.TABLE_SYSTEM, "show_touches", "0");- }- if (config.restoreStayOn != -1) {- Ln.i("Restoring \"stay awake\"");- settings.putValue(ContentProvider.TABLE_GLOBAL, "stay_on_while_plugged_in", String.valueOf(config.restoreStayOn));- }+ Settings settings = new Settings(serviceManager);+ if (config.disableShowTouches) {+ Ln.i("Disabling \"show touches\"");+ settings.putValue(Settings.TABLE_SYSTEM, "show_touches", "0");+ }+ if (config.restoreStayOn != -1) {+ Ln.i("Restoring \"stay awake\"");+ settings.putValue(Settings.TABLE_GLOBAL, "stay_on_while_plugged_in", String.valueOf(config.restoreStayOn));}}diff --git a/server/src/main/java/com/genymobile/scrcpy/Device.java b/server/src/main/java/com/genymobile/scrcpy/Device.javaindex 3e71fe9c..093646e2 100644--- a/server/src/main/java/com/genymobile/scrcpy/Device.java+++ b/server/src/main/java/com/genymobile/scrcpy/Device.java@@ -1,7 +1,6 @@package com.genymobile.scrcpy;import com.genymobile.scrcpy.wrappers.ClipboardManager;-import com.genymobile.scrcpy.wrappers.ContentProvider;import com.genymobile.scrcpy.wrappers.InputManager;import com.genymobile.scrcpy.wrappers.ServiceManager;import com.genymobile.scrcpy.wrappers.SurfaceControl;@@ -29,6 +28,7 @@ public final class Device {public static final int LOCK_VIDEO_ORIENTATION_INITIAL = -2;private static final ServiceManager SERVICE_MANAGER = new ServiceManager();+ private static final Settings SETTINGS = new Settings(SERVICE_MANAGER);public interface RotationListener {void onRotationChanged(int rotation);@@ -296,7 +296,7 @@ public final class Device {}}- public static ContentProvider createSettingsProvider() {- return SERVICE_MANAGER.getActivityManager().createSettingsProvider();+ public static Settings getSettings() {+ return SETTINGS;}}diff --git a/server/src/main/java/com/genymobile/scrcpy/Server.java b/server/src/main/java/com/genymobile/scrcpy/Server.javaindex fdd9db88..c900f872 100644--- a/server/src/main/java/com/genymobile/scrcpy/Server.java+++ b/server/src/main/java/com/genymobile/scrcpy/Server.java@@ -1,7 +1,5 @@package com.genymobile.scrcpy;-import com.genymobile.scrcpy.wrappers.ContentProvider;-import android.graphics.Rect;import android.media.MediaCodec;import android.media.MediaCodecInfo;@@ -27,25 +25,24 @@ public final class Server {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);- }+ Settings settings = Device.getSettings();+ if (options.getShowTouches()) {+ String oldValue = settings.getAndPutValue(Settings.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;+ if (options.getStayAwake()) {+ int stayOn = BatteryManager.BATTERY_PLUGGED_AC | BatteryManager.BATTERY_PLUGGED_USB | BatteryManager.BATTERY_PLUGGED_WIRELESS;+ String oldValue = settings.getAndPutValue(Settings.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;}}}diff --git a/server/src/main/java/com/genymobile/scrcpy/Settings.java b/server/src/main/java/com/genymobile/scrcpy/Settings.javanew file mode 100644index 00000000..b59188d5--- /dev/null+++ b/server/src/main/java/com/genymobile/scrcpy/Settings.java@@ -0,0 +1,39 @@+package com.genymobile.scrcpy;++import com.genymobile.scrcpy.wrappers.ContentProvider;+import com.genymobile.scrcpy.wrappers.ServiceManager;++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;+ }++ public String getValue(String table, String key) {+ try (ContentProvider provider = serviceManager.getActivityManager().createSettingsProvider()) {+ return provider.getValue(table, key);+ }+ }++ public void putValue(String table, String key, String value) {+ try (ContentProvider provider = serviceManager.getActivityManager().createSettingsProvider()) {+ provider.putValue(table, key, value);+ }+ }++ public String getAndPutValue(String table, String key, String value) {+ try (ContentProvider provider = serviceManager.getActivityManager().createSettingsProvider()) {+ String oldValue = provider.getValue(table, key);+ if (!value.equals(oldValue)) {+ provider.putValue(table, key, value);+ }+ return oldValue;+ }+ }+}diff --git a/server/src/main/java/com/genymobile/scrcpy/wrappers/ContentProvider.java b/server/src/main/java/com/genymobile/scrcpy/wrappers/ContentProvider.javaindex 387c7a60..ab95f0df 100644--- a/server/src/main/java/com/genymobile/scrcpy/wrappers/ContentProvider.java+++ b/server/src/main/java/com/genymobile/scrcpy/wrappers/ContentProvider.java@@ -160,12 +160,4 @@ public class ContentProvider implements Closeable {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;- }}