Android Open Source - on-the-hook Register Activity






From Project

Back to project page on-the-hook.

License

The source code is released under:

MIT License

If you think the Android project on-the-hook listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.yoandinkov.onthehook;
/*ww w  .j a  va  2s. co  m*/
import java.util.Locale;

import org.apache.http.HttpStatus;


import com.yoandinkov.onthehook.crypt.JsonConverter;
import com.yoandinkov.onthehook.crypt.SHA1Converter;
import com.yoandinkov.onthehook.models.HttpResponseWrapper;
import com.yoandinkov.onthehook.models.Registration;
import com.yoandinkov.onthehook.models.RequestPurpose;
import com.yoandinkov.onthehook.models.Response;

import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;

public class RegisterActivity extends RequestActivity {

  private Button ButtonLogin;
  private Button ButtonRegister;
  private EditText EditTextUsername;
  private EditText EditTextFirstName;
  private EditText EditTextLastName;
  private ProgressDialog dialog;
  private final int NameMinLength = 2;
  private final int NameMaxLength = 30;
  private final int UsernameMinLength = 6;
  private final int UsernameMaxLength = 12;
  private final int PasswordMinLength = 6;
  private final int PasswordMaxLength = 12;
  private final String NameSymbols = "abcdefghijklmnopqrstuvwxyz";
  private final String UsernameSymbols = "abcdefghijklmnopqrstuvwxyz123456890_";
  private final String apiUrl = "http://on-the-hook.herokuapp.com/api";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);

    this.setButtonLogin((Button) findViewById(R.id.registerButtonLogin));
    this.setButtonRegister((Button) findViewById(R.id.registerButtonRegister));
    this.setEditTextFirstName((EditText) findViewById(R.id.registerEditTextFirstName));
    this.setEditTextLastName((EditText) findViewById(R.id.registerEditTextLastName));
    this.setEditTextUsername((EditText) findViewById(R.id.registerEditTextUsername));

    this.getButtonLogin().setOnClickListener(this);
    this.getButtonRegister().setOnClickListener(this);
  }

  private void showToast(String message) {
    Toast currentMessage = Toast.makeText(this, message, Toast.LENGTH_LONG);

    currentMessage.setGravity(Gravity.CENTER, 0, 0);

    currentMessage.show();
  }

  private void validateInformation(Registration currentRegistration) {
    this.validateUsername(currentRegistration.getUsername());
    this.validateName(currentRegistration.getFirstName());
    this.validateName(currentRegistration.getLastName());
  }

  private void validateUsername(String username)
      throws IllegalArgumentException {
    int usernameLength = username.length();

    if (usernameLength < UsernameMinLength || usernameLength > UsernameMaxLength) {
      throw new IllegalArgumentException(String.format(
          "Username should be between %d and %d symbols long",
          UsernameMinLength, UsernameMaxLength));
    }

    for (int i = 0; i < usernameLength; i++) {
      if (!UsernameSymbols.contains(String.valueOf(username.charAt(i))
          .toLowerCase(Locale.ENGLISH))) {
        throw new IllegalArgumentException(
            "Username should contain english letters in lower or upper case, numbers or symbol _.");
      }
    }
  }

  private void validateName(String name) throws IllegalArgumentException {
    int nameLength = name.length();

    if (nameLength < NameMinLength || nameLength > NameMaxLength) {
      throw new IllegalArgumentException(String.format(
          "Name should be between %d and %d symbols long",
          NameMinLength, NameMaxLength));
    }

    for (int i = 0; i < nameLength; i++) {
      if (!NameSymbols.contains(String.valueOf(name.charAt(i))
          .toLowerCase(Locale.ENGLISH))) {
        throw new IllegalArgumentException(
            "Name should contain english letters in lower or upper case.");
      }
    }
  }

  @Override
  public void onClick(View clickedView) {
    int clickedViewId = clickedView.getId();

    switch (clickedViewId) {
    case R.id.registerButtonRegister:
      this.buttonRegisterClick(clickedView);
      break;
    case R.id.registerButtonLogin:
      this.buttonLoginClick(clickedView);
      break;
    }
  }

  private void buttonLoginClick(View currentView) {
    Intent startLoginIntent = new Intent(this, LoginActivity.class);

    startActivity(startLoginIntent);
  }

  private void buttonRegisterClick(View currentView) {
    Registration currentRegistration = new Registration();

    currentRegistration.setUsername(this.getEditTextUsername().getText()
        .toString());
    currentRegistration.setFirstName(this.getEditTextFirstName().getText()
        .toString());
    currentRegistration.setLastName(this.getEditTextLastName().getText()
        .toString());

    String firstPassword = ((EditText) findViewById(R.id.registerEditTextFirstPassword))
        .getText().toString();
    String secondPassword = ((EditText) findViewById(R.id.registerEditTextSecondPassword))
        .getText().toString();

    if (firstPassword.equals(secondPassword) == false) {
      this.showToast("Passwords don't match");
      return;
    }
    
    if (secondPassword.length() < this.PasswordMinLength || secondPassword.length() > this.PasswordMaxLength) {
      this.showToast(String.format("Password should be between %d and %d symbols.", this.PasswordMinLength, this.PasswordMaxLength));
      return;
    }

    currentRegistration.setAuthKey(SHA1Converter.encryptPassword(secondPassword));

    try {
      this.validateInformation(currentRegistration);
    } catch (Exception ex) {
      this.showToast(ex.getMessage());
      return;
    }

    this.registerUser(currentRegistration);
  }

  private void registerUser(Registration registration) {
    String regisrationAsJson = JsonConverter.toJson(registration);
    
    String url = apiUrl + "/users/register";
    
    HttpPostRequester currentRequest = new HttpPostRequester();
    
    currentRequest.setCurrentPurpose(RequestPurpose.REGISTRATION);

    this.setDialog(ProgressDialog.show(this, "Registering ..", ""));

    currentRequest.execute(url, regisrationAsJson);
  }

  private void startMainMenuActivity(String sessionKey) {
    Intent startMainMenuIntent = new Intent(this, MainMenuActivity.class);

    startMainMenuIntent.putExtra("sessionKey", sessionKey);

    startActivity(startMainMenuIntent);

    finish();
  }

  // Auto-generated methods
  public Button getButtonLogin() {
    return ButtonLogin;
  }

  public void setButtonLogin(Button buttonLogin) {
    ButtonLogin = buttonLogin;
  }

  public Button getButtonRegister() {
    return ButtonRegister;
  }

  public void setButtonRegister(Button buttonRegister) {
    ButtonRegister = buttonRegister;
  }

  public EditText getEditTextUsername() {
    return EditTextUsername;
  }

  public void setEditTextUsername(EditText editTextUsername) {
    EditTextUsername = editTextUsername;
  }

  public EditText getEditTextFirstName() {
    return EditTextFirstName;
  }

  public void setEditTextFirstName(EditText editTextFirstName) {
    EditTextFirstName = editTextFirstName;
  }

  public EditText getEditTextLastName() {
    return EditTextLastName;
  }

  public void setEditTextLastName(EditText editTextLastName) {
    EditTextLastName = editTextLastName;
  }

  @Override
  protected void handleResponse(HttpResponseWrapper response) {
    switch (response.getPurpose()) {
    case REGISTRATION:
      this.completeRegistration(response.getResponse());
      break;
    default:
      break;
    }
  }
  
  private void completeRegistration(Response currentResponse) {
    Log.d("Completed registration status Code:", String.valueOf(currentResponse.getStatusCode()));
    Log.d("Completed registration body:", String.valueOf(currentResponse.getStatusCode()));
    
    if (currentResponse.getStatusCode() == HttpStatus.SC_CREATED) {
      this.getDialog().hide();
      
      String sessionKey = currentResponse.getBody();

      this.startMainMenuActivity(sessionKey);
    } else {
      this.getDialog().hide();
      
      AlertDialog.Builder errorDisplay = new AlertDialog.Builder(this);

      errorDisplay
          .setTitle("Error")
          .setMessage(currentResponse.getBody())
          .setPositiveButton("OK", null);

      errorDisplay.show();
    }
  }

  public ProgressDialog getDialog() {
    return dialog;
  }

  public void setDialog(ProgressDialog dialog) {
    this.dialog = dialog;
  }
}




Java Source Code List

com.yoandinkov.onthehook.DraughtActivity.java
com.yoandinkov.onthehook.FishActivity.java
com.yoandinkov.onthehook.FishingActivity.java
com.yoandinkov.onthehook.LoginActivity.java
com.yoandinkov.onthehook.MainMenuActivity.java
com.yoandinkov.onthehook.RegisterActivity.java
com.yoandinkov.onthehook.RequestActivity.java
com.yoandinkov.onthehook.adapters.FishAdapter.java
com.yoandinkov.onthehook.crypt.JsonConverter.java
com.yoandinkov.onthehook.crypt.SHA1Converter.java
com.yoandinkov.onthehook.db.DatabaseHelper.java
com.yoandinkov.onthehook.db.DatabaseManager.java
com.yoandinkov.onthehook.db.models.FishDbModel.java
com.yoandinkov.onthehook.models.Coordinates.java
com.yoandinkov.onthehook.models.Credentials.java
com.yoandinkov.onthehook.models.FishButton.java
com.yoandinkov.onthehook.models.FishSpecies.java
com.yoandinkov.onthehook.models.Fish.java
com.yoandinkov.onthehook.models.Fishing.java
com.yoandinkov.onthehook.models.HttpResponseWrapper.java
com.yoandinkov.onthehook.models.HttpType.java
com.yoandinkov.onthehook.models.Login.java
com.yoandinkov.onthehook.models.NewFishing.java
com.yoandinkov.onthehook.models.Registration.java
com.yoandinkov.onthehook.models.RequestPurpose.java
com.yoandinkov.onthehook.models.Response.java
com.yoandinkov.onthehook.models.WaterLocation.java