Back to project page GasTracker.
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.
package com.mindalsoblown.gastracker; // w ww . java 2 s.c om import java.io.FileOutputStream; import java.io.IOException; import java.security.MessageDigest; import android.app.Activity; 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.TextView; public class Login extends Activity { DatabaseConnector connection; TextView txtUserName; TextView txtPassword; TextView lblError; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); connection = new DatabaseConnector(); lblError = (TextView)findViewById(R.id.lblErrorLogin); txtPassword = (TextView)findViewById(R.id.txtPasswordLogin); txtUserName = (TextView)findViewById(R.id.txtUserNameLogin); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.login, 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 AttemptLogin (View view) { if (!ValidateInput()) return; User user = connection.GetUserByUserName(txtUserName.getText().toString()); if (user.getID() == 0) { lblError.setText("Username or password incorrect"); 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(); } if (!PasswordHash.equals(user.getPassword())) { lblError.setText("Username or password incorrect"); return; } 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(Login.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(); } public void Register(View view) { Intent intent = new Intent(Login.this, Register.class); startActivity(intent); } private boolean ValidateInput() { boolean valid = true; 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; } }