Create extension to get app names

This commit is contained in:
Benoit Marty 2022-06-02 15:23:22 +02:00 committed by Benoit Marty
parent fc66e5f120
commit 639c570150
3 changed files with 18 additions and 16 deletions

View File

@ -17,7 +17,6 @@
package im.vector.app.core.pushers
import android.content.Context
import android.content.pm.PackageManager
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import com.google.android.material.dialog.MaterialAlertDialogBuilder
@ -25,6 +24,7 @@ import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import im.vector.app.R
import im.vector.app.core.resources.StringProvider
import im.vector.app.core.utils.getApplicationLabel
import im.vector.app.features.VectorFeatures
import im.vector.app.features.settings.BackgroundSyncMode
import im.vector.app.features.settings.VectorPreferences
@ -143,12 +143,7 @@ class UnifiedPushHelper @Inject constructor(
if (it == context.packageName) {
internalDistributorName
} else {
try {
val ai = context.packageManager.getApplicationInfo(it, 0)
context.packageManager.getApplicationLabel(ai)
} catch (e: PackageManager.NameNotFoundException) {
it
}
context.getApplicationLabel(it)
}
}
@ -248,12 +243,7 @@ class UnifiedPushHelper @Inject constructor(
return stringProvider.getString(R.string.unifiedpush_distributor_background_sync)
}
val distributor = up.getDistributor(context)
return try {
val ai = context.packageManager.getApplicationInfo(distributor, 0)
context.packageManager.getApplicationLabel(ai).toString()
} catch (e: PackageManager.NameNotFoundException) {
distributor
}
return context.getApplicationLabel(distributor)
}
fun isEmbeddedDistributor(): Boolean {

View File

@ -17,6 +17,7 @@
package im.vector.app.core.resources
import android.content.Context
import im.vector.app.core.utils.getApplicationLabel
import timber.log.Timber
import javax.inject.Inject
@ -25,9 +26,7 @@ class AppNameProvider @Inject constructor(private val context: Context) {
fun getAppName(): String {
return try {
val appPackageName = context.applicationContext.packageName
val pm = context.packageManager
val appInfo = pm.getApplicationInfo(appPackageName, 0)
var appName = pm.getApplicationLabel(appInfo).toString()
var appName = context.getApplicationLabel(appPackageName)
// Use appPackageName instead of appName if appName contains any non-ASCII character
if (!appName.matches("\\A\\p{ASCII}*\\z".toRegex())) {

View File

@ -23,6 +23,7 @@ import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.PowerManager
@ -59,6 +60,18 @@ fun Context.isAnimationEnabled(): Boolean {
return Settings.Global.getFloat(contentResolver, Settings.Global.ANIMATOR_DURATION_SCALE, 1f) != 0f
}
/**
* Return the application label of the provided package. If not found, the package is returned.
*/
fun Context.getApplicationLabel(packageName: String): String {
return try {
val ai = packageManager.getApplicationInfo(packageName, 0)
packageManager.getApplicationLabel(ai).toString()
} catch (e: PackageManager.NameNotFoundException) {
packageName
}
}
/**
* display the system dialog for granting this permission. If previously granted, the
* system will not show it (so you should call this method).