mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-06 13:05:47 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e63444be59 |
No files matched your search
@@ -87,7 +87,8 @@ object Syncthing {
|
|||||||
fun storageRoots(context: Context): List<File> =
|
fun storageRoots(context: Context): List<File> =
|
||||||
listOf(context.dataDir) + context.getExternalFilesDirs(null).filterNotNull().mapNotNull { it.parentFile }
|
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) {
|
fun init(application: Application) {
|
||||||
app = application.applicationContext
|
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)
|
else GameDetail(key, s, a, lead, backLabel, hopRail = "games", onBack = back)
|
||||||
}
|
}
|
||||||
if (savedDialog) SavedDialog(emptyList(), onOpen = { id -> onSelect("sound:$id") }) { savedDialog = false }
|
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
|
addingSoundtrack = false
|
||||||
a.onSoundtrackAdded(dir)
|
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 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 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 roots = Syncthing.storageRoots(context)
|
||||||
val shared = roots.drop(1).mapIndexed { i, root ->
|
val shared = roots.drop(1).mapIndexed { i, root ->
|
||||||
val volume = volumes.firstOrNull { root.path.startsWith(it.root.path + "/") }
|
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) }
|
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)) +
|
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
|
@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 ctx = LocalContext.current
|
||||||
val colors = MaterialTheme.colorScheme
|
val colors = MaterialTheme.colorScheme
|
||||||
val density = LocalDensity.current
|
val density = LocalDensity.current
|
||||||
val shown = rememberShown(onDismiss)
|
val shown = rememberShown(onDismiss)
|
||||||
val close = { shown.targetState = false }
|
val close = { shown.targetState = false }
|
||||||
val locations = remember { syncLocations(ctx) }
|
val locations = remember { syncLocations(ctx, sharedStorage) }
|
||||||
val guard = remember { Syncthing.guard(ctx) }
|
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 }
|
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 dir by remember { mutableStateOf<File?>(containerHome(ctx).takeIf { it.isDirectory } ?: Syncthing.folderRoot(ctx).also { it.mkdirs() }) }
|
||||||
var refresh by remember { mutableIntStateOf(0) }
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user