aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/ca/allanwang/kau/utils/PackageUtils.kt
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2017-07-04 16:08:03 -0700
committerAllan Wang <me@allanwang.ca>2017-07-04 16:08:03 -0700
commitcf2a7fcd0880a8d276970124cdb5d5845d5631fe (patch)
treecc38ead7853ddb85c9c988e94a4af605e1e676f8 /core/src/main/kotlin/ca/allanwang/kau/utils/PackageUtils.kt
parentfe4632c34a1d671503e0242a269865b884545e13 (diff)
downloadkau-cf2a7fcd0880a8d276970124cdb5d5845d5631fe.tar.gz
kau-cf2a7fcd0880a8d276970124cdb5d5845d5631fe.tar.bz2
kau-cf2a7fcd0880a8d276970124cdb5d5845d5631fe.zip
Separate core components in its own module
Diffstat (limited to 'core/src/main/kotlin/ca/allanwang/kau/utils/PackageUtils.kt')
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/utils/PackageUtils.kt48
1 files changed, 48 insertions, 0 deletions
diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/PackageUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/PackageUtils.kt
new file mode 100644
index 0000000..837c209
--- /dev/null
+++ b/core/src/main/kotlin/ca/allanwang/kau/utils/PackageUtils.kt
@@ -0,0 +1,48 @@
+package ca.allanwang.kau.utils
+
+import android.content.Context
+import android.content.pm.PackageManager
+import android.os.Build
+import android.support.annotation.RequiresApi
+
+/**
+ * Created by Allan Wang on 2017-06-23.
+ */
+
+/**
+ * Checks if a given package is installed
+ * @param packageName packageId
+ * @return true if installed with activity, false otherwise
+ */
+@KauUtils fun Context.isAppInstalled(packageName: String): Boolean {
+ val pm = packageManager
+ var installed: Boolean
+ try {
+ pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES)
+ installed = true
+ } catch (e: PackageManager.NameNotFoundException) {
+ installed = false
+ }
+ return installed
+}
+
+val buildIsLollipopAndUp: Boolean
+ get() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
+
+val buildIsMarshmallowAndUp: Boolean
+ get() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
+
+val buildIsNougatAndUp: Boolean
+ get() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
+
+const val INSTALLER_GOOGLE_PLAY_VENDING = "com.android.vending"
+const val INSTALLER_GOOGLE_PLAY_FEEDBACK = "com.google.android.feedback"
+
+val Context.installerPackageName: String?
+ get() = packageManager.getInstallerPackageName(packageName)
+
+val Context.isFromGooglePlay: Boolean
+ get() {
+ val installer = installerPackageName
+ return arrayOf(INSTALLER_GOOGLE_PLAY_FEEDBACK, INSTALLER_GOOGLE_PLAY_VENDING).any { it == installer }
+ } \ No newline at end of file