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))

View File

@ -0,0 +1,62 @@
package com.simplemobiletools.launcher.adapters
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.simplemobiletools.commons.extensions.getProperTextColor
import com.simplemobiletools.commons.extensions.portrait
import com.simplemobiletools.commons.extensions.realScreenSize
import com.simplemobiletools.launcher.R
import com.simplemobiletools.launcher.activities.SimpleActivity
import com.simplemobiletools.launcher.models.AppLauncher
import kotlinx.android.synthetic.main.item_launcher_label.view.*
class LaunchersAdapter(
val activity: SimpleActivity,
val launchers: ArrayList<AppLauncher>,
val itemClick: (Any) -> Unit
) : RecyclerView.Adapter<LaunchersAdapter.ViewHolder>() {
private var textColor = activity.getProperTextColor()
private var iconPadding = 0
init {
calculateIconWidth()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_launcher_label, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bindView(launchers[position])
}
override fun getItemCount() = launchers.size
private fun calculateIconWidth() {
val currentColumnCount = if (activity.portrait) {
activity.resources.getInteger(R.integer.portrait_column_count)
} else {
activity.resources.getInteger(R.integer.landscape_column_count)
}
val iconWidth = activity.realScreenSize.x / currentColumnCount
iconPadding = (iconWidth * 0.1f).toInt()
}
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
fun bindView(launcher: AppLauncher): View {
itemView.apply {
launcher_label.text = launcher.title
launcher_label.setTextColor(textColor)
launcher_icon.setImageDrawable(launcher.drawable!!)
launcher_icon.setPadding(iconPadding, iconPadding, iconPadding, 0)
setOnClickListener { itemClick(launcher) }
}
return itemView
}
}
}

View File

@ -0,0 +1,38 @@
package com.simplemobiletools.launcher.models
import android.graphics.drawable.Drawable
import com.simplemobiletools.commons.helpers.SORT_BY_TITLE
import com.simplemobiletools.commons.helpers.SORT_DESCENDING
data class AppLauncher(val id: Int, var title: String, val packageName: String, var order: Int, val drawable: Drawable? = null) : Comparable<AppLauncher> {
companion object {
var sorting = 0
}
override fun equals(other: Any?) = packageName.equals((other as AppLauncher).packageName, true)
fun getBubbleText() = title
override fun compareTo(other: AppLauncher): Int {
var result = when {
sorting and SORT_BY_TITLE != 0 -> title.toLowerCase().compareTo(other.title.toLowerCase())
else -> {
if (order > 0 && other.order == 0) {
-1
} else if (order == 0 && other.order > 0) {
1
} else if (order > 0 && other.order > 0) {
order.compareTo(other.order)
} else {
title.toLowerCase().compareTo(other.title.toLowerCase())
}
}
}
if (sorting and SORT_DESCENDING != 0) {
result *= -1
}
return result
}
}