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

build: fix all deprecation and lint in ShareFile(s) #374

Merged

Conversation

mikehardy
Copy link
Collaborator

No need to have Android Studio light up on these files
Each of these fixes was mechanical as suggested by Android Studio and code inspection
Even the possibly-controversial removal of null-checking in a couple spots can be shown to be a no-op by inspection of possible values

final String authority = ((ShareApplication) reactContext.getApplicationContext()).getFileProviderAuthority();

if(this.isBase64File()) {
String encodedImg = this.uri.getSchemeSpecificPart().substring(this.uri.getSchemeSpecificPart().indexOf(";base64,") + 8);
try {
File dir = new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DOWNLOADS );
if (!dir.exists()) {
dir.mkdirs();
Copy link
Member

Choose a reason for hiding this comment

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

Maybe it is better to keep this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

More than keep it - I altered it to not throw away the return value of "dir.mkdirs()" - now that becomes part of a chain of dir.exists + dir.mkdirs, and if one or the other of them doesn't work we get notice. Previously if dir.mkdirs() returned false we'd never know

Copy link
Member

Choose a reason for hiding this comment

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

Nice approach, thanks

@@ -139,7 +134,9 @@ public Uri getURI() {
}
} else if(this.isLocalFile()) {
Uri uri = Uri.parse(this.url);

if (uri.getPath() == null) {
return null;
Copy link
Member

Choose a reason for hiding this comment

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

is this a breaking change?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

A good question - no intention to break - the suggestion here was part of Android Studio lint, it notified that uri.getPath() may be null, so I put a check in. I wasn't sure what to do with it though, what would the consequence of proceeding with previous code if uri.getPath() had been null? I think it would have thrown RuntimeException. I suppose we could null check farther up the call chain, or throw RuntimeException ourselves with a good message, or let it continue as previous (without this change) where null may infiltrate there

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense.
I think we can go on this way and test behaviours. If return null does not work, we could try to throw a RuntimeException, like your suggestion.
We can also try to solve this on JS land.

@@ -79,7 +77,7 @@ private boolean isBase64File(Uri uri) {
String type = uri.getSchemeSpecificPart().substring(0, uri.getSchemeSpecificPart().indexOf(";"));
if (this.intentType == null) {
this.intentType = type;
} else if (type != null && !this.intentType.equalsIgnoreCase(type) && this.intentType.split("/")[0].equalsIgnoreCase((type.split("/"))[0])) {
Copy link
Member

Choose a reason for hiding this comment

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

can we keep type != null check?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Code path analysis indicates it will never be null, it was a no-op. I suppose the substring method will either return something, empty string, or throw.

@@ -109,7 +107,7 @@ private boolean isLocalFile(Uri uri) {

if (this.intentType == null) {
this.intentType = type;
} else if (type != null && !this.intentType.equalsIgnoreCase(type) && this.intentType.split("/")[0].equalsIgnoreCase((type.split("/"))[0])) {
Copy link
Member

Choose a reason for hiding this comment

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

same here, can we keep type != null ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

and same as above - I suppose if paranoid you can keep it, but static analysis indicates it's a nop

@@ -131,6 +129,9 @@ private String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
CursorLoader loader = new CursorLoader(this.reactContext, contentUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
if (cursor == null) {
return "";
Copy link
Member

Choose a reason for hiding this comment

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

why is this early return necessary?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was similar to the null check on the file path earlier - Android Studio lint indicated that null may infiltrate - loader.loadInBackground isn't guaranteed to return non-null by contract, I put in a null check but wasn't entirely sure what to do in that (exceptional) case. The early return then can be considered a stand in for "bail out!", without increasing branch depth for the rest of the method. There may be a better way to bail

Copy link
Member

Choose a reason for hiding this comment

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

Alright, let's keep this and do a few tests to understand if this will produce strange behaviours. Thanks

@@ -150,8 +151,8 @@ private String getRealPathFromURI(Uri contentUri) {
String encodedImg = uri.getSchemeSpecificPart().substring(uri.getSchemeSpecificPart().indexOf(";base64,") + 8);
try {
File dir = new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DOWNLOADS );
if (!dir.exists()) {
dir.mkdirs();
Copy link
Member

Choose a reason for hiding this comment

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

Maybe it is better to keep this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Same as above, this one was actually reinforced by not only being kept but by having it's return value actually used. In fact - ShareFiles and ShareFile should almost inherit from each other, or re-use in some other preferred OO way - the code between them is very duplicate. That's a big change though - much more than mechanical / static-analysis de-lint

@@ -163,7 +164,9 @@ private String getRealPathFromURI(Uri contentUri) {
e.printStackTrace();
}
} else if(this.isLocalFile(uri)) {
finalUris.add(FileProvider.getUriForFile(reactContext, authority, new File(uri.getPath())));
if (uri.getPath() != null) {
Copy link
Member

Choose a reason for hiding this comment

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

nice

Copy link
Member

@jgcmarins jgcmarins left a comment

Choose a reason for hiding this comment

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

Looks good, thanks for this changes.
Just have a few questions.

@jgcmarins
Copy link
Member

Each of these fixes was mechanical as suggested by Android Studio and code inspection
Even the possibly-controversial removal of null-checking in a couple spots can be shown to be a no-op by inspection of possible values

Awesome, I think we are good to go on this

@mikehardy
Copy link
Collaborator Author

Sure - no intention to make a breaking change of course, so the proper way to bail out / return on some of the new defensiveness should be thought about.

@mikehardy
Copy link
Collaborator Author

Hey @jgcmarins - no pressure, just got my PR list to a single page and saw this sitting here. Anything you'd like to see to merge it, or should we close it? Picking lint isn't the most vital thing so I could go either way but it is nice when things are mirror polished thus the PR in the first place. Just let me know. Cheers

Copy link
Member

@jgcmarins jgcmarins left a comment

Choose a reason for hiding this comment

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

great job 💯

@jgcmarins jgcmarins merged commit 67fb59e into react-native-share:master Nov 8, 2018
@mikehardy mikehardy deleted the android-deprecation-delint branch November 8, 2018 14:03
@MateusAndrade MateusAndrade mentioned this pull request May 14, 2019
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants