Skip to content

Commit 42e78c5

Browse files
committed
use targetCellWidth and height for initial widget size on Android 13+
1 parent 456e7fc commit 42e78c5

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

app/src/main/kotlin/com/simplemobiletools/launcher/extensions/Context.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.simplemobiletools.launcher.extensions
22

3+
import android.appwidget.AppWidgetProviderInfo
34
import android.content.Context
45
import android.content.pm.LauncherApps
56
import android.graphics.drawable.Drawable
67
import android.os.Process
8+
import android.util.Size
79
import com.simplemobiletools.commons.extensions.portrait
10+
import com.simplemobiletools.commons.helpers.isSPlus
811
import com.simplemobiletools.launcher.R
912
import com.simplemobiletools.launcher.databases.AppsDatabase
1013
import com.simplemobiletools.launcher.helpers.Config
@@ -47,7 +50,17 @@ fun Context.getDrawableForPackageName(packageName: String): Drawable? {
4750
return drawable
4851
}
4952

50-
fun Context.getTileCount(size: Int): Int {
53+
fun Context.getInitialCellSize(info: AppWidgetProviderInfo, fallbackWidth: Int, fallbackHeight: Int): Size {
54+
return if (isSPlus() && info.targetCellWidth != 0 && info.targetCellHeight != 0) {
55+
Size(info.targetCellWidth, info.targetCellHeight)
56+
} else {
57+
val widthCells = getCellCount(fallbackWidth)
58+
val heightCells = getCellCount(fallbackHeight)
59+
Size(widthCells, heightCells)
60+
}
61+
}
62+
63+
fun Context.getCellCount(size: Int): Int {
5164
val tiles = Math.ceil(((size / resources.displayMetrics.density) - 30) / 70.0).toInt()
5265
return Math.max(tiles, 1)
5366
}

app/src/main/kotlin/com/simplemobiletools/launcher/fragments/WidgetsFragment.kt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package com.simplemobiletools.launcher.fragments
33
import android.annotation.SuppressLint
44
import android.appwidget.AppWidgetManager
55
import android.content.Context
6-
import android.content.Intent
76
import android.content.pm.LauncherApps
8-
import android.content.pm.PackageManager
97
import android.os.Process
108
import android.util.AttributeSet
119
import android.view.MotionEvent
@@ -16,11 +14,9 @@ import com.simplemobiletools.commons.helpers.ensureBackgroundThread
1614
import com.simplemobiletools.commons.helpers.isRPlus
1715
import com.simplemobiletools.launcher.activities.MainActivity
1816
import com.simplemobiletools.launcher.adapters.WidgetsAdapter
19-
import com.simplemobiletools.launcher.extensions.getTileCount
20-
import com.simplemobiletools.launcher.helpers.COLUMN_COUNT
17+
import com.simplemobiletools.launcher.extensions.getInitialCellSize
2118
import com.simplemobiletools.launcher.helpers.ITEM_TYPE_SHORTCUT
2219
import com.simplemobiletools.launcher.helpers.ITEM_TYPE_WIDGET
23-
import com.simplemobiletools.launcher.helpers.ROW_COUNT
2420
import com.simplemobiletools.launcher.interfaces.WidgetsFragmentListener
2521
import com.simplemobiletools.launcher.models.*
2622
import kotlinx.android.synthetic.main.widgets_fragment.view.*
@@ -98,8 +94,9 @@ class WidgetsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
9894
val appIcon = appMetadata.appIcon
9995
val widgetTitle = info.loadLabel(packageManager)
10096
val widgetPreviewImage = info.loadPreviewImage(context, resources.displayMetrics.densityDpi) ?: appIcon
101-
val widthCells = Math.min(COLUMN_COUNT, context.getTileCount(info.minWidth))
102-
val heightCells = Math.min(ROW_COUNT, context.getTileCount(info.minHeight))
97+
val cellSize = context.getInitialCellSize(info, info.minWidth, info.minHeight)
98+
val widthCells = cellSize.width
99+
val heightCells = cellSize.height
103100
val className = info.provider.className
104101
val widget = AppWidget(appPackageName, appTitle, appIcon, widgetTitle, widgetPreviewImage, widthCells, heightCells, false, className, info)
105102
appWidgets.add(widget)

app/src/main/kotlin/com/simplemobiletools/launcher/views/MyAppWidgetResizeFrame.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import android.util.AttributeSet
99
import android.view.MotionEvent
1010
import android.widget.RelativeLayout
1111
import com.simplemobiletools.launcher.R
12-
import com.simplemobiletools.launcher.extensions.getTileCount
12+
import com.simplemobiletools.launcher.extensions.getCellCount
1313
import com.simplemobiletools.launcher.helpers.COLUMN_COUNT
1414
import com.simplemobiletools.launcher.helpers.MAX_CLICK_DURATION
1515
import com.simplemobiletools.launcher.helpers.ROW_COUNT
@@ -76,8 +76,8 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
7676
it.provider.className == gridItem.className
7777
} ?: return
7878

79-
minResizeWidthCells = Math.min(COLUMN_COUNT, context.getTileCount(providerInfo.minResizeWidth))
80-
minResizeHeightCells = Math.min(ROW_COUNT, context.getTileCount(providerInfo.minResizeHeight))
79+
minResizeWidthCells = Math.min(COLUMN_COUNT, context.getCellCount(providerInfo.minResizeWidth))
80+
minResizeHeightCells = Math.min(ROW_COUNT, context.getCellCount(providerInfo.minResizeHeight))
8181
redrawFrame()
8282

8383
occupiedCells.clear()

0 commit comments

Comments
 (0)