Skip to content

Commit

Permalink
Convert identity samples for new connectionless API
Browse files Browse the repository at this point in the history
  • Loading branch information
kroikie committed Nov 6, 2017
1 parent 768508f commit 2c3f906
Show file tree
Hide file tree
Showing 10 changed files with 281 additions and 440 deletions.
2 changes: 1 addition & 1 deletion android/analytics/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ dependencies {
compile 'com.android.support:support-v4:25.3.1'

// [START gms_compile]
compile 'com.google.android.gms:play-services-analytics:11.0.1'
compile 'com.google.android.gms:play-services-analytics:11.6.0'
// [END gms_compile]
}
2 changes: 1 addition & 1 deletion android/gcm/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.android.gms:play-services-gcm:11.0.1'
compile 'com.google.android.gms:play-services-gcm:11.6.0'

compile 'com.android.support:appcompat-v7:25.3.1'

Expand Down
11 changes: 0 additions & 11 deletions android/signin/.google/packaging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,6 @@ level: BEGINNER
# Dimensions: 512x512, PNG fomrat
icon: app/src/main/res/drawable/ic_launcher_big.png

# List of APIs that this sample should be listed under. Use authoritive,
# names that are unique for the product in question. Examples:
#
# Android - android:<class-name>
# App Engine - gae-java:<class name>
# gae-python:<package>
# Web Services - ws:<name of API from Cloud Console>
apiRefs:
- android:com.google.android.gms.common.api.GoogleApiClient
- android:com.google.android.gms.plus.Plusl

# Default: apache2. May be omitted for most samples.
# Alternatives: apache2-android (for AOSP)
license: apache2
2 changes: 1 addition & 1 deletion android/signin/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Google Sign-In Quickstart
=========================

The Google Sign-In Android quickstart demonstrates how to authenticate a user with GoogleApiClient.
The Google Sign-In Android quickstart demonstrates how to authenticate a user with GoogleSignInClient.

Introduction
------------
Expand Down
2 changes: 1 addition & 1 deletion android/signin/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ dependencies {
compile 'com.android.support:design:26.1.0'

// Dependency for Google Sign-In
compile 'com.google.android.gms:play-services-auth:11.4.0'
compile 'com.google.android.gms:play-services-auth:11.6.0'

// Dependencies for the REST API example
compile 'com.google.api-client:google-api-client:1.22.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,32 @@
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.OptionalPendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;

/**
* Demonstrates retrieving an ID token for the current Google user.
*/
public class IdTokenActivity extends AppCompatActivity implements
GoogleApiClient.OnConnectionFailedListener,
View.OnClickListener {

private static final String TAG = "IdTokenActivity";
private static final int RC_GET_TOKEN = 9002;

private GoogleApiClient mGoogleApiClient;
private GoogleSignInClient mGoogleSignInClient;
private TextView mIdTokenTextView;
private Button mRefreshButton;

Expand Down Expand Up @@ -64,71 +63,64 @@ public void onCreate(Bundle savedInstanceState) {
// [END configure_signin]

// Build GoogleAPIClient with the Google Sign-In API and the above options.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
}

private void getIdToken() {
// Show an account picker to let the user choose a Google account from the device.
// If the GoogleSignInOptions only asks for IDToken and/or profile and/or email then no
// consent screen will be shown here.
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_GET_TOKEN);
}

private void refreshIdToken() {
OptionalPendingResult<GoogleSignInResult> opr =
Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);

if (opr.isDone()) {
// Users cached credentials are valid, GoogleSignInResult containing ID token
// is available immediately. This likely means the current ID token is already
// fresh and can be sent to your server.
GoogleSignInResult result = opr.get();
handleSignInResult(result);
} else {
// If the user has not previously signed in on this device or the sign-in has expired,
// this asynchronous branch will attempt to sign in the user silently and get a valid
// ID token. Cross-device single sign on will occur in this branch.
opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(@NonNull GoogleSignInResult result) {
handleSignInResult(result);
}
});
}
// Attempt to silently refresh the GoogleSignInAccount. If the GoogleSignInAccount
// already has a valid token this method may complete immediately.
//
// If the user has not previously signed in on this device or the sign-in has expired,
// this asynchronous branch will attempt to sign in the user silently and get a valid
// ID token. Cross-device single sign on will occur in this branch.
mGoogleSignInClient.silentSignIn()
.addOnCompleteListener(this, new OnCompleteListener<GoogleSignInAccount>() {
@Override
public void onComplete(@NonNull Task<GoogleSignInAccount> task) {
handleSignInResult(task);
}
});
}

private void handleSignInResult(GoogleSignInResult result) {
if (result.isSuccess()) {
String idToken = result.getSignInAccount().getIdToken();
mIdTokenTextView.setText(getString(R.string.id_token_fmt, idToken));
updateUI(true);
} else {
updateUI(false);
// [START handle_sign_in_result]
private void handleSignInResult(@NonNull Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
String idToken = account.getIdToken();

// TODO(developer): send ID Token to server and validate

updateUI(account);
} catch (ApiException e) {
Log.w(TAG, "handleSignInResult:error", e);
updateUI(null);
}
}
// [END handle_sign_in_result]

private void signOut() {
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
Log.d(TAG, "signOut:onResult:" + status);
updateUI(false);
}
});
mGoogleSignInClient.signOut().addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
updateUI(null);
}
});
}

private void revokeAccess() {
Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
mGoogleSignInClient.revokeAccess().addOnCompleteListener(this,
new OnCompleteListener<Void>() {
@Override
public void onResult(Status status) {
Log.d(TAG, "revokeAccess:onResult:" + status);
updateUI(false);
public void onComplete(@NonNull Task<Void> task) {
updateUI(null);
}
});
}
Expand All @@ -139,30 +131,21 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == RC_GET_TOKEN) {
// [START get_id_token]
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
Log.d(TAG, "onActivityResult:GET_TOKEN:success:" + result.getStatus().isSuccess());

if (result.isSuccess()) {
String idToken = result.getSignInAccount().getIdToken();
// TODO(developer): send token to server and validate
}
// This task is always completed immediately, there is no need to attach an
// asynchronous listener.
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
// [END get_id_token]

handleSignInResult(result);
}
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
// An unresolvable error has occurred and Google APIs (including Sign-In) will not
// be available.
Log.d(TAG, "onConnectionFailed:" + connectionResult);
}

private void updateUI(boolean signedIn) {
if (signedIn) {
private void updateUI(@Nullable GoogleSignInAccount account) {
if (account != null) {
((TextView) findViewById(R.id.status)).setText(R.string.signed_in);

String idToken = account.getIdToken();
mIdTokenTextView.setText(getString(R.string.id_token_fmt, idToken));

findViewById(R.id.sign_in_button).setVisibility(View.GONE);
findViewById(R.id.sign_out_and_disconnect).setVisibility(View.VISIBLE);
mRefreshButton.setVisibility(View.VISIBLE);
Expand Down
Loading

0 comments on commit 2c3f906

Please sign in to comment.