PageRenderTime 87ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/jpmc/app/src/main/java/com/example/ranjith/jpmconlinescheduler/WorkFlow/Registration.java

https://bitbucket.org/vnrjpmc/onlinescheduler
Java | 252 lines | 186 code | 61 blank | 5 comment | 19 complexity | 4ffada061ee98ae7a20a6e8c6f8188bc MD5 | raw file
  1. package com.example.ranjith.jpmconlinescheduler.WorkFlow;
  2. import android.app.ProgressDialog;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.SharedPreferences;
  6. import android.os.Bundle;
  7. import android.support.annotation.NonNull;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.text.TextUtils;
  10. import android.view.View;
  11. import android.widget.AdapterView;
  12. import android.widget.ArrayAdapter;
  13. import android.widget.EditText;
  14. import android.widget.Spinner;
  15. import android.widget.Toast;
  16. import com.example.ranjith.jpmconlinescheduler.R;
  17. import com.example.ranjith.jpmconlinescheduler.Util.JpmcToast;
  18. import com.example.ranjith.jpmconlinescheduler.Util.Utils;
  19. import com.google.android.gms.tasks.OnCompleteListener;
  20. import com.google.android.gms.tasks.Task;
  21. import com.google.firebase.auth.AuthResult;
  22. import com.google.firebase.auth.FirebaseAuth;
  23. import com.google.firebase.auth.FirebaseUser;
  24. import com.google.firebase.database.DatabaseReference;
  25. import com.google.firebase.database.FirebaseDatabase;
  26. import java.util.ArrayList;
  27. import java.util.regex.Matcher;
  28. import java.util.regex.Pattern;
  29. public class Registration extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
  30. ArrayList<String> arrayList;
  31. private String var = "SELECT BRANCH";
  32. EditText mobile, name, password, c_password, email;
  33. ProgressDialog progressDialog;
  34. public static final Pattern VALID_EMAIL_ADDRESS_REGEX =
  35. Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
  36. @Override
  37. protected void onCreate(Bundle savedInstanceState) {
  38. super.onCreate(savedInstanceState);
  39. setContentView(R.layout.activity_register);
  40. SharedPreferences shared = getSharedPreferences("loginData", Context.MODE_PRIVATE);
  41. Toast.makeText(getApplicationContext(), shared.getString("user",""),Toast.LENGTH_LONG).show();
  42. arrayList = new ArrayList<String>();
  43. addItems();
  44. progressDialog = new ProgressDialog(this);
  45. Spinner spinner = (Spinner) findViewById(R.id.spinner_search);
  46. final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, arrayList);
  47. spinner.setAdapter(arrayAdapter);
  48. spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  49. @Override
  50. public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
  51. var = arrayAdapter.getItem(i);
  52. }
  53. @Override
  54. public void onNothingSelected(AdapterView<?> adapterView) {
  55. }
  56. });
  57. }
  58. @Override
  59. public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
  60. }
  61. @Override
  62. public void onNothingSelected(AdapterView<?> adapterView) {
  63. }
  64. private void addItems() {
  65. arrayList.add(" SELECT BRANCH");
  66. arrayList.add(" JNTU");
  67. arrayList.add(" JEEDIMETLA");
  68. arrayList.add(" SECUNDERABAD");
  69. arrayList.add(" UPPAL");
  70. arrayList.add(" KUKATPALLY");
  71. arrayList.add(" BACHUPALLY");
  72. arrayList.add(" AMEERPET");
  73. arrayList.add(" BALANAGAR");
  74. }
  75. public void register(View view) {
  76. if (Utils.hasActiveInternetConnection(Registration.this)) {
  77. progressDialog = Utils.showLoadingDialog(Registration.this, false);
  78. name = (EditText) findViewById(R.id.mobile);
  79. email = (EditText) findViewById(R.id.email);
  80. password = (EditText) findViewById(R.id.password);
  81. c_password = (EditText) findViewById(R.id.cpassword);
  82. Matcher matcher = VALID_EMAIL_ADDRESS_REGEX.matcher(email.getText().toString());
  83. final FirebaseAuth mAuth;
  84. if (matcher.find()) {
  85. //Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();
  86. if (!TextUtils.isEmpty(name.getText().toString())) {
  87. if (password.getText().toString().equals(c_password.getText().toString())) {
  88. if (password.getText().toString().length() >= 6) {
  89. if (!var.equals("SELECT BRANCH")) {
  90. mAuth = FirebaseAuth.getInstance();
  91. Task<AuthResult> authResultTask = mAuth.createUserWithEmailAndPassword(email.getText().toString()
  92. , password.getText().toString())
  93. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  94. @Override
  95. public void onComplete(@NonNull Task<AuthResult> task) {
  96. if (task.isSuccessful()) {
  97. // mAuth.reload();
  98. // mAuth.getCurrentUser().reload();
  99. //boolean emailVerified = mAuth.getCurrentUser().isEmailVerified();
  100. progressDialog.cancel();
  101. emailVerification();
  102. storeDetails();
  103. Intent intent = new Intent(Registration.this, MainActivity.class);
  104. SharedPreferences shared = getSharedPreferences("loginData", Context.MODE_PRIVATE);
  105. SharedPreferences.Editor editor = shared.edit();
  106. editor.putString("activity","register");
  107. editor.putString("user", email.getText().toString());
  108. editor.commit();
  109. Toast.makeText(Registration.this, "Successfully Registered..Thanks for Being a part of us..!", Toast.LENGTH_SHORT).show();
  110. startActivity(intent);
  111. Toast.makeText(getBaseContext(), "Job Done..!", Toast.LENGTH_LONG).show();
  112. }
  113. if (!task.isSuccessful()) {
  114. progressDialog.cancel();
  115. Toast.makeText(getBaseContext(), "E-mail I'd already exists",
  116. Toast.LENGTH_SHORT).show();
  117. }
  118. // ...
  119. }
  120. });
  121. } else {
  122. progressDialog.cancel();
  123. Toast.makeText(this, "Please Select Branch", Toast.LENGTH_SHORT).show();
  124. }
  125. } else {
  126. progressDialog.cancel();
  127. Toast.makeText(this, "Minimum length of Password is 6 characters", Toast.LENGTH_SHORT).show();
  128. }
  129. } else {
  130. progressDialog.cancel();
  131. Toast.makeText(this, "Password Mismatch", Toast.LENGTH_SHORT).show();
  132. }
  133. }
  134. else
  135. {
  136. progressDialog.cancel();
  137. Toast.makeText(this,"Please enter your Name",Toast.LENGTH_LONG).show();
  138. }
  139. }
  140. else {
  141. progressDialog.cancel();
  142. Toast.makeText(this, "Please give valid User Email", Toast.LENGTH_SHORT).show();
  143. }
  144. } else {
  145. progressDialog.cancel();
  146. JpmcToast.create(this, R.drawable.ic_error_outline_black_24dp, "No Internet\nPlease try again", Toast.LENGTH_SHORT);
  147. }
  148. }
  149. private void emailVerification() {
  150. FirebaseAuth auth = FirebaseAuth.getInstance();
  151. FirebaseUser user = auth.getCurrentUser();
  152. user.sendEmailVerification()
  153. .addOnCompleteListener(new OnCompleteListener<Void>() {
  154. @Override
  155. public void onComplete(@NonNull Task<Void> task) {
  156. if (task.isSuccessful()) {
  157. progressDialog.cancel();
  158. Toast.makeText(getApplicationContext(),"Email verification link has been sent",Toast.LENGTH_LONG).show();
  159. }
  160. else
  161. {
  162. Toast.makeText(getApplicationContext(),"Email verification link sending failed",Toast.LENGTH_LONG).show();
  163. }
  164. }
  165. });
  166. }
  167. private void storeDetails() {
  168. if (Utils.hasActiveInternetConnection(Registration.this)) {
  169. progressDialog = Utils.showLoadingDialog(Registration.this, false);
  170. if (var != null) {
  171. DatabaseReference mDatabase, mDatabase1, mDatabase2, mDatabase3, mDatabase4;
  172. mDatabase = FirebaseDatabase.getInstance().getReference();
  173. mDatabase1 = mDatabase.child("Customer");
  174. mDatabase2 = mDatabase1.push();
  175. mDatabase2.child("Email").setValue(email.getText().toString());
  176. mDatabase2.child("Name").setValue(name.getText().toString());
  177. mDatabase2.child("Branch").setValue(var);
  178. }
  179. else {
  180. progressDialog.cancel();
  181. Toast.makeText(getApplicationContext(), "Please choose Home branch", Toast.LENGTH_LONG).show();
  182. }
  183. } else {
  184. progressDialog.cancel();
  185. JpmcToast.create(this, R.drawable.ic_error_outline_black_24dp, "No Internet\nPlease try again", Toast.LENGTH_SHORT);
  186. }
  187. }
  188. public void back(View view){
  189. startActivity(new Intent(getApplicationContext(),SignIn.class));
  190. }
  191. }