-
Notifications
You must be signed in to change notification settings - Fork 760
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix Android 13 write permissions #581
Conversation
in Android 13 we don't need to ask for permissions
@breautek the errors here are not related to my commits (IOS for example? ) |
@EYALIN DO you have any experience with the corodva-plugin-saf-mediastore? I need to write media files into the gallery(external storage) on Android 13 but so far I have no success with the saf-mediastore plugin. Any alternatives? I would be glad for any help ,thank you! |
@MauriceFrank i'm using the photo library plugin, you can take my fork: |
@EYALIN the fix works great thank you, can someone merge it ? |
@EYALIN can you please correct the indentation. Also, remove the private field And why not completly removing this whole methods |
Between API-24-28, the permission is not required for your app's external data directory, but is required to write to external shared directories (e.g. the But there are other significant issues with using the external filesystem on API 29+ that makes using a filesystem oriented API not feasible and I feel like we should just simply drop support for it in favour of media store plugin that interfaces with the native MediaStore instead. If we do go that route, then we can drop the permissions and probably simplify the file plugin significantly. |
To me, And, as I work on PR #597, I like the idea that this plugin should concentrate on file operations among the app dirs only, default authorized without any permission ; and that trying to read-write outside should be done using other plugins, to follow the concepts and isolation forced by Google for file-sharing between apps or publicly, relying on SAF, MediaStore, ContentProvider, etc. For more tech review:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WRITE_EXTERNAL_STORAGE has been depreciated for API levels 32+
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
} else { | ||
PermissionHelper.requestPermission(this, requestCode, Manifest.permission.WRITE_EXTERNAL_STORAGE); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be rewritten as to invert the condition so we don't have an empty code block.
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | |
} else { | |
PermissionHelper.requestPermission(this, requestCode, Manifest.permission.WRITE_EXTERNAL_STORAGE); | |
} | |
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { | |
PermissionHelper.requestPermission(this, requestCode, Manifest.permission.WRITE_EXTERNAL_STORAGE); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getWritePermission
is already wrapped by a condition that requires needPermission
to be true
, before it is called.
needPermission
should then hit this condition:
else if(permissionType == WRITE && hasWritePermission()) {
return false;
}
Which should return false
once the hasWritePermission
method is fixed.
I think the only changes needed in this PR should be:
private boolean hasWritePermission() {
return android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU
? true
: PermissionHelper.hasPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@EYALIN Can you review the above messages and make the updates?
I believe you do not need to modify the getWritePermission
method, as I previously mentioned the reasons why.
Only hasWritePermission
needs to be updated. The code could be simplified with what I provided in my previous message.
Also, can you rebase your branch against with Cordova's main branch? There are conflicts.
And include Norman's suggestion about including a comment explaining about the true
return.
// Starting with API 33, requesting WRITE_EXTERNAL_STORAGE is an auto permission rejection
@@ -614,7 +617,11 @@ private boolean hasReadPermission() { | |||
} | |||
|
|||
private boolean hasWritePermission() { | |||
return PermissionHelper.hasPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE); | |||
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | |||
return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return true; | |
// Starting with API 33, requesting WRITE_EXTERNAL_STORAGE is an auto permission rejection | |
// Apps now have read-write access to it's own (created) files, so we can assume `true`, however not all files are readable. | |
// See https://github.com/apache/cordova-plugin-file#androids-external-storage-quirks for more details. | |
return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to explain why we are returning true for future contributors/code maintainers
in Android 13 we don't need to ask for permissions
Platforms affected
Android
Motivation and Context
Closes #606
Closes #577
Description
Testing
Checklist
(platform)
if this change only applies to one platform (e.g.(android)
)