Skip to content

Commit

Permalink
[androiddebugbridge] check device awake state and minor fixes (openha…
Browse files Browse the repository at this point in the history
…b#10106)

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

Signed-off-by: Miguel <miguelwork92@gmail.com>
  • Loading branch information
GiviMAD authored Feb 9, 2021
1 parent 717ef59 commit f8d5ae0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,19 @@ public String getCurrentPackage() throws AndroidDebugBridgeDeviceException, Inte
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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -135,6 +136,12 @@ private void handleCommandInternal(ChannelUID channelUID, Command command)
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();
Expand Down Expand Up @@ -277,6 +284,7 @@ public void checkConnection() {
} catch (AndroidDebugBridgeDeviceException e) {
logger.debug("Error connecting to device; [{}]: {}", e.getClass().getCanonicalName(),
e.getMessage());
adbConnection.disconnect();
updateStatus(ThingStatus.OFFLINE);
return;
}
Expand All @@ -294,6 +302,23 @@ public void checkConnection() {
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down

0 comments on commit f8d5ae0

Please sign in to comment.