mirror of
https://github.com/SimpleMobileTools/Simple-App-Launcher.git
synced 2025-06-05 21:49:21 +02:00
prefill not displayed apps to speed up FAB dialog displaying
This commit is contained in:
@@ -11,6 +11,7 @@ import com.simplemobiletools.applauncher.adapters.LaunchersAdapter
|
|||||||
import com.simplemobiletools.applauncher.dialogs.AddAppLauncherDialog
|
import com.simplemobiletools.applauncher.dialogs.AddAppLauncherDialog
|
||||||
import com.simplemobiletools.applauncher.extensions.config
|
import com.simplemobiletools.applauncher.extensions.config
|
||||||
import com.simplemobiletools.applauncher.extensions.dbHelper
|
import com.simplemobiletools.applauncher.extensions.dbHelper
|
||||||
|
import com.simplemobiletools.applauncher.extensions.getNotDisplayedLaunchers
|
||||||
import com.simplemobiletools.applauncher.extensions.isAPredefinedApp
|
import com.simplemobiletools.applauncher.extensions.isAPredefinedApp
|
||||||
import com.simplemobiletools.applauncher.models.AppLauncher
|
import com.simplemobiletools.applauncher.models.AppLauncher
|
||||||
import com.simplemobiletools.commons.extensions.appLaunched
|
import com.simplemobiletools.commons.extensions.appLaunched
|
||||||
@@ -25,7 +26,8 @@ import kotlinx.android.synthetic.main.activity_main.*
|
|||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
|
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 mStoredPrimaryColor = 0
|
||||||
private var mStoredTextColor = 0
|
private var mStoredTextColor = 0
|
||||||
|
|
||||||
@@ -38,8 +40,10 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
|
|||||||
storeStateVariables()
|
storeStateVariables()
|
||||||
|
|
||||||
fab.setOnClickListener {
|
fab.setOnClickListener {
|
||||||
AddAppLauncherDialog(this, launchers) {
|
if (notDisplayedLaunchers != null) {
|
||||||
setupLaunchers()
|
AddAppLauncherDialog(this, notDisplayedLaunchers!!) {
|
||||||
|
setupLaunchers()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -94,9 +98,9 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
|
|||||||
private fun getGridAdapter() = launchers_grid.adapter as? LaunchersAdapter
|
private fun getGridAdapter() = launchers_grid.adapter as? LaunchersAdapter
|
||||||
|
|
||||||
private fun setupLaunchers() {
|
private fun setupLaunchers() {
|
||||||
launchers = dbHelper.getLaunchers()
|
displayedLaunchers = dbHelper.getLaunchers()
|
||||||
checkInvalidApps()
|
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)
|
val launchIntent = packageManager.getLaunchIntentForPackage((it as AppLauncher).packageName)
|
||||||
if (launchIntent != null) {
|
if (launchIntent != null) {
|
||||||
startActivity(launchIntent)
|
startActivity(launchIntent)
|
||||||
@@ -108,18 +112,25 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
launchers_grid.adapter = adapter
|
launchers_grid.adapter = adapter
|
||||||
|
fillNotDisplayedLaunchers()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun fillNotDisplayedLaunchers() {
|
||||||
|
Thread {
|
||||||
|
notDisplayedLaunchers = getNotDisplayedLaunchers(displayedLaunchers)
|
||||||
|
}.start()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun checkInvalidApps() {
|
private fun checkInvalidApps() {
|
||||||
val invalidIds = ArrayList<String>()
|
val invalidIds = ArrayList<String>()
|
||||||
for ((id, name, packageName) in launchers) {
|
for ((id, name, packageName) in displayedLaunchers) {
|
||||||
val launchIntent = packageManager.getLaunchIntentForPackage(packageName)
|
val launchIntent = packageManager.getLaunchIntentForPackage(packageName)
|
||||||
if (launchIntent == null && !packageName.isAPredefinedApp()) {
|
if (launchIntent == null && !packageName.isAPredefinedApp()) {
|
||||||
invalidIds.add(id.toString())
|
invalidIds.add(id.toString())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
dbHelper.deleteLaunchers(invalidIds)
|
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() {
|
private fun storeStateVariables() {
|
||||||
|
@@ -1,20 +1,16 @@
|
|||||||
package com.simplemobiletools.applauncher.dialogs
|
package com.simplemobiletools.applauncher.dialogs
|
||||||
|
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
import android.content.Intent
|
|
||||||
import android.content.pm.PackageManager
|
|
||||||
import android.support.v7.app.AlertDialog
|
import android.support.v7.app.AlertDialog
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import com.simplemobiletools.applauncher.R
|
import com.simplemobiletools.applauncher.R
|
||||||
import com.simplemobiletools.applauncher.adapters.LaunchersDialogAdapter
|
import com.simplemobiletools.applauncher.adapters.LaunchersDialogAdapter
|
||||||
import com.simplemobiletools.applauncher.extensions.dbHelper
|
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.applauncher.models.AppLauncher
|
||||||
import com.simplemobiletools.commons.extensions.setupDialogStuff
|
import com.simplemobiletools.commons.extensions.setupDialogStuff
|
||||||
import kotlinx.android.synthetic.main.dialog_pick_launchers.view.*
|
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 dialog: AlertDialog
|
||||||
private var view = (activity.layoutInflater.inflate(R.layout.dialog_pick_launchers, null) as ViewGroup)
|
private var view = (activity.layoutInflater.inflate(R.layout.dialog_pick_launchers, null) as ViewGroup)
|
||||||
private var adapter: LaunchersDialogAdapter? = null
|
private var adapter: LaunchersDialogAdapter? = null
|
||||||
@@ -25,12 +21,8 @@ class AddAppLauncherDialog(val activity: Activity, val displayedLaunchers: Array
|
|||||||
.setNegativeButton(R.string.cancel, null)
|
.setNegativeButton(R.string.cancel, null)
|
||||||
.create().apply {
|
.create().apply {
|
||||||
activity.setupDialogStuff(view, this) {
|
activity.setupDialogStuff(view, this) {
|
||||||
Thread {
|
adapter = LaunchersDialogAdapter(activity, notDisplayedLaunchers)
|
||||||
adapter = LaunchersDialogAdapter(activity, getNotDisplayedLaunchers())
|
view.pick_launchers_holder.adapter = adapter
|
||||||
activity.runOnUiThread {
|
|
||||||
view.pick_launchers_holder.adapter = adapter
|
|
||||||
}
|
|
||||||
}.start()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -42,30 +34,4 @@ class AddAppLauncherDialog(val activity: Activity, val displayedLaunchers: Array
|
|||||||
callback()
|
callback()
|
||||||
dialog.dismiss()
|
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>
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -1,9 +1,36 @@
|
|||||||
package com.simplemobiletools.applauncher.extensions
|
package com.simplemobiletools.applauncher.extensions
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.content.pm.PackageManager
|
||||||
import com.simplemobiletools.applauncher.helpers.Config
|
import com.simplemobiletools.applauncher.helpers.Config
|
||||||
import com.simplemobiletools.applauncher.helpers.DBHelper
|
import com.simplemobiletools.applauncher.helpers.DBHelper
|
||||||
|
import com.simplemobiletools.applauncher.models.AppLauncher
|
||||||
|
|
||||||
val Context.config: Config get() = Config.newInstance(applicationContext)
|
val Context.config: Config get() = Config.newInstance(applicationContext)
|
||||||
|
|
||||||
val Context.dbHelper: DBHelper get() = DBHelper.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>
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user