Last active
December 16, 2021 08:10
-
-
Save glcoder/c2783ad8c99063fc12250b9e32620751 to your computer and use it in GitHub Desktop.
GoogleSignInClient sample usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="me.change.please"> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="SampleSignIn" | |
android:roundIcon="@mipmap/ic_launcher_round" | |
android:supportsRtl="true"> | |
<activity android:name=".MainActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN"/> | |
<category android:name="android.intent.category.LAUNCHER"/> | |
</intent-filter> | |
</activity> | |
<meta-data android:name="com.google.android.gms.games.APP_ID" | |
android:value="PUT_APP_ID_HERE"/> | |
<meta-data android:name="com.google.android.gms.version" | |
android:value="@integer/google_play_services_version"/> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'com.android.application' | |
apply plugin: 'com.google.gms.google-services' | |
android { | |
compileSdkVersion 28 | |
defaultConfig { | |
applicationId "me.change.please" | |
minSdkVersion 22 | |
targetSdkVersion 28 | |
versionCode 1 | |
versionName "1.0" | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | |
} | |
} | |
} | |
dependencies { | |
implementation fileTree(dir: 'libs', include: ['*.jar']) | |
implementation 'com.android.support:appcompat-v7:28.0.0' | |
implementation 'com.google.firebase:firebase-core:16.0.7' | |
implementation 'com.android.support.constraint:constraint-layout:1.1.3' | |
implementation 'com.google.android.gms:play-services-auth:16.0.1' | |
implementation 'com.google.android.gms:play-services-games:17.0.0' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.change.please; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import android.util.Log; | |
import android.view.View; | |
import com.google.android.gms.auth.api.signin.GoogleSignIn; | |
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.tasks.OnFailureListener; | |
import com.google.android.gms.tasks.OnSuccessListener; | |
public final class MainActivity extends Activity implements View.OnClickListener { | |
private static final String TAG = "SampleSignIn"; | |
private static final int RC_SIGN_IN = 9001; | |
private GoogleSignInClient mGoogleSignInClient; | |
@Override | |
public void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mGoogleSignInClient = GoogleSignIn.getClient(this, GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN); | |
findViewById(R.id.sign_in).setOnClickListener(this); | |
findViewById(R.id.sign_out).setOnClickListener(this); | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
silentSignIn(); | |
} | |
@Override | |
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
switch (requestCode) { | |
case RC_SIGN_IN: { | |
GoogleSignIn.getSignedInAccountFromIntent(data) | |
.addOnSuccessListener(this, new OnSuccessListener<GoogleSignInAccount>() { | |
@Override | |
public void onSuccess(GoogleSignInAccount account) { | |
Log.d(TAG, "Sign in success"); | |
onSignIn(account); | |
} | |
}) | |
.addOnFailureListener(this, new OnFailureListener() { | |
@Override | |
public void onFailure(@NonNull Exception e) { | |
Log.e(TAG, "Sign in failed", e); | |
} | |
}); | |
break; | |
} | |
} | |
} | |
@Override | |
public void onClick(View v) { | |
switch (v.getId()) { | |
case R.id.sign_in: { | |
signIn(); | |
break; | |
} | |
case R.id.sign_out: { | |
signOut(); | |
break; | |
} | |
} | |
} | |
private void silentSignIn() { | |
mGoogleSignInClient.silentSignIn() | |
.addOnSuccessListener(this, new OnSuccessListener<GoogleSignInAccount>() { | |
@Override | |
public void onSuccess(GoogleSignInAccount account) { | |
Log.d(TAG, "Silent sign in success"); | |
onSignIn(account); | |
} | |
}) | |
.addOnFailureListener(this, new OnFailureListener() { | |
@Override | |
public void onFailure(@NonNull Exception e) { | |
Log.e(TAG, "Silent sign in failed", e); | |
} | |
}); | |
} | |
private void signIn() { | |
startActivityForResult(mGoogleSignInClient.getSignInIntent(), RC_SIGN_IN); | |
} | |
private void signOut() { | |
mGoogleSignInClient.signOut() | |
.addOnSuccessListener(this, new OnSuccessListener<Void>() { | |
@Override | |
public void onSuccess(Void aVoid) { | |
Log.d(TAG, "Sign out success"); | |
} | |
}) | |
.addOnFailureListener(this, new OnFailureListener() { | |
@Override | |
public void onFailure(@NonNull Exception e) { | |
Log.e(TAG, "Sign out failed", e); | |
} | |
}); | |
} | |
private void onSignIn(GoogleSignInAccount account) { | |
final GamesClient gamesClient = Games.getGamesClient(this, account); | |
gamesClient.setViewForPopups(findViewById(android.R.id.content)); | |
gamesClient.setGravityForPopups(Gravity.TOP | Gravity.CENTER_HORIZONTAL); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment