adding an initial implementation of launcher displaying

This commit is contained in:
tibbi
2022-08-08 22:24:44 +02:00
parent e57f7a438d
commit c5de82481f
9 changed files with 258 additions and 6 deletions

View File

@ -1,12 +1,19 @@
package com.simplemobiletools.launcher.activities
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.pm.LauncherApps
import android.content.pm.PackageManager
import android.graphics.drawable.Drawable
import android.os.Bundle
import com.simplemobiletools.commons.extensions.appLaunched
import com.simplemobiletools.commons.extensions.hideKeyboard
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.models.FAQItem
import com.simplemobiletools.commons.views.MyGridLayoutManager
import com.simplemobiletools.launcher.BuildConfig
import com.simplemobiletools.launcher.R
import com.simplemobiletools.launcher.adapters.LaunchersAdapter
import com.simplemobiletools.launcher.models.AppLauncher
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : SimpleActivity() {
@ -15,11 +22,14 @@ class MainActivity : SimpleActivity() {
setContentView(R.layout.activity_main)
appLaunched(BuildConfig.APPLICATION_ID)
setupOptionsMenu()
getLaunchers()
}
override fun onResume() {
super.onResume()
setupToolbar(main_toolbar)
updateTextColors(main_coordinator)
launchers_fastscroller.updateColors(getProperPrimaryColor())
}
private fun setupOptionsMenu() {
@ -33,6 +43,62 @@ class MainActivity : SimpleActivity() {
}
}
@SuppressLint("WrongConstant")
private fun getLaunchers() {
val allApps = ArrayList<AppLauncher>()
val allPackageNames = ArrayList<String>()
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
var drawable: Drawable? = null
try {
// try getting the properly colored launcher icons
val launcher = getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
val activityList = launcher.getActivityList(packageName, android.os.Process.myUserHandle())[0]
drawable = activityList.getBadgedIcon(0)
} catch (e: Exception) {
} catch (e: Error) {
}
if (drawable == null) {
drawable = try {
packageManager.getApplicationIcon(packageName)
} catch (ignored: Exception) {
continue
}
}
allPackageNames.add(packageName)
allApps.add(AppLauncher(0, label, packageName, 0, drawable))
}
val launchers = allApps.distinctBy { it.packageName } as ArrayList<AppLauncher>
launchers.sortBy { it.title.toLowerCase() }
val layoutManager = launchers_grid.layoutManager as MyGridLayoutManager
layoutManager.spanCount = if (portrait) {
resources.getInteger(R.integer.portrait_column_count)
} else {
resources.getInteger(R.integer.landscape_column_count)
}
setupAdapter(launchers)
}
private fun setupAdapter(launchers: ArrayList<AppLauncher>) {
LaunchersAdapter(this, launchers) {
}.apply {
launchers_grid.adapter = this
}
}
private fun launchSettings() {
hideKeyboard()
startActivity(Intent(applicationContext, SettingsActivity::class.java))