Skip to content
This repository has been archived by the owner on Sep 19, 2019. It is now read-only.

Commit

Permalink
Save logic
Browse files Browse the repository at this point in the history
  • Loading branch information
samtstern committed Nov 18, 2015
1 parent 18a7567 commit 72abe11
Showing 1 changed file with 23 additions and 27 deletions.
Expand Up @@ -39,6 +39,7 @@
import com.google.android.gms.common.api.CommonStatusCodes;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.OptionalPendingResult;
import com.google.android.gms.common.api.ResolvingResultCallbacks;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;

Expand Down Expand Up @@ -150,7 +151,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {

@Override
public void onConnected(Bundle bundle) {
saveGoogleCredential(mCredentialToSave);
saveCredentialIfConnected(mCredentialToSave);
}

@Override
Expand Down Expand Up @@ -214,7 +215,7 @@ private void handleGoogleSignIn(GoogleSignInResult gsr) {
.setProfilePictureUri(gsa.getPhotoUrl())
.build();

saveGoogleCredential(credential);
saveCredentialIfConnected(credential);
} else {
// Display signed-out UI
((TextView) findViewById(R.id.text_google_status)).setText(R.string.signed_out);
Expand Down Expand Up @@ -265,23 +266,30 @@ private void resolveResult(Status status, int requestCode) {
}
}

private void saveGoogleCredential(Credential credential) {
private void saveCredentialIfConnected(Credential credential) {
if (credential == null) {
return;
}

// Save Credential if the GoogleApiClient is connected, otherwise the
// Credential is cached and will be saved when onConnected is next called.
mCredentialToSave = credential;
Auth.CredentialsApi.save(mGoogleApiClient, credential).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
// This save should never need resolving because the Google Account
// must correspond to an account on the device, so it should succeed
// without prompt.
Log.d(TAG, "Save Google Credential: " + status);
mCredentialToSave = null;
}
});
if (mGoogleApiClient.isConnected()) {
Auth.CredentialsApi.save(mGoogleApiClient, mCredentialToSave).setResultCallback(
new ResolvingResultCallbacks<Status>(this, RC_CREDENTIALS_SAVE) {
@Override
public void onSuccess(Status status) {
Log.d(TAG, "save:SUCCESS:" + status);
mCredentialToSave = null;
}

@Override
public void onUnresolvableFailure(Status status) {
Log.w(TAG, "save:FAILURE:" + status);
mCredentialToSave = null;
}
});
}
}

private void onGoogleSignInClicked() {
Expand Down Expand Up @@ -331,19 +339,7 @@ private void onEmailSaveClicked() {
.setPassword(password)
.build();

showProgress();
Auth.CredentialsApi.save(mGoogleApiClient, credential).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
hideProgress();
if (status.isSuccess()) {
Toast.makeText(MainActivity.this, "Saved", Toast.LENGTH_SHORT).show();
} else if (status.hasResolution()) {
resolveResult(status, RC_CREDENTIALS_SAVE);
}
}
});
saveCredentialIfConnected(credential);
}

@Override
Expand Down

0 comments on commit 72abe11

Please sign in to comment.