Skip to content

Commit

Permalink
feat(android): activity result use new API and update permission resu…
Browse files Browse the repository at this point in the history
…lt callbacks to match (#4127)
  • Loading branch information
carlpoole committed Feb 8, 2021
1 parent 9ff9d1b commit 002f1e5
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 193 deletions.
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) {
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

0 comments on commit 002f1e5

Please sign in to comment.