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

Commit 8a07476d authored by Andy Mast's avatar Andy Mast
Browse files

Modify config to support app specific themes [1/4]

Summary of Changes
1. The configuration now supports each app to have their own theme.
Each theme can include icons, styles (ie overlays) and font.

2. The theme config is serialized into a json string and stored
in secure settings.  This entry consolidates what was previously 3 separate
keys (style, icon, font) into a single field.

3. 'CustomTheme' was renamed to 'ThemeConfiguration'.

Note: This commit is for the configuration. Additional work needs to be done to
the framework and UI to support app specific themes.

Change-Id: I6f72f542829578201c57c54786456486f6a6e2e3
parent d852faf4
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -39,7 +39,6 @@ import android.content.pm.ServiceInfo;
import android.content.res.AssetManager;
import android.content.res.CompatibilityInfo;
import android.content.res.Configuration;
import android.content.res.CustomTheme;
import android.content.res.Resources;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDebug;
+0 −1
Original line number Diff line number Diff line
@@ -48,7 +48,6 @@ import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.AssetManager;
import android.content.res.CompatibilityInfo;
import android.content.res.Configuration;
import android.content.res.CustomTheme;
import android.content.res.Resources;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
+30 −26
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ import android.content.pm.ThemeUtils;
import android.content.res.AssetManager;
import android.content.res.CompatibilityInfo;
import android.content.res.Configuration;
import android.content.res.CustomTheme;
import android.content.res.ThemeConfig;
import android.content.res.Resources;
import android.content.res.ResourcesKey;
import android.hardware.display.DisplayManagerGlobal;
@@ -219,19 +219,19 @@ public class ResourcesManager {
        boolean iconsAttached = false;
        /* Attach theme information to the resulting AssetManager when appropriate. */
        if (compatInfo.isThemeable && config != null && !context.getPackageManager().isSafeMode()) {
            if (config.customTheme == null) {
            if (config.themeConfig == null) {
                try {
                    config.customTheme = CustomTheme.getBootTheme(context.getContentResolver());
                    config.themeConfig = ThemeConfig.getBootTheme(context.getContentResolver());
                } catch (Exception e) {
                    Slog.d(TAG, "CustomTheme.getBootTheme failed, falling back to system theme", e);
                    config.customTheme = CustomTheme.getSystemTheme();
                    Slog.d(TAG, "ThemeConfig.getBootTheme failed, falling back to system theme", e);
                    config.themeConfig = ThemeConfig.getSystemTheme();
                }
            }

            if (config.customTheme != null) {
                attachThemeAssets(assets, config.customTheme);
                attachCommonAssets(assets, config.customTheme);
                iconsAttached = attachIconAssets(assets, config.customTheme);
            if (config.themeConfig != null) {
                attachThemeAssets(assets, config.themeConfig);
                attachCommonAssets(assets, config.themeConfig);
                iconsAttached = attachIconAssets(assets, config.themeConfig);
            }
        }

@@ -294,11 +294,15 @@ public class ResourcesManager {
        }

        /* Attach theme information to the resulting AssetManager when appropriate. */
        CustomTheme customTheme =
                new CustomTheme(themePackageName, themePackageName, themePackageName);
        attachThemeAssets(assets, customTheme);
        attachCommonAssets(assets, customTheme);
        attachIconAssets(assets, customTheme);
        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);
        attachIconAssets(assets, themeConfig);

        r = new Resources(assets, dm, config, compatInfo, token);
        setActivityIcons(r);
@@ -329,9 +333,9 @@ public class ResourcesManager {
            return;
        }

        final CustomTheme customTheme = r.getConfiguration().customTheme;
        if (pkgName != null && customTheme != null &&
                pkgName.equals(customTheme.getIconPackPkgName())) {
        final ThemeConfig themeConfig = r.getConfiguration().themeConfig;
        if (pkgName != null && themeConfig != null &&
                pkgName.equals(themeConfig.getIconPackPkgName())) {
            return;
        }

@@ -416,10 +420,10 @@ public class ResourcesManager {
                        r.setIconResources(null);
                        r.setComposedIconInfo(null);
                        detachThemeAssets(am);
                        if (config.customTheme != null) {
                            attachThemeAssets(am, config.customTheme);
                            attachCommonAssets(am, config.customTheme);
                            if (attachIconAssets(am, config.customTheme)) {
                        if (config.themeConfig != null) {
                            attachThemeAssets(am, config.themeConfig);
                            attachCommonAssets(am, config.themeConfig);
                            if (attachIconAssets(am, config.themeConfig)) {
                                setActivityIcons(r);
                            }
                        }
@@ -476,7 +480,7 @@ public class ResourcesManager {
     *         removed and the theme manager has yet to revert formally back to
     *         the framework default.
     */
    private boolean attachThemeAssets(AssetManager assets, CustomTheme theme) {
    private boolean attachThemeAssets(AssetManager assets, ThemeConfig theme) {
        PackageInfo piTheme = null;
        PackageInfo piTarget = null;
        PackageInfo piAndroid = null;
@@ -499,7 +503,7 @@ public class ResourcesManager {

        try {
            piTheme = getPackageManager().getPackageInfo(
                    theme.getThemePackageNameForApp(basePackageName), 0,
                    theme.getOverlayPkgNameForApp(basePackageName), 0,
                    UserHandle.getCallingUserId());
            piTarget = getPackageManager().getPackageInfo(
                    basePackageName, 0, UserHandle.getCallingUserId());
@@ -568,7 +572,7 @@ public class ResourcesManager {
     * @param theme
     * @return true if succes, false otherwise
     */
    private boolean attachIconAssets(AssetManager assets, CustomTheme theme) {
    private boolean attachIconAssets(AssetManager assets, ThemeConfig theme) {
        PackageInfo piIcon = null;
        try {
            piIcon = getPackageManager().getPackageInfo(theme.getIconPackPkgName(), 0,
@@ -614,10 +618,10 @@ public class ResourcesManager {
     * @param theme
     * @return true if succes, false otherwise
     */
    private boolean attachCommonAssets(AssetManager assets, CustomTheme theme) {
    private boolean attachCommonAssets(AssetManager assets, ThemeConfig theme) {
        PackageInfo piTheme = null;
        try {
            piTheme = getPackageManager().getPackageInfo(theme.getThemePackageName(), 0,
            piTheme = getPackageManager().getPackageInfo(theme.getOverlayPkgName(), 0,
                    UserHandle.getCallingUserId());
        } catch (RemoteException e) {
        }
+1 −1
Original line number Diff line number Diff line
@@ -49,7 +49,7 @@ import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import static android.content.res.CustomTheme.HOLO_DEFAULT;
import static android.content.res.ThemeConfig.HOLO_DEFAULT;

/**
 * @hide
+37 −28
Original line number Diff line number Diff line
@@ -22,9 +22,6 @@ import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.View;
import android.util.Log;
import android.os.SystemProperties;
import android.text.TextUtils;

import java.util.Locale;

@@ -79,7 +76,7 @@ public final class Configuration implements Parcelable, Comparable<Configuration
    /**
     * @hide
     */
    public CustomTheme customTheme;
    public ThemeConfig themeConfig;

    /**
     * Locale should persist on setting.  This is hidden because it is really
@@ -416,28 +413,40 @@ public final class Configuration implements Parcelable, Comparable<Configuration

    /**
     * @hide
     */
    public static final int THEME_UNDEFINED = 0;

    /**
     * @hide
     * @deprecated
     */
    public static final String THEME_PACKAGE_NAME_PERSISTENCE_PROPERTY = "persist.sys.themePackageName";

    /**
     * @hide
     * @deprecated
     */
    public static final String THEME_SYSTEMUI_PACKAGE_NAME_PERSISTENCE_PROPERTY = "persist.sys.themeSysUiPkgName";
    public static final String THEME_ICONPACK_PACKAGE_NAME_PERSISTENCE_PROPERTY = "themeIconPackPkgName";

    /**
     * @hide
     * @deprecated
     */
    public static final String THEME_ICONPACK_PACKAGE_NAME_PERSISTENCE_PROPERTY = "themeIconPackPkgName";
    public static final String THEME_FONT_PACKAGE_NAME_PERSISTENCE_PROPERTY = "themeFontPackPkgName";

    /**
     * @hide
     * Serialized json structure mapping app pkgnames to their set theme.
     *
     * {
     *  "default":{
     *"     stylePkgName":"com.jasonevil.theme.miuiv5dark",
     *      "iconPkgName":"com.cyngn.hexo",
     *      "fontPkgName":"com.cyngn.hexo"
     *   }
     * }

     * If an app does not have a specific theme set then it will use the 'default' theme+
     * example: 'default' -> overlayPkgName: 'org.blue.theme'
     *          'com.android.phone' -> 'com.red.theme'
     *          'com.google.vending' -> 'com.white.theme'
     */
    public static final String THEME_FONT_PACKAGE_NAME_PERSISTENCE_PROPERTY = "themeFontPackPkgName";
    public static final String THEME_PKG_CONFIGURATION_PERSISTENCE_PROPERTY = "themeConfig";

    /**
     * Overall orientation of the screen.  May be one of
@@ -653,8 +662,8 @@ public final class Configuration implements Parcelable, Comparable<Configuration
        compatScreenHeightDp = o.compatScreenHeightDp;
        compatSmallestScreenWidthDp = o.compatSmallestScreenWidthDp;
        seq = o.seq;
        if (o.customTheme != null) {
            customTheme = (CustomTheme) o.customTheme.clone();
        if (o.themeConfig != null) {
            themeConfig = (ThemeConfig) o.themeConfig.clone();
        }
    }

@@ -792,7 +801,7 @@ public final class Configuration implements Parcelable, Comparable<Configuration
            sb.append(seq);
        }
        sb.append(" themeResource=");
        sb.append(customTheme);
        sb.append(themeConfig);
        sb.append('}');
        return sb.toString();
    }
@@ -819,7 +828,7 @@ public final class Configuration implements Parcelable, Comparable<Configuration
        smallestScreenWidthDp = compatSmallestScreenWidthDp = SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
        densityDpi = DENSITY_DPI_UNDEFINED;
        seq = 0;
        customTheme = null;
        themeConfig = null;
    }

    /** {@hide} */
@@ -963,10 +972,10 @@ public final class Configuration implements Parcelable, Comparable<Configuration
            seq = delta.seq;
        }

        if (delta.customTheme != null
                && (customTheme == null || !customTheme.equals(delta.customTheme))) {
        if (delta.themeConfig != null
                && (themeConfig == null || !themeConfig.equals(delta.themeConfig))) {
            changed |= ActivityInfo.CONFIG_THEME_RESOURCE;
            customTheme = (CustomTheme)delta.customTheme.clone();
            themeConfig = (ThemeConfig)delta.themeConfig.clone();
        }

        return changed;
@@ -1078,8 +1087,8 @@ public final class Configuration implements Parcelable, Comparable<Configuration
                && densityDpi != delta.densityDpi) {
            changed |= ActivityInfo.CONFIG_DENSITY;
        }
        if (delta.customTheme != null &&
                (customTheme == null || !customTheme.equals(delta.customTheme))) {
        if (delta.themeConfig != null &&
                (themeConfig == null || !themeConfig.equals(delta.themeConfig))) {
            changed |= ActivityInfo.CONFIG_THEME_RESOURCE;
        }
        return changed;
@@ -1172,7 +1181,7 @@ public final class Configuration implements Parcelable, Comparable<Configuration
        dest.writeInt(compatScreenHeightDp);
        dest.writeInt(compatSmallestScreenWidthDp);
        dest.writeInt(seq);
        dest.writeParcelable(customTheme, flags);
        dest.writeParcelable(themeConfig, flags);
    }

    public void readFromParcel(Parcel source) {
@@ -1201,7 +1210,7 @@ public final class Configuration implements Parcelable, Comparable<Configuration
        compatScreenHeightDp = source.readInt();
        compatSmallestScreenWidthDp = source.readInt();
        seq = source.readInt();
        customTheme = source.readParcelable(CustomTheme.class.getClassLoader());
        themeConfig = source.readParcelable(ThemeConfig.class.getClassLoader());
    }
    
    public static final Parcelable.Creator<Configuration> CREATOR
@@ -1270,10 +1279,10 @@ public final class Configuration implements Parcelable, Comparable<Configuration
        if (n != 0) return n;
        n = this.densityDpi - that.densityDpi;
        if (n != 0) return n;
        if (this.customTheme == null) {
            if (that.customTheme != null) return 1;
        if (this.themeConfig == null) {
            if (that.themeConfig != null) return 1;
        } else {
            n = this.customTheme.compareTo(that.customTheme);
            n = this.themeConfig.compareTo(that.themeConfig);
        }
        return n;
    }
@@ -1311,8 +1320,8 @@ public final class Configuration implements Parcelable, Comparable<Configuration
        result = 31 * result + screenHeightDp;
        result = 31 * result + smallestScreenWidthDp;
        result = 31 * result + densityDpi;
        result = 31 * result + (this.customTheme != null ?
                                  this.customTheme.hashCode() : 0);
        result = 31 * result + (this.themeConfig != null ?
                                  this.themeConfig.hashCode() : 0);
        return result;
    }

Loading