Open Jasmine-liang opened 3 years ago
Show also icons in RecyclerView.
Steps to consider to make a laucher:
action
and a LAUNCHER category
PackageManager
MainActivity.kt
import android.content.Intent
import android.content.pm.ResolveInfo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
private const val TAG = "MainActivity"
class MainActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerView = findViewById(R.id.app_recycler_view)
recyclerView.layoutManager = LinearLayoutManager(this)
setupAdapter()
}
private fun setupAdapter() {
val startupIntent = Intent(Intent.ACTION_MAIN).apply {
addCategory(Intent.CATEGORY_LAUNCHER)
}
val packageManager = packageManager
val activities = packageManager.queryIntentActivities(startupIntent, 0)
activities.sortWith { a, b ->
String.CASE_INSENSITIVE_ORDER.compare(
a.loadLabel(packageManager).toString(),
b.loadLabel(packageManager).toString()
)
}
recyclerView.adapter = ActivityAdapter(activities)
}
private class ActivityHolder(itemView: View) :
RecyclerView.ViewHolder(itemView),
View.OnClickListener {
val appIcon: ImageView = itemView.findViewById(R.id.app_icon)
val appName: TextView = itemView.findViewById(R.id.app_name)
private lateinit var resolveInfo: ResolveInfo
init {
itemView.setOnClickListener(this)
}
fun bindActivity(resolveInfo: ResolveInfo) {
this.resolveInfo = resolveInfo
val packageManager = itemView.context.packageManager
appName.text = resolveInfo.loadLabel(packageManager).toString()
appIcon.setImageDrawable(resolveInfo.loadIcon(packageManager))
}
override fun onClick(view: View) {
val activityInfo = resolveInfo.activityInfo
val intent = Intent(Intent.ACTION_MAIN).apply {
setClassName(activityInfo.applicationInfo.packageName,
activityInfo.name)
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
val context = view.context
context.startActivity(intent)
}
}
private class ActivityAdapter(val activities: List<ResolveInfo>) :
RecyclerView.Adapter<ActivityHolder>() {
override fun onCreateViewHolder(container: ViewGroup, viewType: Int):
ActivityHolder {
val layoutInflater = LayoutInflater.from(container.context)
val view = layoutInflater
.inflate(R.layout.list_of_apps, container, false)
return ActivityHolder(view)
}
override fun onBindViewHolder(holder: ActivityHolder, position: Int) {
val resolveInfo = activities[position]
holder.bindActivity(resolveInfo)
}
override fun getItemCount(): Int {
return activities.size
}
}
}
_list_of_apps.xml_
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical"
android:padding="8dp"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/app_icon"
android:src="@drawable/abc_vector_test"
android:layout_width="80dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_height="80dp"/>
<TextView
android:id="@+id/app_name"
android:text="app"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
_activity_main.xml_
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/app_recycler_view"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"/>
What it looks like