Created
October 4, 2016 07:56
-
-
Save BenDLH/bcbdf83e59c6f03b8f59febc37b4006d to your computer and use it in GitHub Desktop.
A basic implementation of the GoogleSignInManager
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
// | |
// BaseAuthActivity | |
// Created by Ben De La Haye on 28/04/2016. | |
// | |
public abstract class BaseAuthActivity extends BaseActivity implements GoogleSignInManager.Listener, Callback<LoginResponse> { | |
private GoogleSignInManager _googleSignInManager; | |
private AuthenticatedUser authenticatedUser; | |
public abstract void onUserLoggedIn(); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
_googleSignInManager = new GoogleSignInManager(this, this); | |
authenticatedUser = AuthenticatedUser.getInstance(); | |
} | |
@Override | |
public boolean onPrepareOptionsMenu(Menu menu) { | |
if (AuthenticatedUser.getInstance().isLoggedIn()) { | |
menu.findItem(R.id.action_log_in).setVisible(false); | |
menu.findItem(R.id.action_user_profile).setVisible(true); | |
} else { | |
menu.findItem(R.id.action_log_in).setVisible(true); | |
menu.findItem(R.id.action_user_profile).setVisible(false); | |
} | |
return super.onPrepareOptionsMenu(menu); | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
switch (item.getItemId()) { | |
case R.id.action_log_in: | |
if (AuthenticatedUser.getInstance().isAuthenticated()) { | |
loginToBackend(); | |
} else { | |
_googleSignInManager.signIn(this); | |
} | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
if (!authenticatedUser.isLoggedIn()) { | |
if (authenticatedUser.getSocialToken() != null && authenticatedUser.getAuthPlatform() == AuthenticatedUser.AUTH_GOOGLE) { | |
// Authenticate with Google | |
Toaster.makeShort("attemptSilentSignIn()"); | |
_googleSignInManager.attemptSilentSignIn(); | |
} else { | |
Toaster.makeShort("User isn't logged in"); | |
} | |
} else { | |
Toaster.makeShort("User is already logged in"); | |
} | |
} | |
@Override | |
public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...); | |
_googleSignInManager.handleActivityResult(requestCode, resultCode, data); | |
} | |
@Override | |
public void onUserSignedIn(GoogleSignInAccount account) { | |
Toaster.makeShort("onUserSignedIn()"); | |
authenticatedUser.onUserAuthenticated(account.getDisplayName(), account.getEmail(), account.getIdToken()); | |
if (account.getPhotoUrl() != null) authenticatedUser.setAvatarUrl(account.getPhotoUrl().toString()); | |
_googleSignInManager.getUserData(account); | |
loginToBackend(); | |
} | |
private void loginToBackend() { | |
RequestManager.getInstance().loginGoogle(new GoogleLoginBundle(authenticatedUser.getSocialToken(), authenticatedUser.getUser().getName(), | |
authenticatedUser.getEmail()), this); | |
} | |
@Override | |
public void onUserSignInFailed(int errorCode, String errorMessage) { | |
Toaster.makeShort("onUserSignInFailed() + errorCode = " + errorCode + ", message = " + errorMessage); | |
invalidateOptionsMenu(); | |
} | |
@Override | |
public void onUserDataRetrieved(@Nullable Person person) { | |
AuthenticatedUser authUser = AuthenticatedUser.getInstance(); | |
if (person != null) { | |
authUser.setUserData(person); | |
} | |
} | |
@Override | |
public void onUserSignedOut() { | |
Toaster.makeShort("onUserSignedOut()"); | |
} | |
@Override | |
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) { | |
if (response.isSuccess()) { | |
Toaster.makeShort("User is logged in!"); | |
LoginResponse loginResponse = response.body(); | |
authenticatedUser.onUserLoggedIn(loginResponse); | |
invalidateOptionsMenu(); | |
onUserLoggedIn(); | |
updateUserData(); | |
} else { | |
Toaster.makeShort("Login failed"); | |
} | |
} | |
@Override | |
public void onFailure(Call<LoginResponse> call, Throwable t) { | |
Toaster.makeShort("Login failed"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment