start listening for events with widget host view on start

This commit is contained in:
tibbi 2022-09-27 15:38:56 +02:00
parent 05583d1ee0
commit 7ba519d82d
2 changed files with 18 additions and 4 deletions

View File

@ -91,6 +91,16 @@ class MainActivity : SimpleActivity(), FlingListener {
} }
} }
override fun onStart() {
super.onStart()
home_screen_grid.appWidgetHost.startListening()
}
override fun onStop() {
super.onStop()
home_screen_grid.appWidgetHost.stopListening()
}
override fun onBackPressed() { override fun onBackPressed() {
if (isAllAppsFragmentExpanded()) { if (isAllAppsFragmentExpanded()) {
hideFragment(all_apps_fragment) hideFragment(all_apps_fragment)

View File

@ -42,11 +42,15 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
private var rowHeight = 0 private var rowHeight = 0
private var iconSize = 0 private var iconSize = 0
// apply fake margins at the home screen. Real ones would cause the icons be cut at dragging at screen sides
private var sideMargins = Rect()
private var gridItems = ArrayList<HomeScreenGridItem>() private var gridItems = ArrayList<HomeScreenGridItem>()
private var gridCenters = ArrayList<Pair<Int, Int>>() private var gridCenters = ArrayList<Pair<Int, Int>>()
private var draggedItemCurrentCoords = Pair(-1, -1) private var draggedItemCurrentCoords = Pair(-1, -1)
private var sideMargins = Rect() // apply fake margins at the home screen. Real ones would cause the icons be cut at dragging at screen sides val appWidgetHost = MyAppWidgetHost(context, WIDGET_HOST_ID)
private val appWidgetManager = AppWidgetManager.getInstance(context)
init { init {
textPaint = TextPaint(Paint.ANTI_ALIAS_FLAG).apply { textPaint = TextPaint(Paint.ANTI_ALIAS_FLAG).apply {
@ -259,18 +263,18 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
} }
private fun bindWidget(item: HomeScreenGridItem, isInitialDrawAfterLaunch: Boolean) { private fun bindWidget(item: HomeScreenGridItem, isInitialDrawAfterLaunch: Boolean) {
val infoList = AppWidgetManager.getInstance(context).installedProviders val infoList = appWidgetManager.installedProviders
val appWidgetProviderInfo = infoList.firstOrNull { it.provider.shortClassName == item.shortClassName } val appWidgetProviderInfo = infoList.firstOrNull { it.provider.shortClassName == item.shortClassName }
if (appWidgetProviderInfo != null) { if (appWidgetProviderInfo != null) {
val appWidgetHost = MyAppWidgetHost(context, WIDGET_HOST_ID)
val appWidgetId = appWidgetHost.allocateAppWidgetId() val appWidgetId = appWidgetHost.allocateAppWidgetId()
val appWidgetManager = AppWidgetManager.getInstance(context)
val canCreateWidget = appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, appWidgetProviderInfo.provider) val canCreateWidget = appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, appWidgetProviderInfo.provider)
if (canCreateWidget) { if (canCreateWidget) {
if (appWidgetProviderInfo.configure != null && !isInitialDrawAfterLaunch) { if (appWidgetProviderInfo.configure != null && !isInitialDrawAfterLaunch) {
appWidgetHost.startAppWidgetConfigureActivityForResult(context as MainActivity, appWidgetId, 0, REQUEST_CONFIGURE_WIDGET, null) appWidgetHost.startAppWidgetConfigureActivityForResult(context as MainActivity, appWidgetId, 0, REQUEST_CONFIGURE_WIDGET, null)
} else { } else {
val widgetView = appWidgetHost.createView(context, appWidgetId, appWidgetProviderInfo) as MyAppWidgetHostView val widgetView = appWidgetHost.createView(context, appWidgetId, appWidgetProviderInfo) as MyAppWidgetHostView
widgetView.setAppWidget(appWidgetId, appWidgetProviderInfo)
widgetView.x = item.left * rowWidth + sideMargins.left.toFloat() widgetView.x = item.left * rowWidth + sideMargins.left.toFloat()
widgetView.y = item.top * rowHeight + sideMargins.top.toFloat() widgetView.y = item.top * rowHeight + sideMargins.top.toFloat()
val widgetWidth = item.widthCells * rowWidth val widgetWidth = item.widthCells * rowWidth