From 42e78c550cb0a706572f3e914e1b40a3a75bc21f Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 7 Oct 2022 09:30:57 +0200 Subject: [PATCH] use targetCellWidth and height for initial widget size on Android 13+ --- .../launcher/extensions/Context.kt | 15 ++++++++++++++- .../launcher/fragments/WidgetsFragment.kt | 11 ++++------- .../launcher/views/MyAppWidgetResizeFrame.kt | 6 +++--- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/extensions/Context.kt index 92bd4c7..df03240 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/extensions/Context.kt @@ -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) } diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/WidgetsFragment.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/WidgetsFragment.kt index c6e328e..d42f5be 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/WidgetsFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/WidgetsFragment.kt @@ -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) diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/views/MyAppWidgetResizeFrame.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/views/MyAppWidgetResizeFrame.kt index 2c11982..40f3f31 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/views/MyAppWidgetResizeFrame.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/views/MyAppWidgetResizeFrame.kt @@ -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()