-
Notifications
You must be signed in to change notification settings - Fork 133
/
PackageUtils.kt
60 lines (51 loc) · 1.72 KB
/
PackageUtils.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.woocommerce.android.util
import android.content.Context
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import androidx.core.content.pm.PackageInfoCompat
import com.woocommerce.android.BuildConfig
import java.util.Locale
object PackageUtils {
const val PACKAGE_VERSION_CODE_DEFAULT = -1
private var isTesting: Boolean? = null
/**
* Return true if Debug build. false otherwise.
*/
fun isDebugBuild() = BuildConfig.DEBUG
fun isTesting(): Boolean {
if (isTesting == null) {
isTesting = try {
Class.forName("org.junit.Test")
true
} catch (e: ClassNotFoundException) {
false
}
}
return isTesting!!
}
fun isBetaBuild(context: Context): Boolean {
val versionName = getVersionName(context).toLowerCase(Locale.ROOT)
return (versionName.contains("beta") || versionName.contains("rc"))
}
private fun getPackageInfo(context: Context): PackageInfo? {
return try {
val manager = context.packageManager
manager.getPackageInfo(context.packageName, 0)
} catch (e: PackageManager.NameNotFoundException) {
null
}
}
/**
* Return version code, or -1 if it can't be read
*/
fun getVersionCode(context: Context): Int {
val packageInfo = getPackageInfo(context)
return packageInfo?.let {
PackageInfoCompat.getLongVersionCode(it).toInt()
} ?: PACKAGE_VERSION_CODE_DEFAULT
}
/**
* Return version name, or the string "0" if it can't be read
*/
fun getVersionName(context: Context) = getPackageInfo(context)?.versionName ?: "0"
}