diff --git a/packages/google_sign_in/google_sign_in_android/CHANGELOG.md b/packages/google_sign_in/google_sign_in_android/CHANGELOG.md index 5d1b2d24d7d1..6ce3cb97e8db 100644 --- a/packages/google_sign_in/google_sign_in_android/CHANGELOG.md +++ b/packages/google_sign_in/google_sign_in_android/CHANGELOG.md @@ -1,5 +1,6 @@ -## NEXT +## 6.1.6 +* Minor implementation cleanup * Updates minimum Flutter version to 3.0. ## 6.1.5 diff --git a/packages/google_sign_in/google_sign_in_android/android/src/main/java/io/flutter/plugins/googlesignin/GoogleSignInPlugin.java b/packages/google_sign_in/google_sign_in_android/android/src/main/java/io/flutter/plugins/googlesignin/GoogleSignInPlugin.java index d345d4976c63..8963a5169e89 100644 --- a/packages/google_sign_in/google_sign_in_android/android/src/main/java/io/flutter/plugins/googlesignin/GoogleSignInPlugin.java +++ b/packages/google_sign_in/google_sign_in_android/android/src/main/java/io/flutter/plugins/googlesignin/GoogleSignInPlugin.java @@ -404,9 +404,9 @@ public void init( public void signInSilently(Result result) { checkAndSetPendingOperation(METHOD_SIGN_IN_SILENTLY, result); Task task = signInClient.silentSignIn(); - if (task.isSuccessful()) { + if (task.isComplete()) { // There's immediate result available. - onSignInAccount(task.getResult()); + onSignInResult(task); } else { task.addOnCompleteListener( new OnCompleteListener() { @@ -516,7 +516,7 @@ private void onSignInResult(Task completedTask) { GoogleSignInAccount account = completedTask.getResult(ApiException.class); onSignInAccount(account); } catch (ApiException e) { - // Forward all errors and let Dart side decide how to handle. + // Forward all errors and let Dart decide how to handle. String errorCode = errorCodeForStatus(e.getStatusCode()); finishWithError(errorCode, e.toString()); } catch (RuntimeExecutionException e) { @@ -538,14 +538,20 @@ private void onSignInAccount(GoogleSignInAccount account) { } private String errorCodeForStatus(int statusCode) { - if (statusCode == GoogleSignInStatusCodes.SIGN_IN_CANCELLED) { - return ERROR_REASON_SIGN_IN_CANCELED; - } else if (statusCode == CommonStatusCodes.SIGN_IN_REQUIRED) { - return ERROR_REASON_SIGN_IN_REQUIRED; - } else if (statusCode == CommonStatusCodes.NETWORK_ERROR) { - return ERROR_REASON_NETWORK_ERROR; - } else { - return ERROR_REASON_SIGN_IN_FAILED; + switch (statusCode) { + case GoogleSignInStatusCodes.SIGN_IN_CANCELLED: + return ERROR_REASON_SIGN_IN_CANCELED; + case CommonStatusCodes.SIGN_IN_REQUIRED: + return ERROR_REASON_SIGN_IN_REQUIRED; + case CommonStatusCodes.NETWORK_ERROR: + return ERROR_REASON_NETWORK_ERROR; + case GoogleSignInStatusCodes.SIGN_IN_CURRENTLY_IN_PROGRESS: + case GoogleSignInStatusCodes.SIGN_IN_FAILED: + case CommonStatusCodes.INVALID_ACCOUNT: + case CommonStatusCodes.INTERNAL_ERROR: + return ERROR_REASON_SIGN_IN_FAILED; + default: + return ERROR_REASON_SIGN_IN_FAILED; } } diff --git a/packages/google_sign_in/google_sign_in_android/android/src/test/java/io/flutter/plugins/googlesignin/GoogleSignInTest.java b/packages/google_sign_in/google_sign_in_android/android/src/test/java/io/flutter/plugins/googlesignin/GoogleSignInTest.java index 9692417390a5..78568460c9e6 100644 --- a/packages/google_sign_in/google_sign_in_android/android/src/test/java/io/flutter/plugins/googlesignin/GoogleSignInTest.java +++ b/packages/google_sign_in/google_sign_in_android/android/src/test/java/io/flutter/plugins/googlesignin/GoogleSignInTest.java @@ -17,7 +17,11 @@ import com.google.android.gms.auth.api.signin.GoogleSignInAccount; import com.google.android.gms.auth.api.signin.GoogleSignInClient; import com.google.android.gms.auth.api.signin.GoogleSignInOptions; +import com.google.android.gms.common.api.ApiException; +import com.google.android.gms.common.api.CommonStatusCodes; import com.google.android.gms.common.api.Scope; +import com.google.android.gms.common.api.Status; +import com.google.android.gms.tasks.Task; import io.flutter.plugin.common.BinaryMessenger; import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; @@ -43,6 +47,7 @@ public class GoogleSignInTest { @Mock GoogleSignInWrapper mockGoogleSignIn; @Mock GoogleSignInAccount account; @Mock GoogleSignInClient mockClient; + @Mock Task mockSignInTask; private GoogleSignInPlugin plugin; @Before @@ -204,6 +209,27 @@ public void signInThrowsWithoutActivity() { plugin.onMethodCall(new MethodCall("signIn", null), null); } + @Test + public void signInSilentlyThatImmediatelyCompletesWithoutResultFinishesWithError() + throws ApiException { + final String clientId = "fakeClientId"; + MethodCall methodCall = buildInitMethodCall(clientId, null); + initAndAssertServerClientId(methodCall, clientId); + + ApiException exception = + new ApiException(new Status(CommonStatusCodes.SIGN_IN_REQUIRED, "Error text")); + when(mockClient.silentSignIn()).thenReturn(mockSignInTask); + when(mockSignInTask.isComplete()).thenReturn(true); + when(mockSignInTask.getResult(ApiException.class)).thenThrow(exception); + + plugin.onMethodCall(new MethodCall("signInSilently", null), result); + verify(result) + .error( + "sign_in_required", + "com.google.android.gms.common.api.ApiException: 4: Error text", + null); + } + @Test public void init_LoadsServerClientIdFromResources() { final String packageName = "fakePackageName"; diff --git a/packages/google_sign_in/google_sign_in_android/pubspec.yaml b/packages/google_sign_in/google_sign_in_android/pubspec.yaml index b6b581a75764..4be89f27286a 100644 --- a/packages/google_sign_in/google_sign_in_android/pubspec.yaml +++ b/packages/google_sign_in/google_sign_in_android/pubspec.yaml @@ -2,7 +2,7 @@ name: google_sign_in_android description: Android implementation of the google_sign_in plugin. repository: https://github.com/flutter/plugins/tree/main/packages/google_sign_in/google_sign_in_android issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+google_sign_in%22 -version: 6.1.5 +version: 6.1.6 environment: sdk: ">=2.14.0 <3.0.0"