diff --git a/iconloaderlib/Android.bp b/iconloaderlib/Android.bp index 6867e6b55c5855e06295b594ca6dc2ce6444fcd1..fa3a34fec8f9e04def3c4e896e682082168bd7a3 100644 --- a/iconloaderlib/Android.bp +++ b/iconloaderlib/Android.bp @@ -22,12 +22,14 @@ android_library { min_sdk_version: "26", static_libs: [ "androidx.core_core", + "androidx.core_core-ktx", ], resource_dirs: [ "res", ], srcs: [ "src/**/*.java", + "src/**/*.kt", ], } @@ -37,6 +39,7 @@ android_library { min_sdk_version: "26", static_libs: [ "androidx.core_core", + "androidx.core_core-ktx", ], resource_dirs: [ "res", @@ -44,5 +47,6 @@ android_library { srcs: [ "src/**/*.java", "src_full_lib/**/*.java", + "src/**/*.kt", ], } diff --git a/iconloaderlib/build.gradle b/iconloaderlib/build.gradle index 23c7cb045f9b51c0f4871e44101a96785126742b..7b92aac664ed574b7233c50e4c6320bdf3b43935 100644 --- a/iconloaderlib/build.gradle +++ b/iconloaderlib/build.gradle @@ -1,25 +1,15 @@ plugins { - id 'sysuigradleproject.android-library-conventions' + id 'com.android.library' + id 'org.jetbrains.kotlin.android' } android { + namespace "com.android.launcher3.icons" sourceSets { main { - java.srcDirs = ['src', 'src_full_lib'] + java.srcDirs = ['src'] manifest.srcFile 'AndroidManifest.xml' res.srcDirs = ['res'] } } - - lintOptions { - abortOnError false - } - - tasks.withType(JavaCompile) { - options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation" - } -} - -dependencies { - implementation "androidx.core:core" } diff --git a/iconloaderlib/src/com/android/launcher3/icons/AdaptiveIconGenerator.kt b/iconloaderlib/src/com/android/launcher3/icons/AdaptiveIconGenerator.kt new file mode 100644 index 0000000000000000000000000000000000000000..4a38395b288da4b7a2b842eab08b26a1336df592 --- /dev/null +++ b/iconloaderlib/src/com/android/launcher3/icons/AdaptiveIconGenerator.kt @@ -0,0 +1,26 @@ +package com.android.launcher3.icons + +import android.graphics.Bitmap +import android.graphics.drawable.Drawable +import androidx.core.graphics.drawable.toBitmap + +object AdaptiveIconGenerator { + private const val ICON_SCALE = 1.46f + + @JvmStatic + fun toBitmap(drawable: Drawable) = drawable.toBitmap() + + @JvmStatic + fun getScale(bitmap: Bitmap, scale: Float): Float { + val topRightPx = bitmap.getPixel(0, 0) + val topLeftPx = bitmap.getPixel(0, bitmap.height - 1) + val bottomRightPx = bitmap.getPixel(bitmap.width - 1, 0) + val bottomLeftPx = bitmap.getPixel(bitmap.width - 1, bitmap.height - 1) + + return if (!(topRightPx != 0 && topLeftPx != 0 && bottomRightPx != 0 && bottomLeftPx != 0)) { + scale + } else { + ICON_SCALE + } + } +} diff --git a/iconloaderlib/src/com/android/launcher3/icons/BaseIconFactory.java b/iconloaderlib/src/com/android/launcher3/icons/BaseIconFactory.java index 704df6f9bb24db0697b779532e2d7b37ab834a92..842d0eb0ba2b6f7e1273ecd13c2b00e64948a1cc 100644 --- a/iconloaderlib/src/com/android/launcher3/icons/BaseIconFactory.java +++ b/iconloaderlib/src/com/android/launcher3/icons/BaseIconFactory.java @@ -38,6 +38,7 @@ import androidx.annotation.ColorInt; import androidx.annotation.IntDef; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.core.graphics.ColorUtils; import com.android.launcher3.icons.BitmapInfo.Extender; import com.android.launcher3.util.FlagOp; @@ -323,10 +324,20 @@ public class BaseIconFactory implements AutoCloseable { if (!outShape[0]) { FixedScaleDrawable fsd = ((FixedScaleDrawable) dr.getForeground()); fsd.setDrawable(icon); - fsd.setScale(scale); + + int color; + if (icon.getIntrinsicHeight() > 0 && icon.getIntrinsicWidth() > 0) { + Bitmap bitmap = AdaptiveIconGenerator.toBitmap(icon); + fsd.setScale(AdaptiveIconGenerator.getScale(bitmap, scale)); + color = ColorUtils.setAlphaComponent(new ColorExtractor().findDominantColorByHue(bitmap), 200); + } else { + fsd.setScale(scale); + color = mWrapperBackgroundColor; + } + icon = dr; scale = getNormalizer().getScale(icon, outIconBounds, null, null); - ((ColorDrawable) dr.getBackground()).setColor(mWrapperBackgroundColor); + ((ColorDrawable) dr.getBackground()).setColor(color); } } else { scale = getNormalizer().getScale(icon, outIconBounds, null, null); diff --git a/iconloaderlib/src/com/android/launcher3/icons/DotRenderer.java b/iconloaderlib/src/com/android/launcher3/icons/DotRenderer.java index a42232e3f6d4edd6919865cad070997e28bdc3f1..066760de7fb5d344176703176d976a41e33658b1 100644 --- a/iconloaderlib/src/com/android/launcher3/icons/DotRenderer.java +++ b/iconloaderlib/src/com/android/launcher3/icons/DotRenderer.java @@ -27,9 +27,12 @@ import android.graphics.Path; import android.graphics.PathMeasure; import android.graphics.Rect; import android.graphics.RectF; +import android.graphics.Typeface; import android.util.Log; import android.view.ViewDebug; +import androidx.core.graphics.ColorUtils; + /** * Used to draw a notification dot on top of an icon. */ @@ -38,10 +41,17 @@ public class DotRenderer { private static final String TAG = "DotRenderer"; // The dot size is defined as a percentage of the app icon size. - private static final float SIZE_PERCENTAGE = 0.228f; + private static final float SIZE_PERCENTAGE = 0.21f; + private static final float SIZE_PERCENTAGE_WITH_COUNT = 0.28f; + + // The max number to draw on dots + private static final int MAX_COUNT = 999; private final float mCircleRadius; private final Paint mCirclePaint = new Paint(ANTI_ALIAS_FLAG | FILTER_BITMAP_FLAG); + private final Paint mCircleShadowPaint = new Paint(ANTI_ALIAS_FLAG | FILTER_BITMAP_FLAG); + + private final Paint mTextPaint = new Paint(ANTI_ALIAS_FLAG | FILTER_BITMAP_FLAG); private final Bitmap mBackgroundWithShadow; private final float mBitmapOffset; @@ -51,14 +61,22 @@ public class DotRenderer { private final float[] mLeftDotPosition; private static final int MIN_DOT_SIZE = 1; + private final Rect mTextRect = new Rect(); + private final boolean mDisplayCount; + public DotRenderer(int iconSizePx, Path iconShapePath, int pathSize) { - int size = Math.round(SIZE_PERCENTAGE * iconSizePx); + this(iconSizePx, iconShapePath, pathSize, false, null); + } + + public DotRenderer(int iconSizePx, Path iconShapePath, int pathSize, Boolean displayCount, Typeface typeface) { + mDisplayCount = displayCount; + int size = Math.round((displayCount ? SIZE_PERCENTAGE_WITH_COUNT : SIZE_PERCENTAGE) * iconSizePx); if (size <= 0) { size = MIN_DOT_SIZE; } ShadowGenerator.Builder builder = new ShadowGenerator.Builder(Color.TRANSPARENT); builder.ambientShadowAlpha = 88; - mBackgroundWithShadow = builder.setupBlurForSize(size).createPill(size, size); + mBackgroundWithShadow = builder.setupBlurForSize(size).createPill(size, displayCount ? (size - 25) : size); mCircleRadius = builder.radius; mBitmapOffset = -mBackgroundWithShadow.getHeight() * 0.5f; // Same as width. @@ -66,6 +84,10 @@ public class DotRenderer { // Find the points on the path that are closest to the top left and right corners. mLeftDotPosition = getPathPoint(iconShapePath, pathSize, -1); mRightDotPosition = getPathPoint(iconShapePath, pathSize, 1); + + mTextPaint.setTextSize(size * 0.65f); + mTextPaint.setTextAlign(Paint.Align.LEFT); + mTextPaint.setTypeface(typeface); } private static float[] getPathPoint(Path path, float size, float direction) { @@ -101,6 +123,10 @@ public class DotRenderer { * Draw a circle on top of the canvas according to the given params. */ public void draw(Canvas canvas, DrawParams params) { + draw(canvas, params, 0); + } + + public void draw(Canvas canvas, DrawParams params, int numNotifications) { if (params == null) { Log.e(TAG, "Invalid null argument(s) passed in call to draw."); return; @@ -120,16 +146,70 @@ public class DotRenderer { float offsetY = Math.max(0, canvasBounds.top - (dotCenterY + mBitmapOffset)); // We draw the dot relative to its center. - canvas.translate(dotCenterX + offsetX, dotCenterY + offsetY); + float dx = dotCenterX + offsetX; + float dy = dotCenterY + offsetY - 15f; + + if (numNotifications > 9 && numNotifications < 100) { + canvas.translate(dx - 3f, dy); + } else if (numNotifications > 99 && numNotifications < 1000) { + canvas.translate(dx + 6f, dy); + } else { + canvas.translate(dx - 12f, dy); + } + canvas.scale(params.scale, params.scale); - mCirclePaint.setColor(Color.BLACK); - canvas.drawBitmap(mBackgroundWithShadow, mBitmapOffset, mBitmapOffset, mCirclePaint); mCirclePaint.setColor(params.dotColor); - canvas.drawCircle(0, 0, mCircleRadius, mCirclePaint); + mCircleShadowPaint.setColor(params.shadowDotColor); + + if (numNotifications >= 10 && numNotifications < 1000) { + canvas.drawRoundRect(new RectF(-mCircleRadius + 10, -mCircleRadius, mCircleRadius + 20, mCircleRadius), 50, 50, mCircleShadowPaint); + canvas.drawRoundRect(new RectF(-mCircleRadius + 10, -mCircleRadius, mCircleRadius + 20, mCircleRadius), 50, 50, mCirclePaint); + } else { + canvas.drawCircle(5, 10, mCircleRadius, mCircleShadowPaint); + canvas.drawCircle(5, 10, mCircleRadius, mCirclePaint); + } + + if (mDisplayCount && numNotifications > 0) { + // Draw the numNotifications text + mTextPaint.setColor(getCounterTextColor(Color.WHITE)); + mTextPaint.setTypeface(Typeface.DEFAULT_BOLD); + mTextPaint.setTextSize(32f); + String text = numToNotation(numNotifications); + mTextPaint.getTextBounds(text, 0, text.length(), mTextRect); + float y = mTextRect.height() / 2f - mTextRect.bottom; + if (numNotifications < 10) { + canvas.drawText(text, -4f, 22f, mTextPaint); + } else if (numNotifications < 100) { + canvas.drawText(text, -4f, y, mTextPaint); + } else if (numNotifications >= 1000) { + canvas.drawText(text, -14f, 20f, mTextPaint); + } else { + canvas.drawText(text, -14f, y, mTextPaint); + } + } + canvas.restore(); } + private String numToNotation(int num) { + if (num < 1000) { + return String.valueOf(num); + } else { + return num / 1000 + "k"; + } + } + + /** + * Returns the color to use for the counter text based on the dot's background color. + * + * @param dotBackgroundColor The color of the dot background. + * @return The color to use on the counter text. + */ + private int getCounterTextColor(int dotBackgroundColor) { + return ColorUtils.setAlphaComponent(dotBackgroundColor, 0xFF); + } + public static class DrawParams { /** The color (possibly based on the icon) to use for the dot. */ @ViewDebug.ExportedProperty(category = "notification dot", formatToHexString = true) @@ -146,5 +226,7 @@ public class DotRenderer { /** Whether the dot should align to the top left of the icon rather than the top right. */ @ViewDebug.ExportedProperty(category = "notification dot") public boolean leftAlign; + @ViewDebug.ExportedProperty(category = "notification dot", formatToHexString = true) + public int shadowDotColor; } } diff --git a/iconloaderlib/src/com/android/launcher3/icons/IconProvider.java b/iconloaderlib/src/com/android/launcher3/icons/IconProvider.java index e8ce3b18a3910876f2583936065d2581f6b6e5d3..2f36d741409c458366197b5a4267c6e20b719694 100644 --- a/iconloaderlib/src/com/android/launcher3/icons/IconProvider.java +++ b/iconloaderlib/src/com/android/launcher3/icons/IconProvider.java @@ -69,6 +69,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 = " "; @@ -173,7 +175,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) { @@ -191,6 +195,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) { @@ -225,6 +233,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; + } + } + /** * @return Today's day of the month, zero-indexed. */ @@ -232,6 +262,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); diff --git a/searchuilib/build.gradle b/searchuilib/build.gradle index 6b5027b279e9997ef91d5e52ca45459658763411..4e763db6a0068eb6a6f88a6266a615684288d9e1 100644 --- a/searchuilib/build.gradle +++ b/searchuilib/build.gradle @@ -3,35 +3,11 @@ plugins { } android { - compileSdk TARGET_SDK.toInteger() - buildToolsVersion = BUILD_TOOLS_VERSION - - defaultConfig { - minSdkVersion TARGET_SDK.toInteger() - targetSdkVersion TARGET_SDK.toInteger() - } - + namespace "com.android.app.search" sourceSets { main { java.srcDirs = ['src'] manifest.srcFile 'AndroidManifest.xml' } } - - lintOptions { - abortOnError false - } - - tasks.withType(JavaCompile) { - options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation" - } - - compileOptions { - sourceCompatibility JavaVersion.VERSION_1_8 - targetCompatibility JavaVersion.VERSION_1_8 - } -} - -dependencies { - implementation "androidx.core:core:+" }