package com.appdynamics.demo.gasp.activity; /** * Copyright (c) 2013 Mark Prichard * <p/> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p/> * http://www.apache.org/licenses/LICENSE-2.0 * <p/> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import android.content.Intent; import android.content.IntentSender; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.support.v4.app.FragmentActivity; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.TextView; import com.appdynamics.demo.gasp.R; import com.appdynamics.demo.gasp.fragment.PlayServicesDialogFragment; import com.google.android.gms.auth.GoogleAuthException; import com.google.android.gms.auth.GoogleAuthUtil; import com.google.android.gms.auth.GooglePlayServicesAvailabilityException; import com.google.android.gms.auth.UserRecoverableAuthException; 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.SignInButton; import com.google.android.gms.plus.PlusClient; import java.io.IOException; public class GoogleSignInActivity extends FragmentActivity implements OnClickListener, PlusClient.ConnectionCallbacks, PlusClient.OnConnectionFailedListener, PlusClient.OnAccessRevokedListener, PlayServicesDialogFragment.PlayServicesDialogListener { private static final String TAG = GoogleSignInActivity.class.getName(); private static final String scopes = "https://www.googleapis.com/auth/userinfo.email " + "https://www.googleapis.com/auth/plus.login " + "https://www.googleapis.com/auth/userinfo.profile"; private static final int REQUEST_CODE_SIGN_IN = 1; private static final int REQUEST_CODE_GET_GOOGLE_PLAY_SERVICES = 2; private TextView mSignInStatus; private PlusClient mPlusClient; private SignInButton mSignInButton; private View mSignOutButton; private View mRevokeAccessButton; private ConnectionResult mConnectionResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.google_signin_activity); mPlusClient = new PlusClient.Builder(this, this, this) .setScopes(Scopes.PLUS_LOGIN, scopes) .build(); mSignInStatus = (TextView) findViewById(R.id.sign_in_status); mSignInButton = (SignInButton) findViewById(R.id.sign_in_button); mSignInButton.setOnClickListener(this); mSignOutButton = findViewById(R.id.sign_out_button); mSignOutButton.setOnClickListener(this); mRevokeAccessButton = findViewById(R.id.revoke_access_button); mRevokeAccessButton.setOnClickListener(this); } @Override public void onStart() { super.onStart(); mPlusClient.connect(); } @Override public void onStop() { mPlusClient.disconnect(); super.onStop(); } @Override public void onClick(View view) { switch(view.getId()) { case R.id.sign_in_button: int available = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); if (available != ConnectionResult.SUCCESS) { //showDialog(DIALOG_GET_GOOGLE_PLAY_SERVICES); DialogFragment dialog = new PlayServicesDialogFragment(); dialog.show(getSupportFragmentManager(), "PlayServicesDialogFragment"); return; } try { mSignInStatus.setText(getString(R.string.signing_in_status)); mConnectionResult.startResolutionForResult(this, REQUEST_CODE_SIGN_IN); } catch (IntentSender.SendIntentException e) { // Fetch a new result to start. mPlusClient.connect(); } break; case R.id.sign_out_button: if (mPlusClient.isConnected()) { mPlusClient.clearDefaultAccount(); mPlusClient.disconnect(); mPlusClient.connect(); } break; case R.id.revoke_access_button: if (mPlusClient.isConnected()) { mPlusClient.revokeAccessAndDisconnect(this); updateButtons(false /* isSignedIn */); } break; } } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE_SIGN_IN || requestCode == REQUEST_CODE_GET_GOOGLE_PLAY_SERVICES) { if (resultCode == RESULT_OK && !mPlusClient.isConnected() && !mPlusClient.isConnecting()) { // This time, connect should succeed. mPlusClient.connect(); } } } @Override public void onAccessRevoked(ConnectionResult status) { if (status.isSuccess()) { mSignInStatus.setText(R.string.revoke_access_status); } else { mSignInStatus.setText(R.string.revoke_access_error_status); mPlusClient.disconnect(); } mPlusClient.connect(); } @Override public void onConnected(Bundle connectionHint) { String currentPersonName = mPlusClient.getCurrentPerson() != null ? mPlusClient.getCurrentPerson().getDisplayName() : getString(R.string.unknown_person); mSignInStatus.setText(getString(R.string.signed_in_status, currentPersonName)); Log.d(TAG, "Account Name: " + mPlusClient.getAccountName()); new AuthTokenAsyncTask().execute(); updateButtons(true /* isSignedIn */); } @Override public void onDisconnected() { mSignInStatus.setText(R.string.loading_status); mPlusClient.connect(); updateButtons(false /* isSignedIn */); } @Override public void onConnectionFailed(ConnectionResult result) { mConnectionResult = result; updateButtons(false /* isSignedIn */); } private void updateButtons(boolean isSignedIn) { if (isSignedIn) { mSignInButton.setVisibility(View.INVISIBLE); mSignOutButton.setEnabled(true); mRevokeAccessButton.setEnabled(true); } else { if (mConnectionResult == null) { // Disable the sign-in button until onConnectionFailed is called with result. mSignInButton.setVisibility(View.INVISIBLE); mSignInStatus.setText(getString(R.string.loading_status)); } else { // Enable the sign-in button since a connection result is available. mSignInButton.setVisibility(View.VISIBLE); mSignInStatus.setText(getString(R.string.signed_out_status)); } mSignOutButton.setEnabled(false); mRevokeAccessButton.setEnabled(false); } } private void getAuthToken() { try { final String token = GoogleAuthUtil.getToken(this, mPlusClient.getAccountName(), "oauth2:" + scopes); Log.d(TAG, "Token: " + token); } catch (GooglePlayServicesAvailabilityException playEx) { Log.e(TAG, playEx.getMessage()); } catch (UserRecoverableAuthException userAuthEx) { Log.e(TAG, "Exception: " + userAuthEx.getMessage()); } catch (IOException transientEx) { Log.e(TAG, "Exception: " + transientEx.getMessage()); } catch (GoogleAuthException authEx) { Log.e(TAG, "Exception: " + authEx.getMessage()); } } private class AuthTokenAsyncTask extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... params) { getAuthToken(); return null; } } }