Advertisement
Guest User

Login

a guest
Feb 3rd, 2015
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.18 KB | None | 0 0
  1. package com.appdev.ngoapp;
  2.  
  3. import android.app.Activity;
  4. import android.app.ProgressDialog;
  5. import android.content.Intent;
  6. import android.content.IntentSender;
  7. import android.os.Bundle;
  8. import android.view.View;
  9.  
  10. import com.google.android.gms.common.ConnectionResult;
  11. import com.google.android.gms.common.GooglePlayServicesUtil;
  12. import com.google.android.gms.common.SignInButton;
  13. import com.google.android.gms.common.api.GoogleApiClient;
  14. import com.google.android.gms.plus.Plus;
  15.  
  16. /**
  17.  * Created by Adarsh on 01-02-2015.
  18.  */
  19. public class LoginActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
  20.  
  21.     private GoogleApiClient googleApiClient;
  22.     boolean intentInProgress;
  23.     boolean signInClicked;
  24.     private ConnectionResult connectionResult;
  25.     private SignInButton signInButton;
  26.     private ProgressDialog progressDialog;
  27.     boolean dialogFlag=false;
  28.  
  29.     @Override
  30.     protected void onCreate(Bundle savedInstanceState) {
  31.         super.onCreate(savedInstanceState);
  32.         setContentView(R.layout.login_layout);
  33.         signInButton = (SignInButton) findViewById(R.id.signin);
  34.         signInButton.setOnClickListener(new View.OnClickListener() {
  35.             @Override
  36.             public void onClick(View view) {
  37.                 progressDialog = ProgressDialog.show(LoginActivity.this, "Please Wait", "Logging In", true);
  38.                 progressDialog.setCancelable(false);
  39.                 dialogFlag=true;
  40.                 signInWithGplus();
  41.             }
  42.         });
  43.  
  44.         googleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(Plus.API).addScope(Plus.SCOPE_PLUS_LOGIN).build();
  45.     }
  46.  
  47.  
  48.     protected void onStart() {
  49.         super.onStart();
  50.         googleApiClient.connect();
  51.     }
  52.  
  53.  
  54.     protected void onStop() {
  55.         super.onStop();
  56.         if(googleApiClient.isConnected()) {
  57.             googleApiClient.disconnect();
  58.         }
  59.     }
  60.  
  61.     @Override
  62.     public void onConnectionFailed(ConnectionResult result) {
  63.         if (!result.hasResolution()) {
  64.             GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
  65.                     0).show();
  66.             return;
  67.         }
  68.  
  69.         if (!intentInProgress) {
  70.             // Store the ConnectionResult for later usage
  71.             connectionResult = result;
  72.  
  73.             if (signInClicked) {
  74.                 // The user has already clicked 'sign-in' so we attempt to
  75.                 // resolve all
  76.                 // errors until the user is signed in, or they cancel.
  77.                 resolveSignInError();
  78.             }
  79.         }
  80.     }
  81.  
  82.     @Override
  83.     protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
  84.         if (requestCode == 0) {
  85.             if (responseCode != RESULT_OK) {
  86.                 signInClicked = false;
  87.             }
  88.  
  89.             intentInProgress = false;
  90.  
  91.             if (!googleApiClient.isConnecting()) {
  92.                 googleApiClient.connect();
  93.             }
  94.         }
  95.     }
  96.  
  97.     @Override
  98.     public void onConnected(Bundle arg0) {
  99.         if(dialogFlag) {
  100.             progressDialog.dismiss();
  101.         }
  102.         signInClicked = false;
  103.         String email = Plus.AccountApi.getAccountName(googleApiClient);
  104.         Intent mainActivity =  new Intent(this,MainActivity.class);
  105.         mainActivity.putExtra("email",email);
  106.         mainActivity.putExtra("code",1);
  107.         startActivity(mainActivity);
  108.     }
  109.  
  110.     @Override
  111.     public void onConnectionSuspended(int arg0) {
  112.  
  113.         googleApiClient.connect();
  114.     }
  115.  
  116.     private void signInWithGplus() {
  117.         if (!googleApiClient.isConnecting()) {
  118.             signInClicked = true;
  119.             resolveSignInError();
  120.         }
  121.     }
  122.  
  123.     private void resolveSignInError() {
  124.         if (connectionResult.hasResolution()) {
  125.             try {
  126.                 intentInProgress = true;
  127.                 connectionResult.startResolutionForResult(this, 0);
  128.             } catch (IntentSender.SendIntentException e) {
  129.                 intentInProgress = false;
  130.                 googleApiClient.connect();
  131.             }
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement