From d770f8b1de561bedc41ec599976fb6e5fdaaf04a Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto <5731772+marandaneto@users.noreply.github.com> Date: Fri, 11 Oct 2024 09:58:34 +0200 Subject: [PATCH] chore: expose getApplicationInfo (#187) --- posthog-android/api/posthog-android.api | 4 ++++ .../android/internal/PostHogAndroidUtils.kt | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/posthog-android/api/posthog-android.api b/posthog-android/api/posthog-android.api index 71c994df..0e0d24e9 100644 --- a/posthog-android/api/posthog-android.api +++ b/posthog-android/api/posthog-android.api @@ -41,6 +41,10 @@ public final class com/posthog/android/internal/MainHandler { public final fun getMainLooper ()Landroid/os/Looper; } +public final class com/posthog/android/internal/PostHogAndroidUtilsKt { + public static final fun getApplicationInfo (Landroid/content/Context;)Landroid/content/pm/ApplicationInfo; +} + public abstract interface class com/posthog/android/replay/PostHogDrawableConverter { public abstract fun convert (Landroid/graphics/drawable/Drawable;)Landroid/graphics/Bitmap; } diff --git a/posthog-android/src/main/java/com/posthog/android/internal/PostHogAndroidUtils.kt b/posthog-android/src/main/java/com/posthog/android/internal/PostHogAndroidUtils.kt index c106ca9e..63ae20ad 100644 --- a/posthog-android/src/main/java/com/posthog/android/internal/PostHogAndroidUtils.kt +++ b/posthog-android/src/main/java/com/posthog/android/internal/PostHogAndroidUtils.kt @@ -5,6 +5,7 @@ import android.annotation.SuppressLint import android.app.Activity import android.content.Context import android.content.Intent +import android.content.pm.ApplicationInfo import android.content.pm.PackageInfo import android.content.pm.PackageManager import android.content.pm.PackageManager.GET_META_DATA @@ -16,6 +17,7 @@ import android.os.Process import android.telephony.TelephonyManager import android.util.DisplayMetrics import android.view.WindowManager +import com.posthog.PostHogInternal import com.posthog.android.PostHogAndroidConfig @Suppress("DEPRECATION") @@ -186,3 +188,20 @@ internal fun String.tryParse(config: PostHogAndroidConfig): Uri? { internal fun isMainThread(mainHandler: MainHandler): Boolean { return Thread.currentThread().id == mainHandler.mainLooper.thread.id } + +@PostHogInternal +@Suppress("DEPRECATION") +@Throws(PackageManager.NameNotFoundException::class) +public fun getApplicationInfo(context: Context): ApplicationInfo = + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + context + .packageManager + .getApplicationInfo( + context.packageName, + PackageManager.ApplicationInfoFlags.of(GET_META_DATA.toLong()), + ) + } else { + context + .packageManager + .getApplicationInfo(context.packageName, GET_META_DATA) + }