Skip to content

Commit

Permalink
Load app label and icon via resources [VPNAND-631].
Browse files Browse the repository at this point in the history
This way it is possible to release resources earlier using
Assets.close().
  • Loading branch information
msimonides-proton committed Sep 20, 2021
1 parent c429db0 commit 0715281
Showing 1 changed file with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ import android.Manifest
import android.app.ActivityManager
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.content.res.Resources
import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Process
import android.text.TextUtils
import android.view.View
import android.widget.ProgressBar
import android.widget.TextView
import androidx.core.content.res.ResourcesCompat
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
Expand All @@ -49,6 +54,9 @@ private const val MEMORY_STAT_CODE = "summary.code"
private const val GC_TIMEOUT_MS = 1000L
private const val GC_CHECK_DELAY_MS = 100L

private const val MAX_SAFE_LABEL_CHARS = 1000
private const val MAX_SAFE_LABEL_DP = 500f

@ContentLayout(R.layout.dialog_split_tunnel)
class AppsDialog : BaseDialog() {
private lateinit var adapter: AppsAdapter
Expand Down Expand Up @@ -134,11 +142,36 @@ class AppsDialog : BaseDialog() {
packageManager: PackageManager,
appInfo: ApplicationInfo
): SelectedApplicationEntry {
val label = appInfo.loadLabel(packageManager)
val icon = appInfo.loadIcon(packageManager)
val appResources = packageManager.getResourcesForApplication(appInfo)
val label = getLabel(appResources, appInfo.labelRes) ?: appInfo.packageName
val icon = getIcon(appResources, appInfo.icon) ?: packageManager.defaultActivityIcon
appResources.assets.close()
return SelectedApplicationEntry(appInfo.packageName, label.toString(), icon)
}

private fun getLabel(appResources: Resources, labelResource: Int): String? =
try {
val label = appResources.getString(labelResource)
if (Build.VERSION.SDK_INT >= 29) {
val flags = TextUtils.SAFE_STRING_FLAG_TRIM or TextUtils.SAFE_STRING_FLAG_FIRST_LINE
TextUtils
.makeSafeForPresentation(label, MAX_SAFE_LABEL_CHARS, MAX_SAFE_LABEL_DP, flags)
.toString()
} else {
label
}
} catch (e: Resources.NotFoundException) {
// For some app packages resources fail to load, as if the ID is incorrect.
null
}

private fun getIcon(appResources: Resources, iconResource: Int): Drawable? =
try {
ResourcesCompat.getDrawable(appResources, iconResource, null)
} catch (e: Resources.NotFoundException) {
null
}

@Suppress("ExplicitGarbageCollectionCall")
private suspend fun tryReleaseMemory(initialCodeSizeKb: Int) {
// Loading application metadata with loadLabel and loadIcon increases memory use in the
Expand Down

0 comments on commit 0715281

Please sign in to comment.