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

Commit 25d58dfe authored by d34d's avatar d34d
Browse files

Themes: Allow packages to be set as non-themeable

This patch allows for specific packages to be declared as non
themeable.  Packages that are non themeable will not have theme
resources attached to their assets when their Resources object
is created.

This patch also removes isThemeable from CompatibilityInfo as this
object did not correctly reflect that the package is not themeable.

Change-Id: Id34c63ec0c3ef7c69df083da63559e0720ce0018
parent e05ffea4
Loading
Loading
Loading
Loading
+4 −2
Original line number Original line Diff line number Diff line
@@ -1702,7 +1702,8 @@ public final class ActivityThread {
            String[] libDirs, int displayId, Configuration overrideConfiguration,
            String[] libDirs, int displayId, Configuration overrideConfiguration,
            LoadedApk pkgInfo, Context context, String pkgName) {
            LoadedApk pkgInfo, Context context, String pkgName) {
        return mResourcesManager.getTopLevelResources(resDir, splitResDirs, overlayDirs, libDirs,
        return mResourcesManager.getTopLevelResources(resDir, splitResDirs, overlayDirs, libDirs,
                displayId, pkgName, overrideConfiguration, pkgInfo.getCompatibilityInfo(), context);
                displayId, pkgName, overrideConfiguration, pkgInfo.getCompatibilityInfo(), context,
                pkgInfo.getApplicationInfo().isThemeable);
    }
    }


    /**
    /**
@@ -1711,7 +1712,8 @@ public final class ActivityThread {
    Resources getTopLevelThemedResources(String resDir, int displayId, LoadedApk pkgInfo,
    Resources getTopLevelThemedResources(String resDir, int displayId, LoadedApk pkgInfo,
                                         String pkgName, String themePkgName) {
                                         String pkgName, String themePkgName) {
        return mResourcesManager.getTopLevelThemedResources(resDir, displayId, pkgName,
        return mResourcesManager.getTopLevelThemedResources(resDir, displayId, pkgName,
                themePkgName, pkgInfo.getCompatibilityInfo());
                themePkgName, pkgInfo.getCompatibilityInfo(),
                pkgInfo.getApplicationInfo().isThemeable);
    }
    }


    final Handler getHandler() {
    final Handler getHandler() {
+4 −2
Original line number Original line Diff line number Diff line
@@ -1870,9 +1870,11 @@ class ContextImpl extends Context {
                        packageInfo.getResDir(), packageInfo.getSplitResDirs(),
                        packageInfo.getResDir(), packageInfo.getSplitResDirs(),
                        packageInfo.getOverlayDirs(),
                        packageInfo.getOverlayDirs(),
                        packageInfo.getApplicationInfo().sharedLibraryFiles, displayId,
                        packageInfo.getApplicationInfo().sharedLibraryFiles, displayId,
                        packageInfo.getAppDir(), overrideConfiguration, compatInfo, mOuterContext) :
                        packageInfo.getAppDir(), overrideConfiguration, compatInfo, mOuterContext,
                        packageInfo.getApplicationInfo().isThemeable) :
                mResourcesManager.getTopLevelThemedResources(packageInfo.getResDir(), displayId,
                mResourcesManager.getTopLevelThemedResources(packageInfo.getResDir(), displayId,
                        packageInfo.getPackageName(), themePackageName, compatInfo);
                        packageInfo.getPackageName(), themePackageName, compatInfo,
                        packageInfo.getApplicationInfo().isThemeable);
            }
            }
        }
        }
        mResources = resources;
        mResources = resources;
+22 −25
Original line number Original line Diff line number Diff line
@@ -179,12 +179,12 @@ public class ResourcesManager {
     */
     */
    Resources getTopLevelResources(String resDir, String[] splitResDirs,
    Resources getTopLevelResources(String resDir, String[] splitResDirs,
            String[] overlayDirs, String[] libDirs, int displayId, String packageName,
            String[] overlayDirs, String[] libDirs, int displayId, String packageName,
            Configuration overrideConfiguration, CompatibilityInfo compatInfo, Context context) {
            Configuration overrideConfiguration, CompatibilityInfo compatInfo, Context context,
            boolean isThemeable) {
        final float scale = compatInfo.applicationScale;
        final float scale = compatInfo.applicationScale;
        final boolean isThemeable = compatInfo.isThemeable;
        final ThemeConfig themeConfig = getThemeConfig();
        Configuration overrideConfigCopy = (overrideConfiguration != null)
        Configuration overrideConfigCopy = (overrideConfiguration != null)
                ? new Configuration(overrideConfiguration) : null;
                ? new Configuration(overrideConfiguration) : null;
        final ThemeConfig themeConfig = getThemeConfig();
        ResourcesKey key = new ResourcesKey(resDir, displayId, overrideConfiguration, scale,
        ResourcesKey key = new ResourcesKey(resDir, displayId, overrideConfiguration, scale,
                isThemeable, themeConfig);
                isThemeable, themeConfig);
        Resources r;
        Resources r;
@@ -210,7 +210,7 @@ public class ResourcesManager {


        AssetManager assets = new AssetManager();
        AssetManager assets = new AssetManager();
        assets.setAppName(packageName);
        assets.setAppName(packageName);
        assets.setThemeSupport(compatInfo.isThemeable);
        assets.setThemeSupport(isThemeable);
        // resDir can be null if the 'android' package is creating a new Resources object.
        // resDir can be null if the 'android' package is creating a new Resources object.
        // This is fine, since each AssetManager automatically loads the 'android' package
        // This is fine, since each AssetManager automatically loads the 'android' package
        // already.
        // already.
@@ -267,7 +267,7 @@ public class ResourcesManager {


        boolean iconsAttached = false;
        boolean iconsAttached = false;
        /* Attach theme information to the resulting AssetManager when appropriate. */
        /* Attach theme information to the resulting AssetManager when appropriate. */
        if (compatInfo.isThemeable && config != null && !context.getPackageManager().isSafeMode()) {
        if (isThemeable && config != null && !context.getPackageManager().isSafeMode()) {
            if (config.themeConfig == null) {
            if (config.themeConfig == null) {
                try {
                try {
                    config.themeConfig = ThemeConfig.getBootTheme(context.getContentResolver());
                    config.themeConfig = ThemeConfig.getBootTheme(context.getContentResolver());
@@ -312,19 +312,16 @@ public class ResourcesManager {
     *
     *
     * @param resDir the resource directory.
     * @param resDir the resource directory.
     * @param compatInfo the compability info. Must not be null.
     * @param compatInfo the compability info. Must not be null.
     * @param token the application token for determining stack bounds.
     *
     *
     * @hide
     * @hide
     */
     */
    public Resources getTopLevelThemedResources(String resDir, int displayId,
    public Resources getTopLevelThemedResources(String resDir, int displayId, String packageName,
                                                String packageName,
            String themePackageName, CompatibilityInfo compatInfo, boolean isThemeable) {
                                                String themePackageName,
                                                CompatibilityInfo compatInfo) {
        Resources r;
        Resources r;


        AssetManager assets = new AssetManager();
        AssetManager assets = new AssetManager();
        assets.setAppName(packageName);
        assets.setAppName(packageName);
        assets.setThemeSupport(true);
        assets.setThemeSupport(isThemeable);
        if (assets.addAssetPath(resDir) == 0) {
        if (assets.addAssetPath(resDir) == 0) {
            return null;
            return null;
        }
        }
@@ -340,6 +337,8 @@ public class ResourcesManager {
            config = getConfiguration();
            config = getConfiguration();
        }
        }


        boolean iconsAttached = false;
        if (isThemeable) {
            /* Attach theme information to the resulting AssetManager when appropriate. */
            /* Attach theme information to the resulting AssetManager when appropriate. */
            ThemeConfig.Builder builder = new ThemeConfig.Builder();
            ThemeConfig.Builder builder = new ThemeConfig.Builder();
            builder.defaultOverlay(themePackageName);
            builder.defaultOverlay(themePackageName);
@@ -349,10 +348,10 @@ public class ResourcesManager {
            ThemeConfig themeConfig = builder.build();
            ThemeConfig themeConfig = builder.build();
            attachThemeAssets(assets, themeConfig);
            attachThemeAssets(assets, themeConfig);
            attachCommonAssets(assets, themeConfig);
            attachCommonAssets(assets, themeConfig);
        attachIconAssets(assets, themeConfig);
            iconsAttached = attachIconAssets(assets, themeConfig);

        }
        r = new Resources(assets, dm, config, compatInfo);
        r = new Resources(assets, dm, config, compatInfo);
        setActivityIcons(r);
        if (iconsAttached) setActivityIcons(r);


        return r;
        return r;
    }
    }
@@ -362,8 +361,6 @@ public class ResourcesManager {
     * is then stored in the resource object.
     * is then stored in the resource object.
     * When resource.getDrawable(id) is called it will check this mapping and replace
     * When resource.getDrawable(id) is called it will check this mapping and replace
     * the id with the themed resource id if one is available
     * the id with the themed resource id if one is available
     * @param context
     * @param pkgName
     * @param r
     * @param r
     */
     */
    private void setActivityIcons(Resources r) {
    private void setActivityIcons(Resources r) {
+19 −2
Original line number Original line Diff line number Diff line
@@ -2582,8 +2582,9 @@ public class PackageParser {
        final ApplicationInfo ai = owner.applicationInfo;
        final ApplicationInfo ai = owner.applicationInfo;
        final String pkgName = owner.applicationInfo.packageName;
        final String pkgName = owner.applicationInfo.packageName;


        // assume that this package is themeable unless explicitly set to false.
        String[] nonThemeablePackages =
        ai.isThemeable = true;
                res.getStringArray(com.android.internal.R.array.non_themeable_packages);
        ai.isThemeable = isPackageThemeable(pkgName, nonThemeablePackages);


        TypedArray sa = res.obtainAttributes(attrs,
        TypedArray sa = res.obtainAttributes(attrs,
                com.android.internal.R.styleable.AndroidManifestApplication);
                com.android.internal.R.styleable.AndroidManifestApplication);
@@ -4443,6 +4444,22 @@ public class PackageParser {
        return true;
        return true;
    }
    }


    /**1
     * Returns whether the specified package is themeable
     * @param packageName Name of package to check
     * @param nonThemeablePackages Array of packages that are declared as non-themeable
     * @return True if the package is themeable, false otherwise
     */
    private static boolean isPackageThemeable(String packageName, String[] nonThemeablePackages) {
        for (String pkg : nonThemeablePackages) {
            if (packageName.startsWith(pkg)) {
                return false;
            }
        }

        return true;
    }

    /**
    /**
     * Representation of a full package parsed from APK files on disk. A package
     * Representation of a full package parsed from APK files on disk. A package
     * consists of a single base APK, and zero or more split APKs.
     * consists of a single base APK, and zero or more split APKs.
+2 −15
Original line number Original line Diff line number Diff line
@@ -92,15 +92,9 @@ public class CompatibilityInfo implements Parcelable {
     */
     */
    public final float applicationInvertedScale;
    public final float applicationInvertedScale;


    /**
     * Whether the application supports third-party theming.
     */
    public final boolean isThemeable;

    public CompatibilityInfo(ApplicationInfo appInfo, int screenLayout, int sw,
    public CompatibilityInfo(ApplicationInfo appInfo, int screenLayout, int sw,
            boolean forceCompat) {
            boolean forceCompat) {
        int compatFlags = 0;
        int compatFlags = 0;
        isThemeable = appInfo.isThemeable;


        if (appInfo.requiresSmallestWidthDp != 0 || appInfo.compatibleWidthLimitDp != 0
        if (appInfo.requiresSmallestWidthDp != 0 || appInfo.compatibleWidthLimitDp != 0
                || appInfo.largestWidthLimitDp != 0) {
                || appInfo.largestWidthLimitDp != 0) {
@@ -247,20 +241,17 @@ public class CompatibilityInfo implements Parcelable {
        mCompatibilityFlags = compatFlags;
        mCompatibilityFlags = compatFlags;
    }
    }


    private CompatibilityInfo(int compFlags,
    private CompatibilityInfo(int compFlags, int dens, float scale, float invertedScale) {
            int dens, float scale, float invertedScale, boolean isThemeable) {
        mCompatibilityFlags = compFlags;
        mCompatibilityFlags = compFlags;
        applicationDensity = dens;
        applicationDensity = dens;
        applicationScale = scale;
        applicationScale = scale;
        applicationInvertedScale = invertedScale;
        applicationInvertedScale = invertedScale;
        this.isThemeable = isThemeable;
    }
    }


    private CompatibilityInfo() {
    private CompatibilityInfo() {
        this(NEVER_NEEDS_COMPAT, DisplayMetrics.DENSITY_DEVICE,
        this(NEVER_NEEDS_COMPAT, DisplayMetrics.DENSITY_DEVICE,
                1.0f,
                1.0f,
                1.0f,
                1.0f);
                true);
    }
    }


    /**
    /**
@@ -534,7 +525,6 @@ public class CompatibilityInfo implements Parcelable {
            if (applicationDensity != oc.applicationDensity) return false;
            if (applicationDensity != oc.applicationDensity) return false;
            if (applicationScale != oc.applicationScale) return false;
            if (applicationScale != oc.applicationScale) return false;
            if (applicationInvertedScale != oc.applicationInvertedScale) return false;
            if (applicationInvertedScale != oc.applicationInvertedScale) return false;
            if (isThemeable != oc.isThemeable) return false;
            return true;
            return true;
        } catch (ClassCastException e) {
        } catch (ClassCastException e) {
            return false;
            return false;
@@ -572,7 +562,6 @@ public class CompatibilityInfo implements Parcelable {
        result = 31 * result + applicationDensity;
        result = 31 * result + applicationDensity;
        result = 31 * result + Float.floatToIntBits(applicationScale);
        result = 31 * result + Float.floatToIntBits(applicationScale);
        result = 31 * result + Float.floatToIntBits(applicationInvertedScale);
        result = 31 * result + Float.floatToIntBits(applicationInvertedScale);
        result = 31 * result + (isThemeable ? 1 : 0);
        return result;
        return result;
    }
    }


@@ -587,7 +576,6 @@ public class CompatibilityInfo implements Parcelable {
        dest.writeInt(applicationDensity);
        dest.writeInt(applicationDensity);
        dest.writeFloat(applicationScale);
        dest.writeFloat(applicationScale);
        dest.writeFloat(applicationInvertedScale);
        dest.writeFloat(applicationInvertedScale);
        dest.writeInt(isThemeable ? 1 : 0);
    }
    }


    public static final Parcelable.Creator<CompatibilityInfo> CREATOR
    public static final Parcelable.Creator<CompatibilityInfo> CREATOR
@@ -608,6 +596,5 @@ public class CompatibilityInfo implements Parcelable {
        applicationDensity = source.readInt();
        applicationDensity = source.readInt();
        applicationScale = source.readFloat();
        applicationScale = source.readFloat();
        applicationInvertedScale = source.readFloat();
        applicationInvertedScale = source.readFloat();
        isThemeable = source.readInt() == 1 ? true : false;
    }
    }
}
}
Loading