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

Commit 79ce19b8 authored by Santiago Etchebehere's avatar Santiago Etchebehere
Browse files

[ThemePicker 8/N] Load a default theme

Add a "Default" theme to the list of available themes in the picker.

Bug: 120559294

Change-Id: Ifb1fdbe9bb57969f3cbba6f47054a41f8197183f
parent 988f99d2
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -48,7 +48,9 @@
    <!-- Sample text used to show a preview of a selected font [CHAR LIMIT=3] -->
    <string name="theme_font_example">ABC</string>

    <!-- Name of the card that previews a theme's font [CHAR LIMIT=20] -->
    <!-- Name for the Android Theme that comes preset with the device [CHAR LIMIT=10]-->
    <string name="default_theme_title">Default</string>

    <string name="preview_name_font">Font</string>

    <!-- Title text for previewing a font [CHAR LIMIT=30] -->
+71 −2
Original line number Diff line number Diff line
@@ -44,6 +44,8 @@ public class DefaultThemeProvider implements ThemeBundleProvider {
    private static final String FONT_PREFIX = "theme_overlay_font_";
    private static final String COLOR_PREFIX = "theme_overlay_color_";

    private static final String DEFAULT_THEME_NAME= "default";

    private static final String ACCENT_COLOR_LIGHT_NAME = "accent_device_default_light";
    private static final String ACCENT_COLOR_DARK_NAME = "accent_device_default_dark";
    private static final String CONFIG_BODY_FONT_FAMILY = "config_bodyFontFamily";
@@ -61,6 +63,9 @@ public class DefaultThemeProvider implements ThemeBundleProvider {
    }

    private void init() {
        if (TextUtils.isEmpty(mStubPackageName)) {
            return;
        }
        mStubApkResources = null;
        try {
            PackageManager pm = mContext.getPackageManager();
@@ -83,7 +88,7 @@ public class DefaultThemeProvider implements ThemeBundleProvider {
    public void fetch(OptionsFetchedListener<ThemeBundle> callback, boolean reload) {
        if (mThemes == null || reload) {
            mThemes = new ArrayList<>();
            readThemesFromStub();
            loadAll();
        }

        if(callback != null) {
@@ -91,12 +96,18 @@ public class DefaultThemeProvider implements ThemeBundleProvider {
        }
    }

    private void readThemesFromStub() {
    private void loadAll() {
        addDefaultTheme();

        int themesListResId = mStubApkResources.getIdentifier(THEMES_ARRAY, "array",
                mStubPackageName);
        String[] themeNames = mStubApkResources.getStringArray(themesListResId);

        for (String themeName : themeNames) {
            // Default theme needs special treatment (see #addDefaultTheme())
            if (DEFAULT_THEME_NAME.equals(themeName)) {
                continue;
            }
            ThemeBundle.Builder builder = new Builder();
            try {
                builder.setTitle(mStubApkResources.getString(
@@ -133,6 +144,64 @@ public class DefaultThemeProvider implements ThemeBundleProvider {
        }
    }


    /**
     * Default theme requires different treatment: if there are overlay packages specified in the
     * stub apk, we'll use those, otherwise we'll get the System default values. But we cannot skip
     * the default theme.
     */
    private void addDefaultTheme() {
        ThemeBundle.Builder builder = new Builder();
        Resources system = Resources.getSystem();

        int titleId = mStubApkResources.getIdentifier(TITLE_PREFIX + DEFAULT_THEME_NAME,
                "string", mStubPackageName);
        if (titleId > 0) {
            builder.setTitle(mStubApkResources.getString(titleId));
        } else {
            builder.setTitle(mContext.getString(R.string.default_theme_title));
        }

        String colorOverlayPackage = getOverlayPackage(COLOR_PREFIX, DEFAULT_THEME_NAME);

        try {
            builder.setColorPackage(colorOverlayPackage)
                    .setColorAccentLight(loadColor(ACCENT_COLOR_LIGHT_NAME, colorOverlayPackage))
                    .setColorAccentDark(loadColor(ACCENT_COLOR_DARK_NAME, colorOverlayPackage));
        } catch (NameNotFoundException | NotFoundException e) {
            Log.i(TAG, "Didn't find color overlay for default theme, will use system default", e);
            int colorAccentLight = system.getColor(
                    system.getIdentifier(ACCENT_COLOR_LIGHT_NAME, "color", "android"), null);
            builder.setColorAccentLight(colorAccentLight);

            int colorAccentDark = system.getColor(
                    system.getIdentifier(ACCENT_COLOR_DARK_NAME, "color", "android"), null);
            builder.setColorAccentDark(colorAccentDark);
            builder.setColorPackage(null);
        }

        String fontOverlayPackage = getOverlayPackage(FONT_PREFIX, DEFAULT_THEME_NAME);

        try {
            builder.setFontOverlayPackage(fontOverlayPackage)
                    .setBodyFontFamily(loadTypeface(CONFIG_BODY_FONT_FAMILY,
                            fontOverlayPackage))
                    .setHeadlineFontFamily(loadTypeface(CONFIG_HEADLINE_FONT_FAMILY,
                            fontOverlayPackage));
        } catch (NameNotFoundException | NotFoundException e) {
            Log.i(TAG, "Didn't find font overlay for default theme, will use system default", e);
            String headlineFontFamily = system.getString(system.getIdentifier(
                    CONFIG_HEADLINE_FONT_FAMILY,"string", "android"));
            String bodyFontFamily = system.getString(system.getIdentifier(CONFIG_BODY_FONT_FAMILY,
                    "string", "android"));
            builder.setHeadlineFontFamily(Typeface.create(headlineFontFamily, Typeface.NORMAL))
                    .setBodyFontFamily(Typeface.create(bodyFontFamily, Typeface.NORMAL));
            builder.setFontOverlayPackage(null);
        }

        mThemes.add(builder.build());
    }

    private String getOverlayPackage(String prefix, String themeName) {
        int overlayPackageResId = mStubApkResources.getIdentifier(prefix + themeName,
                "string", mStubPackageName);
+2 −0
Original line number Diff line number Diff line
@@ -39,6 +39,8 @@ import java.util.List;
 */
public class ThemeBundle implements CustomizationOption {

    static final String DEFAULT_PACKAGE = "DEFAULT_PACKAGE";

    private final String mTitle;
    private final PreviewInfo mPreviewInfo;

+1 −0
Original line number Diff line number Diff line
@@ -102,6 +102,7 @@ public class ThemeFragment extends ToolbarFragment {
            if (mSelectedTheme == null) {
                mSelectedTheme = options.get(0);
            }
            mOptionsController.setSelectedOption(mSelectedTheme);
        });
        createAdapter();
    }