Skip to content

Commit

Permalink
Minor performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jahirfiquitiva committed Dec 5, 2021
1 parent 5ab23d3 commit 82a3298
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ data class RequestApp(val name: String, val packageName: String, val component:
var icon: Drawable? = null
private set

suspend fun loadIcon(context: Context?) {
fun loadIcon(context: Context?) {
context ?: return
if (icon != null) return
icon = context.getAppIcon(packageName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ class RequestsViewModel(application: Application) : AndroidViewModel(application
riPkg,
component
)
app.loadIcon(context)
installedApps.add(app)
loaded += 1
}
Expand All @@ -201,6 +200,7 @@ class RequestsViewModel(application: Application) : AndroidViewModel(application
delay(10)
val appsToRequest = loadAppsToRequest(debug)
appsToRequestData.postValue(appsToRequest)
appsToRequest.forEach { it.loadIcon(context) }
}
}

Expand Down Expand Up @@ -295,4 +295,4 @@ class RequestsViewModel(application: Application) : AndroidViewModel(application
appsToRequestData.removeObservers(owner)
selectedAppsData.removeObservers(owner)
}
}
}
37 changes: 22 additions & 15 deletions library/src/main/kotlin/dev/jahir/blueprint/extensions/AppIcon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,55 @@ import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.res.Resources
import android.graphics.drawable.Drawable
import android.os.Build
import android.util.DisplayMetrics
import androidx.annotation.DrawableRes
import androidx.core.content.res.ResourcesCompat
import dev.jahir.blueprint.R
import dev.jahir.frames.extensions.context.drawable
import dev.jahir.frames.extensions.context.string
import dev.jahir.frames.extensions.resources.hasContent


@DrawableRes
fun Context.getAppIconResId(pkg: String): Int = getAppInfo(pkg)?.icon ?: 0
fun getDefaultAppIcon(): Drawable? {
return Resources.getSystem().getAppIcon(android.R.mipmap.sym_def_app_icon)
}

@Suppress("USELESS_ELVIS")
fun Context.getAppIcon(pkg: String): Drawable? {
if (!pkg.hasContent()) return null
return try {
loadIcon(pkg)
?: drawable(getAppIconResId(pkg))
?: packageManager.getApplicationIcon(pkg)
?: drawable(string(R.string.icons_placeholder))
?: drawable(R.drawable.ic_na_launcher)
var icon: Drawable? = null
try {
icon = loadIcon(pkg)
if (icon == null) icon = packageManager.getApplicationIcon(pkg)
} catch (e: Exception) {
null
if (icon == null) icon = drawable(string(R.string.icons_placeholder))
if (icon == null) icon = drawable(R.drawable.ic_na_launcher)
if (icon == null) icon = getDefaultAppIcon()
}
return icon
}

private fun Context.loadIcon(pkg: String): Drawable? {
try {
return try {
val ai = getAppInfo(pkg)
if (ai != null) {
var icon = ai.loadIcon(packageManager)
if (icon == null) icon = getResources(ai)?.getAppIcon(ai.icon)
return icon
}
} else null
} catch (e: Exception) {
null
}
return null
}

private fun Resources.getAppIcon(iconId: Int): Drawable? {
return try {
ResourcesCompat.getDrawableForDensity(this, iconId, DisplayMetrics.DENSITY_XXXHIGH, null)
ResourcesCompat.getDrawableForDensity(
this,
iconId,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) DisplayMetrics.DENSITY_DEVICE_STABLE
else DisplayMetrics.DENSITY_DEFAULT,
null
)
} catch (e: Exception) {
null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ class IconViewHolder(itemView: View) : SectionedViewHolder(itemView) {
internal const val ICON_ANIMATION_DELAY: Long = 50L
internal const val ICON_ANIMATION_DURATION: Long = 250L
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ class RequestViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
}
}
}
}
}

0 comments on commit 82a3298

Please sign in to comment.