mirror of
https://github.com/SimpleMobileTools/Simple-Launcher.git
synced 2025-04-23 06:17:20 +02:00
handle storing and redrawing widgets
This commit is contained in:
parent
e83aef8d9d
commit
5fca4ff4f1
@ -34,6 +34,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
|
|||||||
private var textPaint: TextPaint
|
private var textPaint: TextPaint
|
||||||
private var dragShadowCirclePaint: Paint
|
private var dragShadowCirclePaint: Paint
|
||||||
private var draggedItem: HomeScreenGridItem? = null
|
private var draggedItem: HomeScreenGridItem? = null
|
||||||
|
private var isFirstDraw = true
|
||||||
|
|
||||||
// let's use a 6x5 grid for now with 1 special row at the bottom, prefilled with default apps
|
// let's use a 6x5 grid for now with 1 special row at the bottom, prefilled with default apps
|
||||||
private var rowXCoords = ArrayList<Int>(COLUMN_COUNT)
|
private var rowXCoords = ArrayList<Int>(COLUMN_COUNT)
|
||||||
@ -236,25 +237,17 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (areAllCellsEmpty) {
|
if (areAllCellsEmpty) {
|
||||||
val infoList = AppWidgetManager.getInstance(context).installedProviders
|
val widgetItem = draggedItem!!.copy()
|
||||||
val appWidgetProviderInfo = infoList.firstOrNull { it.provider.shortClassName == draggedItem?.shortClassName }
|
widgetItem.apply {
|
||||||
if (appWidgetProviderInfo != null) {
|
left = widgetRect.left
|
||||||
val appWidgetHost = AppWidgetHost(context, WIDGET_HOST_ID)
|
top = widgetRect.top
|
||||||
val appWidgetId = appWidgetHost.allocateAppWidgetId()
|
right = widgetRect.right
|
||||||
val appWidgetManager = AppWidgetManager.getInstance(context)
|
bottom = widgetRect.bottom
|
||||||
val canCreateWidget = appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, appWidgetProviderInfo.provider)
|
|
||||||
if (canCreateWidget) {
|
|
||||||
if (appWidgetProviderInfo.configure != null) {
|
|
||||||
appWidgetHost.startAppWidgetConfigureActivityForResult(context as MainActivity, appWidgetId, 0, REQUEST_CONFIGURE_WIDGET, null)
|
|
||||||
} else {
|
|
||||||
val widgetView = appWidgetHost.createView(context, appWidgetId, appWidgetProviderInfo)
|
|
||||||
widgetView.x = widgetRect.left * rowWidth + sideMargins.left.toFloat()
|
|
||||||
widgetView.y = widgetRect.top * rowHeight + sideMargins.top.toFloat()
|
|
||||||
val widgetWidth = draggedItem!!.widthCells * rowWidth
|
|
||||||
val widgetHeight = draggedItem!!.heightCells * rowHeight
|
|
||||||
addView(widgetView, widgetWidth, widgetHeight)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bindWidget(widgetItem, false)
|
||||||
|
ensureBackgroundThread {
|
||||||
|
context.homeScreenGridItemsDB.insert(widgetItem)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
performHapticFeedback()
|
performHapticFeedback()
|
||||||
@ -266,6 +259,29 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
|
|||||||
redrawGrid()
|
redrawGrid()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun bindWidget(item: HomeScreenGridItem, isInitialDrawAfterLaunch: Boolean) {
|
||||||
|
val infoList = AppWidgetManager.getInstance(context).installedProviders
|
||||||
|
val appWidgetProviderInfo = infoList.firstOrNull { it.provider.shortClassName == item.shortClassName }
|
||||||
|
if (appWidgetProviderInfo != null) {
|
||||||
|
val appWidgetHost = AppWidgetHost(context, WIDGET_HOST_ID)
|
||||||
|
val appWidgetId = appWidgetHost.allocateAppWidgetId()
|
||||||
|
val appWidgetManager = AppWidgetManager.getInstance(context)
|
||||||
|
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)
|
||||||
|
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 ->
|
||||||
@ -280,9 +296,11 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun redrawGrid() {
|
private fun redrawGrid() {
|
||||||
|
post {
|
||||||
setWillNotDraw(false)
|
setWillNotDraw(false)
|
||||||
invalidate()
|
invalidate()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun getFakeWidth() = width - sideMargins.left - sideMargins.right
|
private fun getFakeWidth() = width - sideMargins.left - sideMargins.right
|
||||||
|
|
||||||
@ -310,7 +328,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
gridItems.filter { it.drawable != null }.forEach { item ->
|
gridItems.filter { it.drawable != null && it.type == ITEM_TYPE_ICON }.forEach { item ->
|
||||||
if (item.id != draggedItem?.id) {
|
if (item.id != draggedItem?.id) {
|
||||||
val drawableX = rowXCoords[item.left] + iconMargin + sideMargins.left
|
val drawableX = rowXCoords[item.left] + iconMargin + sideMargins.left
|
||||||
|
|
||||||
@ -343,6 +361,12 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isFirstDraw) {
|
||||||
|
gridItems.filter { it.type == ITEM_TYPE_WIDGET }.forEach { item ->
|
||||||
|
bindWidget(item, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (draggedItem != null && draggedItemCurrentCoords.first != -1 && draggedItemCurrentCoords.second != -1) {
|
if (draggedItem != null && draggedItemCurrentCoords.first != -1 && draggedItemCurrentCoords.second != -1) {
|
||||||
if (draggedItem!!.type == ITEM_TYPE_ICON || draggedItem!!.type == ITEM_TYPE_SHORTCUT) {
|
if (draggedItem!!.type == ITEM_TYPE_ICON || draggedItem!!.type == ITEM_TYPE_SHORTCUT) {
|
||||||
// draw a circle under the current cell
|
// draw a circle under the current cell
|
||||||
@ -368,6 +392,8 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
|
|||||||
draggedItem!!.drawable!!.setBounds(drawableX, drawableY, drawableX + iconSize, drawableY + iconSize)
|
draggedItem!!.drawable!!.setBounds(drawableX, drawableY, drawableX + iconSize, drawableY + iconSize)
|
||||||
draggedItem!!.drawable!!.draw(canvas)
|
draggedItem!!.drawable!!.draw(canvas)
|
||||||
} else if (draggedItem!!.type == ITEM_TYPE_WIDGET) {
|
} else if (draggedItem!!.type == ITEM_TYPE_WIDGET) {
|
||||||
|
// at first draw we are loading the widget from the database at some exact spot, not dragging it
|
||||||
|
if (!isFirstDraw) {
|
||||||
val center = gridCenters.minBy {
|
val center = gridCenters.minBy {
|
||||||
Math.abs(it.first - draggedItemCurrentCoords.first + sideMargins.left) + Math.abs(it.second - draggedItemCurrentCoords.second + sideMargins.top)
|
Math.abs(it.first - draggedItemCurrentCoords.first + sideMargins.left) + Math.abs(it.second - draggedItemCurrentCoords.second + sideMargins.top)
|
||||||
}
|
}
|
||||||
@ -399,6 +425,9 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isFirstDraw = false
|
||||||
|
}
|
||||||
|
|
||||||
// get the clickable area around the icon, it includes text too
|
// get the clickable area around the icon, it includes text too
|
||||||
private fun getClickableRect(item: HomeScreenGridItem): Rect {
|
private fun getClickableRect(item: HomeScreenGridItem): Rect {
|
||||||
val clickableLeft = item.left * rowWidth + sideMargins.left
|
val clickableLeft = item.left * rowWidth + sideMargins.left
|
||||||
|
Loading…
x
Reference in New Issue
Block a user