Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

The app code

  • Loading branch information...
commit 874a79661d606d013726c70e90a9a4bb86f570fc 0 parents
Andrei Pop authored
Showing with 993 additions and 0 deletions.
  1. BIN  .DS_Store
  2. +32 −0 AndroidManifest.xml
  3. BIN  bin/LoginApp.apk
  4. BIN  bin/classes.dex
  5. BIN  bin/com/android/database/DatabaseAdapter.class
  6. BIN  bin/com/android/database/DatabaseHelper.class
  7. BIN  bin/com/android/loginapp/Helloworld.class
  8. BIN  bin/com/android/loginapp/R$attr.class
  9. BIN  bin/com/android/loginapp/R$drawable.class
  10. BIN  bin/com/android/loginapp/R$id.class
  11. BIN  bin/com/android/loginapp/R$layout.class
  12. BIN  bin/com/android/loginapp/R$string.class
  13. BIN  bin/com/android/loginapp/R.class
  14. BIN  bin/com/android/loginapp/Register$1.class
  15. BIN  bin/com/android/loginapp/Register$2.class
  16. BIN  bin/com/android/loginapp/Register$3.class
  17. BIN  bin/com/android/loginapp/Register.class
  18. BIN  bin/com/android/loginapp/login$1.class
  19. BIN  bin/com/android/loginapp/login$2.class
  20. BIN  bin/com/android/loginapp/login$3.class
  21. BIN  bin/com/android/loginapp/login$4.class
  22. BIN  bin/com/android/loginapp/login$5.class
  23. BIN  bin/com/android/loginapp/login.class
  24. BIN  bin/resources.ap_
  25. +11 −0 default.properties
  26. +48 −0 gen/com/android/loginapp/R.java
  27. +36 −0 proguard.cfg
  28. BIN  res/drawable-hdpi/icon.png
  29. BIN  res/drawable-ldpi/icon.png
  30. BIN  res/drawable-mdpi/icon.png
  31. +12 −0 res/layout/hello.xml
  32. +108 −0 res/layout/main.xml
  33. +97 −0 res/layout/register.xml
  34. +11 −0 res/values/strings.xml
  35. +136 −0 src/com/android/database/DatabaseAdapter.java
  36. +45 −0 src/com/android/database/DatabaseHelper.java
  37. +28 −0 src/com/android/loginapp/Helloworld.java
  38. +206 −0 src/com/android/loginapp/Register.java
  39. +223 −0 src/com/android/loginapp/login.java
