From 2a3a080a950244e59f81833f626289264fa149ca Mon Sep 17 00:00:00 2001 From: "Jan N. Klug" Date: Thu, 10 Oct 2024 17:50:49 +0200 Subject: [PATCH] [androiddebugbridge] Broken hdmi status on Fire TV Cube (#582) * fixed: missing Content-Length header (BodyPublisher.ofByteArrays JDK 17 issue) Signed-off-by: Tom Blum Signed-off-by: Jan N. Klug --- .../internal/AndroidDebugBridgeDevice.java | 41 ++++--------------- .../internal/AndroidDebugBridgeHandler.java | 13 +++--- 2 files changed, 14 insertions(+), 40 deletions(-) diff --git a/bundles/org.smarthomej.binding.androiddebugbridge/src/main/java/org/smarthomej/binding/androiddebugbridge/internal/AndroidDebugBridgeDevice.java b/bundles/org.smarthomej.binding.androiddebugbridge/src/main/java/org/smarthomej/binding/androiddebugbridge/internal/AndroidDebugBridgeDevice.java index c0c001d5b9..375ef50cd9 100644 --- a/bundles/org.smarthomej.binding.androiddebugbridge/src/main/java/org/smarthomej/binding/androiddebugbridge/internal/AndroidDebugBridgeDevice.java +++ b/bundles/org.smarthomej.binding.androiddebugbridge/src/main/java/org/smarthomej/binding/androiddebugbridge/internal/AndroidDebugBridgeDevice.java @@ -28,7 +28,6 @@ import java.util.Base64; import java.util.HashMap; import java.util.Map; -import java.util.Optional; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.ScheduledExecutorService; @@ -258,42 +257,19 @@ public boolean isAwake() throws InterruptedException, AndroidDebugBridgeDeviceEx public boolean isScreenOn() throws InterruptedException, AndroidDebugBridgeDeviceException, AndroidDebugBridgeDeviceReadException, TimeoutException, ExecutionException { - String result = runAdbShell("dumpsys", "power", "|", "grep", "'Display Power'"); - String[] splitResult = result.split("="); - if (splitResult.length >= 2) { - return "ON".equals(splitResult[1]); + String result = runAdbShell("dumpsys", "display", "|", "grep", "mScreenState"); + if (result.contains("mScreenState=")) { + return result.contains("mScreenState=ON"); } throw new AndroidDebugBridgeDeviceReadException(SCREEN_STATE_CHANNEL, result); } - public Optional isHDMIOn() throws InterruptedException, AndroidDebugBridgeDeviceException, + public boolean isHdmiOn() throws InterruptedException, AndroidDebugBridgeDeviceException, AndroidDebugBridgeDeviceReadException, TimeoutException, ExecutionException { - if (channelFallbackMap.get(HDMI_STATE_CHANNEL) == FallbackModes.LOGCAT) { - return isHDMIOnWithLogcat(); - } - String result = runAdbShell("cat", "/sys/devices/virtual/switch/hdmi/state"); - if ("0".equals(result) || "1".equals(result)) { - return Optional.of("1".equals(result)); - } else { - LOGGER.debug("set fallback {} for {}", FallbackModes.LOGCAT, HDMI_STATE_CHANNEL); - channelFallbackMap.put(HDMI_STATE_CHANNEL, FallbackModes.LOGCAT); - return isHDMIOnWithLogcat(); + String result = runAdbShell("dumpsys", "hdmi_control", "|", "grep", "mPowerStatus"); + if (result.contains("mPowerStatus:")) { + return result.contains("mPowerStatus: 0"); } - } - - private Optional isHDMIOnWithLogcat() throws InterruptedException, AndroidDebugBridgeDeviceException, - AndroidDebugBridgeDeviceReadException, TimeoutException, ExecutionException { - String result = runAdbShell("logcat", "-d", "|", "grep", "hdmi", "|", "grep", "SWITCH_STATE=", "|", "tail", - "-1"); - if (result.contains("SWITCH_STATE=")) { - return Optional.of(result.contains("SWITCH_STATE=1")); - } else if (result.isEmpty()) { - // IF THE DEVICE DO NOT SUPPORT THIS VALUE IN LOGCAT THE USER WILL NEVER KNOW THE CHANNEL WON'T WORK - // FIND A BETTER SOLUTION - return Optional.empty(); - } - LOGGER.debug("remove fallback for {}", HDMI_STATE_CHANNEL); - channelFallbackMap.remove(HDMI_STATE_CHANNEL); throw new AndroidDebugBridgeDeviceReadException(HDMI_STATE_CHANNEL, result); } @@ -548,7 +524,6 @@ public static class VolumeInfo { private enum FallbackModes { MONKEY, MONKEY_LEANBACK_LAUNCHER, - DUMPSYS_ACTIVITY_RECENTS, - LOGCAT, + DUMPSYS_ACTIVITY_RECENTS } } diff --git a/bundles/org.smarthomej.binding.androiddebugbridge/src/main/java/org/smarthomej/binding/androiddebugbridge/internal/AndroidDebugBridgeHandler.java b/bundles/org.smarthomej.binding.androiddebugbridge/src/main/java/org/smarthomej/binding/androiddebugbridge/internal/AndroidDebugBridgeHandler.java index 5b936ce622..d65676a2a8 100644 --- a/bundles/org.smarthomej.binding.androiddebugbridge/src/main/java/org/smarthomej/binding/androiddebugbridge/internal/AndroidDebugBridgeHandler.java +++ b/bundles/org.smarthomej.binding.androiddebugbridge/src/main/java/org/smarthomej/binding/androiddebugbridge/internal/AndroidDebugBridgeHandler.java @@ -214,13 +214,12 @@ private void handleCommandInternal(ChannelUID channelUID, Command command) break; case HDMI_STATE_CHANNEL: if (command instanceof RefreshType) { - adbConnection.isHDMIOn().ifPresent(hdmiState -> { - boolean lastHDMIState = (boolean) channelLastStateMap.getOrDefault(HDMI_STATE_CHANNEL, false); - if (hdmiState.equals(lastHDMIState)) { - updateState(channelUID, OnOffType.from(hdmiState)); - } - channelLastStateMap.put(HDMI_STATE_CHANNEL, hdmiState); - }); + boolean hdmiState = adbConnection.isHdmiOn(); + boolean lastHdmiState = (boolean) channelLastStateMap.getOrDefault(HDMI_STATE_CHANNEL, false); + if (hdmiState == lastHdmiState) { + updateState(channelUID, OnOffType.from(hdmiState)); + } + channelLastStateMap.put(HDMI_STATE_CHANNEL, hdmiState); } break; }