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

Commit 627b296c authored by d34d's avatar d34d Committed by Clark Scheff
Browse files

Themes: Add helper methods for creating complete component maps

These methods can be used to apply a theme while replacing any
components not found in the theme with the defaults.

Default components come from the default theme for the system
with any missing components in that default theme coming from HOLO.

Change-Id: Idea55cc684ccc8304b01a991c36b6993f55b8880
parent c6c260e1
Loading
Loading
Loading
Loading
+50 −0
Original line number Diff line number Diff line
@@ -47,7 +47,9 @@ import java.io.OutputStream;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
@@ -618,4 +620,52 @@ public class ThemeUtils {
        }
        return supportedComponents;
    }

    /**
     * Get the components from the default theme.  If the default theme is not HOLO then any
     * components that are not in the default theme will come from HOLO to create a complete
     * component map.
     * @param context
     * @return
     */
    public static Map<String, String> getDefaultComponents(Context context) {
        String defaultThemePkg = getDefaultThemePackageName(context);
        List<String> defaultComponents = null;
        List<String> holoComponents = getSupportedComponents(context, HOLO_DEFAULT);
        if (!HOLO_DEFAULT.equals(defaultThemePkg)) {
            defaultComponents = getSupportedComponents(context, defaultThemePkg);
        }

        Map<String, String> componentMap = new HashMap<String, String>(holoComponents.size());
        if (defaultComponents != null) {
            for (String component : defaultComponents) {
                componentMap.put(component, defaultThemePkg);
            }
        }
        for (String component : holoComponents) {
            if (!componentMap.containsKey(component)) {
                componentMap.put(component, HOLO_DEFAULT);
            }
        }

        return componentMap;
    }

    /**
     * Takes an existing component map and adds any missing components from the default
     * map of components.
     * @param context
     * @param componentMap An existing component map
     */
    public static void completeComponentMap(Context context,
            Map<String, String> componentMap) {
        if (componentMap == null) return;

        Map<String, String> defaultComponents = getDefaultComponents(context);
        for (String component : defaultComponents.keySet()) {
            if (!componentMap.containsKey(component)) {
                componentMap.put(component, defaultComponents.get(component));
            }
        }
    }
}