Passing correct user id to the getInstallerPackageName api

Fixes the issue that the device failed to uninstall the package
in the work profile.

Bug: 253637749
Test: uninstall a profile app manually
Change-Id: Idfe753f0a49de06aa4710063c77e04225e37ac76
This commit is contained in:
Rhed Jao
2022-10-17 16:40:40 +08:00
parent a918fd1767
commit 158b681b23
5 changed files with 15 additions and 14 deletions

View File

@@ -479,7 +479,7 @@ public interface Computer extends PackageDataSnapshot {
boolean getBlockUninstallForUser(@NonNull String packageName, @UserIdInt int userId);
@Nullable
String getInstallerPackageName(@NonNull String packageName);
String getInstallerPackageName(@NonNull String packageName, @UserIdInt int userId);
@Nullable
InstallSourceInfo getInstallSourceInfo(@NonNull String packageName);

View File

@@ -5244,9 +5244,9 @@ public class ComputerEngine implements Computer {
@Nullable
@Override
public String getInstallerPackageName(@NonNull String packageName) {
public String getInstallerPackageName(@NonNull String packageName, @UserIdInt int userId) {
final int callingUid = Binder.getCallingUid();
final InstallSource installSource = getInstallSource(packageName, callingUid);
final InstallSource installSource = getInstallSource(packageName, callingUid, userId);
if (installSource == null) {
throw new IllegalArgumentException("Unknown package: " + packageName);
}
@@ -5262,7 +5262,8 @@ public class ComputerEngine implements Computer {
}
@Nullable
private InstallSource getInstallSource(@NonNull String packageName, int callingUid) {
private InstallSource getInstallSource(@NonNull String packageName, int callingUid,
int userId) {
final PackageStateInternal ps = mSettings.getPackage(packageName);
// Installer info for Apex is not stored in PackageManager
@@ -5270,8 +5271,7 @@ public class ComputerEngine implements Computer {
return InstallSource.EMPTY;
}
if (ps == null || shouldFilterApplicationIncludingUninstalled(
ps, callingUid, UserHandle.getUserId(callingUid))) {
if (ps == null || shouldFilterApplicationIncludingUninstalled(ps, callingUid, userId)) {
return null;
}
@@ -5288,7 +5288,7 @@ public class ComputerEngine implements Computer {
String initiatingPackageName;
String originatingPackageName;
final InstallSource installSource = getInstallSource(packageName, callingUid);
final InstallSource installSource = getInstallSource(packageName, callingUid, userId);
if (installSource == null) {
return null;
}

View File

@@ -705,7 +705,8 @@ final class DeletePackageHelper {
final int uid = Binder.getCallingUid();
if (!isOrphaned(snapshot, internalPackageName)
&& !allowSilentUninstall
&& !isCallerAllowedToSilentlyUninstall(snapshot, uid, internalPackageName)) {
&& !isCallerAllowedToSilentlyUninstall(
snapshot, uid, internalPackageName, userId)) {
mPm.mHandler.post(() -> {
try {
final Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);
@@ -816,15 +817,15 @@ final class DeletePackageHelper {
}
private boolean isCallerAllowedToSilentlyUninstall(@NonNull Computer snapshot, int callingUid,
String pkgName) {
String pkgName, int userId) {
if (callingUid == Process.SHELL_UID || callingUid == Process.ROOT_UID
|| UserHandle.getAppId(callingUid) == Process.SYSTEM_UID) {
return true;
}
final int callingUserId = UserHandle.getUserId(callingUid);
// If the caller installed the pkgName, then allow it to silently uninstall.
if (callingUid == snapshot.getPackageUid(snapshot.getInstallerPackageName(pkgName), 0,
callingUserId)) {
if (callingUid == snapshot.getPackageUid(
snapshot.getInstallerPackageName(pkgName, userId), 0, callingUserId)) {
return true;
}

View File

@@ -492,7 +492,7 @@ public abstract class IPackageManagerBase extends IPackageManager.Stub {
@Override
@Deprecated
public final String getInstallerPackageName(@NonNull String packageName) {
return snapshot().getInstallerPackageName(packageName);
return snapshot().getInstallerPackageName(packageName, UserHandle.getCallingUserId());
}
@Override

View File

@@ -72,12 +72,12 @@ final class PackageManagerNative extends IPackageManagerNative.Stub {
@Override
public String getInstallerForPackage(String packageName) throws RemoteException {
final Computer snapshot = mPm.snapshotComputer();
final String installerName = snapshot.getInstallerPackageName(packageName);
final int callingUser = UserHandle.getUserId(Binder.getCallingUid());
final String installerName = snapshot.getInstallerPackageName(packageName, callingUser);
if (!TextUtils.isEmpty(installerName)) {
return installerName;
}
// differentiate between preload and sideload
int callingUser = UserHandle.getUserId(Binder.getCallingUid());
ApplicationInfo appInfo = snapshot.getApplicationInfo(packageName,
/*flags*/ 0,
/*userId*/ callingUser);