Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.eksell.eksell.eksell;
- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import com.backendless.Backendless;
- import com.backendless.BackendlessUser;
- import com.eksell.eksell.utility.BackendSettings;
- import com.eksell.eksell.utility.LoadingCallback;
- import com.eksell.eksell.utility.Validator;
- import java.util.Random;
- import java.util.Stack;
- /**
- * Registers the user through entered entries
- *
- * @author Eileen Mao
- * @version May 12, 2016
- *
- * @author Period - 3
- * @author Assignment - EKSell
- *
- * @author Sources - Backendless API
- */
- public class RegistrationActivity extends AppCompatActivity {
- /**
- * Creates the screen for the activity and initializes the BackEndless Application. The method
- * then creates the registration button and saves the entered entries (if valid) and saves them
- * into a user on BackEndless
- * @param savedInstanceState is the state of the app
- */
- @Override
- protected void onCreate( Bundle savedInstanceState )
- {
- super.onCreate( savedInstanceState );
- setContentView( R.layout.activity_registration );
- final TextView checkCaptchaField = (TextView)findViewById(R.id.textView);
- String beforeFlip = "";
- Random rand = new Random();
- for (int i=0; i<5; i++)
- {
- Integer x = (Integer) rand.nextInt(10);
- beforeFlip += x.toString();
- }
- checkCaptchaField.setText(beforeFlip);
- Backendless.initApp( this, BackendSettings.APPLICATION_ID, BackendSettings.SECRET_KEY,
- BackendSettings.VERSION );
- // click registration button
- Button registerButton = (Button) findViewById( R.id.registerButton );
- registerButton.setOnClickListener(new View.OnClickListener() {
- // once clicked, retrieves the entries and saves it into a user onto Backendless
- @Override
- public void onClick(View v) {
- EditText nameField = (EditText) findViewById( R.id.nameField );
- EditText emailField = (EditText) findViewById( R.id.emailField );
- EditText passwordField = (EditText) findViewById( R.id.passwordField );
- EditText passwordConfirmField = (EditText) findViewById( R.id.passwordConfirmField );
- //start
- EditText enteredTextField = (EditText)findViewById(R.id.notARobot);
- //end
- CharSequence name = nameField.getText();
- CharSequence email = emailField.getText();
- CharSequence password = passwordField.getText();
- CharSequence passwordConfirmation = passwordConfirmField.getText();
- //start
- CharSequence enteredText = enteredTextField.getText();
- if( isRegistrationValuesValid( name, email, password, passwordConfirmation, checkCaptchaField.getText(), enteredText ) )//end
- {
- BackendlessUser user = new BackendlessUser();
- user.setEmail( email.toString() );
- user.setPassword( password.toString() );
- user.setProperty( "name", name );
- LoadingCallback<BackendlessUser> registrationCallback = createRegistrationCallback();
- registrationCallback.showLoading();
- Backendless.UserService.register( user, registrationCallback );
- }
- }
- });
- }
- /**
- * Retrieves the result of the registration call
- * @return failure or success
- */
- public LoadingCallback<BackendlessUser> createRegistrationCallback()
- {
- return new LoadingCallback<BackendlessUser>( this, getString( R.string.loading_register ) )
- {
- /**
- * If the response is successful, register the user by their email, set the result to
- * okay, and finish the activity
- * @param registeredUser is the user that is being registered
- */
- @Override
- public void handleResponse( BackendlessUser registeredUser )
- {
- super.handleResponse( registeredUser );
- Intent registrationResult = new Intent();
- registrationResult.putExtra( BackendlessUser.EMAIL_KEY, registeredUser.getEmail() );
- setResult( RESULT_OK, registrationResult );
- RegistrationActivity.this.finish();
- }
- };
- }
- //start
- /**
- * Checks to see if the entered values are valid
- * @param name is the name of the user
- * @param email is the user's email
- * @param password is the user's desired password
- * @param passwordConfirm is a confirmation of the password
- * @return true if the registration is valid, false otherwise
- */
- private boolean isRegistrationValuesValid( CharSequence name, CharSequence email,
- CharSequence password, CharSequence passwordConfirm,
- CharSequence checkCaptcha, CharSequence enteredText )
- {
- return Validator.isNameValid( this, name )
- && Validator.isEmailValid( this, email )
- && Validator.isPasswordValid( this, password )
- && Validator.isPasswordsMatch( this, password, passwordConfirm )
- && Validator.isNotRobot(this, checkCaptcha, enteredText);
- //end
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement