Implemented determining OS type

This commit is contained in:
dankito 2020-12-06 21:03:40 +01:00
parent d38f7b224b
commit eac1d1da40
5 changed files with 76 additions and 0 deletions

View File

@ -0,0 +1,19 @@
package net.dankito.utils.multiplatform.os
open class Os(open val osHelper: OsHelper = OsHelper()) {
open val osType: OsType
get() = osHelper.osType
open val isRunningOnJvm: Boolean
get() = osType == OsType.JVM
open val isRunningOnAndroid: Boolean
get() = osType == OsType.Android
open val isRunningOniOS: Boolean
get() = osType == OsType.iOS
}

View File

@ -0,0 +1,8 @@
package net.dankito.utils.multiplatform.os
expect class OsHelper {
val osType: OsType
}

View File

@ -0,0 +1,12 @@
package net.dankito.utils.multiplatform.os
enum class OsType {
JVM,
Android,
iOS
}

View File

@ -0,0 +1,8 @@
package net.dankito.utils.multiplatform.os
actual class OsHelper {
actual val osType = OsType.iOS
}

View File

@ -0,0 +1,29 @@
package net.dankito.utils.multiplatform.os
actual open class OsHelper {
actual val osType: OsType
get() {
return if (isRunningOnAndroid) {
OsType.Android
}
else {
OsType.JVM
}
}
open val isRunningOnAndroid: Boolean = determineIsRunningOnAndroid()
protected open fun determineIsRunningOnAndroid(): Boolean {
try {
Class.forName("android.app.Activity")
return true
} catch (ex: Exception) { }
return false
}
}