-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use
androidx.startup
to find default DatabasesDir
for Android Run…
…time (#95)
- Loading branch information
Showing
10 changed files
with
174 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...ry/driver/src/androidMain/kotlin/io/toxicity/sqlite/mc/driver/internal/AndroidPlatform.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") } | ||
} | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...river/src/androidMain/kotlin/io/toxicity/sqlite/mc/driver/internal/SqliteMCInitializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* 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 android.content.Context | ||
import androidx.startup.AppInitializer | ||
import androidx.startup.Initializer | ||
import java.io.File | ||
|
||
internal class SqliteMCInitializer: Initializer<SqliteMCInitializer.Impl> { | ||
|
||
internal class Impl private constructor() { | ||
|
||
internal companion object { | ||
|
||
@JvmField | ||
internal val INSTANCE: Impl = Impl() | ||
|
||
@JvmStatic | ||
internal fun Context.databasesDirFile(): File { | ||
val path = applicationContext.getDatabasePath("d") | ||
return path.parentFile ?: File(path.path.dropLast(1)) | ||
} | ||
} | ||
|
||
@get:JvmName("databasesDir") | ||
internal var databasesDir: File? = null | ||
private set | ||
|
||
@get:JvmName("isInitialized") | ||
internal val isInitialized: Boolean get() = databasesDir != null | ||
|
||
@JvmSynthetic | ||
internal fun init(context: Context) { | ||
databasesDir = context.databasesDirFile() | ||
} | ||
} | ||
|
||
override fun create(context: Context): Impl { | ||
val appInitializer = AppInitializer.getInstance(context) | ||
check(appInitializer.isEagerlyInitialized(javaClass)) { | ||
""" | ||
SqliteMCInitializer cannot be initialized lazily. | ||
Please ensure that you have: | ||
<meta-data | ||
android:name='io.toxicity.sqlite.mc.driver.internal.SqliteMCInitializer' | ||
android:value='androidx.startup' /> | ||
under InitializationProvider in your AndroidManifest.xml | ||
""".trimIndent() | ||
} | ||
Impl.INSTANCE.init(context) | ||
return Impl.INSTANCE | ||
} | ||
|
||
override fun dependencies(): List<Class<out Initializer<*>>> = emptyList() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...ver/src/jvmAndroidMain/kotlin/io/toxicity/sqlite/mc/driver/internal/JvmAndroidPlatform.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? |
24 changes: 24 additions & 0 deletions
24
library/driver/src/jvmMain/kotlin/io/toxicity/sqlite/mc/driver/internal/JvmPlatform.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") } | ||
} |