Skip to content

Commit 4c32c9f

Browse files
committed
fix compatibility issue with androids older than 10 after introducing the MediaStore workaround for people who deleted/disabled their com.android.documentsui
1 parent 65856ca commit 4c32c9f

6 files changed

Lines changed: 58 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
13.0
2+
====
3+
4+
- Fix permission issue for Androids < 10 introduced by the workaround
5+
introduced in version 10.0 which uses the MediaStore to output audiobooks to
6+
a default directory
7+
- Add a LaunchedEffect to re-check if permissions were gained/lost, in case
8+
they were gained/lost since the app was started and since we last checked
9+
and the activity wasn't reaped by the system
10+
111
12.0
212
====
313

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 = 12
16-
versionName = "12.0"
15+
versionCode = 13
16+
versionName = "13.0"
1717

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

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
88
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
99
<uses-permission android:name="android.permission.WAKE_LOCK" />
10+
<!-- compatibility with older Androids -->
11+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
12+
android:maxSdkVersion="28" />
1013

1114
<queries>
1215
<intent>

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,35 @@ fun OpenAudioBookifyApp(viewModel: MainViewModel) {
281281
)
282282
}
283283

284+
var hasWriteExternalPermission by remember {
285+
mutableStateOf(
286+
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
287+
ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
288+
} else {
289+
// permission no longer necessary starting in Q when using MediaStore;
290+
// if an output directory is set with SAF, you grant permission then, any version
291+
true
292+
}
293+
)
294+
}
295+
296+
// Re-verify permission whenever the Composable comes into the foreground
297+
// (in case the user changed permissions in system settings)
298+
LaunchedEffect(Unit) {
299+
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
300+
hasWriteExternalPermission = ContextCompat.checkSelfPermission(
301+
context,
302+
Manifest.permission.WRITE_EXTERNAL_STORAGE
303+
) == PackageManager.PERMISSION_GRANTED
304+
}
305+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
306+
hasNotificationPermission = ContextCompat.checkSelfPermission(
307+
context,
308+
Manifest.permission.POST_NOTIFICATIONS
309+
) == PackageManager.PERMISSION_GRANTED
310+
}
311+
}
312+
284313
val permissionLauncher = rememberLauncherForActivityResult(
285314
contract = ActivityResultContracts.RequestPermission(),
286315
onResult = { isGranted ->
@@ -291,6 +320,16 @@ fun OpenAudioBookifyApp(viewModel: MainViewModel) {
291320
}
292321
)
293322

323+
val writeExternalPermissionLauncher = rememberLauncherForActivityResult(
324+
contract = ActivityResultContracts.RequestPermission(),
325+
onResult = { isGranted ->
326+
hasWriteExternalPermission = isGranted
327+
if (!isGranted) {
328+
Toast.makeText(context, context.getString(R.string.write_external_permission_required), Toast.LENGTH_SHORT).show()
329+
}
330+
}
331+
)
332+
294333
val filePickerLauncher = rememberLauncherForActivityResult(
295334
contract = ActivityResultContracts.OpenMultipleDocuments(),
296335
onResult = { uris ->
@@ -387,6 +426,8 @@ fun OpenAudioBookifyApp(viewModel: MainViewModel) {
387426
if (selectedBooks.isNotEmpty()) {
388427
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && !hasNotificationPermission) {
389428
permissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
429+
} else if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P && !hasWriteExternalPermission) {
430+
writeExternalPermissionLauncher.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE);
390431
} else {
391432
startProcessingService()
392433
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<string name="about">Despre</string>
33
<string name="settings">Setări</string>
44
<string name="notification_permission_required">Aplicația are nevoie de permisiunea de a afișa notificări pentru procesarea datelor în fundal</string>
5+
<string name="write_external_permission_required">Aplicația are nevoie de permisiunea de a scrie pe mediul de stocare extern ("Write External Storage"), cu excepția cazului în care alegeți explicit un dosar de ieșire</string>
56
<string name="processing_started">Procesarea a început în fundal</string>
67
<string name="error_no_output_folder">Nu a fost selectat niciun dosar pentru datele de ieșire</string>
78
<string name="error_no_books_added">Trebuie să adăugați cărți</string>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<string name="about">About</string>
33
<string name="settings">Settings</string>
44
<string name="notification_permission_required">Notification permission required for background processing</string>
5+
<string name="write_external_permission_required">Write External Storage permission required, unless you explicitly set an output directory</string>
56
<string name="processing_started">Processing started in background</string>
67
<string name="error_no_output_folder">No output folder selected</string>
78
<string name="error_no_books_added">Add some books first</string>

0 commit comments

Comments
 (0)