list all installed apps at the Add launcher dialog

This commit is contained in:
tibbi 2016-08-13 20:33:41 +02:00
parent 12d4997d94
commit 69645885f3
5 changed files with 83 additions and 4 deletions

View File

@ -25,7 +25,7 @@ class MyCursorAdapter(cxt: Context, dataCursor: Cursor, val itemClick: (AppLaunc
val name = cursor.getString(cursor.getColumnIndex(DbHelper.NAME))
val pkgName = cursor.getString(cursor.getColumnIndex(DbHelper.PKG_NAME))
val icon = cursor.getInt(cursor.getColumnIndex(DbHelper.ICON_ID))
val launcher = AppLauncher(name, icon, pkgName)
val launcher = AppLauncher(name, pkgName, icon, null)
holder.bindView(context, launcher)
}
@ -37,7 +37,7 @@ class MyCursorAdapter(cxt: Context, dataCursor: Cursor, val itemClick: (AppLaunc
fun bindView(context: Context, launcher: AppLauncher) {
with(launcher) {
itemView.launcher_label.text = launcher.name
itemView.launcher_icon.setImageDrawable(context.resources.getDrawable(launcher.icon))
itemView.launcher_icon.setImageDrawable(context.resources.getDrawable(launcher.iconId))
itemView.setOnClickListener { itemClick(this) }
}
}

View File

@ -0,0 +1,37 @@
package com.simplemobiletools.applauncher.adapters
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.simplemobiletools.applauncher.R
import com.simplemobiletools.applauncher.models.AppLauncher
import kotlinx.android.synthetic.main.app_launcher_item.view.*
import java.util.*
class RecyclerAdapter(val launchers: ArrayList<AppLauncher>, val itemClick: (AppLauncher) -> Unit) :
RecyclerView.Adapter<RecyclerAdapter.ViewHolder>() {
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bindView(launchers[position])
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent?.context).inflate(R.layout.app_launcher_item, parent, false)
return ViewHolder(view, itemClick)
}
override fun getItemCount(): Int {
return launchers.count()
}
class ViewHolder(view: View, val itemClick: (AppLauncher) -> Unit) : RecyclerView.ViewHolder(view) {
fun bindView(launcher: AppLauncher) {
with(launcher) {
itemView.launcher_label.text = launcher.name
itemView.launcher_icon.setImageDrawable(launcher.drawable)
itemView.setOnClickListener { itemClick(this) }
}
}
}
}

View File

@ -1,15 +1,27 @@
package com.simplemobiletools.applauncher.dialogs
import android.app.AlertDialog
import android.app.Dialog
import android.app.DialogFragment
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.view.View
import com.simplemobiletools.applauncher.R
import com.simplemobiletools.applauncher.adapters.RecyclerAdapter
import com.simplemobiletools.applauncher.models.AppLauncher
import kotlinx.android.synthetic.main.launcher_picker.view.*
import java.util.*
class AddAppDialog() : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val builder = AlertDialog.Builder(activity)
builder.setTitle(R.string.add_apps)
val recyclerView = View.inflate(activity, R.layout.launcher_picker, null)
fillGrid(recyclerView)
builder.setView(recyclerView)
builder.setPositiveButton(android.R.string.ok, { dialogInterface, i ->
})
@ -17,4 +29,21 @@ class AddAppDialog() : DialogFragment() {
builder.setNegativeButton(android.R.string.cancel, null)
return builder.create()
}
private fun fillGrid(recyclerView: View) {
val apps = ArrayList<AppLauncher>()
val packageManager = activity.packageManager
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
apps.add(AppLauncher(componentInfo.loadLabel(packageManager).toString(), componentInfo.packageName, 0, componentInfo.loadIcon(packageManager)))
}
apps.sortBy { it.name }
recyclerView.launchers_holder.adapter = RecyclerAdapter(apps) {
}
}
}

View File

@ -1,3 +1,5 @@
package com.simplemobiletools.applauncher.models
data class AppLauncher(val name: String, val icon: Int, val pkgName: String)
import android.graphics.drawable.Drawable
data class AppLauncher(val name: String, val pkgName: String, val iconId: Int, val drawable: Drawable?)

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
android:id="@+id/launchers_holder"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingTop="@dimen/activity_margin"
app:layoutManager="android.support.v7.widget.GridLayoutManager"
app:spanCount="@integer/columns"/>