feat: Check if folder is accessible via IO

Signed-off-by: Myzel394 <50424412+Myzel394@users.noreply.github.com>
This commit is contained in:
Myzel394 2024-04-20 22:05:22 +02:00
parent f77aeb78c6
commit 257e38632b
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
2 changed files with 59 additions and 46 deletions

View File

@ -25,6 +25,9 @@ import app.myzel394.alibi.ui.SUPPORTS_SCOPED_STORAGE
import app.myzel394.alibi.ui.utils.PermissionHelper
import com.arthenica.ffmpegkit.FFmpegKitConfig
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
@ -592,20 +595,29 @@ abstract class BatchesFolder(
return 350 * 1024 * 1024
}
fun canAccessFolder(context: Context, uri: Uri): Boolean {
return try {
suspend fun canAccessFolder(context: Context, uri: Uri): Boolean {
var canAccess = false
CoroutineScope(Dispatchers.IO).launch {
try {
// Create temp file
val tempFile = DocumentFile.fromSingleUri(context, uri)!!.createFile(
val documentFile = DocumentFile.fromTreeUri(context, uri)!!
val tempFile = documentFile.createFile(
"application/octet-stream",
"temp"
)!!
if (!tempFile.exists() || !tempFile.canWrite() || !tempFile.canRead()) {
return@launch
}
tempFile.delete()
true
canAccess = true
} catch (error: RuntimeException) {
error.printStackTrace()
false
}
}.join()
return canAccess
}
}
}

View File

@ -96,10 +96,12 @@ fun SaveFolderTile(
val successMessage = stringResource(R.string.ui_settings_option_saveFolder_success)
fun updateValue(path: String?) {
scope.launch {
if (path != null && path != RECORDER_MEDIA_SELECTED_VALUE) {
context.contentResolver.takePersistableUriPermission(
path.toUri(),
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
Intent.FLAG_GRANT_READ_URI_PERMISSION
or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
if (!BatchesFolder.canAccessFolder(context, path.toUri())) {
@ -111,7 +113,7 @@ fun SaveFolderTile(
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
}
return
return@launch
}
}
@ -132,7 +134,6 @@ fun SaveFolderTile(
}
}
scope.launch {
dataStore.updateData {
it.setSaveFolder(path)
}