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

Commit 3bf53c60 authored by JW Wang's avatar JW Wang
Browse files

getVersionedPackage can't see packages installed on the 2nd user

See b/161410257#comment1 for the details.

We need to pass MATCH_ANY_USER to ensure packages installed on the
2nd user are visiable to PackageWatchdog so it can observe packages
with rollback enabled correctly.

Bug: 161410257
Test: atest MultiUserRollbackTest#testBadUpdateRollback
Change-Id: Ie06eadfe14e003675b1898208f907ba7bb1d86b7
parent 838dea54
Loading
Loading
Loading
Loading
+23 −2
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import static java.lang.annotation.RetentionPolicy.SOURCE;
import android.annotation.IntDef;
import android.annotation.Nullable;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.VersionedPackage;
import android.net.ConnectivityModuleConnector;
@@ -306,6 +307,8 @@ public class PackageWatchdog {
            MonitoredPackage pkg = newMonitoredPackage(packageNames.get(i), durationMs, false);
            if (pkg != null) {
                packages.add(pkg);
            } else {
                Slog.w(TAG, "Failed to create MonitoredPackage for pkg=" + packageNames.get(i));
            }
        }

@@ -861,6 +864,25 @@ public class PackageWatchdog {
        });
    }

    /**
     * Gets PackageInfo for the given package. Matches any user and apex.
     *
     * @throws PackageManager.NameNotFoundException if no such package is installed.
     */
    private PackageInfo getPackageInfo(String packageName)
            throws PackageManager.NameNotFoundException {
        PackageManager pm = mContext.getPackageManager();
        try {
            // The MATCH_ANY_USER flag doesn't mix well with the MATCH_APEX
            // flag, so make two separate attempts to get the package info.
            // We don't need both flags at the same time because we assume
            // apex files are always installed for all users.
            return pm.getPackageInfo(packageName, PackageManager.MATCH_ANY_USER);
        } catch (PackageManager.NameNotFoundException e) {
            return pm.getPackageInfo(packageName, PackageManager.MATCH_APEX);
        }
    }

    @Nullable
    private VersionedPackage getVersionedPackage(String packageName) {
        final PackageManager pm = mContext.getPackageManager();
@@ -868,8 +890,7 @@ public class PackageWatchdog {
            return null;
        }
        try {
            final long versionCode = pm.getPackageInfo(
                    packageName, 0 /* flags */).getLongVersionCode();
            final long versionCode = getPackageInfo(packageName).getLongVersionCode();
            return new VersionedPackage(packageName, versionCode);
        } catch (PackageManager.NameNotFoundException e) {
            return null;
+9 −0
Original line number Diff line number Diff line
@@ -87,6 +87,13 @@ public class MultiUserRollbackTest extends BaseHostJUnit4Test {
        runPhaseForUsers("testStagedRollback_Phase4", mSecondaryUserId);
    }

    @Test
    public void testBadUpdateRollback() throws Exception {
        // Need to switch user in order to send broadcasts in device tests
        assertTrue(getDevice().switchUser(mSecondaryUserId));
        runPhaseForUsers("testBadUpdateRollback", mSecondaryUserId);
    }

    @Test
    public void testMultipleUsers() throws Exception {
        runPhaseForUsers("testMultipleUsersInstallV1", mOriginalUserId, mSecondaryUserId);
@@ -111,6 +118,8 @@ public class MultiUserRollbackTest extends BaseHostJUnit4Test {

    private void removeSecondaryUserIfNecessary() throws Exception {
        if (mSecondaryUserId != -1) {
            // Can't remove the 2nd user without switching out of it
            assertTrue(getDevice().switchUser(mOriginalUserId));
            getDevice().removeUser(mSecondaryUserId);
            mSecondaryUserId = -1;
        }
+5 −0
Original line number Diff line number Diff line
@@ -70,6 +70,11 @@ public class MultiUserRollbackTest {
        new RollbackTest().testBasic();
    }

    @Test
    public void testBadUpdateRollback() throws Exception {
        new RollbackTest().testBadUpdateRollback();
    }

    /**
     * Install version 1 of the test app. This method is run for both users.
     */