store the launcher drawable in the model for improved performance

This commit is contained in:
tibbi 2017-11-12 17:06:26 +01:00
parent d184370fb3
commit f92c2052ec
10 changed files with 101 additions and 85 deletions

View File

@ -1,7 +1,6 @@
package com.simplemobiletools.applauncher.activities
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.view.Menu
@ -9,7 +8,7 @@ import android.view.MenuItem
import com.simplemobiletools.applauncher.BuildConfig
import com.simplemobiletools.applauncher.R
import com.simplemobiletools.applauncher.adapters.RecyclerAdapter
import com.simplemobiletools.applauncher.dialogs.AddAppDialog
import com.simplemobiletools.applauncher.dialogs.AddAppLauncherDialog
import com.simplemobiletools.applauncher.extensions.dbHelper
import com.simplemobiletools.applauncher.extensions.isAPredefinedApp
import com.simplemobiletools.applauncher.models.AppLauncher
@ -32,7 +31,7 @@ class MainActivity : SimpleActivity(), RecyclerAdapter.AppLaunchersListener {
checkWhatsNewDialog()
fab.setOnClickListener {
AddAppDialog(this, getNotDisplayedLaunchers()) {
AddAppLauncherDialog(this, launchers) {
refreshLaunchers()
}
}
@ -82,23 +81,6 @@ class MainActivity : SimpleActivity(), RecyclerAdapter.AppLaunchersListener {
}
}
private fun getNotDisplayedLaunchers(): ArrayList<AppLauncher> {
val allApps = ArrayList<AppLauncher>()
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()
allApps.add(AppLauncher(0, label, componentInfo.packageName))
}
val sorted = allApps.sortedWith(compareBy { it.name.toLowerCase() })
val unique = sorted.distinctBy { it.packageName }
val filtered = unique.filter { !launchers.contains(it) && it.packageName != "com.simplemobiletools.applauncher" }
return filtered as ArrayList<AppLauncher>
}
private fun checkInvalidApps() {
val invalidIds = ArrayList<String>()
for ((id, name, packageName) in launchers) {

View File

@ -14,8 +14,6 @@ import com.simplemobiletools.applauncher.activities.SimpleActivity
import com.simplemobiletools.applauncher.dialogs.EditDialog
import com.simplemobiletools.applauncher.extensions.config
import com.simplemobiletools.applauncher.extensions.dbHelper
import com.simplemobiletools.applauncher.extensions.getLauncherDrawable
import com.simplemobiletools.applauncher.extensions.isAPredefinedApp
import com.simplemobiletools.applauncher.models.AppLauncher
import com.simplemobiletools.commons.dialogs.ConfirmationDialog
import com.simplemobiletools.commons.extensions.applyColorFilter
@ -106,6 +104,8 @@ class RecyclerAdapter(val activity: SimpleActivity, val launchers: MutableList<A
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
itemViews.put(position, holder.bindView(launchers[position], textColor, resources, packageManager))
toggleItemSelection(selectedPositions.contains(position), position)
holder.itemView.tag = holder
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
@ -157,16 +157,10 @@ class RecyclerAdapter(val activity: SimpleActivity, val launchers: MutableList<A
itemView.apply {
launcher_label.text = launcher.name
launcher_label.setTextColor(textColor)
launcher_icon.setImageDrawable(launcher.drawable!!)
setOnClickListener { viewClicked(launcher) }
setOnLongClickListener { viewLongClicked(); true }
val drawable = if (launcher.packageName.isAPredefinedApp()) {
resources.getLauncherDrawable(launcher.packageName)
} else {
packageManager.getApplicationIcon(launcher.packageName)
}
launcher_icon.setImageDrawable(drawable)
}
return itemView
}

View File

@ -10,8 +10,6 @@ import android.view.View
import android.view.ViewGroup
import com.simplemobiletools.applauncher.R
import com.simplemobiletools.applauncher.extensions.config
import com.simplemobiletools.applauncher.extensions.getLauncherDrawable
import com.simplemobiletools.applauncher.extensions.isAPredefinedApp
import com.simplemobiletools.applauncher.models.AppLauncher
import com.simplemobiletools.commons.extensions.applyColorFilter
import com.simplemobiletools.commons.extensions.beVisibleIf
@ -59,6 +57,7 @@ class RecyclerDialogAdapter(activity: Activity, val launchers: List<AppLauncher>
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
itemViews.put(position, holder.bindView(launchers[position], textColor, resources, packageManager))
toggleItemSelection(selectedPositions.contains(position), position)
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
@ -73,16 +72,10 @@ class RecyclerDialogAdapter(activity: Activity, val launchers: List<AppLauncher>
itemView.apply {
launcher_label.text = launcher.name
launcher_label.setTextColor(textColor)
launcher_icon.setImageDrawable(launcher.drawable!!)
setOnClickListener { viewClicked() }
setOnLongClickListener { viewClicked(); true }
val drawable = if (launcher.packageName.isAPredefinedApp()) {
resources.getLauncherDrawable(launcher.packageName)
} else {
packageManager.getApplicationIcon(launcher.packageName)
}
launcher_icon.setImageDrawable(drawable)
}
return itemView
}

View File

@ -1,37 +0,0 @@
package com.simplemobiletools.applauncher.dialogs
import android.app.Activity
import android.support.v7.app.AlertDialog
import android.view.ViewGroup
import com.simplemobiletools.applauncher.R
import com.simplemobiletools.applauncher.adapters.RecyclerDialogAdapter
import com.simplemobiletools.applauncher.extensions.dbHelper
import com.simplemobiletools.applauncher.models.AppLauncher
import com.simplemobiletools.commons.extensions.setupDialogStuff
import kotlinx.android.synthetic.main.dialog_pick_launchers.view.*
import java.util.*
class AddAppDialog(val activity: Activity, val availableLaunchers: ArrayList<AppLauncher>, val callback: () -> Unit) {
var dialog: AlertDialog
var view = (activity.layoutInflater.inflate(R.layout.dialog_pick_launchers, null) as ViewGroup)
lateinit var adapter: RecyclerDialogAdapter
init {
dialog = AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok, { dialogInterface, i -> confirmSelection() })
.setNegativeButton(R.string.cancel, null)
.create().apply {
activity.setupDialogStuff(view, this)
adapter = RecyclerDialogAdapter(activity, availableLaunchers)
view.pick_launchers_holder.adapter = adapter
}
}
private fun confirmSelection() {
adapter.getSelectedLaunchers().forEach {
activity.dbHelper.insertAppLauncher(it)
}
callback()
dialog.dismiss()
}
}

View File

@ -0,0 +1,73 @@
package com.simplemobiletools.applauncher.dialogs
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.support.v7.app.AlertDialog
import android.view.ViewGroup
import com.simplemobiletools.applauncher.R
import com.simplemobiletools.applauncher.adapters.RecyclerDialogAdapter
import com.simplemobiletools.applauncher.extensions.dbHelper
import com.simplemobiletools.applauncher.extensions.getLauncherDrawable
import com.simplemobiletools.applauncher.extensions.isAPredefinedApp
import com.simplemobiletools.applauncher.models.AppLauncher
import com.simplemobiletools.commons.extensions.setupDialogStuff
import kotlinx.android.synthetic.main.dialog_pick_launchers.view.*
import java.util.*
class AddAppLauncherDialog(val activity: Activity, val displayedLaunchers: ArrayList<AppLauncher>, val callback: () -> Unit) {
private var dialog: AlertDialog
private var view = (activity.layoutInflater.inflate(R.layout.dialog_pick_launchers, null) as ViewGroup)
private var adapter: RecyclerDialogAdapter? = null
init {
dialog = AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok, { dialogInterface, i -> confirmSelection() })
.setNegativeButton(R.string.cancel, null)
.create().apply {
activity.setupDialogStuff(view, this)
Thread({
adapter = RecyclerDialogAdapter(activity, getNotDisplayedLaunchers())
activity.runOnUiThread {
view.pick_launchers_holder.adapter = adapter
}
}).start()
}
}
private fun confirmSelection() {
adapter?.getSelectedLaunchers()?.forEach {
activity.dbHelper.insertAppLauncher(it)
}
callback()
dialog.dismiss()
}
private fun getNotDisplayedLaunchers(): ArrayList<AppLauncher> {
val resources = activity.resources
val packageManager = activity.packageManager
val allApps = ArrayList<AppLauncher>()
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
val drawable = if (packageName.isAPredefinedApp()) {
resources.getLauncherDrawable(packageName)
} else {
packageManager.getApplicationIcon(packageName)
}
allApps.add(AppLauncher(0, label, componentInfo.packageName, drawable))
}
val sorted = allApps.sortedWith(compareBy { it.name.toLowerCase() })
val unique = sorted.distinctBy { it.packageName }
val filtered = unique.filter { !displayedLaunchers.contains(it) && it.packageName != "com.simplemobiletools.applauncher" }
return filtered as ArrayList<AppLauncher>
}
}

View File

@ -6,6 +6,8 @@ import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import android.text.TextUtils
import com.simplemobiletools.applauncher.R
import com.simplemobiletools.applauncher.extensions.getLauncherDrawable
import com.simplemobiletools.applauncher.extensions.isAPredefinedApp
import com.simplemobiletools.applauncher.models.AppLauncher
import com.simplemobiletools.commons.extensions.getIntValue
import com.simplemobiletools.commons.extensions.getStringValue
@ -15,7 +17,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
private val MAIN_TABLE_NAME = "launchers"
private val COL_ID = "id"
private val COL_NAME = "name"
private val COL_PKG_NAME = "pkgName"
private val COL_PKG_NAME = "package_name"
private val COL_POSITION = "position"
private val mDb = writableDatabase
@ -95,6 +97,8 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
}
fun getLaunchers(): ArrayList<AppLauncher> {
val resources = context.resources
val packageManager = context.packageManager
val launchers = ArrayList<AppLauncher>()
val cols = arrayOf(COL_ID, COL_NAME, COL_PKG_NAME)
val cursor = mDb.query(MAIN_TABLE_NAME, cols, null, null, null, null, COL_NAME)
@ -102,8 +106,15 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
while (cursor.moveToNext()) {
val id = cursor.getIntValue(COL_ID)
val name = cursor.getStringValue(COL_NAME)
val pkgName = cursor.getStringValue(COL_PKG_NAME)
val launcher = AppLauncher(id, name, pkgName)
val packageName = cursor.getStringValue(COL_PKG_NAME)
val drawable = if (packageName.isAPredefinedApp()) {
resources.getLauncherDrawable(packageName)
} else {
packageManager.getApplicationIcon(packageName)
}
val launcher = AppLauncher(id, name, packageName, drawable)
launchers.add(launcher)
}
}

