handle the permission for binding widgets

This commit is contained in:
tibbi 2022-09-28 21:35:11 +02:00
parent 7ba519d82d
commit cd15023c3a
3 changed files with 53 additions and 19 deletions

View File

@ -2,6 +2,9 @@ package com.simplemobiletools.launcher.activities
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.app.Activity
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProviderInfo
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
@ -25,10 +28,7 @@ import com.simplemobiletools.launcher.extensions.*
import com.simplemobiletools.launcher.fragments.AllAppsFragment
import com.simplemobiletools.launcher.fragments.MyFragment
import com.simplemobiletools.launcher.fragments.WidgetsFragment
import com.simplemobiletools.launcher.helpers.ITEM_TYPE_ICON
import com.simplemobiletools.launcher.helpers.ITEM_TYPE_WIDGET
import com.simplemobiletools.launcher.helpers.ROW_COUNT
import com.simplemobiletools.launcher.helpers.UNINSTALL_APP_REQUEST_CODE
import com.simplemobiletools.launcher.helpers.*
import com.simplemobiletools.launcher.interfaces.FlingListener
import com.simplemobiletools.launcher.models.AppLauncher
import com.simplemobiletools.launcher.models.HomeScreenGridItem
@ -47,6 +47,7 @@ class MainActivity : SimpleActivity(), FlingListener {
private var mOpenPopupMenu: PopupMenu? = null
private var mCachedLaunchers = ArrayList<AppLauncher>()
private var mLastTouchCoords = Pair(0f, 0f)
private var mActionOnCanBindWidget: ((granted: Boolean) -> Unit)? = null
private lateinit var mDetector: GestureDetectorCompat
@ -117,6 +118,8 @@ class MainActivity : SimpleActivity(), FlingListener {
ensureBackgroundThread {
refetchLaunchers()
}
} else if (requestCode == REQUEST_ALLOW_BINDING_WIDGET) {
mActionOnCanBindWidget?.invoke(resultCode == Activity.RESULT_OK)
}
}
@ -482,6 +485,21 @@ class MainActivity : SimpleActivity(), FlingListener {
homeScreenGridItemsDB.insertAll(homeScreenGridItems)
}
fun handleWidgetBinding(appWidgetManager: AppWidgetManager, appWidgetId: Int, appWidgetInfo: AppWidgetProviderInfo, callback: (canBind: Boolean) -> Unit) {
mActionOnCanBindWidget = null
val canCreateWidget = appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, appWidgetInfo.provider)
if (canCreateWidget) {
callback(true)
} else {
mActionOnCanBindWidget = callback
Intent(AppWidgetManager.ACTION_APPWIDGET_BIND).apply {
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider)
startActivityForResult(this, REQUEST_ALLOW_BINDING_WIDGET)
}
}
}
// taken from https://gist.github.com/maxjvh/a6ab15cbba9c82a5065d
private fun calculateAverageColor(bitmap: Bitmap): Int {
var red = 0

View File

@ -12,6 +12,7 @@ const val COLUMN_COUNT = 5
const val UNINSTALL_APP_REQUEST_CODE = 50
const val REQUEST_CONFIGURE_WIDGET = 51
const val REQUEST_ALLOW_BINDING_WIDGET = 52
const val ITEM_TYPE_ICON = 0
const val ITEM_TYPE_WIDGET = 1

View File

@ -248,9 +248,12 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
bottom = widgetRect.bottom
}
bindWidget(widgetItem, false)
ensureBackgroundThread {
context.homeScreenGridItemsDB.insert(widgetItem)
val itemId = context.homeScreenGridItemsDB.insert(widgetItem)
widgetItem.id = itemId
post {
bindWidget(widgetItem, false)
}
}
} else {
performHapticFeedback()
@ -263,23 +266,35 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
}
private fun bindWidget(item: HomeScreenGridItem, isInitialDrawAfterLaunch: Boolean) {
val infoList = appWidgetManager.installedProviders
val activity = context as MainActivity
val infoList = appWidgetManager!!.installedProviders
val appWidgetProviderInfo = infoList.firstOrNull { it.provider.shortClassName == item.shortClassName }
if (appWidgetProviderInfo != null) {
val appWidgetId = appWidgetHost.allocateAppWidgetId()
val canCreateWidget = appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, appWidgetProviderInfo.provider)
if (canCreateWidget) {
if (appWidgetProviderInfo.configure != null && !isInitialDrawAfterLaunch) {
appWidgetHost.startAppWidgetConfigureActivityForResult(context as MainActivity, appWidgetId, 0, REQUEST_CONFIGURE_WIDGET, null)
} else {
val widgetView = appWidgetHost.createView(context, appWidgetId, appWidgetProviderInfo) as MyAppWidgetHostView
widgetView.setAppWidget(appWidgetId, appWidgetProviderInfo)
activity.handleWidgetBinding(appWidgetManager, appWidgetId, appWidgetProviderInfo) { canBind ->
if (canBind) {
if (appWidgetProviderInfo.configure != null && !isInitialDrawAfterLaunch) {
appWidgetHost.startAppWidgetConfigureActivityForResult(
context as MainActivity,
appWidgetId,
0,
REQUEST_CONFIGURE_WIDGET,
null
)
} else {
val widgetView = appWidgetHost.createView(context, 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)
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) {
ensureBackgroundThread {
context.homeScreenGridItemsDB.deleteById(item.id!!)
}
}
}
}