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 ww. j a v a 2 s. co m import java.util.ArrayList; import java.util.HashMap; 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.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import com.google.analytics.tracking.android.EasyTracker; /** * * @author Hunter Kehoe * * Provides the activity for the user to view the profile. * */ public class Profile extends Activity { Button btnHome, btnSettings, btnGroups, btnNewGroup; Button btnUpdateProfile; ListView lvGroups, lvAdmin; ProgressDialog pDialog; String getGroupsURL = "", getAdminURL = ""; ArrayList<HashMap<String,String>> groupList = new ArrayList<HashMap<String,String>>(); ArrayList<HashMap<String,String>> adminList = new ArrayList<HashMap<String,String>>(); public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.profile); btnHome = (Button) findViewById(R.id.btnHome); btnSettings = (Button) findViewById(R.id.btnSettings); btnGroups = (Button) findViewById(R.id.btnGroups); btnNewGroup = (Button) findViewById(R.id.btnNewGroup); btnUpdateProfile = (Button) findViewById(R.id.btnUpdateProfile); lvGroups = (ListView) findViewById(R.id.lvGroups); lvAdmin = (ListView) findViewById(R.id.lvAdmin); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); String userId = String.valueOf(prefs.getInt(Constants.ID_KEY, -1)); getGroupsURL = Constants.BASE_URL+"mode=gugl&id="+userId; getAdminURL = Constants.BASE_URL+"mode=gual&id="+userId; new GetGroups().execute(); 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); } }); 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); } }); btnNewGroup.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); } }); btnUpdateProfile.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent i = new Intent(getApplicationContext(), UpdateProfile.class); startActivity(i); } }); lvGroups.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, final View v, int position, long id) { if (!((TextView) v.findViewById(R.id.name)).getText().toString().equals("No groups yet")) { Intent i = new Intent(getApplicationContext(), GroupInfo.class); i.putExtra(Constants.ID_KEY, ((TextView) v.findViewById(R.id.id)).getText().toString()); startActivity(i); } } }); lvAdmin.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, final View v, int position, long id) { Intent i = new Intent(getApplicationContext(), UpdateGroup.class); i.putExtra("gid", ((TextView) v.findViewById(R.id.id)).getText().toString()); startActivity(i); } }); } /** * * @author Hunter Kehoe * * Makes an asynchronous call to the server to get all of the groups. * */ private class GetGroups extends AsyncTask<Void, Void, Void> { JSONArray groups = null; protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(Profile.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(getGroupsURL, ServiceHandler.GET); //Log.d("Response: ", "> " + jsonStr); if (jsonStr.equals("no groups")) { HashMap<String,String> group = new HashMap<String, String>(); group.put(Constants.NAME_KEY, "No groups yet"); groupList.add(group); } else if (!jsonStr.contains("failed")) { 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); int id = u.getInt(Constants.ID_KEY); String name = u.getString(Constants.NAME_KEY); String className = u.getString(Constants.CLASS_KEY); String teacher = u.getString(Constants.TEACHER_KEY); HashMap<String,String> group = new HashMap<String,String>(); group.put(Constants.ID_KEY, String.valueOf(id)); group.put(Constants.NAME_KEY, name.replaceAll("\\\\","")); group.put("className", className); group.put(Constants.TEACHER_KEY, teacher); groupList.add(group); } } catch (JSONException e) { //Log.d("Error: ", "catch in JSON"); e.printStackTrace(); } } else if (jsonStr.length()== 0) { //Log.e("ServiceHandler", "Couldn't get any data from url"); } return null; } protected void onPostExecute(Void result) { super.onPostExecute(result); if(pDialog.isShowing()) { pDialog.dismiss(); } ListAdapter adapter = new SimpleAdapter(Profile.this, groupList, R.layout.group_list_item, new String[] { Constants.ID_KEY, Constants.NAME_KEY, "className", Constants.TEACHER_KEY }, new int[] { R.id.id, R.id.name, R.id.className, R.id.teacher }); lvGroups.setAdapter(adapter); new GetAdmin().execute(); } } /** * * @author Hunter Kehoe * * Asynchronous call to get the groups that this user is an admin of. * */ private class GetAdmin extends AsyncTask<Void, Void, Void> { JSONArray groups = null; protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(Profile.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(getAdminURL, ServiceHandler.GET); //Log.d("Response: ", "> " + jsonStr); if (jsonStr.equals("no groups")) { HashMap<String,String> group = new HashMap<String, String>(); group.put("name", "Haven\'t created any groups yet"); adminList.add(group); } else if (!jsonStr.contains("failed")) { 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); int id = u.getInt(Constants.ID_KEY); String name = u.getString(Constants.NAME_KEY); String className = u.getString(Constants.CLASS_KEY); String teacher = u.getString(Constants.TEACHER_KEY); HashMap<String, String> group = new HashMap<String, String>(); group.put(Constants.ID_KEY, String.valueOf(id)); group.put(Constants.NAME_KEY, name); group.put("className", className); group.put(Constants.TEACHER_KEY, teacher); adminList.add(group); } } catch (JSONException e) { e.printStackTrace(); } } else if (jsonStr.length()== 0) { //Log.e("ServiceHandler", "Couldn't get any data from url"); } else { HashMap<String, String> group = new HashMap<String, String>(); group.put(Constants.ID_KEY, ""); group.put(Constants.NAME_KEY, "You don't manage any groups yet"); group.put("className", ""); group.put(Constants.TEACHER_KEY, ""); adminList.add(group); } return null; } protected void onPostExecute(Void result) { super.onPostExecute(result); if(pDialog.isShowing()) { pDialog.dismiss(); } ListAdapter adapter = new SimpleAdapter(Profile.this, adminList, R.layout.group_list_item, new String[] { Constants.ID_KEY, Constants.NAME_KEY, "className", Constants.TEACHER_KEY }, new int[] { R.id.id, R.id.name, R.id.className, R.id.teacher }); lvAdmin.setAdapter(adapter); } } public void onBackPressed() { super.onBackPressed(); this.overridePendingTransition(0, 0); } public void onStart() { super.onStart(); EasyTracker.getInstance(this).activityStart(this); } public void onStop() { super.onStop(); EasyTracker.getInstance(this).activityStop(this); } }