forked from hussien89aa/AndroidTutorialForBeginners
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
hussienalrubaye
committed
Oct 23, 2016
1 parent
dde62ab
commit 0fc1974
Showing
2 changed files
with
361 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
// login firebase with play services | ||
//1-- add to gradle | ||
compile 'com.google.firebase:firebase-auth:9.6.1' | ||
compile 'com.google.android.gms:play-services-auth:9.6.1' | ||
|
||
//1- extend | ||
GoogleApiClient.OnConnectionFailedListener | ||
//2 - define in public | ||
|
||
|
||
private static final String TAG = "GoogleActivity"; | ||
private static final int RC_SIGN_IN = 9001; | ||
|
||
// [START declare_auth] | ||
private FirebaseAuth mAuth; | ||
// [END declare_auth] | ||
|
||
// [START declare_auth_listener] | ||
private FirebaseAuth.AuthStateListener mAuthListener; | ||
// [END declare_auth_listener] | ||
|
||
private GoogleApiClient mGoogleApiClient; | ||
|
||
|
||
//3- onCreate add | ||
|
||
|
||
// [START config_signin] | ||
// Configure Google Sign In | ||
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) | ||
.requestIdToken(getString(R.string.default_web_client_id)) | ||
.requestEmail() | ||
.build(); | ||
// [END config_signin] | ||
|
||
mGoogleApiClient = new GoogleApiClient.Builder(this) | ||
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */) | ||
.addApi(Auth.GOOGLE_SIGN_IN_API, gso) | ||
.build(); | ||
|
||
// [START initialize_auth] | ||
mAuth = FirebaseAuth.getInstance(); | ||
// [END initialize_auth] | ||
|
||
// [START auth_state_listener] | ||
mAuthListener = new FirebaseAuth.AuthStateListener() { | ||
@Override | ||
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { | ||
FirebaseUser user = firebaseAuth.getCurrentUser(); | ||
if (user != null) { | ||
// User is signed in | ||
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid()); | ||
} else { | ||
// User is signed out | ||
Log.d(TAG, "onAuthStateChanged:signed_out"); | ||
} | ||
// [START_EXCLUDE] | ||
updateUI(user); | ||
// [END_EXCLUDE] | ||
} | ||
}; | ||
// [END auth_state_listener] | ||
|
||
//4 deine this methods | ||
// [START on_start_add_listener] | ||
@Override | ||
public void onStart() { | ||
super.onStart(); | ||
mAuth.addAuthStateListener(mAuthListener); | ||
} | ||
// [END on_start_add_listener] | ||
|
||
// [START on_stop_remove_listener] | ||
@Override | ||
public void onStop() { | ||
super.onStop(); | ||
if (mAuthListener != null) { | ||
mAuth.removeAuthStateListener(mAuthListener); | ||
} | ||
} | ||
// [END on_stop_remove_listener] | ||
|
||
// [START onactivityresult] | ||
@Override | ||
public void onActivityResult(int requestCode, int resultCode, Intent data) { | ||
super.onActivityResult(requestCode, resultCode, data); | ||
|
||
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...); | ||
if (requestCode == RC_SIGN_IN) { | ||
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); | ||
if (result.isSuccess()) { | ||
// Google Sign In was successful, authenticate with Firebase | ||
GoogleSignInAccount account = result.getSignInAccount(); | ||
firebaseAuthWithGoogle(account); | ||
} else { | ||
// Google Sign In failed, update UI appropriately | ||
// [START_EXCLUDE] | ||
updateUI(null); | ||
// [END_EXCLUDE] | ||
} | ||
} | ||
} | ||
// [END onactivityresult] | ||
|
||
// [START auth_with_google] | ||
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) { | ||
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId()); | ||
// [START_EXCLUDE silent] | ||
showProgressDialog(); | ||
// [END_EXCLUDE] | ||
|
||
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null); | ||
mAuth.signInWithCredential(credential) | ||
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { | ||
@Override | ||
public void onComplete(@NonNull Task<AuthResult> task) { | ||
Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful()); | ||
|
||
// If sign in fails, display a message to the user. If sign in succeeds | ||
// the auth state listener will be notified and logic to handle the | ||
// signed in user can be handled in the listener. | ||
if (!task.isSuccessful()) { | ||
Log.w(TAG, "signInWithCredential", task.getException()); | ||
Toast.makeText(GoogleSignInActivity.this, "Authentication failed.", | ||
Toast.LENGTH_SHORT).show(); | ||
} | ||
// [START_EXCLUDE] | ||
hideProgressDialog(); | ||
// [END_EXCLUDE] | ||
} | ||
}); | ||
} | ||
// [END auth_with_google] | ||
|
||
// [START signin] | ||
private void signIn() { | ||
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); | ||
startActivityForResult(signInIntent, RC_SIGN_IN); | ||
} | ||
// [END signin] | ||
|
||
private void signOut() { | ||
// Firebase sign out | ||
mAuth.signOut(); | ||
|
||
// Google sign out | ||
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback( | ||
new ResultCallback<Status>() { | ||
@Override | ||
public void onResult(@NonNull Status status) { | ||
updateUI(null); | ||
} | ||
}); | ||
} | ||
|
||
|
||
|
||
private void updateUI(FirebaseUser user) { | ||
hideProgressDialog(); | ||
String Email=user.getEmail(); | ||
String Uid= user.getUid(); | ||
} | ||
|
||
@Override | ||
public void onConnected(Bundle connectionHint) { | ||
|
||
Log.d(TAG, "onConnection is connected:" ); | ||
} | ||
@Override | ||
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { | ||
// An unresolvable error has occurred and Google APIs (including Sign-In) will not | ||
// be available. | ||
Log.d(TAG, "onConnectionFailed:" + connectionResult); | ||
Toast.makeText(this, "Google Play Services error.", Toast.LENGTH_SHORT).show(); | ||
} | ||
|
||
|
||
@VisibleForTesting | ||
public ProgressDialog mProgressDialog; | ||
|
||
public void showProgressDialog() { | ||
if (mProgressDialog == null) { | ||
mProgressDialog = new ProgressDialog(this); | ||
mProgressDialog.setMessage(getString(R.string.loading)); | ||
mProgressDialog.setIndeterminate(true); | ||
} | ||
|
||
mProgressDialog.show(); | ||
} | ||
|
||
public void hideProgressDialog() { | ||
if (mProgressDialog != null && mProgressDialog.isShowing()) { | ||
mProgressDialog.dismiss(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onStop() { | ||
super.onStop(); | ||
hideProgressDialog(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// Fire base sign in | ||
|
||
//add to grade | ||
compile 'com.google.firebase:firebase-auth:9.6.1' | ||
|
||
//code | ||
|
||
|
||
/** | ||
* Activity to demonstrate anonymous login and account linking (with an email/password account). | ||
*/ | ||
//1- define | ||
private static final String TAG = "AnonymousAuth"; | ||
|
||
// [START declare_auth] | ||
private FirebaseAuth mAuth; | ||
// [END declare_auth] | ||
|
||
// [START declare_auth_listener] | ||
private FirebaseAuth.AuthStateListener mAuthListener; | ||
// [END declare_auth_listener] | ||
|
||
//2- initiailze OnCreate() | ||
// [START initialize_auth] | ||
mAuth = FirebaseAuth.getInstance(); | ||
// [END initialize_auth] | ||
|
||
// [START auth_state_listener] | ||
mAuthListener = new FirebaseAuth.AuthStateListener() { | ||
@Override | ||
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { | ||
FirebaseUser user = firebaseAuth.getCurrentUser(); | ||
if (user != null) { | ||
// User is signed in | ||
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid()); | ||
} else { | ||
// User is signed out | ||
Log.d(TAG, "onAuthStateChanged:signed_out"); | ||
} | ||
// [START_EXCLUDE] | ||
updateUI(user); | ||
// [END_EXCLUDE] | ||
} | ||
}; | ||
// [END auth_state_listener] | ||
private void updateUI(FirebaseUser user) { | ||
hideProgressDialog(); | ||
String Email=user.getEmail(); | ||
String Uid= user.getUid(); | ||
} | ||
|
||
// [START on_start_add_listener] | ||
@Override | ||
public void onStart() { | ||
super.onStart(); | ||
mAuth.addAuthStateListener(mAuthListener); | ||
} | ||
// [END on_start_add_listener] | ||
|
||
// [START on_stop_remove_listener] | ||
@Override | ||
public void onStop() { | ||
super.onStop(); | ||
if (mAuthListener != null) { | ||
mAuth.removeAuthStateListener(mAuthListener); | ||
} | ||
} | ||
// [END on_stop_remove_listener] | ||
|
||
private void signInAnonymously() { | ||
showProgressDialog(); | ||
// [START signin_anonymously] | ||
mAuth.signInAnonymously() | ||
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { | ||
@Override | ||
public void onComplete(@NonNull Task<AuthResult> task) { | ||
Log.d(TAG, "signInAnonymously:onComplete:" + task.isSuccessful()); | ||
|
||
// If sign in fails, display a message to the user. If sign in succeeds | ||
// the auth state listener will be notified and logic to handle the | ||
// signed in user can be handled in the listener. | ||
if (!task.isSuccessful()) { | ||
Log.w(TAG, "signInAnonymously", task.getException()); | ||
Toast.makeText(MainActivity.this, "Authentication failed.", | ||
Toast.LENGTH_SHORT).show(); | ||
} | ||
|
||
// [START_EXCLUDE] | ||
hideProgressDialog(); | ||
// [END_EXCLUDE] | ||
} | ||
}); | ||
// [END signin_anonymously] | ||
} | ||
|
||
private void signOut() { | ||
mAuth.signOut(); | ||
|
||
} | ||
|
||
private void linkAccount() { | ||
|
||
|
||
// Get email and password from form | ||
String email = "User_Email"; | ||
String password ="User_Password"; | ||
|
||
// Create EmailAuthCredential with email and password | ||
AuthCredential credential = EmailAuthProvider.getCredential(email, password); | ||
|
||
// Link the anonymous user to the email credential | ||
showProgressDialog(); | ||
// [START link_credential] | ||
mAuth.getCurrentUser().linkWithCredential(credential) | ||
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { | ||
@Override | ||
public void onComplete(@NonNull Task<AuthResult> task) { | ||
Log.d(TAG, "linkWithCredential:onComplete:" + task.isSuccessful()); | ||
|
||
// If sign in fails, display a message to the user. If sign in succeeds | ||
// the auth state listener will be notified and logic to handle the | ||
// signed in user can be handled in the listener. | ||
if (!task.isSuccessful()) { | ||
Toast.makeText(MainActivity.this, "Authentication failed.", | ||
Toast.LENGTH_SHORT).show(); | ||
} | ||
|
||
// [START_EXCLUDE] | ||
hideProgressDialog(); | ||
// [END_EXCLUDE] | ||
} | ||
}); | ||
// [END link_credential] | ||
} | ||
|
||
|
||
@VisibleForTesting | ||
public ProgressDialog mProgressDialog; | ||
|
||
public void showProgressDialog() { | ||
if (mProgressDialog == null) { | ||
mProgressDialog = new ProgressDialog(this); | ||
mProgressDialog.setMessage(getString(R.string.loading)); | ||
mProgressDialog.setIndeterminate(true); | ||
} | ||
|
||
mProgressDialog.show(); | ||
} | ||
|
||
public void hideProgressDialog() { | ||
if (mProgressDialog != null && mProgressDialog.isShowing()) { | ||
mProgressDialog.dismiss(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onStop() { | ||
super.onStop(); | ||
hideProgressDialog(); | ||
} |