From f37840094bfffd76980d541cfbfa3e2ae04c0cec Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 29 Sep 2022 16:04:37 +0200 Subject: [PATCH] handle displaying config screen before placing widget, when available --- .../launcher/activities/MainActivity.kt | 25 +++++++++--- .../launcher/views/HomeScreenGrid.kt | 39 +++++++++++-------- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt index 269185a..e937fde 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt @@ -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() 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 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 d43c692..729de37 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt @@ -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): Pair? { rowXCoords.forEachIndexed { xIndex, xCell ->