Guest User

Untitled

a guest
Apr 3rd, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.15 KB | None | 0 0
  1. package com.winkel.winkelapp.Activities;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.content.Intent;
  5. import android.support.annotation.NonNull;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.text.TextUtils;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.view.WindowManager;
  12. import android.widget.Button;
  13. import android.widget.EditText;
  14. import android.widget.Toast;
  15.  
  16. import com.winkel.winkelapp.R;
  17.  
  18.  
  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.FirebaseAuthActionCodeException;
  24. import com.google.firebase.auth.FirebaseAuthException;
  25. import com.google.firebase.auth.FirebaseUser;
  26.  
  27. import java.util.Calendar;
  28.  
  29. import com.amazonaws.auth.CognitoCachingCredentialsProvider;
  30. import com.amazonaws.regions.Regions;
  31. import com.amazonaws.services.dynamodbv2.*;
  32. import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.*;
  33. import com.amazonaws.services.dynamodbv2.model.*;
  34.  
  35. public class SignupActivity extends AppCompatActivity implements View.OnClickListener {
  36.  
  37. Button signupBtn, loginBtn;
  38. //definig the view objects
  39. private Button signup_btn;
  40. private EditText email, password, conf_password, firstname, lastname, gradyear;
  41. private FirebaseAuth firebaseAuth;
  42. private int year = Calendar.getInstance().get(Calendar.YEAR);
  43. final String LOG_TAG="";
  44. private ProgressDialog progressDialog;
  45. @Override
  46. protected void onCreate(Bundle savedInstanceState) {
  47. super.onCreate(savedInstanceState);
  48. setContentView(R.layout.activity_signup);
  49.  
  50. //defining firebase authentification
  51. firebaseAuth = FirebaseAuth.getInstance();
  52. //if getCurrentUser does not returns null , that means user is already logged in , so th activity closes and opens the merchant profile
  53. if(firebaseAuth.getCurrentUser() != null){
  54. finish();
  55. //start profile here
  56. startActivity(new Intent(getApplicationContext(),MerchantProfileActivity.class));
  57. }
  58.  
  59. progressDialog = new ProgressDialog(this);
  60. //this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
  61. this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
  62. //initializing the views
  63. signup_btn = (Button)findViewById(R.id. signup_btn);
  64. email = (EditText) findViewById(R.id.email);
  65. password = (EditText) findViewById(R.id.password);
  66. conf_password = (EditText) findViewById(R.id.conf_password);
  67. firstname = (EditText) findViewById(R.id.first_name);
  68. lastname = (EditText) findViewById(R.id.last_name);
  69. gradyear = (EditText) findViewById(R.id.graduation_year);
  70. signupBtn = (Button) findViewById(R.id.signup_btn);
  71. loginBtn = (Button) findViewById(R.id.signin_btn);
  72.  
  73.  
  74. signupBtn.setOnClickListener(this);
  75. loginBtn.setOnClickListener(this);
  76. }
  77.  
  78.  
  79. private void sendEmail() {
  80.  
  81. final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
  82. user.sendEmailVerification()
  83. .addOnCompleteListener(new OnCompleteListener<Void>() {
  84. @Override
  85. public void onComplete(@NonNull Task<Void> task) {
  86. if (task.isSuccessful()){
  87. Toast.makeText(SignupActivity.this, "You have been sent a confirmation email, Please verify your account", Toast.LENGTH_SHORT).show();
  88. }
  89. }
  90. });
  91. }
  92. String [] domainCheck;
  93. private void registerUser(){
  94. String email1 = email.getText().toString().trim();
  95. String email2 = email1;
  96. String password1 = password.getText().toString().trim();
  97. String conf_password1 = conf_password.getText().toString().trim();
  98. String firstname1 = firstname.getText().toString().trim();
  99. String lastname1 = lastname.getText().toString().trim();
  100. String gradyear1 = gradyear.getText().toString().trim();
  101. if (TextUtils.isEmpty(firstname1)){ //checking if user has filled the first name
  102. //if first name is empty
  103. Toast.makeText(this, "Please Enter Your First Name", Toast.LENGTH_SHORT).show();
  104. return; //stopping the function from executing further
  105. }
  106. if (TextUtils.isEmpty(lastname1)){ //checking if user has filled the last name
  107. //if last name is empty
  108. Toast.makeText(this, "Please Enter Your Last Name", Toast.LENGTH_SHORT).show();
  109. return; //stopping the function from executing further
  110. }
  111. if (TextUtils.isEmpty(email1)){ //if user has filled the email address
  112. //if email is empty
  113. Toast.makeText(this,"Please Enter The Correct Email Address", Toast.LENGTH_SHORT).show();
  114. return; // stops the functon from excecuting further
  115. }
  116. domainCheck = new String [2]; //spliting the email address to check the domain
  117. if(email2.indexOf("@")>=0){
  118. domainCheck = email2.split("@");
  119. }
  120. if(email2.indexOf("@")<0){ //if the email format is not correct
  121. Toast.makeText(this, "Please Enter A Valid Email Address", Toast.LENGTH_SHORT).show();
  122. return;
  123. }
  124. if (!(domainCheck[1].equals("brunel.ac.uk") || domainCheck[1].equals("my.brunel.ac.uk") || domainCheck[1].equals("winkelapp.com"))){ //checking if user has filled the supported domain
  125. Toast.makeText(this, "Your domain is currently not supported, please try again later", Toast.LENGTH_SHORT).show();
  126. return;
  127. }
  128. if (TextUtils.isEmpty(gradyear1)){ //if user has filled the graduation year
  129. //if graduation year is empty
  130. Toast.makeText(this, "Please Enter Your Graduation Year", Toast.LENGTH_SHORT).show();
  131. return; //stopping the function from executing further
  132. }
  133. if (Integer.parseInt(gradyear1)<year){ //if graduation year is correct
  134. Toast.makeText(this, "Please Make Sure The Graduation Year Is Correct", Toast.LENGTH_SHORT).show();
  135. return;
  136. }
  137. if (TextUtils.isEmpty(password1)){ //if user has filled the password
  138. //if password is empty
  139. Toast.makeText(this, "Please Enter The Password", Toast.LENGTH_SHORT).show();
  140. return; //stopping the function from executing further
  141. }
  142. if (TextUtils.isEmpty(conf_password1)){ //if user has filled confirm password
  143. //if confirm password is empty
  144. Toast.makeText(this, "Please Confirm Your Password", Toast.LENGTH_SHORT).show();
  145. return; //stopping the function from executing further
  146. }
  147. if (!(password1.equals(conf_password1))){ //if two password are same
  148. Toast.makeText(this, "Please make sure you're typing the same password", Toast.LENGTH_SHORT).show();
  149. return;
  150. }
  151. progressDialog.setMessage("Registering User");
  152. progressDialog.show();
  153.  
  154.  
  155. firebaseAuth.createUserWithEmailAndPassword(email1,password1)
  156. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  157. @Override
  158. public void onComplete(@NonNull Task<AuthResult> task) {
  159.  
  160. if (task.isSuccessful()){
  161. Toast.makeText(SignupActivity.this, "Registered Successfully", Toast.LENGTH_SHORT).show();
  162. sendEmail();
  163. /*try{
  164. insertData();
  165. }
  166. catch (Exception e) {
  167. Toast.makeText(SignupActivity.this, "User Registered but Failed to store data in the database", Toast.LENGTH_SHORT).show();
  168. firebaseAuth.signOut();
  169. }*/
  170. finish();
  171. //start profile here
  172. startActivity(new Intent(getApplicationContext(),LoginActivity.class));
  173.  
  174. }
  175.  
  176. else{
  177. Toast.makeText(SignupActivity.this, "Registration Failed, please make sure all fields are complete", Toast.LENGTH_SHORT).show();
  178. }
  179. progressDialog.dismiss();
  180. }
  181. });
  182. }
  183. /*@Override
  184. public void onBackPressed(){
  185. finish();
  186. }*/
  187. public void onClick(View view) {
  188. if(view == signupBtn)
  189. registerUser();
  190. else if (view == loginBtn) {
  191. Intent launchNextActivity;
  192. launchNextActivity = new Intent(SignupActivity.this, LoginActivity.class);
  193. startActivity(launchNextActivity);
  194. }
  195. }
  196. FirebaseUser user1 = null;
  197. /*public void insertData() {
  198. // Fetch the default configured DynamoDB ObjectMapper
  199. final DynamoDBMapper dynamoDBMapper = AWSMobileClient.defaultMobileClient().getDynamoDBMapper();
  200. //final UserDO ud = new UserDO(); // Initialize the Notes Object
  201. //user1 = FirebaseAuth.getInstance().getCurrentUser();
  202. // The userId has to be set to user's Cognito Identity Id for private / protected tables.
  203. // User's Cognito Identity Id can be fetched by using:
  204. // AWSMobileClient.defaultMobileClient().getIdentityManager().getCachedUserID()
  205. *//*ud.setUserId(user1.getUid());
  206. ud.setEmail(email.toString().trim());
  207. ud.setFName(firstname.toString().trim());
  208. ud.setLName(lastname.toString().trim());
  209. ud.setGradYear(gradyear.toString().trim());
  210. if(domainCheck[2].equals("winkelapp.com")){
  211. ud.setUniversityId("0001");
  212. }
  213. if (domainCheck[2].equals("my.brunel.ac.uk") || domainCheck[2].equals("brunel.ac.uk")){
  214. ud.setUniversityId("0002");
  215. }
  216. AmazonClientException lastException = null;
  217. try {
  218. dynamoDBMapper.save(ud);
  219. } catch (final AmazonClientException ex) {
  220. Log.e(LOG_TAG, "Failed saving item : " + ex.getMessage(), ex);
  221. lastException = ex;
  222. }*//*
  223. }*/
Add Comment
Please, Sign In to add comment