View File

@ -1,6 +1,8 @@
package com.simplemobiletools.applauncher.models
data class AppLauncher(val id: Int, var name: String, val packageName: String) {
import android.graphics.drawable.Drawable
data class AppLauncher(val id: Int, var name: String, val packageName: String, val drawable: Drawable? = null) {
override fun equals(other: Any?): Boolean {
return packageName.equals((other as AppLauncher).packageName, true)
}

View File

@ -13,7 +13,7 @@
<ImageView
android:id="@+id/launcher_icon"
android:layout_width="match_parent"
android:layout_height="@dimen/icon_size_smaller"
android:layout_height="@dimen/launcher_icon_size"
android:layout_gravity="center"/>
<TextView
@ -32,7 +32,6 @@
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_margin="@dimen/small_margin"
android:background="@drawable/circle_background"
android:padding="@dimen/tiny_margin"
android:src="@drawable/ic_check"

View File

@ -4,8 +4,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/pick_launchers_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:padding="@dimen/medium_margin"
android:layout_height="wrap_content"
android:scrollbars="vertical"
app:layoutManager="android.support.v7.widget.GridLayoutManager"
app:spanCount="@integer/columns"/>

View File

@ -1,3 +1,3 @@
<resources>
<dimen name="icon_size_smaller">50dp</dimen>
<dimen name="launcher_icon_size">50dp</dimen>
</resources>