mirror of
https://github.com/Myzel394/Alibi.git
synced 2025-06-18 23:05:26 +02:00
feat: Check if folder is accessible via IO
Signed-off-by: Myzel394 <50424412+Myzel394@users.noreply.github.com>
This commit is contained in:
parent
f77aeb78c6
commit
257e38632b
@ -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 {
|
||||
// Create temp file
|
||||
val tempFile = DocumentFile.fromSingleUri(context, uri)!!.createFile(
|
||||
"application/octet-stream",
|
||||
"temp"
|
||||
)!!
|
||||
tempFile.delete()
|
||||
suspend fun canAccessFolder(context: Context, uri: Uri): Boolean {
|
||||
var canAccess = false
|
||||
|
||||
true
|
||||
} catch (error: RuntimeException) {
|
||||
error.printStackTrace()
|
||||
false
|
||||
}
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
try {
|
||||
// Create temp file
|
||||
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()
|
||||
|
||||
canAccess = true
|
||||
} catch (error: RuntimeException) {
|
||||
error.printStackTrace()
|
||||
}
|
||||
}.join()
|
||||
|
||||
return canAccess
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -96,43 +96,44 @@ fun SaveFolderTile(
|
||||
|
||||
val successMessage = stringResource(R.string.ui_settings_option_saveFolder_success)
|
||||
fun updateValue(path: String?) {
|
||||
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
|
||||
)
|
||||
|
||||
if (!BatchesFolder.canAccessFolder(context, path.toUri())) {
|
||||
showError = true
|
||||
|
||||
runCatching {
|
||||
context.contentResolver.releasePersistableUriPermission(
|
||||
path.toUri(),
|
||||
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
|
||||
)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
runCatching {
|
||||
// Clean up
|
||||
val grantedURIs = context.contentResolver.persistedUriPermissions;
|
||||
|
||||
grantedURIs.forEach { permission ->
|
||||
if (permission.uri == path?.toUri()) {
|
||||
return@forEach
|
||||
}
|
||||
|
||||
context.contentResolver.releasePersistableUriPermission(
|
||||
permission.uri,
|
||||
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
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
scope.launch {
|
||||
if (!BatchesFolder.canAccessFolder(context, path.toUri())) {
|
||||
showError = true
|
||||
|
||||
runCatching {
|
||||
context.contentResolver.releasePersistableUriPermission(
|
||||
path.toUri(),
|
||||
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
|
||||
)
|
||||
}
|
||||
return@launch
|
||||
}
|
||||
}
|
||||
|
||||
runCatching {
|
||||
// Clean up
|
||||
val grantedURIs = context.contentResolver.persistedUriPermissions;
|
||||
|
||||
grantedURIs.forEach { permission ->
|
||||
if (permission.uri == path?.toUri()) {
|
||||
return@forEach
|
||||
}
|
||||
|
||||
context.contentResolver.releasePersistableUriPermission(
|
||||
permission.uri,
|
||||
Intent.FLAG_GRANT_READ_URI_PERMISSION
|
||||
or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
dataStore.updateData {
|
||||
it.setSaveFolder(path)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user