Skip to content

Commit e5f4c99

Browse files
authored
Implement function to check for package installation.
1 parent eb4c6ad commit e5f4c99

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package ai.elimu.common.utils
2+
3+
import android.content.Context
4+
import android.content.Intent
5+
import android.content.pm.PackageInfo
6+
import android.content.pm.PackageManager
7+
import android.util.Log
8+
import androidx.appcompat.app.AlertDialog
9+
10+
fun Context.isPackageInstalled(packageName: String,
11+
launchPackage: String,
12+
launchClass: String,
13+
dialogMessage: String,
14+
buttonText: String): Boolean {
15+
try {
16+
val packageInfoAppstore: PackageInfo =
17+
packageManager.getPackageInfo(packageName, 0)
18+
Log.i("isPackageInstalled", "packageInfoAppstore.versionCode: " + packageInfoAppstore.versionCode)
19+
return true
20+
} catch (e: PackageManager.NameNotFoundException) {
21+
Log.e("isPackageInstalled","getPackageInfo exception: " + e.message)
22+
AlertDialog.Builder(this)
23+
.setMessage(dialogMessage)
24+
.setPositiveButton(buttonText
25+
) { _, _ ->
26+
val openProviderIntent = Intent().apply {
27+
setClassName(launchPackage, launchClass)
28+
}
29+
try {
30+
startActivity(openProviderIntent)
31+
} catch (e: Exception) {
32+
e.printStackTrace()
33+
Log.e("isPackageInstalled", "startActivity exception: " + e.message)
34+
}
35+
}
36+
.setCancelable(false)
37+
.create().show()
38+
return false
39+
}
40+
}

0 commit comments

Comments
 (0)