From eac1d1da4032b4541ad23009d0b8994f8ae24282 Mon Sep 17 00:00:00 2001 From: dankito Date: Sun, 6 Dec 2020 21:03:40 +0100 Subject: [PATCH] Implemented determining OS type --- .../net/dankito/utils/multiplatform/os/Os.kt | 19 ++++++++++++ .../utils/multiplatform/os/OsHelper.kt | 8 +++++ .../dankito/utils/multiplatform/os/OsType.kt | 12 ++++++++ .../utils/multiplatform/os/OsHelper.kt | 8 +++++ .../utils/multiplatform/os/OsHelper.kt | 29 +++++++++++++++++++ 5 files changed, 76 insertions(+) create mode 100644 common/src/commonMain/kotlin/net/dankito/utils/multiplatform/os/Os.kt create mode 100644 common/src/commonMain/kotlin/net/dankito/utils/multiplatform/os/OsHelper.kt create mode 100644 common/src/commonMain/kotlin/net/dankito/utils/multiplatform/os/OsType.kt create mode 100644 common/src/iosMain/kotlin/net/dankito/utils/multiplatform/os/OsHelper.kt create mode 100644 common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/os/OsHelper.kt diff --git a/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/os/Os.kt b/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/os/Os.kt new file mode 100644 index 00000000..58959f5e --- /dev/null +++ b/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/os/Os.kt @@ -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 + +} \ No newline at end of file diff --git a/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/os/OsHelper.kt b/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/os/OsHelper.kt new file mode 100644 index 00000000..3d713667 --- /dev/null +++ b/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/os/OsHelper.kt @@ -0,0 +1,8 @@ +package net.dankito.utils.multiplatform.os + + +expect class OsHelper { + + val osType: OsType + +} \ No newline at end of file diff --git a/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/os/OsType.kt b/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/os/OsType.kt new file mode 100644 index 00000000..3fe3d65f --- /dev/null +++ b/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/os/OsType.kt @@ -0,0 +1,12 @@ +package net.dankito.utils.multiplatform.os + + +enum class OsType { + + JVM, + + Android, + + iOS + +} \ No newline at end of file diff --git a/common/src/iosMain/kotlin/net/dankito/utils/multiplatform/os/OsHelper.kt b/common/src/iosMain/kotlin/net/dankito/utils/multiplatform/os/OsHelper.kt new file mode 100644 index 00000000..70cd2db3 --- /dev/null +++ b/common/src/iosMain/kotlin/net/dankito/utils/multiplatform/os/OsHelper.kt @@ -0,0 +1,8 @@ +package net.dankito.utils.multiplatform.os + + +actual class OsHelper { + + actual val osType = OsType.iOS + +} \ No newline at end of file diff --git a/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/os/OsHelper.kt b/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/os/OsHelper.kt new file mode 100644 index 00000000..126f7286 --- /dev/null +++ b/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/os/OsHelper.kt @@ -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 + } + +} \ No newline at end of file