Merge "Simplifying IconShape" into main
diff --git a/res/xml/folder_shapes.xml b/res/xml/folder_shapes.xml
deleted file mode 100644
index e60d333..0000000
--- a/res/xml/folder_shapes.xml
+++ /dev/null
@@ -1,33 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
- Copyright (C) 2019 The Android Open Source Project
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
--->
-<shapes xmlns:launcher="http://schemas.android.com/apk/res-auto" >
-
- <Circle launcher:folderIconRadius="1" />
-
- <!-- Default icon for AOSP -->
- <RoundedSquare launcher:folderIconRadius="0.16" />
-
- <!-- Rounded icon from RRO -->
- <RoundedSquare launcher:folderIconRadius="0.6" />
-
- <!-- Square icon -->
- <RoundedSquare launcher:folderIconRadius="0" />
-
- <TearDrop launcher:folderIconRadius="0.3" />
- <Squircle launcher:folderIconRadius="0.2" />
-
-</shapes>
\ No newline at end of file
diff --git a/src/com/android/launcher3/graphics/IconShape.kt b/src/com/android/launcher3/graphics/IconShape.kt
index c64d4da..1377610 100644
--- a/src/com/android/launcher3/graphics/IconShape.kt
+++ b/src/com/android/launcher3/graphics/IconShape.kt
@@ -17,23 +17,29 @@
import android.animation.Animator
import android.animation.AnimatorListenerAdapter
-import android.animation.FloatArrayEvaluator
import android.animation.ValueAnimator
-import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
+import android.graphics.Matrix
+import android.graphics.Matrix.ScaleToFit.FILL
import android.graphics.Paint
import android.graphics.Path
import android.graphics.Rect
+import android.graphics.RectF
import android.graphics.Region
import android.graphics.drawable.AdaptiveIconDrawable
import android.graphics.drawable.ColorDrawable
-import android.util.Xml
+import android.util.Log
import android.view.View
import android.view.ViewOutlineProvider
-import com.android.launcher3.R
+import androidx.annotation.VisibleForTesting
+import androidx.graphics.shapes.CornerRounding
+import androidx.graphics.shapes.Morph
+import androidx.graphics.shapes.RoundedPolygon
+import androidx.graphics.shapes.SvgPathParser
+import androidx.graphics.shapes.toPath
+import androidx.graphics.shapes.transformed
import com.android.launcher3.anim.RoundedRectRevealOutlineProvider
-import com.android.launcher3.dagger.ApplicationContext
import com.android.launcher3.dagger.LauncherAppComponent
import com.android.launcher3.dagger.LauncherAppSingleton
import com.android.launcher3.graphics.ThemeManager.ThemeChangeListener
@@ -42,74 +48,38 @@
import com.android.launcher3.util.DaggerSingletonObject
import com.android.launcher3.util.DaggerSingletonTracker
import com.android.launcher3.views.ClipPathView
-import java.io.IOException
import javax.inject.Inject
-import org.xmlpull.v1.XmlPullParser
-import org.xmlpull.v1.XmlPullParserException
/** Abstract representation of the shape of an icon shape */
@LauncherAppSingleton
-class IconShape
-@Inject
-constructor(
- @ApplicationContext context: Context,
- themeManager: ThemeManager,
- lifeCycle: DaggerSingletonTracker,
-) {
- var shape: ShapeDelegate = Circle()
- private set
+class IconShape @Inject constructor(themeManager: ThemeManager, lifeCycle: DaggerSingletonTracker) {
var normalizationScale: Float = IconNormalizer.ICON_VISIBLE_AREA_FACTOR
private set
- init {
- pickBestShape(context)
+ var shape: ShapeDelegate = pickBestShape(themeManager)
+ private set
- val changeListener = ThemeChangeListener { pickBestShape(context) }
+ init {
+ val changeListener = ThemeChangeListener { shape = pickBestShape(themeManager) }
+
themeManager.addChangeListener(changeListener)
lifeCycle.addCloseable { themeManager.removeChangeListener(changeListener) }
}
/** Initializes the shape which is closest to the [AdaptiveIconDrawable] */
- fun pickBestShape(context: Context) {
- // Pick any large size
- val size = 200
- val full = Region(0, 0, size, size)
- val shapePath = Path()
- val shapeR = Region()
- val iconR = Region()
- val drawable = AdaptiveIconDrawable(ColorDrawable(Color.BLACK), ColorDrawable(Color.BLACK))
- drawable.setBounds(0, 0, size, size)
- iconR.setPath(drawable.iconMask, full)
-
- // Find the shape with minimum area of divergent region.
- var minArea = Int.MAX_VALUE
- var closestShape: ShapeDelegate? = null
- for (shape in getAllShapes(context)) {
- shapePath.reset()
- shape.addToPath(shapePath, 0f, 0f, size / 2f)
- shapeR.setPath(shapePath, full)
- shapeR.op(iconR, Region.Op.XOR)
-
- val area = GraphicsUtils.getArea(shapeR)
- if (area < minArea) {
- minArea = area
- closestShape = shape
+ private fun pickBestShape(themeManager: ThemeManager): ShapeDelegate {
+ val drawable =
+ AdaptiveIconDrawable(null, ColorDrawable(Color.BLACK)).apply {
+ setBounds(0, 0, AREA_CALC_SIZE, AREA_CALC_SIZE)
}
- }
- if (closestShape != null) {
- shape = closestShape
- }
-
- // Initialize shape properties
- normalizationScale = IconNormalizer.normalizeAdaptiveIcon(drawable, size, null)
+ normalizationScale = IconNormalizer.normalizeAdaptiveIcon(drawable, AREA_CALC_SIZE, null)
+ return pickBestShape(drawable.iconMask, themeManager.iconState.iconMask)
}
interface ShapeDelegate {
- fun enableShapeDetection(): Boolean {
- return false
- }
+ fun enableShapeDetection() = false
fun drawShape(canvas: Canvas, offsetX: Float, offsetY: Float, radius: Float, paint: Paint)
@@ -121,38 +91,11 @@
endRect: Rect,
endRadius: Float,
isReversed: Boolean,
- ): ValueAnimator where T : View?, T : ClipPathView?
+ ): ValueAnimator where T : View, T : ClipPathView
}
- /** Abstract shape where the reveal animation is a derivative of a round rect animation */
- private abstract class SimpleRectShape : ShapeDelegate {
- override fun <T> createRevealAnimator(
- target: T,
- startRect: Rect,
- endRect: Rect,
- endRadius: Float,
- isReversed: Boolean,
- ): ValueAnimator where T : View?, T : ClipPathView? {
- return object :
- RoundedRectRevealOutlineProvider(
- getStartRadius(startRect),
- endRadius,
- startRect,
- endRect,
- ) {
- override fun shouldRemoveElevationDuringAnimation(): Boolean {
- return true
- }
- }
- .createRevealAnimator(target, isReversed)
- }
-
- protected abstract fun getStartRadius(startRect: Rect): Float
- }
-
- /** Abstract shape which draws using [Path] */
- abstract class PathShape : ShapeDelegate {
- private val mTmpPath = Path()
+ @VisibleForTesting
+ class Circle : RoundedSquare(1f) {
override fun drawShape(
canvas: Canvas,
@@ -160,138 +103,18 @@
offsetY: Float,
radius: Float,
paint: Paint,
- ) {
- mTmpPath.reset()
- addToPath(mTmpPath, offsetX, offsetY, radius)
- canvas.drawPath(mTmpPath, paint)
- }
+ ) = canvas.drawCircle(radius + offsetX, radius + offsetY, radius, paint)
- protected abstract fun newUpdateListener(
- startRect: Rect,
- endRect: Rect,
- endRadius: Float,
- outPath: Path,
- ): ValueAnimator.AnimatorUpdateListener
-
- override fun <T> createRevealAnimator(
- target: T,
- startRect: Rect,
- endRect: Rect,
- endRadius: Float,
- isReversed: Boolean,
- ): ValueAnimator where T : View?, T : ClipPathView? {
- val path = Path()
- val listener = newUpdateListener(startRect, endRect, endRadius, path)
-
- val va =
- if (isReversed) ValueAnimator.ofFloat(1f, 0f) else ValueAnimator.ofFloat(0f, 1f)
- va.addListener(
- object : AnimatorListenerAdapter() {
- private var mOldOutlineProvider: ViewOutlineProvider? = null
-
- override fun onAnimationStart(animation: Animator) {
- target?.apply {
- mOldOutlineProvider = outlineProvider
- outlineProvider = null
- translationZ = -target.elevation
- }
- }
-
- override fun onAnimationEnd(animation: Animator) {
- target?.apply {
- translationZ = 0f
- setClipPath(null)
- outlineProvider = mOldOutlineProvider
- }
- }
- }
- )
-
- va.addUpdateListener { anim: ValueAnimator ->
- path.reset()
- listener.onAnimationUpdate(anim)
- target?.setClipPath(path)
- }
-
- return va
- }
- }
-
- open class Circle : PathShape() {
- private val mTempRadii = FloatArray(8)
-
- override fun newUpdateListener(
- startRect: Rect,
- endRect: Rect,
- endRadius: Float,
- outPath: Path,
- ): ValueAnimator.AnimatorUpdateListener {
- val r1 = getStartRadius(startRect)
-
- val startValues =
- floatArrayOf(
- startRect.left.toFloat(),
- startRect.top.toFloat(),
- startRect.right.toFloat(),
- startRect.bottom.toFloat(),
- r1,
- r1,
- )
- val endValues =
- floatArrayOf(
- endRect.left.toFloat(),
- endRect.top.toFloat(),
- endRect.right.toFloat(),
- endRect.bottom.toFloat(),
- endRadius,
- endRadius,
- )
-
- val evaluator = FloatArrayEvaluator(FloatArray(6))
-
- return ValueAnimator.AnimatorUpdateListener { anim: ValueAnimator ->
- val progress = anim.animatedValue as Float
- val values = evaluator.evaluate(progress, startValues, endValues)
- outPath.addRoundRect(
- values[0],
- values[1],
- values[2],
- values[3],
- getRadiiArray(values[4], values[5]),
- Path.Direction.CW,
- )
- }
- }
-
- private fun getRadiiArray(r1: Float, r2: Float): FloatArray {
- mTempRadii[7] = r1
- mTempRadii[6] = mTempRadii[7]
- mTempRadii[3] = mTempRadii[6]
- mTempRadii[2] = mTempRadii[3]
- mTempRadii[1] = mTempRadii[2]
- mTempRadii[0] = mTempRadii[1]
- mTempRadii[5] = r2
- mTempRadii[4] = mTempRadii[5]
- return mTempRadii
- }
-
- override fun addToPath(path: Path, offsetX: Float, offsetY: Float, radius: Float) {
+ override fun addToPath(path: Path, offsetX: Float, offsetY: Float, radius: Float) =
path.addCircle(radius + offsetX, radius + offsetY, radius, Path.Direction.CW)
- }
- private fun getStartRadius(startRect: Rect): Float {
- return startRect.width() / 2f
- }
-
- override fun enableShapeDetection(): Boolean {
- return true
- }
+ override fun enableShapeDetection() = true
}
- private class RoundedSquare(
- /** Ratio of corner radius to half size. */
- private val mRadiusRatio: Float
- ) : SimpleRectShape() {
+ /** Rounded square with [radiusRatio] as a ratio of its half edge size */
+ @VisibleForTesting
+ open class RoundedSquare(val radiusRatio: Float) : ShapeDelegate {
+
override fun drawShape(
canvas: Canvas,
offsetX: Float,
@@ -301,14 +124,14 @@
) {
val cx = radius + offsetX
val cy = radius + offsetY
- val cr = radius * mRadiusRatio
+ val cr = radius * radiusRatio
canvas.drawRoundRect(cx - radius, cy - radius, cx + radius, cy + radius, cr, cr, paint)
}
override fun addToPath(path: Path, offsetX: Float, offsetY: Float, radius: Float) {
val cx = radius + offsetX
val cy = radius + offsetY
- val cr = radius * mRadiusRatio
+ val cr = radius * radiusRatio
path.addRoundRect(
cx - radius,
cy - radius,
@@ -320,213 +143,205 @@
)
}
- override fun getStartRadius(startRect: Rect): Float {
- return (startRect.width() / 2f) * mRadiusRatio
+ override fun <T> createRevealAnimator(
+ target: T,
+ startRect: Rect,
+ endRect: Rect,
+ endRadius: Float,
+ isReversed: Boolean,
+ ): ValueAnimator where T : View, T : ClipPathView {
+ return object :
+ RoundedRectRevealOutlineProvider(
+ (startRect.width() / 2f) * radiusRatio,
+ endRadius,
+ startRect,
+ endRect,
+ ) {
+ override fun shouldRemoveElevationDuringAnimation() = true
+ }
+ .createRevealAnimator(target, isReversed)
}
}
- private class TearDrop(
- /**
- * Radio of short radius to large radius, based on the shape options defined in the config.
- */
- private val mRadiusRatio: Float
- ) : PathShape() {
- private val mTempRadii = FloatArray(8)
-
- override fun addToPath(path: Path, offsetX: Float, offsetY: Float, radius: Float) {
- val r2 = radius * mRadiusRatio
- val cx = radius + offsetX
- val cy = radius + offsetY
-
- path.addRoundRect(
- cx - radius,
- cy - radius,
- cx + radius,
- cy + radius,
- getRadiiArray(radius, r2),
- Path.Direction.CW,
+ /** Generic shape delegate with pathString in bounds [0, 0, 100, 100] */
+ class GenericPathShape(pathString: String) : ShapeDelegate {
+ private val poly =
+ RoundedPolygon(
+ features = SvgPathParser.parseFeatures(pathString),
+ centerX = 50f,
+ centerY = 50f,
)
- }
-
- fun getRadiiArray(r1: Float, r2: Float): FloatArray {
- mTempRadii[7] = r1
- mTempRadii[6] = mTempRadii[7]
- mTempRadii[3] = mTempRadii[6]
- mTempRadii[2] = mTempRadii[3]
- mTempRadii[1] = mTempRadii[2]
- mTempRadii[0] = mTempRadii[1]
- mTempRadii[5] = r2
- mTempRadii[4] = mTempRadii[5]
- return mTempRadii
- }
-
- override fun newUpdateListener(
- startRect: Rect,
- endRect: Rect,
- endRadius: Float,
- outPath: Path,
- ): ValueAnimator.AnimatorUpdateListener {
- val r1 = startRect.width() / 2f
- val r2 = r1 * mRadiusRatio
-
- val startValues =
- floatArrayOf(
- startRect.left.toFloat(),
- startRect.top.toFloat(),
- startRect.right.toFloat(),
- startRect.bottom.toFloat(),
- r1,
- r2,
- )
- val endValues =
- floatArrayOf(
- endRect.left.toFloat(),
- endRect.top.toFloat(),
- endRect.right.toFloat(),
- endRect.bottom.toFloat(),
- endRadius,
- endRadius,
- )
-
- val evaluator = FloatArrayEvaluator(FloatArray(6))
-
- return ValueAnimator.AnimatorUpdateListener { anim: ValueAnimator ->
- val progress = anim.animatedValue as Float
- val values = evaluator.evaluate(progress, startValues, endValues)
- outPath.addRoundRect(
- values[0],
- values[1],
- values[2],
- values[3],
- getRadiiArray(values[4], values[5]),
- Path.Direction.CW,
- )
+ // This ensures that a valid morph is possible from the provided path
+ private val basePath =
+ Path().apply {
+ Morph(poly, createRoundedRect(0f, 0f, 100f, 100f, 25f)).toPath(0f, this)
}
- }
- }
+ private val tmpPath = Path()
+ private val tmpMatrix = Matrix()
- private class Squircle(
- /** Radio of radius to circle radius, based on the shape options defined in the config. */
- private val mRadiusRatio: Float
- ) : PathShape() {
+ override fun drawShape(
+ canvas: Canvas,
+ offsetX: Float,
+ offsetY: Float,
+ radius: Float,
+ paint: Paint,
+ ) {
+ tmpPath.reset()
+ addToPath(tmpPath, offsetX, offsetY, radius)
+ canvas.drawPath(tmpPath, paint)
+ }
+
override fun addToPath(path: Path, offsetX: Float, offsetY: Float, radius: Float) {
- val cx = radius + offsetX
- val cy = radius + offsetY
- val control = radius - radius * mRadiusRatio
-
- path.moveTo(cx, cy - radius)
- addLeftCurve(cx, cy, radius, control, path)
- addRightCurve(cx, cy, radius, control, path)
- addLeftCurve(cx, cy, -radius, -control, path)
- addRightCurve(cx, cy, -radius, -control, path)
- path.close()
+ tmpMatrix.setScale(radius / 50, radius / 50)
+ tmpMatrix.postTranslate(offsetX, offsetY)
+ basePath.transform(tmpMatrix, path)
}
- fun addLeftCurve(cx: Float, cy: Float, r: Float, control: Float, path: Path) {
- path.cubicTo(cx - control, cy - r, cx - r, cy - control, cx - r, cy)
- }
-
- fun addRightCurve(cx: Float, cy: Float, r: Float, control: Float, path: Path) {
- path.cubicTo(cx - r, cy + control, cx - control, cy + r, cx, cy + r)
- }
-
- override fun newUpdateListener(
+ override fun <T> createRevealAnimator(
+ target: T,
startRect: Rect,
endRect: Rect,
endRadius: Float,
- outPath: Path,
- ): ValueAnimator.AnimatorUpdateListener {
- val startCX = startRect.exactCenterX()
- val startCY = startRect.exactCenterY()
- val startR = startRect.width() / 2f
- val startControl = startR - startR * mRadiusRatio
- val startHShift = 0f
- val startVShift = 0f
+ isReversed: Boolean,
+ ): ValueAnimator where T : View, T : ClipPathView {
+ // End poly is defined as a rectangle starting at top/center so that the
+ // transformation has minimum motion
+ val morph =
+ Morph(
+ start =
+ poly.transformed(
+ Matrix().apply {
+ setRectToRect(RectF(0f, 0f, 100f, 100f), RectF(startRect), FILL)
+ }
+ ),
+ end =
+ createRoundedRect(
+ left = endRect.left.toFloat(),
+ top = endRect.top.toFloat(),
+ right = endRect.right.toFloat(),
+ bottom = endRect.bottom.toFloat(),
+ cornerR = endRadius,
+ ),
+ )
- val endCX = endRect.exactCenterX()
- val endCY = endRect.exactCenterY()
- // Approximate corner circle using bezier curves
- // http://spencermortensen.com/articles/bezier-circle/
- val endControl = endRadius * 0.551915024494f
- val endHShift = endRect.width() / 2f - endRadius
- val endVShift = endRect.height() / 2f - endRadius
+ val va =
+ if (isReversed) ValueAnimator.ofFloat(1f, 0f) else ValueAnimator.ofFloat(0f, 1f)
+ va.addListener(
+ object : AnimatorListenerAdapter() {
+ private var oldOutlineProvider: ViewOutlineProvider? = null
- return ValueAnimator.AnimatorUpdateListener { anim: ValueAnimator ->
- val progress = anim.animatedValue as Float
- val cx = (1 - progress) * startCX + progress * endCX
- val cy = (1 - progress) * startCY + progress * endCY
- val r = (1 - progress) * startR + progress * endRadius
- val control = (1 - progress) * startControl + progress * endControl
- val hShift = (1 - progress) * startHShift + progress * endHShift
- val vShift = (1 - progress) * startVShift + progress * endVShift
+ override fun onAnimationStart(animation: Animator) {
+ target?.apply {
+ oldOutlineProvider = outlineProvider
+ outlineProvider = null
+ translationZ = -target.elevation
+ }
+ }
- outPath.moveTo(cx, cy - vShift - r)
- outPath.rLineTo(-hShift, 0f)
+ override fun onAnimationEnd(animation: Animator) {
+ target.apply {
+ translationZ = 0f
+ setClipPath(null)
+ outlineProvider = oldOutlineProvider
+ }
+ }
+ }
+ )
- addLeftCurve(cx - hShift, cy - vShift, r, control, outPath)
- outPath.rLineTo(0f, vShift + vShift)
-
- addRightCurve(cx - hShift, cy + vShift, r, control, outPath)
- outPath.rLineTo(hShift + hShift, 0f)
-
- addLeftCurve(cx + hShift, cy + vShift, -r, -control, outPath)
- outPath.rLineTo(0f, -vShift - vShift)
-
- addRightCurve(cx + hShift, cy - vShift, -r, -control, outPath)
- outPath.close()
+ val path = Path()
+ va.addUpdateListener { anim: ValueAnimator ->
+ path.reset()
+ morph.toPath(anim.animatedValue as Float, path)
+ target.setClipPath(path)
}
+ return va
}
}
companion object {
@JvmField var INSTANCE = DaggerSingletonObject(LauncherAppComponent::getIconShape)
- private fun getShapeDefinition(type: String, radius: Float): ShapeDelegate {
- return when (type) {
- "Circle" -> Circle()
- "RoundedSquare" -> RoundedSquare(radius)
- "TearDrop" -> TearDrop(radius)
- "Squircle" -> Squircle(radius)
- else -> throw IllegalArgumentException("Invalid shape type: $type")
+ const val TAG = "IconShape"
+
+ const val AREA_CALC_SIZE = 1000
+ // .1% error margin
+ const val AREA_DIFF_THRESHOLD = AREA_CALC_SIZE * AREA_CALC_SIZE / 1000
+
+ /** Returns a function to calculate area diff from [base] */
+ @VisibleForTesting
+ fun areaDiffCalculator(base: Path): (ShapeDelegate) -> Int {
+ val fullRegion = Region(0, 0, AREA_CALC_SIZE, AREA_CALC_SIZE)
+ val iconRegion = Region().apply { setPath(base, fullRegion) }
+
+ val shapePath = Path()
+ val shapeRegion = Region()
+ return fun(shape: ShapeDelegate): Int {
+ shapePath.reset()
+ shape.addToPath(shapePath, 0f, 0f, AREA_CALC_SIZE / 2f)
+ shapeRegion.setPath(shapePath, fullRegion)
+ shapeRegion.op(iconRegion, Region.Op.XOR)
+ return GraphicsUtils.getArea(shapeRegion)
}
}
- private fun getAllShapes(context: Context): List<ShapeDelegate> {
- val result = ArrayList<ShapeDelegate>()
- try {
- context.resources.getXml(R.xml.folder_shapes).use { parser ->
- // Find the root tag
- var type: Int = parser.next()
- while (
- type != XmlPullParser.END_TAG &&
- type != XmlPullParser.END_DOCUMENT &&
- "shapes" != parser.name
- ) {
- type = parser.next()
- }
- val depth = parser.depth
- val radiusAttr = intArrayOf(R.attr.folderIconRadius)
- type = parser.next()
- while (
- (type != XmlPullParser.END_TAG || parser.depth > depth) &&
- type != XmlPullParser.END_DOCUMENT
- ) {
- if (type == XmlPullParser.START_TAG) {
- val attrs = Xml.asAttributeSet(parser)
- val arr = context.obtainStyledAttributes(attrs, radiusAttr)
- val shape = getShapeDefinition(parser.name, arr.getFloat(0, 1f))
- arr.recycle()
- result.add(shape)
- }
- type = parser.next()
- }
+ @VisibleForTesting
+ fun pickBestShape(baseShape: Path, shapeStr: String): ShapeDelegate {
+ val calcAreaDiff = areaDiffCalculator(baseShape)
+
+ // Find the shape with minimum area of divergent region.
+ var closestShape: ShapeDelegate = Circle()
+ var minAreaDiff = calcAreaDiff(closestShape)
+
+ // Try some common rounded rect edges
+ for (f in 0..20) {
+ val rectShape = RoundedSquare(f.toFloat() / 20)
+ val rectArea = calcAreaDiff(rectShape)
+ if (rectArea < minAreaDiff) {
+ minAreaDiff = rectArea
+ closestShape = rectShape
}
- } catch (e: IOException) {
- throw RuntimeException(e)
- } catch (e: XmlPullParserException) {
- throw RuntimeException(e)
}
- return result
+
+ // Use the generic shape only if we have more than .1% error
+ if (shapeStr.isNotEmpty() && minAreaDiff > AREA_DIFF_THRESHOLD) {
+ try {
+ val generic = GenericPathShape(shapeStr)
+ closestShape = generic
+ } catch (e: Exception) {
+ Log.e(TAG, "Error converting mask to generic shape", e)
+ }
+ }
+ return closestShape
}
+
+ /**
+ * Creates a rounded rect with the start point at the center of the top edge. This ensures a
+ * better animation since our shape paths also start at top-center of the bounding box.
+ */
+ fun createRoundedRect(
+ left: Float,
+ top: Float,
+ right: Float,
+ bottom: Float,
+ cornerR: Float,
+ ) =
+ RoundedPolygon(
+ vertices =
+ floatArrayOf(
+ (left + right) / 2,
+ top,
+ right,
+ top,
+ right,
+ bottom,
+ left,
+ bottom,
+ left,
+ top,
+ ),
+ centerX = (left + right) / 2,
+ centerY = (top + bottom) / 2,
+ rounding = CornerRounding(cornerR),
+ )
}
}
diff --git a/src/com/android/launcher3/shapes/AppShapesProvider.kt b/src/com/android/launcher3/shapes/AppShapesProvider.kt
index 097f3ef..3f4549a 100644
--- a/src/com/android/launcher3/shapes/AppShapesProvider.kt
+++ b/src/com/android/launcher3/shapes/AppShapesProvider.kt
@@ -26,22 +26,22 @@
AppShape(
"arch",
"arch",
- "M100 83.46C100 85.471 100 86.476 99.9 87.321 99.116 93.916 93.916 99.116 87.321 99.9 86.476 100 85.471 100 83.46 100H16.54C14.529 100 13.524 100 12.679 99.9 6.084 99.116.884 93.916.1 87.321 0 86.476 0 85.471 0 83.46L0 50C0 22.386 22.386 0 50 0 77.614 0 100 22.386 100 50V83.46Z",
+ "M100 83.46C100 85.471 100 86.476 99.9 87.321 99.116 93.916 93.916 99.116 87.321 99.9 86.476 100 85.471 100 83.46 100H16.54C14.529 100 13.524 100 12.679 99.9 6.084 99.116 .884 93.916 .1 87.321 0 86.476 0 85.471 0 83.46L0 50C0 22.386 22.386 0 50 0 77.614 0 100 22.386 100 50V83.46Z",
),
AppShape(
"4_sided_cookie",
"4 sided cookie",
- "M63.605 3C84.733-6.176 106.176 15.268 97 36.395L95.483 39.888C92.681 46.338 92.681 53.662 95.483 60.112L97 63.605C106.176 84.732 84.733 106.176 63.605 97L60.112 95.483C53.662 92.681 46.338 92.681 39.888 95.483L36.395 97C15.267 106.176-6.176 84.732 3 63.605L4.517 60.112C7.319 53.662 7.319 46.338 4.517 39.888L3 36.395C-6.176 15.268 15.267-6.176 36.395 3L39.888 4.517C46.338 7.319 53.662 7.319 60.112 4.517L63.605 3Z",
+ "M63.605 3C84.733 -6.176 106.176 15.268 97 36.395L95.483 39.888C92.681 46.338 92.681 53.662 95.483 60.112L97 63.605C106.176 84.732 84.733 106.176 63.605 97L60.112 95.483C53.662 92.681 46.338 92.681 39.888 95.483L36.395 97C15.267 106.176 -6.176 84.732 3 63.605L4.517 60.112C7.319 53.662 7.319 46.338 4.517 39.888L3 36.395C -6.176 15.268 15.267 -6.176 36.395 3L39.888 4.517C46.338 7.319 53.662 7.319 60.112 4.517L63.605 3Z",
),
AppShape(
"seven_sided_cookie",
"7 sided cookie",
- "M35.209 4.878C36.326 3.895 36.884 3.404 37.397 3.006 44.82-2.742 55.18-2.742 62.603 3.006 63.116 3.404 63.674 3.895 64.791 4.878 65.164 5.207 65.351 5.371 65.539 5.529 68.167 7.734 71.303 9.248 74.663 9.932 74.902 9.981 75.147 10.025 75.637 10.113 77.1 10.375 77.831 10.506 78.461 10.66 87.573 12.893 94.032 21.011 94.176 30.412 94.186 31.062 94.151 31.805 94.08 33.293 94.057 33.791 94.045 34.04 94.039 34.285 93.958 37.72 94.732 41.121 96.293 44.18 96.404 44.399 96.522 44.618 96.759 45.056 97.467 46.366 97.821 47.021 98.093 47.611 102.032 56.143 99.727 66.266 92.484 72.24 91.983 72.653 91.381 73.089 90.177 73.961 89.774 74.254 89.572 74.4 89.377 74.548 86.647 76.626 84.477 79.353 83.063 82.483 82.962 82.707 82.865 82.936 82.671 83.395 82.091 84.766 81.8 85.451 81.51 86.033 77.31 94.44 67.977 98.945 58.801 96.994 58.166 96.859 57.451 96.659 56.019 96.259 55.54 96.125 55.3 96.058 55.063 95.998 51.74 95.154 48.26 95.154 44.937 95.998 44.699 96.058 44.46 96.125 43.981 96.259 42.549 96.659 41.834 96.859 41.199 96.994 32.023 98.945 22.69 94.44 18.49 86.033 18.2 85.451 17.909 84.766 17.329 83.395 17.135 82.936 17.038 82.707 16.937 82.483 15.523 79.353 13.353 76.626 10.623 74.548 10.428 74.4 10.226 74.254 9.823 73.961 8.619 73.089 8.017 72.653 7.516 72.24.273 66.266-2.032 56.143 1.907 47.611 2.179 47.021 2.533 46.366 3.241 45.056 3.478 44.618 3.596 44.399 3.707 44.18 5.268 41.121 6.042 37.72 5.961 34.285 5.955 34.04 5.943 33.791 5.92 33.293 5.849 31.805 5.814 31.062 5.824 30.412 5.968 21.011 12.427 12.893 21.539 10.66 22.169 10.506 22.9 10.375 24.363 10.113 24.853 10.025 25.098 9.981 25.337 9.932 28.697 9.248 31.833 7.734 34.461 5.529 34.649 5.371 34.836 5.207 35.209 4.878Z",
+ "M35.209 4.878C36.326 3.895 36.884 3.404 37.397 3.006 44.82 -2.742 55.18 -2.742 62.603 3.006 63.116 3.404 63.674 3.895 64.791 4.878 65.164 5.207 65.351 5.371 65.539 5.529 68.167 7.734 71.303 9.248 74.663 9.932 74.902 9.981 75.147 10.025 75.637 10.113 77.1 10.375 77.831 10.506 78.461 10.66 87.573 12.893 94.032 21.011 94.176 30.412 94.186 31.062 94.151 31.805 94.08 33.293 94.057 33.791 94.045 34.04 94.039 34.285 93.958 37.72 94.732 41.121 96.293 44.18 96.404 44.399 96.522 44.618 96.759 45.056 97.467 46.366 97.821 47.021 98.093 47.611 102.032 56.143 99.727 66.266 92.484 72.24 91.983 72.653 91.381 73.089 90.177 73.961 89.774 74.254 89.572 74.4 89.377 74.548 86.647 76.626 84.477 79.353 83.063 82.483 82.962 82.707 82.865 82.936 82.671 83.395 82.091 84.766 81.8 85.451 81.51 86.033 77.31 94.44 67.977 98.945 58.801 96.994 58.166 96.859 57.451 96.659 56.019 96.259 55.54 96.125 55.3 96.058 55.063 95.998 51.74 95.154 48.26 95.154 44.937 95.998 44.699 96.058 44.46 96.125 43.981 96.259 42.549 96.659 41.834 96.859 41.199 96.994 32.023 98.945 22.69 94.44 18.49 86.033 18.2 85.451 17.909 84.766 17.329 83.395 17.135 82.936 17.038 82.707 16.937 82.483 15.523 79.353 13.353 76.626 10.623 74.548 10.428 74.4 10.226 74.254 9.823 73.961 8.619 73.089 8.017 72.653 7.516 72.24 .273 66.266 -2.032 56.143 1.907 47.611 2.179 47.021 2.533 46.366 3.241 45.056 3.478 44.618 3.596 44.399 3.707 44.18 5.268 41.121 6.042 37.72 5.961 34.285 5.955 34.04 5.943 33.791 5.92 33.293 5.849 31.805 5.814 31.062 5.824 30.412 5.968 21.011 12.427 12.893 21.539 10.66 22.169 10.506 22.9 10.375 24.363 10.113 24.853 10.025 25.098 9.981 25.337 9.932 28.697 9.248 31.833 7.734 34.461 5.529 34.649 5.371 34.836 5.207 35.209 4.878Z",
),
AppShape(
"sunny",
"sunny",
- "M42.846 4.873C46.084-.531 53.916-.531 57.154 4.873L60.796 10.951C62.685 14.103 66.414 15.647 69.978 14.754L76.851 13.032C82.962 11.5 88.5 17.038 86.968 23.149L85.246 30.022C84.353 33.586 85.897 37.315 89.049 39.204L95.127 42.846C100.531 46.084 100.531 53.916 95.127 57.154L89.049 60.796C85.897 62.685 84.353 66.414 85.246 69.978L86.968 76.851C88.5 82.962 82.962 88.5 76.851 86.968L69.978 85.246C66.414 84.353 62.685 85.898 60.796 89.049L57.154 95.127C53.916 100.531 46.084 100.531 42.846 95.127L39.204 89.049C37.315 85.898 33.586 84.353 30.022 85.246L23.149 86.968C17.038 88.5 11.5 82.962 13.032 76.851L14.754 69.978C15.647 66.414 14.103 62.685 10.951 60.796L4.873 57.154C-.531 53.916-.531 46.084 4.873 42.846L10.951 39.204C14.103 37.315 15.647 33.586 14.754 30.022L13.032 23.149C11.5 17.038 17.038 11.5 23.149 13.032L30.022 14.754C33.586 15.647 37.315 14.103 39.204 10.951L42.846 4.873Z",
+ "M42.846 4.873C46.084 -.531 53.916 -.531 57.154 4.873L60.796 10.951C62.685 14.103 66.414 15.647 69.978 14.754L76.851 13.032C82.962 11.5 88.5 17.038 86.968 23.149L85.246 30.022C84.353 33.586 85.897 37.315 89.049 39.204L95.127 42.846C100.531 46.084 100.531 53.916 95.127 57.154L89.049 60.796C85.897 62.685 84.353 66.414 85.246 69.978L86.968 76.851C88.5 82.962 82.962 88.5 76.851 86.968L69.978 85.246C66.414 84.353 62.685 85.898 60.796 89.049L57.154 95.127C53.916 100.531 46.084 100.531 42.846 95.127L39.204 89.049C37.315 85.898 33.586 84.353 30.022 85.246L23.149 86.968C17.038 88.5 11.5 82.962 13.032 76.851L14.754 69.978C15.647 66.414 14.103 62.685 10.951 60.796L4.873 57.154C-.531 53.916 -.531 46.084 4.873 42.846L10.951 39.204C14.103 37.315 15.647 33.586 14.754 30.022L13.032 23.149C11.5 17.038 17.038 11.5 23.149 13.032L30.022 14.754C33.586 15.647 37.315 14.103 39.204 10.951L42.846 4.873Z",
),
AppShape(
"circle",
@@ -51,7 +51,7 @@
AppShape(
"square",
"square",
- "M99.18 53.689C99.18 67.434 99.18 74.306 97.022 79.758 93.897 87.649 87.649 93.897 79.758 97.022 74.306 99.18 67.434 99.18 53.689 99.18H46.311C32.566 99.18 25.694 99.18 20.242 97.022 12.351 93.897 6.103 87.649 2.978 79.758.82 74.306.82 67.434.82 53.689L.82 46.311C.82 32.566.82 25.694 2.978 20.242 6.103 12.351 12.351 6.103 20.242 2.978 25.694.82 32.566.82 46.311.82L53.689.82C67.434.82 74.306.82 79.758 2.978 87.649 6.103 93.897 12.351 97.022 20.242 99.18 25.694 99.18 32.566 99.18 46.311V53.689Z\n",
+ "M99.18 53.689C99.18 67.434 99.18 74.306 97.022 79.758 93.897 87.649 87.649 93.897 79.758 97.022 74.306 99.18 67.434 99.18 53.689 99.18H46.311C32.566 99.18 25.694 99.18 20.242 97.022 12.351 93.897 6.103 87.649 2.978 79.758 .82 74.306 .82 67.434 .82 53.689L.82 46.311C.82 32.566 .82 25.694 2.978 20.242 6.103 12.351 12.351 6.103 20.242 2.978 25.694 .82 32.566 .82 46.311 .82L53.689 .82C67.434 .82 74.306 .82 79.758 2.978 87.649 6.103 93.897 12.351 97.022 20.242 99.18 25.694 99.18 32.566 99.18 46.311V53.689Z",
),
)
else if (Flags.newCustomizationPickerUi() && !Flags.enableLauncherIconShapes())
diff --git a/tests/multivalentTests/src/com/android/launcher3/graphics/IconShapeTest.kt b/tests/multivalentTests/src/com/android/launcher3/graphics/IconShapeTest.kt
new file mode 100644
index 0000000..311676a
--- /dev/null
+++ b/tests/multivalentTests/src/com/android/launcher3/graphics/IconShapeTest.kt
@@ -0,0 +1,218 @@
+/*
+ * Copyright (C) 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.launcher3.graphics
+
+import android.graphics.Matrix
+import android.graphics.Matrix.ScaleToFit.FILL
+import android.graphics.Path
+import android.graphics.Path.Direction
+import android.graphics.Rect
+import android.graphics.RectF
+import android.graphics.Region
+import android.platform.uiautomatorhelpers.DeviceHelpers.context
+import android.view.View
+import androidx.core.graphics.PathParser
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import androidx.test.filters.SmallTest
+import com.android.launcher3.graphics.IconShape.Circle
+import com.android.launcher3.graphics.IconShape.Companion.AREA_CALC_SIZE
+import com.android.launcher3.graphics.IconShape.Companion.AREA_DIFF_THRESHOLD
+import com.android.launcher3.graphics.IconShape.Companion.areaDiffCalculator
+import com.android.launcher3.graphics.IconShape.Companion.pickBestShape
+import com.android.launcher3.graphics.IconShape.GenericPathShape
+import com.android.launcher3.graphics.IconShape.RoundedSquare
+import com.android.launcher3.icons.GraphicsUtils
+import com.android.launcher3.views.ClipPathView
+import com.google.common.truth.Truth.assertThat
+import org.junit.Test
+import org.junit.runner.RunWith
+
+@SmallTest
+@RunWith(AndroidJUnit4::class)
+class IconShapeTest {
+
+ @Test
+ fun `areaDiffCalculator increases with outwards shape`() {
+ val diffCalculator =
+ areaDiffCalculator(
+ Path().apply {
+ addCircle(
+ AREA_CALC_SIZE / 2f,
+ AREA_CALC_SIZE / 2f,
+ AREA_CALC_SIZE / 2f,
+ Direction.CW,
+ )
+ }
+ )
+ assertThat(diffCalculator(Circle())).isLessThan(AREA_DIFF_THRESHOLD)
+ assertThat(diffCalculator(Circle())).isLessThan(diffCalculator(RoundedSquare(.9f)))
+ assertThat(diffCalculator(RoundedSquare(.9f)))
+ .isLessThan(diffCalculator(RoundedSquare(.8f)))
+ assertThat(diffCalculator(RoundedSquare(.8f)))
+ .isLessThan(diffCalculator(RoundedSquare(.7f)))
+ assertThat(diffCalculator(RoundedSquare(.7f)))
+ .isLessThan(diffCalculator(RoundedSquare(.6f)))
+ assertThat(diffCalculator(RoundedSquare(.6f)))
+ .isLessThan(diffCalculator(RoundedSquare(.5f)))
+ }
+
+ @Test
+ fun `areaDiffCalculator increases with inwards shape`() {
+ val diffCalculator = areaDiffCalculator(roundedRectPath(0.5f))
+ assertThat(diffCalculator(RoundedSquare(.5f))).isLessThan(AREA_DIFF_THRESHOLD)
+ assertThat(diffCalculator(RoundedSquare(.5f)))
+ .isLessThan(diffCalculator(RoundedSquare(.6f)))
+ assertThat(diffCalculator(RoundedSquare(.5f)))
+ .isLessThan(diffCalculator(RoundedSquare(.4f)))
+ }
+
+ @Test
+ fun `pickBestShape picks circle`() {
+ val r = AREA_CALC_SIZE / 2
+ val pathStr = "M 50 0 a 50 50 0 0 1 0 100 a 50 50 0 0 1 0 -100"
+ val path = Path().apply { addCircle(r.toFloat(), r.toFloat(), r.toFloat(), Direction.CW) }
+ assertThat(pickBestShape(path, pathStr)).isInstanceOf(Circle::class.java)
+ }
+
+ @Test
+ fun `pickBestShape picks rounded rect`() {
+ val factor = 0.5f
+ var shape = pickBestShape(roundedRectPath(factor), roundedRectString(factor))
+ assertThat(shape).isInstanceOf(RoundedSquare::class.java)
+ assertThat((shape as RoundedSquare).radiusRatio).isEqualTo(factor)
+
+ val factor2 = 0.2f
+ shape = pickBestShape(roundedRectPath(factor2), roundedRectString(factor2))
+ assertThat(shape).isInstanceOf(RoundedSquare::class.java)
+ assertThat((shape as RoundedSquare).radiusRatio).isEqualTo(factor2)
+ }
+
+ @Test
+ fun `pickBestShape picks generic shape`() {
+ val path = cookiePath(Rect(0, 0, AREA_CALC_SIZE, AREA_CALC_SIZE))
+ val pathStr = FOUR_SIDED_COOKIE
+ val shape = pickBestShape(path, pathStr)
+ assertThat(shape).isInstanceOf(GenericPathShape::class.java)
+
+ val diffCalculator = areaDiffCalculator(path)
+ assertThat(diffCalculator(shape)).isLessThan(AREA_DIFF_THRESHOLD)
+ }
+
+ @Test
+ fun `generic shape creates smooth animation`() {
+ val shape = GenericPathShape(FOUR_SIDED_COOKIE)
+ val target = TestClipView()
+ val anim =
+ shape.createRevealAnimator(
+ target,
+ Rect(0, 0, AREA_CALC_SIZE, AREA_CALC_SIZE),
+ Rect(0, 0, AREA_CALC_SIZE, AREA_CALC_SIZE),
+ AREA_CALC_SIZE * .25f,
+ false,
+ )
+
+ // Verify that the start rect is similar to initial path
+ anim.setCurrentFraction(0f)
+ assertThat(
+ getAreaDiff(
+ target.currentClip!!,
+ cookiePath(Rect(0, 0, AREA_CALC_SIZE, AREA_CALC_SIZE)),
+ )
+ )
+ .isLessThan(AREA_CALC_SIZE)
+
+ // Verify that end rect is similar to end path
+ anim.setCurrentFraction(1f)
+ assertThat(getAreaDiff(target.currentClip!!, roundedRectPath(0.5f)))
+ .isLessThan(AREA_CALC_SIZE)
+
+ // Ensure that when running animation, area increases smoothly. We run the animation over
+ // [steps] and verify increase of max 5 times the linear diff increase
+ val steps = 1000
+ val incrementalDiff =
+ getAreaDiff(
+ cookiePath(Rect(0, 0, AREA_CALC_SIZE, AREA_CALC_SIZE)),
+ roundedRectPath(0.5f),
+ ) * 5 / steps
+ var lastPath = cookiePath(Rect(0, 0, AREA_CALC_SIZE, AREA_CALC_SIZE))
+ for (progress in 1..steps) {
+ anim.setCurrentFraction(progress / 1000f)
+ val currentPath = Path(target.currentClip!!)
+ assertThat(getAreaDiff(lastPath, currentPath)).isLessThan(incrementalDiff)
+ lastPath = currentPath
+ }
+ assertThat(getAreaDiff(lastPath, roundedRectPath(0.5f))).isLessThan(AREA_CALC_SIZE)
+ }
+
+ private fun roundedRectPath(factor: Float) =
+ Path().apply {
+ val r = factor * AREA_CALC_SIZE / 2
+ addRoundRect(
+ 0f,
+ 0f,
+ AREA_CALC_SIZE.toFloat(),
+ AREA_CALC_SIZE.toFloat(),
+ r,
+ r,
+ Direction.CW,
+ )
+ }
+
+ private fun roundedRectString(factor: Float): String {
+ val s = 100f
+ val r = (factor * s / 2)
+ val t = s - r
+ return "M $r 0 " +
+ "L $t 0 " +
+ "A $r $r 0 0 1 $s $r " +
+ "L $s $t " +
+ "A $r $r 0 0 1 $t $s " +
+ "L $r $s " +
+ "A $r $r 0 0 1 0 $t " +
+ "L 0 $r " +
+ "A $r $r 0 0 1 $r 0 Z"
+ }
+
+ private fun getAreaDiff(p1: Path, p2: Path): Int {
+ val fullRegion = Region(0, 0, AREA_CALC_SIZE, AREA_CALC_SIZE)
+ val iconRegion = Region().apply { setPath(p1, fullRegion) }
+ val shapeRegion = Region().apply { setPath(p2, fullRegion) }
+ shapeRegion.op(iconRegion, Region.Op.XOR)
+ return GraphicsUtils.getArea(shapeRegion)
+ }
+
+ class TestClipView : View(context), ClipPathView {
+
+ var currentClip: Path? = null
+
+ override fun setClipPath(clipPath: Path?) {
+ currentClip = clipPath
+ }
+ }
+
+ companion object {
+ const val FOUR_SIDED_COOKIE =
+ "M63.605 3C84.733 -6.176 106.176 15.268 97 36.395L95.483 39.888C92.681 46.338 92.681 53.662 95.483 60.112L97 63.605C106.176 84.732 84.733 106.176 63.605 97L60.112 95.483C53.662 92.681 46.338 92.681 39.888 95.483L36.395 97C15.267 106.176 -6.176 84.732 3 63.605L4.517 60.112C7.319 53.662 7.319 46.338 4.517 39.888L3 36.395C -6.176 15.268 15.267 -6.176 36.395 3L39.888 4.517C46.338 7.319 53.662 7.319 60.112 4.517L63.605 3Z"
+
+ private fun cookiePath(bounds: Rect) =
+ PathParser.createPathFromPathData(FOUR_SIDED_COOKIE).apply {
+ transform(
+ Matrix().apply { setRectToRect(RectF(0f, 0f, 100f, 100f), RectF(bounds), FILL) }
+ )
+ }
+ }
+}