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 va 2 s . c o 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.Menu; import android.view.MenuItem; 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 "home page" of the app. * */ public class Main extends Activity { Button btnProfile, btnSettings, btnGroups, btnNewGroup; ProgressDialog pDialog; ArrayList<HashMap<String,String>> groups = new ArrayList<HashMap<String, String>>(); String getGroupsURL = ""; ListView lvSessionList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); lvSessionList = (ListView) findViewById(R.id.lvSessionList); btnProfile = (Button) findViewById(R.id.btnProfile); btnSettings = (Button) findViewById(R.id.btnSettings); btnGroups = (Button) findViewById(R.id.btnGroups); btnNewGroup = (Button) findViewById(R.id.btnNewGroup); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); String userId = String.valueOf(prefs.getInt(Constants.ID_KEY, -1)); getGroupsURL = Constants.BASE_URL + "mode=gugla&id=" + userId; new GetGroups().execute(); 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); } }); 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); } }); 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); } }); 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); } }); lvSessionList.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); } } }); } /** * * @author Hunter Kehoe * * Provides a way to asynchronously grab the groups from the remote database. * */ private class GetGroups extends AsyncTask<Void, Void, Void> { JSONArray groupsArray = null; protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(Main.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); if (jsonStr != null && jsonStr.length() > 0) { try { JSONObject jsonObj = new JSONObject(jsonStr); groupsArray = jsonObj.getJSONArray(Constants.GROUPS_KEY); for(int i = 0; i < groupsArray.length(); i++) { JSONObject u = groupsArray.getJSONObject(i); int id = u.getInt(Constants.ID_KEY); String name = u.getString(Constants.NAME_KEY); String day = u.getString(Constants.DAY_KEY); String time = u.getString(Constants.TIME_KEY); String location = u.getString(Constants.LOCATION_KEY); //String teacher = u.getString("teacher"); //if (teacher.length() == 0) { // teacher = ""; //} day = day.replaceAll(";", Constants.NORMAL_SPACE); if(!day.contains("|")) { HashMap<String,String> group = new HashMap<String,String>(); group.put(Constants.ID_KEY, String.valueOf(id)); group.put(Constants.NAME_KEY, name.replaceAll("\\\\", Constants.EMPTY_STRING)); group.put(Constants.DAY_KEY, day); group.put(Constants.TIME_KEY, time); group.put(Constants.LOCATION_KEY, location.replaceAll("\\\\", Constants.EMPTY_STRING)); //group.put("teacher", teacher); groups.add(group); } } } catch (JSONException e) { e.printStackTrace(); } } else if (jsonStr.length() == 0) { HashMap<String,String> group = new HashMap<String,String>(); group.put(Constants.NAME_KEY, "No groups yet"); groups.add(group); } else { //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(Main.this, groups, R.layout.main_list_item, new String[] { Constants.ID_KEY, Constants.NAME_KEY, Constants.TEACHER_KEY, Constants.DAY_KEY, Constants.TIME_KEY, Constants.LOCATION_KEY }, new int[] { R.id.id, R.id.name, R.id.teacher, R.id.day, R.id.time, R.id.place }); lvSessionList.setAdapter(adapter); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item){ switch(item.getItemId()){ case R.id.actionLogout: SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences.Editor editor = prefs.edit(); editor.clear(); editor.commit(); Intent i = new Intent(getApplicationContext(), Login.class); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(i); finish(); break; } return super.onOptionsItemSelected(item); } 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); } }