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

fix(android): list installed packages on some Samsung devices #5713

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions lib/common/mobile/android/android-application-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,51 @@ export class AndroidApplicationManager extends ApplicationManagerBase {
}

public async getInstalledApplications(): Promise<string[]> {
const result =
(await this.adb.executeShellCommand(["pm", "list", "packages"])) || "";
let result = "";
try {
result = await this.adb.executeShellCommand(["pm", "list", "packages"]);
} catch (err) {
/**
* on some devices (Samsung) listing packages is prevented by a permission error
* notably, some system apps (bloatware) is installed under user 150
* and listing these packages results in a permission error.
* if this happens, we have to first list all the users, and then loop through
* all the users and trying to list packages for that specific user, ignoring
* any errors. These are all then concatenated together and parsed normally.
* This is a slower operation, so we only do it in case listing failed in the first place.
*/
const userIDs: string[] = [];
const users = await this.adb.executeShellCommand(["pm", "list", "users"]);
/**
* Users:
* UserInfo{0:Owner:c13} running
*/

const userIDRegex = /UserInfo{(\d+)[:}]/;
users.split(EOL).forEach((line: string) => {
const [, userID] = line.match(userIDRegex) ?? [];

if (userID) {
userIDs.push(userID);
}
});

for (let id of userIDs) {
try {
result +=
EOL +
(await this.adb.executeShellCommand([
"pm",
"list",
"packages",
"--user",
id,
]));
} catch (err) {
// ignore - likely permission denied.
}
}
}
const regex = /package:(.+)/;
return result
.split(EOL)
Expand Down