mirror of
https://github.com/Nezumi-2711/Foodify.git
synced 2026-09-22 13:38:41 +00:00
Auto retrieve otp message code, fix bug
This commit is contained in:
@@ -48,6 +48,8 @@ dependencies {
|
|||||||
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
|
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
|
||||||
implementation 'com.google.firebase:firebase-auth:21.1.0'
|
implementation 'com.google.firebase:firebase-auth:21.1.0'
|
||||||
implementation 'com.google.firebase:firebase-storage:20.1.0'
|
implementation 'com.google.firebase:firebase-storage:20.1.0'
|
||||||
|
implementation 'com.google.android.gms:play-services-auth:20.4.1'
|
||||||
|
implementation 'com.google.android.gms:play-services-auth-api-phone:18.0.1'
|
||||||
|
|
||||||
testImplementation 'junit:junit:4.13.2'
|
testImplementation 'junit:junit:4.13.2'
|
||||||
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
|
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
|
||||||
|
|||||||
@@ -146,8 +146,6 @@ public class AccountAndProfileActivity extends AppCompatActivity {
|
|||||||
deleteOldImage();
|
deleteOldImage();
|
||||||
|
|
||||||
updateUser(uri.toString());
|
updateUser(uri.toString());
|
||||||
|
|
||||||
Toast.makeText(AccountAndProfileActivity.this, "Upload ảnh thành công!", Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -171,7 +169,7 @@ public class AccountAndProfileActivity extends AppCompatActivity {
|
|||||||
storageReference.delete().addOnCompleteListener(new OnCompleteListener<Void>() {
|
storageReference.delete().addOnCompleteListener(new OnCompleteListener<Void>() {
|
||||||
@Override
|
@Override
|
||||||
public void onComplete(@NonNull Task<Void> task) {
|
public void onComplete(@NonNull Task<Void> task) {
|
||||||
Toast.makeText(AccountAndProfileActivity.this, "Delete old image successfully!", Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
}
|
||||||
}).addOnFailureListener(new OnFailureListener() {
|
}).addOnFailureListener(new OnFailureListener() {
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -319,9 +319,10 @@ public class SignUpActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
private void signUp(User user){
|
private void signUp(User user){
|
||||||
FirebaseAuth auth = FirebaseAuth.getInstance();
|
FirebaseAuth auth = FirebaseAuth.getInstance();
|
||||||
|
auth.setLanguageCode("vi");
|
||||||
PhoneAuthOptions options = PhoneAuthOptions.newBuilder(auth)
|
PhoneAuthOptions options = PhoneAuthOptions.newBuilder(auth)
|
||||||
.setPhoneNumber(Common.PHONE_CODE + edt_phone.getText().toString())
|
.setPhoneNumber(Common.PHONE_CODE + edt_phone.getText().toString())
|
||||||
.setTimeout(60L, TimeUnit.SECONDS)
|
.setTimeout(0L, TimeUnit.SECONDS)
|
||||||
.setActivity(this)
|
.setActivity(this)
|
||||||
.setCallbacks(new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
|
.setCallbacks(new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,25 +1,37 @@
|
|||||||
package com.capstone.foodify.Activity;
|
package com.capstone.foodify.Activity;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import android.Manifest;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import android.app.Activity;
|
||||||
import androidx.constraintlayout.widget.ConstraintLayout;
|
|
||||||
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.IntentFilter;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.CountDownTimer;
|
import android.os.CountDownTimer;
|
||||||
import android.text.Editable;
|
import android.text.Editable;
|
||||||
import android.text.TextWatcher;
|
import android.text.TextWatcher;
|
||||||
import android.util.Log;
|
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import androidx.activity.result.ActivityResult;
|
||||||
|
import androidx.activity.result.ActivityResultCallback;
|
||||||
|
import androidx.activity.result.ActivityResultLauncher;
|
||||||
|
import androidx.activity.result.contract.ActivityResultContracts;
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
import androidx.constraintlayout.widget.ConstraintLayout;
|
||||||
|
import androidx.core.app.ActivityCompat;
|
||||||
|
import androidx.core.content.ContextCompat;
|
||||||
|
|
||||||
import com.capstone.foodify.API.FoodApi;
|
import com.capstone.foodify.API.FoodApi;
|
||||||
import com.capstone.foodify.Common;
|
import com.capstone.foodify.Common;
|
||||||
import com.capstone.foodify.Model.User;
|
import com.capstone.foodify.Model.User;
|
||||||
import com.capstone.foodify.R;
|
import com.capstone.foodify.R;
|
||||||
|
import com.capstone.foodify.SmsBroadcastReceiver;
|
||||||
|
import com.google.android.gms.auth.api.phone.SmsRetriever;
|
||||||
|
import com.google.android.gms.auth.api.phone.SmsRetrieverClient;
|
||||||
import com.google.android.gms.tasks.OnCompleteListener;
|
import com.google.android.gms.tasks.OnCompleteListener;
|
||||||
import com.google.android.gms.tasks.Task;
|
import com.google.android.gms.tasks.Task;
|
||||||
import com.google.android.material.button.MaterialButton;
|
import com.google.android.material.button.MaterialButton;
|
||||||
@@ -29,10 +41,10 @@ import com.google.firebase.auth.FirebaseAuth;
|
|||||||
import com.google.firebase.auth.PhoneAuthCredential;
|
import com.google.firebase.auth.PhoneAuthCredential;
|
||||||
import com.google.firebase.auth.PhoneAuthOptions;
|
import com.google.firebase.auth.PhoneAuthOptions;
|
||||||
import com.google.firebase.auth.PhoneAuthProvider;
|
import com.google.firebase.auth.PhoneAuthProvider;
|
||||||
import com.saadahmedsoft.popupdialog.PopupDialog;
|
|
||||||
import com.saadahmedsoft.popupdialog.Styles;
|
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import retrofit2.Call;
|
import retrofit2.Call;
|
||||||
import retrofit2.Callback;
|
import retrofit2.Callback;
|
||||||
@@ -40,7 +52,8 @@ import retrofit2.Response;
|
|||||||
|
|
||||||
public class VerifyAccountActivity extends AppCompatActivity {
|
public class VerifyAccountActivity extends AppCompatActivity {
|
||||||
private static final String PHONE_CODE = "+84";
|
private static final String PHONE_CODE = "+84";
|
||||||
|
ActivityResultLauncher<Intent> activityResultLauncher;
|
||||||
|
SmsBroadcastReceiver smsBroadcastReceiver;
|
||||||
private EditText inputCode1, inputCode2, inputCode3, inputCode4, inputCode5, inputCode6;
|
private EditText inputCode1, inputCode2, inputCode3, inputCode4, inputCode5, inputCode6;
|
||||||
ImageView close_image;
|
ImageView close_image;
|
||||||
MaterialButton confirm_code;
|
MaterialButton confirm_code;
|
||||||
@@ -57,6 +70,24 @@ public class VerifyAccountActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
initComponent();
|
initComponent();
|
||||||
|
|
||||||
|
//Auto fill OTP Code
|
||||||
|
requestSMSPermission();
|
||||||
|
startSmartUserContent();
|
||||||
|
activityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
|
||||||
|
new ActivityResultCallback<ActivityResult>() {
|
||||||
|
@Override
|
||||||
|
public void onActivityResult(ActivityResult result) {
|
||||||
|
if(result.getResultCode() == Activity.RESULT_OK){
|
||||||
|
Intent data = result.getData();
|
||||||
|
|
||||||
|
if(data != null){
|
||||||
|
String message = data.getStringExtra(SmsRetriever.EXTRA_SMS_MESSAGE);
|
||||||
|
getOtpFromMessage(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
//Get code
|
//Get code
|
||||||
if(getIntent() != null){
|
if(getIntent() != null){
|
||||||
verificationId = getIntent().getStringExtra("verificationId");
|
verificationId = getIntent().getStringExtra("verificationId");
|
||||||
@@ -85,6 +116,7 @@ public class VerifyAccountActivity extends AppCompatActivity {
|
|||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
startActivity(new Intent(VerifyAccountActivity.this, SignUpActivity.class));
|
startActivity(new Intent(VerifyAccountActivity.this, SignUpActivity.class));
|
||||||
|
finish();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -113,6 +145,26 @@ public class VerifyAccountActivity extends AppCompatActivity {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void getOtpFromMessage(String message) {
|
||||||
|
Pattern otpPattern = Pattern.compile("(|^)\\d{6}");
|
||||||
|
Matcher matcher = otpPattern.matcher(message);
|
||||||
|
if (matcher.find()) {
|
||||||
|
Double otp = Double.parseDouble(matcher.group(0));
|
||||||
|
|
||||||
|
inputCode6.setText(String.valueOf(otp % 10));
|
||||||
|
otp = otp / 10;
|
||||||
|
inputCode5.setText(String.valueOf(otp % 10));
|
||||||
|
otp = otp / 10;
|
||||||
|
inputCode4.setText(String.valueOf(otp % 10));
|
||||||
|
otp = otp / 10;
|
||||||
|
inputCode3.setText(String.valueOf(otp % 10));
|
||||||
|
otp = otp / 10;
|
||||||
|
inputCode2.setText(String.valueOf(otp % 10));
|
||||||
|
otp = otp / 10;
|
||||||
|
inputCode1.setText(String.valueOf(otp % 10));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void showResendCode(){
|
private void showResendCode(){
|
||||||
resendCodeTextButton.setVisibility(View.VISIBLE);
|
resendCodeTextButton.setVisibility(View.VISIBLE);
|
||||||
|
|
||||||
@@ -196,6 +248,7 @@ public class VerifyAccountActivity extends AppCompatActivity {
|
|||||||
//Create user success;
|
//Create user success;
|
||||||
Toast.makeText(VerifyAccountActivity.this, "Tạo tài khoản thành công!", Toast.LENGTH_SHORT).show();
|
Toast.makeText(VerifyAccountActivity.this, "Tạo tài khoản thành công!", Toast.LENGTH_SHORT).show();
|
||||||
startActivity(new Intent(VerifyAccountActivity.this, SignInActivity.class));
|
startActivity(new Intent(VerifyAccountActivity.this, SignInActivity.class));
|
||||||
|
finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -323,6 +376,44 @@ public class VerifyAccountActivity extends AppCompatActivity {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void requestSMSPermission()
|
||||||
|
{
|
||||||
|
String permission = Manifest.permission.RECEIVE_SMS;
|
||||||
|
|
||||||
|
int grant = ContextCompat.checkSelfPermission(this, permission);
|
||||||
|
if(grant != PackageManager.PERMISSION_GRANTED){
|
||||||
|
String[] permission_list = new String[1];
|
||||||
|
permission_list[0] = permission;
|
||||||
|
|
||||||
|
ActivityCompat.requestPermissions(this, permission_list, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void startSmartUserContent(){
|
||||||
|
|
||||||
|
SmsRetrieverClient client = SmsRetriever.getClient(this);
|
||||||
|
client.startSmsUserConsent(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void registerBroadcastReceiver(){
|
||||||
|
smsBroadcastReceiver = new SmsBroadcastReceiver();
|
||||||
|
|
||||||
|
smsBroadcastReceiver.smsBroadcastReceiverListener = new SmsBroadcastReceiver.SmsBroadcastReceiverListener() {
|
||||||
|
@Override
|
||||||
|
public void onSuccess(Intent intent) {
|
||||||
|
activityResultLauncher.launch(intent);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFailure() {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
IntentFilter intentFilter = new IntentFilter(SmsRetriever.SMS_RETRIEVED_ACTION);
|
||||||
|
registerReceiver(smsBroadcastReceiver, intentFilter);
|
||||||
|
}
|
||||||
|
|
||||||
private void initComponent() {
|
private void initComponent() {
|
||||||
confirm_code = (MaterialButton) findViewById(R.id.confirm_code);
|
confirm_code = (MaterialButton) findViewById(R.id.confirm_code);
|
||||||
close_image = (ImageView) findViewById(R.id.close_image);
|
close_image = (ImageView) findViewById(R.id.close_image);
|
||||||
@@ -341,4 +432,16 @@ public class VerifyAccountActivity extends AppCompatActivity {
|
|||||||
countDown = findViewById(R.id.textview_countdown);
|
countDown = findViewById(R.id.textview_countdown);
|
||||||
errorText = findViewById(R.id.errorText);
|
errorText = findViewById(R.id.errorText);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onStart() {
|
||||||
|
super.onStart();
|
||||||
|
registerBroadcastReceiver();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onStop() {
|
||||||
|
super.onStop();
|
||||||
|
unregisterReceiver(smsBroadcastReceiver);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -135,5 +135,12 @@ public class ProfileFragment extends Fragment {
|
|||||||
|
|
||||||
//Update user name on textview
|
//Update user name on textview
|
||||||
user_name.setText(Common.CURRENT_USER.getFullName());
|
user_name.setText(Common.CURRENT_USER.getFullName());
|
||||||
|
|
||||||
|
//Set image for user
|
||||||
|
if(Common.CURRENT_USER.getImageUrl().isEmpty()){
|
||||||
|
profile_avatar.setImageResource(R.drawable.profile_avatar);
|
||||||
|
} else {
|
||||||
|
Picasso.get().load(Common.CURRENT_USER.getImageUrl()).into(profile_avatar);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package com.capstone.foodify;
|
||||||
|
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.os.Bundle;
|
||||||
|
|
||||||
|
import com.google.android.gms.auth.api.phone.SmsRetriever;
|
||||||
|
import com.google.android.gms.common.api.CommonStatusCodes;
|
||||||
|
import com.google.android.gms.common.api.Status;
|
||||||
|
|
||||||
|
public class SmsBroadcastReceiver extends BroadcastReceiver {
|
||||||
|
|
||||||
|
public SmsBroadcastReceiverListener smsBroadcastReceiverListener;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onReceive(Context context, Intent intent) {
|
||||||
|
|
||||||
|
if(intent.getAction() == SmsRetriever.SMS_RETRIEVED_ACTION){
|
||||||
|
Bundle extras = intent.getExtras();
|
||||||
|
|
||||||
|
Status smsRetrieverStatus = (Status) extras.get(SmsRetriever.EXTRA_STATUS);
|
||||||
|
|
||||||
|
switch (smsRetrieverStatus.getStatusCode()){
|
||||||
|
|
||||||
|
case CommonStatusCodes.SUCCESS:
|
||||||
|
Intent messageIntent = extras.getParcelable(SmsRetriever.EXTRA_CONSENT_INTENT);
|
||||||
|
smsBroadcastReceiverListener.onSuccess(messageIntent);
|
||||||
|
break;
|
||||||
|
case CommonStatusCodes.TIMEOUT:
|
||||||
|
smsBroadcastReceiverListener.onFailure();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface SmsBroadcastReceiverListener{
|
||||||
|
|
||||||
|
void onSuccess(Intent intent);
|
||||||
|
|
||||||
|
void onFailure();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user