Generate folder icon from icons inside of it

This commit is contained in:
Ensar Sarajčić 2023-08-22 13:59:21 +02:00
parent a5499a58ad
commit 516f61fe90
1 changed files with 50 additions and 10 deletions

View File

@ -10,6 +10,7 @@ import android.appwidget.AppWidgetProviderInfo
import android.content.Context import android.content.Context
import android.graphics.* import android.graphics.*
import android.graphics.drawable.BitmapDrawable import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.os.Bundle import android.os.Bundle
import android.text.Layout import android.text.Layout
import android.text.StaticLayout import android.text.StaticLayout
@ -27,8 +28,7 @@ import androidx.core.view.accessibility.AccessibilityNodeInfoCompat
import androidx.customview.widget.ExploreByTouchHelper import androidx.customview.widget.ExploreByTouchHelper
import com.google.android.material.math.MathUtils import com.google.android.material.math.MathUtils
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.commons.helpers.isSPlus
import com.simplemobiletools.launcher.R import com.simplemobiletools.launcher.R
import com.simplemobiletools.launcher.activities.MainActivity import com.simplemobiletools.launcher.activities.MainActivity
import com.simplemobiletools.launcher.databinding.HomeScreenGridBinding import com.simplemobiletools.launcher.databinding.HomeScreenGridBinding
@ -65,6 +65,8 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
private var emptyPageIndicatorPaint: Paint private var emptyPageIndicatorPaint: Paint
private var currentPageIndicatorPaint: Paint private var currentPageIndicatorPaint: Paint
private var folderBackgroundPaint: Paint private var folderBackgroundPaint: Paint
private var folderIconBackgroundPaint: Paint
private var folderIconBorderPaint: Paint
private var draggedItem: HomeScreenGridItem? = null private var draggedItem: HomeScreenGridItem? = null
private var resizedWidget: HomeScreenGridItem? = null private var resizedWidget: HomeScreenGridItem? = null
private var isFirstDraw = true private var isFirstDraw = true
@ -157,6 +159,17 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
style = Paint.Style.FILL style = Paint.Style.FILL
} }
folderIconBackgroundPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
color = context.getProperBackgroundColor().adjustAlpha(MEDIUM_ALPHA)
style = Paint.Style.FILL
}
folderIconBorderPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
color = context.getProperBackgroundColor().adjustAlpha(HIGHER_ALPHA)
strokeWidth = context.resources.getDimension(R.dimen.page_indicator_stroke_width) * 5
style = Paint.Style.STROKE
}
val sideMargin = context.resources.getDimension(com.simplemobiletools.commons.R.dimen.normal_margin).toInt() val sideMargin = context.resources.getDimension(com.simplemobiletools.commons.R.dimen.normal_margin).toInt()
sideMargins.apply { sideMargins.apply {
top = context.statusBarHeight top = context.statusBarHeight
@ -181,8 +194,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
if (item.type == ITEM_TYPE_ICON) { if (item.type == ITEM_TYPE_ICON) {
item.drawable = context.getDrawableForPackageName(item.packageName) item.drawable = context.getDrawableForPackageName(item.packageName)
} else if (item.type == ITEM_TYPE_FOLDER) { } else if (item.type == ITEM_TYPE_FOLDER) {
item.drawable = item.drawable = item.generateFolderDrawable()
resources.getColoredDrawableWithColor(com.simplemobiletools.commons.R.drawable.ic_folder_vector, context.getProperPrimaryColor())
} else if (item.type == ITEM_TYPE_SHORTCUT) { } else if (item.type == ITEM_TYPE_SHORTCUT) {
if (item.icon != null) { if (item.icon != null) {
item.drawable = BitmapDrawable(item.icon) item.drawable = BitmapDrawable(item.icon)
@ -258,8 +270,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
if (draggedItem!!.drawable == null) { if (draggedItem!!.drawable == null) {
if (draggedItem?.type == ITEM_TYPE_FOLDER) { if (draggedItem?.type == ITEM_TYPE_FOLDER) {
draggedItem!!.drawable = draggedItem!!.drawable = draggedGridItem!!.generateFolderDrawable()
resources.getColoredDrawableWithColor(com.simplemobiletools.commons.R.drawable.ic_folder_vector, context.getProperPrimaryColor())
} else { } else {
draggedItem!!.drawable = context.getDrawableForPackageName(draggedGridItem.packageName) draggedItem!!.drawable = context.getDrawableForPackageName(draggedGridItem.packageName)
} }
@ -972,7 +983,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
val drawableX = baseItemX + iconMargin + extraXMargin val drawableX = baseItemX + iconMargin + extraXMargin
val drawable = if (item.type == ITEM_TYPE_FOLDER) { val drawable = if (item.type == ITEM_TYPE_FOLDER) {
resources.getColoredDrawableWithColor(com.simplemobiletools.commons.R.drawable.ic_folder_vector, context.getProperPrimaryColor()) item.generateFolderDrawable()
} else { } else {
item.drawable!! item.drawable!!
} }
@ -980,10 +991,10 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
if (item.docked) { if (item.docked) {
val drawableY = cellYCoords[rowCount - 1] + cellHeight - iconMargin - iconSize + sideMargins.top val drawableY = cellYCoords[rowCount - 1] + cellHeight - iconMargin - iconSize + sideMargins.top
drawable.setBounds(drawableX, drawableY, drawableX + iconSize, drawableY + iconSize) drawable?.setBounds(drawableX, drawableY, drawableX + iconSize, drawableY + iconSize)
} else { } else {
val drawableY = baseItemY + iconMargin + extraYMargin val drawableY = baseItemY + iconMargin + extraYMargin
drawable.setBounds(drawableX, drawableY, drawableX + iconSize, drawableY + iconSize) drawable?.setBounds(drawableX, drawableY, drawableX + iconSize, drawableY + iconSize)
if (item.id != draggedItem?.id && item.title.isNotEmpty()) { if (item.id != draggedItem?.id && item.title.isNotEmpty()) {
val textX = baseItemX.toFloat() + labelSideMargin val textX = baseItemX.toFloat() + labelSideMargin
@ -1007,7 +1018,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
} }
} }
drawable.draw(canvas) drawable?.draw(canvas)
} }
} }
@ -1494,6 +1505,35 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
private fun HomeScreenGridItem.getFolderItems() = private fun HomeScreenGridItem.getFolderItems() =
gridItems.filter { (it.drawable != null && it.type == ITEM_TYPE_ICON || it.type == ITEM_TYPE_SHORTCUT) && it.parentId == id } gridItems.filter { (it.drawable != null && it.type == ITEM_TYPE_ICON || it.type == ITEM_TYPE_SHORTCUT) && it.parentId == id }
private fun HomeScreenGridItem.generateFolderDrawable(): Drawable? {
if (iconSize == 0) {
return null
}
val bitmap = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
val circlePath = Path().apply { addCircle((iconSize / 2).toFloat(), (iconSize / 2).toFloat(), (iconSize / 2).toFloat(), Path.Direction.CCW) }
canvas.clipPath(circlePath)
canvas.drawPaint(folderIconBackgroundPaint)
val items = getFolderItems()
val itemsCount = getFolderItems().count()
val folderColumnCount = ceil(sqrt(itemsCount.toDouble())).roundToInt()
val folderRowCount = ceil(itemsCount.toFloat() / folderColumnCount).roundToInt()
val scaledCellSize = (iconSize.toFloat() / folderColumnCount)
val scaledGap = scaledCellSize / 5f
val scaledIconSize = (iconSize - (folderColumnCount + 1) * scaledGap) / folderColumnCount
val extraYMargin = if (folderRowCount < folderColumnCount) scaledIconSize / 2 else 0f
items.forEach {
val (row, column) = it.getPositionInFolder(this@generateFolderDrawable)
val drawableX = (scaledGap + column * scaledIconSize + column * scaledGap).toInt()
val drawableY = (extraYMargin + scaledGap + row * scaledIconSize + row * scaledGap).toInt()
it.drawable?.setBounds(drawableX, drawableY, drawableX + scaledIconSize.toInt(), drawableY + scaledIconSize.toInt())
it.drawable?.draw(canvas)
}
canvas.drawPath(circlePath, folderIconBorderPaint)
return BitmapDrawable(resources, bitmap)
}
private fun HomeScreenGridItem.getFolderRect(): RectF { private fun HomeScreenGridItem.getFolderRect(): RectF {
val count = getFolderItems().count() val count = getFolderItems().count()
val columnsCount = ceil(sqrt(count.toDouble())).roundToInt() val columnsCount = ceil(sqrt(count.toDouble())).roundToInt()