From 28221ee1721d26caff85c1b396e4e9fdf327cb09 Mon Sep 17 00:00:00 2001 From: sim Date: Mon, 28 Oct 2024 11:16:07 +0000 Subject: [PATCH] Fix application package name following AND_3 --- .../nextpush/utils/PackageUtils.kt | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/utils/PackageUtils.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/utils/PackageUtils.kt index beabae7..e63dba3 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/utils/PackageUtils.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/utils/PackageUtils.kt @@ -1,6 +1,7 @@ package org.unifiedpush.distributor.nextpush.utils import android.content.Context +import android.content.pm.ApplicationInfo import android.content.pm.PackageManager import android.os.Build import android.util.Log @@ -9,19 +10,23 @@ private const val U_TAG = "PackageUtils" fun Context.getApplicationName(packageId: String): String? { try { - val ai = if (Build.VERSION.SDK_INT >= 33) { - this.packageManager.getApplicationInfo( - packageId, - PackageManager.ApplicationInfoFlags.of( - PackageManager.GET_META_DATA.toLong() - ) - ) - } else { - this.packageManager.getApplicationInfo(packageId, 0) - } + val ai = appInfoForMetadata(packageId) return this.packageManager.getApplicationLabel(ai).toString() } catch (e: PackageManager.NameNotFoundException) { Log.e(U_TAG, "Could not resolve app name", e) return null } } + +fun Context.appInfoForMetadata(packageId: String): ApplicationInfo { + return if (Build.VERSION.SDK_INT >= 33) { + this.packageManager.getApplicationInfo( + packageId, + PackageManager.ApplicationInfoFlags.of( + PackageManager.GET_META_DATA.toLong() + ) + ) + } else { + this.packageManager.getApplicationInfo(packageId, 0) + } +}