From b4fccbed2cbe0499853037bb4fe1ac000094d18b Mon Sep 17 00:00:00 2001 From: Wenhao Wang Date: Thu, 15 Dec 2022 08:04:52 +0000 Subject: [PATCH] [BIC] Replace IPackageManager with PackageManager Leverage PackageManager APIs instead of IPackageManager aidl interface to call getInstallSourceInfo and getApplicationInfo. Update the corresponding tests. Bug: 238451991 Test: BackgroundInstallControlServiceTest Change-Id: I82834002cc7ef5907c5ccd49e13873860fac5b69 --- .../pm/BackgroundInstallControlService.java | 57 +++++++------------ .../BackgroundInstallControlServiceTest.java | 46 +++++++-------- 2 files changed, 42 insertions(+), 61 deletions(-) diff --git a/services/core/java/com/android/server/pm/BackgroundInstallControlService.java b/services/core/java/com/android/server/pm/BackgroundInstallControlService.java index d4c4c694a6c57..db3a3434388ec 100644 --- a/services/core/java/com/android/server/pm/BackgroundInstallControlService.java +++ b/services/core/java/com/android/server/pm/BackgroundInstallControlService.java @@ -22,8 +22,6 @@ import android.app.usage.UsageStatsManagerInternal; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.IBackgroundInstallControlService; -import android.content.pm.IPackageManager; -import android.content.pm.InstallSourceInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManagerInternal; @@ -32,8 +30,6 @@ import android.os.Environment; import android.os.Handler; import android.os.Looper; import android.os.Message; -import android.os.RemoteException; -import android.os.ServiceManager; import android.os.SystemClock; import android.os.UserHandle; import android.util.ArraySet; @@ -54,6 +50,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.util.List; import java.util.ListIterator; import java.util.Set; import java.util.TreeSet; @@ -75,7 +72,7 @@ public class BackgroundInstallControlService extends SystemService { private final Context mContext; private final BinderService mBinderService; - private final IPackageManager mIPackageManager; + private final PackageManager mPackageManager; private final PackageManagerInternal mPackageManagerInternal; private final UsageStatsManagerInternal mUsageStatsManagerInternal; private final PermissionManagerServiceInternal mPermissionManager; @@ -98,7 +95,7 @@ public class BackgroundInstallControlService extends SystemService { BackgroundInstallControlService(@NonNull Injector injector) { super(injector.getContext()); mContext = injector.getContext(); - mIPackageManager = injector.getIPackageManager(); + mPackageManager = injector.getPackageManager(); mPackageManagerInternal = injector.getPackageManagerInternal(); mPermissionManager = injector.getPermissionManager(); mHandler = new EventHandler(injector.getLooper(), this); @@ -131,16 +128,12 @@ public class BackgroundInstallControlService extends SystemService { @VisibleForTesting ParceledListSlice getBackgroundInstalledPackages( @PackageManager.PackageInfoFlagsBits long flags, int userId) { - ParceledListSlice packages; - try { - packages = mIPackageManager.getInstalledPackages(flags, userId); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } + List packages = mPackageManager.getInstalledPackagesAsUser( + PackageManager.PackageInfoFlags.of(flags), userId); initBackgroundInstalledPackages(); - ListIterator iter = packages.getList().listIterator(); + ListIterator iter = packages.listIterator(); while (iter.hasNext()) { String packageName = iter.next().packageName; if (!mBackgroundInstalledPackages.contains(userId, packageName)) { @@ -148,7 +141,7 @@ public class BackgroundInstallControlService extends SystemService { } } - return packages; + return new ParceledListSlice<>(packages); } private static class EventHandler extends Handler { @@ -181,31 +174,21 @@ public class BackgroundInstallControlService extends SystemService { } void handlePackageAdd(String packageName, int userId) { - InstallSourceInfo installSourceInfo = null; + ApplicationInfo appInfo = null; try { - installSourceInfo = mIPackageManager.getInstallSourceInfo(packageName); - } catch (RemoteException e) { - // Failed to talk to PackageManagerService Should never happen! - throw e.rethrowFromSystemServer(); - } - String installerPackageName = - installSourceInfo == null ? null : installSourceInfo.getInstallingPackageName(); - if (installerPackageName == null) { - Slog.w(TAG, "fails to get installerPackageName for " + packageName); + appInfo = mPackageManager.getApplicationInfoAsUser(packageName, + PackageManager.ApplicationInfoFlags.of(0), userId); + } catch (PackageManager.NameNotFoundException e) { + Slog.w(TAG, "Package's appInfo not found " + packageName); return; } - ApplicationInfo appInfo = null; + String installerPackageName = null; try { - appInfo = mIPackageManager.getApplicationInfo(packageName, - 0, userId); - } catch (RemoteException e) { - // Failed to talk to PackageManagerService Should never happen! - throw e.rethrowFromSystemServer(); - } - - if (appInfo == null) { - Slog.w(TAG, "fails to get appInfo for " + packageName); + installerPackageName = mPackageManager + .getInstallSourceInfo(packageName).getInstallingPackageName(); + } catch (PackageManager.NameNotFoundException e) { + Slog.w(TAG, "Package's installer not found " + packageName); return; } @@ -486,7 +469,7 @@ public class BackgroundInstallControlService extends SystemService { interface Injector { Context getContext(); - IPackageManager getIPackageManager(); + PackageManager getPackageManager(); PackageManagerInternal getPackageManagerInternal(); @@ -512,8 +495,8 @@ public class BackgroundInstallControlService extends SystemService { } @Override - public IPackageManager getIPackageManager() { - return IPackageManager.Stub.asInterface(ServiceManager.getService("package")); + public PackageManager getPackageManager() { + return mContext.getPackageManager(); } @Override diff --git a/services/tests/servicestests/src/com/android/server/pm/BackgroundInstallControlServiceTest.java b/services/tests/servicestests/src/com/android/server/pm/BackgroundInstallControlServiceTest.java index 54fa272dd3b66..d80aa57111998 100644 --- a/services/tests/servicestests/src/com/android/server/pm/BackgroundInstallControlServiceTest.java +++ b/services/tests/servicestests/src/com/android/server/pm/BackgroundInstallControlServiceTest.java @@ -22,8 +22,8 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; -import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doReturn; @@ -37,12 +37,10 @@ import android.app.usage.UsageStatsManagerInternal; import android.app.usage.UsageStatsManagerInternal.UsageEventListener; import android.content.Context; import android.content.pm.ApplicationInfo; -import android.content.pm.IPackageManager; import android.content.pm.InstallSourceInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManagerInternal; -import android.content.pm.ParceledListSlice; import android.os.FileUtils; import android.os.Looper; import android.os.RemoteException; @@ -103,7 +101,7 @@ public final class BackgroundInstallControlServiceTest { @Mock private Context mContext; @Mock - private IPackageManager mIPackageManager; + private PackageManager mPackageManager; @Mock private PackageManagerInternal mPackageManagerInternal; @Mock @@ -538,19 +536,19 @@ public final class BackgroundInstallControlServiceTest { @Test public void testHandleUsageEvent_packageAddedNoUsageEvent() throws - RemoteException, NoSuchFieldException { + NoSuchFieldException, PackageManager.NameNotFoundException { assertNull(mBackgroundInstallControlService.getBackgroundInstalledPackages()); InstallSourceInfo installSourceInfo = new InstallSourceInfo( /* initiatingPackageName = */ null, /* initiatingPackageSigningInfo = */ null, /* originatingPackageName = */ null, /* installingPackageName = */ INSTALLER_NAME_1); assertEquals(installSourceInfo.getInstallingPackageName(), INSTALLER_NAME_1); - when(mIPackageManager.getInstallSourceInfo(anyString())).thenReturn(installSourceInfo); + when(mPackageManager.getInstallSourceInfo(anyString())).thenReturn(installSourceInfo); ApplicationInfo appInfo = mock(ApplicationInfo.class); - when(mIPackageManager.getApplicationInfo( + when(mPackageManager.getApplicationInfoAsUser( eq(PACKAGE_NAME_1), - eq(0L), + any(), anyInt()) ).thenReturn(appInfo); @@ -574,19 +572,19 @@ public final class BackgroundInstallControlServiceTest { @Test public void testHandleUsageEvent_packageAddedInsideTimeFrame() throws - RemoteException, NoSuchFieldException { + NoSuchFieldException, PackageManager.NameNotFoundException { assertNull(mBackgroundInstallControlService.getBackgroundInstalledPackages()); InstallSourceInfo installSourceInfo = new InstallSourceInfo( /* initiatingPackageName = */ null, /* initiatingPackageSigningInfo = */ null, /* originatingPackageName = */ null, /* installingPackageName = */ INSTALLER_NAME_1); assertEquals(installSourceInfo.getInstallingPackageName(), INSTALLER_NAME_1); - when(mIPackageManager.getInstallSourceInfo(anyString())).thenReturn(installSourceInfo); + when(mPackageManager.getInstallSourceInfo(anyString())).thenReturn(installSourceInfo); ApplicationInfo appInfo = mock(ApplicationInfo.class); - when(mIPackageManager.getApplicationInfo( + when(mPackageManager.getApplicationInfoAsUser( eq(PACKAGE_NAME_1), - eq(0L), + any(), anyInt()) ).thenReturn(appInfo); @@ -618,19 +616,19 @@ public final class BackgroundInstallControlServiceTest { @Test public void testHandleUsageEvent_packageAddedOutsideTimeFrame1() throws - RemoteException, NoSuchFieldException { + NoSuchFieldException, PackageManager.NameNotFoundException { assertNull(mBackgroundInstallControlService.getBackgroundInstalledPackages()); InstallSourceInfo installSourceInfo = new InstallSourceInfo( /* initiatingPackageName = */ null, /* initiatingPackageSigningInfo = */ null, /* originatingPackageName = */ null, /* installingPackageName = */ INSTALLER_NAME_1); assertEquals(installSourceInfo.getInstallingPackageName(), INSTALLER_NAME_1); - when(mIPackageManager.getInstallSourceInfo(anyString())).thenReturn(installSourceInfo); + when(mPackageManager.getInstallSourceInfo(anyString())).thenReturn(installSourceInfo); ApplicationInfo appInfo = mock(ApplicationInfo.class); - when(mIPackageManager.getApplicationInfo( + when(mPackageManager.getApplicationInfoAsUser( eq(PACKAGE_NAME_1), - eq(0L), + any(), anyInt()) ).thenReturn(appInfo); @@ -666,19 +664,19 @@ public final class BackgroundInstallControlServiceTest { } @Test public void testHandleUsageEvent_packageAddedOutsideTimeFrame2() throws - RemoteException, NoSuchFieldException { + NoSuchFieldException, PackageManager.NameNotFoundException { assertNull(mBackgroundInstallControlService.getBackgroundInstalledPackages()); InstallSourceInfo installSourceInfo = new InstallSourceInfo( /* initiatingPackageName = */ null, /* initiatingPackageSigningInfo = */ null, /* originatingPackageName = */ null, /* installingPackageName = */ INSTALLER_NAME_1); assertEquals(installSourceInfo.getInstallingPackageName(), INSTALLER_NAME_1); - when(mIPackageManager.getInstallSourceInfo(anyString())).thenReturn(installSourceInfo); + when(mPackageManager.getInstallSourceInfo(anyString())).thenReturn(installSourceInfo); ApplicationInfo appInfo = mock(ApplicationInfo.class); - when(mIPackageManager.getApplicationInfo( + when(mPackageManager.getApplicationInfoAsUser( eq(PACKAGE_NAME_1), - eq(0L), + any(), anyInt()) ).thenReturn(appInfo); @@ -760,8 +758,8 @@ public final class BackgroundInstallControlServiceTest { packages.add(packageInfo2); var packageInfo3 = makePackageInfo(PACKAGE_NAME_3); packages.add(packageInfo3); - doReturn(new ParceledListSlice<>(packages)).when(mIPackageManager).getInstalledPackages( - anyLong(), anyInt()); + doReturn(packages).when(mPackageManager).getInstalledPackagesAsUser( + any(), anyInt()); var resultPackages = mBackgroundInstallControlService.getBackgroundInstalledPackages(0L, USER_ID_1); @@ -808,8 +806,8 @@ public final class BackgroundInstallControlServiceTest { } @Override - public IPackageManager getIPackageManager() { - return mIPackageManager; + public PackageManager getPackageManager() { + return mPackageManager; } @Override