BIN  .DS_Store
Binary file not shown
32 AndroidManifest.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.android.loginapp"
+ android:versionCode="1"
+ android:versionName="1.0">
+ <uses-sdk android:minSdkVersion="9" />
+
+ <application android:icon="@drawable/icon" android:label="@string/app_name">
+ <activity android:name=".login"
+ android:label="@string/app_name">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="android.intent.category.LAUNCHER" />
+ </intent-filter>
+ </activity>
+ <activity android:name=".Register"
+ android:label="@string/app_name">
+ <intent-filter>
+ <action android:name="android.intent.action.VIEW" />
+ <category android:name="android.intent.category.DEFAULT" />
+ </intent-filter>
+ </activity>
+
+ <activity android:name=".Helloworld"
+ android:label="@string/app_name">
+ <intent-filter>
+ <action android:name="android.intent.action.VIEW" />
+ <category android:name="android.intent.category.DEFAULT" />
+ </intent-filter>
+ </activity>
+ </application>
+</manifest>
BIN  bin/LoginApp.apk
Binary file not shown
BIN  bin/classes.dex
Binary file not shown
BIN  bin/com/android/database/DatabaseAdapter.class
Binary file not shown
BIN  bin/com/android/database/DatabaseHelper.class
Binary file not shown
BIN  bin/com/android/loginapp/Helloworld.class
Binary file not shown
BIN  bin/com/android/loginapp/R$attr.class
Binary file not shown
BIN  bin/com/android/loginapp/R$drawable.class
Binary file not shown
BIN  bin/com/android/loginapp/R$id.class
Binary file not shown
BIN  bin/com/android/loginapp/R$layout.class
Binary file not shown
BIN  bin/com/android/loginapp/R$string.class
Binary file not shown
BIN  bin/com/android/loginapp/R.class
Binary file not shown
BIN  bin/com/android/loginapp/Register$1.class
Binary file not shown
BIN  bin/com/android/loginapp/Register$2.class
Binary file not shown
BIN  bin/com/android/loginapp/Register$3.class
Binary file not shown
BIN  bin/com/android/loginapp/Register.class
Binary file not shown
BIN  bin/com/android/loginapp/login$1.class
Binary file not shown
BIN  bin/com/android/loginapp/login$2.class
Binary file not shown
BIN  bin/com/android/loginapp/login$3.class
Binary file not shown
BIN  bin/com/android/loginapp/login$4.class
Binary file not shown
BIN  bin/com/android/loginapp/login$5.class
Binary file not shown
BIN  bin/com/android/loginapp/login.class
Binary file not shown
BIN  bin/resources.ap_
Binary file not shown
11 default.properties
@@ -0,0 +1,11 @@
+# This file is automatically generated by Android Tools.
+# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
+#
+# This file must be checked in Version Control Systems.
+#
+# To customize properties used by the Ant build system use,
+# "build.properties", and override values to adapt the script to your
+# project structure.
+
+# Project target.
+target=android-9
48 gen/com/android/loginapp/R.java
@@ -0,0 +1,48 @@
+/* AUTO-GENERATED FILE. DO NOT MODIFY.
+ *
+ * This class was automatically generated by the
+ * aapt tool from the resource data it found. It
+ * should not be modified by hand.
+ */
+
+package com.android.loginapp;
+
+public final class R {
+ public static final class attr {
+ }
+ public static final class drawable {
+ public static final int icon=0x7f020000;
+ }
+ public static final class id {
+ public static final int Clear=0x7f050006;
+ public static final int Exit=0x7f050008;
+ public static final int Label=0x7f050001;
+ public static final int Login=0x7f050005;
+ public static final int Password=0x7f050003;
+ public static final int Register=0x7f050007;
+ public static final int RememberMe=0x7f050004;
+ public static final int Username=0x7f050002;
+ public static final int myTextView=0x7f050000;
+ public static final int nBack=0x7f05000e;
+ public static final int nClear=0x7f05000d;
+ public static final int nConfiPass=0x7f05000b;
+ public static final int nPassword=0x7f05000a;
+ public static final int nRegister=0x7f05000c;
+ public static final int nUsername=0x7f050009;
+ }
+ public static final class layout {
+ public static final int hello=0x7f030000;
+ public static final int main=0x7f030001;
+ public static final int register=0x7f030002;
+ }
+ public static final class string {
+ public static final int Back=0x7f040007;
+ public static final int Clear=0x7f040004;
+ public static final int Exit=0x7f040003;
+ public static final int Login=0x7f040006;
+ public static final int Register=0x7f040005;
+ public static final int app_name=0x7f040002;
+ public static final int registration=0x7f040001;
+ public static final int title=0x7f040000;
+ }
+}
36 proguard.cfg
@@ -0,0 +1,36 @@
+-optimizationpasses 5
+-dontusemixedcaseclassnames
+-dontskipnonpubliclibraryclasses
+-dontpreverify
+-verbose
+-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
+
+-keep public class * extends android.app.Activity
+-keep public class * extends android.app.Application
+-keep public class * extends android.app.Service
+-keep public class * extends android.content.BroadcastReceiver
+-keep public class * extends android.content.ContentProvider
+-keep public class * extends android.app.backup.BackupAgentHelper
+-keep public class * extends android.preference.Preference
+-keep public class com.android.vending.licensing.ILicensingService
+
+-keepclasseswithmembernames class * {
+ native <methods>;
+}
+
+-keepclasseswithmembernames class * {
+ public <init>(android.content.Context, android.util.AttributeSet);
+}
+
+-keepclasseswithmembernames class * {
+ public <init>(android.content.Context, android.util.AttributeSet, int);
+}
+
+-keepclassmembers enum * {
+ public static **[] values();
+ public static ** valueOf(java.lang.String);
+}
+
+-keep class * implements android.os.Parcelable {
+ public static final android.os.Parcelable$Creator *;
+}
BIN  res/drawable-hdpi/icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  res/drawable-ldpi/icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  res/drawable-mdpi/icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 res/layout/hello.xml
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="utf-8"?>
+ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:orientation="vertical"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent">
+ <TextView
+ android:id="@+id/myTextView"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:text="Hello World!"
+ />
+ </LinearLayout>
108 res/layout/main.xml
@@ -0,0 +1,108 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_height="fill_parent"
+ android:layout_width="fill_parent"
+ android:layout_gravity="fill_horizontal"
+ android:orientation="vertical">
+ <TextView
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:text="@string/title"
+ android:id="@+id/Label"
+ />
+ <LinearLayout
+ android:orientation="horizontal"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ >
+ <EditText
+ android:layout_height="wrap_content"
+ android:id="@+id/Username"
+ android:selectAllOnFocus="true"
+ android:lines="1"
+ android:hint="Please enter your username"
+ android:inputType="text"
+ android:layout_width="fill_parent"
+ android:layout_gravity="fill_horizontal"
+ android:gravity="fill_horizontal">
+ </EditText>
+ </LinearLayout>
+ <LinearLayout
+ android:orientation="horizontal"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ >
+ <EditText
+ android:layout_height="wrap_content"
+ android:id="@+id/Password"
+ android:selectAllOnFocus="true"
+ android:lines="1"
+ android:hint="Please enter your password"
+ android:inputType="textPassword"
+ android:layout_width="fill_parent"
+ android:layout_gravity="fill_horizontal"
+ android:gravity="fill_horizontal">
+ </EditText>
+ </LinearLayout>
+ <LinearLayout
+ android:layout_height="wrap_content"
+ android:layout_gravity="center_horizontal"
+ android:orientation="horizontal"
+ android:layout_width="fill_parent">
+ <CheckBox
+ android:text="Remember me"
+ android:id="@+id/RememberMe"
+ android:layout_height="wrap_content"
+ android:layout_gravity="center_horizontal"
+ android:layout_width="wrap_content">
+ </CheckBox>
+ </LinearLayout>
+ <LinearLayout
+ android:layout_height="wrap_content"
+ android:layout_gravity="center_horizontal"
+ android:orientation="horizontal"
+ android:layout_width="fill_parent">
+ <Button
+ android:layout_height="wrap_content"
+ android:gravity="center_vertical|center_horizontal"
+ android:text="@string/Login"
+ android:id="@+id/Login"
+ android:clickable="true"
+ android:layout_width="fill_parent"
+ android:layout_weight="1">
+ </Button>
+ <Button
+ android:layout_height="wrap_content"
+ android:gravity="center_vertical|center_horizontal"
+ android:text="@string/Clear"
+ android:id="@+id/Clear"
+ android:clickable="true"
+ android:layout_width="fill_parent"
+ android:layout_weight="1">
+ </Button>
+ </LinearLayout>
+ <LinearLayout
+ android:orientation="horizontal"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ >
+ <Button
+ android:layout_height="wrap_content"
+ android:gravity="center_vertical|center_horizontal"
+ android:text="@string/Register"
+ android:id="@+id/Register"
+ android:clickable="true"
+ android:layout_width="fill_parent"
+ android:layout_weight="1">
+ </Button>
+ <Button
+ android:layout_height="wrap_content"
+ android:gravity="center_vertical|center_horizontal"
+ android:text="@string/Exit"
+ android:id="@+id/Exit"
+ android:clickable="true"
+ android:layout_width="fill_parent"
+ android:layout_weight="1">
+ </Button>
+ </LinearLayout>
+</LinearLayout>
97 res/layout/register.xml
@@ -0,0 +1,97 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_height="fill_parent"
+ android:layout_width="fill_parent"
+ android:layout_gravity="fill_horizontal"
+ android:orientation="vertical">
+ <TextView
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:text="@string/registration"
+ android:id="@+id/Label"
+ />
+ <LinearLayout
+ android:orientation="horizontal"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content">
+ <EditText
+ android:layout_height="wrap_content"
+ android:id="@+id/nUsername"
+ android:selectAllOnFocus="true"
+ android:lines="1"
+ android:hint="Enter your User Name"
+ android:inputType="text"
+ android:layout_width="fill_parent"
+ android:layout_gravity="fill_horizontal"
+ android:gravity="fill_horizontal">
+ </EditText>
+ </LinearLayout>
+ <LinearLayout
+ android:orientation="horizontal"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content">
+ <EditText
+ android:layout_height="wrap_content"
+ android:gravity="left"
+ android:selectAllOnFocus="true"
+ android:lines="1"
+ android:id="@+id/nPassword"
+ android:hint="Please enter a password"
+ android:inputType="textPassword"
+ android:layout_gravity="fill_horizontal"
+ android:layout_width="match_parent">
+ </EditText>
+ </LinearLayout>
+ <LinearLayout
+ android:orientation="horizontal"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content">
+ <EditText
+ android:layout_height="wrap_content"
+ android:gravity="left"
+ android:selectAllOnFocus="true"
+ android:lines="1"
+ android:id="@+id/nConfiPass"
+ android:hint="Please confirm your new password"
+ android:inputType="textPassword"
+ android:layout_gravity="fill_horizontal"
+ android:layout_width="match_parent">
+ </EditText>
+ </LinearLayout>
+ <LinearLayout
+ android:layout_height="wrap_content"
+ android:layout_gravity="center_horizontal"
+ android:orientation="horizontal"
+ android:layout_width="fill_parent">
+ <Button
+ android:layout_height="wrap_content"
+ android:gravity="center_vertical|center_horizontal"
+ android:text="@string/Register"
+ android:id="@+id/nRegister"
+ android:clickable="true"
+ android:layout_width="fill_parent"
+ android:layout_weight="1">
+ </Button>
+ <Button
+ android:layout_height="wrap_content"
+ android:gravity="center_vertical|center_horizontal"
+ android:text="@string/Clear"
+ android:id="@+id/nClear"
+ android:clickable="true"
+ android:layout_width="fill_parent"
+ android:layout_weight="1">
+ </Button>
+ </LinearLayout>
+ <LinearLayout
+ android:layout_height="wrap_content"
+ android:layout_gravity="center_horizontal"
+ android:orientation="horizontal"
+ android:layout_width="fill_parent">
+ <Button
+ android:layout_height="wrap_content"
+ android:gravity="center_vertical|center_horizontal"
+ android:text="@string/Back"
+ android:id="@+id/nBack"
+ android:clickable="true" android:layout_width="fill_parent" android:layout_weight="1"></Button>
+ </LinearLayout>
+</LinearLayout>
11 res/values/strings.xml
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+ <string name="title">Login form</string>
+ <string name="registration">Registration form</string>
+ <string name="app_name">Login App</string>
+ <string name="Exit">Exit</string>
+ <string name="Clear">Clear</string>
+ <string name="Register">Register</string>
+ <string name="Login">Login</string>
+ <string name="Back">Back</string>
+</resources>
136 src/com/android/database/DatabaseAdapter.java
@@ -0,0 +1,136 @@
+package com.android.database;
+
+import android.content.ContentValues;
+import android.content.Context;
+import android.database.Cursor;
+import android.database.SQLException;
+import android.database.sqlite.SQLiteDatabase;
+/**
+ * Adapts the database to deal with the front end.
+ *
+ * @author Andrei
+ *
+ */
+public class DatabaseAdapter {
+ //Table name
+ private static final String LOGIN_TABLE = "user";
+ //Table unique id
+ public static final String COL_ID = "id";
+ //Table username and password columns
+ public static final String COL_USERNAME = "username";
+ public static final String COL_PASSWORD = "password";
+
+ private Context context;
+ private SQLiteDatabase database;
+ private DatabaseHelper dbHelper;
+
+ /**
+ * The adapter constructor.
+ * @param context
+ */
+ public DatabaseAdapter(Context context) {
+ this.context = context;
+ }
+
+ /**
+ * Creates the database helper and gets the database.
+ *
+ * @return
+ * @throws SQLException
+ */
+ public DatabaseAdapter open() throws SQLException {
+ dbHelper = new DatabaseHelper(context);
+ database = dbHelper.getWritableDatabase();
+ return this;
+ }
+
+ /**
+ * Closes the database.
+ */
+ public void close() {
+ dbHelper.close();
+ }
+
+ /**
+ * Creates the user name and password.
+ *
+ * @param username The username.
+ * @param password The password.
+ * @return
+ */
+ public long createUser(String username, String password) {
+ ContentValues initialValues = createUserTableContentValues(username, password);
+ return database.insert(LOGIN_TABLE, null, initialValues);
+ }
+
+ /**
+ * Removes a user's details given an id.
+ *
+ * @param rowId Column id.
+ * @return
+ */
+ public boolean deleteUser(long rowId) {
+ return database.delete(LOGIN_TABLE, COL_ID + "=" + rowId, null) > 0;
+ }
+
+ public boolean updateUserTable(long rowId, String username, String password) {
+ ContentValues updateValues = createUserTableContentValues(username, password);
+ return database.update(LOGIN_TABLE, updateValues, COL_ID + "=" + rowId, null) > 0;
+ }
+
+ /**
+ * Retrieves the details of all the users stored in the login table.
+ *
+ * @return
+ */
+ public Cursor fetchAllUsers() {
+ return database.query(LOGIN_TABLE, new String[] { COL_ID, COL_USERNAME,
+ COL_PASSWORD }, null, null, null, null, null);
+ }
+
+ /**
+ * Retrieves the details of a specific user, given a username and password.
+ *
+ * @return
+ */
+ public Cursor fetchUser(String username, String password) {
+ Cursor myCursor = database.query(LOGIN_TABLE,
+ new String[] { COL_ID, COL_USERNAME, COL_PASSWORD },
+ COL_USERNAME + "='" + username + "' AND " +
+ COL_PASSWORD + "='" + password + "'", null, null, null, null);
+
+ if (myCursor != null) {
+ myCursor.moveToFirst();
+ }
+ return myCursor;
+ }
+
+ /**
+ * Returns the table details given a row id.
+ * @param rowId The table row id.
+ * @return
+ * @throws SQLException
+ */
+ public Cursor fetchUserById(long rowId) throws SQLException {
+ Cursor myCursor = database.query(LOGIN_TABLE,
+ new String[] { COL_ID, COL_USERNAME, COL_PASSWORD },
+ COL_ID + "=" + rowId, null, null, null, null);
+ if (myCursor != null) {
+ myCursor.moveToFirst();
+ }
+ return myCursor;
+ }
+
+ /**
+ * Stores the username and password upon creation of new login details.
+ * @param username The user name.
+ * @param password The password.
+ * @return The entered values.
+ */
+ private ContentValues createUserTableContentValues(String username, String password) {
+ ContentValues values = new ContentValues();
+ values.put(COL_USERNAME, username);
+ values.put(COL_PASSWORD, password);
+ return values;
+ }
+}
45 src/com/android/database/DatabaseHelper.java
@@ -0,0 +1,45 @@
+package com.android.database;
+
+import android.content.Context;
+import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteOpenHelper;
+import android.util.Log;
+/**
+ * This class creates the relation with the SQLite Database Helper
+ * through which queries can be SQL called.
+ * @author Andrei
+ *
+ */
+public class DatabaseHelper extends SQLiteOpenHelper {
+ // The database name and version
+ private static final String DB_NAME = "login";
+ private static final int DB_VERSION = 1;
+ // The database user table
+ private static final String DB_TABLE = "create table user (id integer primary key autoincrement, "
+ + "username text not null, password text not null);";
+ /**
+ * Database Helper constructor.
+ * @param context
+ */
+ public DatabaseHelper(Context context) {
+ super(context, DB_NAME, null, DB_VERSION);
+ }
+ /**
+ * Creates the database tables.
+ */
+ @Override
+ public void onCreate(SQLiteDatabase database) {
+ database.execSQL(DB_TABLE);
+ }
+ /**
+ * Handles the table version and the drop of a table.
+ */
+ @Override
+ public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
+ Log.w(DatabaseHelper.class.getName(),
+ "Upgrading databse from version" + oldVersion + "to "
+ + newVersion + ", which will destroy all old data");
+ database.execSQL("DROP TABLE IF EXISTS user");
+ onCreate(database);
+ }
+}
28 src/com/android/loginapp/Helloworld.java
@@ -0,0 +1,28 @@
+package com.android.loginapp;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.widget.TextView;
+
+/***
+ * Simple Hello World landing.
+ * @author Andrei
+ *
+ */
+public class Helloworld extends Activity {
+
+ public TextView helloTextView;
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.hello);
+ initControls();
+ }
+
+ public void initControls(){
+ helloTextView = (TextView)findViewById(R.id.myTextView);
+ }
+
+}
+
206 src/com/android/loginapp/Register.java
@@ -0,0 +1,206 @@
+package com.android.loginapp;
+
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+import android.app.Activity;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.content.SharedPreferences.Editor;
+import android.database.Cursor;
+import android.os.Bundle;
+import android.view.View;
+import android.widget.Button;
+import android.widget.EditText;
+import android.widget.Toast;
+
+import com.android.database.DatabaseAdapter;
+/**
+ * Handles the user registration activity.
+ *
+ */
+public class Register extends Activity {
+ private EditText newUsername;
+ private EditText newPassword;
+ private EditText newConfiPass;
+ private Button registerButton;
+ private Button clearButton;
+ private Button backButton;
+
+ private DatabaseAdapter dbHelper;
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ SharedPreferences settings = getSharedPreferences(login.MY_PREFS, 0);
+ Editor editor = settings.edit();
+ editor.putLong("uid", 0);
+ editor.commit();
+
+ dbHelper = new DatabaseAdapter(this);
+ dbHelper.open();
+ setContentView(R.layout.register);
+ initControls();
+ }
+
+ /**
+ * Handles interface controls.
+ */
+ private void initControls()
+ {
+ newUsername = (EditText) findViewById(R.id.nUsername);
+ newPassword = (EditText) findViewById(R.id.nPassword);
+ newConfiPass = (EditText) findViewById(R.id.nConfiPass);
+ registerButton = (Button) findViewById(R.id.nRegister);
+ clearButton = (Button) findViewById(R.id.nClear);
+ backButton = (Button) findViewById(R.id.nBack);
+
+ registerButton.setOnClickListener(new Button.OnClickListener() {
+ public void onClick (View v){
+ RegisterMe(v); }});
+
+ clearButton.setOnClickListener(new Button.OnClickListener() {
+ public void onClick (View v){
+ ClearForm(); }});
+
+ backButton.setOnClickListener(new Button.OnClickListener() {
+ public void onClick (View v){
+ BackToLogin(); }});
+ }
+
+ /**
+ * Clears the registration fields.
+ */
+ private void ClearForm()
+ {
+ saveLoggedInUId(0, "", "");
+ newUsername.setText("");
+ newPassword.setText("");
+ newConfiPass.setText("");
+ }
+
+ /**
+ * Takes user back to login.
+ */
+ private void BackToLogin()
+ {
+ finish();
+ }
+
+ /**
+ * Handles the registration process.
+ * @param v
+ */
+ private void RegisterMe(View v)
+ {
+ //Get user details.
+ String username = newUsername.getText().toString();
+ String password = newPassword.getText().toString();
+ String confirmpassword = newConfiPass.getText().toString();
+
+ //Check if all fields have been completed.
+ if (username.equals("") || password.equals("")){
+ Toast.makeText(getApplicationContext(),
+ "Please ensure all fields have been completed.",
+ Toast.LENGTH_SHORT).show();
+ return;
+ }
+
+ //Check password match.
+ if (!password.equals(confirmpassword)) {
+ Toast.makeText(getApplicationContext(),
+ "The password does not match.",
+ Toast.LENGTH_SHORT).show();
+ newPassword.setText("");
+ newConfiPass.setText("");
+ return;
+ }
+
+ //Encrypt password with MD5.
+ password = md5(password);
+
+ //Check database for existing users.
+ Cursor user = dbHelper.fetchUser(username, password);
+ if (user == null) {
+ Toast.makeText(getApplicationContext(), "Database query error",
+ Toast.LENGTH_SHORT).show();
+ } else {
+ startManagingCursor(user);
+
+ //Check for duplicate usernames
+ if (user.getCount() > 0) {
+ Toast.makeText(getApplicationContext(), "The username is already registered",
+ Toast.LENGTH_SHORT).show();
+ stopManagingCursor(user);
+ user.close();
+ return;
+ }
+ stopManagingCursor(user);
+ user.close();
+ user = dbHelper.fetchUser(username, password);
+ if (user == null) {
+ Toast.makeText(getApplicationContext(), "Database query error",
+ Toast.LENGTH_SHORT).show();
+ return;
+ } else {
+ startManagingCursor(user);
+
+ if (user.getCount() > 0) {
+ Toast.makeText(getApplicationContext(), "The username is already registered",
+ Toast.LENGTH_SHORT).show();
+ stopManagingCursor(user);
+ user.close();
+ return;
+ }
+ stopManagingCursor(user);
+ user.close();
+ }
+ //Create the new username.
+ long id = dbHelper.createUser(username, password);
+ if (id > 0) {
+ Toast.makeText(getApplicationContext(), "Your username was created",
+ Toast.LENGTH_SHORT).show();
+ saveLoggedInUId(id, username, newPassword.getText().toString());
+ Intent i = new Intent(v.getContext(), Helloworld.class);
+ startActivity(i);
+
+ finish();
+ } else {
+ Toast.makeText(getApplicationContext(), "Failt to create new username",
+ Toast.LENGTH_SHORT).show();
+ }
+ }
+ }
+
+ private void saveLoggedInUId(long id, String username, String password) {
+ SharedPreferences settings = getSharedPreferences(login.MY_PREFS, 0);
+ Editor editor = settings.edit();
+ editor.putLong("uid", id);
+ editor.putString("username", username);
+ editor.putString("password", password);
+ editor.commit();
+ }
+ /**
+ * Hashes the password with MD5.
+ * @param s
+ * @return
+ */
+ private String md5(String s) {
+ try {
+
+ MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
+ digest.update(s.getBytes());
+ byte messageDigest[] = digest.digest();
+
+
+ StringBuffer hexString = new StringBuffer();
+ for (int i=0; i<messageDigest.length; i++)
+ hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
+ return hexString.toString();
+
+ } catch (NoSuchAlgorithmException e) {
+ return s;
+ }
+ }
+}
223 src/com/android/loginapp/login.java
@@ -0,0 +1,223 @@
+package com.android.loginapp;
+
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+
+import com.android.database.DatabaseAdapter;
+
+import android.app.Activity;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.content.SharedPreferences.Editor;
+import android.database.Cursor;
+import android.os.Bundle;
+import android.view.View;
+import android.widget.Button;
+import android.widget.CheckBox;
+import android.widget.EditText;
+import android.widget.Toast;
+/**
+ * The main application activity which serves as a login page.
+ * @author Andrei
+ *
+ */
+public class login extends Activity {
+
+ public static final String MY_PREFS = "SharedPreferences";
+ private DatabaseAdapter dbHelper;
+ private EditText theUsername;
+ private EditText thePassword;
+ private Button loginButton;
+ private Button registerButton;
+ private Button clearButton;
+ private Button exitButton;
+ private CheckBox rememberDetails;
+
+
+
+ /** Called when the activity is first created. */
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ SharedPreferences mySharedPreferences = getSharedPreferences(MY_PREFS, 0);
+ Editor editor = mySharedPreferences.edit();
+ editor.putLong("uid", 0);
+ editor.commit();
+
+ dbHelper = new DatabaseAdapter(this);
+ dbHelper.open();
+
+ setContentView(R.layout.main);
+ initControls();
+ }
+
+ private void initControls() {
+ //Set the activity layout.
+ theUsername = (EditText) findViewById(R.id.Username);
+ thePassword = (EditText) findViewById(R.id.Password);
+ loginButton = (Button) findViewById(R.id.Login);
+ registerButton = (Button) findViewById(R.id.Register);
+ clearButton = (Button) findViewById(R.id.Clear);
+ exitButton = (Button) findViewById(R.id.Exit);
+ rememberDetails = (CheckBox) findViewById(R.id.RememberMe);
+
+ //Create touch listeners for all buttons.
+ loginButton.setOnClickListener(new Button.OnClickListener(){
+ public void onClick (View v){
+ LogMeIn(v);
+ }
+ });
+
+ registerButton.setOnClickListener(new Button.OnClickListener(){
+ public void onClick (View v){
+ Register(v);
+ }
+ });
+
+ clearButton.setOnClickListener(new Button.OnClickListener(){
+ public void onClick (View v){
+ ClearForm();
+ }
+ });
+
+ exitButton.setOnClickListener(new Button.OnClickListener(){
+ public void onClick (View v){
+ Exit();
+ }
+ });
+ //Create remember password check box listener.
+ rememberDetails.setOnClickListener(new CheckBox.OnClickListener(){
+ public void onClick (View v){
+ RememberMe();
+ }
+ });
+
+ //Handle remember password preferences.
+ SharedPreferences prefs = getSharedPreferences(MY_PREFS, 0);
+ String thisUsername = prefs.getString("username", "");
+ String thisPassword = prefs.getString("password", "");
+ boolean thisRemember = prefs.getBoolean("remember", false);
+ if(thisRemember) {
+ theUsername.setText(thisUsername);
+ thePassword.setText(thisPassword);
+ rememberDetails.setChecked(thisRemember);
+ }
+
+ }
+
+ /**
+ * Deals with Exit option - exits the application.
+ */
+ private void Exit()
+ {
+ finish();
+ }
+
+ /**
+ * Clears the login form.
+ */
+ private void ClearForm() {
+ saveLoggedInUId(0,"","");
+ theUsername.setText("");
+ thePassword.setText("");
+ }
+
+ /**
+ * Handles the remember password option.
+ */
+ private void RememberMe() {
+ boolean thisRemember = rememberDetails.isChecked();
+ SharedPreferences prefs = getSharedPreferences(MY_PREFS, 0);
+ Editor editor = prefs.edit();
+ editor.putBoolean("remember", thisRemember);
+ editor.commit();
+ }
+
+ /**
+ * This method handles the user login process.
+ * @param v
+ */
+ private void LogMeIn(View v) {
+ //Get the username and password
+ String thisUsername = theUsername.getText().toString();
+ String thisPassword = thePassword.getText().toString();
+
+ //Assign the hash to the password
+ thisPassword = md5(thisPassword);
+
+ // Check the existing user name and password database
+ Cursor theUser = dbHelper.fetchUser(thisUsername, thisPassword);
+ if (theUser != null) {
+ startManagingCursor(theUser);
+ if (theUser.getCount() > 0) {
+ saveLoggedInUId(theUser.getLong(theUser.getColumnIndex(DatabaseAdapter.COL_ID)), thisUsername, thePassword.getText().toString());
+ stopManagingCursor(theUser);
+ theUser.close();
+ Intent i = new Intent(v.getContext(), Helloworld.class);
+ startActivity(i);
+ }
+
+ //Returns appropriate message if no match is made
+ else {
+ Toast.makeText(getApplicationContext(),
+ "You have entered an incorrect username or password.",
+ Toast.LENGTH_SHORT).show();
+ saveLoggedInUId(0, "", "");
+ }
+ stopManagingCursor(theUser);
+ theUser.close();
+ }
+
+ else {
+ Toast.makeText(getApplicationContext(),
+ "Database query error",
+ Toast.LENGTH_SHORT).show();
+ }
+ }
+
+ /**
+ * Open the Registration activity.
+ * @param v
+ */
+ private void Register(View v)
+ {
+ Intent i = new Intent(v.getContext(), Register.class);
+ startActivity(i);
+ }
+
+ private void saveLoggedInUId(long id, String username, String password) {
+ SharedPreferences settings = getSharedPreferences(MY_PREFS, 0);
+ Editor myEditor = settings.edit();
+ myEditor.putLong("uid", id);
+ myEditor.putString("username", username);
+ myEditor.putString("password", password);
+ boolean rememberThis = rememberDetails.isChecked();
+ myEditor.putBoolean("rememberThis", rememberThis);
+ myEditor.commit();
+ }
+
+ /**
+ * Deals with the password encryption.
+ * @param s The password.
+ * @return
+ */
+ private String md5(String s) {
+ try {
+ MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
+ digest.update(s.getBytes());
+ byte messageDigest[] = digest.digest();
+
+ StringBuffer hexString = new StringBuffer();
+ for (int i=0; i<messageDigest.length; i++)
+ hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
+
+ return hexString.toString();
+ }
+
+ catch (NoSuchAlgorithmException e) {
+ return s;
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.