fetch apps on a background thread
This commit is contained in:
parent
a195bff520
commit
4c079b2832
|
@ -13,6 +13,7 @@ import android.view.Surface
|
||||||
import android.view.WindowManager
|
import android.view.WindowManager
|
||||||
import android.widget.FrameLayout
|
import android.widget.FrameLayout
|
||||||
import com.simplemobiletools.commons.extensions.*
|
import com.simplemobiletools.commons.extensions.*
|
||||||
|
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
|
||||||
import com.simplemobiletools.commons.helpers.isRPlus
|
import com.simplemobiletools.commons.helpers.isRPlus
|
||||||
import com.simplemobiletools.commons.views.MyGridLayoutManager
|
import com.simplemobiletools.commons.views.MyGridLayoutManager
|
||||||
import com.simplemobiletools.launcher.BuildConfig
|
import com.simplemobiletools.launcher.BuildConfig
|
||||||
|
@ -61,52 +62,56 @@ class MainActivity : SimpleActivity() {
|
||||||
val intent = Intent(Intent.ACTION_MAIN, null)
|
val intent = Intent(Intent.ACTION_MAIN, null)
|
||||||
intent.addCategory(Intent.CATEGORY_LAUNCHER)
|
intent.addCategory(Intent.CATEGORY_LAUNCHER)
|
||||||
|
|
||||||
val list = packageManager.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED)
|
ensureBackgroundThread {
|
||||||
for (info in list) {
|
val list = packageManager.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED)
|
||||||
val componentInfo = info.activityInfo.applicationInfo
|
for (info in list) {
|
||||||
val label = componentInfo.loadLabel(packageManager).toString()
|
val componentInfo = info.activityInfo.applicationInfo
|
||||||
val packageName = componentInfo.packageName
|
val label = componentInfo.loadLabel(packageManager).toString()
|
||||||
|
val packageName = componentInfo.packageName
|
||||||
|
|
||||||
var drawable: Drawable? = null
|
var drawable: Drawable? = null
|
||||||
try {
|
try {
|
||||||
// try getting the properly colored launcher icons
|
// try getting the properly colored launcher icons
|
||||||
val launcher = getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
|
val launcher = getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
|
||||||
val activityList = launcher.getActivityList(packageName, android.os.Process.myUserHandle())[0]
|
val activityList = launcher.getActivityList(packageName, android.os.Process.myUserHandle())[0]
|
||||||
drawable = activityList.getBadgedIcon(0)
|
drawable = activityList.getBadgedIcon(0)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
} catch (e: Error) {
|
} catch (e: Error) {
|
||||||
}
|
|
||||||
|
|
||||||
if (drawable == null) {
|
|
||||||
drawable = try {
|
|
||||||
packageManager.getApplicationIcon(packageName)
|
|
||||||
} catch (ignored: Exception) {
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (drawable == null) {
|
||||||
|
drawable = try {
|
||||||
|
packageManager.getApplicationIcon(packageName)
|
||||||
|
} catch (ignored: Exception) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
allPackageNames.add(packageName)
|
||||||
|
allApps.add(AppLauncher(0, label, packageName, 0, drawable))
|
||||||
}
|
}
|
||||||
|
|
||||||
allPackageNames.add(packageName)
|
val launchers = allApps.distinctBy { it.packageName } as ArrayList<AppLauncher>
|
||||||
allApps.add(AppLauncher(0, label, packageName, 0, drawable))
|
launchers.sortBy { it.title.toLowerCase() }
|
||||||
|
|
||||||
|
val layoutManager = launchers_grid.layoutManager as MyGridLayoutManager
|
||||||
|
layoutManager.spanCount = getColumnCount()
|
||||||
|
setupAdapter(launchers)
|
||||||
}
|
}
|
||||||
|
|
||||||
val launchers = allApps.distinctBy { it.packageName } as ArrayList<AppLauncher>
|
|
||||||
launchers.sortBy { it.title.toLowerCase() }
|
|
||||||
|
|
||||||
val layoutManager = launchers_grid.layoutManager as MyGridLayoutManager
|
|
||||||
layoutManager.spanCount = getColumnCount()
|
|
||||||
setupAdapter(launchers)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setupAdapter(launchers: ArrayList<AppLauncher>) {
|
private fun setupAdapter(launchers: ArrayList<AppLauncher>) {
|
||||||
LaunchersAdapter(this, launchers, launchers_fastscroller) {
|
runOnUiThread {
|
||||||
val launchIntent = packageManager.getLaunchIntentForPackage((it as AppLauncher).packageName)
|
LaunchersAdapter(this, launchers, launchers_fastscroller) {
|
||||||
try {
|
val launchIntent = packageManager.getLaunchIntentForPackage((it as AppLauncher).packageName)
|
||||||
startActivity(launchIntent)
|
try {
|
||||||
} catch (e: Exception) {
|
startActivity(launchIntent)
|
||||||
showErrorToast(e)
|
} catch (e: Exception) {
|
||||||
|
showErrorToast(e)
|
||||||
|
}
|
||||||
|
}.apply {
|
||||||
|
launchers_grid.adapter = this
|
||||||
}
|
}
|
||||||
}.apply {
|
|
||||||
launchers_grid.adapter = this
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue