Skip to content

Commit ca24659

Browse files
2 Dec 2022
1 parent 2f846f1 commit ca24659

30 files changed

Lines changed: 662 additions & 0 deletions

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
147 KB
Loading
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
package com.assistant.android.bolchal;
2+
3+
import androidx.annotation.NonNull;
4+
import androidx.appcompat.app.AppCompatActivity;
5+
6+
import android.content.Intent;
7+
import android.os.Bundle;
8+
import android.os.CountDownTimer;
9+
import android.view.View;
10+
import android.widget.Button;
11+
import android.widget.EditText;
12+
import android.widget.TextView;
13+
import android.widget.Toast;
14+
15+
import com.assistant.android.bolchal.ui.OptEditText;
16+
import com.google.android.gms.tasks.OnCompleteListener;
17+
import com.google.android.gms.tasks.Task;
18+
import com.google.firebase.FirebaseException;
19+
import com.google.firebase.auth.AuthResult;
20+
import com.google.firebase.auth.FirebaseAuth;
21+
import com.google.firebase.auth.FirebaseAuthInvalidCredentialsException;
22+
import com.google.firebase.auth.FirebaseUser;
23+
import com.google.firebase.auth.PhoneAuthCredential;
24+
import com.google.firebase.auth.PhoneAuthOptions;
25+
import com.google.firebase.auth.PhoneAuthProvider;
26+
import com.hbb20.CountryCodePicker;
27+
28+
import java.util.Objects;
29+
import java.util.concurrent.TimeUnit;
30+
31+
public class Otp_verification extends AppCompatActivity {
32+
33+
private static final String TAG = "LogIn_page";
34+
private FirebaseAuth mAuth;
35+
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
36+
private String SmsCode;
37+
private TextView timer;
38+
private TextView resendTxtView;
39+
private TextView verify;
40+
private TextView xxPhone_num;
41+
42+
@Override
43+
protected void onCreate(Bundle savedInstanceState) {
44+
super.onCreate(savedInstanceState);
45+
setContentView(R.layout.layout_otp_verification);
46+
47+
//Hide Top ActionBar
48+
Objects.requireNonNull(getSupportActionBar()).hide();
49+
50+
OptEditText code = findViewById(R.id.codeTextView);
51+
verify = findViewById(R.id.verify);
52+
timer = findViewById(R.id.timer);
53+
resendTxtView = findViewById(R.id.resend);
54+
xxPhone_num = findViewById(R.id.xxx_Ph);
55+
56+
57+
mAuth = FirebaseAuth.getInstance();
58+
59+
/**
60+
* get Phone Num as extra from Intent of Phone Number Sent
61+
*/
62+
String phone_num = getIntent().getStringExtra("phoneNum");
63+
64+
//Set coded phoneNUm to the screen
65+
String coded = "+" + phone_num.charAt(1) + phone_num.charAt(2) + "XXXXXXX" + phone_num.charAt(phone_num.length()-3) +
66+
phone_num.charAt(phone_num.length()-2) + phone_num.charAt(phone_num.length()-1);
67+
xxPhone_num.setText(phone_num);
68+
69+
70+
/**
71+
* Verification Check up
72+
*/
73+
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
74+
/**
75+
* ON different device Manually input code
76+
*
77+
* @param s
78+
* @param forceResendingToken
79+
*/
80+
@Override
81+
public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
82+
SmsCode = s;
83+
}
84+
85+
@Override //Automatic code verify in case of same number in the device
86+
public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
87+
signInWithPhoneAuthCredential(phoneAuthCredential);
88+
}
89+
90+
@Override
91+
public void onVerificationFailed(@NonNull FirebaseException e) {
92+
Toast.makeText(Otp_verification.this, e + " exception", Toast.LENGTH_SHORT).show();
93+
}
94+
};
95+
96+
97+
/**
98+
* This Method Sends Sms for verification
99+
*/
100+
sendMsg(phone_num);
101+
102+
verify.setOnClickListener(new View.OnClickListener() {
103+
@Override
104+
public void onClick(View view) {
105+
if (code.getText().toString().length() == 6) {
106+
PhoneAuthCredential phoneAuthCredential = PhoneAuthProvider.getCredential(SmsCode, code.getText().toString());
107+
signInWithPhoneAuthCredential(phoneAuthCredential);
108+
}
109+
}
110+
});
111+
112+
//SetTimer
113+
startTimer();
114+
115+
resendTxtView.setOnClickListener(view -> {
116+
resendTxtView.setVisibility(View.GONE);
117+
timer.setVisibility(View.VISIBLE);
118+
sendMsg(phone_num);
119+
startTimer();
120+
});
121+
122+
}
123+
124+
//Send messages
125+
private void sendMsg(String phone_num){
126+
PhoneAuthOptions options =
127+
PhoneAuthOptions.newBuilder(mAuth)
128+
.setPhoneNumber(phone_num) // Phone number to verify
129+
.setTimeout(30L, TimeUnit.SECONDS) // Timeout and unit
130+
.setActivity(Otp_verification.this) // Activity (for callback binding)
131+
.setCallbacks(mCallbacks) // OnVerificationStateChangedCallbacks
132+
.build();
133+
PhoneAuthProvider.verifyPhoneNumber(options);
134+
}
135+
136+
//Make CountDown Timer
137+
private void startTimer() {
138+
new CountDownTimer(30000,1000){
139+
140+
@Override
141+
public void onTick(long l) {
142+
long sec = l/1000;
143+
timer.setText("Resend OTP in: 00:"+sec);
144+
}
145+
146+
@Override
147+
public void onFinish() {
148+
timer.setVisibility(View.GONE);
149+
resendTxtView.setVisibility(View.VISIBLE);
150+
}
151+
}.start();
152+
}
153+
154+
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
155+
mAuth.signInWithCredential(credential)
156+
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
157+
@Override
158+
public void onComplete(@NonNull Task<AuthResult> task) {
159+
if (task.isSuccessful()) {
160+
// Sign in success, update UI with the signed-in user's information
161+
Intent intent = new Intent(Otp_verification.this,details.class);
162+
163+
FirebaseUser user = task.getResult().getUser();
164+
165+
intent.putExtra("userName",user);
166+
startActivity(intent);
167+
finish();
168+
// Update UI
169+
} else {
170+
// Sign in failed, display a message and update the UI
171+
Toast.makeText(Otp_verification.this, "signInWithCredential:failure" + task.getException(),Toast.LENGTH_LONG).show();
172+
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
173+
// The verification code entered was invalid
174+
}
175+
}
176+
}
177+
});
178+
}
179+
180+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.assistant.android.bolchal;
2+
3+
import androidx.activity.result.contract.ActivityResultContracts;
4+
import androidx.annotation.NonNull;
5+
import androidx.appcompat.app.AppCompatActivity;
6+
7+
import android.app.Activity;
8+
import android.content.Intent;
9+
import android.net.Uri;
10+
import android.os.Bundle;
11+
import android.view.View;
12+
import android.widget.Button;
13+
import android.widget.EditText;
14+
import android.widget.ImageView;
15+
import android.widget.Toast;
16+
17+
import com.bumptech.glide.Glide;
18+
import com.google.android.gms.tasks.OnCompleteListener;
19+
import com.google.android.gms.tasks.Task;
20+
import com.google.firebase.auth.FirebaseAuth;
21+
import com.google.firebase.auth.FirebaseUser;
22+
import com.google.firebase.auth.UserProfileChangeRequest;
23+
import com.mikhaellopez.circularimageview.CircularImageView;
24+
25+
import java.util.Objects;
26+
27+
public class details extends Activity {
28+
29+
private EditText userName;
30+
private CircularImageView profile_img;
31+
private FirebaseAuth firebaseAuth;
32+
private String user_name;
33+
34+
private static final int RC_PROFILE_PICKER = 20;
35+
private Uri profile_pic_url;
36+
37+
@Override
38+
protected void onCreate(Bundle savedInstanceState) {
39+
super.onCreate(savedInstanceState);
40+
setContentView(R.layout.layout_details);
41+
42+
userName = findViewById(R.id.userName_inputArea);
43+
Button uploadImg = findViewById(R.id.upload_button);
44+
Button updateProfile = findViewById(R.id.Updatebutton);
45+
profile_img = findViewById(R.id.profile_image);
46+
47+
firebaseAuth = FirebaseAuth.getInstance();
48+
FirebaseUser user = firebaseAuth.getCurrentUser();
49+
50+
user_name = "anonymas";
51+
52+
//Choose a profile picture from the storage
53+
uploadImg.setOnClickListener(view -> {
54+
Intent profile_picker_intent = new Intent();
55+
profile_picker_intent.setType("image/jpeg");
56+
profile_picker_intent.setAction(Intent.ACTION_GET_CONTENT);
57+
profile_picker_intent.putExtra(Intent.EXTRA_LOCAL_ONLY,true);
58+
startActivityForResult(profile_picker_intent,RC_PROFILE_PICKER);
59+
});
60+
61+
//Finish And Save Final Profile
62+
updateProfile.setOnClickListener(new View.OnClickListener() {
63+
@Override
64+
public void onClick(View view) {
65+
if (!userName.getText().toString().isEmpty()) {
66+
user_name = userName.getText().toString();
67+
}
68+
69+
UserProfileChangeRequest profilUpdate = new UserProfileChangeRequest.Builder()
70+
.setDisplayName(user_name)
71+
.setPhotoUri(Uri.parse(String.valueOf(profile_pic_url)))
72+
.build();
73+
74+
if (user != null) {
75+
user.updateProfile(profilUpdate)
76+
.addOnCompleteListener(new OnCompleteListener<Void>() {
77+
@Override
78+
public void onComplete(@NonNull Task<Void> task) {
79+
if (task.isSuccessful()) {
80+
Toast.makeText(getApplicationContext(), "Profile Updated", Toast.LENGTH_LONG).show();
81+
Intent intent = new Intent(details.this, MainActivity.class);
82+
startActivity(intent);
83+
finish();
84+
}
85+
}
86+
});
87+
}
88+
}
89+
});
90+
}
91+
92+
@Override
93+
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
94+
if(requestCode==RC_PROFILE_PICKER && resultCode==RESULT_OK){
95+
Toast.makeText(getApplicationContext(),"Profile pic Selected",Toast.LENGTH_LONG).show();
96+
profile_pic_url = Objects.requireNonNull(data).getData();
97+
98+
//update ImageView Profile_holder
99+
Glide.with(profile_img.getContext())
100+
.load(profile_pic_url)
101+
.centerCrop().placeholder(R.drawable.profile_placeholder).into(profile_img);
102+
}
103+
super.onActivityResult(requestCode, resultCode, data);
104+
}
105+
}

0 commit comments

Comments
 (0)