Skip to content

Commit

Permalink
Android TV: Fix storage detection crashing
Browse files Browse the repository at this point in the history
The "Kids Account" is returned via UserManager, but we can't find a specific data area for it and end up with duplicate Android/media data areas.
  • Loading branch information
d4rken committed Aug 12, 2023
1 parent 7b25d7b commit a5a4770
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@ import android.content.Context
import android.os.Environment
import androidx.core.content.ContextCompat
import dagger.hilt.android.qualifiers.ApplicationContext
import eu.darken.sdmse.common.debug.logging.Logging.Priority.WARN
import eu.darken.sdmse.common.debug.logging.log
import eu.darken.sdmse.common.debug.logging.logTag
import eu.darken.sdmse.common.files.local.LocalPath
import eu.darken.sdmse.common.files.local.toLocalPath
import eu.darken.sdmse.common.user.UserHandle2
import eu.darken.sdmse.common.user.UserManager2
import java.io.File
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class StorageEnvironment @Inject constructor(
@ApplicationContext private val context: Context,
private val storageManager: StorageManager2,
private val userManager: UserManager2,
) {

fun getVariable(variableName: String): String? = System.getenv(variableName)
Expand Down Expand Up @@ -68,33 +73,68 @@ class StorageEnvironment @Inject constructor(
root?.let { LocalPath.build(it) }
}

fun getPublicPrimaryStorage(userHandle: UserHandle2): LocalPath {
val path = Environment.getExternalStorageDirectory()
val volume = storageManager.getStorageVolume(path)
requireNotNull(volume) { "Can't find volume for $path" }
return LocalPath.build(path)
suspend fun getPublicPrimaryStorage(userHandle: UserHandle2): LocalPath? {
val userPath = Environment.getExternalStorageDirectory().let { path ->
if (path.name.toIntOrNull() == userManager.currentUser().handle.handleId) {
// If this matches, then we can make assumptions on the path
File(path.parentFile!!, userHandle.handleId.toString())
} else {
path
}
}

val volume = storageManager.getStorageVolume(userPath)
if (volume == null) {
log(TAG, WARN) { "Can't find volume for $userPath" }
return null
}

return LocalPath.build(userPath)
}

// http://androidxref.com/5.1.1_r6/xref/frameworks/base/core/java/android/os/Environment.java#136
fun getPublicSecondaryStorage(userHandle: UserHandle2): Collection<LocalPath> {
suspend fun getPublicSecondaryStorage(userHandle: UserHandle2): Set<LocalPath> {
val primary = getPublicPrimaryStorage(userHandle)
if (primary == null) {
log(TAG, WARN) { "No primary storage? No secondary storage!" }
return emptySet()
}

val pathResult = mutableListOf<LocalPath>()
for (extMyDir in ContextCompat.getExternalFilesDirs(context, null)) {
if (extMyDir == null) continue
if (!extMyDir.isAbsolute) continue
var findRoot = extMyDir
for (i in 0..3) {
findRoot = findRoot.parentFile
if (findRoot == null) break

ContextCompat.getExternalFilesDirs(context, null)
.filterNotNull()
.filter { it.isAbsolute }
.mapNotNull { extMyDir ->
var findRoot: File? = extMyDir
for (i in 0..3) { // Android/data/pkg
findRoot = findRoot?.parentFile
if (findRoot == null) break
}
findRoot
}
if (findRoot == null) continue
pathResult.add(LocalPath.build(findRoot))
}
val primary = getPublicPrimaryStorage(userHandle)
return pathResult.filter { it != primary }
.map { root ->
if (root.name.toIntOrNull() == userManager.currentUser().handle.handleId) {
// If this matches, then we can make assumptions on the path
File(root.parentFile!!, userHandle.handleId.toString())
} else {
root
}
}
.map { LocalPath.build(it) }
.forEach {
log(TAG) { "Secondary public storage: $it" }
pathResult.add(it)
}

return pathResult.filter { it != primary }.toSet()
}

fun getPublicStorage(userHandle: UserHandle2): Collection<LocalPath> {
return listOf(getPublicPrimaryStorage(userHandle)).plus(getPublicSecondaryStorage(userHandle))
suspend fun getPublicStorage(userHandle: UserHandle2): Collection<LocalPath> {
val paths = mutableListOf<LocalPath>()
getPublicPrimaryStorage(userHandle)?.let { paths.add(it) }
paths.addAll(getPublicSecondaryStorage(userHandle))
return paths
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SdcardsModule @Inject constructor(

// TODO we are not getting multiuser sdcards
storageEnvironment.getPublicPrimaryStorage(userManager2.currentUser().handle)
.let { determineAreaAccessPath(it) }
?.let { determineAreaAccessPath(it) }
?.let {
DataArea(
path = it,
Expand Down

0 comments on commit a5a4770

Please sign in to comment.