use targetCellWidth and height for initial widget size on Android 13+

This commit is contained in:
tibbi 2022-10-07 09:30:57 +02:00
parent 456e7fcd05
commit 42e78c550c
3 changed files with 21 additions and 11 deletions

View File

@ -1,10 +1,13 @@
package com.simplemobiletools.launcher.extensions
import android.appwidget.AppWidgetProviderInfo
import android.content.Context
import android.content.pm.LauncherApps
import android.graphics.drawable.Drawable
import android.os.Process
import android.util.Size
import com.simplemobiletools.commons.extensions.portrait
import com.simplemobiletools.commons.helpers.isSPlus
import com.simplemobiletools.launcher.R
import com.simplemobiletools.launcher.databases.AppsDatabase
import com.simplemobiletools.launcher.helpers.Config
@ -47,7 +50,17 @@ fun Context.getDrawableForPackageName(packageName: String): Drawable? {
return drawable
}
fun Context.getTileCount(size: Int): Int {
fun Context.getInitialCellSize(info: AppWidgetProviderInfo, fallbackWidth: Int, fallbackHeight: Int): Size {
return if (isSPlus() && info.targetCellWidth != 0 && info.targetCellHeight != 0) {
Size(info.targetCellWidth, info.targetCellHeight)
} else {
val widthCells = getCellCount(fallbackWidth)
val heightCells = getCellCount(fallbackHeight)
Size(widthCells, heightCells)
}
}
fun Context.getCellCount(size: Int): Int {
val tiles = Math.ceil(((size / resources.displayMetrics.density) - 30) / 70.0).toInt()
return Math.max(tiles, 1)
}

View File

@ -3,9 +3,7 @@ package com.simplemobiletools.launcher.fragments
import android.annotation.SuppressLint
import android.appwidget.AppWidgetManager
import android.content.Context
import android.content.Intent
import android.content.pm.LauncherApps
import android.content.pm.PackageManager
import android.os.Process
import android.util.AttributeSet
import android.view.MotionEvent
@ -16,11 +14,9 @@ import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.helpers.isRPlus
import com.simplemobiletools.launcher.activities.MainActivity
import com.simplemobiletools.launcher.adapters.WidgetsAdapter
import com.simplemobiletools.launcher.extensions.getTileCount
import com.simplemobiletools.launcher.helpers.COLUMN_COUNT
import com.simplemobiletools.launcher.extensions.getInitialCellSize
import com.simplemobiletools.launcher.helpers.ITEM_TYPE_SHORTCUT
import com.simplemobiletools.launcher.helpers.ITEM_TYPE_WIDGET
import com.simplemobiletools.launcher.helpers.ROW_COUNT
import com.simplemobiletools.launcher.interfaces.WidgetsFragmentListener
import com.simplemobiletools.launcher.models.*
import kotlinx.android.synthetic.main.widgets_fragment.view.*
@ -98,8 +94,9 @@ class WidgetsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
val appIcon = appMetadata.appIcon
val widgetTitle = info.loadLabel(packageManager)
val widgetPreviewImage = info.loadPreviewImage(context, resources.displayMetrics.densityDpi) ?: appIcon
val widthCells = Math.min(COLUMN_COUNT, context.getTileCount(info.minWidth))
val heightCells = Math.min(ROW_COUNT, context.getTileCount(info.minHeight))
val cellSize = context.getInitialCellSize(info, info.minWidth, info.minHeight)
val widthCells = cellSize.width
val heightCells = cellSize.height
val className = info.provider.className
val widget = AppWidget(appPackageName, appTitle, appIcon, widgetTitle, widgetPreviewImage, widthCells, heightCells, false, className, info)
appWidgets.add(widget)

View File

@ -9,7 +9,7 @@ import android.util.AttributeSet
import android.view.MotionEvent
import android.widget.RelativeLayout
import com.simplemobiletools.launcher.R
import com.simplemobiletools.launcher.extensions.getTileCount
import com.simplemobiletools.launcher.extensions.getCellCount
import com.simplemobiletools.launcher.helpers.COLUMN_COUNT
import com.simplemobiletools.launcher.helpers.MAX_CLICK_DURATION
import com.simplemobiletools.launcher.helpers.ROW_COUNT
@ -76,8 +76,8 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
it.provider.className == gridItem.className
} ?: return
minResizeWidthCells = Math.min(COLUMN_COUNT, context.getTileCount(providerInfo.minResizeWidth))
minResizeHeightCells = Math.min(ROW_COUNT, context.getTileCount(providerInfo.minResizeHeight))
minResizeWidthCells = Math.min(COLUMN_COUNT, context.getCellCount(providerInfo.minResizeWidth))
minResizeHeightCells = Math.min(ROW_COUNT, context.getCellCount(providerInfo.minResizeHeight))
redrawFrame()
occupiedCells.clear()