reworking some widget size handling

This commit is contained in:
tibbi 2022-10-05 10:08:08 +02:00
parent e69dc7438c
commit a8755b566e
5 changed files with 60 additions and 41 deletions

View File

@ -497,7 +497,7 @@ class MainActivity : SimpleActivity(), FlingListener {
try {
val defaultDialerPackage = (getSystemService(Context.TELECOM_SERVICE) as TelecomManager).defaultDialerPackage
appLaunchers.firstOrNull { it.packageName == defaultDialerPackage }?.apply {
val dialerIcon = HomeScreenGridItem(null, 0, ROW_COUNT - 1, 1, ROW_COUNT, 1, 1, defaultDialerPackage, title, ITEM_TYPE_ICON, "", -1, null, null)
val dialerIcon = HomeScreenGridItem(null, 0, ROW_COUNT - 1, 1, ROW_COUNT, defaultDialerPackage, title, ITEM_TYPE_ICON, "", -1)
homeScreenGridItems.add(dialerIcon)
}
} catch (e: Exception) {
@ -507,7 +507,7 @@ class MainActivity : SimpleActivity(), FlingListener {
val defaultSMSMessengerPackage = Telephony.Sms.getDefaultSmsPackage(this)
appLaunchers.firstOrNull { it.packageName == defaultSMSMessengerPackage }?.apply {
val SMSMessengerIcon =
HomeScreenGridItem(null, 1, ROW_COUNT - 1, 2, ROW_COUNT, 1, 1, defaultSMSMessengerPackage, title, ITEM_TYPE_ICON, "", -1, null, null)
HomeScreenGridItem(null, 1, ROW_COUNT - 1, 2, ROW_COUNT, defaultSMSMessengerPackage, title, ITEM_TYPE_ICON, "", -1)
homeScreenGridItems.add(SMSMessengerIcon)
}
} catch (e: Exception) {
@ -519,7 +519,7 @@ class MainActivity : SimpleActivity(), FlingListener {
val defaultBrowserPackage = resolveInfo!!.activityInfo.packageName
appLaunchers.firstOrNull { it.packageName == defaultBrowserPackage }?.apply {
val browserIcon =
HomeScreenGridItem(null, 2, ROW_COUNT - 1, 3, ROW_COUNT, 1, 1, defaultBrowserPackage, title, ITEM_TYPE_ICON, "", -1, null, null)
HomeScreenGridItem(null, 2, ROW_COUNT - 1, 3, ROW_COUNT, defaultBrowserPackage, title, ITEM_TYPE_ICON, "", -1)
homeScreenGridItems.add(browserIcon)
}
} catch (e: Exception) {
@ -530,7 +530,7 @@ class MainActivity : SimpleActivity(), FlingListener {
val storePackage = potentialStores.firstOrNull { isPackageInstalled(it) && appLaunchers.map { it.packageName }.contains(it) }
if (storePackage != null) {
appLaunchers.firstOrNull { it.packageName == storePackage }?.apply {
val storeIcon = HomeScreenGridItem(null, 3, ROW_COUNT - 1, 4, ROW_COUNT, 1, 1, storePackage, title, ITEM_TYPE_ICON, "", -1, null, null)
val storeIcon = HomeScreenGridItem(null, 3, ROW_COUNT - 1, 4, ROW_COUNT, storePackage, title, ITEM_TYPE_ICON, "", -1)
homeScreenGridItems.add(storeIcon)
}
}
@ -542,7 +542,7 @@ class MainActivity : SimpleActivity(), FlingListener {
val resolveInfo = packageManager.resolveActivity(cameraIntent, PackageManager.MATCH_DEFAULT_ONLY)
val defaultCameraPackage = resolveInfo!!.activityInfo.packageName
appLaunchers.firstOrNull { it.packageName == defaultCameraPackage }?.apply {
val cameraIcon = HomeScreenGridItem(null, 4, ROW_COUNT - 1, 5, ROW_COUNT, 1, 1, defaultCameraPackage, title, ITEM_TYPE_ICON, "", -1, null, null)
val cameraIcon = HomeScreenGridItem(null, 4, ROW_COUNT - 1, 5, ROW_COUNT, defaultCameraPackage, title, ITEM_TYPE_ICON, "", -1)
homeScreenGridItems.add(cameraIcon)
}
} catch (e: Exception) {

View File

@ -230,15 +230,15 @@ class WidgetsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
-1,
-1,
-1,
appWidget.widthCells,
appWidget.heightCells,
appWidget.appPackageName,
"",
type,
appWidget.className,
-1,
appWidget.widgetPreviewImage,
appWidget.providerInfo
appWidget.providerInfo,
appWidget.widthCells,
appWidget.heightCells
)
activity?.widgetLongPressedOnList(gridItem)

View File

@ -13,16 +13,28 @@ data class HomeScreenGridItem(
@ColumnInfo(name = "top") var top: Int,
@ColumnInfo(name = "right") var right: Int,
@ColumnInfo(name = "bottom") var bottom: Int,
@ColumnInfo(name = "width_cells") var widthCells: Int,
@ColumnInfo(name = "height_cells") var heightCells: Int,
@ColumnInfo(name = "package_name") var packageName: String,
@ColumnInfo(name = "title") var title: String,
@ColumnInfo(name = "type") var type: Int,
@ColumnInfo(name = "class_name") var className: String,
@ColumnInfo(name = "widget_id") var widgetId: Int,
@Ignore var drawable: Drawable?,
@Ignore var providerInfo: AppWidgetProviderInfo?
@Ignore var drawable: Drawable? = null,
@Ignore var providerInfo: AppWidgetProviderInfo? = null,
@Ignore var widthCells: Int = 1,
@Ignore var heightCells: Int = 1
) {
constructor() : this(null, -1, -1, -1, -1, 1, 1, "", "", ITEM_TYPE_ICON, "", -1, null, null)
constructor() : this(null, -1, -1, -1, -1, "", "", ITEM_TYPE_ICON, "", -1, null, null, 1, 1)
fun getWidthInCells() = if (right == -1 || left == -1) {
widthCells
} else {
right - left + 1
}
fun getHeightInCells() = if (bottom == -1 || top == -1) {
heightCells
} else {
bottom - top + 1
}
}

View File

@ -17,6 +17,7 @@ import androidx.core.graphics.drawable.toDrawable
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.helpers.isSPlus
import com.simplemobiletools.commons.helpers.mydebug
import com.simplemobiletools.launcher.R
import com.simplemobiletools.launcher.activities.MainActivity
import com.simplemobiletools.launcher.extensions.getDrawableForPackageName
@ -189,7 +190,15 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
}
resize_frame.onResizeListener = { cellsRect ->
mydebug("resized to $cellsRect")
val minWidth = (cellsRect.width() + 1) * cellWidth
val minHeight = (cellsRect.height() + 1) * cellHeight
widgetView.updateAppWidgetSize(Bundle(), minWidth, minHeight, minWidth, minHeight)
widgetView.layoutParams.width = minWidth
widgetView.layoutParams.height = minHeight
ensureBackgroundThread {
context.homeScreenGridItemsDB.updateItemPosition(cellsRect.left, cellsRect.top, cellsRect.right, cellsRect.bottom, item.id!!)
}
}
widgetView.ignoreTouches = true
@ -264,8 +273,6 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
yIndex,
xIndex + 1,
yIndex + 1,
1,
1,
draggedItem!!.packageName,
draggedItem!!.title,
draggedItem!!.type,
@ -420,8 +427,8 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
widgetView.x = calculateWidgetX(item.left)
widgetView.y = calculateWidgetY(item.top)
val widgetWidth = item.widthCells * cellWidth
val widgetHeight = item.heightCells * cellHeight
val widgetWidth = item.getWidthInCells() * cellWidth
val widgetHeight = item.getHeightInCells() * cellHeight
// set initial sizes to avoid some glitches
if (isSPlus()) {
@ -566,8 +573,8 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
val widgetRect = getWidgetOccupiedRect(gridCells)
val leftSide = widgetRect.left * cellWidth + sideMargins.left + iconMargin.toFloat()
val topSide = widgetRect.top * cellHeight + sideMargins.top + iconMargin.toFloat()
val rightSide = leftSide + draggedItem!!.widthCells * cellWidth - sideMargins.right - iconMargin.toFloat()
val bottomSide = topSide + draggedItem!!.heightCells * cellHeight - sideMargins.top
val rightSide = leftSide + draggedItem!!.getWidthInCells() * cellWidth - sideMargins.right - iconMargin.toFloat()
val bottomSide = topSide + draggedItem!!.getHeightInCells() * cellHeight - sideMargins.top
canvas.drawRoundRect(leftSide, topSide, rightSide, bottomSide, roundedCornerRadius, roundedCornerRadius, dragShadowCirclePaint)
}
@ -576,7 +583,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
val aspectRatio = drawable.minimumHeight / drawable.minimumWidth.toFloat()
val drawableX = (draggedItemCurrentCoords.first - drawable.minimumWidth / 2f).toInt()
val drawableY = (draggedItemCurrentCoords.second - drawable.minimumHeight / 3f).toInt()
val drawableWidth = draggedItem!!.widthCells * cellWidth - iconMargin * (draggedItem!!.widthCells - 1)
val drawableWidth = draggedItem!!.getWidthInCells() * cellWidth - iconMargin * (draggedItem!!.getWidthInCells() - 1)
drawable.setBounds(
drawableX,
drawableY,
@ -612,8 +619,8 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
// drag the center of the widget, not the top left corner
private fun getWidgetOccupiedRect(item: Pair<Int, Int>): Rect {
val left = item.first - Math.floor((draggedItem!!.widthCells - 1) / 2.0).toInt()
val rect = Rect(left, item.second, left + draggedItem!!.widthCells, item.second + draggedItem!!.heightCells)
val left = item.first - Math.floor((draggedItem!!.getWidthInCells() - 1) / 2.0).toInt()
val rect = Rect(left, item.second, left + draggedItem!!.getWidthInCells(), item.second + draggedItem!!.getHeightInCells())
if (rect.left < 0) {
rect.right -= rect.left
rect.left = 0
@ -643,8 +650,8 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
} else if (gridItem.type == ITEM_TYPE_WIDGET) {
val left = calculateWidgetX(gridItem.left)
val top = calculateWidgetY(gridItem.top)
val right = left + gridItem.widthCells * cellWidth
val bottom = top + gridItem.heightCells * cellHeight
val right = left + gridItem.getWidthInCells() * cellWidth
val bottom = top + gridItem.getHeightInCells() * cellHeight
if (x >= left && x <= right && y >= top && y <= bottom) {
return gridItem

View File

@ -137,8 +137,8 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
val closestCellX = roundToClosestMultiplyOfNumber(wantedLeft - sideMargins.left, cellWidth) / cellWidth
var areAllCellsFree = true
for (xCell in closestCellX until cellsRect.right) {
for (yCell in cellsRect.top until cellsRect.bottom) {
for (xCell in closestCellX..cellsRect.right) {
for (yCell in cellsRect.top..cellsRect.bottom) {
if (occupiedCells.contains(Pair(xCell, yCell))) {
areAllCellsFree = false
}
@ -162,8 +162,8 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
val closestCellY = roundToClosestMultiplyOfNumber(wantedTop - sideMargins.top, cellHeight) / cellHeight
var areAllCellsFree = true
for (xCell in cellsRect.left until cellsRect.right) {
for (yCell in closestCellY until cellsRect.bottom) {
for (xCell in cellsRect.left..cellsRect.right) {
for (yCell in closestCellY..cellsRect.bottom) {
if (occupiedCells.contains(Pair(xCell, yCell))) {
areAllCellsFree = false
}
@ -187,8 +187,8 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
val closestCellX = roundToClosestMultiplyOfNumber(wantedRight - sideMargins.left, cellWidth) / cellWidth - 1
var areAllCellsFree = true
for (xCell in cellsRect.left until closestCellX + 1) {
for (yCell in cellsRect.top until cellsRect.bottom) {
for (xCell in cellsRect.left..closestCellX + 1) {
for (yCell in cellsRect.top..cellsRect.bottom) {
if (occupiedCells.contains(Pair(xCell, yCell))) {
areAllCellsFree = false
}
@ -212,8 +212,8 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
val closestCellY = roundToClosestMultiplyOfNumber(wantedBottom - sideMargins.top, cellHeight) / cellHeight - 1
var areAllCellsFree = true
for (xCell in cellsRect.left until cellsRect.right) {
for (yCell in cellsRect.top until closestCellY + 1) {
for (xCell in cellsRect.left..cellsRect.right) {
for (yCell in cellsRect.top..closestCellY + 1) {
if (occupiedCells.contains(Pair(xCell, yCell))) {
areAllCellsFree = false
}
@ -245,8 +245,8 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
val wantedLeft = roundToClosestMultiplyOfNumber(frameRect.left - sideMargins.left, cellWidth)
val wantedLeftCellX = wantedLeft / cellWidth
var areAllCellsFree = true
for (xCell in wantedLeftCellX until cellsRect.right) {
for (yCell in cellsRect.top until cellsRect.bottom) {
for (xCell in wantedLeftCellX..cellsRect.right) {
for (yCell in cellsRect.top..cellsRect.bottom) {
if (occupiedCells.contains(Pair(xCell, yCell))) {
areAllCellsFree = false
}
@ -264,8 +264,8 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
val wantedTop = roundToClosestMultiplyOfNumber(frameRect.top - sideMargins.top, cellHeight)
val wantedTopCellY = wantedTop / cellHeight
var areAllCellsFree = true
for (xCell in cellsRect.left until cellsRect.right) {
for (yCell in wantedTopCellY until cellsRect.bottom) {
for (xCell in cellsRect.left..cellsRect.right) {
for (yCell in wantedTopCellY..cellsRect.bottom) {
if (occupiedCells.contains(Pair(xCell, yCell))) {
areAllCellsFree = false
}
@ -283,8 +283,8 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
val wantedRight = roundToClosestMultiplyOfNumber(frameRect.right - sideMargins.left, cellWidth)
val wantedRightCellX = wantedRight / cellWidth - 1
var areAllCellsFree = true
for (xCell in cellsRect.left until wantedRightCellX + 1) {
for (yCell in cellsRect.top until cellsRect.bottom) {
for (xCell in cellsRect.left..wantedRightCellX + 1) {
for (yCell in cellsRect.top..cellsRect.bottom) {
if (occupiedCells.contains(Pair(xCell, yCell))) {
areAllCellsFree = false
}
@ -302,8 +302,8 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
val wantedBottom = roundToClosestMultiplyOfNumber(frameRect.bottom - sideMargins.top, cellHeight)
val wantedBottomCellY = wantedBottom / cellHeight - 1
var areAllCellsFree = true
for (xCell in cellsRect.left until cellsRect.right) {
for (yCell in cellsRect.top until wantedBottomCellY + 1) {
for (xCell in cellsRect.left..cellsRect.right) {
for (yCell in cellsRect.top..wantedBottomCellY + 1) {
if (occupiedCells.contains(Pair(xCell, yCell))) {
areAllCellsFree = false
}