Add util function to get the APK version

Use "dumpsys package com.genymobile.scrcpy" to get the APK version.
This commit is contained in:
Romain Vimont
2022-06-09 15:39:39 +02:00
parent 00766b8ab6
commit a73a04da87
5 changed files with 126 additions and 0 deletions

View File

@@ -253,3 +253,34 @@ sc_adb_parse_installed_apk_path(char *str) {
return strdup(s);
}
char *
sc_adb_parse_installed_apk_version(const char *str) {
// str is the (beginning of the) output of `dumpsys package`
// We want to extract the version string from a line starting with 4 spaces
// then `versionName=` then the version string.
#define VERSION_NAME_PREFIX "\n versionName="
char *s = strstr(str, VERSION_NAME_PREFIX);
if (!s) {
// Not found
return NULL;
}
s+= sizeof(VERSION_NAME_PREFIX) - 1;
size_t len = strspn(s, "0123456789.");
if (!len) {
LOGW("Unexpected version name with no value");
return NULL;
}
char *version = malloc(len + 1);
if (!version) {
return NULL;
}
memcpy(version, s, len);
version[len] = '\0';
return version;
}