Android Open Source - GasTracker Register






From Project

Back to project page GasTracker.

License

The source code is released under:

Copyright 2014 kurtzy317

If you think the Android project GasTracker 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.mindalsoblown.gastracker;
//w  w w  . j a va2 s  . c o  m
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.util.List;

import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Switch;

public class Register extends ActionBarActivity {

  DatabaseConnector connection;
  EditText txtUserName;
  EditText txtFName;
  EditText txtLName;
  EditText txtPassword;
  EditText txtEMail;
  Switch swicCarMpg;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);
    
    connection = new DatabaseConnector();
    
    txtEMail = (EditText)findViewById(R.id.txtEmailReg);
    txtPassword = (EditText)findViewById(R.id.txtPasswordReg);
    txtLName = (EditText)findViewById(R.id.txtLNameReg);
    txtFName = (EditText)findViewById(R.id.txtFNameReg);
    txtUserName = (EditText)findViewById(R.id.txtUserNameReg);
    swicCarMpg = (Switch)findViewById(R.id.swicCarMPG);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.register, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
      return true;
    }
    return super.onOptionsItemSelected(item);
  }
  
  public void NewUser(View view)
  {
    Switch swicCarMpg = (Switch)findViewById(R.id.swicCarMPG);
    
    if (!CheckRequiredFields())
      return;
    
    if (!ValidateUsername())
    {
      txtUserName.setError("Username already is use");
      return;
    }
    
    if (!ValidateEmail())
    {
      txtUserName.setError("Email already is use");
      return;
    }
    
    String text = txtPassword.getText().toString();
    MessageDigest md;
    String PasswordHash = null;
    try
    {
      md = MessageDigest.getInstance("SHA-1");
      md.update(text.getBytes("iso-8859-1"), 0, text.length());
          PasswordHash = convertToHex(md.digest());
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    User user = new User();
    
    user.setFName(txtFName.getText().toString());
    user.setLName(txtLName.getText().toString());
    user.setEmail(txtEMail.getText().toString());
    user.setUserName(txtUserName.getText().toString());
    user.setCarMPG(swicCarMpg.isChecked());
    user.setPassword(PasswordHash);
    
    connection.Register(user);
    
    user = connection.GetUserByUserName(user.getUserName());
     try
      {
      FileOutputStream outputStream = openFileOutput("ID", Context.MODE_PRIVATE);
      outputStream.write(String.valueOf(user.getID()).getBytes());
      outputStream.close();
      }
      catch (IOException e)
      {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
    
    Intent intent = new Intent(Register.this, MainMenu.class);
      startActivity(intent);
  }
  
  private static String convertToHex(byte[] data)
  {
        StringBuilder buf = new StringBuilder();
        for (byte b : data) {
            int halfbyte = (b >>> 4) & 0x0F;
            int two_halfs = 0;
            do {
                buf.append((0 <= halfbyte) && (halfbyte <= 9) ? (char) ('0' + halfbyte) : (char) ('a' + (halfbyte - 10)));
                halfbyte = b & 0x0F;
            } while (two_halfs++ < 1);
        }
        return buf.toString();
  }
  
  //return true if valid
  private boolean ValidateUsername ()
  {
    List<String> UserNames = connection.GetAllUserNames();
    String userName = txtUserName.getText().toString();
    
    for (String string : UserNames)
    {
      if (userName.equals(string))
      {
        return false;
      }
    }
    return true;
  }
  
  //return true if valid
  private boolean ValidateEmail ()
  {
    List<String> Emails = connection.GetAllEmails();
    String email = txtEMail.getText().toString();
    
    for (String string : Emails)
    {
      if (email.equals(string))
      {
        return false;
      }
    }
    return true;
  }
  
  //returns true if all fields are filled out
  private boolean CheckRequiredFields ()
  {
    boolean valid = true;
    if (txtEMail.getText().toString().trim().equals(""))
    {
      txtEMail.setError("Please enter your email.");
      valid =  false;
    }
    if (txtFName.getText().toString().trim().equals(""))
    {
      txtFName.setError("Please enter your first name.");
      valid = false;
    }
    if (txtLName.getText().toString().trim().equals(""))
    {
      txtLName.setError("Please enter your last name.");
      valid = false;
    }
    if (txtPassword.getText().toString().trim().equals(""))
    {
      txtPassword.setError("Please enter a password.");
      valid = false;
    }
    if (txtUserName.getText().toString().trim().equals(""))
    {
      txtUserName.setError("Please enter a user name.");
      valid = false;
    }
    return valid;
  }
}




Java Source Code List

com.mindalsoblown.gastracker.AddEntry.java
com.mindalsoblown.gastracker.DatabaseConnector.java
com.mindalsoblown.gastracker.Entry.java
com.mindalsoblown.gastracker.FillUpDetail.java
com.mindalsoblown.gastracker.History.java
com.mindalsoblown.gastracker.Login.java
com.mindalsoblown.gastracker.MainMenu.java
com.mindalsoblown.gastracker.Register.java
com.mindalsoblown.gastracker.Stats.java
com.mindalsoblown.gastracker.User.java