Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c8f5a70b authored by Wenhao Wang's avatar Wenhao Wang Committed by Android (Google) Code Review
Browse files

Merge " [BIC] Replace IPackageManager with PackageManager"

parents 49e55e1a b4fccbed
Loading
Loading
Loading
Loading
+20 −37
Original line number Diff line number Diff line
@@ -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<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)) {
@@ -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
+22 −24
Original line number Diff line number Diff line
@@ -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