store drawables inside the GridItem model itself

This commit is contained in:
tibbi 2022-09-23 12:05:15 +02:00
parent 5d85b9d7b4
commit 8247f8770d
4 changed files with 52 additions and 53 deletions

View File

@ -140,6 +140,10 @@ class MainActivity : SimpleActivity(), FlingListener {
hideFragment(all_apps_fragment)
}
if (mLongPressedIcon != null) {
home_screen_grid.draggedItemMoved(event.x.toInt(), event.y.toInt())
}
if (mTouchDownY != -1 && !mIgnoreMoveEvents) {
val diffY = mTouchDownY - event.y
val newY = mCurrentFragmentY - diffY
@ -365,14 +369,15 @@ class MainActivity : SimpleActivity(), FlingListener {
val homeScreenGridItems = ArrayList<HomeScreenGridItem>()
try {
val defaultDialerPackage = (getSystemService(Context.TELECOM_SERVICE) as TelecomManager).defaultDialerPackage
val dialerIcon = HomeScreenGridItem(null, 0, ROW_COUNT - 1, 1, ROW_COUNT, defaultDialerPackage, "")
val dialerIcon = HomeScreenGridItem(null, 0, ROW_COUNT - 1, 1, ROW_COUNT, defaultDialerPackage, "", null)
homeScreenGridItems.add(dialerIcon)
} catch (e: Exception) {
}
try {
val defaultSMSMessengerPackage = Telephony.Sms.getDefaultSmsPackage(this)
val SMSMessengerIcon = HomeScreenGridItem(null, 1, ROW_COUNT - 1, 2, ROW_COUNT, defaultSMSMessengerPackage, "")
val SMSMessengerIcon =
HomeScreenGridItem(null, 1, ROW_COUNT - 1, 2, ROW_COUNT, defaultSMSMessengerPackage, "", null)
homeScreenGridItems.add(SMSMessengerIcon)
} catch (e: Exception) {
}
@ -381,7 +386,8 @@ class MainActivity : SimpleActivity(), FlingListener {
val browserIntent = Intent("android.intent.action.VIEW", Uri.parse("http://"))
val resolveInfo = packageManager.resolveActivity(browserIntent, PackageManager.MATCH_DEFAULT_ONLY)
val defaultBrowserPackage = resolveInfo!!.activityInfo.packageName
val browserIcon = HomeScreenGridItem(null, 2, ROW_COUNT - 1, 3, ROW_COUNT, defaultBrowserPackage, "")
val browserIcon =
HomeScreenGridItem(null, 2, ROW_COUNT - 1, 3, ROW_COUNT, defaultBrowserPackage, "", null)
homeScreenGridItems.add(browserIcon)
} catch (e: Exception) {
}
@ -390,7 +396,7 @@ class MainActivity : SimpleActivity(), FlingListener {
val potentialStores = arrayListOf("com.android.vending", "org.fdroid.fdroid", "com.aurora.store")
val storePackage = potentialStores.firstOrNull { isPackageInstalled(it) }
if (storePackage != null) {
val storeIcon = HomeScreenGridItem(null, 3, ROW_COUNT - 1, 4, ROW_COUNT, storePackage, "")
val storeIcon = HomeScreenGridItem(null, 3, ROW_COUNT - 1, 4, ROW_COUNT, storePackage, "", null)
homeScreenGridItems.add(storeIcon)
}
} catch (e: Exception) {
@ -400,7 +406,7 @@ class MainActivity : SimpleActivity(), FlingListener {
val cameraIntent = Intent("android.media.action.IMAGE_CAPTURE")
val resolveInfo = packageManager.resolveActivity(cameraIntent, PackageManager.MATCH_DEFAULT_ONLY)
val defaultCameraPackage = resolveInfo!!.activityInfo.packageName
val cameraIcon = HomeScreenGridItem(null, 4, ROW_COUNT - 1, 5, ROW_COUNT, defaultCameraPackage, "")
val cameraIcon = HomeScreenGridItem(null, 4, ROW_COUNT - 1, 5, ROW_COUNT, defaultCameraPackage, "", null)
homeScreenGridItems.add(cameraIcon)
} catch (e: Exception) {
}

View File

@ -130,7 +130,7 @@ class AllAppsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
}
override fun onAppLauncherLongPressed(x: Float, y: Float, appLauncher: AppLauncher) {
val gridItem = HomeScreenGridItem(null, -1, -1, -1, 1, appLauncher.packageName, appLauncher.title)
val gridItem = HomeScreenGridItem(null, -1, -1, -1, 1, appLauncher.packageName, appLauncher.title, appLauncher.drawable)
activity?.showHomeIconMenu(x, y, gridItem, true)
ignoreTouches = true
}

View File

@ -1,9 +1,7 @@
package com.simplemobiletools.launcher.models
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey
import android.graphics.drawable.Drawable
import androidx.room.*
// grid coords are from 0-5 by default. Icons occupy 1 slot only, widgets can be bigger
@Entity(tableName = "home_screen_grid_items", indices = [(Index(value = ["id"], unique = true))])
@ -13,6 +11,10 @@ data class HomeScreenGridItem(
@ColumnInfo(name = "top") var top: Int,
@ColumnInfo(name = "right") var right: Int,
@ColumnInfo(name = "bottom") var bottom: Int,
@ColumnInfo(name = "package_name") val packageName: String,
@ColumnInfo(name = "title") val title: String
)
@ColumnInfo(name = "package_name") var packageName: String,
@ColumnInfo(name = "title") var title: String,
@Ignore var drawable: Drawable?
) {
constructor() : this(null, -1, -1, -1, -1, "", "", null)
}

View File

@ -5,7 +5,6 @@ import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.drawable.Drawable
import android.text.Layout
import android.text.StaticLayout
import android.text.TextPaint
@ -36,7 +35,6 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
private var iconSize = 0
private var gridItems = ArrayList<HomeScreenGridItem>()
private var gridItemDrawables = HashMap<String, Drawable>()
private var gridCenters = ArrayList<Pair<Int, Int>>()
init {
@ -51,13 +49,9 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
fun fetchGridItems() {
ensureBackgroundThread {
gridItemDrawables.clear()
gridItems = context.homeScreenGridItemsDB.getAllItems() as ArrayList<HomeScreenGridItem>
gridItems.forEach { item ->
val drawable = context.getDrawableForPackageName(item.packageName)
if (drawable != null) {
gridItemDrawables[item.packageName] = drawable
}
item.drawable = context.getDrawableForPackageName(item.packageName)
}
invalidate()
@ -77,6 +71,12 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
invalidate()
}
fun draggedItemMoved(x: Int, y: Int) {
if (draggedItem == null) {
return
}
}
// figure out at which cell was the item dropped, if it is empty
fun itemDraggingStopped(x: Int, y: Int) {
if (draggedItem == null) {
@ -111,17 +111,11 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
} else if (draggedItem != null) {
// we are dragging a new item at the home screen from the All Apps fragment
val newHomeScreenGridItem =
HomeScreenGridItem(null, xIndex, yIndex, xIndex + 1, yIndex + 1, draggedItem!!.packageName, draggedItem!!.title)
HomeScreenGridItem(null, xIndex, yIndex, xIndex + 1, yIndex + 1, draggedItem!!.packageName, draggedItem!!.title, draggedItem!!.drawable)
ensureBackgroundThread {
val newId = context.homeScreenGridItemsDB.insert(newHomeScreenGridItem)
newHomeScreenGridItem.id = newId
gridItems.add(newHomeScreenGridItem)
val drawable = context.getDrawableForPackageName(newHomeScreenGridItem.packageName)
if (drawable != null) {
gridItemDrawables[newHomeScreenGridItem.packageName] = drawable
}
invalidate()
}
}
@ -171,37 +165,34 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
}
}
gridItems.forEach { item ->
val drawable = gridItemDrawables[item.packageName]
if (drawable != null) {
val drawableX = rowXCoords[item.left] + iconMargin
gridItems.filter { it.drawable != null }.forEach { item ->
val drawableX = rowXCoords[item.left] + iconMargin
// icons at the bottom are drawn at the bottom of the grid and they have no label
if (item.top == ROW_COUNT - 1) {
val drawableY = rowYCoords[item.top] + rowHeight - iconSize - iconMargin * 2
drawable.setBounds(drawableX, drawableY, drawableX + iconSize, drawableY + iconSize)
} else {
val drawableY = rowYCoords[item.top] + iconSize / 2
drawable.setBounds(drawableX, drawableY, drawableX + iconSize, drawableY + iconSize)
// icons at the bottom are drawn at the bottom of the grid and they have no label
if (item.top == ROW_COUNT - 1) {
val drawableY = rowYCoords[item.top] + rowHeight - iconSize - iconMargin * 2
item.drawable!!.setBounds(drawableX, drawableY, drawableX + iconSize, drawableY + iconSize)
} else {
val drawableY = rowYCoords[item.top] + iconSize / 2
item.drawable!!.setBounds(drawableX, drawableY, drawableX + iconSize, drawableY + iconSize)
if (item.id != draggedItem?.id) {
val textY = rowYCoords[item.top] + iconSize * 1.5f + labelSideMargin
val staticLayout = StaticLayout.Builder
.obtain(item.title, 0, item.title.length, textPaint, rowWidth - 2 * labelSideMargin)
.setMaxLines(2)
.setEllipsize(TextUtils.TruncateAt.END)
.setAlignment(Layout.Alignment.ALIGN_CENTER)
.build()
if (item.id != draggedItem?.id) {
val textY = rowYCoords[item.top] + iconSize * 1.5f + labelSideMargin
val staticLayout = StaticLayout.Builder
.obtain(item.title, 0, item.title.length, textPaint, rowWidth - 2 * labelSideMargin)
.setMaxLines(2)
.setEllipsize(TextUtils.TruncateAt.END)
.setAlignment(Layout.Alignment.ALIGN_CENTER)
.build()
canvas.save()
canvas.translate(rowXCoords[item.left].toFloat() + labelSideMargin, textY)
staticLayout.draw(canvas)
canvas.restore()
}
canvas.save()
canvas.translate(rowXCoords[item.left].toFloat() + labelSideMargin, textY)
staticLayout.draw(canvas)
canvas.restore()
}
drawable.draw(canvas)
}
item.drawable!!.draw(canvas)
}
}