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 package com.simplemobiletools.launcher.adapters
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -59,8 +60,12 @@ class LaunchersAdapter(
itemView.apply { itemView.apply {
launcher_label.text = launcher.title launcher_label.text = launcher.title
launcher_label.setTextColor(textColor) launcher_label.setTextColor(textColor)
launcher_icon.setImageDrawable(launcher.drawable!!)
launcher_icon.setPadding(iconPadding, iconPadding, iconPadding, 0) launcher_icon.setPadding(iconPadding, iconPadding, iconPadding, 0)
if (launcher.drawable != null) {
launcher_icon.setImageDrawable(launcher.drawable!!)
}
setOnClickListener { itemClick(launcher) } setOnClickListener { itemClick(launcher) }
setOnLongClickListener { view -> setOnLongClickListener { view ->
allAppsListener.onIconLongPressed(view.x, view.y, launcher.packageName) 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.Database
import androidx.room.Room import androidx.room.Room
import androidx.room.RoomDatabase import androidx.room.RoomDatabase
import com.simplemobiletools.launcher.interfaces.AppLaunchersDao
import com.simplemobiletools.launcher.models.AppLauncher import com.simplemobiletools.launcher.models.AppLauncher
@Database(entities = [AppLauncher::class], version = 1) @Database(entities = [AppLauncher::class], version = 1)
abstract class AppsDatabase : RoomDatabase() { abstract class AppsDatabase : RoomDatabase() {
abstract fun AppLaunchersDao(): AppLaunchersDao
companion object { companion object {
private var db: AppsDatabase? = null private var db: AppsDatabase? = null

View File

@ -6,10 +6,14 @@ import android.graphics.drawable.Drawable
import android.os.Process import android.os.Process
import com.simplemobiletools.commons.extensions.portrait import com.simplemobiletools.commons.extensions.portrait
import com.simplemobiletools.launcher.R import com.simplemobiletools.launcher.R
import com.simplemobiletools.launcher.databases.AppsDatabase
import com.simplemobiletools.launcher.helpers.Config import com.simplemobiletools.launcher.helpers.Config
import com.simplemobiletools.launcher.interfaces.AppLaunchersDao
val Context.config: Config get() = Config.newInstance(applicationContext) val Context.config: Config get() = Config.newInstance(applicationContext)
val Context.launchersDB: AppLaunchersDao get() = AppsDatabase.getInstance(applicationContext).AppLaunchersDao()
fun Context.getColumnCount(): Int { fun Context.getColumnCount(): Int {
return if (portrait) { return if (portrait) {
resources.getInteger(R.integer.portrait_column_count) 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.getColumnCount
import com.simplemobiletools.launcher.extensions.getDrawableForPackageName import com.simplemobiletools.launcher.extensions.getDrawableForPackageName
import com.simplemobiletools.launcher.extensions.launchApp import com.simplemobiletools.launcher.extensions.launchApp
import com.simplemobiletools.launcher.extensions.launchersDB
import com.simplemobiletools.launcher.interfaces.AllAppsListener import com.simplemobiletools.launcher.interfaces.AllAppsListener
import com.simplemobiletools.launcher.models.AppLauncher import com.simplemobiletools.launcher.models.AppLauncher
import kotlinx.android.synthetic.main.all_apps_fragment.view.* import kotlinx.android.synthetic.main.all_apps_fragment.view.*
@ -73,12 +74,15 @@ class AllAppsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
@SuppressLint("WrongConstant") @SuppressLint("WrongConstant")
private fun getLaunchers() { private fun getLaunchers() {
val allApps = ArrayList<AppLauncher>()
val allPackageNames = ArrayList<String>()
val intent = Intent(Intent.ACTION_MAIN, null)
intent.addCategory(Intent.CATEGORY_LAUNCHER)
ensureBackgroundThread { 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) val list = context.packageManager.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED)
for (info in list) { for (info in list) {
val componentInfo = info.activityInfo.applicationInfo val componentInfo = info.activityInfo.applicationInfo
@ -88,20 +92,25 @@ class AllAppsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
val placeholderColor = calculateAverageColor(drawable.toBitmap()) val placeholderColor = calculateAverageColor(drawable.toBitmap())
allPackageNames.add(packageName) 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> val launchers = allApps.distinctBy { it.packageName } as ArrayList<AppLauncher>
launchers.sortBy { it.title.normalizeString().lowercase() } context.launchersDB.insertAll(launchers)
gotLaunchers(launchers)
val layoutManager = all_apps_grid.layoutManager as MyGridLayoutManager
layoutManager.spanCount = context.getColumnCount()
setupAdapter(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>) { private fun setupAdapter(launchers: ArrayList<AppLauncher>) {
activity?.runOnUiThread { activity?.runOnUiThread {
val layoutManager = all_apps_grid.layoutManager as MyGridLayoutManager
layoutManager.spanCount = context.getColumnCount()
LaunchersAdapter(activity!!, launchers, all_apps_fastscroller, this) { LaunchersAdapter(activity!!, launchers, all_apps_fastscroller, this) {
activity?.launchApp((it as AppLauncher).packageName) activity?.launchApp((it as AppLauncher).packageName)
}.apply { }.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>)
}