Skip to content

Commit

Permalink
No media permission will be asked on Android 14+ devices because part…
Browse files Browse the repository at this point in the history
…ial media access permissions are confusing for the end-user + Fixed RequestPermission sometimes not working when the permission was reset via Settings
  • Loading branch information
yasirkula committed Jul 11, 2023
1 parent acfd94f commit 9c6272a
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,16 @@ public static int CheckPermission( Context context, final boolean isPicturePermi
if( context.checkSelfPermission( Manifest.permission.READ_EXTERNAL_STORAGE ) != PackageManager.PERMISSION_GRANTED )
return 0;
}
else if( isPicturePermission )
else if( Build.VERSION.SDK_INT < 34 && context.checkSelfPermission( isPicturePermission ? "android.permission.READ_MEDIA_IMAGES" : "android.permission.READ_MEDIA_VIDEO" ) != PackageManager.PERMISSION_GRANTED )
{
if( context.checkSelfPermission( "android.permission.READ_MEDIA_IMAGES" ) != PackageManager.PERMISSION_GRANTED )
return 0;
}
else
{
if( context.checkSelfPermission( "android.permission.READ_MEDIA_VIDEO" ) != PackageManager.PERMISSION_GRANTED )
return 0;
// On Android 14+ (34), partial media access permission is introduced which we want to avoid in a camera app (it'd be confusing for the end user):
// https://developer.android.com/about/versions/14/changes/partial-photo-video-access
// We're hoping that by now, the native camera apps have become smart enough to avoid saving the captured media to Gallery.
return 0;
}
}

// Credit: https://blog.egorand.me/taking-photos-not-so-simply-how-i-got-bitten-by-action_image_capture/
// If CAMERA permission is declared, we must request it: https://developer.android.com/reference/android/provider/MediaStore#ACTION_IMAGE_CAPTURE
if( NativeCameraUtils.IsPermissionDefinedInManifest( context, Manifest.permission.CAMERA ) && context.checkSelfPermission( Manifest.permission.CAMERA ) != PackageManager.PERMISSION_GRANTED )
return 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,18 @@ public void onCreate( Bundle savedInstanceState )

if( Build.VERSION.SDK_INT < 33 || getActivity().getApplicationInfo().targetSdkVersion < 33 )
permissions.add( Manifest.permission.READ_EXTERNAL_STORAGE );
else
{
boolean isPicturePermission = getArguments().getBoolean( PICTURE_PERMISSION_ID );
if( isPicturePermission )
permissions.add( "android.permission.READ_MEDIA_IMAGES" );
else
permissions.add( "android.permission.READ_MEDIA_VIDEO" );
}
else if( Build.VERSION.SDK_INT < 34 )
permissions.add( getArguments().getBoolean( PICTURE_PERMISSION_ID ) ? "android.permission.READ_MEDIA_IMAGES" : "android.permission.READ_MEDIA_VIDEO" );

String[] permissionsArray = new String[permissions.size()];
permissions.toArray( permissionsArray );
if( permissions.size() > 0 )
{
String[] permissionsArray = new String[permissions.size()];
permissions.toArray( permissionsArray );

requestPermissions( permissionsArray, PERMISSIONS_REQUEST_CODE );
requestPermissions( permissionsArray, PERMISSIONS_REQUEST_CODE );
}
else
onRequestPermissionsResult( PERMISSIONS_REQUEST_CODE, new String[0], new int[0] );
}
}

Expand Down
Binary file modified Plugins/NativeCamera/Android/NativeCamera.aar
Binary file not shown.
2 changes: 1 addition & 1 deletion Plugins/NativeCamera/NativeCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public static Permission RequestPermission( bool isPicturePermission )
{
NCPermissionCallbackAndroid nativeCallback = new NCPermissionCallbackAndroid( threadLock );

AJC.CallStatic( "RequestPermission", Context, nativeCallback, isPicturePermission, PlayerPrefs.GetInt( "NativeCameraPermission", (int) Permission.ShouldAsk ) );
AJC.CallStatic( "RequestPermission", Context, nativeCallback, isPicturePermission, (int) Permission.ShouldAsk );

if( nativeCallback.Result == -1 )
System.Threading.Monitor.Wait( threadLock );
Expand Down
2 changes: 1 addition & 1 deletion Plugins/NativeCamera/README.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
= Native Camera for Android & iOS (v1.3.8) =
= Native Camera for Android & iOS (v1.3.9) =

Online documentation & example code available at: https://github.com/yasirkula/UnityNativeCamera
E-mail: [email protected]
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.yasirkula.nativecamera",
"displayName": "Native Camera",
"version": "1.3.8",
"version": "1.3.9",
"documentationUrl": "https://github.com/yasirkula/UnityNativeCamera",
"changelogUrl": "https://github.com/yasirkula/UnityNativeCamera/releases",
"licensesUrl": "https://github.com/yasirkula/UnityNativeCamera/blob/master/LICENSE.txt",
Expand Down

0 comments on commit 9c6272a

Please sign in to comment.