[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_CURRENT_PACKAGE_CHANNEL = "stop-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 SCREEN_STATE_CHANNEL = "screen-state";
// List of all Parameters

View File

@ -120,12 +120,19 @@ public class AndroidDebugBridgeDevice {
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,
AndroidDebugBridgeDeviceReadException, TimeoutException, ExecutionException {
String devicesResp = runAdbShell("dumpsys", "power", "|", "grep", "'Display Power'");
if (devicesResp.contains("=")) {
try {
return devicesResp.split("=")[1].equals("ON");
var state = devicesResp.split("=")[1].trim();
return state.equals("ON");
} catch (NumberFormatException e) {
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 @Nullable ScheduledFuture<?> connectionCheckerSchedule;
private AndroidDebugBridgeMediaStatePackageConfig @Nullable [] packageConfigs = null;
private boolean deviceAwake = false;
public AndroidDebugBridgeHandler(Thing thing) {
super(thing);
@ -135,6 +136,12 @@ public class AndroidDebugBridgeHandler extends BaseThingHandler {
updateState(channelUID, new DecimalType(lock));
}
break;
case AWAKE_STATE_CHANNEL:
if (command instanceof RefreshType) {
boolean awakeState = adbConnection.isAwake();
updateState(channelUID, OnOffType.from(awakeState));
}
break;
case SCREEN_STATE_CHANNEL:
if (command instanceof RefreshType) {
boolean screenState = adbConnection.isScreenOn();
@ -277,6 +284,7 @@ public class AndroidDebugBridgeHandler extends BaseThingHandler {
} catch (AndroidDebugBridgeDeviceException e) {
logger.debug("Error connecting to device; [{}]: {}", e.getClass().getCanonicalName(),
e.getMessage());
adbConnection.disconnect();
updateStatus(ThingStatus.OFFLINE);
return;
}
@ -294,6 +302,23 @@ public class AndroidDebugBridgeHandler extends BaseThingHandler {
}
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 {
handleCommandInternal(new ChannelUID(this.thing.getUID(), MEDIA_VOLUME_CHANNEL), RefreshType.REFRESH);
} catch (AndroidDebugBridgeDeviceReadException e) {

View File

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