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

Commit 3f629940 authored by Martijn Coenen's avatar Martijn Coenen
Browse files

Delaying populating APEX cache until boot is completed.

To avoid taking a boot time hit.

Bug: 124528038
Test: boots
Change-Id: Id551bc686fc61c0d5e40b4b5a421baa48916c848
parent 9ab22eae
Loading
Loading
Loading
Loading
+36 −23
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import android.os.ServiceManager;
import android.os.ServiceManager.ServiceNotFoundException;
import android.util.Slog;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.util.IndentingPrintWriter;

import java.io.File;
@@ -48,7 +49,9 @@ import java.util.stream.Collectors;
class ApexManager {
    static final String TAG = "ApexManager";
    private final IApexService mApexService;
    private final Map<String, PackageInfo> mActivePackagesCache;
    private final Object mLock = new Object();
    @GuardedBy("mLock")
    private Map<String, PackageInfo> mActivePackagesCache;

    ApexManager() {
        try {
@@ -57,11 +60,13 @@ class ApexManager {
        } catch (ServiceNotFoundException e) {
            throw new IllegalStateException("Required service apexservice not available");
        }
        mActivePackagesCache = populateActivePackagesCache();
    }

    @NonNull
    private Map<String, PackageInfo> populateActivePackagesCache() {
    private void populateActivePackagesCacheIfNeeded() {
        synchronized (mLock) {
            if (mActivePackagesCache != null) {
                return;
            }
            try {
                List<PackageInfo> list = new ArrayList<>();
                final ApexInfo[] activePkgs = mApexService.getActivePackages();
@@ -78,12 +83,14 @@ class ApexManager {
                        throw new IllegalStateException("Unable to parse: " + ai, pe);
                    }
                }
            return list.stream().collect(Collectors.toMap(p -> p.packageName, Function.identity()));
                mActivePackagesCache = list.stream().collect(
                        Collectors.toMap(p -> p.packageName, Function.identity()));
            } catch (RemoteException re) {
                Slog.e(TAG, "Unable to retrieve packages from apexservice: " + re.toString());
                throw new RuntimeException(re);
            }
        }
    }

    /**
     * Retrieves information about an active APEX package.
@@ -96,6 +103,7 @@ class ApexManager {
     *         is not found.
     */
    @Nullable PackageInfo getActivePackage(String packageName) {
        populateActivePackagesCacheIfNeeded();
        return mActivePackagesCache.get(packageName);
    }

@@ -106,6 +114,7 @@ class ApexManager {
     *         active package.
     */
    Collection<PackageInfo> getActivePackages() {
        populateActivePackagesCacheIfNeeded();
        return mActivePackagesCache.values();
    }

@@ -217,7 +226,7 @@ class ApexManager {
        ipw.println("Active APEX packages:");
        ipw.increaseIndent();
        try {
            populateActivePackagesCache();
            populateActivePackagesCacheIfNeeded();
            for (PackageInfo pi : mActivePackagesCache.values()) {
                if (packageName != null && !packageName.equals(pi.packageName)) {
                    continue;
@@ -254,4 +263,8 @@ class ApexManager {
            ipw.println("Couldn't communicate with apexd.");
        }
    }

    public void onBootCompleted() {
        populateActivePackagesCacheIfNeeded();
    }
}
+4 −2
Original line number Diff line number Diff line
@@ -209,8 +209,10 @@ public class PackageInstallerService extends IPackageInstaller.Stub implements
        mStagingManager = new StagingManager(pm, this, am);
    }

    private void setBootCompleted()  {
    private void onBootCompleted()  {
        mBootCompleted = true;
        // Tell APEX manager about it as well
        mApexManager.onBootCompleted();
    }

    boolean isBootCompleted()  {
@@ -223,7 +225,7 @@ public class PackageInstallerService extends IPackageInstaller.Stub implements
        mContext.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                setBootCompleted();
                onBootCompleted();
                mContext.unregisterReceiver(this);
            }
        }, new IntentFilter(Intent.ACTION_BOOT_COMPLETED));