Skip to content

Instantly share code, notes, and snippets.

@BenDLH
Created October 4, 2016 07:52
Show Gist options
  • Save BenDLH/41a73d1e7648c1441131a2df37865d5f to your computer and use it in GitHub Desktop.
Save BenDLH/41a73d1e7648c1441131a2df37865d5f to your computer and use it in GitHub Desktop.
Helper class for integrating Google sign in on Android
//
// GoogleSignInManager
// Created by Ben De La Haye on 24/03/2016.
//
public class GoogleSignInManager implements GoogleApiClient.OnConnectionFailedListener {
public interface Listener {
void onUserSignedIn(GoogleSignInAccount account);
void onUserSignInFailed(int errorCode, String errorMessage);
void onUserDataRetrieved(@Nullable Person person);
void onUserSignedOut();
}
private static final boolean DEBUG = true;
private static final int RC_SIGN_IN = 123;
private GoogleSignInOptions _signInOptions;
private GoogleApiClient _googleApiClient;
private GoogleApiClient _googlePlusApiClient;
private ProgressDialog _progressDialog;
private final FragmentActivity _activity;
private final Listener _listener;
public GoogleSignInManager(FragmentActivity activity, Listener listener) {
_activity = activity;
_listener = listener;
// Configure sign-in to request the user's ID, email address, and basic
// profile. ID and basic profile are included in DEFAULT_SIGN_IN.
_signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestIdToken(Constants.WEB_CLIENT_ID)
.build();
// Build a GoogleApiClient with access to the Google Sign-In API and the
// options specified by gso.
_googleApiClient = new GoogleApiClient.Builder(_activity)
.addApi(Auth.GOOGLE_SIGN_IN_API, _signInOptions)
.build();
// Build a GoogleApiClient with access to the Google Sign-In API and the
// options specified by gso.
_googlePlusApiClient = new GoogleApiClient.Builder(_activity)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();
}
public void attemptSilentSignIn() {
if (DEBUG) Logger.debug(this, "attemptSilentSignIn()");
ensureClientsAreConnected();
OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(_googleApiClient);
if (opr.isDone()) {
// If the user's cached credentials are valid, the OptionalPendingResult will be "done"
// and the GoogleSignInResult will be available instantly.
GoogleSignInResult result = opr.get();
handleSignInResult(result);
} else {
// If the user has not previously signed in on this device or the sign-in has expired,
// this asynchronous branch will attempt to sign in the user silently. Cross-device
// single sign-on will occur in this branch.
showProgressDialog();
opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(@NonNull GoogleSignInResult googleSignInResult) {
handleSignInResult(googleSignInResult);
}
});
}
}
public void signIn(Activity activity) {
if (DEBUG) Logger.debug(this, "signIn()");
ensureClientsAreConnected();
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(_googleApiClient);
activity.startActivityForResult(signInIntent, RC_SIGN_IN);
}
private void signOut() {
if (DEBUG) Logger.debug(this, "signOut()");
ensureClientsAreConnected();
Auth.GoogleSignInApi.signOut(_googleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
_listener.onUserSignedOut();
}
});
}
private void ensureClientsAreConnected() {
if (!_googleApiClient.isConnected()) _googleApiClient.connect();
if (!_googlePlusApiClient.isConnected()) _googlePlusApiClient.connect();
}
public void handleActivityResult(int requestCode, int resultCode, Intent data) {
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (resultCode == Activity.RESULT_OK && requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
private void handleSignInResult(final GoogleSignInResult result) {
Logger.debug(this, "handleSignInResult:" + result.isSuccess());
hideProgressDialog();
if (result.isSuccess() && result.getSignInAccount() != null) {
_listener.onUserSignedIn(result.getSignInAccount());
} else {
// Signed out, show unauthenticated UI.
_listener.onUserSignInFailed(result.getStatus().getStatusCode(), result.getStatus().getStatusMessage());
}
}
public void getUserData(GoogleSignInAccount account) {
// TODO Not working
Plus.PeopleApi.load(_googlePlusApiClient, account.getId()).setResultCallback(new ResultCallback<People.LoadPeopleResult>() {
@Override
public void onResult(@NonNull People.LoadPeopleResult loadPeopleResult) {
if (loadPeopleResult.getPersonBuffer() != null) {
Person person = loadPeopleResult.getPersonBuffer().get(0);
_listener.onUserDataRetrieved(person);
} else {
_listener.onUserSignInFailed(-1, loadPeopleResult.getStatus().getStatusMessage());
}
}
});
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
if (DEBUG) Logger.debug(this, "onConnectionFailed()" + connectionResult);
_listener.onUserSignInFailed(connectionResult.getErrorCode(), connectionResult.getErrorMessage());
}
private void showProgressDialog() {
if (_progressDialog == null) {
_progressDialog = new ProgressDialog(_activity);
_progressDialog.setMessage("Signing you in...");
_progressDialog.setIndeterminate(true);
}
_progressDialog.show();
}
private void hideProgressDialog() {
if (_progressDialog != null && _progressDialog.isShowing()) {
_progressDialog.hide();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment