-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSignUpActivity.java
More file actions
175 lines (134 loc) · 6.77 KB
/
Copy pathSignUpActivity.java
File metadata and controls
175 lines (134 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package com.example.updateapp.views.activites;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.example.updateapp.R;
import com.example.updateapp.databinding.ActivitySignUpBinding;
import com.example.updateapp.utils.LocaleHelper;
import com.google.firebase.FirebaseException;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthOptions;
import com.google.firebase.auth.PhoneAuthProvider;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
public class SignUpActivity extends AppCompatActivity {
ActivitySignUpBinding binding;
FirebaseAuth auth;
ProgressDialog progressDialog;
ImageView googleBtn;
ImageView emailBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
);
binding = ActivitySignUpBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
auth = FirebaseAuth.getInstance();
googleBtn = findViewById(R.id.google_btn);
emailBtn = findViewById(R.id.email_btn);
progressDialog = new ProgressDialog(this);
progressDialog.setTitle(getString(R.string.creating_account));
progressDialog.setMessage(getString(R.string.account_creating));
binding.btnSignUp.setOnClickListener(v -> doValidation());
binding.login.setOnClickListener(v -> {
startActivity(new Intent(SignUpActivity.this, LoginActivity.class));
finish();
});
googleBtn.setOnClickListener(v -> {
Toast.makeText(this, getString(R.string.google_login_message), Toast.LENGTH_SHORT).show();
startActivity(new Intent(SignUpActivity.this, LoginActivity.class));
finish();
});
emailBtn.setOnClickListener(v -> doValidation());
}
private void doValidation() {
String name = binding.edtName.getText().toString().trim();
String email = binding.edtEmail.getText().toString().trim().toLowerCase(Locale.ROOT);
String number = binding.edtMobile.getText().toString().trim();
String password = binding.edtPassword.getText().toString();
if (name.isEmpty()) { binding.edtName.setError(getString(R.string.enter_good_name)); return; }
if (email.isEmpty()) { binding.edtEmail.setError(getString(R.string.enter_valid_email)); return; }
if (number.isEmpty()) { binding.edtMobile.setError(getString(R.string.enter_valid_mobile)); return; }
if (password.isEmpty()) { binding.edtPassword.setError(getString(R.string.enter_strong_password)); return; }
checkEmailAlreadyExists(name, email, number, password);
}
private void checkEmailAlreadyExists(String name, String email, String number, String password) {
progressDialog.show();
auth.fetchSignInMethodsForEmail(email)
.addOnCompleteListener(task -> {
if (!task.isSuccessful()) {
progressDialog.dismiss();
String errorMessage = task.getException() != null ?
task.getException().getMessage() :
getString(R.string.generic_error);
Toast.makeText(this, getString(R.string.error_message, errorMessage), Toast.LENGTH_SHORT).show();
return;
}
boolean exists = !task.getResult().getSignInMethods().isEmpty();
if (exists) {
progressDialog.dismiss();
Toast.makeText(this,
getString(R.string.email_already_registered),
Toast.LENGTH_LONG).show();
return;
}
sendOtpForVerification(name, email, number, password);
});
}
private void sendOtpForVerification(String name, String email, String number, String password) {
String phoneNumber = "+91" + number;
PhoneAuthOptions options =
PhoneAuthOptions.newBuilder(auth)
.setPhoneNumber(phoneNumber)
.setTimeout(60L, TimeUnit.SECONDS)
.setActivity(this)
.setCallbacks(new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(@NonNull PhoneAuthCredential credential) {
progressDialog.dismiss();
goToOtpScreen(name, email, number, password, "", true, credential);
}
@Override
public void onVerificationFailed(@NonNull FirebaseException e) {
progressDialog.dismiss();
Toast.makeText(SignUpActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
@Override
public void onCodeSent(@NonNull String verificationId,
@NonNull PhoneAuthProvider.ForceResendingToken token) {
progressDialog.dismiss();
goToOtpScreen(name, email, number, password, verificationId, false, null);
}
})
.build();
PhoneAuthProvider.verifyPhoneNumber(options);
}
private void goToOtpScreen(String name, String email, String number, String password,
String verificationId, boolean autoVerify, PhoneAuthCredential credential) {
Intent intent = new Intent(SignUpActivity.this, OTPActivity.class);
intent.putExtra("name", name);
intent.putExtra("email", email);
intent.putExtra("number", number);
intent.putExtra("password", password);
intent.putExtra("verificationId", verificationId);
intent.putExtra("autoVerify", autoVerify);
if (autoVerify && credential != null) {
OTPActivity.autoCredential = credential;
}
startActivity(intent);
}
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(LocaleHelper.onAttach(newBase));
}
}