Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.49 KB | None | 0 0
  1. public class RegisterActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener,
  2. View.OnClickListener {
  3.  
  4. private FirebaseAuth mFirebaseAuth;
  5. private ProgressBar progressBar;
  6. private ProgressDialog mProgressDialog;
  7. private GoogleApiClient mGoogleApiClient;
  8. private static final String TAG = "RegisterActivity";
  9.  
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_register);
  14.  
  15. // Get FireBase mFirebaseAuth instance
  16. mFirebaseAuth = FirebaseAuth.getInstance();
  17.  
  18. if (mFirebaseAuth.getCurrentUser() != null) {
  19. Log.e("45hh", "User logged in already...");
  20. startActivity(new Intent(RegisterActivity.this, MainActivity.class));
  21. }
  22.  
  23.  
  24. GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
  25. .requestScopes(new Scope(Scopes.PLUS_LOGIN))
  26. .requestScopes(new Scope(Scopes.PLUS_ME))
  27. .requestEmail()
  28. .build();
  29. mGoogleApiClient = new GoogleApiClient.Builder(this)
  30. .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
  31. .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
  32. .build();
  33.  
  34. final EditText txtEmail = (EditText) findViewById(R.id.txtEmail);
  35. final EditText txtPassword = (EditText) findViewById(R.id.txtPassword);
  36. Button btnRegister = (Button) findViewById(R.id.btnRegister);
  37. Button btnGSignin = (Button) findViewById(R.id.btnGSignin);
  38. TextView txtAlreadyHaveAccount = (TextView) findViewById(R.id.txtAlreadyHaveAccount);
  39. progressBar = (ProgressBar) findViewById(R.id.progressBar);
  40.  
  41. btnRegister.setOnClickListener(new View.OnClickListener() {
  42. @Override
  43. public void onClick(View v) {
  44. String email = txtEmail.getText().toString().trim();
  45. String password = txtPassword.getText().toString().trim();
  46.  
  47. if (TextUtils.isEmpty(email)) {
  48. Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
  49. return;
  50. } else if (TextUtils.isEmpty(password)) {
  51. Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
  52. return;
  53. }
  54.  
  55. if (password.length() < 6) {
  56. Toast.makeText(getApplicationContext(), "Password too short, enter minimum 6 characters!", Toast.LENGTH_SHORT).show();
  57. return;
  58. }
  59.  
  60. progressBar.setVisibility(View.VISIBLE);
  61. // Create user
  62. mFirebaseAuth.createUserWithEmailAndPassword(email, password)
  63. .addOnCompleteListener(RegisterActivity.this, new OnCompleteListener<AuthResult>() {
  64. @Override
  65. public void onComplete(@NonNull Task<AuthResult> task) {
  66. if (task.isSuccessful()) {
  67. Toast.makeText(RegisterActivity.this, "Successfully registered!", Toast.LENGTH_SHORT).show();
  68. progressBar.setVisibility(View.GONE);
  69. startActivity(new Intent(RegisterActivity.this, MainActivity.class));
  70. finish();
  71. } else {
  72. Toast.makeText(RegisterActivity.this, "Authentication failed. " + task.getResult(),
  73. Toast.LENGTH_SHORT).show();
  74. }
  75. }
  76. });
  77. }
  78. });
  79.  
  80. btnGSignin.setOnClickListener(new View.OnClickListener() {
  81. @Override
  82. public void onClick(View v) {
  83. signIn();
  84. }
  85. });
  86.  
  87. txtAlreadyHaveAccount.setOnClickListener(new View.OnClickListener() {
  88. @Override
  89. public void onClick(View v) {
  90. startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
  91. }
  92. });
  93. }
  94.  
  95. private void handleFirebaseAuthResult(AuthResult authResult) {
  96. if (authResult != null) {
  97. // Welcome the user
  98. FirebaseUser user = authResult.getUser();
  99. Toast.makeText(this, "Welcome " + user.getEmail(), Toast.LENGTH_SHORT).show();
  100.  
  101. // Go back to the main activity
  102. startActivity(new Intent(this, MainActivity.class));
  103. }
  104. }
  105.  
  106. private void signIn() {
  107. Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
  108. startActivityForResult(signInIntent, 9001);
  109. showProgressDialog();
  110. }
  111.  
  112. @Override
  113. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  114. super.onActivityResult(requestCode, resultCode, data);
  115.  
  116. // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
  117. if (requestCode == 9001) {
  118. GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
  119. if (result.isSuccess()) {
  120. // Google Sign In was successful, authenticate with Firebase
  121. GoogleSignInAccount account = result.getSignInAccount();
  122. firebaseAuthWithGoogle(account);
  123. } else {
  124. if(mProgressDialog!=null){
  125. hideProgressDialog();
  126. }
  127. // Google Sign In failed
  128. Log.e(TAG, "Google Sign In failed. Status = " + result.getStatus());
  129. }
  130. }
  131. }
  132.  
  133. private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
  134. Log.d(TAG, "firebaseAuthWithGooogle:" + acct.getId());
  135. AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
  136. mFirebaseAuth.signInWithCredential(credential)
  137. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  138. @Override
  139. public void onComplete(@NonNull Task<AuthResult> task) {
  140. Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
  141.  
  142. // If sign in fails, display a message to the user. If sign in succeeds
  143. // the auth state listener will be notified and logic to handle the
  144. // signed in user can be handled in the listener.
  145. if (!task.isSuccessful()) {
  146. Log.w(TAG, "signInWithCredential", task.getException());
  147. Toast.makeText(RegisterActivity.this, "Authentication failed.",
  148. Toast.LENGTH_SHORT).show();
  149. } else {
  150. Toast.makeText(RegisterActivity.this, "Authentication Success. Please Wait",
  151. Toast.LENGTH_SHORT).show();
  152. startActivity(new Intent(RegisterActivity.this, MainActivity.class));
  153. finish();
  154. }
  155. hideProgressDialog();
  156. }
  157. });
  158. }
  159.  
  160. public void showProgressDialog() {
  161. if (mProgressDialog == null) {
  162. mProgressDialog = new ProgressDialog(this);
  163. mProgressDialog.setMessage("Loading...");
  164. mProgressDialog.setIndeterminate(true);
  165. }
  166.  
  167. mProgressDialog.show();
  168. }
  169.  
  170. public void hideProgressDialog() {
  171. if (mProgressDialog != null && mProgressDialog.isShowing()) {
  172. mProgressDialog.dismiss();
  173. }
  174. }
  175.  
  176. @Override
  177. public void onClick(View v) {
  178.  
  179. }
  180.  
  181. @Override
  182. public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
  183. // An unresolvable error has occurred and Google APIs (including Sign-In) will not
  184. // be available.
  185. Log.d(TAG, "onConnectionFailed:" + connectionResult);
  186. Toast.makeText(this, "Google Play Services error.", Toast.LENGTH_SHORT).show();
  187. }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement