Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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