From e5f4c99e20e6bdf11c6406fe31cb67ab221dc85b Mon Sep 17 00:00:00 2001 From: Tuan Nguyen Date: Tue, 18 Mar 2025 15:23:30 +0700 Subject: [PATCH 1/3] Implement function to check for package installation. --- .../java/ai/elimu/common/utils/ContextExt.kt | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 utils/src/main/java/ai/elimu/common/utils/ContextExt.kt diff --git a/utils/src/main/java/ai/elimu/common/utils/ContextExt.kt b/utils/src/main/java/ai/elimu/common/utils/ContextExt.kt new file mode 100644 index 0000000..8733d32 --- /dev/null +++ b/utils/src/main/java/ai/elimu/common/utils/ContextExt.kt @@ -0,0 +1,40 @@ +package ai.elimu.common.utils + +import android.content.Context +import android.content.Intent +import android.content.pm.PackageInfo +import android.content.pm.PackageManager +import android.util.Log +import androidx.appcompat.app.AlertDialog + +fun Context.isPackageInstalled(packageName: String, + launchPackage: String, + launchClass: String, + dialogMessage: String, + buttonText: String): Boolean { + try { + val packageInfoAppstore: PackageInfo = + packageManager.getPackageInfo(packageName, 0) + Log.i("isPackageInstalled", "packageInfoAppstore.versionCode: " + packageInfoAppstore.versionCode) + return true + } catch (e: PackageManager.NameNotFoundException) { + Log.e("isPackageInstalled","getPackageInfo exception: " + e.message) + AlertDialog.Builder(this) + .setMessage(dialogMessage) + .setPositiveButton(buttonText + ) { _, _ -> + val openProviderIntent = Intent().apply { + setClassName(launchPackage, launchClass) + } + try { + startActivity(openProviderIntent) + } catch (e: Exception) { + e.printStackTrace() + Log.e("isPackageInstalled", "startActivity exception: " + e.message) + } + } + .setCancelable(false) + .create().show() + return false + } +} \ No newline at end of file From 7e45ea1a0506db978564eb999f1bec66a146bfb9 Mon Sep 17 00:00:00 2001 From: Tuan Nguyen Date: Tue, 18 Mar 2025 15:26:13 +0700 Subject: [PATCH 2/3] Implement ext functions to parse data from Bundle --- .../java/ai/elimu/common/utils/BundleExt.kt | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 utils/src/main/java/ai/elimu/common/utils/BundleExt.kt diff --git a/utils/src/main/java/ai/elimu/common/utils/BundleExt.kt b/utils/src/main/java/ai/elimu/common/utils/BundleExt.kt new file mode 100644 index 0000000..4c10bfa --- /dev/null +++ b/utils/src/main/java/ai/elimu/common/utils/BundleExt.kt @@ -0,0 +1,23 @@ +package ai.elimu.common.utils + +import android.os.Build +import android.os.Bundle + +fun Bundle?.getParcelableCompat(key: String, clazz: Class): T? { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + this?.getParcelable(key, clazz) + } else { + @Suppress("DEPRECATION") + this?.getParcelable(key) + } +} + +fun Bundle?.getParcelableArrayListCompat(key: String, clazz: Class) + : java.util.ArrayList? { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + this?.getParcelableArrayList(key, clazz) + } else { + @Suppress("DEPRECATION") + this?.getParcelableArrayList(key) + } +} \ No newline at end of file From bc6828fcb47cb617ede6b287c0cae6310562b5a1 Mon Sep 17 00:00:00 2001 From: Tuan Nguyen Date: Tue, 18 Mar 2025 15:35:46 +0700 Subject: [PATCH 3/3] Add KDoc for BundleExt --- .../main/java/ai/elimu/common/utils/BundleExt.kt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/utils/src/main/java/ai/elimu/common/utils/BundleExt.kt b/utils/src/main/java/ai/elimu/common/utils/BundleExt.kt index 4c10bfa..8e20d0c 100644 --- a/utils/src/main/java/ai/elimu/common/utils/BundleExt.kt +++ b/utils/src/main/java/ai/elimu/common/utils/BundleExt.kt @@ -3,6 +3,13 @@ package ai.elimu.common.utils import android.os.Build import android.os.Bundle +/** + * Gets a parcelable object from the Bundle in a way that's compatible with all Android API levels. + * + * @param key The key to retrieve the parcelable object. + * @param clazz The class of the parcelable object. + * @return The parcelable object, or null if not found. + */ fun Bundle?.getParcelableCompat(key: String, clazz: Class): T? { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { this?.getParcelable(key, clazz) @@ -12,6 +19,13 @@ fun Bundle?.getParcelableCompat(key: String, clazz: Class): T? { } } +/** + * Gets an ArrayList of parcelable objects from the Bundle in a way that's compatible with all Android API levels. + * + * @param key The key to retrieve the ArrayList. + * @param clazz The class of the parcelable objects in the ArrayList. + * @return The ArrayList of parcelable objects, or null if not found. + */ fun Bundle?.getParcelableArrayListCompat(key: String, clazz: Class) : java.util.ArrayList? { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {