import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
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;
import com.google.android.gms.common.api.OptionalPendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
/**
* Created by Kir on 05.10.2017.
*
* Deploying :
*
* public class SignInActivity extends AppCompatActivity { ... }
* GoogleAuth googleAuth;
* @Override protected void onCreate(Bundle savedInstanceState) {
* googleAuth = new GoogleAuth();
* googleAuth.configure(this,this, new GoogleAuth.Callback(){ ... }
* }
*
* @Override
* public void onStart() {
* super.onStart();
* googleAuth.start();
* }
* @Override
* public void onActivityResult(int requestCode, int resultCode, Intent data) {
* super.onActivityResult(requestCode, resultCode, data);
* googleAuth.getResult(requestCode,data);
* }
*
* Button actions :
* signIn()
* signOut()
* revokeAccess()
*
*/
public class GoogleAuth {
static final String TAG = "GoogleAuth";
private static final int RC_SIGN_IN = 9001;
private GoogleApiClient mGoogleApiClient;
private Context context;
private FragmentActivity fragment;
public Callback callback;
public void configure(Context c, FragmentActivity f, Callback cb){
context = c;
fragment = f;
callback = cb;
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
GoogleApiClient.OnConnectionFailedListener failedListener = new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
callback.connectionFailed(connectionResult);
}
};
// Build a GoogleApiClient with access to the Google Sign-In API and the
// options specified by gso.
mGoogleApiClient = new GoogleApiClient.Builder(context)
.enableAutoManage(fragment,failedListener)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
}
public void start(){
OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if (opr.isDone()) {
// If the user's cached credentials are valid, the OptionalPendingResult will be "done"
// and the GoogleSignInResult will be available instantly.
Log.d(TAG, "Got cached sign-in");
GoogleSignInResult result = opr.get();
handleResult(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(GoogleSignInResult googleSignInResult) {
handleResult(googleSignInResult);
}
});
}
}
public void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
fragment.startActivityForResult(signInIntent, RC_SIGN_IN);
}
public void signOut(){
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
callback.signOut(status);
}
});
}
public void getResult(int requestCode, Intent data){
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleResult(result);
}
}
private void handleResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
GoogleSignInAccount acct = result.getSignInAccount();
callback.success(acct);
} else {
// Signed out, show unauthenticated UI.
callback.failed(result);
}
}
/** Disconnect */
public void revokeAccess() {
Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
callback.revokeAccess(status);
}
});
}
public interface Callback {
void connectionFailed(ConnectionResult c);
void success(GoogleSignInAccount g);
void revokeAccess(Status status);
void failed(GoogleSignInResult result);
void signOut(Status status);
}
}