From bea56b00a4faa75a8c389415f047c2d7f45156eb Mon Sep 17 00:00:00 2001 From: Andrey Pavlov Date: Sun, 3 Sep 2023 22:07:20 +0500 Subject: [PATCH] Tempoorary fix access the request for Android 13 tiramisu --- android/jdonkey/AndroidManifest.xml | 4 +- .../util/DangerousPermissionsChecker.java | 45 +++++++------------ .../java/org/dkf/jmule/util/SystemUtils.java | 3 ++ 3 files changed, 21 insertions(+), 31 deletions(-) diff --git a/android/jdonkey/AndroidManifest.xml b/android/jdonkey/AndroidManifest.xml index 57aa3bdd..2f52c9cb 100644 --- a/android/jdonkey/AndroidManifest.xml +++ b/android/jdonkey/AndroidManifest.xml @@ -3,8 +3,8 @@ xmlns:tools="http://schemas.android.com/tools" package="org.dkf.jmule" android:installLocation="auto" - android:versionCode="36" - android:versionName="36"> + android:versionCode="37" + android:versionName="37"> diff --git a/android/jdonkey/src/main/java/org/dkf/jmule/util/DangerousPermissionsChecker.java b/android/jdonkey/src/main/java/org/dkf/jmule/util/DangerousPermissionsChecker.java index ff6a663f..97776624 100644 --- a/android/jdonkey/src/main/java/org/dkf/jmule/util/DangerousPermissionsChecker.java +++ b/android/jdonkey/src/main/java/org/dkf/jmule/util/DangerousPermissionsChecker.java @@ -90,17 +90,20 @@ public void requestPermissions() { } Activity activity = activityRef.get(); String[] permissions = null; - switch (requestCode) { - case EXTERNAL_STORAGE_PERMISSIONS_REQUEST_CODE: - if (SystemUtils.hasAndroid10OrNewer()) { - permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}; - } else { - permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}; - } - break; - case ACCESS_COARSE_LOCATION_PERMISSIONS_REQUEST_CODE: - permissions = new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}; - break; + if (requestCode == EXTERNAL_STORAGE_PERMISSIONS_REQUEST_CODE) { + if (SystemUtils.hasAndroid13OrNewer()) { + // As of Android13 the geniuses at Android decided yet another change + // on how to ask for permissions, now we have to be more granular about it + permissions = new String[]{Manifest.permission.READ_MEDIA_VIDEO, Manifest.permission.READ_MEDIA_AUDIO, Manifest.permission.READ_MEDIA_IMAGES}; + } else if (SystemUtils.hasAndroid11OrNewer()) { + // no more need for WRITE_EXTERNAL_STORAGE permission on Android 11, + // android:requestLegacyExternalStorage does nothing for android11 and up + // and it's ok because they finally let you use File API on the public downloads folders + permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}; + } else { + // Android 10 (29) + android:requestLegacyExternalStorage should make it work + permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}; + } } if (permissions != null) { @@ -110,14 +113,8 @@ public void requestPermissions() { @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { - switch (requestCode) { - case EXTERNAL_STORAGE_PERMISSIONS_REQUEST_CODE: - onExternalStoragePermissionsResult(permissions, grantResults); - break; - case ACCESS_COARSE_LOCATION_PERMISSIONS_REQUEST_CODE: - onAccessCoarseLocationPermissionsResult(permissions, grantResults); - default: - break; + if (requestCode == EXTERNAL_STORAGE_PERMISSIONS_REQUEST_CODE) { + onExternalStoragePermissionsResult(permissions, grantResults); } } @@ -207,16 +204,6 @@ private boolean onExternalStoragePermissionsResult(String[] permissions, int[] g return true; } - private boolean onAccessCoarseLocationPermissionsResult(String[] permissions, int[] grantResults) { - for (int i = 0; i < permissions.length; i++) { - if (permissions[i].equals(Manifest.permission.ACCESS_COARSE_LOCATION)) { - log.info("ACCESS_COARSE_LOCATION permission granted? " + (grantResults[i] == PackageManager.PERMISSION_GRANTED)); - return grantResults[i] == PackageManager.PERMISSION_GRANTED; - } - } - return false; - } - private void shutdownMule() { if (!Ref.alive(activityRef)) { return; diff --git a/android/jdonkey/src/main/java/org/dkf/jmule/util/SystemUtils.java b/android/jdonkey/src/main/java/org/dkf/jmule/util/SystemUtils.java index f3514370..4bd766d1 100644 --- a/android/jdonkey/src/main/java/org/dkf/jmule/util/SystemUtils.java +++ b/android/jdonkey/src/main/java/org/dkf/jmule/util/SystemUtils.java @@ -54,6 +54,7 @@ public final class SystemUtils { private static final int VERSION_SDK_ANDROID_10_Q = 29; private static final int VERSION_SDK_ANDROID_11_R = 30; private static final int VERSION_SDK_ANDROID_12_S = 31; + private static final int VERSION_SDK_ANDROID_13_TIRAMISU = 33; private SystemUtils() { } @@ -216,6 +217,8 @@ public static boolean hasAndroid11OrNewer() { return hasSdkOrNewer(VERSION_SDK_ANDROID_11_R); } + public static boolean hasAndroid13OrNewer() { return hasSdkOrNewer(VERSION_SDK_ANDROID_13_TIRAMISU); } + /** * Used to determine if the device is running * KitKat (Android 4.4) or greater