Skip to content

Instantly share code, notes, and snippets.

@rsteckler
Last active August 29, 2015 14:15
Show Gist options
  • Save rsteckler/9f6bfe0e11e8e4ac0ee5 to your computer and use it in GitHub Desktop.
Save rsteckler/9f6bfe0e11e8e4ac0ee5 to your computer and use it in GitHub Desktop.
Fit running in a service
private void notifyUiFailedConnection(ConnectionResult result) {
Intent intent = new Intent(FIT_NOTIFY_INTENT);
intent.putExtra(FIT_EXTRA_NOTIFY_FAILED_STATUS_CODE, result.getErrorCode());
intent.putExtra(FIT_EXTRA_NOTIFY_FAILED_INTENT, result.getResolution());
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
private BroadcastReceiver mFitStatusReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
if (intent.hasExtra(GoogleFitService.FIT_EXTRA_NOTIFY_FAILED_STATUS_CODE) &&
intent.hasExtra(GoogleFitService.FIT_EXTRA_NOTIFY_FAILED_STATUS_CODE)) {
//Recreate the connection result
int statusCode = intent.getIntExtra(GoogleFitService.FIT_EXTRA_NOTIFY_FAILED_STATUS_CODE, 0);
PendingIntent pendingIntent = intent.getParcelableExtra(GoogleFitService.FIT_EXTRA_NOTIFY_FAILED_INTENT);
ConnectionResult result = new ConnectionResult(statusCode, pendingIntent);
Log.d(TAG, "Fit connection failed - opening connect screen.");
fitHandleFailedConnection(result);
}
}
};
private void fitHandleFailedConnection(ConnectionResult result) {
Log.i(TAG, "Activity Thread Google Fit Connection failed. Cause: " + result.toString());
if (!result.hasResolution()) {
// Show the localized error dialog
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), MainActivity.this, 0).show();
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) {
Log.i(TAG, "Activity Thread Google Fit Attempting to resolve failed connection");
try {
authInProgress = true;
mFitResultResolution = result;
mFitResultResolution.startResolutionForResult(MainActivity.this, REQUEST_OAUTH);
} catch (IntentSender.SendIntentException e) {
Log.e(TAG,
"Activity Thread Google Fit Exception while starting resolution activity", e);
}
}
}
}
private void fitActivityResult(int requestCode, int resultCode) {
if (requestCode == REQUEST_OAUTH) {
try {
authInProgress = true;
mFitResultResolution.startResolutionForResult(MainActivity.this, REQUEST_OAUTH);
} catch (IntentSender.SendIntentException e) {
Log.e(TAG,
"Activity Thread Google Fit Exception while starting resolution activity", e);
}
}
}
private void fitSaveInstanceState(Bundle outState) {
outState.putBoolean(AUTH_PENDING, authInProgress);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment