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 10ec889d1c6eceea40848b4cea14950da10011ea..7b92aac664ed574b7233c50e4c6320bdf3b43935 100644 --- a/iconloaderlib/build.gradle +++ b/iconloaderlib/build.gradle @@ -1,36 +1,15 @@ -apply plugin: 'com.android.library' +plugins { + id 'com.android.library' + id 'org.jetbrains.kotlin.android' +} android { - compileSdkVersion COMPILE_SDK - buildToolsVersion BUILD_TOOLS_VERSION - - defaultConfig { - minSdkVersion 26 - targetSdkVersion 28 - } - + 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" - } - - compileOptions { - sourceCompatibility JavaVersion.VERSION_1_8 - targetCompatibility JavaVersion.VERSION_1_8 - } -} - -dependencies { - implementation "androidx.core:core:${ANDROID_X_VERSION}" } 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 f5ce8144b0eeb29319be77c48b8ec4fd2c1d9fe2..88265b87a31c93148aec4b6394626d56fb4e9e32 100644 --- a/iconloaderlib/src/com/android/launcher3/icons/BaseIconFactory.java +++ b/iconloaderlib/src/com/android/launcher3/icons/BaseIconFactory.java @@ -26,6 +26,7 @@ import android.os.Process; import android.os.UserHandle; import androidx.annotation.NonNull; +import androidx.core.graphics.ColorUtils; import com.android.launcher3.icons.BitmapInfo.Extender; @@ -305,11 +306,20 @@ public class BaseIconFactory implements AutoCloseable { if (!(icon instanceof AdaptiveIconDrawable) && !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/ClockDrawableWrapper.java b/iconloaderlib/src/com/android/launcher3/icons/ClockDrawableWrapper.java index d2cbe72fffb569212f3f37887c158df50f6aef4d..e79d11f56a7b01f0e5d2bd3324a4bdb190264b4c 100644 --- a/iconloaderlib/src/com/android/launcher3/icons/ClockDrawableWrapper.java +++ b/iconloaderlib/src/com/android/launcher3/icons/ClockDrawableWrapper.java @@ -59,7 +59,7 @@ public class ClockDrawableWrapper extends AdaptiveIconDrawable implements Bitmap private static final String TAG = "ClockDrawableWrapper"; - private static final boolean DISABLE_SECONDS = true; + private static final boolean DISABLE_SECONDS = false; // Time after which the clock icon should check for an update. The actual invalidate // will only happen in case of any change. diff --git a/iconloaderlib/src/com/android/launcher3/icons/DotRenderer.java b/iconloaderlib/src/com/android/launcher3/icons/DotRenderer.java index 97a0fd3ffca69988b42fc25456a39c15b715804f..5209c092069b4dab728963139a9ef506abeac88f 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 = 99; 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; @@ -50,11 +60,19 @@ public class DotRenderer { private final float[] mRightDotPosition; private final float[] mLeftDotPosition; + 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); 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. @@ -62,6 +80,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) { @@ -97,6 +119,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; @@ -116,16 +142,68 @@ 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); - canvas.scale(params.scale, params.scale); + 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); + } - mCirclePaint.setColor(Color.BLACK); - canvas.drawBitmap(mBackgroundWithShadow, mBitmapOffset, mBitmapOffset, mCirclePaint); mCirclePaint.setColor(params.color); - 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) @@ -139,5 +217,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 449c0daa54beac952c8c87df680393ff9de505fe..a4dd4bf0a947572ffcbc301e32c95b094e95bfe4 100644 --- a/iconloaderlib/src/com/android/launcher3/icons/IconProvider.java +++ b/iconloaderlib/src/com/android/launcher3/icons/IconProvider.java @@ -32,6 +32,7 @@ import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.res.Resources; import android.content.res.XmlResourceParser; +import android.graphics.drawable.AdaptiveIconDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.Handler; @@ -70,6 +71,9 @@ public class IconProvider { private static final boolean DEBUG = false; 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 = " "; private static final String THEMED_ICON_MAP_FILE = "grayscale_icon_map"; @@ -223,6 +227,7 @@ public class IconProvider { } private Drawable loadCalendarDrawable(int iconDpi) { + Log.e("lulz", "loading cal iconn"); PackageManager pm = mContext.getPackageManager(); try { final Bundle metadata = pm.getActivityInfo( @@ -231,9 +236,15 @@ 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); return resources.getDrawableForDensity(id, iconDpi, null /* theme */); + } 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) { @@ -272,6 +283,29 @@ 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. */ @@ -279,6 +313,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 3bf65fe644cc51c1c9b1bcea90b61581f8ab41ba..fb11340951c7e31238ea79729df7508f62e81395 100644 --- a/searchuilib/build.gradle +++ b/searchuilib/build.gradle @@ -1,35 +1,11 @@ apply plugin: 'com.android.library' android { - compileSdkVersion COMPILE_SDK - buildToolsVersion BUILD_TOOLS_VERSION - - defaultConfig { - minSdkVersion 25 - targetSdkVersion 28 - } - + 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:${ANDROID_X_VERSION}" }