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

View File

@ -2,6 +2,7 @@ package com.simplemobiletools.launcher.views
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.appwidget.AppWidgetManager import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProviderInfo
import android.content.Context import android.content.Context
import android.graphics.Canvas import android.graphics.Canvas
import android.graphics.Color import android.graphics.Color
@ -274,23 +275,17 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
activity.handleWidgetBinding(appWidgetManager, appWidgetId, appWidgetProviderInfo) { canBind -> activity.handleWidgetBinding(appWidgetManager, appWidgetId, appWidgetProviderInfo) { canBind ->
if (canBind) { if (canBind) {
if (appWidgetProviderInfo.configure != null && !isInitialDrawAfterLaunch) { if (appWidgetProviderInfo.configure != null && !isInitialDrawAfterLaunch) {
appWidgetHost.startAppWidgetConfigureActivityForResult( activity.handleWidgetConfigureScreen(appWidgetHost, appWidgetId) { success ->
context as MainActivity, if (success) {
appWidgetId, placeAppWidget(appWidgetId, appWidgetProviderInfo, item)
0, } else if (item.id != null) {
REQUEST_CONFIGURE_WIDGET, ensureBackgroundThread {
null context.homeScreenGridItemsDB.deleteById(item.id!!)
) }
}
}
} else { } else {
// we have to pass the base context here, else there will be errors with the themes placeAppWidget(appWidgetId, appWidgetProviderInfo, item)
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)
} }
} else if (item.id != null) { } else if (item.id != null) {
ensureBackgroundThread { 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 // convert stuff like 102x192 to grid cells like 0x1
private fun getClosestGridCells(center: Pair<Int, Int>): Pair<Int, Int>? { private fun getClosestGridCells(center: Pair<Int, Int>): Pair<Int, Int>? {
rowXCoords.forEachIndexed { xIndex, xCell -> rowXCoords.forEachIndexed { xIndex, xCell ->