Skip to content
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

Closed
wants to merge 2 commits into from
Closed

Conversation

EYALIN
Copy link
Contributor

@EYALIN EYALIN commented Jul 11, 2023

in Android 13 we don't need to ask for permissions

Platforms affected

Android

Motivation and Context

Closes #606
Closes #577

Description

Testing

Checklist

  • I've run the tests to see all new and existing tests pass
  • I added automated test coverage as appropriate for this change
  • Commit is prefixed with (platform) if this change only applies to one platform (e.g. (android))
  • If this Pull Request resolves an issue, I linked to the issue in the text above (and used the correct keyword to close issues using keywords)
  • I've updated the documentation if necessary

in Android 13 we don't need to ask for permissions
@EYALIN
Copy link
Contributor Author

EYALIN commented Jul 14, 2023

@breautek the errors here are not related to my commits (IOS for example? )
can someone merge it?

@MauriceFrank
Copy link
Contributor

@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!

@EYALIN
Copy link
Contributor Author

EYALIN commented Jul 20, 2023

@MauriceFrank i'm using the photo library plugin, you can take my fork:
https://github.com/EYALIN/cordova-plugin-photo-library

@alexisGandar
Copy link

@EYALIN the fix works great thank you, can someone merge it ?

@ath0mas
Copy link

ath0mas commented Aug 15, 2023

@EYALIN can you please correct the indentation.

Also, remove the private field permissions which is never used (lines 102-116).

And why not completly removing this whole methods hasWritePermission and getWritePermission? since cordova-plugin-file v8 requires cordova-android 12+ with support for Android 24+, while WRITE_EXTERNAL_STORAGE is not supported since 19.

@breautek
Copy link
Contributor

And why not completly removing this whole methods hasWritePermission and getWritePermission? since cordova-plugin-file v8 requires cordova-android 12+ with support for Android 24+, while WRITE_EXTERNAL_STORAGE is not supported since 19.

WRITE_EXTERNAL_STORAGE is still used between API 24-28. It's not used with Scoped Storage mechanism.

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 Downloads directly).

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.

@ath0mas
Copy link

ath0mas commented Aug 24, 2023

To me, hasReadPermission and hasWritePermission should be left unchanged, because they already return the correct status for *_EXTERNAL_STORAGE permissions - if considering it strictly.

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:

  • getWritePermission( is supposed to continue through its callbackContext param, and permission process, and so doing nothing in the if you added seems wrong

Copy link

@pixellet14 pixellet14 left a 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+

Comment on lines +603 to +606
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
} else {
PermissionHelper.requestPermission(this, requestCode, Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
Copy link
Contributor

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.

Suggested change
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);
}

Copy link
Member

@erisu erisu Oct 13, 2023

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);
    }

Copy link
Member

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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;

Copy link
Contributor

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

@breautek breautek added the bug label Oct 13, 2023
@breautek breautek added this to the 8.0.1 milestone Oct 13, 2023
@erisu erisu closed this in #608 Oct 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

getDirectory and Android 13 filewriter always throws error code 2
7 participants