From 639c5701501e5c427cec06b016589a29894bab9e Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 2 Jun 2022 15:23:22 +0200 Subject: [PATCH] Create extension to get app names --- .../vector/app/core/pushers/UnifiedPushHelper.kt | 16 +++------------- .../vector/app/core/resources/AppNameProvider.kt | 5 ++--- .../java/im/vector/app/core/utils/SystemUtils.kt | 13 +++++++++++++ 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/vector/src/main/java/im/vector/app/core/pushers/UnifiedPushHelper.kt b/vector/src/main/java/im/vector/app/core/pushers/UnifiedPushHelper.kt index 53f2045249..88646a778d 100644 --- a/vector/src/main/java/im/vector/app/core/pushers/UnifiedPushHelper.kt +++ b/vector/src/main/java/im/vector/app/core/pushers/UnifiedPushHelper.kt @@ -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 { diff --git a/vector/src/main/java/im/vector/app/core/resources/AppNameProvider.kt b/vector/src/main/java/im/vector/app/core/resources/AppNameProvider.kt index 90558e35b7..3b6a8b595c 100644 --- a/vector/src/main/java/im/vector/app/core/resources/AppNameProvider.kt +++ b/vector/src/main/java/im/vector/app/core/resources/AppNameProvider.kt @@ -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())) { diff --git a/vector/src/main/java/im/vector/app/core/utils/SystemUtils.kt b/vector/src/main/java/im/vector/app/core/utils/SystemUtils.kt index 1939bdf6a9..bb38411980 100644 --- a/vector/src/main/java/im/vector/app/core/utils/SystemUtils.kt +++ b/vector/src/main/java/im/vector/app/core/utils/SystemUtils.kt @@ -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).