Android Open Source - GroupSmart-Android Update Profile






From Project

Back to project page GroupSmart-Android.

License

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.

Java Source Code

package com.dargueta.groupsmart;
/* ww  w.ja v a 2s .  c  om*/
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.TextView;
import android.widget.Toast;

import com.google.analytics.tracking.android.EasyTracker;

/**
 * 
 * @author Hunter Kehoe
 * 
 * Provides the Activity for the user to update his or her profile.
 * 
 * TODO this class needs to be reviewed. It seems like there is a lot of unnecessary code here.
 *
 */
public class UpdateProfile extends Activity {
  
  EditText etFirstName, etLastName, etEmail, etPassword, etPasswordCheck;
  Button btnMajor, btnCourse1, btnCourse2, btnCourse3, btnCourse4, btnCourse5, btnCourse6, btnRegister;
  CheckBox cbPrivacy;
  
  String groups = "";
  int id;
  
  String checkUserURL = "";
  String userLoginURL = "";
  String updateUserURL = "";
  
  public 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);
    
    etPassword.setVisibility(View.GONE);
    etPasswordCheck.setVisibility(View.GONE);
    
    ((TextView) findViewById(R.id.tvRegister)).setText(R.string.updateProfile);
    btnRegister.setText(R.string.saveChanges);
    
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    
    if (!prefs.getString(Constants.MAJOR_KEY, Constants.EMPTY_STRING).equals(Constants.EMPTY_STRING)) {
      
      btnMajor.setText(prefs.getString(Constants.MAJOR_KEY, Constants.EMPTY_STRING));
    }
    
    if (!prefs.getString(Constants.COURSE_ONE_KEY, "NULL").equals("NULL")) {
      
      btnCourse1.setText(prefs.getString(Constants.COURSE_ONE_KEY,""));
    }
    
    if (!prefs.getString(Constants.COURSE_TWO_KEY, "NULL").equals("NULL")) {
      
      btnCourse2.setText(prefs.getString(Constants.COURSE_TWO_KEY,""));
    }
    
    if (!prefs.getString(Constants.COURSE_THREE_KEY, "NULL").equals("NULL")) {
      
      btnCourse3.setText(prefs.getString(Constants.COURSE_THREE_KEY,""));
    }
    
    if (!prefs.getString(Constants.COURSE_FOUR_KEY, "NULL").equals("NULL")) {
      
      btnCourse4.setText(prefs.getString(Constants.COURSE_FOUR_KEY,""));
    }
    
    if (!prefs.getString(Constants.COURSE_FIVE_KEY, "NULL").equals("NULL")) {
      
      btnCourse5.setText(prefs.getString(Constants.COURSE_FIVE_KEY,""));
    }
    
    if (!prefs.getString(Constants.COURSE_SIX_KEY, "NULL").equals("NULL")) {
      
      btnCourse6.setText(prefs.getString(Constants.COURSE_SIX_KEY,""));
    }
    
    if (!prefs.getString(Constants.FIRST_NAME_KEY, Constants.EMPTY_STRING).equals(Constants.EMPTY_STRING)) {
      etFirstName.setText(prefs.getString(Constants.FIRST_NAME_KEY,""));
    }
    
    if (!prefs.getString(Constants.LAST_NAME_KEY, Constants.EMPTY_STRING).equals(Constants.EMPTY_STRING)) {
      etLastName.setText(prefs.getString(Constants.LAST_NAME_KEY,""));
    }
    
    if (!prefs.getString(Constants.EMAIL_KEY, Constants.EMPTY_STRING).equals(Constants.EMPTY_STRING)) {
      etEmail.setText(prefs.getString(Constants.EMAIL_KEY, Constants.EMPTY_STRING));
    }
    
    if (prefs.getInt(Constants.PRIVACY_KEY, -10) != -10) {
      
      if (prefs.getInt(Constants.PRIVACY_KEY, -10) == 1) {
        
        cbPrivacy.setChecked(true);
      } else {
        
        cbPrivacy.setChecked(false);
      }
    }
    if (!prefs.getString(Constants.GROUPS_KEY, Constants.EMPTY_STRING).equals(Constants.EMPTY_STRING)) {
      
      groups = prefs.getString(Constants.GROUPS_KEY, Constants.EMPTY_STRING);
    }
    if (prefs.getInt(Constants.ID_KEY, -1) != -1) {
      id = prefs.getInt(Constants.ID_KEY, -1);
    }
    
    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;
        }
    });
    
    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 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 (major.equals(R.string.major)) {
          
          Toast.makeText(getApplicationContext(),
                   "Please select a major",
                   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;
        updateUserURL = Constants.BASE_URL + "mode=uu&i=" + id + "&fn=" + firstName + "&ln=" + lastName + "&e=" + email + "&m=" + major + "&g=" + groups + "&c1=" + course1 + "&c2=" + course2 + "&c3=" + course3 + "&c4=" + course4 + "&c5=" + course5 + "&pr=" + privacy;
        updateUserURL = updateUserURL.replaceAll(Constants.NORMAL_SPACE, Constants.URL_SPACE);
        
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        String oldEmail = prefs.getString(Constants.EMAIL_KEY, Constants.EMPTY_STRING);
        
        if (oldEmail.length() != 0 && !oldEmail.equals(email)) {
          
          new CheckUser().execute();
        } else {
          
          new NewUser().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;
      
    }
  }

  private class NewUser extends AsyncTask<Void, Void, Void> {
    
    boolean userUpdated = false;
    
    ProgressDialog pDialog;
    
    protected void onPreExecute() {
      
      super.onPreExecute();
      pDialog = new ProgressDialog(UpdateProfile.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(updateUserURL, ServiceHandler.GET);
      //Log.d("Response: ", "> " + jsonStr);
      
      if (jsonStr.contains("Successfully")) {
        
        userUpdated = true;
      }
          
      return null;
    }
    
    protected void onPostExecute(Void result) {
      
      super.onPostExecute(result);
      
      if(pDialog.isShowing()) {
        
        pDialog.dismiss();
      }
      
      if (userUpdated) {
        
        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);
        userLoginURL = Constants.BASE_URL+"mode=usi&e="+username+"&p="+password;
        new UserLogin().execute();
      }
    }
  }
  
  private class CheckUser extends AsyncTask<Void, Void, Void> {
    
    boolean userAvailable = false;
    
    ProgressDialog pDialog;
    
    protected void onPreExecute() {
      
      super.onPreExecute();
      pDialog = new ProgressDialog(UpdateProfile.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(pDialog.isShowing()) {
        
        pDialog.dismiss();
      }
      
      if (userAvailable) {
        
        new NewUser().execute();
      } else {
        
        Toast.makeText(getApplicationContext(), "Email already in use", Toast.LENGTH_SHORT).show();
      }
    }
  }
  
  private class UserLogin extends AsyncTask<Void, Void, Void> {  
    
    boolean successful = false;
    
    ProgressDialog pDialog;
    
    protected void onPreExecute() {
      
      super.onPreExecute();
      pDialog = new ProgressDialog(UpdateProfile.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 firstName="",
             lastName="",
             email="",
             major="",
             groups="",
             course1="",
             course2="",
             course3="",
             course4="",
             course5="";
        
        try {
          
          JSONObject user = new JSONObject(jsonStr);
          
          id = user.getInt(Constants.ID_KEY);
          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);
          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.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.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(),
                 "Profile Updated",
                 Toast.LENGTH_SHORT).show();
        finish();
      } else {
        
        Toast.makeText(getApplicationContext(),
                 R.string.somethingWrong,
                 Toast.LENGTH_SHORT).show();
      }
    }
  }
  
  // Google Analytics
  public void onStart() {
    
    super.onStart();
    EasyTracker.getInstance(this).activityStart(this);
  }
  
  public void onStop() {
    
    super.onStop();
    EasyTracker.getInstance(this).activityStop(this);
  }
}




Java Source Code List

com.dargueta.groupsmart.ChangePassword.java
com.dargueta.groupsmart.Constants.java
com.dargueta.groupsmart.CourseSelect.java
com.dargueta.groupsmart.CreateGroup.java
com.dargueta.groupsmart.Feedback.java
com.dargueta.groupsmart.FindGroup.java
com.dargueta.groupsmart.GroupInfo.java
com.dargueta.groupsmart.Login.java
com.dargueta.groupsmart.Main.java
com.dargueta.groupsmart.MajorSelect.java
com.dargueta.groupsmart.Profile.java
com.dargueta.groupsmart.Register.java
com.dargueta.groupsmart.ServiceHandler.java
com.dargueta.groupsmart.Settings.java
com.dargueta.groupsmart.UpdateGroup.java
com.dargueta.groupsmart.UpdateProfile.java