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

Commit a015a67d authored by TheScarastic's avatar TheScarastic Committed by Mohammed Althaf T
Browse files

feat(icons): Add month icon support for calendar

parent 256571bd
Loading
Loading
Loading
Loading
+35 −1
Original line number Diff line number Diff line
@@ -66,6 +66,8 @@ public class IconProvider {
    public static final boolean ATLEAST_T = BuildCompat.isAtLeastT();

    private static final String ICON_METADATA_KEY_PREFIX = ".dynamic_icons";
    private static final String MONTH_BG_ICON_METADATA_KEY_PREFIX = ".month_dynamic_icons";
    private static final String DAY_FG_ICON_METADATA_KEY_PREFIX = ".day_dynamic_icons";

    private static final String SYSTEM_STATE_SEPARATOR = " ";

@@ -208,7 +210,9 @@ public class IconProvider {
                    .metaData;
            final Resources resources = pm.getResourcesForApplication(mCalendar.getPackageName());
            final int id = getDynamicIconId(metadata, resources);
            if (id != ID_NULL) {
            final int[] monthDayIds = getDynamicBackgroundIconId(metadata, resources);
            if (id != ID_NULL && (monthDayIds == null ||
                    monthDayIds[0] == ID_NULL || monthDayIds[1] == ID_NULL )) {
                if (DEBUG) Log.d(TAG, "Got icon #" + id);
                Drawable drawable = resources.getDrawableForDensity(id, iconDpi, null /* theme */);
                if (ATLEAST_T && drawable instanceof AdaptiveIconDrawable && td != null) {
@@ -226,6 +230,10 @@ public class IconProvider {
                    }
                }
                return drawable;
            } else if (monthDayIds != null) {
                Drawable background = resources.getDrawableForDensity(monthDayIds[0], iconDpi, null /* theme */);
                Drawable foreground = resources.getDrawableForDensity(monthDayIds[1], iconDpi, null /* theme */);
                return new AdaptiveIconDrawable(background, foreground);
            }
        } catch (PackageManager.NameNotFoundException e) {
            if (DEBUG) {
@@ -269,6 +277,28 @@ public class IconProvider {
        }
    }

    private int[] getDynamicBackgroundIconId(Bundle metadata, Resources resources) {
        if (metadata == null) {
            return null;
        }
        String bgKey = mCalendar.getPackageName() + MONTH_BG_ICON_METADATA_KEY_PREFIX;
        String fgKey = mCalendar.getPackageName() + DAY_FG_ICON_METADATA_KEY_PREFIX;
        final int bgArrayId = metadata.getInt(bgKey, ID_NULL);
        final int fgArrayId = metadata.getInt(fgKey, ID_NULL);
        if (bgArrayId == ID_NULL || fgArrayId == ID_NULL) {
            return null;
        }
        try {
            return new int[] {resources.obtainTypedArray(bgArrayId).getResourceId(getMonth(), ID_NULL),
                    resources.obtainTypedArray(fgArrayId).getResourceId(getDay(), ID_NULL)};
        } catch (Resources.NotFoundException e) {
            if (DEBUG) {
                Log.d(TAG, "package defines '" + bgKey + " and " + fgKey + "' but corresponding array not found");
            }
            return null;
        }
    }

    /**
     * Refreshes the system state definition used to check the validity of an app icon. It
     * incorporates all the properties that can affect the app icon like the list of enabled locale
@@ -286,6 +316,10 @@ public class IconProvider {
        return Calendar.getInstance().get(Calendar.DAY_OF_MONTH) - 1;
    }

    private static int getMonth() {
        return Calendar.getInstance().get(Calendar.MONTH);
    }

    private static ComponentName parseComponentOrNull(Context context, int resId) {
        String cn = context.getString(resId);
        return TextUtils.isEmpty(cn) ? null : ComponentName.unflattenFromString(cn);