Skip to content

Commit

Permalink
feat: Test if custom folder is accessible before setting it
Browse files Browse the repository at this point in the history
Signed-off-by: Myzel394 <[email protected]>
  • Loading branch information
Myzel394 committed Apr 8, 2024
1 parent 721a3da commit b076b8c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 9 deletions.
20 changes: 16 additions & 4 deletions app/src/main/java/app/myzel394/alibi/helpers/BatchesFolder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -586,15 +586,27 @@ abstract class BatchesFolder(
MEDIA,
}

class InaccessibleError : RuntimeException() {

}

companion object {
fun requiredBytesForOneMinuteOfRecording(appSettings: AppSettings): Long {
// 350 MiB sounds like a good default
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()

true
} catch (error: RuntimeException) {
error.printStackTrace()
false
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.InsertDriveFile
import androidx.compose.material.icons.filled.CameraAlt
import androidx.compose.material.icons.filled.Cancel
import androidx.compose.material.icons.filled.Error
import androidx.compose.material.icons.filled.Folder
import androidx.compose.material.icons.filled.Lock
import androidx.compose.material.icons.filled.Mic
Expand Down Expand Up @@ -62,6 +63,7 @@ import app.myzel394.alibi.R
import app.myzel394.alibi.dataStore
import app.myzel394.alibi.db.AppSettings
import app.myzel394.alibi.helpers.AudioBatchesFolder
import app.myzel394.alibi.helpers.BatchesFolder
import app.myzel394.alibi.helpers.VideoBatchesFolder
import app.myzel394.alibi.ui.AUDIO_RECORDING_BATCHES_SUBFOLDER_NAME
import app.myzel394.alibi.ui.MEDIA_SUBFOLDER_NAME
Expand Down Expand Up @@ -91,18 +93,35 @@ fun SaveFolderTile(
val context = LocalContext.current
val dataStore = context.dataStore

var showError by remember { mutableStateOf(false) }

val successMessage = stringResource(R.string.ui_settings_option_saveFolder_success)
fun updateValue(path: String?) {
if (path == null) {
return
}

if (!BatchesFolder.canAccessFolder(context, path.toUri())) {
showError = true
return
}

if (settings.saveFolder != null && settings.saveFolder != RECORDER_MEDIA_SELECTED_VALUE) {
runCatching {
context.contentResolver.releasePersistableUriPermission(
Uri.parse(settings.saveFolder),
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
// Clean up
val grantedURIs = context.contentResolver.persistedUriPermissions;

grantedURIs.forEach { permission ->
context.contentResolver.releasePersistableUriPermission(
permission.uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION
or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
}
}
}

if (path != null && path != RECORDER_MEDIA_SELECTED_VALUE) {
if (path != RECORDER_MEDIA_SELECTED_VALUE) {
context.contentResolver.takePersistableUriPermission(
Uri.parse(path),
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
Expand Down Expand Up @@ -130,6 +149,45 @@ fun SaveFolderTile(
}
}

if (showError) {
AlertDialog(
onDismissRequest = {
showError = false
},
icon = {
Icon(
Icons.Default.Error,
contentDescription = null,
)
},
title = {
Text(stringResource(R.string.ui_error_occurred_title))
},
confirmButton = {
Button(onClick = {
showError = false
}) {
Text(stringResource(R.string.dialog_close_neutral_label))
}
},
text = {
Column(
modifier = Modifier
.fillMaxWidth()
.verticalScroll(rememberScrollState()),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(32.dp),
) {
Text(
stringResource(R.string.ui_settings_option_saveFolder_batchesFolderInaccessible_error),
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurface,
)
}
}
)
}

if (selectionVisible) {
SelectionSheet(
sheetState = selectionSheetState,
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,6 @@
<string name="ui_about_support_title">Get Support</string>
<string name="ui_about_support_message">If you have any questions, feedback or face any issues, please don\'t hesitate to contact me. I\'m happy to help you! Below is a list of ways to get in touch with me:</string>
<string name="ui_welcome_timeSettings_values_1min">1 Minute</string>
<string name="ui_error_occurred_title">There was an error</string>
<string name="ui_settings_option_saveFolder_batchesFolderInaccessible_error">Alibi can\'t access this folder. Please select a different one</string>
</resources>

0 comments on commit b076b8c

Please sign in to comment.