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; /*from w w w. j a v a 2s . co 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.CheckBox; import android.widget.EditText; import android.widget.Toast; import com.google.analytics.tracking.android.EasyTracker; /** * * @author Hunter Kehoe * * Provides the Activity where the user can register an account. * */ public class Register extends Activity { private ProgressDialog pDialog; EditText etFirstName, etLastName, etEmail, etPassword, etPasswordCheck; Button btnMajor, btnCourse1, btnCourse2, btnCourse3, btnCourse4, btnCourse5, btnCourse6, btnRegister; CheckBox cbPrivacy; String newUserURL = ""; String checkUserURL = ""; String userLoginURL = ""; boolean userAvailable = false; boolean userAdded = false; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.register); btnMajor = (Button) findViewById(R.id.btnMajor); btnCourse1 = (Button) findViewById(R.id.btnCourse1); btnCourse2 = (Button) findViewById(R.id.btnCourse2); btnCourse3 = (Button) findViewById(R.id.btnCourse3); btnCourse4 = (Button) findViewById(R.id.btnCourse4); btnCourse5 = (Button) findViewById(R.id.btnCourse5); btnCourse6 = (Button) findViewById(R.id.btnCourse6); btnRegister = (Button) findViewById(R.id.btnRegister); etFirstName = (EditText) findViewById(R.id.etFirstName); etLastName = (EditText) findViewById(R.id.etLastName); etEmail = (EditText) findViewById(R.id.etEmail); etPassword = (EditText) findViewById(R.id.etPassword); etPasswordCheck = (EditText) findViewById(R.id.etPasswordCheck); cbPrivacy = (CheckBox) findViewById(R.id.cbPrivacy); Intent i = getIntent(); String username = i.getStringExtra(Constants.USERNAME_KEY); String password = i.getStringExtra(Constants.PASSWORD_KEY); if(!username.equals(Constants.EMPTY_STRING)) { etEmail.setText(username); } if(!password.equals(Constants.EMPTY_STRING)) { etPassword.setText(password); } etFirstName.setOnKeyListener(new OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) { etLastName.requestFocus(); return true; } return false; } }); etPasswordCheck.setOnKeyListener(new OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) { btnMajor.performClick(); return true; } return false; } }); btnMajor.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent i = new Intent(getApplicationContext(), MajorSelect.class); startActivityForResult(i,7); } }); btnCourse1.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent i = new Intent(getApplicationContext(), CourseSelect.class); startActivityForResult(i, 1); } }); btnCourse2.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent i = new Intent(getApplicationContext(), CourseSelect.class); startActivityForResult(i, 2); } }); btnCourse3.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent i = new Intent(getApplicationContext(), CourseSelect.class); startActivityForResult(i, 3); } }); btnCourse4.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent i = new Intent(getApplicationContext(), CourseSelect.class); startActivityForResult(i, 4); } }); btnCourse5.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent i = new Intent(getApplicationContext(), CourseSelect.class); startActivityForResult(i, 5); } }); btnCourse6.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent i = new Intent(getApplicationContext(), CourseSelect.class); startActivityForResult(i, 6); } }); btnRegister.setOnClickListener(new OnClickListener() { public void onClick(View v) { String firstName = etFirstName.getText().toString(); String lastName = etLastName.getText().toString(); String email = etEmail.getText().toString(); String password = etPassword.getText().toString(); String passwordCheck = etPasswordCheck.getText().toString(); String major = btnMajor.getText().toString(); String course1 = btnCourse1.getText().toString(); String course2 = btnCourse2.getText().toString(); String course3 = btnCourse3.getText().toString(); String course4 = btnCourse4.getText().toString(); String course5 = btnCourse5.getText().toString(); String course6 = btnCourse6.getText().toString(); int privacy = 0; if(cbPrivacy.isChecked()) { privacy = 1; } if(firstName.length() == 0 || lastName.length() == 0) { Toast.makeText(getApplicationContext(), "Please fill both name fields", Toast.LENGTH_SHORT).show(); return; } if (email.length() == 0) { Toast.makeText(getApplicationContext(), R.string.provideEmail, Toast.LENGTH_SHORT).show(); return; } if (password.length() == 0) { Toast.makeText(getApplicationContext(), R.string.providePassword, Toast.LENGTH_SHORT).show(); return; } if (!password.equals(passwordCheck)) { Toast.makeText(getApplicationContext(), "Passwords don't match", Toast.LENGTH_SHORT).show(); return; } if (major.equals(R.string.major)) { Toast.makeText(getApplicationContext(), R.string.selectMajor, Toast.LENGTH_SHORT).show(); return; } if (course1.equals(R.string.course1) && course2.equals(R.string.course2) && course3.equals(R.string.course3) && course4.equals(R.string.course4) && course5.equals(R.string.course5) && course6.equals(R.string.course6)) { Toast.makeText(getApplicationContext(), "Please add at least 1 course", Toast.LENGTH_SHORT).show(); return; } if (course1.equals(R.string.course1)) { course1="NULL"; } if (course2.equals(R.string.course2)) { course2="NULL"; } if (course3.equals(R.string.course3)) { course3="NULL"; } if (course4.equals(R.string.course4)) { course4="NULL"; } if (course5.equals(R.string.course5)) { course5="NULL"; } if (course6.equals(R.string.course6)) { course6="NULL"; } checkUserURL = Constants.BASE_URL+"mode=cdu&e="+email; newUserURL = Constants.BASE_URL+"mode=nu&p="+password+"&fn="+firstName+"&ln="+lastName+"&e="+email+"&m="+major+"&c1="+course1+"&c2="+course2+"&c3="+course3+"&c4="+course4+"&c5="+course5+"&c6="+course6+"&pr="+privacy; newUserURL = newUserURL.replaceAll(Constants.NORMAL_SPACE, Constants.URL_SPACE); userLoginURL = Constants.BASE_URL + "mode=usi&e=" + email + "&p=" + password; new CheckUser().execute(); } }); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch(requestCode){ case 1: if(resultCode == Activity.RESULT_OK) { String course = data.getStringExtra(Constants.NAME_KEY); btnCourse1.setText(course); } else { //Toast.makeText(this,"Cancelled", Toast.LENGTH_SHORT).show(); } break; case 2: if(resultCode == Activity.RESULT_OK) { String course = data.getStringExtra(Constants.NAME_KEY); btnCourse2.setText(course); } else { //Toast.makeText(this,"Cancelled", Toast.LENGTH_SHORT).show(); } break; case 3: if(resultCode == Activity.RESULT_OK) { String course = data.getStringExtra(Constants.NAME_KEY); btnCourse3.setText(course); } else { //Toast.makeText(this,"Cancelled", Toast.LENGTH_SHORT).show(); } break; case 4: if(resultCode == Activity.RESULT_OK) { String course = data.getStringExtra(Constants.NAME_KEY); btnCourse4.setText(course); } else { //Toast.makeText(this,"Cancelled", Toast.LENGTH_SHORT).show(); } break; case 5: if(resultCode == Activity.RESULT_OK) { String course = data.getStringExtra(Constants.NAME_KEY); btnCourse5.setText(course); } else { //Toast.makeText(this,"Cancelled", Toast.LENGTH_SHORT).show(); } break; case 6: if(resultCode == Activity.RESULT_OK) { String course = data.getStringExtra(Constants.NAME_KEY); btnCourse6.setText(course); } else { //Toast.makeText(this,"Cancelled", Toast.LENGTH_SHORT).show(); } break; case 7: if(resultCode == Activity.RESULT_OK) { String course = data.getStringExtra(Constants.NAME_KEY); btnMajor.setText(course); } else { //Toast.makeText(this,"Cancelled", Toast.LENGTH_SHORT).show(); } break; } } /** * * @author Hunter Kehoe * * Makes an asynchronous call to the server to check if the user's email and other information * is available to avoid duplicate users. * */ private class CheckUser extends AsyncTask<Void, Void, Void> { protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(Register.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(checkUserURL, ServiceHandler.GET); if (jsonStr.equals("available")) { userAvailable = true; } return null; } protected void onPostExecute(Void result) { super.onPostExecute(result); if (userAvailable) { new NewUser().execute(); } else { if(pDialog.isShowing()) { pDialog.dismiss(); } Toast.makeText(getApplicationContext(), R.string.emailInUse, Toast.LENGTH_SHORT).show(); } } } /** * * @author Hunter Kehoe * * Makes an asynchronous call to the server to add the new user. * */ private class NewUser extends AsyncTask<Void, Void, Void> { protected void onPreExecute() { super.onPreExecute(); } protected Void doInBackground(Void... arg0) { ServiceHandler sh = new ServiceHandler(); String jsonStr = sh.makeServiceCall(newUserURL, ServiceHandler.GET); //Log.d("Response: ", "> " + jsonStr); if (jsonStr.contains("Successfully")) { userAdded = true; } return null; } protected void onPostExecute(Void result) { super.onPostExecute(result); if(pDialog.isShowing()) { pDialog.dismiss(); } if (userAdded) { new UserLogin().execute(); } } } /** * * @author Hunter Kehoe * * Makes an asynchronous call to now login the new user. * * TODO Integrate this code with the user registration to make it one fluid operation. * */ private class UserLogin extends AsyncTask<Void, Void, Void> { boolean successful = false; protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(Register.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); //Log.d("Response: ", "> " + jsonStr); 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) { Toast.makeText(getApplicationContext(), "User Added", Toast.LENGTH_SHORT).show(); Intent i = new Intent(getApplicationContext(), Main.class); startActivity(i); finish(); } } } // Google Analytics public void onStart() { super.onStart(); EasyTracker.getInstance(this).activityStart(this); } public void onStop() { super.onStop(); EasyTracker.getInstance(this).activityStop(this); } }