Advertisement
Guest User

RegistrationNew2

a guest
May 29th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.68 KB | None | 0 0
  1. package com.eksell.eksell.eksell;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.TextView;
  10.  
  11. import com.backendless.Backendless;
  12. import com.backendless.BackendlessUser;
  13. import com.eksell.eksell.utility.BackendSettings;
  14. import com.eksell.eksell.utility.LoadingCallback;
  15. import com.eksell.eksell.utility.Validator;
  16.  
  17. import java.util.Random;
  18. import java.util.Stack;
  19.  
  20. /**
  21. * Registers the user through entered entries
  22. *
  23. * @author Eileen Mao
  24. * @version May 12, 2016
  25. *
  26. * @author Period - 3
  27. * @author Assignment - EKSell
  28. *
  29. * @author Sources - Backendless API
  30. */
  31. public class RegistrationActivity extends AppCompatActivity {
  32. /**
  33. * Creates the screen for the activity and initializes the BackEndless Application. The method
  34. * then creates the registration button and saves the entered entries (if valid) and saves them
  35. * into a user on BackEndless
  36. * @param savedInstanceState is the state of the app
  37. */
  38. @Override
  39. protected void onCreate( Bundle savedInstanceState )
  40. {
  41. super.onCreate( savedInstanceState );
  42. setContentView( R.layout.activity_registration );
  43. final TextView checkCaptchaField = (TextView)findViewById(R.id.textView);
  44.  
  45. String beforeFlip = "";
  46. Random rand = new Random();
  47. for (int i=0; i<5; i++)
  48. {
  49. Integer x = (Integer) rand.nextInt(10);
  50. beforeFlip += x.toString();
  51. }
  52.  
  53. checkCaptchaField.setText(beforeFlip);
  54.  
  55. Backendless.initApp( this, BackendSettings.APPLICATION_ID, BackendSettings.SECRET_KEY,
  56. BackendSettings.VERSION );
  57.  
  58. // click registration button
  59. Button registerButton = (Button) findViewById( R.id.registerButton );
  60. registerButton.setOnClickListener(new View.OnClickListener() {
  61.  
  62. // once clicked, retrieves the entries and saves it into a user onto Backendless
  63. @Override
  64. public void onClick(View v) {
  65. EditText nameField = (EditText) findViewById( R.id.nameField );
  66. EditText emailField = (EditText) findViewById( R.id.emailField );
  67. EditText passwordField = (EditText) findViewById( R.id.passwordField );
  68. EditText passwordConfirmField = (EditText) findViewById( R.id.passwordConfirmField );
  69. //start
  70.  
  71. EditText enteredTextField = (EditText)findViewById(R.id.notARobot);
  72. //end
  73.  
  74. CharSequence name = nameField.getText();
  75. CharSequence email = emailField.getText();
  76. CharSequence password = passwordField.getText();
  77. CharSequence passwordConfirmation = passwordConfirmField.getText();
  78. //start
  79.  
  80. CharSequence enteredText = enteredTextField.getText();
  81.  
  82. if( isRegistrationValuesValid( name, email, password, passwordConfirmation, checkCaptchaField.getText(), enteredText ) )//end
  83. {
  84. BackendlessUser user = new BackendlessUser();
  85. user.setEmail( email.toString() );
  86. user.setPassword( password.toString() );
  87. user.setProperty( "name", name );
  88.  
  89. LoadingCallback<BackendlessUser> registrationCallback = createRegistrationCallback();
  90. registrationCallback.showLoading();
  91. Backendless.UserService.register( user, registrationCallback );
  92. }
  93. }
  94. });
  95. }
  96.  
  97. /**
  98. * Retrieves the result of the registration call
  99. * @return failure or success
  100. */
  101. public LoadingCallback<BackendlessUser> createRegistrationCallback()
  102. {
  103. return new LoadingCallback<BackendlessUser>( this, getString( R.string.loading_register ) )
  104. {
  105. /**
  106. * If the response is successful, register the user by their email, set the result to
  107. * okay, and finish the activity
  108. * @param registeredUser is the user that is being registered
  109. */
  110. @Override
  111. public void handleResponse( BackendlessUser registeredUser )
  112. {
  113. super.handleResponse( registeredUser );
  114. Intent registrationResult = new Intent();
  115. registrationResult.putExtra( BackendlessUser.EMAIL_KEY, registeredUser.getEmail() );
  116. setResult( RESULT_OK, registrationResult );
  117. RegistrationActivity.this.finish();
  118. }
  119. };
  120. }
  121.  
  122. //start
  123. /**
  124. * Checks to see if the entered values are valid
  125. * @param name is the name of the user
  126. * @param email is the user's email
  127. * @param password is the user's desired password
  128. * @param passwordConfirm is a confirmation of the password
  129. * @return true if the registration is valid, false otherwise
  130. */
  131. private boolean isRegistrationValuesValid( CharSequence name, CharSequence email,
  132. CharSequence password, CharSequence passwordConfirm,
  133. CharSequence checkCaptcha, CharSequence enteredText )
  134. {
  135.  
  136. return Validator.isNameValid( this, name )
  137. && Validator.isEmailValid( this, email )
  138. && Validator.isPasswordValid( this, password )
  139. && Validator.isPasswordsMatch( this, password, passwordConfirm )
  140. && Validator.isNotRobot(this, checkCaptcha, enteredText);
  141. //end
  142. }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement