handle displaying config screen before placing widget, when available

This commit is contained in:
tibbi 2022-09-29 16:04:37 +02:00
parent 0bb2c1066e
commit f37840094b
2 changed files with 43 additions and 21 deletions

View file

@ -3,6 +3,7 @@ package com.simplemobiletools.launcher.activities
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.app.Activity
import android.appwidget.AppWidgetHost
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProviderInfo
import android.content.Context
@ -48,6 +49,7 @@ class MainActivity : SimpleActivity(), FlingListener {
private var mCachedLaunchers = ArrayList<AppLauncher>()
private var mLastTouchCoords = Pair(0f, 0f)
private var mActionOnCanBindWidget: ((granted: Boolean) -> Unit)? = null
private var mActionOnWidgetConfiguredWidget: ((granted: Boolean) -> Unit)? = null
private lateinit var mDetector: GestureDetectorCompat
@ -114,12 +116,14 @@ class MainActivity : SimpleActivity(), FlingListener {
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
super.onActivityResult(requestCode, resultCode, resultData)
if (requestCode == UNINSTALL_APP_REQUEST_CODE) {
ensureBackgroundThread {
refetchLaunchers()
when (requestCode) {
UNINSTALL_APP_REQUEST_CODE -> {
ensureBackgroundThread {
refetchLaunchers()
}
}
} else if (requestCode == REQUEST_ALLOW_BINDING_WIDGET) {
mActionOnCanBindWidget?.invoke(resultCode == Activity.RESULT_OK)
REQUEST_ALLOW_BINDING_WIDGET -> mActionOnCanBindWidget?.invoke(resultCode == Activity.RESULT_OK)
REQUEST_CONFIGURE_WIDGET -> mActionOnWidgetConfiguredWidget?.invoke(resultCode == Activity.RESULT_OK)
}
}
@ -500,6 +504,17 @@ class MainActivity : SimpleActivity(), FlingListener {
}
}
fun handleWidgetConfigureScreen(appWidgetHost: AppWidgetHost, appWidgetId: Int, callback: (canBind: Boolean) -> Unit) {
mActionOnWidgetConfiguredWidget = callback
appWidgetHost.startAppWidgetConfigureActivityForResult(
this,
appWidgetId,
0,
REQUEST_CONFIGURE_WIDGET,
null
)
}
// taken from https://gist.github.com/maxjvh/a6ab15cbba9c82a5065d
private fun calculateAverageColor(bitmap: Bitmap): Int {
var red = 0

View file

@ -2,6 +2,7 @@ package com.simplemobiletools.launcher.views
import android.annotation.SuppressLint
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProviderInfo
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
@ -274,23 +275,17 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
activity.handleWidgetBinding(appWidgetManager, appWidgetId, appWidgetProviderInfo) { canBind ->
if (canBind) {
if (appWidgetProviderInfo.configure != null && !isInitialDrawAfterLaunch) {
appWidgetHost.startAppWidgetConfigureActivityForResult(
context as MainActivity,
appWidgetId,
0,
REQUEST_CONFIGURE_WIDGET,
null
)
activity.handleWidgetConfigureScreen(appWidgetHost, appWidgetId) { success ->
if (success) {
placeAppWidget(appWidgetId, appWidgetProviderInfo, item)
} else if (item.id != null) {
ensureBackgroundThread {
context.homeScreenGridItemsDB.deleteById(item.id!!)
}
}
}
} else {
// we have to pass the base context here, else there will be errors with the themes
val widgetView = appWidgetHost.createView(activity.baseContext, appWidgetId, appWidgetProviderInfo) as MyAppWidgetHostView
widgetView.setAppWidget(appWidgetId, appWidgetProviderInfo)
widgetView.x = item.left * rowWidth + sideMargins.left.toFloat()
widgetView.y = item.top * rowHeight + sideMargins.top.toFloat()
val widgetWidth = item.widthCells * rowWidth
val widgetHeight = item.heightCells * rowHeight
addView(widgetView, widgetWidth, widgetHeight)
placeAppWidget(appWidgetId, appWidgetProviderInfo, item)
}
} else if (item.id != null) {
ensureBackgroundThread {
@ -301,6 +296,18 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
}
}
private fun placeAppWidget(appWidgetId: Int, appWidgetProviderInfo: AppWidgetProviderInfo, item: HomeScreenGridItem) {
// we have to pass the base context here, else there will be errors with the themes
val widgetView = appWidgetHost.createView((context as MainActivity).baseContext, appWidgetId, appWidgetProviderInfo) as MyAppWidgetHostView
widgetView.setAppWidget(appWidgetId, appWidgetProviderInfo)
widgetView.x = item.left * rowWidth + sideMargins.left.toFloat()
widgetView.y = item.top * rowHeight + sideMargins.top.toFloat()
val widgetWidth = item.widthCells * rowWidth
val widgetHeight = item.heightCells * rowHeight
addView(widgetView, widgetWidth, widgetHeight)
}
// convert stuff like 102x192 to grid cells like 0x1
private fun getClosestGridCells(center: Pair<Int, Int>): Pair<Int, Int>? {
rowXCoords.forEachIndexed { xIndex, xCell ->