prefill not displayed apps to speed up FAB dialog displaying

This commit is contained in:
tibbi 2018-04-04 20:47:27 +02:00
parent 89f3f106a2
commit b0adffb670
3 changed files with 48 additions and 44 deletions

View File

@ -11,6 +11,7 @@ import com.simplemobiletools.applauncher.adapters.LaunchersAdapter
import com.simplemobiletools.applauncher.dialogs.AddAppLauncherDialog
import com.simplemobiletools.applauncher.extensions.config
import com.simplemobiletools.applauncher.extensions.dbHelper
import com.simplemobiletools.applauncher.extensions.getNotDisplayedLaunchers
import com.simplemobiletools.applauncher.extensions.isAPredefinedApp
import com.simplemobiletools.applauncher.models.AppLauncher
import com.simplemobiletools.commons.extensions.appLaunched
@ -25,7 +26,8 @@ import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
private var launchers = ArrayList<AppLauncher>()
private var displayedLaunchers = ArrayList<AppLauncher>()
private var notDisplayedLaunchers: ArrayList<AppLauncher>? = null
private var mStoredPrimaryColor = 0
private var mStoredTextColor = 0
@ -38,8 +40,10 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
storeStateVariables()
fab.setOnClickListener {
AddAppLauncherDialog(this, launchers) {
setupLaunchers()
if (notDisplayedLaunchers != null) {
AddAppLauncherDialog(this, notDisplayedLaunchers!!) {
setupLaunchers()
}
}
}
}
@ -94,9 +98,9 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
private fun getGridAdapter() = launchers_grid.adapter as? LaunchersAdapter
private fun setupLaunchers() {
launchers = dbHelper.getLaunchers()
displayedLaunchers = dbHelper.getLaunchers()
checkInvalidApps()
val adapter = LaunchersAdapter(this, launchers, this, launchers_grid) {
val adapter = LaunchersAdapter(this, displayedLaunchers, this, launchers_grid) {
val launchIntent = packageManager.getLaunchIntentForPackage((it as AppLauncher).packageName)
if (launchIntent != null) {
startActivity(launchIntent)
@ -108,18 +112,25 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
}
}
launchers_grid.adapter = adapter
fillNotDisplayedLaunchers()
}
private fun fillNotDisplayedLaunchers() {
Thread {
notDisplayedLaunchers = getNotDisplayedLaunchers(displayedLaunchers)
}.start()
}
private fun checkInvalidApps() {
val invalidIds = ArrayList<String>()
for ((id, name, packageName) in launchers) {
for ((id, name, packageName) in displayedLaunchers) {
val launchIntent = packageManager.getLaunchIntentForPackage(packageName)
if (launchIntent == null && !packageName.isAPredefinedApp()) {
invalidIds.add(id.toString())
}
}
dbHelper.deleteLaunchers(invalidIds)
launchers = launchers.filter { !invalidIds.contains(it.id.toString()) } as ArrayList<AppLauncher>
displayedLaunchers = displayedLaunchers.filter { !invalidIds.contains(it.id.toString()) } as ArrayList<AppLauncher>
}
private fun storeStateVariables() {

View File

@ -1,20 +1,16 @@
package com.simplemobiletools.applauncher.dialogs
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.support.v7.app.AlertDialog
import android.view.ViewGroup
import com.simplemobiletools.applauncher.R
import com.simplemobiletools.applauncher.adapters.LaunchersDialogAdapter
import com.simplemobiletools.applauncher.extensions.dbHelper
import com.simplemobiletools.applauncher.extensions.getLauncherDrawable
import com.simplemobiletools.applauncher.extensions.isAPredefinedApp
import com.simplemobiletools.applauncher.models.AppLauncher
import com.simplemobiletools.commons.extensions.setupDialogStuff
import kotlinx.android.synthetic.main.dialog_pick_launchers.view.*
class AddAppLauncherDialog(val activity: Activity, val displayedLaunchers: ArrayList<AppLauncher>, val callback: () -> Unit) {
class AddAppLauncherDialog(val activity: Activity, val notDisplayedLaunchers: ArrayList<AppLauncher>, val callback: () -> Unit) {
private var dialog: AlertDialog
private var view = (activity.layoutInflater.inflate(R.layout.dialog_pick_launchers, null) as ViewGroup)
private var adapter: LaunchersDialogAdapter? = null
@ -25,12 +21,8 @@ class AddAppLauncherDialog(val activity: Activity, val displayedLaunchers: Array
.setNegativeButton(R.string.cancel, null)
.create().apply {
activity.setupDialogStuff(view, this) {
Thread {
adapter = LaunchersDialogAdapter(activity, getNotDisplayedLaunchers())
activity.runOnUiThread {
view.pick_launchers_holder.adapter = adapter
}
}.start()
adapter = LaunchersDialogAdapter(activity, notDisplayedLaunchers)
view.pick_launchers_holder.adapter = adapter
}
}
}
@ -42,30 +34,4 @@ class AddAppLauncherDialog(val activity: Activity, val displayedLaunchers: Array
callback()
dialog.dismiss()
}
private fun getNotDisplayedLaunchers(): ArrayList<AppLauncher> {
val resources = activity.resources
val packageManager = activity.packageManager
val allApps = ArrayList<AppLauncher>()
val intent = Intent(Intent.ACTION_MAIN, null)
intent.addCategory(Intent.CATEGORY_LAUNCHER)
val list = packageManager.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED)
for (info in list) {
val componentInfo = info.activityInfo.applicationInfo
val label = componentInfo.loadLabel(packageManager).toString()
val packageName = componentInfo.packageName
val drawable = if (packageName.isAPredefinedApp()) {
resources.getLauncherDrawable(packageName)
} else {
packageManager.getApplicationIcon(packageName)
}
allApps.add(AppLauncher(0, label, packageName, drawable))
}
val sorted = allApps.sortedWith(compareBy { it.name.toLowerCase() })
val unique = sorted.distinctBy { it.packageName }
return unique.filter { !displayedLaunchers.contains(it) && it.packageName != "com.simplemobiletools.applauncher" } as ArrayList<AppLauncher>
}
}

View File

@ -1,9 +1,36 @@
package com.simplemobiletools.applauncher.extensions
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import com.simplemobiletools.applauncher.helpers.Config
import com.simplemobiletools.applauncher.helpers.DBHelper
import com.simplemobiletools.applauncher.models.AppLauncher
val Context.config: Config get() = Config.newInstance(applicationContext)
val Context.dbHelper: DBHelper get() = DBHelper.newInstance(applicationContext)
fun Context.getNotDisplayedLaunchers(displayedLaunchers: ArrayList<AppLauncher>): ArrayList<AppLauncher> {
val allApps = ArrayList<AppLauncher>()
val intent = Intent(Intent.ACTION_MAIN, null)
intent.addCategory(Intent.CATEGORY_LAUNCHER)
val list = packageManager.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED)
for (info in list) {
val componentInfo = info.activityInfo.applicationInfo
val label = componentInfo.loadLabel(packageManager).toString()
val packageName = componentInfo.packageName
val drawable = if (packageName.isAPredefinedApp()) {
resources.getLauncherDrawable(packageName)
} else {
packageManager.getApplicationIcon(packageName)
}
allApps.add(AppLauncher(0, label, packageName, drawable))
}
val sorted = allApps.sortedWith(compareBy { it.name.toLowerCase() })
val unique = sorted.distinctBy { it.packageName }
return unique.filter { !displayedLaunchers.contains(it) && it.packageName != "com.simplemobiletools.applauncher" } as ArrayList<AppLauncher>
}