Skip to content

Commit

Permalink
Fixed issues with openGallery() on android 13. This addresses android…
Browse files Browse the repository at this point in the history
… behavioural changes on Android 13 that affects permissions. https://developer.android.com/about/versions/13/behavior-changes-13?hl=en
  • Loading branch information
shannah committed Mar 29, 2023
1 parent ab2d334 commit 90346cc
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

import android.Manifest;
import android.annotation.TargetApi;
import com.codename1.impl.android.permissions.DevicePermission;
import com.codename1.impl.android.permissions.PermissionsHelper;
import com.codename1.location.AndroidLocationManager;
import android.app.*;
import android.content.pm.PackageManager.NameNotFoundException;
Expand Down Expand Up @@ -3097,9 +3099,12 @@ private Intent createIntentForURL(String url) {
intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
} else {
if(url.startsWith("/") || url.startsWith("file:")) {
if(!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "This is required to open the file")){
return null;
if (PermissionsHelper.requiresExternalStoragePermissionForMediaAccess()) {
if(!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "This is required to open the file")){
return null;
}
}

}
intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Expand Down Expand Up @@ -3478,7 +3483,7 @@ public Media createMedia(final String uri, boolean isVideo, final Runnable onCom
return null;
}
if(!uri.startsWith(FileSystemStorage.getInstance().getAppHomePath())) {
if(!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "This is required to play media")){
if(!PermissionsHelper.checkForPermission(isVideo ? DevicePermission.PERMISSION_READ_VIDEO : DevicePermission.PERMISSION_READ_AUDIO, "This is required to play media")){
return null;
}
}
Expand Down Expand Up @@ -8141,14 +8146,19 @@ else if (requestCode == FILECHOOSER_RESULTCODE) {
}
}



@Override
public void capturePhoto(ActionListener response) {
if (getActivity() == null) {
throw new RuntimeException("Cannot capture photo in background mode");
}
if(!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "This is required to take a picture")){
return;
if (PermissionsHelper.requiresExternalStoragePermissionForMediaAccess()) {
if(!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "This is required to take a picture")){
return;
}
}

if (getRequestedPermissions().contains(Manifest.permission.CAMERA)) {
// Normally we don't need to request the CAMERA permission since we use
// the ACTION_IMAGE_CAPTURE intent, which handles permissions itself.
Expand Down Expand Up @@ -8198,9 +8208,12 @@ public void captureVideo(VideoCaptureConstraints cnst, ActionListener response)
if (getActivity() == null) {
throw new RuntimeException("Cannot capture video in background mode");
}
if(!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "This is required to take a video")){
return;
if (PermissionsHelper.requiresExternalStoragePermissionForMediaAccess()) {
if(!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "This is required to take a video")){
return;
}
}

if (getRequestedPermissions().contains(Manifest.permission.CAMERA)) {
// Normally we don't need to request the CAMERA permission since we use
// the ACTION_VIDEO_CAPTURE intent, which handles permissions itself.
Expand Down Expand Up @@ -8414,8 +8427,10 @@ public void openGallery(final ActionListener response, int type){
if (getActivity() == null) {
throw new RuntimeException("Cannot open galery in background mode");
}
if(!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "This is required to browse the photos")){
return;
if (PermissionsHelper.requiresExternalStoragePermissionForMediaAccess()) {
if(!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "This is required to browse the photos")){
return;
}
}
if(editInProgress()) {
stopEditing(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.codename1.impl.android.permissions;

public class DevicePermission {
public static final int PERMISSION_READ_IMAGES = 1;
public static final int PERMISSION_READ_VIDEO = 2;
public static final int PERMISSION_READ_AUDIO = 4;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.codename1.impl.android.permissions;

import android.Manifest;
import android.os.Build;

import com.codename1.impl.android.AndroidImplementation;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class PermissionsHelper {

private static final String READ_MEDIA_IMAGES = "android.permission.READ_MEDIA_IMAGES";
private static final String READ_MEDIA_VIDEO = "android.permission.READ_MEDIA_VIDEO";
private static final String READ_MEDIA_AUDIO = "android.permission.READ_MEDIA_AUDIO";


/**
* Takes a concatenation of permission values in {@link DevicePermission} as input
* and returns collection of corresponding android permissions.
*
* This helps for Android SDK 33 that changed the permissions required load files.
*
* @param permissions
* @return Collection of android permissions.
*/
public static Collection<String> getAndroidPermissions(int permissions) {
List<String> androidPermissionsList = new ArrayList<String>();
if (Build.VERSION.SDK_INT >= 33) {
if ((permissions & DevicePermission.PERMISSION_READ_IMAGES) != 0) {
androidPermissionsList.add(READ_MEDIA_IMAGES);
}
if ((permissions & DevicePermission.PERMISSION_READ_AUDIO) != 0) {
androidPermissionsList.add(READ_MEDIA_AUDIO);
}
if ((permissions & DevicePermission.PERMISSION_READ_VIDEO) != 0) {
androidPermissionsList.add(READ_MEDIA_VIDEO);
}
} else {
if ((permissions & (DevicePermission.PERMISSION_READ_VIDEO | DevicePermission.PERMISSION_READ_AUDIO | DevicePermission.PERMISSION_READ_IMAGES)) != 0) {
androidPermissionsList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
}

return androidPermissionsList;
}

public static boolean checkForPermission(int permissions, String description) {
for (String permission : getAndroidPermissions(permissions)) {
if (!AndroidImplementation.checkForPermission(permission, description)) {
return false;
}
}

return true;
}

/**
* API 33 does not require WRITE_EXTERNAL_STORAGE permission for media access from other apps.
* In fact it is better not to included it so that it can work permissionlessly using the
* photopicker.
* https://developer.android.com/reference/android/Manifest.permission?hl=en#WRITE_EXTERNAL_STORAGE
* https://developer.android.com/about/versions/13/behavior-changes-13?hl=en
* @return
*/
public static boolean requiresExternalStoragePermissionForMediaAccess() {
return Build.VERSION.SDK_INT < 33;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1989,7 +1989,7 @@ public void usesClassMethod(String cls, String method) {
if (request.getArg("android.removeBasePermissions", "false").equals("true")) {
basePermissions = "";
}
String externalStoragePermission = " <uses-permission android:name=\"android.permission.WRITE_EXTERNAL_STORAGE\" android:required=\"false\" />\n";
String externalStoragePermission = " <uses-permission android:name=\"android.permission.WRITE_EXTERNAL_STORAGE\" android:required=\"false\" android:maxSdkVersion=\"32\" />\n";
if (request.getArg("android.blockExternalStoragePermission", "false").equals("true")) {
externalStoragePermission = "";
}
Expand Down Expand Up @@ -3277,6 +3277,22 @@ public void usesClassMethod(String cls, String method) {
compileSdkVersion = "29";
supportLibVersion = "28";
}
if (buildToolsVersion.startsWith("30")) {
compileSdkVersion = "30";
supportLibVersion = "28";
}
if (buildToolsVersion.startsWith("31")) {
compileSdkVersion = "31";
supportLibVersion = "28";
}
if (buildToolsVersion.startsWith("32")) {
compileSdkVersion = "32";
supportLibVersion = "28";
}
if (buildToolsVersion.startsWith("33")) {
compileSdkVersion = "33";
supportLibVersion = "28";
}
jcenter =
" google()\n" +
" jcenter()\n" +
Expand Down

0 comments on commit 90346cc

Please sign in to comment.