|
| 1 | +package com.dede.android_eggs.alterable_adaptive_icon |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.graphics.drawable.AdaptiveIconDrawable |
| 5 | +import android.graphics.drawable.Drawable |
| 6 | +import android.os.Build |
| 7 | +import androidx.annotation.DrawableRes |
| 8 | +import androidx.compose.foundation.Image |
| 9 | +import androidx.compose.foundation.background |
| 10 | +import androidx.compose.foundation.layout.Arrangement |
| 11 | +import androidx.compose.foundation.layout.Box |
| 12 | +import androidx.compose.foundation.layout.Column |
| 13 | +import androidx.compose.foundation.layout.aspectRatio |
| 14 | +import androidx.compose.foundation.layout.fillMaxSize |
| 15 | +import androidx.compose.foundation.layout.size |
| 16 | +import androidx.compose.foundation.layout.wrapContentSize |
| 17 | +import androidx.compose.runtime.Composable |
| 18 | +import androidx.compose.runtime.remember |
| 19 | +import androidx.compose.ui.Alignment |
| 20 | +import androidx.compose.ui.Modifier |
| 21 | +import androidx.compose.ui.draw.clip |
| 22 | +import androidx.compose.ui.draw.drawWithContent |
| 23 | +import androidx.compose.ui.graphics.Color |
| 24 | +import androidx.compose.ui.graphics.Matrix |
| 25 | +import androidx.compose.ui.graphics.Path |
| 26 | +import androidx.compose.ui.graphics.asComposePath |
| 27 | +import androidx.compose.ui.graphics.drawscope.withTransform |
| 28 | +import androidx.compose.ui.layout.layout |
| 29 | +import androidx.compose.ui.platform.LocalContext |
| 30 | +import androidx.compose.ui.tooling.preview.Preview |
| 31 | +import androidx.compose.ui.unit.Constraints |
| 32 | +import androidx.compose.ui.unit.dp |
| 33 | +import androidx.compose.ui.util.fastRoundToInt |
| 34 | +import com.dede.android_eggs.util.PathInflater |
| 35 | +import com.dede.android_eggs.views.settings.compose.prefs.IconShapePrefUtil |
| 36 | +import com.dede.basic.DefType |
| 37 | +import com.dede.basic.getIdentifier |
| 38 | +import com.dede.basic.requireDrawable |
| 39 | +import com.google.accompanist.drawablepainter.rememberDrawablePainter |
| 40 | + |
| 41 | + |
| 42 | +private const val ADAPTIVE_ICON_INSET_FACTOR = 1 / 4f |
| 43 | +private const val DEFAULT_CHILD_SCALE = 1 + 2 * ADAPTIVE_ICON_INSET_FACTOR |
| 44 | + |
| 45 | +private const val BACKGROUND_ID = 0 |
| 46 | +private const val FOREGROUND_ID = 1 |
| 47 | + |
| 48 | +private class ChildDrawable( |
| 49 | + val drawable: Drawable, |
| 50 | + val id: Int |
| 51 | +) |
| 52 | + |
| 53 | +private fun Array<ChildDrawable>.getBackground(): Drawable { |
| 54 | + return checkNotNull(this.find { it.id == BACKGROUND_ID }).drawable |
| 55 | +} |
| 56 | + |
| 57 | +private fun Array<ChildDrawable>.getForeground(): Drawable? { |
| 58 | + return this.find { it.id == FOREGROUND_ID }?.drawable |
| 59 | +} |
| 60 | + |
| 61 | +private fun buildChildDrawableArray(context: Context, @DrawableRes res: Int): Array<ChildDrawable> { |
| 62 | + val drawable = context.requireDrawable(res) |
| 63 | + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && drawable is AdaptiveIconDrawable) { |
| 64 | + arrayOf( |
| 65 | + ChildDrawable(drawable.background, BACKGROUND_ID), |
| 66 | + ChildDrawable(drawable.foreground, FOREGROUND_ID), |
| 67 | + ) |
| 68 | + } else { |
| 69 | + arrayOf( |
| 70 | + ChildDrawable(drawable, BACKGROUND_ID), |
| 71 | + ) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +@Preview(showBackground = true) |
| 76 | +@Composable |
| 77 | +private fun PreviewAlterableAdaptiveIcon() { |
| 78 | + Column( |
| 79 | + modifier = Modifier |
| 80 | + .background(Color.White) |
| 81 | + .fillMaxSize(), |
| 82 | + horizontalAlignment = Alignment.CenterHorizontally, |
| 83 | + verticalArrangement = Arrangement.spacedBy(10.dp, Alignment.CenterVertically), |
| 84 | + ) { |
| 85 | + val context = LocalContext.current |
| 86 | + val id = context.getIdentifier("ic_launcher", DefType.MIPMAP) |
| 87 | + val maskPathStr = IconShapePrefUtil.getMaskPath(context) |
| 88 | + val composePath = PathInflater.inflate(maskPathStr).asComposePath() |
| 89 | + AlterableAdaptiveIcon( |
| 90 | + modifier = Modifier.size(100.dp), |
| 91 | + res = id, |
| 92 | + maskPath = composePath |
| 93 | + ) |
| 94 | + |
| 95 | + Image( |
| 96 | + painter = rememberDrawablePainter(context.requireDrawable(id)), |
| 97 | + contentDescription = null, |
| 98 | + modifier = Modifier.size(100.dp), |
| 99 | + ) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +private fun Modifier.adaptiveIconChild(scale: Float = DEFAULT_CHILD_SCALE) = this then Modifier |
| 104 | + .layout { measurable, constraints -> |
| 105 | + val width: Int = if (constraints.hasBoundedWidth) { |
| 106 | + (constraints.maxWidth * scale).fastRoundToInt() |
| 107 | + } else { |
| 108 | + constraints.maxWidth |
| 109 | + } |
| 110 | + val height: Int = if (constraints.hasBoundedHeight) { |
| 111 | + (constraints.maxHeight * scale).fastRoundToInt() |
| 112 | + } else { |
| 113 | + constraints.maxHeight |
| 114 | + } |
| 115 | + |
| 116 | + val placeable = measurable.measure(Constraints(width, width, height, height)) |
| 117 | + layout(width, height) { |
| 118 | + placeable.placeRelative(0, 0) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | +@Composable |
| 123 | +fun AlterableAdaptiveIcon( |
| 124 | + modifier: Modifier = Modifier, |
| 125 | + maskPath: Path, |
| 126 | + @DrawableRes res: Int, |
| 127 | + foregroundMatrix: Matrix = Matrix(), |
| 128 | +) { |
| 129 | + val context = LocalContext.current |
| 130 | + val childDrawables = remember(res, context.theme) { |
| 131 | + buildChildDrawableArray(context, res) |
| 132 | + } |
| 133 | + val clipShape = remember(maskPath) { |
| 134 | + PathShape(maskPath) |
| 135 | + } |
| 136 | + val isAdaptiveIcon = childDrawables.size >= 2 |
| 137 | + Box( |
| 138 | + modifier = Modifier |
| 139 | + .then(modifier) |
| 140 | + .clip(clipShape) |
| 141 | + .aspectRatio(1f), |
| 142 | + contentAlignment = Alignment.Center, |
| 143 | + ) { |
| 144 | + val background: Drawable = childDrawables.getBackground() |
| 145 | + if (!isAdaptiveIcon) { |
| 146 | + Image( |
| 147 | + painter = rememberDrawablePainter(background), |
| 148 | + modifier = Modifier.wrapContentSize(), |
| 149 | + contentDescription = null, |
| 150 | + ) |
| 151 | + return@Box |
| 152 | + } |
| 153 | + |
| 154 | + Image( |
| 155 | + painter = rememberDrawablePainter(background), |
| 156 | + modifier = Modifier.adaptiveIconChild(), |
| 157 | + contentDescription = null, |
| 158 | + ) |
| 159 | + |
| 160 | + val foreground: Drawable? = childDrawables.getForeground() |
| 161 | + if (foreground != null) { |
| 162 | + Image( |
| 163 | + painter = rememberDrawablePainter(foreground), |
| 164 | + modifier = Modifier |
| 165 | + .drawWithContent { |
| 166 | + withTransform({ |
| 167 | + transform(foregroundMatrix) |
| 168 | + }) { |
| 169 | + this@drawWithContent.drawContent() |
| 170 | + } |
| 171 | + } |
| 172 | + .adaptiveIconChild(), |
| 173 | + contentDescription = null, |
| 174 | + ) |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments