From a71fe022d3aa25a14c634b9e2e7a9418001b3f9b Mon Sep 17 00:00:00 2001 From: Matthew Nelson Date: Fri, 1 Dec 2023 15:43:19 -0500 Subject: [PATCH] Use androidx.startup library to obtain default databases directory for Android Runtime --- .../mc/driver/internal/AndroidPlatform.kt | 34 ++++++++++++++ .../sqlite/mc/driver/config/DatabasesDir.kt | 3 +- .../sqlite/mc/driver/config/DatabasesDir.kt | 46 ++----------------- .../mc/driver/internal/JvmAndroidPlatform.kt | 20 ++++++++ .../sqlite/mc/driver/internal/JvmPlatform.kt | 24 ++++++++++ 5 files changed, 84 insertions(+), 43 deletions(-) create mode 100644 library/driver/src/androidMain/kotlin/io/toxicity/sqlite/mc/driver/internal/AndroidPlatform.kt create mode 100644 library/driver/src/jvmAndroidMain/kotlin/io/toxicity/sqlite/mc/driver/internal/JvmAndroidPlatform.kt create mode 100644 library/driver/src/jvmMain/kotlin/io/toxicity/sqlite/mc/driver/internal/JvmPlatform.kt diff --git a/library/driver/src/androidMain/kotlin/io/toxicity/sqlite/mc/driver/internal/AndroidPlatform.kt b/library/driver/src/androidMain/kotlin/io/toxicity/sqlite/mc/driver/internal/AndroidPlatform.kt new file mode 100644 index 0000000..ad93543 --- /dev/null +++ b/library/driver/src/androidMain/kotlin/io/toxicity/sqlite/mc/driver/internal/AndroidPlatform.kt @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Toxicity + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **/ +package io.toxicity.sqlite.mc.driver.internal + +import java.io.File + +internal actual val DEFAULT_DATABASES_DIR: File? by lazy { + SqliteMCInitializer.Impl.INSTANCE.databasesDir ?: run { + + if (System.getProperty("java.runtime.name")?.contains("android", ignoreCase = true) == true) { + // It's android runtime and androidx.startup was not initialized... something + // is very wrong. Set to null so exception is lazily thrown. + null + } else { + // Android unit tests. Default to temporary directory for the host machine + System.getProperty("java.io.tmpdir") + ?.ifBlank { null } + ?.let { tmp -> File(tmp).resolve("sqlite_mc").resolve(".databases") } + } + } +} diff --git a/library/driver/src/commonMain/kotlin/io/toxicity/sqlite/mc/driver/config/DatabasesDir.kt b/library/driver/src/commonMain/kotlin/io/toxicity/sqlite/mc/driver/config/DatabasesDir.kt index f70b791..b5bc7b4 100644 --- a/library/driver/src/commonMain/kotlin/io/toxicity/sqlite/mc/driver/config/DatabasesDir.kt +++ b/library/driver/src/commonMain/kotlin/io/toxicity/sqlite/mc/driver/config/DatabasesDir.kt @@ -33,7 +33,8 @@ import kotlin.jvm.JvmField * to not throw exception when [DatabasesDir] is instantiated. * * Default Locations: - * - Android: `/data/user/{userid}/{your.application.id}/databases/` + * - Android Runtime: `/data/user/{userid}/{your.application.id}/databases/` + * - Android Unit Tests: `{tmp dir}/sqlite_mc/.databases` * - Jvm - Unix: `~/.databases/` * - Jvm - Mingw: `{drive}:\Users\{username}\.databases\` * - Native - Apple: `~/Documents/databases/` diff --git a/library/driver/src/jvmAndroidMain/kotlin/io/toxicity/sqlite/mc/driver/config/DatabasesDir.kt b/library/driver/src/jvmAndroidMain/kotlin/io/toxicity/sqlite/mc/driver/config/DatabasesDir.kt index 46064ed..d035408 100644 --- a/library/driver/src/jvmAndroidMain/kotlin/io/toxicity/sqlite/mc/driver/config/DatabasesDir.kt +++ b/library/driver/src/jvmAndroidMain/kotlin/io/toxicity/sqlite/mc/driver/config/DatabasesDir.kt @@ -18,6 +18,7 @@ package io.toxicity.sqlite.mc.driver.config import io.toxicity.sqlite.mc.driver.SQLiteMCDriver +import io.toxicity.sqlite.mc.driver.internal.DEFAULT_DATABASES_DIR import java.io.File import java.nio.file.Path import kotlin.io.path.pathString @@ -34,7 +35,8 @@ import kotlin.io.path.pathString * to not throw exception when [DatabasesDir] is instantiated. * * Default Locations: - * - Android: `/data/user/{userid}/{your.application.id}/databases/` + * - Android Runtime: `/data/user/{userid}/{your.application.id}/databases/` + * - Android Unit Tests: `{tmp dir}/sqlite_mc/.databases` * - Jvm - Unix: `~/.databases/` * - Jvm - Mingw: `{drive}:\Users\{username}\.databases\` * @@ -50,50 +52,10 @@ public actual class DatabasesDir public actual constructor(path: String?) { public actual val path: String? = if (!path.isNullOrBlank()) { File(path).absolutePath } else { - DEFAULT_DIR + DEFAULT_DATABASES_DIR?.absolutePath } public actual override fun equals(other: Any?): Boolean = other is DatabasesDir && other.path == path public actual override fun hashCode(): Int = 17 * 31 + path.hashCode() public actual override fun toString(): String = path ?: "" - - private companion object { - - private val DEFAULT_DIR: String? by lazy { - var defaultDir: File? = null - - if ( - System.getProperty("os.name")?.contains("Linux", ignoreCase = true) == true - && System.getProperty("java.vendor")?.equals("The Android Project", ignoreCase = true) == true - ) { - val cache: String? = System.getProperty("java.io.tmpdir") - - defaultDir = if ( - cache?.startsWith("/data/user/") == true - && cache.endsWith("/cache") - ) { - File(cache) - } else { - val dexcache: String? = System.getProperty("dexmaker.dexcache") - - if ( - dexcache?.startsWith("/data/user/") == true - && dexcache.endsWith("/app_dxmaker_cache") - ) { - File(dexcache) - } else { - null - } - }?.resolveSibling("databases") - } - - if (defaultDir == null) { - defaultDir = System.getProperty("user.home") - ?.ifBlank { null } - ?.let { home -> File(home).resolve(".databases") } - } - - defaultDir?.absolutePath - } - } } diff --git a/library/driver/src/jvmAndroidMain/kotlin/io/toxicity/sqlite/mc/driver/internal/JvmAndroidPlatform.kt b/library/driver/src/jvmAndroidMain/kotlin/io/toxicity/sqlite/mc/driver/internal/JvmAndroidPlatform.kt new file mode 100644 index 0000000..34c146b --- /dev/null +++ b/library/driver/src/jvmAndroidMain/kotlin/io/toxicity/sqlite/mc/driver/internal/JvmAndroidPlatform.kt @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2023 Toxicity + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **/ +package io.toxicity.sqlite.mc.driver.internal + +import java.io.File + +internal expect val DEFAULT_DATABASES_DIR: File? diff --git a/library/driver/src/jvmMain/kotlin/io/toxicity/sqlite/mc/driver/internal/JvmPlatform.kt b/library/driver/src/jvmMain/kotlin/io/toxicity/sqlite/mc/driver/internal/JvmPlatform.kt new file mode 100644 index 0000000..30f4ca3 --- /dev/null +++ b/library/driver/src/jvmMain/kotlin/io/toxicity/sqlite/mc/driver/internal/JvmPlatform.kt @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2023 Toxicity + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **/ +package io.toxicity.sqlite.mc.driver.internal + +import java.io.File + +internal actual val DEFAULT_DATABASES_DIR: File? by lazy { + System.getProperty("user.home") + ?.ifBlank { null } + ?.let { home -> File(home).resolve(".databases") } +}