Android Open Source - GroupSmart-Android Update Group






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;
/*from w  ww  .java2 s . c  o  m*/
import org.json.JSONArray;
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.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

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

/**
 * 
 * @author Hunter Kehoe
 * 
 * Provides an Activity for the user to update the information about this group.
 *
 */
public class UpdateGroup extends Activity{
  
  EditText groupName, teacherName, groupDescription, place, notes;
  CheckBox monday, tuesday, wednesday, thursday, friday, saturday, sunday;
  Button btnCourse, btnUpdateGroup, btnHalfOfDay;
  
  Spinner sHour, sMinute;
  
  Button btnHome, btnProfile, btnCreateGroup, btnSettings;
  
  String groupId = "-1";
  
  String getGroupURL = "";
  String updateGroupURL = "";
  String updateNotesURL = "";
  
  String memberList = "";
  String school = "";
  
  String note = "";
  
  public void onCreate(Bundle savedInstanceState) {
    
    super.onCreate(savedInstanceState);
    setContentView(R.layout.update_group);
    
    TextView tvGroupLabel = (TextView) findViewById(R.id.tvGroupLabel);
    tvGroupLabel.setText("Update Group");
    
    groupId = getIntent().getStringExtra("gid");
    
    btnProfile = (Button) findViewById(R.id.btnProfile);
    btnSettings = (Button) findViewById(R.id.btnSettings);
    btnCreateGroup = (Button) findViewById(R.id.btnNewGroup);
    btnHome = (Button) findViewById(R.id.btnHome);
    
    btnProfile.setOnClickListener(new OnClickListener() {
      
      public void onClick(View v) {
        
        Intent i = new Intent(getApplicationContext(), Profile.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        startActivity(i);
        finish();
        overridePendingTransition(0, 0);
      }
    });
    
    btnSettings.setOnClickListener(new OnClickListener() {
      
      public void onClick(View v) {
        
        Intent i = new Intent(getApplicationContext(), Settings.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        startActivity(i);
        finish();
        overridePendingTransition(0, 0);
      }
    });
    
    btnCreateGroup.setOnClickListener(new OnClickListener() {
      
      public void onClick(View v) {
        
        Intent i = new Intent(getApplicationContext(), CreateGroup.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        startActivity(i);
        finish();
        overridePendingTransition(0, 0);
      }
    });
    
    btnHome.setOnClickListener(new OnClickListener() {
      
      public void onClick(View v) {
        
        Intent i = new Intent(getApplicationContext(), Main.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        startActivity(i);
        finish();
        overridePendingTransition(0, 0);
      }
    });
    
    groupName = (EditText) findViewById(R.id.etGroupName);
    teacherName = (EditText) findViewById(R.id.etTeacherName);
    groupDescription = (EditText) findViewById(R.id.etGroupDescription);
    place = (EditText) findViewById(R.id.etPlace);
    notes = (EditText) findViewById(R.id.etNotes);
    
    sHour = (Spinner) findViewById(R.id.sHour);
    sMinute = (Spinner) findViewById(R.id.sMinute);
    
    String[] hours = {"1","2","3","4","5","6","7","8","9","10","11","12"};
    String[] minutes = {"00", "15", "30", "45"};
    
    ArrayAdapter<String> hourAdapter = new ArrayAdapter<String>(this, R.layout.spinner_item, hours);
    ArrayAdapter<String> minuteAdapter = new ArrayAdapter<String>(this, R.layout.spinner_item, minutes);
    
    hourAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    minuteAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    sHour.setAdapter(hourAdapter);
    sMinute.setAdapter(minuteAdapter);
    
    btnHalfOfDay = (Button) findViewById(R.id.btnHalfOfDay);
    btnHalfOfDay.setOnClickListener(new OnClickListener() {
      
      public void onClick(View v) {
        
        if (btnHalfOfDay.getText().toString().equals(R.string.morning)) {
          
          btnHalfOfDay.setText(R.string.afternoon);
        } else {
          
          btnHalfOfDay.setText(R.string.morning);
        }
      }
    });
    
    monday = (CheckBox) findViewById(R.id.cbM);
    tuesday = (CheckBox) findViewById(R.id.cbT);
    wednesday = (CheckBox) findViewById(R.id.cbW);
    thursday = (CheckBox) findViewById(R.id.cbTh);
    friday = (CheckBox) findViewById(R.id.cbF);
    saturday = (CheckBox) findViewById(R.id.cbS);
    sunday = (CheckBox) findViewById(R.id.cbSu);
    
    btnCourse = (Button) findViewById(R.id.btnGroupClass);
    btnUpdateGroup = (Button) findViewById(R.id.btnUpdateGroup);
    
    
    getGroupURL = Constants.BASE_URL+"mode=ggifi&id="+groupId;
    new GetGroup().execute();
    
    btnCourse.setOnClickListener(new OnClickListener() {
      
      public void onClick(View v) {
        
        Intent i = new Intent(getApplicationContext(), CourseSelect.class);
        startActivityForResult(i, 1);
      }
    });
    
    btnUpdateGroup.setOnClickListener(new OnClickListener() {
      
      public void onClick(View v) {
        
        String name = groupName.getText().toString();
        String teacher = teacherName.getText().toString();
        String description = groupDescription.getText().toString();
        String location = place.getText().toString();
        String course = btnCourse.getText().toString();
        note = notes.getText().toString();
        
        String hour = sHour.getSelectedItem().toString();
        String minute = sMinute.getSelectedItem().toString();
        String dayHalf = btnHalfOfDay.getText().toString();
        
        
        String fullTime = hour + ":" + minute + Constants.NORMAL_SPACE + dayHalf;
        
        if(name.length() == 0) {
          
          Toast.makeText(getApplicationContext(), "Fill in group name", Toast.LENGTH_SHORT).show();
          return;
        }
        
        if(teacher.length() == 0) {
          
          Toast.makeText(getApplicationContext(), "Fill in teacher name", Toast.LENGTH_SHORT).show();
          return;
        }
        
        if(place.length() == 0) {
          
          Toast.makeText(getApplicationContext(), "Fill in meeting place", Toast.LENGTH_SHORT).show();
          return;
        }
        
        if (!monday.isChecked() &&
          !tuesday.isChecked() &&
          !wednesday.isChecked() &&
          !thursday.isChecked() &&
          !friday.isChecked() &&
          !saturday.isChecked() &&
          !sunday.isChecked()) {
          
          Toast.makeText(getApplicationContext(), "Fill in at least 1 meet day", Toast.LENGTH_SHORT).show();
          return;
        }
        
        if (course.equals("Course Name")) {
          
          Toast.makeText(getApplicationContext(), "Select a course", Toast.LENGTH_SHORT).show();
          return;
        }
        
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        String userId = String.valueOf(prefs.getInt(Constants.ID_KEY, -1));
        
        if(userId.equals("")) {
          
          Toast.makeText(getApplicationContext(), "Please login in", Toast.LENGTH_SHORT).show();
          return;
        }
        
        String day = "";
        
        if (sunday.isChecked()) {
          day += "Su;";
        }
        
        if (monday.isChecked()) {
          day += "M;";
        }
        
        if (tuesday.isChecked()) {
          day += "T;";
        }
        
        if (wednesday.isChecked()) {
          day += "W;";
        }
        
        if (thursday.isChecked()) {
          day += "Th;";
        }
        
        if (friday.isChecked()) {
          day += "F;";
        }
        
        if (saturday.isChecked()) {
          day += "S;";
        }
        
        updateGroupURL = Constants.BASE_URL+"mode=ug&gid="+groupId+"&n="+name+"&c="+course+"&t="+teacher+"&d="+day+"&ti="+fullTime+"&l="+location+"&sl=1&lead="+userId+"&o=1&desc="+description+"&s=1&ml=&ot=0";
        updateGroupURL = updateGroupURL.replaceAll(Constants.NORMAL_SPACE, Constants.URL_SPACE);
        
        new Update().execute();
      }
    });
    
  }
  
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
    if (requestCode == 1) {
      
      if(resultCode == Activity.RESULT_OK) {
        
        String course = data.getStringExtra(Constants.NAME_KEY);
        btnCourse.setText(course);
      } else {
        
        //Toast.makeText(this,"Cancelled", Toast.LENGTH_SHORT).show();
      }
    } 
  }
  
  /**
   * 
   * @author Hunter Kehoe
   * 
   * Makes an asynchronous call to update the information about this group.
   *
   */
  private class Update extends AsyncTask<Void, Void, Void> {
    
    boolean successful = false;
    ProgressDialog pDialog;
    
    protected void onPreExecute() {
      
      super.onPreExecute();
      pDialog = new ProgressDialog(UpdateGroup.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(updateGroupURL, ServiceHandler.GET);
      
      if (jsonStr.contains("Successfully")) {
        
        successful = true;
      }
          
      return null;
    }
    
    protected void onPostExecute(Void result) {
      
      super.onPostExecute(result);
      if(pDialog.isShowing()) {
        
        pDialog.dismiss();
      }
      
      if (successful) {
        
        if (note.length() > 0) {
          
          updateNotesURL = Constants.BASE_URL+"mode=un&no="+note+"&gid="+groupId;
          updateNotesURL = updateNotesURL.replaceAll(Constants.NORMAL_SPACE, Constants.URL_SPACE);
          new UpdateNotes().execute();
          
        } else {
          
          Toast.makeText(getApplicationContext(), "Group updated", Toast.LENGTH_SHORT).show();
          finish();
        }
        
      } else {
        
        Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_SHORT).show();
      }
    }
  }
  
  /**
   * 
   * @author Hunter Kehoe
   * 
   * Makes an asynchronous call to update the notes of this group.
   *
   */
  private class UpdateNotes extends AsyncTask<Void, Void, Void> {
    
    boolean successful = false;
    ProgressDialog pDialog;
    
    protected void onPreExecute() {
      
      super.onPreExecute();
      pDialog = new ProgressDialog(UpdateGroup.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(updateNotesURL, ServiceHandler.GET);
      
      if (jsonStr.contains("Successfully")) {
        
        successful = true;
      }
          
      return null;
    }
    
    protected void onPostExecute(Void result) {
      
      super.onPostExecute(result);
      
      if(pDialog.isShowing()) {
        
        pDialog.dismiss();
      }
      
      if (successful) {
        
        Toast.makeText(getApplicationContext(), "Group updated", Toast.LENGTH_SHORT).show();
        finish();
        
      } else {
        
        Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_SHORT).show();
      }
    }
  }
  
  /**
   * 
   * @author Hunter Kehoe
   * 
   * Makes an asynchronous call to get the information of this group from the server.
   *
   */
  private class GetGroup extends AsyncTask<Void, Void, Void> {
    
    boolean successful = false;
    ProgressDialog pDialog;
    
    JSONArray groups = null;
    
    String oldName, oldTeacher, oldDescription, oldCourse, oldTime, oldDay, oldPlace, oldNotes;
    
    protected void onPreExecute() {
      
      super.onPreExecute();
      pDialog = new ProgressDialog(UpdateGroup.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(getGroupURL, ServiceHandler.GET);
      
      if (!jsonStr.equals("Failed Execution")) {    
        
        successful = true;
        
        try {
          
          JSONObject jsonObj = new JSONObject(jsonStr);
          
          groups = jsonObj.getJSONArray(Constants.GROUPS_KEY);
          
          for(int i = 0; i < groups.length(); i++) {
            
            JSONObject u = groups.getJSONObject(i);
            oldName = u.getString(Constants.NAME_KEY);
            oldCourse = u.getString(Constants.CLASS_KEY);
            oldTeacher = u.getString(Constants.TEACHER_KEY);
            oldDay = u.getString(Constants.DAY_KEY);
            oldTime = u.getString(Constants.TIME_KEY);
            oldPlace = u.getString(Constants.LOCATION_KEY);
            oldNotes = u.getString(Constants.NOTES_KEY);
            oldDescription = u.getString(Constants.DESCRIPTION_KEY);
            school = String.valueOf(u.getInt(Constants.SCHOOL_KEY));
            memberList = u.getString(Constants.MEMBER_LIST_KEY);
          }
        } catch (JSONException e) {
          
          //Log.d("Error: ", "catch in JSON");
          e.printStackTrace();
        }
        
      }
          
      return null;
    }
    
    protected void onPostExecute(Void result) {
      
      super.onPostExecute(result);
      
      if(pDialog.isShowing()) {
        
        pDialog.dismiss();
      }
      
      if (successful) {
        
        groupName.setText(oldName);
        teacherName.setText(oldTeacher);
        groupDescription.setText(oldDescription);
        place.setText(oldPlace);
        notes.setText(oldNotes);
        btnCourse.setText(oldCourse);
        
        if (oldDay.contains("M;")) {
          
          monday.setChecked(true);
        }
        
        if (oldDay.contains("T;")) {
          
          tuesday.setChecked(true);
        }
        
        if (oldDay.contains("W;")) {
          
          wednesday.setChecked(true);
        }
        
        if (oldDay.contains("Th;")) {
          
          thursday.setChecked(true);
        }
        
        if (oldDay.contains("F;")) {
          
          friday.setChecked(true);
        }
        
        if (oldDay.contains("S;")) {
          
          saturday.setChecked(true);
        }
        
        if (oldDay.contains("Su;")) {
          
          sunday.setChecked(true);
        }
        
        String oldHour = oldTime.substring(0,oldTime.indexOf(":"));
        String oldMinute = oldTime.substring(oldTime.indexOf(":") + 1,oldTime.indexOf(Constants.NORMAL_SPACE));
        String oldDayHalf = oldTime.substring(oldTime.indexOf(Constants.NORMAL_SPACE)+1);
        
        sHour.setSelection(Integer.valueOf(oldHour) - 1);
        
        if (oldMinute.equals("00")) {
          
          sMinute.setSelection(0);
          
        } else if (oldMinute.equals("15")) {
          
          sMinute.setSelection(1);
          
        } else if (oldMinute.equals("30")) {
          
          sMinute.setSelection(2);
          
        } else if (oldMinute.equals("45")) {
          
          sMinute.setSelection(3);
          
        }
        
        sMinute.setSelection(Integer.valueOf(oldMinute));
        btnHalfOfDay.setText(oldDayHalf);
        
        
        
      } else {
        
        Toast.makeText(getApplicationContext(), "Something went wrong", 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