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

View File

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