diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/adapters/LaunchersAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/adapters/LaunchersAdapter.kt index 66732933..01fc81ca 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/adapters/LaunchersAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/adapters/LaunchersAdapter.kt @@ -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) diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/databases/AppsDatabase.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/databases/AppsDatabase.kt index 7b89046c..99347ae7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/databases/AppsDatabase.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/databases/AppsDatabase.kt @@ -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 diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/extensions/Context.kt index 62c2ea43..18df6d07 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/extensions/Context.kt @@ -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) diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt index 2e46f00e..f7324cc2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt @@ -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() - val allPackageNames = ArrayList() - val intent = Intent(Intent.ACTION_MAIN, null) - intent.addCategory(Intent.CATEGORY_LAUNCHER) - ensureBackgroundThread { + val cachedLaunchers = context.launchersDB.getAppLaunchers() as ArrayList + gotLaunchers(cachedLaunchers) + + val allApps = ArrayList() + val allPackageNames = ArrayList() + 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 - 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) { + val sorted = appLaunchers.sortedBy { it.title.normalizeString().lowercase() }.toList() as ArrayList + setupAdapter(sorted) + } + private fun setupAdapter(launchers: ArrayList) { 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 { diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/interfaces/AppLaunchersDao.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/interfaces/AppLaunchersDao.kt new file mode 100644 index 00000000..32f602b1 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/interfaces/AppLaunchersDao.kt @@ -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 + + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun insertAll(appLaunchers: List) +}