Skip to content

Commit

Permalink
Fix SysTempDir for Android API 15 and below
Browse files Browse the repository at this point in the history
  • Loading branch information
05nelsonm committed Mar 2, 2024
1 parent d418501 commit dd1ccc6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2024 Matthew Nelson
*
* 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.matthewnelson.kmp.file.test.android

import android.app.Application
import androidx.test.core.app.ApplicationProvider
import io.matthewnelson.kmp.file.SysTempDir
import kotlin.test.Test
import kotlin.test.assertEquals

class SysTempDirAndroidTest {

private val ctx = ApplicationProvider.getApplicationContext<Application>()

@Test
fun givenAndroidRuntime_whenSysTempDir_thenIsSameAsCacheDir() {
assertEquals(ctx.cacheDir, SysTempDir)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
**/
package io.matthewnelson.kmp.file.internal

import io.matthewnelson.kmp.file.ANDROID
import io.matthewnelson.kmp.file.IOException
import io.matthewnelson.kmp.file.toFile
import java.io.File
import kotlin.io.resolve
import kotlin.io.readText as _readText
import kotlin.io.readBytes as _readBytes
import kotlin.io.resolve as _resolve
Expand All @@ -27,10 +29,46 @@ import kotlin.io.writeText as _writeText
@Suppress("NOTHING_TO_INLINE")
internal actual inline fun platformDirSeparator(): Char = File.separatorChar

@Suppress("NOTHING_TO_INLINE")
internal actual inline fun platformTempDirectory(): File = System
.getProperty("java.io.tmpdir")
.toFile()
@Suppress("NOTHING_TO_INLINE", "SdCardPath")
internal actual inline fun platformTempDirectory(): File {
val jTemp = System
.getProperty("java.io.tmpdir")
.toFile()

return ANDROID.SDK_INT?.let { sdk ->
if (sdk > 15) return@let null

// java.io.tmpdir=/sdcard
if (
!jTemp.path.startsWith("/data")
|| jTemp.path.startsWith("/sdcard")
) {
// dexmaker.dexcache=/data/data/com.example.app/app_dxmaker_cache
val parent = System.getProperty("dexmaker.dexcache")
?.toFile()
?.parentFile
?: return@let null

try {
if (!parent.exists()) return@let null
} catch (_: Throwable) {
return@let null
}

val cacheDir = parent.resolve("cache")

try {
if (!cacheDir.exists() && !cacheDir.mkdirs()) return@let null
} catch (_: Throwable) {
return@let null
}

cacheDir
} else {
null
}
} ?: jTemp
}

internal actual val IsWindows: Boolean = System.getProperty("os.name")
?.ifBlank { null }
Expand Down

0 comments on commit dd1ccc6

Please sign in to comment.