Created
September 25, 2016 06:37
-
-
Save fnk0/facd2a72f47614350c1490a884fda598 to your computer and use it in GitHub Desktop.
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
package com.gabilheri.fithub.ui.linking; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.content.IntentSender; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import com.google.android.gms.common.ConnectionResult; | |
import com.google.android.gms.common.GooglePlayServicesUtil; | |
import com.google.android.gms.common.Scopes; | |
import com.google.android.gms.common.api.GoogleApiClient; | |
import com.google.android.gms.common.api.PendingResult; | |
import com.google.android.gms.common.api.Scope; | |
import com.google.android.gms.common.api.Status; | |
import com.google.android.gms.fitness.Fitness; | |
import hugo.weaving.DebugLog; | |
import timber.log.Timber; | |
/** | |
* Created by <a href="mailto:[email protected]">Marcus Gabilheri</a> | |
* | |
* @author Marcus Gabilheri | |
* @version 1.0 | |
* @since 9/6/16. | |
*/ | |
public class GoogleFitAuthManager implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { | |
public interface OnGoogleFitConnected { | |
void onGoogleFitConnected(); | |
} | |
public interface OnGoogleFitRemoved { | |
void onGoogleFitRemoved(); | |
} | |
protected GoogleApiClient mGoogleApiClient; | |
protected static final int REQUEST_OAUTH = 146; | |
protected boolean authInProgress = false; | |
protected OnGoogleFitConnected mOnGoogleFitConnected; | |
protected OnGoogleFitRemoved mOnGoogleFitRemoved; | |
protected Activity mActivity; | |
protected boolean isRemoving = false; | |
public GoogleFitAuthManager(Activity activity) { | |
mActivity = activity; | |
} | |
public GoogleFitAuthManager(Activity activity, OnGoogleFitConnected onGoogleFitConnected) { | |
mActivity = activity; | |
mOnGoogleFitConnected = onGoogleFitConnected; | |
} | |
public GoogleFitAuthManager(OnGoogleFitConnected onGoogleFitConnected, OnGoogleFitRemoved onGoogleFitRemoved, Activity activity) { | |
mOnGoogleFitConnected = onGoogleFitConnected; | |
mOnGoogleFitRemoved = onGoogleFitRemoved; | |
mActivity = activity; | |
} | |
public GoogleFitAuthManager setOnGoogleFitRemoved(OnGoogleFitRemoved onGoogleFitRemoved) { | |
mOnGoogleFitRemoved = onGoogleFitRemoved; | |
return this; | |
} | |
public void startApiClient() { | |
if (mGoogleApiClient == null) { | |
GoogleApiClient.Builder builder = new GoogleApiClient.Builder(mActivity); | |
builder.addApi(Fitness.HISTORY_API) | |
.addApi(Fitness.CONFIG_API) | |
.addScope(new Scope(Scopes.FITNESS_LOCATION_READ_WRITE)) | |
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE)); | |
builder.addConnectionCallbacks(this); | |
builder.addOnConnectionFailedListener(this); | |
mGoogleApiClient = builder.build(); | |
} | |
mGoogleApiClient.connect(); | |
} | |
@Override | |
public void onConnected(@Nullable Bundle bundle) { | |
if (mOnGoogleFitConnected != null) { | |
mOnGoogleFitConnected.onGoogleFitConnected(); | |
} | |
if (isRemoving) { | |
revokeGoogleFitAccess(); | |
} | |
} | |
@Override | |
public void onConnectionSuspended(int i) { | |
} | |
@Override | |
public void onConnectionFailed(@NonNull ConnectionResult result) { | |
Timber.d("Connection failed. Cause: " + result.toString()); | |
if (!result.hasResolution()) { | |
// Show the localized error dialog | |
if (mActivity != null) { | |
GooglePlayServicesUtil.showErrorDialogFragment(result.getErrorCode(), mActivity, null, 0, dialog -> { | |
}); | |
} | |
return; | |
} | |
// The failure has a resolution. Resolve it. | |
// Called typically when the app is not yet authorized, and an | |
// authorization dialog is displayed to the user. | |
if (!authInProgress) { | |
try { | |
Timber.i("Attempting to resolve failed connection"); | |
authInProgress = true; | |
result.startResolutionForResult(mActivity, REQUEST_OAUTH); | |
} catch (IntentSender.SendIntentException e) { | |
Timber.e(e, "Exception while starting resolution activity: %s", e.getMessage()); | |
} | |
} | |
} | |
public void onStart() { | |
// Connect to the Fitness API | |
Timber.d("Connecting..."); | |
if (mGoogleApiClient != null) { | |
if (!mGoogleApiClient.isConnecting()) { | |
mGoogleApiClient.connect(); | |
} | |
} | |
} | |
public void onStop() { | |
if (mGoogleApiClient != null) { | |
if (mGoogleApiClient.isConnected()) { | |
mGoogleApiClient.disconnect(); | |
} | |
} | |
} | |
public void onDestroy() { | |
mGoogleApiClient = null; | |
} | |
public void onPause() { | |
if (mGoogleApiClient != null) { | |
mGoogleApiClient.unregisterConnectionCallbacks(this); | |
mGoogleApiClient.unregisterConnectionFailedListener(this); | |
} | |
} | |
public void onResume() { | |
if (mGoogleApiClient != null) { | |
mGoogleApiClient.registerConnectionCallbacks(this); | |
mGoogleApiClient.registerConnectionFailedListener(this); | |
} | |
} | |
@DebugLog | |
void revokeGoogleFitAccess() { | |
if (mGoogleApiClient != null) { | |
if (mGoogleApiClient.isConnected()) { | |
PendingResult<Status> pendingResult = Fitness.ConfigApi.disableFit(mGoogleApiClient); | |
pendingResult.setResultCallback(status -> { | |
isRemoving = false; | |
mOnGoogleFitRemoved.onGoogleFitRemoved(); | |
}); | |
} | |
} else { | |
isRemoving = true; | |
startApiClient(); | |
} | |
} | |
public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
if (requestCode == REQUEST_OAUTH) { | |
authInProgress = false; | |
if (resultCode == Activity.RESULT_OK) { | |
if (mGoogleApiClient != null) { | |
// Make sure the app is not already connected or attempting to connect | |
if (!mGoogleApiClient.isConnecting() && !mGoogleApiClient.isConnected()) { | |
mGoogleApiClient.connect(); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment