blob: 48f4abb75ca7e8e92e034361d27e5a2b85ee588e [file] [log] [blame]
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:flutter/foundation.dart' show visibleForTesting;
import 'package:google_sign_in_platform_interface/google_sign_in_platform_interface.dart';
import 'src/messages.g.dart';
/// Android implementation of [GoogleSignInPlatform].
class GoogleSignInAndroid extends GoogleSignInPlatform {
/// Creates a new plugin implementation instance.
@visibleForTesting GoogleSignInApi? api,
}) : _api = api ?? GoogleSignInApi();
final GoogleSignInApi _api;
/// Registers this class as the default instance of [GoogleSignInPlatform].
static void registerWith() {
GoogleSignInPlatform.instance = GoogleSignInAndroid();
Future<void> init({
List<String> scopes = const <String>[],
SignInOption signInOption = SignInOption.standard,
String? hostedDomain,
String? clientId,
}) {
return initWithParams(SignInInitParameters(
signInOption: signInOption,
scopes: scopes,
hostedDomain: hostedDomain,
clientId: clientId,
Future<void> initWithParams(SignInInitParameters params) {
return _api.init(InitParams(
signInType: _signInTypeForOption(params.signInOption),
scopes: params.scopes,
hostedDomain: params.hostedDomain,
clientId: params.clientId,
serverClientId: params.serverClientId,
forceCodeForRefreshToken: params.forceCodeForRefreshToken,
Future<GoogleSignInUserData?> signInSilently() {
return _api.signInSilently().then(_signInUserDataFromChannelData);
Future<GoogleSignInUserData?> signIn() {
return _api.signIn().then(_signInUserDataFromChannelData);
Future<GoogleSignInTokenData> getTokens(
{required String email, bool? shouldRecoverAuth = true}) {
return _api
.getAccessToken(email, shouldRecoverAuth ?? true)
.then((String result) => GoogleSignInTokenData(
accessToken: result,
Future<void> signOut() {
return _api.signOut();
Future<void> disconnect() {
return _api.disconnect();
Future<bool> isSignedIn() {
return _api.isSignedIn();
Future<void> clearAuthCache({String? token}) {
// The token is not acutally nullable; see
return _api.clearAuthCache(token!);
Future<bool> requestScopes(List<String> scopes) {
return _api.requestScopes(scopes);
SignInType _signInTypeForOption(SignInOption option) {
switch (option) {
case SignInOption.standard:
return SignInType.standard;
// Handle the case where a new type is added to the platform interface in
// the future, and this version of the package is used with it.
// ignore: dead_code
throw UnimplementedError('Unsupported sign in option: $option');
GoogleSignInUserData _signInUserDataFromChannelData(UserData data) {
return GoogleSignInUserData(
displayName: data.displayName,
photoUrl: data.photoUrl,
idToken: data.idToken,
serverAuthCode: data.serverAuthCode,