Skip to content

Instantly share code, notes, and snippets.

@PenzK
Created May 17, 2016 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PenzK/95e9c5a364043f0b45d9883e801ce424 to your computer and use it in GitHub Desktop.
Save PenzK/95e9c5a364043f0b45d9883e801ce424 to your computer and use it in GitHub Desktop.
package com.yalantis.foxrey.transporter.util;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.Scopes;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.Scope;
import com.yalantis.foxrey.transporter.R;
import timber.log.Timber;
/**
* @author Penzykov Kyrylo (06.05.2016).
*/
public class GoogleApiHelper implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
private GoogleApiClient mGoogleApiClient;
private Context mContext;
public GoogleApiHelper(Context context) {
mContext = context;
buildGoogleApiClient();
connect();
}
public GoogleApiClient getGoogleApiClient() {
return this.mGoogleApiClient;
}
@Override
public void onConnected(@Nullable Bundle bundle) {
Timber.d("onConnected: googleApiClient.connected");
}
@Override
public void onConnectionSuspended(int i) {
Timber.d("onConnectionSuspended: googleApiClient.connect()");
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
public void connect() {
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
}
public boolean isConnected() {
if (mGoogleApiClient != null) {
return mGoogleApiClient.isConnected();
} else {
return false;
}
}
public void disconnect() {
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
private void buildGoogleApiClient() {
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(new Scope(Scopes.PLUS_LOGIN))
.requestScopes(new Scope(Scopes.PLUS_ME))
.requestIdToken(mContext.getString(R.string.google_server_client_id))
.requestServerAuthCode(mContext.getString(R.string.google_server_client_id))
.requestEmail()
.build();
// Build a GoogleApiClient with access to the Google Sign-In API and the
// options specified by gso.
mGoogleApiClient = new GoogleApiClient.Builder(mContext)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment