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

feat(android): activity result use new API and updated permission result callbacks to match #4127

Merged
merged 16 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 31 additions & 26 deletions android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ public PluginHandle getPlugin(String pluginId) {
* @param requestCode
* @return
*/
@Deprecated
public PluginHandle getPluginWithRequestCode(int requestCode) {
for (PluginHandle handle : this.plugins.values()) {
int[] requestCodes;
Expand All @@ -501,13 +502,11 @@ public PluginHandle getPluginWithRequestCode(int requestCode) {
}

requestCodes = legacyPluginAnnotation.requestCodes();
} else {
requestCodes = pluginAnnotation.requestCodes();
}

for (int rc : requestCodes) {
if (rc == requestCode) {
return handle;
for (int rc : requestCodes) {
if (rc == requestCode) {
return handle;
}
}
}
}
Expand Down Expand Up @@ -638,6 +637,12 @@ public PluginCall getSavedCall(String callbackId) {
return this.savedCalls.get(callbackId);
}

PluginCall getPluginCallForLastActivity() {
PluginCall pluginCallForLastActivity = this.pluginCallForLastActivity;
this.pluginCallForLastActivity = null;
return pluginCallForLastActivity;
}

/**
* Release a retained call
* @param call
Expand Down Expand Up @@ -900,36 +905,36 @@ protected Map<String, PermissionState> getPermissionStates(Plugin plugin) {
* @param resultCode
* @param data
*/
public void onActivityResult(int requestCode, int resultCode, Intent data) {
boolean onActivityResult(int requestCode, int resultCode, Intent data) {
jcesarmobile marked this conversation as resolved.
Show resolved Hide resolved
PluginHandle plugin = getPluginWithRequestCode(requestCode);

if (plugin == null || plugin.getInstance() == null) {
Logger.debug("Unable to find a Capacitor plugin to handle requestCode, trying Cordova plugins " + requestCode);
cordovaInterface.onActivityResult(requestCode, resultCode, data);
return;
return cordovaInterface.onActivityResult(requestCode, resultCode, data);
}

// deprecated, to be removed
PluginCall lastCall = plugin.getInstance().getSavedCall();
CapacitorPlugin pluginAnnotation = plugin.getPluginClass().getAnnotation(CapacitorPlugin.class);
if (pluginAnnotation == null) {
// deprecated, to be removed
PluginCall lastCall = plugin.getInstance().getSavedCall();

// If we don't have a saved last call (because our app was killed and restarted, for example),
// Then we should see if we have any saved plugin call information and generate a new,
// "dangling" plugin call (a plugin call that doesn't have a corresponding web callback)
// and then send that to the plugin
if (lastCall == null && pluginCallForLastActivity != null) {
plugin.getInstance().saveCall(pluginCallForLastActivity);
}

plugin.getInstance().handleOnActivityResult(requestCode, resultCode, data);

// If we don't have a saved last call (because our app was killed and restarted, for example),
// Then we should see if we have any saved plugin call information and generate a new,
// "dangling" plugin call (a plugin call that doesn't have a corresponding web callback)
// and then send that to the plugin
if (lastCall == null && pluginCallForLastActivity != null) {
plugin.getInstance().saveCall(pluginCallForLastActivity);
}
// Clear the plugin call we may have re-hydrated on app launch
pluginCallForLastActivity = null;

CapacitorPlugin pluginAnnotation = plugin.getPluginClass().getAnnotation(CapacitorPlugin.class);
if (pluginAnnotation != null) {
// Use new callback with new @CapacitorPlugin plugins
plugin.getInstance().handleOnActivityResult(pluginCallForLastActivity, requestCode, resultCode, data);
return true;
} else {
plugin.getInstance().handleOnActivityResult(requestCode, resultCode, data);
return false;
}

// Clear the plugin call we may have re-hydrated on app launch
pluginCallForLastActivity = null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,31 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in
}
}

/**
* Handles activity results.
*
* Capacitor is backwards compatible such that plugins using legacy activity result codes
* may coexist with plugins using the AndroidX Activity v1.2 activity callback flow introduced
* in Capacitor 3.0.
*
* In this method, plugins are checked first for ownership of the legacy request code. If the
* {@link Bridge#onActivityResult(int, int, Intent)} method indicates it has handled the activity
* result, then the callback will be considered complete. Otherwise, the result will be handled
* using the AndroidX Activiy flow.
*
* @param requestCode the request code associated with the activity result
* @param resultCode the result code
* @param data any data included with the activity result
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (this.bridge == null) {
return;
}

this.bridge.onActivityResult(requestCode, resultCode, data);
if (!bridge.onActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
}

@Override
Expand Down
Loading