[androiddebugbridge] fix get current package and get mac address (#13390)

Signed-off-by: Miguel Álvarez <miguelwork92@gmail.com>

Signed-off-by: Miguel Álvarez <miguelwork92@gmail.com>
This commit is contained in:
GiviMAD 2022-09-18 17:44:59 +02:00 committed by GitHub
parent e8d20f0341
commit 798b3ede04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 5 deletions

View File

@ -68,6 +68,9 @@ public class AndroidDebugBridgeDevice {
"https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,4}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)$"); "https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,4}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)$");
private static final Pattern INPUT_EVENT_PATTERN = Pattern private static final Pattern INPUT_EVENT_PATTERN = Pattern
.compile("/(?<input>\\S+): (?<n1>\\S+) (?<n2>\\S+) (?<n3>\\S+)$", Pattern.MULTILINE); .compile("/(?<input>\\S+): (?<n1>\\S+) (?<n2>\\S+) (?<n3>\\S+)$", Pattern.MULTILINE);
private static final Pattern VERSION_PATTERN = Pattern
.compile("^(?<major>\\d+)(\\.)?(?<minor>\\d+)?(\\.)?(?<patch>\\*|\\d+)?");
private static final Pattern MAC_PATTERN = Pattern.compile("^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$");
private static final Pattern SECURE_SHELL_INPUT_PATTERN = Pattern.compile("^[^\\|\\&;\\\"]+$"); private static final Pattern SECURE_SHELL_INPUT_PATTERN = Pattern.compile("^[^\\|\\&;\\\"]+$");
@ -97,6 +100,9 @@ public class AndroidDebugBridgeDevice {
private @Nullable Socket socket; private @Nullable Socket socket;
private @Nullable AdbConnection connection; private @Nullable AdbConnection connection;
private @Nullable Future<String> commandFuture; private @Nullable Future<String> commandFuture;
private int majorVersionNumber = 0;
private int minorVersionNumber = 0;
private int patchVersionNumber = 0;
public AndroidDebugBridgeDevice(ScheduledExecutorService scheduler) { public AndroidDebugBridgeDevice(ScheduledExecutorService scheduler) {
this.scheduler = scheduler; this.scheduler = scheduler;
@ -201,7 +207,12 @@ public class AndroidDebugBridgeDevice {
public String getCurrentPackage() throws AndroidDebugBridgeDeviceException, InterruptedException, public String getCurrentPackage() throws AndroidDebugBridgeDeviceException, InterruptedException,
AndroidDebugBridgeDeviceReadException, TimeoutException, ExecutionException { AndroidDebugBridgeDeviceReadException, TimeoutException, ExecutionException {
var out = runAdbShell("dumpsys", "window", "windows", "|", "grep", "mFocusedApp"); String out;
if (isAtLeastVersion(10)) {
out = runAdbShell("dumpsys", "window", "displays", "|", "grep", "mFocusedApp");
} else {
out = runAdbShell("dumpsys", "window", "windows", "|", "grep", "mFocusedApp");
}
var targetLine = Arrays.stream(out.split("\n")).findFirst().orElse(""); var targetLine = Arrays.stream(out.split("\n")).findFirst().orElse("");
var lineParts = targetLine.split(" "); var lineParts = targetLine.split(" ");
if (lineParts.length >= 2) { if (lineParts.length >= 2) {
@ -293,6 +304,19 @@ public class AndroidDebugBridgeDevice {
return getDeviceProp("ro.build.version.release"); return getDeviceProp("ro.build.version.release");
} }
public void setAndroidVersion(String version) {
var matcher = VERSION_PATTERN.matcher(version);
if (!matcher.find()) {
logger.warn("Unable to parse android version");
return;
}
this.majorVersionNumber = Integer.parseInt(matcher.group("major"));
var minorMatch = matcher.group("minor");
var patchMatch = matcher.group("patch");
this.minorVersionNumber = minorMatch != null ? Integer.parseInt(minorMatch) : 0;
this.patchVersionNumber = patchMatch != null ? Integer.parseInt(patchMatch) : 0;
}
public String getBrand() throws AndroidDebugBridgeDeviceException, InterruptedException, public String getBrand() throws AndroidDebugBridgeDeviceException, InterruptedException,
AndroidDebugBridgeDeviceReadException, TimeoutException, ExecutionException { AndroidDebugBridgeDeviceReadException, TimeoutException, ExecutionException {
return getDeviceProp("ro.product.brand"); return getDeviceProp("ro.product.brand");
@ -305,7 +329,17 @@ public class AndroidDebugBridgeDevice {
public String getMacAddress() throws AndroidDebugBridgeDeviceException, InterruptedException, public String getMacAddress() throws AndroidDebugBridgeDeviceException, InterruptedException,
AndroidDebugBridgeDeviceReadException, TimeoutException, ExecutionException { AndroidDebugBridgeDeviceReadException, TimeoutException, ExecutionException {
return runAdbShell("cat", "/sys/class/net/wlan0/address").replace("\n", "").replace("\r", ""); var macAddress = runAdbShell("cat", "/sys/class/net/wlan0/address").replace("\n", "").replace("\r", "");
var matcher = MAC_PATTERN.matcher(macAddress);
if (!matcher.find()) {
macAddress = runAdbShell("ip", "address", "|", "grep", "-m", "1", "link/ether", "|", "awk", "'{print $2}'")
.replace("\n", "").replace("\r", "");
matcher = MAC_PATTERN.matcher(macAddress);
if (matcher.find()) {
return macAddress;
}
}
return "00:00:00:00:00:00";
} }
private String getDeviceProp(String name) throws AndroidDebugBridgeDeviceException, InterruptedException, private String getDeviceProp(String name) throws AndroidDebugBridgeDeviceException, InterruptedException,
@ -768,6 +802,19 @@ public class AndroidDebugBridgeDevice {
} }
} }
private boolean isAtLeastVersion(int major) {
return isAtLeastVersion(major, 0);
}
private boolean isAtLeastVersion(int major, int minor) {
return isAtLeastVersion(major, minor, 0);
}
private boolean isAtLeastVersion(int major, int minor, int patch) {
return majorVersionNumber > major || (majorVersionNumber == major
&& (minorVersionNumber > minor || (minorVersionNumber == minor && patchVersionNumber >= patch)));
}
public static class VolumeInfo { public static class VolumeInfo {
public int current; public int current;
public int min; public int min;

View File

@ -55,7 +55,6 @@ import com.google.gson.JsonSyntaxException;
*/ */
@NonNullByDefault @NonNullByDefault
public class AndroidDebugBridgeHandler extends BaseThingHandler { public class AndroidDebugBridgeHandler extends BaseThingHandler {
public static final String KEY_EVENT_PLAY = "126"; public static final String KEY_EVENT_PLAY = "126";
public static final String KEY_EVENT_PAUSE = "127"; public static final String KEY_EVENT_PAUSE = "127";
public static final String KEY_EVENT_NEXT = "87"; public static final String KEY_EVENT_NEXT = "87";
@ -323,6 +322,11 @@ public class AndroidDebugBridgeHandler extends BaseThingHandler {
} }
adbConnection.configure(currentConfig.ip, currentConfig.port, currentConfig.timeout, adbConnection.configure(currentConfig.ip, currentConfig.port, currentConfig.timeout,
currentConfig.recordDuration); currentConfig.recordDuration);
var androidVersion = thing.getProperties().get(Thing.PROPERTY_FIRMWARE_VERSION);
if (androidVersion != null) {
// configure android implementation to use
adbConnection.setAndroidVersion(androidVersion);
}
updateStatus(ThingStatus.UNKNOWN); updateStatus(ThingStatus.UNKNOWN);
connectionCheckerSchedule = scheduler.scheduleWithFixedDelay(this::checkConnection, 0, connectionCheckerSchedule = scheduler.scheduleWithFixedDelay(this::checkConnection, 0,
currentConfig.refreshTime, TimeUnit.SECONDS); currentConfig.refreshTime, TimeUnit.SECONDS);
@ -360,8 +364,11 @@ public class AndroidDebugBridgeHandler extends BaseThingHandler {
try { try {
logger.debug("Refresh device {} status", currentConfig.ip); logger.debug("Refresh device {} status", currentConfig.ip);
if (adbConnection.isConnected()) { if (adbConnection.isConnected()) {
if (!ThingStatus.ONLINE.equals(getThing().getStatus())) {
// refresh properties only on state changes
refreshProperties();
}
updateStatus(ThingStatus.ONLINE); updateStatus(ThingStatus.ONLINE);
refreshProperties();
refreshStatus(); refreshStatus();
} else { } else {
try { try {
@ -394,7 +401,10 @@ public class AndroidDebugBridgeHandler extends BaseThingHandler {
Map<String, String> editProperties = editProperties(); Map<String, String> editProperties = editProperties();
editProperties.put(Thing.PROPERTY_SERIAL_NUMBER, adbConnection.getSerialNo()); editProperties.put(Thing.PROPERTY_SERIAL_NUMBER, adbConnection.getSerialNo());
editProperties.put(Thing.PROPERTY_MODEL_ID, adbConnection.getModel()); editProperties.put(Thing.PROPERTY_MODEL_ID, adbConnection.getModel());
editProperties.put(Thing.PROPERTY_FIRMWARE_VERSION, adbConnection.getAndroidVersion()); var androidVersion = adbConnection.getAndroidVersion();
editProperties.put(Thing.PROPERTY_FIRMWARE_VERSION, androidVersion);
// refresh android version to use
adbConnection.setAndroidVersion(androidVersion);
editProperties.put(Thing.PROPERTY_VENDOR, adbConnection.getBrand()); editProperties.put(Thing.PROPERTY_VENDOR, adbConnection.getBrand());
try { try {
editProperties.put(Thing.PROPERTY_MAC_ADDRESS, adbConnection.getMacAddress()); editProperties.put(Thing.PROPERTY_MAC_ADDRESS, adbConnection.getMacAddress());