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

Commit 4080163a authored by Jiakai Zhang's avatar Jiakai Zhang Committed by Android (Google) Code Review
Browse files

Merge changes If9968708,Ic53352fa,Iadaa3d7d into main

* changes:
  Support creating a filtered snapshot from an existing snapshot computer.
  Remove unused code around getLastPackageUsageTimeInMills.
  Add back PackageUsage.java.
parents 252934f1 d5d21230
Loading
Loading
Loading
Loading
+0 −25
Original line number Diff line number Diff line
@@ -6891,10 +6891,6 @@ public class PackageParser {
        @UnsupportedAppUsage
        public int mPreferredOrder = 0;

        // For use by package manager to keep track of when a package was last used.
        public long[] mLastPackageUsageTimeInMills =
                new long[PackageManager.NOTIFY_PACKAGE_USE_REASONS_COUNT];

        // // User set enabled state.
        // public int mSetEnabled = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
        //
@@ -7281,27 +7277,6 @@ public class PackageParser {
            return true;
        }

        public long getLatestPackageUseTimeInMills() {
            long latestUse = 0L;
            for (long use : mLastPackageUsageTimeInMills) {
                latestUse = Math.max(latestUse, use);
            }
            return latestUse;
        }

        public long getLatestForegroundPackageUseTimeInMills() {
            int[] foregroundReasons = {
                PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY,
                PackageManager.NOTIFY_PACKAGE_USE_FOREGROUND_SERVICE
            };

            long latestUse = 0L;
            for (int reason : foregroundReasons) {
                latestUse = Math.max(latestUse, mLastPackageUsageTimeInMills[reason]);
            }
            return latestUse;
        }

        public String toString() {
            return "Package{"
                + Integer.toHexString(System.identityHashCode(this))
+1 −1
Original line number Diff line number Diff line
@@ -3118,7 +3118,7 @@ public class ComputerEngine implements Computer {
                }
                ipw.println("Dexopt state:");
                ipw.increaseIndent();
                DexOptHelper.dumpDexoptState(ipw, packageName);
                DexOptHelper.dumpDexoptState(ipw, this, packageName);
                ipw.decreaseIndent();
                break;
            }
+16 −7
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApexStagedEvent;
import android.content.pm.Flags;
import android.content.pm.IPackageManagerNative;
import android.content.pm.IStagedApexObserver;
import android.content.pm.PackageManager;
@@ -63,6 +64,7 @@ import com.android.server.pinner.PinnerService;
import com.android.server.pm.dex.InstallScenarioHelper;
import com.android.server.pm.local.PackageManagerLocalImpl;
import com.android.server.pm.pkg.AndroidPackage;
import com.android.server.pm.snapshot.PackageDataSnapshot;

import java.io.File;
import java.io.IOException;
@@ -163,17 +165,24 @@ public final class DexOptHelper {
    /**
     * Dumps the dexopt state for the given package, or all packages if it is null.
     */
    public static void dumpDexoptState(
            @NonNull IndentingPrintWriter ipw, @Nullable String packageName) {
    public static void dumpDexoptState(@NonNull IndentingPrintWriter ipw,
            @NonNull PackageDataSnapshot computer, @Nullable String packageName) {
        try (PackageManagerLocal.FilteredSnapshot snapshot =
                        getPackageManagerLocal().withFilteredSnapshot()) {
                        getPackageManagerLocal().withUnownedFilteredSnapshot(computer)) {
            if (packageName != null) {
                if (Flags.alternativeForDexoptCleanup()) {
                    // The caller has already vetted the package name against the computer, so
                    // IllegalArgumentException cannot happen.
                    DexOptHelper.getArtManagerLocal().dumpPackage(ipw, snapshot, packageName);
                } else {
                    try {
                        DexOptHelper.getArtManagerLocal().dumpPackage(ipw, snapshot, packageName);
                    } catch (IllegalArgumentException e) {
                    // Package isn't found, but that should only happen due to race.
                        // Package isn't found, but that should only happen due to race. It can
                        // happen because the snapshot is not created from the computer.
                        ipw.println(e);
                    }
                }
            } else {
                DexOptHelper.getArtManagerLocal().dump(ipw, snapshot);
            }
+10 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import android.os.UserHandle;

import com.android.server.pm.pkg.PackageState;
import com.android.server.pm.pkg.SharedUserApi;
import com.android.server.pm.snapshot.PackageDataSnapshot;

import java.io.IOException;
import java.lang.annotation.Retention;
@@ -125,6 +126,15 @@ public interface PackageManagerLocal {
    @NonNull
    FilteredSnapshot withFilteredSnapshot(int callingUid, @NonNull UserHandle user);

    /**
     * Same as {@link #withFilteredSnapshot()}, but from an existing snapshot computer. The created
     * object does not own the computer and is not responsible for releasing the resources that the
     * computer holds (if any).
     *
     * @hide
     */
    @NonNull FilteredSnapshot withUnownedFilteredSnapshot(@NonNull PackageDataSnapshot computer);

    /**
     * Add a pair of signing details so that packages signed with {@code oldSigningDetails} will
     * behave as if they are signed by the {@code newSigningDetails}.
+11 −6
Original line number Diff line number Diff line
@@ -967,6 +967,8 @@ public class PackageManagerService implements PackageSender, TestUtilityService
    private final @NonNull String mRequiredSdkSandboxPackage;
    private final @Nullable ComponentName mDeveloperVerificationServiceProvider;
    private final @Nullable String mDeveloperVerificationPolicyDelegatePackage;
    @GuardedBy("mLock")
    private final PackageUsage mPackageUsage = new PackageUsage();

    private final DomainVerificationConnection mDomainVerificationConnection;

@@ -2348,6 +2350,10 @@ public class PackageManagerService implements PackageSender, TestUtilityService
                setting.updateProcesses();
            }

            // Now that we know all the packages we are keeping,
            // read and update their last usage times.
            mPackageUsage.read(packageSettings);

            EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_SCAN_END,
                    SystemClock.uptimeMillis());
            Slog.i(TAG, "Time to scan packages: "
@@ -3069,6 +3075,8 @@ public class PackageManagerService implements PackageSender, TestUtilityService
        }

        synchronized (mLock) {
            mPackageUsage.writeNow(mSettings.getPackagesLocked());

            if (mHandler.hasMessages(WRITE_SETTINGS)
                    || mBackgroundHandler.hasMessages(WRITE_DIRTY_PACKAGE_RESTRICTIONS)
                    || mHandler.hasMessages(WRITE_PACKAGE_LIST)) {
@@ -5697,12 +5705,10 @@ public class PackageManagerService implements PackageSender, TestUtilityService
                return;
            }

            UserHandle user = Binder.getCallingUserHandle();
            DexUseManagerLocal dexUseManager = DexOptHelper.getDexUseManagerLocal();
            // TODO(chiuwinson): Retrieve filtered snapshot from Computer instance instead.
            try (PackageManagerLocal.FilteredSnapshot filteredSnapshot =
                            LocalManagerRegistry.getManager(PackageManagerLocal.class)
                            .withFilteredSnapshot(callingUid, user)) {
                                    .withUnownedFilteredSnapshot(snapshot)) {
                // This is called from binder, so exceptions thrown here are caught and handled
                // by it.
                dexUseManager.notifyDexContainersLoaded(
@@ -7654,10 +7660,9 @@ public class PackageManagerService implements PackageSender, TestUtilityService
        PackageManagerServiceUtils.enforceSystemOrRootOrShell(
                "Only the system or shell can delete oat artifacts");

        // TODO(chiuwinson): Retrieve filtered snapshot from Computer instance instead.
        try (PackageManagerLocal.FilteredSnapshot filteredSnapshot =
                        PackageManagerServiceUtils.getPackageManagerLocal()
                                .withFilteredSnapshot()) {
                                .withUnownedFilteredSnapshot(snapshot)) {
            try {
                DeleteResult res = DexOptHelper.getArtManagerLocal().deleteDexoptArtifacts(
                        filteredSnapshot, packageName);
Loading