2929import com .anotherdev .firebase .auth .provider .GoogleAuthProvider ;
3030import com .anotherdev .firebase .auth .provider .IdpAuthCredential ;
3131import com .anotherdev .firebase .auth .provider .Provider ;
32- import com .anotherdev .firebase .auth .util .FarGson ;
3332import com .anotherdev .firebase .auth .util .RxUtil ;
3433import com .facebook .CallbackManager ;
3534import com .facebook .FacebookCallback ;
3938import com .facebook .login .widget .LoginButton ;
4039import com .github .florent37 .inlineactivityresult .rx .RxInlineActivityResult ;
4140import com .google .android .gms .auth .api .signin .GoogleSignIn ;
41+ import com .google .android .gms .auth .api .signin .GoogleSignInAccount ;
4242import com .google .android .gms .auth .api .signin .GoogleSignInClient ;
4343import com .google .android .gms .auth .api .signin .GoogleSignInOptions ;
44+ import com .google .android .gms .common .ConnectionResult ;
45+ import com .google .android .gms .common .GoogleApiAvailability ;
4446import com .google .firebase .FirebaseApp ;
4547import com .yarolegovich .lovelydialog .LovelyTextInputDialog ;
4648
@@ -371,6 +373,14 @@ public void onSuccess(LoginResult loginResult) {
371373 });
372374 }
373375
376+ private Observable <String > getGoogleIdTokenBySdk () {
377+ return new RxInlineActivityResult (this )
378+ .request (googleSignInClient .getSignInIntent ())
379+ .map (result -> GoogleSignIn .getSignedInAccountFromIntent (result .getData ()).getResult ())
380+ .map (GoogleSignInAccount ::getIdToken )
381+ .as (RxJavaBridge .toV3Observable ());
382+ }
383+
374384 private Observable <String > getGoogleIdTokenByChromeCustomTab () {
375385 AuthorizationServiceConfiguration asc = new AuthorizationServiceConfiguration (
376386 Uri .parse ("https://accounts.google.com/o/oauth2/v2/auth" ),
@@ -398,7 +408,7 @@ private Observable<String> getGoogleIdTokenByChromeCustomTab() {
398408 .map (AuthorizationResponse ::fromIntent )
399409 .flatMap (authResponse -> io .reactivex .Observable .<String >create (emitter -> service .performTokenRequest (
400410 authResponse .createTokenExchangeRequest (),
401- new ClientSecretBasic (BuildConfig .GOOGLE_CLIENT_SECRET + "wtf" ),
411+ new ClientSecretBasic (BuildConfig .GOOGLE_CLIENT_SECRET ),
402412 (tokenResponse , e ) -> {
403413 if (tokenResponse != null ) {
404414 emitter .onNext (tokenResponse .idToken );
@@ -413,43 +423,27 @@ private void setupSignInWithGoogleButton(FirebaseAuth firebaseAuth) {
413423 setupButton (firebaseAuth ,
414424 signInWithGoogleButton ,
415425 v -> {
416- if (true ) {
417- onDestroy .add (getGoogleIdTokenByChromeCustomTab ()
418- .doOnNext (tokenResponse -> Log .d ("http" , "tokenResponse: " + FarGson .get ().toJson (tokenResponse )))
419- .flatMapSingle (idToken -> firebaseAuth .signInWithCredential (GoogleAuthProvider .getCredential (idToken )))
420- .doOnNext (signInResponse -> Log .d ("http" , "signInResponse: " + FarGson .get ().toJson (signInResponse )))
421- .doOnError (e -> Log .e ("http" , "error: " + e , e ))
422- .doOnError (e -> {
423- dialog (e );
424- googleSignInClient .signOut ();
425- })
426- .subscribe (Functions .emptyConsumer (), RxUtil .ON_ERROR_LOG_V3 ));
427- return ;
428- }
429-
430426 FirebaseUser user = firebaseAuth .getCurrentUser ();
431427 if (user != null && user .isSignedInWith (Provider .GOOGLE )) {
432428 onDestroy .add (unlinkProvider (user , signInWithGoogleButton , Provider .GOOGLE , R .string .google )
433429 .subscribe (() -> {}, RxUtil .ON_ERROR_LOG_V3 ));
434430 } else {
435- onDestroy .add (RxJavaBridge
436- .toV3Disposable (new RxInlineActivityResult (this )
437- .request (googleSignInClient .getSignInIntent ())
438- .map (result -> {
439- Intent data = result .getData ();
440- return GoogleSignIn .getSignedInAccountFromIntent (data )
441- .getResult ();
442- })
443- .flatMapSingle (account -> {
444- String token = account .getIdToken ();
445- IdpAuthCredential credential = GoogleAuthProvider .getCredential (token );
446- return RxJavaBridge .toV2Single (firebaseAuth .signInWithCredential (credential ));
447- })
448- .doOnError (e -> {
449- dialog (e );
450- googleSignInClient .signOut ();
451- })
452- .subscribe (io .reactivex .internal .functions .Functions .emptyConsumer (), RxUtil .ON_ERROR_LOG_V2 )));
431+ final boolean hasGms = ConnectionResult .SUCCESS == GoogleApiAvailability .getInstance ()
432+ .isGooglePlayServicesAvailable (this );
433+ Observable <String > getGoogleIdToken = hasGms
434+ ? getGoogleIdTokenBySdk ()
435+ : getGoogleIdTokenByChromeCustomTab ();
436+ //noinspection ResultOfMethodCallIgnored
437+ getGoogleIdToken .doOnSubscribe (onDestroy ::add )
438+ .flatMapSingle (token -> {
439+ IdpAuthCredential credential = GoogleAuthProvider .getCredential (token );
440+ return firebaseAuth .signInWithCredential (credential );
441+ })
442+ .doOnError (e -> {
443+ dialog (e );
444+ googleSignInClient .signOut ();
445+ })
446+ .subscribe (Functions .emptyConsumer (), RxUtil .ON_ERROR_LOG_V3 );
453447 }
454448 },
455449 auth -> {
0 commit comments