Loading services/core/java/com/android/server/pm/BackgroundInstallControlService.java +20 −37 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading @@ -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; Loading @@ -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; Loading @@ -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); Loading Loading @@ -131,16 +128,12 @@ public class BackgroundInstallControlService extends SystemService { @VisibleForTesting ParceledListSlice<PackageInfo> getBackgroundInstalledPackages( @PackageManager.PackageInfoFlagsBits long flags, int userId) { ParceledListSlice<PackageInfo> packages; try { packages = mIPackageManager.getInstalledPackages(flags, userId); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } List<PackageInfo> packages = mPackageManager.getInstalledPackagesAsUser( PackageManager.PackageInfoFlags.of(flags), userId); initBackgroundInstalledPackages(); ListIterator<PackageInfo> iter = packages.getList().listIterator(); ListIterator<PackageInfo> iter = packages.listIterator(); while (iter.hasNext()) { String packageName = iter.next().packageName; if (!mBackgroundInstalledPackages.contains(userId, packageName)) { Loading @@ -148,7 +141,7 @@ public class BackgroundInstallControlService extends SystemService { } } return packages; return new ParceledListSlice<>(packages); } private static class EventHandler extends Handler { Loading Loading @@ -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; } Loading Loading @@ -486,7 +469,7 @@ public class BackgroundInstallControlService extends SystemService { interface Injector { Context getContext(); IPackageManager getIPackageManager(); PackageManager getPackageManager(); PackageManagerInternal getPackageManagerInternal(); Loading @@ -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 Loading services/tests/servicestests/src/com/android/server/pm/BackgroundInstallControlServiceTest.java +22 −24 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading Loading @@ -103,7 +101,7 @@ public final class BackgroundInstallControlServiceTest { @Mock private Context mContext; @Mock private IPackageManager mIPackageManager; private PackageManager mPackageManager; @Mock private PackageManagerInternal mPackageManagerInternal; @Mock Loading Loading @@ -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); Loading @@ -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); Loading Loading @@ -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); Loading Loading @@ -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); Loading Loading @@ -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); Loading Loading @@ -808,8 +806,8 @@ public final class BackgroundInstallControlServiceTest { } @Override public IPackageManager getIPackageManager() { return mIPackageManager; public PackageManager getPackageManager() { return mPackageManager; } @Override Loading Loading
services/core/java/com/android/server/pm/BackgroundInstallControlService.java +20 −37 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading @@ -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; Loading @@ -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; Loading @@ -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); Loading Loading @@ -131,16 +128,12 @@ public class BackgroundInstallControlService extends SystemService { @VisibleForTesting ParceledListSlice<PackageInfo> getBackgroundInstalledPackages( @PackageManager.PackageInfoFlagsBits long flags, int userId) { ParceledListSlice<PackageInfo> packages; try { packages = mIPackageManager.getInstalledPackages(flags, userId); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } List<PackageInfo> packages = mPackageManager.getInstalledPackagesAsUser( PackageManager.PackageInfoFlags.of(flags), userId); initBackgroundInstalledPackages(); ListIterator<PackageInfo> iter = packages.getList().listIterator(); ListIterator<PackageInfo> iter = packages.listIterator(); while (iter.hasNext()) { String packageName = iter.next().packageName; if (!mBackgroundInstalledPackages.contains(userId, packageName)) { Loading @@ -148,7 +141,7 @@ public class BackgroundInstallControlService extends SystemService { } } return packages; return new ParceledListSlice<>(packages); } private static class EventHandler extends Handler { Loading Loading @@ -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; } Loading Loading @@ -486,7 +469,7 @@ public class BackgroundInstallControlService extends SystemService { interface Injector { Context getContext(); IPackageManager getIPackageManager(); PackageManager getPackageManager(); PackageManagerInternal getPackageManagerInternal(); Loading @@ -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 Loading
services/tests/servicestests/src/com/android/server/pm/BackgroundInstallControlServiceTest.java +22 −24 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading Loading @@ -103,7 +101,7 @@ public final class BackgroundInstallControlServiceTest { @Mock private Context mContext; @Mock private IPackageManager mIPackageManager; private PackageManager mPackageManager; @Mock private PackageManagerInternal mPackageManagerInternal; @Mock Loading Loading @@ -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); Loading @@ -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); Loading Loading @@ -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); Loading Loading @@ -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); Loading Loading @@ -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); Loading Loading @@ -808,8 +806,8 @@ public final class BackgroundInstallControlServiceTest { } @Override public IPackageManager getIPackageManager() { return mIPackageManager; public PackageManager getPackageManager() { return mPackageManager; } @Override Loading