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

Unable to Retry Nearby.saveFile() after initial permission failure (granting after initial failure) #4

Closed
Faolain opened this issue Mar 24, 2019 · 2 comments

Comments

@Faolain
Copy link

Faolain commented Mar 24, 2019

Hey Butch, first need to say amazing library!

I have a question regarding the ability to call Nearby.saveFile(serviceId,endpointId,payloadId) after an initial permission failure. Currently, as long as a user has granted permissions to READ/WRITE external storage, calling Nearby.saveFile() has the promise resolve successfully. If permissions are not granted or are revoked before calling saveFile, one encounters a promise rejection. Until here things are still behaving as expected.

However, in my case I am trying to offer the user the ability to retry executing saveFile() at a later point by storing the serviceId, endpointId, and payloadId before initially calling saveFile. This way, once saveFile() is called and it fails, the user can be prompted to grant permissions within their settings or elsewhere and try calling it again. The problem is that when I allow the user to retry using the same serviceId, endpointId, and payloadId which previously failed due to permissions not being granted, the promise still rejects.

When I adb logcat I see it is failing with the message 03-24 14:31:03.806 24644 26856 V NearbyConnection: Cannot convert to file., which is line #1501 here

public void saveFile(final String serviceId, final String endpointId, final String payloadId, final Promise promise) {
logV("saveFile from service "+serviceId+" and endpoint " + endpointId + " and payload "+payloadId);
final Payload payload = mReceivedPayloads.get(serviceId+"_"+endpointId+"_"+payloadId);
if (payload == null) {
logV("Cannot find payload.");
promise.reject("");
return;
}
if (payload.getType() != Payload.Type.FILE) {
logV("Cannot save, not a file.");
promise.reject("");
return;
}
final String payloadFileData = mPayloadFileData.get(payloadId);
int colonIndex = payloadFileData.indexOf(':');
final String payloadFilename = payloadFileData.substring(0, colonIndex);
final String payloadMetadata = payloadFileData.substring(colonIndex+1);
if (payloadFilename == null) {
logV("Cannot find filename for "+payloadId);
promise.reject("");
return;
}
final Activity activity = getCurrentActivity();
permissionsCheck(activity, Arrays.asList(getRequiredStoragePermissions()), new Callable<Void>() {
@Override
public Void call() throws Exception {
File payloadFile = payload.asFile().asJavaFile();
if (payloadFile == null) {
logV("Cannot convert to file.");
promise.reject("");
}
else {
File destPath = payloadFile.getParentFile();
String extension = NearbyConnectionModule.getFileExtension(payloadFilename);
File destFile = NearbyConnectionModule.getUniqueFile(destPath, extension);
payloadFile.renameTo(destFile);
String destFilePath = destFile.getPath();
logV("file saved to "+destFilePath);
WritableMap out = Arguments.createMap();
out.putString("path", destFilePath);
out.putString("originalFilename", payloadFilename);
out.putString("metadata", payloadMetadata);
promise.resolve(out);
}
return null;
}
});
}

In other words it seems that
File payloadFile = payload.asFile().asJavaFile(); is null. I decided to log every payload variable until permissionsCheck fires and this is the below output:

03-24 14:31:03.805 24644 26856 V NearbyConnection: saveFile from service com.google.myApp.v1 and endpoint hT4- and payload -6821529802993021226
03-24 14:31:03.805 24644 26856 V NearbyConnection: Payload com.google.android.gms.nearby.connection.Payload@16edba2
03-24 14:31:03.805 24644 26856 V NearbyConnection: payload.getType() 2
03-24 14:31:03.805 24644 26856 V NearbyConnection: payloadFileData  awesomePhoto.WEBP:{"description":"a flower"}
03-24 14:31:03.805 24644 26856 V NearbyConnection: payloadFilename awesomePhoto.WEBP
03-24 14:31:03.805 24644 26856 V NearbyConnection: payloadMetadata {"description":"a flower"}
03-24 14:31:03.806 24644 26856 V NearbyConnection: Cannot convert to file.

Question/s: Any idea why payload.asFile().asJavaFile() returns null when retrying saveFile? Is it that the initial permissions failure causes the payload content to somehow be destroyed within the NearbyConnection cache so there is nothing to call payload.asFile().asJavaFile() on? Although I find it strange that calling payload.getType() works and other metadata still exists on the payload but asFile() fails. Any thoughts here would be amazing.

Assumption/Hypothesis: After the final payload is delivered, Nearby Connections attempts to write the file which, if the permissions are not given, fails. Therefore, regardless if permissions are then granted after, that final payload will still not have a file associated and, as such, will forever fail. If this is correct then there is no other option than to resend the file I'm guessing.

Stackoverflow link: https://stackoverflow.com/questions/55328027/nearbyconnection-payload-asfile-asjavafile-is-null-when-retrying-to-savefil

@butchmarshall
Copy link
Owner

Hey @Faolain - i'm not entirely sure what's going on here either. Your use case should work.

The payload is still in the mReceivedPayloads array otherwise you'd get Cannot find payload.

I don't think it would attempt to write the file if no permissions. It may be the underlying native cleaning up files? That doesn't make much sense though either that a only a failed permission check would cause that.

I'm sorry but I don't have time to do a deeper investigation at the moment.

@Faolain
Copy link
Author

Faolain commented Mar 25, 2019

Thanks @butchmarshall appreciate it, turns out the google sdk is caching the failures which is leading to this issue. Xlythe submitted a bug ticket to remedy that.

@Faolain Faolain closed this as completed Mar 25, 2019
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

No branches or pull requests

2 participants