How to deal with ResolvableApiException in Capacitor 3 #5133
-
Hello ! Short version of my problem :In an Android plugin, The contextI'm trying to create a GoogleSmartLock plugin with Capacitor 3, thanks to this official tutorial. This plugin should be exclusive to Android In case the current user has multiple credentials we are in a ResolvableApiException case : an exception that should open a modal to le the user to choose which credentials we want to use. However, the current official tutorial is based on the combination of BUT, startActivityForResult take an intent, while my ResolvableApiException can return nothing or a PendingIntent that is not compatible with the capacitor mecanism. So here we are, I tried this : The load() methodFirst of all, I register a callback to handle the result of my ResolvableApiException. @Override
public void load() {
this.launchSomeActivity = this.getActivity().registerForActivityResult(
new ActivityResultContracts.StartIntentSenderForResult(),
result -> {
PluginCall savedCall = GoogleSmartLock.this.bridge.getSavedCall(callbackId);
if (result.getResultCode() == Activity.RESULT_OK) {
Intent i = result.getData();
Credential credential = Objects.requireNonNull(i).getParcelableExtra(Credential.EXTRA_KEY);
savedCall.resolve(GoogleSmartLock.this.createResponse(credential));
} else {
savedCall.reject("Failed to choose a Credential.");
}
GoogleSmartLock.this.bridge.releaseCall(callbackId);
}
);
} The plugin methodNow I ask GoogleSmartLock to retrieve my stored credentials. (The relevant part is in the @PluginMethod
public void getCredentials(final PluginCall call) {
this.callbackId = call.getCallbackId();
this.bridge.saveCall(call);
CredentialsClient mCredentialsClient = Credentials.getClient(this.getContext());
CredentialRequest mCredentialRequest = new CredentialRequest.Builder()
.setPasswordLoginSupported(true)
.build();
mCredentialsClient.request(mCredentialRequest)
.addOnSuccessListener( result -> {
Credential cred = Objects.requireNonNull(result).getCredential();
call.resolve(createResponse(cred));
this.bridge.releaseCall(call);
})
.addOnFailureListener( exception -> {
if (exception instanceof ResolvableApiException) {
/**
* Here I convert the PendingIntent to an IntentSenderRequest, that can be handled by
* registerForActivityResult.
*/
IntentSenderRequest.Builder builder = new IntentSenderRequest.Builder(((ResolvableApiException) exception).getResolution());
IntentSenderRequest isr = builder.build();
/**
* At first time this PluginMethod is called, this call doesn't trigger
* the callback registered previously in the load() method. But next
* call will do it !!!
*/
this.launchSomeActivity.launch(isr);
} else if (exception instanceof ApiException) {
call.reject("ApiException", ((ApiException) exception).getStatusCode() + "");
this.bridge.releaseCall(call);
} else {
call.reject("Unknown error", exception);
this.bridge.releaseCall(call);
}
});
} The problemThis is almost working, but the first time I call for the credential resolution, my registered callback in load method is not called. Next calls will fire it. Does anybody have an idea on what I'm doing wrong ? Thank you again for your time and sorry for my English ! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
This was a bug from another plugin, cordova-plugin-facebook-connect. It has been fixed in v3.1.0 by this commit : I found the solution because I encountered the same problem with https://github.com/CodetrixStudio/CapacitorGoogleAuth plugin. |
Beta Was this translation helpful? Give feedback.
This was a bug from another plugin, cordova-plugin-facebook-connect. It has been fixed in v3.1.0 by this commit :
cordova-plugin-facebook-connect/cordova-plugin-facebook-connect@34f02b2.
I found the solution because I encountered the same problem with https://github.com/CodetrixStudio/CapacitorGoogleAuth plugin.