Last active
April 25, 2017 15:24
-
-
Save deda9/a50b698d2b6c4c198406798e1451c5e7 to your computer and use it in GitHub Desktop.
How to Integrate Google Plus Login with android.
This file contains hidden or 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
import android.app.Activity; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.support.v4.app.FragmentActivity; | |
import com.apptcom.external.logging.Logger; | |
import com.apptcom.external.model.UserSocialModel; | |
import com.google.android.gms.auth.api.Auth; | |
import com.google.android.gms.auth.api.signin.GoogleSignInAccount; | |
import com.google.android.gms.auth.api.signin.GoogleSignInOptions; | |
import com.google.android.gms.auth.api.signin.GoogleSignInResult; | |
import com.google.android.gms.common.api.GoogleApiClient; | |
import com.google.android.gms.common.api.OptionalPendingResult; | |
import com.google.android.gms.common.api.ResultCallback; | |
import com.google.android.gms.common.api.Status; | |
import com.google.android.gms.plus.Plus; | |
import com.google.android.gms.plus.model.people.Person; | |
import java.lang.ref.WeakReference; | |
public class GooglePlusLogin { | |
public static final int GOOGLE_SIGN_IN = 9001; | |
private GoogleApiClient mGoogleApiClient; | |
private WeakReference<Context> weakReference; | |
private GoogleApiClient.OnConnectionFailedListener mOnConnectionFailedListener; | |
private GooglePlusLoginListener successListener; | |
public GooglePlusLogin(Context context, GoogleApiClient.OnConnectionFailedListener onConnectionFailedListener) { | |
this.weakReference = new WeakReference<>(context); | |
this.mOnConnectionFailedListener = onConnectionFailedListener; | |
} | |
public GoogleApiClient getGoogleApiClient() { | |
return mGoogleApiClient; | |
} | |
public void setOnSuccessListener(GooglePlusLoginListener successListener) { | |
this.successListener = successListener; | |
} | |
public void setupGooglePlusClient() { | |
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) | |
.requestEmail() | |
.build(); | |
if (mGoogleApiClient == null) | |
mGoogleApiClient = new GoogleApiClient.Builder(weakReference.get()) | |
.enableAutoManage((FragmentActivity) weakReference.get(), mOnConnectionFailedListener) | |
.addApi(Auth.GOOGLE_SIGN_IN_API, gso) | |
.addApi(Plus.API, Plus.PlusOptions.builder().build()) | |
.build(); | |
} | |
public void loginWithGooglePlus() { | |
if (mGoogleApiClient != null) { | |
OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient); | |
if (opr.isDone()) { | |
logOutGooglePlusConnection(); | |
} | |
} | |
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); | |
((Activity) weakReference.get()).startActivityForResult(signInIntent, GOOGLE_SIGN_IN); | |
} | |
public void onActivityResultSuccess(Intent data) { | |
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); | |
handleSignInResult(result); | |
} | |
private void handleSignInResult(GoogleSignInResult result) { | |
UserSocialModel userModel; | |
if (result.isSuccess()) { | |
userModel = new UserSocialModel(); | |
GoogleSignInAccount userAccount = result.getSignInAccount(); | |
userModel.name = userAccount.getDisplayName(); | |
userModel.email = userAccount.getEmail(); | |
userModel.photo = userAccount.getPhotoUrl().toString(); | |
try { | |
Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient); | |
userModel.about = currentPerson.getAboutMe(); | |
userModel.link = currentPerson.getUrl(); | |
userModel.currentCity = currentPerson.getCurrentLocation(); | |
if (currentPerson.hasOrganizations()) | |
userModel.work = currentPerson.getOrganizations().get(0).getName(); | |
} catch (Exception error) { | |
Logger.error("GooglePlusLogin", "Error while getting Plus.PeopleApi.getCurrentPerson"); | |
} | |
if (successListener != null) | |
successListener.onGooglePlusSuccess(userModel); | |
} | |
} | |
public void logOutGooglePlusConnection() { | |
if (mGoogleApiClient.isConnected()) { | |
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback( | |
new ResultCallback<Status>() { | |
@Override | |
public void onResult(Status status) { | |
} | |
}); | |
Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback( | |
new ResultCallback<Status>() { | |
@Override | |
public void onResult(Status status) { | |
} | |
}); | |
} | |
} | |
public interface GooglePlusLoginListener { | |
void onGooglePlusSuccess(UserSocialModel userModel); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment