Guest User

Untitled

a guest
Apr 11th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.50 KB | None | 0 0
  1. final FirebaseUser annonymsUser;
  2.  
  3.  
  4. mAuth.signInAnnonmously();
  5.  
  6. annonymsUser=mAuth.getCurrentUser(); //storing annonyms instance
  7.  
  8. mAuth.signInWithEmailAndPassword(email,password)...{}
  9.  
  10. private static final String TAG = "sms";
  11. private BroadcastReceiver smsBroadcastReceiver;
  12. IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
  13. public static final String SMS_BUNDLE = "pdus";
  14.  
  15. private static final String KEY_VERIFY_IN_PROGRESS = "key_verify_in_progress";
  16. // [START declare_auth]
  17. private FirebaseAuth mAuth,sAuth;
  18. // [END declare_auth]
  19. private FirebaseUser annonymsUser;
  20. private boolean mVerificationInProgress = false;
  21. private String mVerificationId;
  22. private PhoneAuthProvider.ForceResendingToken mResendToken;
  23. private PhoneAuthProvider.OnVerificationStateChangedCallbacks mVarificatioCallbacks;
  24.  
  25. String phoneNo,password;
  26. FirebaseAuth annonymsAuth;
  27. FirebaseUser annonymsUserUsed;
  28. FirebaseAuth.AuthStateListener AuthStateListener;
  29.  
  30. public OtpActivity() {
  31. }
  32.  
  33. @Override
  34. protected void onCreate(Bundle savedInstanceState) {
  35. super.onCreate(savedInstanceState);
  36. setContentView(R.layout.activity_otp);
  37.  
  38. mAuth=FirebaseAuth.getInstance();
  39. annonymsUser =mAuth.getCurrentUser();
  40.  
  41. if(annonymsUser.isAnonymous()){ Toast.makeText(OtpActivity.this, "is Annonyms"+annonymsUser.getUid().toString(),
  42. Toast.LENGTH_SHORT).show(); }
  43. else {
  44. Toast.makeText(OtpActivity.this, "Not Annonyms",
  45. Toast.LENGTH_SHORT).show();}
  46.  
  47.  
  48. AuthStateListener=new FirebaseAuth.AuthStateListener() {
  49. @Override
  50. public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
  51.  
  52. if(annonymsUser.isAnonymous()){ Log.e("sarthakshubham","isAnnonyms"); }
  53. else {
  54. Log.e("state","NotAnnonyms");}
  55.  
  56.  
  57. }};
  58.  
  59.  
  60. smsBroadcastReceiver = new BroadcastReceiver() {
  61. @Override
  62. public void onReceive(Context context, Intent intent) {
  63. Log.e("smsBroadcastReceiver", "onReceive");
  64. Bundle pudsBundle = intent.getExtras();
  65. Object[] pdus = (Object[]) pudsBundle.get(SMS_BUNDLE);
  66. SmsMessage messages = SmsMessage.createFromPdu((byte[]) pdus[0]);
  67. Log.i(TAG, messages.getMessageBody());
  68.  
  69. String firebaseVerificationCode = messages.getMessageBody().trim().split(" ")[0];//only a number code
  70. Toast.makeText(getApplicationContext(), firebaseVerificationCode,Toast.LENGTH_SHORT).show();
  71.  
  72. }};
  73.  
  74. final TextInputLayout mPhone=findViewById(R.id.signup_mobileno);
  75. Button mContinue=findViewById(R.id.signup_continue);
  76.  
  77. if (savedInstanceState != null) {
  78. Log.d("vow","savedInstaceState");
  79. onRestoreInstanceState(savedInstanceState);
  80. }
  81.  
  82. mContinue.setOnClickListener(new View.OnClickListener() {
  83. @Override
  84. public void onClick(View v) {
  85.  
  86. TextInputLayout mPassword=findViewById(R.id.signup_password);
  87. password=mPassword.getEditText().getText().toString();
  88. phoneNo=mPhone.getEditText().getText().toString();
  89. startPhoneNumberVarification("+91"+phoneNo);
  90.  
  91. final Dialog addUnitDialog;
  92. addUnitDialog = new Dialog(OtpActivity.this);
  93. addUnitDialog.setContentView(R.layout.dialgo_otp);
  94. addUnitDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  95. final TextInputLayout mOtpCode=addUnitDialog.findViewById(R.id.signup_enterotp);
  96.  
  97. Button mResend=addUnitDialog.findViewById(R.id.signup_resend);
  98. mResend.setOnClickListener(new View.OnClickListener() {
  99. @Override
  100. public void onClick(View v) {
  101. reStartPhoneNumberVerification("+91"+phoneNo);
  102. }});
  103.  
  104. Button mNext=addUnitDialog.findViewById(R.id.signup_next);
  105. mNext.setOnClickListener(new View.OnClickListener() {
  106. @Override
  107. public void onClick(View v) {
  108.  
  109. signInWIthCredential(credential);
  110.  
  111.  
  112. }});
  113.  
  114. addUnitDialog.show();
  115. }});
  116.  
  117.  
  118.  
  119. mVarificatioCallbacks=new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
  120. @Override
  121. public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
  122. Log.d("abhi", "onVerificationCompleted: ");
  123. mVerificationInProgress=false;
  124.  
  125. }
  126.  
  127. @Override
  128. public void onVerificationFailed(FirebaseException e) {
  129.  
  130. Log.d("abhi","Verification failed") ;
  131. mVerificationInProgress=false;
  132. }
  133.  
  134. @Override
  135. public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
  136.  
  137. Log.d("abhi", "onCodeSent: ");
  138. Toast.makeText(getApplicationContext(),"code has been sent!!!",Toast.LENGTH_SHORT).show();
  139. mVerificationId=s;
  140. Log.d("varificationid",s);
  141. mVerificationInProgress=true;
  142.  
  143. mResendToken=forceResendingToken;
  144. }
  145.  
  146. @Override
  147. public void onCodeAutoRetrievalTimeOut(String s) {
  148. super.onCodeAutoRetrievalTimeOut(s);
  149.  
  150. Log.d("timeout",s);
  151. }
  152. };
  153.  
  154.  
  155. }
  156.  
  157.  
  158. @Override
  159. protected void onStart() {
  160. super.onStart();
  161.  
  162.  
  163. Log.d("shubham", ""+mVerificationInProgress);
  164. if(mVerificationInProgress==true){
  165. Log.d("shubham", "onStart:");
  166. startPhoneNumberVarification(phoneNo);
  167. }
  168.  
  169. }
  170.  
  171.  
  172. public void startPhoneNumberVarification(String phoneNo){
  173. Log.d("shubham", "onStartPhoneNumberAuth:");
  174. PhoneAuthProvider.getInstance().verifyPhoneNumber(
  175. phoneNo,
  176. 60,
  177. TimeUnit.SECONDS,
  178. this,
  179. mVarificatioCallbacks
  180. );
  181.  
  182. mVerificationInProgress=true;
  183.  
  184.  
  185. }
  186.  
  187. public void reStartPhoneNumberVerification(String phoneNo){
  188. Log.d("vow","onRestartVerificationNumber");
  189. PhoneAuthProvider.getInstance().verifyPhoneNumber(
  190. phoneNo,
  191. 60,
  192. TimeUnit.SECONDS,
  193. this,
  194. mVarificatioCallbacks,
  195. mResendToken
  196. );
  197.  
  198. mVerificationInProgress=true;
  199.  
  200. }
  201.  
  202.  
  203. private void link(AuthCredential credential, String id) {
  204.  
  205. //String id = user.getUid();
  206. String id2 = annonymsUser.getUid(); //here i get id of currently sign in user
  207. if(annonymsUser.isAnonymous()){ Toast.makeText(OtpActivity.this, "is Annonyms",
  208. Toast.LENGTH_SHORT).show(); }
  209. else {
  210. Toast.makeText(OtpActivity.this, "Not Annonyms",
  211. Toast.LENGTH_SHORT).show();}
  212.  
  213. annonymsUser.linkWithCredential(credential)
  214. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  215. @Override
  216. public void onComplete(@NonNull Task<AuthResult> task) {
  217. if (task.isSuccessful()) {
  218. Log.d(TAG, "linkWithCredential:success");
  219. FirebaseUser muser = task.getResult().getUser();
  220. Toast.makeText(OtpActivity.this, "link: success.",
  221. Toast.LENGTH_SHORT).show();
  222.  
  223. } else {
  224. Log.w(TAG, "linkWithCredential:failure", task.getException());
  225. Toast.makeText(OtpActivity.this, "link: failed.",
  226. Toast.LENGTH_SHORT).show();
  227.  
  228. }
  229.  
  230. // ...
  231. }
  232. });
  233. }
  234.  
  235.  
  236. private void signInWIthCredential(PhoneAuthCredential credential) {
  237.  
  238. mAuth.signInWithCredential(credential)
  239. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  240. @Override
  241. public void onComplete(@NonNull Task<AuthResult> task) {
  242. if (task.isSuccessful()) {
  243. // Sign in success, update UI with the signed-in user's information
  244. Log.d(TAG, "signInWithCredential:success");
  245. final String id = FirebaseAuth.getInstance().getCurrentUser().getUid();
  246. mAuth.getCurrentUser().delete().addOnSuccessListener(new OnSuccessListener<Void>() {
  247. @Override
  248. public void onSuccess(Void aVoid) {
  249.  
  250. Toast.makeText(getApplicationContext(),"account varified",Toast.LENGTH_SHORT).show();
  251.  
  252. AuthCredential emailAuthCredential = EmailAuthProvider.getCredential(phoneNo+"@gmail.com", password);
  253. link(emailAuthCredential,id);
  254.  
  255. }});
  256.  
  257.  
  258. FirebaseUser user = task.getResult().getUser();
  259.  
  260. // ...
  261. } else {
  262. // Sign in failed, display a message and update the UI
  263. Log.w(TAG, "signInWithCredential:failure", task.getException());
  264. if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
  265. // The verification code entered was invalid
  266. Toast.makeText(getApplicationContext(),"account not varified",Toast.LENGTH_SHORT).show();
  267.  
  268. }
  269. }
  270. }
  271. });
  272. }
  273.  
  274.  
  275.  
  276. @Override
  277. public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
  278. super.onSaveInstanceState(outState, outPersistentState);
  279.  
  280. outState.putBoolean("progress",mVerificationInProgress);
  281. }
  282.  
  283. @Override
  284. protected void onRestoreInstanceState(Bundle savedInstanceState) {
  285. super.onRestoreInstanceState(savedInstanceState);
  286.  
  287. mVerificationInProgress=savedInstanceState.getBoolean("progress");
  288. }
Add Comment
Please, Sign In to add comment