2016-05-14 14:26:44 +02:00
|
|
|
package org.mariotaku.twidere.activity;
|
|
|
|
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
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.ConnectionResult;
|
|
|
|
import com.google.android.gms.common.api.GoogleApiClient;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by mariotaku on 16/5/14.
|
|
|
|
*/
|
2016-05-15 12:18:04 +02:00
|
|
|
public class PlusServiceGoogleSignInActivity extends BasePlusServiceSignInActivity implements
|
2016-05-14 14:26:44 +02:00
|
|
|
GoogleApiClient.OnConnectionFailedListener {
|
|
|
|
|
|
|
|
private static final int REQUEST_GOOGLE_SIGN_IN = 101;
|
|
|
|
|
|
|
|
private GoogleApiClient mGoogleApiClient;
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
// Configure sign-in to request the user's ID, email address, and basic
|
|
|
|
// profile. ID and basic profile are included in DEFAULT_SIGN_IN.
|
|
|
|
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
|
|
|
|
.requestEmail()
|
|
|
|
.requestIdToken(GOOGLE_APIS_SERVER_CLIENT_ID)
|
|
|
|
.build();
|
|
|
|
// Build a GoogleApiClient with access to the Google Sign-In API and the
|
|
|
|
// options specified by gso.
|
|
|
|
mGoogleApiClient = new GoogleApiClient.Builder(this)
|
|
|
|
.enableAutoManage(this, this)
|
|
|
|
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
|
|
|
|
.build();
|
|
|
|
signInWithGoogle();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
|
|
|
|
|
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
|
|
|
|
if (requestCode == REQUEST_GOOGLE_SIGN_IN) {
|
|
|
|
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
|
|
|
|
handleSignInResult(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void signInWithGoogle() {
|
|
|
|
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
|
|
|
|
startActivityForResult(signInIntent, REQUEST_GOOGLE_SIGN_IN);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleSignInResult(GoogleSignInResult result) {
|
|
|
|
Log.d(LOGTAG, "handleSignInResult:" + result.isSuccess());
|
|
|
|
if (result.isSuccess()) {
|
|
|
|
// TODO Signed in successfully, show authenticated UI.
|
|
|
|
GoogleSignInAccount acct = result.getSignInAccount();
|
|
|
|
acct.getIdToken();
|
|
|
|
Log.d(LOGTAG, "sign in name:" + acct.getDisplayName());
|
|
|
|
} else {
|
|
|
|
// TODO Signed out, show unauthenticated UI.
|
|
|
|
}
|
2016-05-15 12:18:04 +02:00
|
|
|
finish();
|
2016-05-14 14:26:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|