Support custom calendar icon

This commit is contained in:
Suphon Thanakornpakapong
2021-08-05 11:09:59 +07:00
parent 4d04065e8d
commit c2cedc7559
3 changed files with 85 additions and 35 deletions
@@ -19,6 +19,7 @@ import android.os.Build;
import android.text.TextUtils;
import androidx.annotation.ColorInt;
import androidx.core.content.res.ResourcesCompat;
import androidx.core.graphics.ColorUtils;
import androidx.palette.graphics.Palette;
@@ -46,6 +47,7 @@ public class IconPack {
private String packageName;
private Context mContext;
private Map<String, String> mIconPackResources = new HashMap<>();
private Map<String, String> mCalendarResources = new HashMap<>();
private List<String> mIconBackStrings;
private List<Drawable> mIconBackList;
private Drawable mIconUpon, mIconMask;
@@ -65,9 +67,10 @@ public class IconPack {
continue;
}
String name = parser.getName().toLowerCase();
if (name.equals("item")) {
boolean isCalendar = name.equals("calendar");
if (isCalendar || name.equals("item")) {
String component = parser.getAttributeValue(null, "component");
String drawable = parser.getAttributeValue(null, "drawable");
String drawable = parser.getAttributeValue(null, isCalendar ? "prefix" : "drawable");
// Validate component/drawable exist
if (TextUtils.isEmpty(component) || TextUtils.isEmpty(drawable)) {
@@ -83,7 +86,7 @@ public class IconPack {
// Sanitize stored value
component = component.substring(14, component.length() - 1);
Map<String, String> iconPackResources = mIconPackResources;
Map<String, String> iconPackResources = isCalendar ? mCalendarResources : mIconPackResources;
if (!component.contains("/")) {
// Package icon reference
iconPackResources.put(component, drawable);
@@ -133,6 +136,14 @@ public class IconPack {
setIcons(mIconPackResources, iconBackStrings);
}
public boolean isCalendar(String packageName) {
return mCalendarResources.containsKey(packageName);
}
public boolean isNormalIcon(String packageName) {
return mIconPackResources.containsKey(packageName);
}
public void setIcons(Map<String, String> iconPackResources, List<String> iconBackStrings) {
mIconPackResources = iconPackResources;
mIconBackStrings = iconBackStrings;
@@ -143,11 +154,11 @@ public class IconPack {
// must never happen cause itys checked already in the provider
return;
}
mIconMask = getDrawableForName(ICON_MASK_TAG);
mIconUpon = getDrawableForName(ICON_UPON_TAG);
mIconMask = getDrawableForName(ICON_MASK_TAG, 0);
mIconUpon = getDrawableForName(ICON_UPON_TAG, 0);
for (int i = 0; i < mIconBackStrings.size(); i++) {
String backIconString = mIconBackStrings.get(i);
Drawable backIcon = getDrawableWithName(backIconString);
Drawable backIcon = getDrawableWithName(backIconString, 0);
if (backIcon != null) {
mIconBackList.add(backIcon);
}
@@ -161,20 +172,29 @@ public class IconPack {
}
}
public Drawable getIcon(LauncherActivityInfo info, Drawable appIcon) {
return getIcon(info.getComponentName(), appIcon);
public Drawable getIcon(LauncherActivityInfo info, int iconDpi) {
return getIcon(info.getComponentName(), iconDpi);
}
public Drawable getIcon(ActivityInfo info, Drawable appIcon) {
return getIcon(new ComponentName(info.packageName, info.name), appIcon);
public Drawable getIcon(ActivityInfo info, int iconDpi) {
return getIcon(new ComponentName(info.packageName, info.name), iconDpi);
}
public Drawable getIcon(ComponentName name, Drawable appIcon) {
return getDrawable(name.flattenToString(), appIcon);
public Drawable getIcon(ComponentName name, int iconDpi) {
return getDrawable(name.flattenToString(), iconDpi);
}
public Drawable getIcon(String packageName, Drawable appIcon) {
return getDrawable(packageName, appIcon);
public Drawable getIcon(String packageName, int iconDpi) {
return getDrawable(packageName, iconDpi);
}
public Drawable getCalendarIcon(String packageName, int iconDpi, int day) {
String prefix = mCalendarResources.get(packageName);
if (prefix == null) {
return null;
}
String drawableName = prefix + (day + 1);
return getDrawableWithName(drawableName, iconDpi);
}
private static Bitmap pad(Bitmap src) {
@@ -212,12 +232,8 @@ public class IconPack {
return bitmap;
}
private Drawable getDrawable(String name, Drawable appIcon) {
Drawable d = getDrawableForName(name);
if (d == null && appIcon != null) {
d = appIcon;
}
return wrapAdaptiveIcon(d, mContext);
private Drawable getDrawable(String name, int iconDpi) {
return getDrawableForName(name, iconDpi);
}
public static Drawable wrapAdaptiveIcon(Drawable d, Context context) {
@@ -265,21 +281,21 @@ public class IconPack {
return resId;
}
private Drawable getDrawableForName(String name) {
private Drawable getDrawableForName(String name, int iconDpi) {
String item = mIconPackResources.get(name);
if (!TextUtils.isEmpty(item)) {
int id = getResourceIdForDrawable(item);
if (id != 0) {
return mLoadedIconPackResource.getDrawable(id);
return ResourcesCompat.getDrawableForDensity(mLoadedIconPackResource, id, iconDpi, null);
}
}
return null;
}
private Drawable getDrawableWithName(String name) {
int id = getResourceIdForDrawable(name);
private Drawable getDrawableWithName(String drawableName, int iconDpi) {
int id = getResourceIdForDrawable(drawableName);
if (id != 0) {
return mLoadedIconPackResource.getDrawable(id);
return ResourcesCompat.getDrawableForDensity(mLoadedIconPackResource, id, iconDpi, null);
}
return null;
}
+2 -2
View File
@@ -90,8 +90,8 @@
<!-- Default packages -->
<string name="wallpaper_picker_package" translatable="false"></string>
<string name="calendar_component_name" translatable="false"></string>
<string name="clock_component_name" translatable="false"></string>
<string name="calendar_component_name" translatable="false">com.google.android.calendar/com.android.calendar.AllInOneActivity</string>
<string name="clock_component_name" translatable="false">com.google.android.deskclock/com.android.deskclock.DeskClock</string>
<!-- Accessibility actions -->
<item type="id" name="action_remove" />
@@ -37,10 +37,13 @@ import android.os.UserHandle;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.Nullable;
import com.android.launcher3.R;
import com.android.launcher3.icons.BitmapInfo.Extender;
import app.lawnchair.iconpack.IconPack;
import app.lawnchair.iconpack.IconPackProvider;
import com.android.launcher3.pm.UserCache;
import com.android.launcher3.util.ComponentKey;
import com.android.launcher3.util.SafeCloseable;
@@ -64,6 +67,9 @@ public class IconProvider {
// Default value returned if there are problems getting resources.
private static final int NO_ID = 0;
private final BiFunction<LauncherActivityInfo, Integer, Drawable> LAI_IP_LOADER =
this::loadFromIconPack;
private static final BiFunction<LauncherActivityInfo, Integer, Drawable> LAI_LOADER =
LauncherActivityInfo::getIcon;
@@ -86,13 +92,26 @@ public class IconProvider {
* is used by caches to check for icon invalidation.
*/
public String getSystemStateForPackage(String systemState, String packageName) {
if (mCalendar != null && mCalendar.getPackageName().equals(packageName)) {
if (isCalendar(packageName)) {
return systemState + SYSTEM_STATE_SEPARATOR + getDay();
} else {
return systemState;
}
}
private boolean isCalendar(String packageName) {
IconPack iconPack = IconPackProvider.loadAndGetIconPack(mContext);
if (iconPack != null) {
if (iconPack.isCalendar(packageName)) {
return true;
}
if (iconPack.isNormalIcon(packageName)) {
return false;
}
}
return mCalendar != null && mCalendar.getPackageName().equals(packageName);
}
/**
* Loads the icon for the provided LauncherActivityInfo such that it can be drawn directly
* on the UI
@@ -109,13 +128,21 @@ public class IconProvider {
* Loads the icon for the provided LauncherActivityInfo
*/
public Drawable getIcon(LauncherActivityInfo info, int iconDpi) {
return getIcon(info.getApplicationInfo().packageName, info.getUser(),
info, iconDpi, LAI_IP_LOADER);
}
@Nullable
private Drawable loadFromIconPack(LauncherActivityInfo info, int iconDpi) {
Drawable icon = null;
IconPack iconPack = IconPackProvider.loadAndGetIconPack(mContext);
Drawable d = getIcon(info.getApplicationInfo().packageName, info.getUser(),
info, iconDpi, LAI_LOADER);
if (iconPack != null) {
d = iconPack.getIcon(info, d);
icon = iconPack.getIcon(info, iconDpi);
}
return d;
if (icon == null) {
icon = LAI_LOADER.apply(info, iconDpi);
}
return icon;
}
/**
@@ -129,8 +156,8 @@ public class IconProvider {
private <T, P> Drawable getIcon(String packageName, UserHandle user, T obj, P param,
BiFunction<T, P, Drawable> loader) {
Drawable icon = null;
if (mCalendar != null && mCalendar.getPackageName().equals(packageName)) {
icon = loadCalendarDrawable(0);
if (isCalendar(packageName)) {
icon = loadCalendarDrawable(packageName, 0);
} else if (mClock != null
&& mClock.getPackageName().equals(packageName)
&& Process.myUserHandle().equals(user)) {
@@ -143,8 +170,15 @@ public class IconProvider {
return ret;
}
private Drawable loadCalendarDrawable(int iconDpi) {
private Drawable loadCalendarDrawable(String packageName, int iconDpi) {
PackageManager pm = mContext.getPackageManager();
IconPack iconPack = IconPackProvider.loadAndGetIconPack(mContext);
if (iconPack != null) {
Drawable icon = iconPack.getCalendarIcon(packageName, iconDpi, getDay());
if (icon != null) {
return icon;
}
}
try {
final Bundle metadata = pm.getActivityInfo(
mCalendar,