[androiddebugbridge] check device awake state and minor fixes (#10106)

* check device awake state and minor fixes
* avoid update awake channel when not linked

Signed-off-by: Miguel <miguelwork92@gmail.com>
This commit is contained in:
GiviMAD 2021-02-08 17:25:52 -08:00 committed by GitHub
parent 717ef5950a
commit f8d5ae081b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 1 deletions

View File

@ -42,6 +42,7 @@ public class AndroidDebugBridgeBindingConstants {
public static final String STOP_PACKAGE_CHANNEL = "stop-package"; public static final String STOP_PACKAGE_CHANNEL = "stop-package";
public static final String STOP_CURRENT_PACKAGE_CHANNEL = "stop-current-package"; public static final String STOP_CURRENT_PACKAGE_CHANNEL = "stop-current-package";
public static final String CURRENT_PACKAGE_CHANNEL = "current-package"; public static final String CURRENT_PACKAGE_CHANNEL = "current-package";
public static final String AWAKE_STATE_CHANNEL = "awake-state";
public static final String WAKE_LOCK_CHANNEL = "wake-lock"; public static final String WAKE_LOCK_CHANNEL = "wake-lock";
public static final String SCREEN_STATE_CHANNEL = "screen-state"; public static final String SCREEN_STATE_CHANNEL = "screen-state";
// List of all Parameters // List of all Parameters

View File

@ -120,12 +120,19 @@ public class AndroidDebugBridgeDevice {
throw new AndroidDebugBridgeDeviceReadException("can read package name"); throw new AndroidDebugBridgeDeviceReadException("can read package name");
} }
public boolean isAwake()
throws InterruptedException, AndroidDebugBridgeDeviceException, TimeoutException, ExecutionException {
String devicesResp = runAdbShell("dumpsys", "activity", "|", "grep", "mWakefulness");
return devicesResp.contains("mWakefulness=Awake");
}
public boolean isScreenOn() throws InterruptedException, AndroidDebugBridgeDeviceException, public boolean isScreenOn() throws InterruptedException, AndroidDebugBridgeDeviceException,
AndroidDebugBridgeDeviceReadException, TimeoutException, ExecutionException { AndroidDebugBridgeDeviceReadException, TimeoutException, ExecutionException {
String devicesResp = runAdbShell("dumpsys", "power", "|", "grep", "'Display Power'"); String devicesResp = runAdbShell("dumpsys", "power", "|", "grep", "'Display Power'");
if (devicesResp.contains("=")) { if (devicesResp.contains("=")) {
try { try {
return devicesResp.split("=")[1].equals("ON"); var state = devicesResp.split("=")[1].trim();
return state.equals("ON");
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
logger.debug("Unable to parse device wake lock: {}", e.getMessage()); logger.debug("Unable to parse device wake lock: {}", e.getMessage());
} }

View File

@ -59,6 +59,7 @@ public class AndroidDebugBridgeHandler extends BaseThingHandler {
private AndroidDebugBridgeConfiguration config = new AndroidDebugBridgeConfiguration(); private AndroidDebugBridgeConfiguration config = new AndroidDebugBridgeConfiguration();
private @Nullable ScheduledFuture<?> connectionCheckerSchedule; private @Nullable ScheduledFuture<?> connectionCheckerSchedule;
private AndroidDebugBridgeMediaStatePackageConfig @Nullable [] packageConfigs = null; private AndroidDebugBridgeMediaStatePackageConfig @Nullable [] packageConfigs = null;
private boolean deviceAwake = false;
public AndroidDebugBridgeHandler(Thing thing) { public AndroidDebugBridgeHandler(Thing thing) {
super(thing); super(thing);
@ -135,6 +136,12 @@ public class AndroidDebugBridgeHandler extends BaseThingHandler {
updateState(channelUID, new DecimalType(lock)); updateState(channelUID, new DecimalType(lock));
} }
break; break;
case AWAKE_STATE_CHANNEL:
if (command instanceof RefreshType) {
boolean awakeState = adbConnection.isAwake();
updateState(channelUID, OnOffType.from(awakeState));
}
break;
case SCREEN_STATE_CHANNEL: case SCREEN_STATE_CHANNEL:
if (command instanceof RefreshType) { if (command instanceof RefreshType) {
boolean screenState = adbConnection.isScreenOn(); boolean screenState = adbConnection.isScreenOn();
@ -277,6 +284,7 @@ public class AndroidDebugBridgeHandler extends BaseThingHandler {
} catch (AndroidDebugBridgeDeviceException e) { } catch (AndroidDebugBridgeDeviceException e) {
logger.debug("Error connecting to device; [{}]: {}", e.getClass().getCanonicalName(), logger.debug("Error connecting to device; [{}]: {}", e.getClass().getCanonicalName(),
e.getMessage()); e.getMessage());
adbConnection.disconnect();
updateStatus(ThingStatus.OFFLINE); updateStatus(ThingStatus.OFFLINE);
return; return;
} }
@ -294,6 +302,23 @@ public class AndroidDebugBridgeHandler extends BaseThingHandler {
} }
private void refreshStatus() throws InterruptedException, AndroidDebugBridgeDeviceException, ExecutionException { private void refreshStatus() throws InterruptedException, AndroidDebugBridgeDeviceException, ExecutionException {
boolean awakeState;
boolean prevDeviceAwake = deviceAwake;
try {
awakeState = adbConnection.isAwake();
deviceAwake = awakeState;
} catch (TimeoutException e) {
logger.warn("Unable to refresh awake state: Timeout");
return;
}
var awakeStateChannelUID = new ChannelUID(this.thing.getUID(), AWAKE_STATE_CHANNEL);
if (isLinked(awakeStateChannelUID)) {
updateState(awakeStateChannelUID, OnOffType.from(awakeState));
}
if (!awakeState && !prevDeviceAwake) {
logger.debug("device {} is sleeping", config.ip);
return;
}
try { try {
handleCommandInternal(new ChannelUID(this.thing.getUID(), MEDIA_VOLUME_CHANNEL), RefreshType.REFRESH); handleCommandInternal(new ChannelUID(this.thing.getUID(), MEDIA_VOLUME_CHANNEL), RefreshType.REFRESH);
} catch (AndroidDebugBridgeDeviceReadException e) { } catch (AndroidDebugBridgeDeviceReadException e) {

View File

@ -18,6 +18,7 @@
<channel id="current-package" typeId="current-package-channel"/> <channel id="current-package" typeId="current-package-channel"/>
<channel id="wake-lock" typeId="wake-lock-channel"/> <channel id="wake-lock" typeId="wake-lock-channel"/>
<channel id="screen-state" typeId="screen-state-channel"/> <channel id="screen-state" typeId="screen-state-channel"/>
<channel id="awake-state" typeId="awake-state-channel"/>
</channels> </channels>
<representation-property>serial</representation-property> <representation-property>serial</representation-property>
<config-description> <config-description>
@ -386,6 +387,13 @@
<state readOnly="true"/> <state readOnly="true"/>
</channel-type> </channel-type>
<channel-type id="awake-state-channel" advanced="true">
<item-type>Switch</item-type>
<label>Awake State</label>
<description>Awake State</description>
<state readOnly="true"/>
</channel-type>
<channel-type id="screen-state-channel" advanced="true"> <channel-type id="screen-state-channel" advanced="true">
<item-type>Switch</item-type> <item-type>Switch</item-type>
<label>Screen State</label> <label>Screen State</label>