Created
May 17, 2016 21:18
-
-
Save PenzK/8950a81b489fdf98e24666de213ee26e to your computer and use it in GitHub Desktop.
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
/** | |
* @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