Compare commits

...
4 changed files with 62 additions and 7 deletions

No files matched your search

@@ -87,7 +87,8 @@ object Syncthing {
fun storageRoots(context: Context): List<File> =
listOf(context.dataDir) + context.getExternalFilesDirs(null).filterNotNull().mapNotNull { it.parentFile }
fun guard(context: Context) = SyncthingGuard(folderRoot(context), storageRoots(context), listOf(home(context)))
fun guard(context: Context, extraRoots: List<File> = emptyList()) =
SyncthingGuard(folderRoot(context), storageRoots(context) + extraRoots, listOf(home(context)))
fun init(application: Application) {
app = application.applicationContext
@@ -578,7 +578,7 @@ internal fun LibraryPage(s: FrontEndState, a: FrontEndActions, selected: String,
else GameDetail(key, s, a, lead, backLabel, hopRail = "games", onBack = back)
}
if (savedDialog) SavedDialog(emptyList(), onOpen = { id -> onSelect("sound:$id") }) { savedDialog = false }
if (addingSoundtrack) SyncFolderPicker(stringResource(R.string.soundtracks_add_title), null, onDismiss = { addingSoundtrack = false }) { dir ->
if (addingSoundtrack) SyncFolderPicker(stringResource(R.string.soundtracks_add_title), null, sharedStorage = true, onDismiss = { addingSoundtrack = false }) { dir ->
addingSoundtrack = false
a.onSoundtrackAdded(dir)
}
@@ -48,8 +48,11 @@ internal class SyncLocation(val root: File, val label: String, val icon: ImageVe
internal fun containerHome(context: Context) = File(LinuxRuntime.rootDir(context), "root")
internal fun syncLocations(context: Context): List<SyncLocation> {
internal fun syncLocations(context: Context, sharedStorage: Boolean = false): List<SyncLocation> {
val volumes = pickVolumes(context)
val storage = if (!sharedStorage) emptyList() else volumes.map {
SyncLocation(it.root, it.label, if (it.removable) Icons.Outlined.SdCard else Icons.Outlined.Smartphone)
}
val roots = Syncthing.storageRoots(context)
val shared = roots.drop(1).mapIndexed { i, root ->
val volume = volumes.firstOrNull { root.path.startsWith(it.root.path + "/") }
@@ -58,18 +61,23 @@ internal fun syncLocations(context: Context): List<SyncLocation> {
}
val home = containerHome(context).takeIf { it.isDirectory }?.let { SyncLocation(it, context.getString(R.string.sync_location_home), Icons.Outlined.Home) }
return listOfNotNull(home, SyncLocation(Syncthing.folderRoot(context), context.getString(R.string.sync_location_sync), Icons.Outlined.FolderSpecial)) +
shared + SyncLocation(roots.first(), context.getString(R.string.sync_location_app), Icons.Outlined.Folder)
storage + shared + SyncLocation(roots.first(), context.getString(R.string.sync_location_app), Icons.Outlined.Folder)
}
internal fun syncPickerGuard(context: Context, locations: List<SyncLocation>, sharedStorage: Boolean) =
Syncthing.guard(context, if (sharedStorage) locations.map { it.root } else emptyList())
@Composable
internal fun SyncFolderPicker(title: String, suggestedName: String?, onDismiss: () -> Unit, onPick: (File) -> Unit) {
internal fun SyncFolderPicker(
title: String, suggestedName: String?, sharedStorage: Boolean = false, onDismiss: () -> Unit, onPick: (File) -> Unit,
) {
val ctx = LocalContext.current
val colors = MaterialTheme.colorScheme
val density = LocalDensity.current
val shown = rememberShown(onDismiss)
val close = { shown.targetState = false }
val locations = remember { syncLocations(ctx) }
val guard = remember { Syncthing.guard(ctx) }
val locations = remember { syncLocations(ctx, sharedStorage) }
val guard = remember { syncPickerGuard(ctx, locations, sharedStorage) }
fun locationOf(d: File) = locations.filter { d.path == it.root.path || d.path.startsWith(it.root.path + "/") }.maxByOrNull { it.root.path.length }
var dir by remember { mutableStateOf<File?>(containerHome(ctx).takeIf { it.isDirectory } ?: Syncthing.folderRoot(ctx).also { it.mkdirs() }) }
var refresh by remember { mutableIntStateOf(0) }
@@ -0,0 +1,46 @@
package com.droiddeck.launcher.ui
import android.os.Environment
import com.droiddeck.launcher.R
import com.droiddeck.launcher.sync.Syncthing
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import java.io.File
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33])
class SyncFolderPickerTest {
private val context = RuntimeEnvironment.getApplication()
private val internal = Environment.getExternalStorageDirectory()
private val music = File(internal, "Music")
@Test fun sharedStorageListsInternalStorageAndAllowsItsMusicFolder() {
val locations = syncLocations(context, sharedStorage = true)
val guard = syncPickerGuard(context, locations, sharedStorage = true)
assertTrue(locations.any { it.root.path == internal.path && it.label == context.getString(R.string.games_internal) })
assertTrue(guard.allows(music.path))
assertTrue(guard.browsable(music))
}
@Test fun sharedStorageKeepsTheSyncthingConfigBlocked() {
val guard = syncPickerGuard(context, syncLocations(context, sharedStorage = true), sharedStorage = true)
assertFalse(guard.allows(Syncthing.home(context).path))
assertFalse(guard.browsable(Syncthing.home(context)))
}
@Test fun theSyncthingPickerStaysInsideAppStorage() {
val locations = syncLocations(context)
val guard = syncPickerGuard(context, locations, sharedStorage = false)
assertFalse(locations.any { it.root.path == internal.path })
assertFalse(guard.allows(music.path))
assertTrue(guard.allows(Syncthing.folderRoot(context).path))
}
}