Guest User

RegistrationNew

a guest
May 29th, 2016
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 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.  
  44. Backendless.initApp( this, BackendSettings.APPLICATION_ID, BackendSettings.SECRET_KEY,
  45. BackendSettings.VERSION );
  46.  
  47. // click registration button
  48. Button registerButton = (Button) findViewById( R.id.registerButton );
  49. registerButton.setOnClickListener(new View.OnClickListener() {
  50.  
  51. // once clicked, retrieves the entries and saves it into a user onto Backendless
  52. @Override
  53. public void onClick(View v) {
  54. EditText nameField = (EditText) findViewById( R.id.nameField );
  55. EditText emailField = (EditText) findViewById( R.id.emailField );
  56. EditText passwordField = (EditText) findViewById( R.id.passwordField );
  57. EditText passwordConfirmField = (EditText) findViewById( R.id.passwordConfirmField );
  58. //start
  59. TextView checkCaptchaField = (TextView)findViewById(R.id.textView);
  60.  
  61. String beforeFlip = "";
  62. Random rand = new Random();
  63. Stack firstStack = new Stack();
  64. for (int i=0; i<5; i++)
  65. {
  66. Integer x = (Integer) rand.nextInt(10);
  67. firstStack.push(x);
  68.  
  69. beforeFlip += x.toString();
  70. }
  71.  
  72. EditText enteredTextField = (EditText)findViewById(R.id.notARobot);
  73. //end
  74.  
  75. CharSequence name = nameField.getText();
  76. CharSequence email = emailField.getText();
  77. CharSequence password = passwordField.getText();
  78. CharSequence passwordConfirmation = passwordConfirmField.getText();
  79. //start
  80. checkCaptchaField.setText(beforeFlip);
  81. CharSequence enteredText = enteredTextField.getText();
  82.  
  83. if( isRegistrationValuesValid( name, email, password, passwordConfirmation, beforeFlip, enteredText ) )//end
  84. {
  85. BackendlessUser user = new BackendlessUser();
  86. user.setEmail( email.toString() );
  87. user.setPassword( password.toString() );
  88. user.setProperty( "name", name );
  89.  
  90. LoadingCallback<BackendlessUser> registrationCallback = createRegistrationCallback();
  91. registrationCallback.showLoading();
  92. Backendless.UserService.register( user, registrationCallback );
  93. }
  94. }
  95. });
  96. }
  97.  
  98. /**
  99. * Retrieves the result of the registration call
  100. * @return failure or success
  101. */
  102. public LoadingCallback<BackendlessUser> createRegistrationCallback()
  103. {
  104. return new LoadingCallback<BackendlessUser>( this, getString( R.string.loading_register ) )
  105. {
  106. /**
  107. * If the response is successful, register the user by their email, set the result to
  108. * okay, and finish the activity
  109. * @param registeredUser is the user that is being registered
  110. */
  111. @Override
  112. public void handleResponse( BackendlessUser registeredUser )
  113. {
  114. super.handleResponse( registeredUser );
  115. Intent registrationResult = new Intent();
  116. registrationResult.putExtra( BackendlessUser.EMAIL_KEY, registeredUser.getEmail() );
  117. setResult( RESULT_OK, registrationResult );
  118. RegistrationActivity.this.finish();
  119. }
  120. };
  121. }
  122.  
  123. //start
  124. /**
  125. * Checks to see if the entered values are valid
  126. * @param name is the name of the user
  127. * @param email is the user's email
  128. * @param password is the user's desired password
  129. * @param passwordConfirm is a confirmation of the password
  130. * @return true if the registration is valid, false otherwise
  131. */
  132. private boolean isRegistrationValuesValid( CharSequence name, CharSequence email,
  133. CharSequence password, CharSequence passwordConfirm,
  134. String checkCaptcha, CharSequence enteredText )
  135. {
  136.  
  137. return Validator.isNameValid( this, name )
  138. && Validator.isEmailValid( this, email )
  139. && Validator.isPasswordValid( this, password )
  140. && Validator.isPasswordsMatch( this, password, passwordConfirm )
  141. && Validator.isNotRobot(this, checkCaptcha, enteredText);
  142. //end
  143. }
  144. }
Add Comment
Please, Sign In to add comment