Back to project page GroupSmart-Android.
The source code is released under:
Copyright (C) <2014> <Derek Argueta - Hunter Kehoe> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), ...
If you think the Android project GroupSmart-Android 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.dargueta.groupsmart; // ww w. jav a 2 s .c o m import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.content.SharedPreferences; import android.os.AsyncTask; import android.os.Bundle; import android.preference.PreferenceManager; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnKeyListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.google.analytics.tracking.android.EasyTracker; /** * * @author Hunter Kehoe * * Provides the Activity for the user to log in. * */ public class Login extends Activity { Button btnNewUser, btnLogin; EditText etUsername, etPassword; String userLoginURL = ""; ProgressDialog pDialog; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); etUsername = (EditText) findViewById(R.id.etUsername); etPassword = (EditText) findViewById(R.id.etPassword); btnNewUser = (Button) findViewById(R.id.btnNewUser); btnLogin = (Button) findViewById(R.id.btnLogin); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); String username = prefs.getString(Constants.EMAIL_KEY, Constants.EMPTY_STRING); String password = prefs.getString(Constants.PASSWORD_KEY, Constants.EMPTY_STRING); if (username.length() != 0 && password.length() != 0) { userLoginURL = Constants.BASE_URL + "mode=usi&e=" + username + "&p=" + password; new UserUpdate().execute(); } etPassword.setOnKeyListener(new OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) { btnLogin.performClick(); return true; } return false; } }); btnNewUser.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent i = new Intent(getApplicationContext(), Register.class); String username = etUsername.getText().toString(); String password = etPassword.getText().toString(); i.putExtra(Constants.USERNAME_KEY, username); i.putExtra(Constants.PASSWORD_KEY, password); startActivity(i); } }); btnLogin.setOnClickListener(new OnClickListener() { public void onClick(View v) { String username = etUsername.getText().toString(); String password = etPassword.getText().toString(); if (username.length() == 0 || password.length() == 0) { Toast.makeText(getApplicationContext(), "Please enter username/password", Toast.LENGTH_SHORT).show(); return; } else { if (username.indexOf(' ') != -1 || password.indexOf(' ') != -1) { Toast.makeText(getApplicationContext(), "No spaces allowed in username/password", Toast.LENGTH_SHORT).show(); return; } else if (username.indexOf("@") == -1) { Toast.makeText(getApplicationContext(), "Email needs an @ symbol", Toast.LENGTH_SHORT).show(); return; } else { userLoginURL = Constants.BASE_URL + "mode=usi&e=" + username + "&p=" + password; } } new UserLogin().execute(); } }); } /** * * @author Hunter Kehoe * * Asynchronously log the user in on the remote server. * */ private class UserLogin extends AsyncTask<Void, Void, Void> { boolean successful = false; protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(Login.this); pDialog.setMessage(getString(R.string.pleaseWait)); pDialog.setCancelable(false); pDialog.show(); } protected Void doInBackground(Void... arg0) { ServiceHandler sh = new ServiceHandler(); String jsonStr = sh.makeServiceCall(userLoginURL, ServiceHandler.GET); if (!jsonStr.equals("LOGIN FAILED")) { successful = true; SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences.Editor editor = prefs.edit(); int id=0, privacy=0, adminMessage=0, active=0; String password="", firstName="", lastName="", email="", major="", groups="", course1="", course2="", course3="", course4="", course5="", course6=""; try { JSONObject user = new JSONObject(jsonStr); id = user.getInt(Constants.ID_KEY); password = etPassword.getText().toString(); firstName = user.getString(Constants.FIRST_NAME_KEY); lastName = user.getString(Constants.LAST_NAME_KEY); email = user.getString(Constants.EMAIL_KEY); major = user.getString(Constants.MAJOR_KEY); groups = user.getString(Constants.GROUPS_KEY); course1 = user.getString(Constants.COURSE_ONE_KEY); course2 = user.getString(Constants.COURSE_TWO_KEY); course3 = user.getString(Constants.COURSE_THREE_KEY); course4 = user.getString(Constants.COURSE_FOUR_KEY); course5 = user.getString(Constants.COURSE_FIVE_KEY); course6 = user.getString(Constants.COURSE_SIX_KEY); privacy = user.getInt(Constants.PRIVACY_KEY); adminMessage = user.getInt(Constants.ADMIN_MESSAGE_KEY); active = user.getInt(Constants.ACTIVE_KEY); } catch (JSONException e) { e.printStackTrace(); } editor.putInt(Constants.ID_KEY, id); editor.putString(Constants.PASSWORD_KEY, password); editor.putString(Constants.FIRST_NAME_KEY, firstName); editor.putString(Constants.LAST_NAME_KEY, lastName); editor.putString(Constants.EMAIL_KEY, email); editor.putString(Constants.MAJOR_KEY, major); editor.putString(Constants.GROUPS_KEY, groups); editor.putString(Constants.COURSE_ONE_KEY, course1); editor.putString(Constants.COURSE_TWO_KEY, course2); editor.putString(Constants.COURSE_THREE_KEY, course3); editor.putString(Constants.COURSE_FOUR_KEY, course4); editor.putString(Constants.COURSE_FIVE_KEY, course5); editor.putString(Constants.COURSE_SIX_KEY, course6); editor.putInt(Constants.PRIVACY_KEY, privacy); editor.putInt(Constants.ADMIN_MESSAGE_KEY, adminMessage); editor.putInt(Constants.ACTIVE_KEY, active); editor.commit(); } return null; } protected void onPostExecute(Void result) { super.onPostExecute(result); if(pDialog.isShowing()) { pDialog.dismiss(); } if (successful) { Intent i = new Intent(getApplicationContext(), Main.class); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(i); finish(); } else { Toast.makeText(getApplicationContext(), "Invalid email/password combination", Toast.LENGTH_SHORT).show(); } } } private class UserUpdate extends AsyncTask<Void, Void, Void> { boolean successful = false; protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(Login.this); pDialog.setMessage(getString(R.string.pleaseWait)); pDialog.setCancelable(false); pDialog.show(); } protected Void doInBackground(Void... arg0) { ServiceHandler sh = new ServiceHandler(); String jsonStr = sh.makeServiceCall(userLoginURL, ServiceHandler.GET); if (!jsonStr.equals("LOGIN FAILED")) { successful = true; } return null; } protected void onPostExecute(Void result) { super.onPostExecute(result); if(pDialog.isShowing()) { pDialog.dismiss(); } if (successful) { Intent i = new Intent(getApplicationContext(), Main.class); startActivity(i); finish(); } else { Toast.makeText(getApplicationContext(), "Password changed. Login again", Toast.LENGTH_SHORT).show(); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); if(prefs.getString(Constants.EMAIL_KEY, Constants.EMPTY_STRING).length() != 0) { etUsername.setText(prefs.getString(Constants.EMAIL_KEY, Constants.EMPTY_STRING)); } } } } public void onStart() { super.onStart(); EasyTracker.getInstance(this).activityStart(this); } public void onStop() { super.onStop(); EasyTracker.getInstance(this).activityStop(this); } }