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

CB-10554: Implementing plugin save/restore API for Android #60

Closed
wants to merge 2 commits into from
Closed

CB-10554: Implementing plugin save/restore API for Android #60

wants to merge 2 commits into from

Conversation

riknoll
Copy link
Contributor

@riknoll riknoll commented Apr 27, 2016

The purpose of this PR is to handle the case where the Webview gets destroyed in the background while a capture activity is in the foreground. #51 was invalidated by some updates that were recently made to the plugin to handle overlapping permission and activity requests.

Also worth noting is that I did something different than the usual resume event API for getting pending plugin callback results. Instead of returning the native result to the javascript directly, this plugin needs to first wrap the results returned by the native code in a MediaFile object for those results to be useful. For that reason, I altered the plugin to expose two document events on Android that the user can subscribe to rather than the cordova-android generated resume event. These events directly return the wrapped result rather than the raw native one. It ends up looking like this:

function onDeviceReady() {
    // pendingcaptureresult is fired if the capture call is successful
    document.addEventListener('pendingcaptureresult', function(mediaFiles) {
        // Do something with result
    });

    // pendingcaptureerror is fired if the capture call is unsuccessful
    document.addEventListener('pendingcaptureerror', function(error) {
        // Handle error case
    });
}
document.addEventListener('deviceready', onDeviceReady);

Whereas using the resume event directly would require us to expose a helper API on Android:

function onDeviceReady() {
    document.addEventListener(resume, function(event) {
        if(event.pendingResult && event.pendingResult.pluginServiceName === "Capture") {
            if(event.pendingResult.pluginStatus === "OK") {
                var rawMediaFiles = event.pendingResult.result;
                // Here, we would need to expose some weird helper function to deal with the native result
                var mediaFiles = capture.wrapMediaFiles(mediaFiles);
                // Do something with result
            } else {
                var error = event.pendingResult.result;
                // Handle the error case
            }
        }
    });
}
document.addEventListener('deviceready', onDeviceReady);

I think this makes the API a bit cleaner and also opens the way for plugins that do much more heavy javascript work before returning callback results. This hasn't really come up before because the other two core plugins that had this issue (camera and contacts) could just pass the native result directly to the javascript.

I'd appreciate some feedback on this API and if it seems okay I might update the Android plugin guide with an example so that other plugins can use this pattern.

@riknoll
Copy link
Contributor Author

riknoll commented Apr 27, 2016

@adamduren @nikhilkh @infil00p I'd appreciate some feedback.

@nikhilkh
Copy link
Contributor

nikhilkh commented May 6, 2016

LGTM - Consider exposing these events on the capture object instead of document

@asfgit asfgit closed this in b2e29fc May 10, 2016
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.

None yet

2 participants