Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions utils/src/main/java/ai/elimu/common/utils/BundleExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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 <T> Bundle?.getParcelableCompat(key: String, clazz: Class<T>): T? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
this?.getParcelable(key, clazz)
} else {
@Suppress("DEPRECATION")
this?.getParcelable(key)
}
}

/**
* 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 <T : android.os.Parcelable> Bundle?.getParcelableArrayListCompat(key: String, clazz: Class<T>)
: java.util.ArrayList<T>? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
this?.getParcelableArrayList(key, clazz)
} else {
@Suppress("DEPRECATION")
this?.getParcelableArrayList(key)
}
}
40 changes: 40 additions & 0 deletions utils/src/main/java/ai/elimu/common/utils/ContextExt.kt
Original file line number Diff line number Diff line change
@@ -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
}
}