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 cf9afe2..92bd4c7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/extensions/Context.kt @@ -46,3 +46,8 @@ fun Context.getDrawableForPackageName(packageName: String): Drawable? { return drawable } + +fun Context.getTileCount(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 b0fa78e..bf2bccc 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/WidgetsFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/WidgetsFragment.kt @@ -16,6 +16,7 @@ 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.helpers.ITEM_TYPE_SHORTCUT import com.simplemobiletools.launcher.helpers.ITEM_TYPE_WIDGET @@ -93,8 +94,8 @@ 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, getTileCount(info.minWidth)) - val heightCells = Math.min(ROW_COUNT, getTileCount(info.minHeight)) + val widthCells = Math.min(COLUMN_COUNT, context.getTileCount(info.minWidth)) + val heightCells = Math.min(ROW_COUNT, context.getTileCount(info.minHeight)) val className = info.provider.className val widget = AppWidget(appPackageName, appTitle, appIcon, widgetTitle, widgetPreviewImage, widthCells, heightCells, false, className, info) appWidgets.add(widget) @@ -120,11 +121,6 @@ class WidgetsFragment(context: Context, attributeSet: AttributeSet) : MyFragment } } - private fun getTileCount(size: Int): Int { - val tiles = Math.ceil(((size / resources.displayMetrics.density) - 30) / 70.0).toInt() - return Math.max(tiles, 1) - } - private fun splitWidgetsByApps(appWidgets: ArrayList) { var currentAppPackageName = "" val widgetListItems = ArrayList() diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt index 07772a0..90ac07d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt @@ -177,7 +177,10 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel val widgetView = widgetViews.firstOrNull { it.tag == resizedWidget!!.widgetId } resize_frame.beGone() if (widgetView != null) { - val appWidgetProviderInfo = item.providerInfo ?: appWidgetManager!!.installedProviders.firstOrNull { it.provider.className == item.className } + val appWidgetProviderInfo = item.providerInfo ?: appWidgetManager!!.installedProviders.firstOrNull { + it.provider.className == item.className + } ?: return + val viewX = widgetView.x.toInt() val viewY = widgetView.y.toInt() val frameRect = Rect(viewX, viewY, viewX + widgetView.width, viewY + widgetView.height) 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 32618ba..999729f 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,10 @@ 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.helpers.COLUMN_COUNT import com.simplemobiletools.launcher.helpers.MAX_CLICK_DURATION +import com.simplemobiletools.launcher.helpers.ROW_COUNT @SuppressLint("ViewConstructor") class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: Int) : RelativeLayout(context, attrs, defStyle) { @@ -22,6 +25,8 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In private var frameRect = Rect(0, 0, 0, 0) private var cellWidth = 0 private var cellHeight = 0 + private var minResizeWidthCells = 1 + private var minResizeHeightCells = 1 private var providerInfo: AppWidgetProviderInfo? = null private var sideMargins = Rect() private val lineDotRadius = context.resources.getDimension(R.dimen.resize_frame_dot_radius) @@ -49,12 +54,14 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In } } - fun updateFrameCoords(coords: Rect, cellWidth: Int, cellHeight: Int, sideMargins: Rect, providerInfo: AppWidgetProviderInfo?) { + fun updateFrameCoords(coords: Rect, cellWidth: Int, cellHeight: Int, sideMargins: Rect, providerInfo: AppWidgetProviderInfo) { frameRect = coords this.cellWidth = cellWidth this.cellHeight = cellHeight this.sideMargins = sideMargins this.providerInfo = providerInfo + minResizeWidthCells = Math.min(COLUMN_COUNT, context.getTileCount(providerInfo.minResizeWidth)) + minResizeHeightCells = Math.min(ROW_COUNT, context.getTileCount(providerInfo.minResizeHeight)) redrawFrame() }