cache launchers in a db

This commit is contained in:
tibbi 2022-09-18 10:59:07 +02:00
parent 9f20afef1c
commit df2f816102
5 changed files with 49 additions and 12 deletions

View File

@ -1,5 +1,6 @@
package com.simplemobiletools.launcher.adapters
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
@ -59,8 +60,12 @@ class LaunchersAdapter(
itemView.apply {
launcher_label.text = launcher.title
launcher_label.setTextColor(textColor)
launcher_icon.setImageDrawable(launcher.drawable!!)
launcher_icon.setPadding(iconPadding, iconPadding, iconPadding, 0)
if (launcher.drawable != null) {
launcher_icon.setImageDrawable(launcher.drawable!!)
}
setOnClickListener { itemClick(launcher) }
setOnLongClickListener { view ->
allAppsListener.onIconLongPressed(view.x, view.y, launcher.packageName)

View File

@ -4,11 +4,14 @@ import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.simplemobiletools.launcher.interfaces.AppLaunchersDao
import com.simplemobiletools.launcher.models.AppLauncher
@Database(entities = [AppLauncher::class], version = 1)
abstract class AppsDatabase : RoomDatabase() {
abstract fun AppLaunchersDao(): AppLaunchersDao
companion object {
private var db: AppsDatabase? = null

View File

@ -6,10 +6,14 @@ import android.graphics.drawable.Drawable
import android.os.Process
import com.simplemobiletools.commons.extensions.portrait
import com.simplemobiletools.launcher.R
import com.simplemobiletools.launcher.databases.AppsDatabase
import com.simplemobiletools.launcher.helpers.Config
import com.simplemobiletools.launcher.interfaces.AppLaunchersDao
val Context.config: Config get() = Config.newInstance(applicationContext)
val Context.launchersDB: AppLaunchersDao get() = AppsDatabase.getInstance(applicationContext).AppLaunchersDao()
fun Context.getColumnCount(): Int {
return if (portrait) {
resources.getInteger(R.integer.portrait_column_count)

View File

@ -22,6 +22,7 @@ import com.simplemobiletools.launcher.adapters.LaunchersAdapter
import com.simplemobiletools.launcher.extensions.getColumnCount
import com.simplemobiletools.launcher.extensions.getDrawableForPackageName
import com.simplemobiletools.launcher.extensions.launchApp
import com.simplemobiletools.launcher.extensions.launchersDB
import com.simplemobiletools.launcher.interfaces.AllAppsListener
import com.simplemobiletools.launcher.models.AppLauncher
import kotlinx.android.synthetic.main.all_apps_fragment.view.*
@ -73,12 +74,15 @@ class AllAppsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
@SuppressLint("WrongConstant")
private fun getLaunchers() {
val allApps = ArrayList<AppLauncher>()
val allPackageNames = ArrayList<String>()
val intent = Intent(Intent.ACTION_MAIN, null)
intent.addCategory(Intent.CATEGORY_LAUNCHER)
ensureBackgroundThread {
val cachedLaunchers = context.launchersDB.getAppLaunchers() as ArrayList<AppLauncher>
gotLaunchers(cachedLaunchers)
val allApps = ArrayList<AppLauncher>()
val allPackageNames = ArrayList<String>()
val intent = Intent(Intent.ACTION_MAIN, null)
intent.addCategory(Intent.CATEGORY_LAUNCHER)
val list = context.packageManager.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED)
for (info in list) {
val componentInfo = info.activityInfo.applicationInfo
@ -88,20 +92,25 @@ class AllAppsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
val placeholderColor = calculateAverageColor(drawable.toBitmap())
allPackageNames.add(packageName)
allApps.add(AppLauncher(0, label, packageName, 0, placeholderColor, drawable))
allApps.add(AppLauncher(null, label, packageName, 0, placeholderColor, drawable))
}
val launchers = allApps.distinctBy { it.packageName } as ArrayList<AppLauncher>
launchers.sortBy { it.title.normalizeString().lowercase() }
val layoutManager = all_apps_grid.layoutManager as MyGridLayoutManager
layoutManager.spanCount = context.getColumnCount()
setupAdapter(launchers)
context.launchersDB.insertAll(launchers)
gotLaunchers(launchers)
}
}
private fun gotLaunchers(appLaunchers: ArrayList<AppLauncher>) {
val sorted = appLaunchers.sortedBy { it.title.normalizeString().lowercase() }.toList() as ArrayList<AppLauncher>
setupAdapter(sorted)
}
private fun setupAdapter(launchers: ArrayList<AppLauncher>) {
activity?.runOnUiThread {
val layoutManager = all_apps_grid.layoutManager as MyGridLayoutManager
layoutManager.spanCount = context.getColumnCount()
LaunchersAdapter(activity!!, launchers, all_apps_fastscroller, this) {
activity?.launchApp((it as AppLauncher).packageName)
}.apply {

View File

@ -0,0 +1,16 @@
package com.simplemobiletools.launcher.interfaces
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.simplemobiletools.launcher.models.AppLauncher
@Dao
interface AppLaunchersDao {
@Query("SELECT * FROM apps")
fun getAppLaunchers(): List<AppLauncher>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(appLaunchers: List<AppLauncher>)
}