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

Commit 4829576f authored by Roman Birg's avatar Roman Birg
Browse files

Resources: cache themed resources



Change-Id: I408665180d19eb422386fc69bc9afa4bbe0d05d6
Signed-off-by: default avatarRoman Birg <roman@cyngn.com>
parent ec08e5fa
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -1673,10 +1673,11 @@ public final class ActivityThread {
    /**
     * Creates the top level resources for the given package.
     */
    Resources getTopLevelThemedResources(String resDir, int displayId, LoadedApk pkgInfo,
    Resources getTopLevelThemedResources(String resDir, int displayId,
                                         Configuration overrideConfiguration, LoadedApk pkgInfo,
                                         String pkgName, String themePkgName) {
        return mResourcesManager.getTopLevelThemedResources(resDir, displayId, pkgName,
                themePkgName, pkgInfo.getCompatibilityInfo(), null,
                themePkgName, overrideConfiguration, pkgInfo.getCompatibilityInfo(), null,
                pkgInfo.getApplicationInfo().isThemeable);
    }

+2 −1
Original line number Diff line number Diff line
@@ -976,7 +976,8 @@ final class ApplicationPackageManager extends PackageManager {

        Resources r = mContext.mMainThread.getTopLevelThemedResources(
                app.uid == Process.myUid() ? app.sourceDir : app.publicSourceDir,
                Display.DEFAULT_DISPLAY, mContext.mPackageInfo, app.packageName, themePkgName);
                Display.DEFAULT_DISPLAY, null, mContext.mPackageInfo, app.packageName,
                themePkgName);
        if (r != null) {
            return r;
        }
+2 −2
Original line number Diff line number Diff line
@@ -2346,8 +2346,8 @@ class ContextImpl extends Context {
                        packageInfo.getAppDir(), overrideConfiguration, compatInfo, activityToken,
                        mOuterContext, packageInfo.getApplicationInfo().isThemeable) :
                mResourcesManager.getTopLevelThemedResources(packageInfo.getResDir(), displayId,
                        packageInfo.getPackageName(), themePackageName, compatInfo, activityToken,
                        packageInfo.getApplicationInfo().isThemeable);
                        packageInfo.getPackageName(), themePackageName, overrideConfiguration,
                        compatInfo, activityToken, packageInfo.getApplicationInfo().isThemeable);
            }
        }
        mResources = resources;
+57 −12
Original line number Diff line number Diff line
@@ -177,7 +177,7 @@ public class ResourcesManager {
            Configuration overrideConfiguration, CompatibilityInfo compatInfo, IBinder token,
            Context context, boolean isThemeable) {
        final float scale = compatInfo.applicationScale;
        final ThemeConfig themeConfig = getThemeConfig();
        final ThemeConfig themeConfig = isThemeable ? getThemeConfig() : null;
        ResourcesKey key = new ResourcesKey(resDir, displayId, overrideConfiguration, scale,
                isThemeable, themeConfig, token);
        Resources r;
@@ -313,10 +313,36 @@ public class ResourcesManager {
     * @hide
     */
    public Resources getTopLevelThemedResources(String resDir, int displayId, String packageName,
            String themePackageName, CompatibilityInfo compatInfo, IBinder token,
            boolean isThemeable) {
            String themePackageName, Configuration overrideConfiguration,
            CompatibilityInfo compatInfo, IBinder token, boolean isThemeable) {
        Resources r;

        ThemeConfig themeConfig;
        if (isThemeable) {
            ThemeConfig.Builder builder = new ThemeConfig.Builder();
            builder.defaultOverlay(themePackageName);
            builder.defaultIcon(themePackageName);
            builder.defaultFont(themePackageName);
            themeConfig = builder.build();
        } else {
            themeConfig = null;
        }

        ResourcesKey key = new ResourcesKey(resDir, displayId, overrideConfiguration,
                compatInfo.applicationScale, isThemeable, isThemeable ? themeConfig : null, token);

        synchronized (this) {
            WeakReference<Resources> wr = mActiveResources.get(key);
            r = wr != null ? wr.get() : null;
            if (r != null && r.getAssets().isUpToDate()) {
                if (false) {
                    Slog.w(TAG, "Returning cached resources " + r + " " + resDir
                            + ": appScale=" + r.getCompatibilityInfo().applicationScale);
                }
                return r;
            }
        }

        AssetManager assets = new AssetManager();
        assets.setAppName(packageName);
        assets.setThemeSupport(isThemeable);
@@ -328,9 +354,15 @@ public class ResourcesManager {
        DisplayMetrics dm = getDisplayMetricsLocked(displayId);
        Configuration config;
        boolean isDefaultDisplay = (displayId == Display.DEFAULT_DISPLAY);
        if (!isDefaultDisplay) {
        final boolean hasOverrideConfig = key.hasOverrideConfiguration();
        if (!isDefaultDisplay || hasOverrideConfig) {
            config = new Configuration(getConfiguration());
            if (!isDefaultDisplay) {
                applyNonDefaultDisplayMetricsToConfigurationLocked(dm, config);
            }
            if (hasOverrideConfig) {
                config.updateFrom(key.mOverrideConfiguration);
            }
        } else {
            config = getConfiguration();
        }
@@ -338,12 +370,6 @@ public class ResourcesManager {
        boolean iconsAttached = false;
        if (isThemeable) {
            /* Attach theme information to the resulting AssetManager when appropriate. */
            ThemeConfig.Builder builder = new ThemeConfig.Builder();
            builder.defaultOverlay(themePackageName);
            builder.defaultIcon(themePackageName);
            builder.defaultFont(themePackageName);

            ThemeConfig themeConfig = builder.build();
            attachThemeAssets(assets, themeConfig);
            attachCommonAssets(assets, themeConfig);
            iconsAttached = attachIconAssets(assets, themeConfig);
@@ -351,8 +377,27 @@ public class ResourcesManager {
        r = new Resources(assets, dm, config, compatInfo, token);
        if (iconsAttached) setActivityIcons(r);

        if (false) {
            Slog.i(TAG, "Created THEMED app resources " + resDir + " " + r + ": "
                    + r.getConfiguration() + " appScale="
                    + r.getCompatibilityInfo().applicationScale);
        }

        synchronized (this) {
            WeakReference<Resources> wr = mActiveResources.get(key);
            Resources existing = wr != null ? wr.get() : null;
            if (existing != null && existing.getAssets().isUpToDate()) {
                // Someone else already created the resources while we were
                // unlocked; go ahead and use theirs.
                r.getAssets().close();
                return existing;
            }

            // XXX need to remove entries when weak references go away
            mActiveResources.put(key, new WeakReference<Resources>(r));
            return r;
        }
    }

    /**
     * Creates a map between an activity & app's icon ids to its component info. This map
+14 −1
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ public final class ResourcesKey {
    final private boolean mIsThemeable;
    private final int mHash;
    private final IBinder mToken;
    private final ThemeConfig mThemeConfig;

    public final int mDisplayId;
    public final Configuration mOverrideConfiguration = new Configuration();
@@ -39,6 +40,7 @@ public final class ResourcesKey {
        mScale = scale;
        mIsThemeable = isThemeable;
        mToken = token;
        mThemeConfig = themeConfig;

        int hash = 17;
        hash = 31 * hash + (mResDir == null ? 0 : mResDir.hashCode());
@@ -92,7 +94,18 @@ public final class ResourcesKey {
        if (mScale != peer.mScale) {
            return false;
        }
        return mIsThemeable == peer.mIsThemeable;
        if (mIsThemeable != peer.mIsThemeable) {
            return false;
        }
        if (mThemeConfig != peer.mThemeConfig) {
            if (mThemeConfig == null || peer.mThemeConfig == null) {
                return false;
            }
            if (!mThemeConfig.equals(peer.mThemeConfig)) {
                return false;
            }
        }
        return true;
    }

    @Override