Skip to content

Commit

Permalink
Use androidx.startup library to obtain default databases directory fo…
Browse files Browse the repository at this point in the history
…r Android Runtime
  • Loading branch information
05nelsonm committed Dec 1, 2023
1 parent 711aa33 commit a71fe02
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -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") }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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/`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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\`
*
Expand All @@ -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
}
}
}
Original file line number Diff line number Diff line change
@@ -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?
Original file line number Diff line number Diff line change
@@ -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") }
}

0 comments on commit a71fe02

Please sign in to comment.