Display and control your Android device
Refactor wrappers for Android SDK classes
Romain Vimontbdd05b4a16a2
diff --git a/server/src/main/java/com/genymobile/scrcpy/wrappers/ClipboardManager.java b/server/src/main/java/com/genymobile/scrcpy/wrappers/ClipboardManager.javaindex ebd89238..592bdf6b 100644--- a/server/src/main/java/com/genymobile/scrcpy/wrappers/ClipboardManager.java+++ b/server/src/main/java/com/genymobile/scrcpy/wrappers/ClipboardManager.java@@ -22,31 +22,23 @@ public class ClipboardManager {this.manager = manager;}- private Method getGetPrimaryClipMethod() {+ private Method getGetPrimaryClipMethod() throws NoSuchMethodException {if (getPrimaryClipMethod == null) {- try {- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {- getPrimaryClipMethod = manager.getClass().getMethod("getPrimaryClip", String.class);- } else {- getPrimaryClipMethod = manager.getClass().getMethod("getPrimaryClip", String.class, int.class);- }- } catch (NoSuchMethodException e) {- Ln.e("Could not find method", e);+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {+ getPrimaryClipMethod = manager.getClass().getMethod("getPrimaryClip", String.class);+ } else {+ getPrimaryClipMethod = manager.getClass().getMethod("getPrimaryClip", String.class, int.class);}}return getPrimaryClipMethod;}- private Method getSetPrimaryClipMethod() {+ private Method getSetPrimaryClipMethod() throws NoSuchMethodException {if (setPrimaryClipMethod == null) {- try {- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {- setPrimaryClipMethod = manager.getClass().getMethod("setPrimaryClip", ClipData.class, String.class);- } else {- setPrimaryClipMethod = manager.getClass().getMethod("setPrimaryClip", ClipData.class, String.class, int.class);- }- } catch (NoSuchMethodException e) {- Ln.e("Could not find method", e);+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {+ setPrimaryClipMethod = manager.getClass().getMethod("setPrimaryClip", ClipData.class, String.class);+ } else {+ setPrimaryClipMethod = manager.getClass().getMethod("setPrimaryClip", ClipData.class, String.class, int.class);}}return setPrimaryClipMethod;@@ -69,32 +61,26 @@ public class ClipboardManager {}public CharSequence getText() {- Method method = getGetPrimaryClipMethod();- if (method == null) {- return null;- }try {+ Method method = getGetPrimaryClipMethod();ClipData clipData = getPrimaryClip(method, manager);if (clipData == null || clipData.getItemCount() == 0) {return null;}return clipData.getItemAt(0).getText();- } catch (InvocationTargetException | IllegalAccessException e) {- Ln.e("Could not invoke " + method.getName(), e);+ } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {+ Ln.e("Could not invoke method", e);return null;}}public void setText(CharSequence text) {- Method method = getSetPrimaryClipMethod();- if (method == null) {- return;- }- ClipData clipData = ClipData.newPlainText(null, text);try {+ Method method = getSetPrimaryClipMethod();+ ClipData clipData = ClipData.newPlainText(null, text);setPrimaryClip(method, manager, clipData);- } catch (InvocationTargetException | IllegalAccessException e) {- Ln.e("Could not invoke " + method.getName(), e);+ } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {+ Ln.e("Could not invoke method", e);}}}diff --git a/server/src/main/java/com/genymobile/scrcpy/wrappers/InputManager.java b/server/src/main/java/com/genymobile/scrcpy/wrappers/InputManager.javaindex 788a04c7..44fa613b 100644--- a/server/src/main/java/com/genymobile/scrcpy/wrappers/InputManager.java+++ b/server/src/main/java/com/genymobile/scrcpy/wrappers/InputManager.java@@ -21,26 +21,19 @@ public final class InputManager {this.manager = manager;}- private Method getInjectInputEventMethod() {+ private Method getInjectInputEventMethod() throws NoSuchMethodException {if (injectInputEventMethod == null) {- try {- injectInputEventMethod = manager.getClass().getMethod("injectInputEvent", InputEvent.class, int.class);- } catch (NoSuchMethodException e) {- Ln.e("Could not find method", e);- }+ injectInputEventMethod = manager.getClass().getMethod("injectInputEvent", InputEvent.class, int.class);}return injectInputEventMethod;}public boolean injectInputEvent(InputEvent inputEvent, int mode) {- Method method = getInjectInputEventMethod();- if (method == null) {- return false;- }try {- return (Boolean) method.invoke(manager, inputEvent, mode);- } catch (InvocationTargetException | IllegalAccessException e) {- Ln.e("Could not invoke " + method.getName(), e);+ Method method = getInjectInputEventMethod();+ return (boolean) method.invoke(manager, inputEvent, mode);+ } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {+ Ln.e("Could not invoke method", e);return false;}}diff --git a/server/src/main/java/com/genymobile/scrcpy/wrappers/PowerManager.java b/server/src/main/java/com/genymobile/scrcpy/wrappers/PowerManager.javaindex 66acdba8..8ff074b3 100644--- a/server/src/main/java/com/genymobile/scrcpy/wrappers/PowerManager.java+++ b/server/src/main/java/com/genymobile/scrcpy/wrappers/PowerManager.java@@ -17,28 +17,21 @@ public final class PowerManager {this.manager = manager;}- private Method getIsScreenOnMethod() {+ private Method getIsScreenOnMethod() throws NoSuchMethodException {if (isScreenOnMethod == null) {- try {- @SuppressLint("ObsoleteSdkInt") // we may lower minSdkVersion in the future- String methodName = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH ? "isInteractive" : "isScreenOn";- isScreenOnMethod = manager.getClass().getMethod(methodName);- } catch (NoSuchMethodException e) {- Ln.e("Could not find method", e);- }+ @SuppressLint("ObsoleteSdkInt") // we may lower minSdkVersion in the future+ String methodName = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH ? "isInteractive" : "isScreenOn";+ isScreenOnMethod = manager.getClass().getMethod(methodName);}return isScreenOnMethod;}public boolean isScreenOn() {- Method method = getIsScreenOnMethod();- if (method == null) {- return false;- }try {- return (Boolean) method.invoke(manager);- } catch (InvocationTargetException | IllegalAccessException e) {- Ln.e("Could not invoke " + method.getName(), e);+ Method method = getIsScreenOnMethod();+ return (boolean) method.invoke(manager);+ } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {+ Ln.e("Could not invoke method", e);return false;}}diff --git a/server/src/main/java/com/genymobile/scrcpy/wrappers/StatusBarManager.java b/server/src/main/java/com/genymobile/scrcpy/wrappers/StatusBarManager.javaindex 670de952..6f8941bd 100644--- a/server/src/main/java/com/genymobile/scrcpy/wrappers/StatusBarManager.java+++ b/server/src/main/java/com/genymobile/scrcpy/wrappers/StatusBarManager.java@@ -17,49 +17,35 @@ public class StatusBarManager {this.manager = manager;}- private Method getExpandNotificationsPanelMethod() {+ private Method getExpandNotificationsPanelMethod() throws NoSuchMethodException {if (expandNotificationsPanelMethod == null) {- try {- expandNotificationsPanelMethod = manager.getClass().getMethod("expandNotificationsPanel");- } catch (NoSuchMethodException e) {- Ln.e("Could not find method", e);- }+ expandNotificationsPanelMethod = manager.getClass().getMethod("expandNotificationsPanel");}return expandNotificationsPanelMethod;}- private Method getCollapsePanelsMethod() {+ private Method getCollapsePanelsMethod() throws NoSuchMethodException {if (collapsePanelsMethod == null) {- try {- collapsePanelsMethod = manager.getClass().getMethod("collapsePanels");- } catch (NoSuchMethodException e) {- Ln.e("Could not find method", e);- }+ collapsePanelsMethod = manager.getClass().getMethod("collapsePanels");}return collapsePanelsMethod;}public void expandNotificationsPanel() {- Method method = getExpandNotificationsPanelMethod();- if (method == null) {- return;- }try {+ Method method = getExpandNotificationsPanelMethod();method.invoke(manager);- } catch (InvocationTargetException | IllegalAccessException e) {- Ln.e("Could not invoke " + method.getName(), e);+ } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {+ Ln.e("Could not invoke method", e);}}public void collapsePanels() {- Method method = getCollapsePanelsMethod();- if (method == null) {- return;- }try {+ Method method = getCollapsePanelsMethod();method.invoke(manager);- } catch (InvocationTargetException | IllegalAccessException e) {- Ln.e("Could not invoke " + method.getName(), e);+ } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {+ Ln.e("Could not invoke method", e);}}}diff --git a/server/src/main/java/com/genymobile/scrcpy/wrappers/SurfaceControl.java b/server/src/main/java/com/genymobile/scrcpy/wrappers/SurfaceControl.javaindex bef6e5d9..227bbc85 100644--- a/server/src/main/java/com/genymobile/scrcpy/wrappers/SurfaceControl.java+++ b/server/src/main/java/com/genymobile/scrcpy/wrappers/SurfaceControl.java@@ -84,29 +84,23 @@ public final class SurfaceControl {}}- private static Method getGetBuiltInDisplayMethod() {+ private static Method getGetBuiltInDisplayMethod() throws NoSuchMethodException {if (getBuiltInDisplayMethod == null) {- try {- // the method signature has changed in Android Q- // <https://github.com/Genymobile/scrcpy/issues/586>- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {- getBuiltInDisplayMethod = CLASS.getMethod("getBuiltInDisplay", int.class);- } else {- getBuiltInDisplayMethod = CLASS.getMethod("getInternalDisplayToken");- }- } catch (NoSuchMethodException e) {- Ln.e("Could not find method", e);+ // the method signature has changed in Android Q+ // <https://github.com/Genymobile/scrcpy/issues/586>+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {+ getBuiltInDisplayMethod = CLASS.getMethod("getBuiltInDisplay", int.class);+ } else {+ getBuiltInDisplayMethod = CLASS.getMethod("getInternalDisplayToken");}}return getBuiltInDisplayMethod;}public static IBinder getBuiltInDisplay() {- Method method = getGetBuiltInDisplayMethod();- if (method == null) {- return null;- }+try {+ Method method = getGetBuiltInDisplayMethod();if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {// call getBuiltInDisplay(0)return (IBinder) method.invoke(null, 0);@@ -114,32 +108,25 @@ public final class SurfaceControl {// call getInternalDisplayToken()return (IBinder) method.invoke(null);- } catch (InvocationTargetException | IllegalAccessException e) {- Ln.e("Could not invoke " + method.getName(), e);+ } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {+ Ln.e("Could not invoke method", e);return null;}}- private static Method getSetDisplayPowerModeMethod() {+ private static Method getSetDisplayPowerModeMethod() throws NoSuchMethodException {if (setDisplayPowerModeMethod == null) {- try {- setDisplayPowerModeMethod = CLASS.getMethod("setDisplayPowerMode", IBinder.class, int.class);- } catch (NoSuchMethodException e) {- Ln.e("Could not find method", e);- }+ setDisplayPowerModeMethod = CLASS.getMethod("setDisplayPowerMode", IBinder.class, int.class);}return setDisplayPowerModeMethod;}public static void setDisplayPowerMode(IBinder displayToken, int mode) {- Method method = getSetDisplayPowerModeMethod();- if (method == null) {- return;- }try {+ Method method = getSetDisplayPowerModeMethod();method.invoke(null, displayToken, mode);- } catch (InvocationTargetException | IllegalAccessException e) {- Ln.e("Could not invoke " + method.getName(), e);+ } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {+ Ln.e("Could not invoke method", e);}}