|
| 1 | +// login firebase with play services |
| 2 | +//1-- add to gradle |
| 3 | + compile 'com.google.firebase:firebase-auth:9.6.1' |
| 4 | + compile 'com.google.android.gms:play-services-auth:9.6.1' |
| 5 | + |
| 6 | + //1- extend |
| 7 | + GoogleApiClient.OnConnectionFailedListener |
| 8 | + //2 - define in public |
| 9 | + |
| 10 | + |
| 11 | + private static final String TAG = "GoogleActivity"; |
| 12 | + private static final int RC_SIGN_IN = 9001; |
| 13 | + |
| 14 | + // [START declare_auth] |
| 15 | + private FirebaseAuth mAuth; |
| 16 | + // [END declare_auth] |
| 17 | + |
| 18 | + // [START declare_auth_listener] |
| 19 | + private FirebaseAuth.AuthStateListener mAuthListener; |
| 20 | + // [END declare_auth_listener] |
| 21 | + |
| 22 | + private GoogleApiClient mGoogleApiClient; |
| 23 | + |
| 24 | + |
| 25 | + //3- onCreate add |
| 26 | + |
| 27 | + |
| 28 | + // [START config_signin] |
| 29 | + // Configure Google Sign In |
| 30 | + GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) |
| 31 | + .requestIdToken(getString(R.string.default_web_client_id)) |
| 32 | + .requestEmail() |
| 33 | + .build(); |
| 34 | + // [END config_signin] |
| 35 | + |
| 36 | + mGoogleApiClient = new GoogleApiClient.Builder(this) |
| 37 | + .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */) |
| 38 | + .addApi(Auth.GOOGLE_SIGN_IN_API, gso) |
| 39 | + .build(); |
| 40 | + |
| 41 | + // [START initialize_auth] |
| 42 | + mAuth = FirebaseAuth.getInstance(); |
| 43 | + // [END initialize_auth] |
| 44 | + |
| 45 | + // [START auth_state_listener] |
| 46 | + mAuthListener = new FirebaseAuth.AuthStateListener() { |
| 47 | + @Override |
| 48 | + public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { |
| 49 | + FirebaseUser user = firebaseAuth.getCurrentUser(); |
| 50 | + if (user != null) { |
| 51 | + // User is signed in |
| 52 | + Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid()); |
| 53 | + } else { |
| 54 | + // User is signed out |
| 55 | + Log.d(TAG, "onAuthStateChanged:signed_out"); |
| 56 | + } |
| 57 | + // [START_EXCLUDE] |
| 58 | + updateUI(user); |
| 59 | + // [END_EXCLUDE] |
| 60 | + } |
| 61 | + }; |
| 62 | + // [END auth_state_listener] |
| 63 | + |
| 64 | +//4 deine this methods |
| 65 | + // [START on_start_add_listener] |
| 66 | + @Override |
| 67 | + public void onStart() { |
| 68 | + super.onStart(); |
| 69 | + mAuth.addAuthStateListener(mAuthListener); |
| 70 | + } |
| 71 | + // [END on_start_add_listener] |
| 72 | + |
| 73 | + // [START on_stop_remove_listener] |
| 74 | + @Override |
| 75 | + public void onStop() { |
| 76 | + super.onStop(); |
| 77 | + if (mAuthListener != null) { |
| 78 | + mAuth.removeAuthStateListener(mAuthListener); |
| 79 | + } |
| 80 | + } |
| 81 | + // [END on_stop_remove_listener] |
| 82 | + |
| 83 | + // [START onactivityresult] |
| 84 | + @Override |
| 85 | + public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 86 | + super.onActivityResult(requestCode, resultCode, data); |
| 87 | + |
| 88 | + // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...); |
| 89 | + if (requestCode == RC_SIGN_IN) { |
| 90 | + GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); |
| 91 | + if (result.isSuccess()) { |
| 92 | + // Google Sign In was successful, authenticate with Firebase |
| 93 | + GoogleSignInAccount account = result.getSignInAccount(); |
| 94 | + firebaseAuthWithGoogle(account); |
| 95 | + } else { |
| 96 | + // Google Sign In failed, update UI appropriately |
| 97 | + // [START_EXCLUDE] |
| 98 | + updateUI(null); |
| 99 | + // [END_EXCLUDE] |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + // [END onactivityresult] |
| 104 | + |
| 105 | + // [START auth_with_google] |
| 106 | + private void firebaseAuthWithGoogle(GoogleSignInAccount acct) { |
| 107 | + Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId()); |
| 108 | + // [START_EXCLUDE silent] |
| 109 | + showProgressDialog(); |
| 110 | + // [END_EXCLUDE] |
| 111 | + |
| 112 | + AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null); |
| 113 | + mAuth.signInWithCredential(credential) |
| 114 | + .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { |
| 115 | + @Override |
| 116 | + public void onComplete(@NonNull Task<AuthResult> task) { |
| 117 | + Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful()); |
| 118 | + |
| 119 | + // If sign in fails, display a message to the user. If sign in succeeds |
| 120 | + // the auth state listener will be notified and logic to handle the |
| 121 | + // signed in user can be handled in the listener. |
| 122 | + if (!task.isSuccessful()) { |
| 123 | + Log.w(TAG, "signInWithCredential", task.getException()); |
| 124 | + Toast.makeText(GoogleSignInActivity.this, "Authentication failed.", |
| 125 | + Toast.LENGTH_SHORT).show(); |
| 126 | + } |
| 127 | + // [START_EXCLUDE] |
| 128 | + hideProgressDialog(); |
| 129 | + // [END_EXCLUDE] |
| 130 | + } |
| 131 | + }); |
| 132 | + } |
| 133 | + // [END auth_with_google] |
| 134 | + |
| 135 | + // [START signin] |
| 136 | + private void signIn() { |
| 137 | + Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); |
| 138 | + startActivityForResult(signInIntent, RC_SIGN_IN); |
| 139 | + } |
| 140 | + // [END signin] |
| 141 | + |
| 142 | + private void signOut() { |
| 143 | + // Firebase sign out |
| 144 | + mAuth.signOut(); |
| 145 | + |
| 146 | + // Google sign out |
| 147 | + Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback( |
| 148 | + new ResultCallback<Status>() { |
| 149 | + @Override |
| 150 | + public void onResult(@NonNull Status status) { |
| 151 | + updateUI(null); |
| 152 | + } |
| 153 | + }); |
| 154 | + } |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + private void updateUI(FirebaseUser user) { |
| 159 | + hideProgressDialog(); |
| 160 | + String Email=user.getEmail(); |
| 161 | + String Uid= user.getUid(); |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public void onConnected(Bundle connectionHint) { |
| 166 | + |
| 167 | + Log.d(TAG, "onConnection is connected:" ); |
| 168 | + } |
| 169 | + @Override |
| 170 | + public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { |
| 171 | + // An unresolvable error has occurred and Google APIs (including Sign-In) will not |
| 172 | + // be available. |
| 173 | + Log.d(TAG, "onConnectionFailed:" + connectionResult); |
| 174 | + Toast.makeText(this, "Google Play Services error.", Toast.LENGTH_SHORT).show(); |
| 175 | + } |
| 176 | + |
| 177 | + |
| 178 | + @VisibleForTesting |
| 179 | + public ProgressDialog mProgressDialog; |
| 180 | + |
| 181 | + public void showProgressDialog() { |
| 182 | + if (mProgressDialog == null) { |
| 183 | + mProgressDialog = new ProgressDialog(this); |
| 184 | + mProgressDialog.setMessage(getString(R.string.loading)); |
| 185 | + mProgressDialog.setIndeterminate(true); |
| 186 | + } |
| 187 | + |
| 188 | + mProgressDialog.show(); |
| 189 | + } |
| 190 | + |
| 191 | + public void hideProgressDialog() { |
| 192 | + if (mProgressDialog != null && mProgressDialog.isShowing()) { |
| 193 | + mProgressDialog.dismiss(); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + @Override |
| 198 | + public void onStop() { |
| 199 | + super.onStop(); |
| 200 | + hideProgressDialog(); |
| 201 | + } |
0 commit comments