Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 23 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,23 @@
package ai.elimu.common.utils

import android.os.Build
import android.os.Bundle

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)
}
}

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
}
}