Skip to content

Commit 2afb2e0

Browse files
committed
add fallbacks in case the Files/DocumentUI picker is indisposed on a given system
1 parent e992da6 commit 2afb2e0

5 files changed

Lines changed: 47 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
9.0
2+
===
3+
4+
- Add fallbacks for the directory picker in case the Files/DocumentUI activity is indisposed
5+
16
8.0
27
===
38

app/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ android {
1212
applicationId = "alzwded.openaudiobookify"
1313
minSdk = 24
1414
targetSdk = 35
15-
versionCode = 8
16-
versionName = "8.0"
15+
versionCode = 9
16+
versionName = "9.0"
1717

1818
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
1919
vectorDrawables {

app/src/main/java/alzwded/openaudiobookify/MainActivity.kt

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,40 @@ fun OpenAudioBookifyApp(viewModel: MainViewModel) {
370370
onAddBooksClick = { filePickerLauncher.launch(arrayOf("*/*")) },
371371
onClearBooksClick = { viewModel.clearBooks() },
372372
onRemoveBookClick = { book -> viewModel.removeBook(book) },
373-
onSetOutputFolderClick = { dirPickerLauncher.launch(null) },
373+
onSetOutputFolderClick = {
374+
try {
375+
dirPickerLauncher.launch(null)
376+
} catch (e: android.content.ActivityNotFoundException) {
377+
// user might have deleted the Files app to "debloat" their phone
378+
// fallback to Downloads or App Dir
379+
Log.e(TAG, "DocumentsUI missing, falling back to local folders", e)
380+
381+
// Fallback 1: System Downloads folder
382+
val downloadsDir = android.os.Environment.getExternalStoragePublicDirectory(
383+
android.os.Environment.DIRECTORY_DOWNLOADS
384+
)
385+
386+
// Note: On Android 10, raw file writing to Downloads might fail without
387+
// requestLegacyExternalStorage="true" in the Manifest, so we check if it's writable.
388+
if (downloadsDir != null && downloadsDir.exists() && downloadsDir.canWrite()) {
389+
viewModel.setOutputDirUri(Uri.fromFile(downloadsDir))
390+
Toast.makeText(context, context.getString(R.string.using_downloads), Toast.LENGTH_LONG).show()
391+
} else {
392+
// Fallback 2: App Data folder
393+
val appDataDir = context.getExternalFilesDir(null)
394+
if (appDataDir != null) {
395+
viewModel.setOutputDirUri(Uri.fromFile(appDataDir))
396+
Toast.makeText(
397+
context,
398+
context.getString(R.string.using_app_private_dir, context.packageName),
399+
Toast.LENGTH_LONG
400+
).show()
401+
} else {
402+
Toast.makeText(context, context.getString(R.string.err_no_writeable_dir), Toast.LENGTH_LONG).show()
403+
}
404+
}
405+
}
406+
},
374407
onStartProcessingClick = {
375408
if (outputDirUri != null && selectedBooks.isNotEmpty()) {
376409
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && !hasNotificationPermission) {

app/src/main/res/values-ro/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,7 @@
8282
<string name="tts_list_item">Itemul %1$d: %2$s</string>
8383
<string name="tts_table">Tabel:</string>
8484
<string name="tts_table_row">Rândul %1$d:</string>
85+
<string name="using_downloads">S-a ales dosarul Descărcări</string>
86+
<string name="using_app_private_dir">se va salva în Android/data/%1$d${context.packageName}/files</string>
87+
<string name="err_no_writeable_dir">Nu s-a putut identifica un dosar în care să se poată scrie. Verificați permisiunile</string>
8588
</resources>

app/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."</string>
104104
<string name="tts_list_item">List item %1$d: %2$s</string>
105105
<string name="tts_table">Table:</string>
106106
<string name="tts_table_row">Row %1$d:</string>
107+
<string name="using_downloads">Using Downloads folder</string>
108+
<string name="using_app_private_dir">saving to Android/data/%1$d${context.packageName}/files</string>
109+
<string name="err_no_writeable_dir">Could not find a writeable directory. Check permissions</string>
107110
</resources>

0 commit comments

Comments
 (0)