Android Open Source - GroupSmart-Android Create 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   ww w.j ava 2 s .c  om
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.Toast;

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

/**
 * 
 * @author Hunter Kehoe
 * 
 * Provides the Activity for the user to create a new group.
 *
 */
public class CreateGroup extends Activity {
  
  EditText groupName, teacherName, groupDescription, place;
  CheckBox monday, tuesday, wednesday, thursday, friday, saturday, sunday;
  Button btnCourse, createGroup, btnHalfOfDay;
  
  Spinner sHour, sMinute;
  
  Button btnHome, btnProfile, btnGroups, btnSettings;
  
  ProgressDialog pDialog;
  String checkGroupURL = "";
  String createGroupURL = "";
  
  public void onCreate(Bundle savedInstanceState) {
    
    super.onCreate(savedInstanceState);
    setContentView(R.layout.create_group);
    
    // Navigation Tabs
    btnProfile = (Button) findViewById(R.id.btnProfile);
    btnSettings = (Button) findViewById(R.id.btnSettings);
    btnGroups = (Button) findViewById(R.id.btnGroups);
    btnHome = (Button) findViewById(R.id.btnNewGroup);
    
    // Button listeners for the navigation
    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);
      }
    });
    
    btnGroups.setOnClickListener(new OnClickListener() {
      
      public void onClick(View v) {
        
        Intent i = new Intent(getApplicationContext(), FindGroup.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);
    
    // Set the time spinner
    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);
    createGroup = (Button) findViewById(R.id.btnCreateGroup);
    
    btnCourse.setOnClickListener(new OnClickListener() {
      
      public void onClick(View v) {
        
        Intent i = new Intent(getApplicationContext(), CourseSelect.class);
        startActivityForResult(i, 1);
      }
    });
    
    createGroup.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();
        
        String hour = sHour.getSelectedItem().toString();
        String minute = sMinute.getSelectedItem().toString();
        String dayHalf = btnHalfOfDay.getText().toString();
        
        
        String fullTime = hour+":"+minute+" "+dayHalf;
        
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        String userId = String.valueOf(prefs.getInt(Constants.ID_KEY, -1));
        
        if(!checkFormIsValid(name, teacher, course, userId)) {
          return;
        }
        
        String day = createDayString();
        
        checkGroupURL = Constants.BASE_URL+"mode=cdg&n="+name;
        checkGroupURL = checkGroupURL.replaceAll(Constants.NORMAL_SPACE, Constants.URL_SPACE);
        createGroupURL = Constants.BASE_URL+"mode=ng&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";
        createGroupURL = createGroupURL.replaceAll(Constants.NORMAL_SPACE, Constants.URL_SPACE);
        
        new CheckGroup().execute();
      }
    });
  }
  
  /**
   * Creates the string to indicate what days the group meets on for the database
   * @return A string representation of the days the group meets
   */
  public String createDayString() {
    
    String day = Constants.EMPTY_STRING;
    
    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;";
    }
    
    return day;
  }
  
  /**
   * Checks to make sure that the user filled in all required fields
   * @param name
   * @param teacher
   * @param course
   * @param userId
   * @return A boolean value indicating whether the form was filled correctly or not.
   */
  public Boolean checkFormIsValid(String name, String teacher, String course, String userId) {
    
    if(name.length() == 0) {
      
      Toast.makeText(getApplicationContext(),
               R.string.fillGroupName,
               Toast.LENGTH_SHORT).show();
      return false;
    }
    
    if(teacher.length() == 0) {
      
      Toast.makeText(getApplicationContext(),
               R.string.fillTeacherName,
               Toast.LENGTH_SHORT).show();
      return false;
    }
    
    if(place.length() == 0) {
      
      Toast.makeText(getApplicationContext(),
               R.string.fillMeetingPlace,
               Toast.LENGTH_SHORT).show();
      return false;
    }
    
    if (!monday.isChecked() &&
      !tuesday.isChecked() &&
      !wednesday.isChecked() &&
      !thursday.isChecked() &&
      !friday.isChecked() &&
      !saturday.isChecked() &&
      !sunday.isChecked()) {
      Toast.makeText(getApplicationContext(),
               R.string.fillOneDay,
               Toast.LENGTH_SHORT).show();
      return false;
    }
    
    if (course.equals("Course Name")) {
      
      Toast.makeText(getApplicationContext(),
               R.string.selectCourse,
               Toast.LENGTH_SHORT).show();
      return false;
    }
    
    
    
    if(userId.equals("")) {
      
      Toast.makeText(getApplicationContext(),
               R.string.pleaseLogin,
               Toast.LENGTH_SHORT).show();
      return false;
    }
    
    return true;
  }
  
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
    if (requestCode == 1) {
      
      if(resultCode == Activity.RESULT_OK) {
        
        String courseName = data.getStringExtra(Constants.NAME_KEY);
        btnCourse.setText(courseName);
      }
    }
  }
  
  
  protected void onNewIntent(Intent intent) {
    
    super.onNewIntent(intent);
  }

  /**
   * 
   * @author Hunter Kehoe
   * 
   * Asynchronously check if the group already exists.
   *
   */
  private class CheckGroup extends AsyncTask<Void, Void, Void> {
    
    boolean groupAvailable = false;
    
    protected void onPreExecute() {
      
      super.onPreExecute();
      pDialog = new ProgressDialog(CreateGroup.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(checkGroupURL, ServiceHandler.GET);
      //Log.d("Response: ", "> " + jsonStr);
      
      if (jsonStr.equals("available")) {
        
        groupAvailable = true;
      }
          
      return null;
    }
    
    protected void onPostExecute(Void result) {
      
      super.onPostExecute(result);
      
      if (groupAvailable) {
        
        new NewGroup().execute();
        
      } else {
        
        if(pDialog.isShowing()) {
          
          pDialog.dismiss();
        }
        
        Toast.makeText(getApplicationContext(),
                 "Group name already in use",
                 Toast.LENGTH_SHORT).show();
      }
    }
  }
  
  /**
   * 
   * @author Hunter Kehoe
   * 
   * Asynchronously send the group to the new server.
   *
   */
  private class NewGroup extends AsyncTask<Void, Void, Void> {
    
    boolean groupAdded = false;
    
    protected void onPreExecute() {
      
      super.onPreExecute();
    }
    
    protected Void doInBackground(Void... arg0) {
      
      ServiceHandler sh = new ServiceHandler();
      String jsonStr = sh.makeServiceCall(createGroupURL, ServiceHandler.GET);
      
      if (jsonStr.contains("Successfully")) {
        
        groupAdded = true;
      }
          
      return null;
    }
    
    protected void onPostExecute(Void result) {
      
      super.onPostExecute(result);
      
      if(pDialog.isShowing()) {
        
        pDialog.dismiss();
      }
      
      if (groupAdded) {
        
        Toast.makeText(getApplicationContext(),
                 R.string.groupCreated,
                 Toast.LENGTH_SHORT).show();
        Intent i = new Intent(getApplicationContext(), Profile.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        startActivity(i);
        finish();
        overridePendingTransition(0, 0);
      }
    }
  }
  
  public void onBackPressed() {
    
    super.onBackPressed();
    this.overridePendingTransition(0, 0);
  }
  
  // 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