Skip to content
Browse files

Moved connection requests to async tasks, added additional informatio…

…n on event item click, removed header completely from app
  • Loading branch information...
1 parent c17b326 commit f54924bd2b3429c5099904eb6c83aaf44f9f9a92 @fkautz committed Nov 27, 2011
View
8 AndroidManifest.xml
@@ -8,13 +8,15 @@
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".EventActivity"
android:label="@string/app_name"
- android:theme="@android:style/Theme.NoTitleBar">
+ android:theme="@android:style/Theme.NoTitleBar"
+ >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".StaffActivity"
- android:theme="@android:style/Theme.NoTitleBar"/>
+ android:theme="@android:style/Theme.NoTitleBar"
+ />
</application>
-</manifest>
+</manifest>
View
11 res/layout/main.xml
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:id="@+id/mainLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
@@ -9,17 +10,9 @@
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
- android:layout_marginTop="10px"
+ android:layout_margin="10px"
android:src="@drawable/hacker_dojo_logo_transparent"
/>
- <TextView
- android:id="@+id/header"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textSize="16pt"
- android:gravity="center_vertical|center_horizontal"
- android:visibility="visible"
- />
<ListView
android:id="@android:id/list"
View
7 src/com/hackerdojo/android/infoapp/Event.java
@@ -8,6 +8,7 @@
private String title;
private String host;
private String location;
+ private int size;
public Date getStartDate() {
return startDate;
}
@@ -48,4 +49,10 @@ public int compareTo(Event another) {
}
return comp;
}
+ public int getSize() {
+ return size;
+ }
+ public void setSize(int size) {
+ this.size = size;
+ }
}
View
383 src/com/hackerdojo/android/infoapp/EventActivity.java
@@ -1,168 +1,271 @@
package com.hackerdojo.android.infoapp;
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicReference;
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpResponse;
-import org.apache.http.client.ClientProtocolException;
-import org.apache.http.client.HttpClient;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.os.AsyncTask;
+import android.os.Bundle;
import android.util.Log;
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.ListView;
import android.widget.TextView;
+import android.widget.Toast;
-public class EventActivity extends HackerDojoActivity implements OnClickListener {
- private static final String HACKER_DOJO = "HACKER_DOJO";
- private static List<Event> events;
- private static Calendar lastUpdated = Calendar.getInstance();
- private static List<String> startDates;
- private static List<String> endDates;
- private static List<String> titles;
- static {
- lastUpdated.setTime(new Date(0));
- }
-
-
- private List<Event> getEvents() {
- HttpClient client = new DefaultHttpClient();
- StringBuffer sb = new StringBuffer();
- try {
- Log.i(HACKER_DOJO, "FETCHING EVENTS");
- HttpResponse response = client.execute(new HttpGet("http://events.hackerdojo.com/events.json"));
- HttpEntity entity = response.getEntity();
- char[] buf = new char[1024];
- InputStream content = entity.getContent();
- BufferedReader reader = new BufferedReader(new InputStreamReader(content));
- int c=0;
- while(c != -1) {
- c = reader.read(buf);
- if(c > 0) {
- sb.append(buf, 0, c);
- }
- }
- } catch (ClientProtocolException e) {
- Log.e(HACKER_DOJO, e.getMessage(), e);
- } catch (IOException e) {
- Log.e(HACKER_DOJO, e.getMessage(), e);
- }
-
- ArrayList<Event> events = new ArrayList<Event>();
- try {
- JSONArray json = new JSONArray(sb.toString());
- for(int i=0; i<json.length(); i++) {
- Event event = new Event();
- JSONObject jsonObject = json.getJSONObject(i);
- if(jsonObject.has("status") && jsonObject.has("start_time") && jsonObject.has("end_time") && jsonObject.has("name") && jsonObject.has("member") && jsonObject.has("rooms")) {
- if(jsonObject.getString("status").equals("approved")) {
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
- Date startDate = format.parse(jsonObject.getString("start_time")); // hardcode date for parsing
- Date endDate = format.parse(jsonObject.getString("end_time"));
- event.setStartDate(startDate);
- event.setEndDate(endDate);
- event.setTitle(jsonObject.getString("name"));
- event.setLocation(jsonObject.getString("rooms"));
- event.setHost(jsonObject.getString("member"));
- events.add(event);
- }
- }
- }
- } catch (JSONException e) {
- Log.e(HACKER_DOJO, e.getMessage(), e);
- } catch (ParseException e) {
- Log.e(HACKER_DOJO, e.getMessage(), e);
+public class EventActivity extends HackerDojoActivity implements
+ OnClickListener {
+
+ private final static String eventsUrl = "http://events.hackerdojo.com/events.json";
+
+ private final static AtomicReference<List<Event>> events = new AtomicReference<List<Event>>(
+ new ArrayList<Event>());
+ private final static AtomicReference<Map<Integer, Event>> eventIndexes = new AtomicReference<Map<Integer, Event>>(
+ new HashMap<Integer, Event>());
+
+ private final static AtomicReference<Calendar> lastChecked = new AtomicReference<Calendar>();
+ static {
+ Calendar cal = Calendar.getInstance();
+ cal.setTime(new Date(0));
+ lastChecked.set(cal);
+ }
+
+ @Override
+ public void onStart() {
+ super.onStart();
+ }
+
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ }
+
+ @Override
+ public void onResume() {
+ super.onResume();
+
+ Button eventButton = (Button) findViewById(R.id.eventsButton);
+ eventButton.setVisibility(View.GONE);
+
+ if (Calendar.getInstance().getTimeInMillis()
+ - lastChecked.get().getTimeInMillis() > (1000 * 60 * 10)) { // 10
+ // minutes
+ new UpdateEventsTask(this).execute(eventsUrl);
+ TextView emptyText = (TextView) findViewById(R.id.empty);
+ emptyText.setText("Loading...");
+ emptyText.setVisibility(View.VISIBLE);
+ } else {
+ new UpdateEventsView(this).execute(EventActivity.events.get());
}
- Collections.sort(events);
- return events;
- }
+ }
@Override
public void onClick(View v) {
super.onClick(v);
}
-
- @Override
- public void initializeView() {
- setHeader("Events");
- if(Calendar.getInstance().getTimeInMillis() - lastUpdated.getTimeInMillis() > 600000 /* 10 minutes */) {
- lastUpdated = Calendar.getInstance();
-
- events = getEvents();
-
- startDates = new ArrayList<String>();
- endDates = new ArrayList<String>();
- titles = new ArrayList<String>();
- Date lastDate = new Date(0);
- Date today = new Date();
- SimpleDateFormat format = new SimpleDateFormat("EEEE, MM/dd/yyyy");
- TextView emptyMessage = (TextView) findViewById(R.id.empty);
- if(events.size() == 0) {
- emptyMessage.setText("Check your network connection.");
- emptyMessage.setVisibility(View.VISIBLE);
- lastUpdated.setTime(new Date(0));
- } else {
- emptyMessage.setVisibility(View.GONE);
- }
- for(int i=0; i<events.size(); i++) {
- Event event = events.get(i);
- if(event.getEndDate().before(new Date())) {
- continue;
- }
- if(event.getStartDate().getYear()>lastDate.getYear() || event.getStartDate().getMonth() > lastDate.getMonth() || event.getStartDate().getDate() > lastDate.getDate()) {
- startDates.add("");
- endDates.add("");
- Date date = event.getStartDate();
- if(date.getYear() == today.getYear() && date.getMonth() == today.getMonth() && date.getDate() == today.getDate()) {
- titles.add("Today");
- } else {
- String dateString = format.format(date);
- titles.add(dateString);
- }
- lastDate = event.getStartDate();
- }
- String startMeridiem = "am";
- String endMeridiem = "am";
- int startHour = event.getStartDate().getHours();
- int endHour = event.getEndDate().getHours();
- if(startHour >= 12) {
- startMeridiem = "pm";
- startHour = startHour - 12;
- }
- if(endHour >= 12) {
- endMeridiem = "pm";
- endHour = endHour - 12;
- }
- if(startHour == 0) {
- startHour = 12;
- }
- if(endHour == 0) {
- endHour = 12;
- }
- startDates.add(String.format("%02d:%02d %s", startHour, event.getStartDate().getMinutes(),startMeridiem));
- endDates.add(String.format("%02d:%02d %s", endHour, event.getEndDate().getMinutes(),endMeridiem));
- titles.add(event.getTitle());
- }
- }
- EventArrayAdapter adapter2 = new EventArrayAdapter(this, startDates, endDates, titles);
- setListAdapter(adapter2);
-
- Button eventButton = (Button)findViewById(R.id.eventsButton);
- eventButton.setVisibility(View.GONE);
+
+ private class UpdateEventsTask extends JsonUpdateTask<Event> {
+ private Activity activity;
+
+ public UpdateEventsTask(Activity activity) {
+ this.activity = activity;
+ }
+
+ @Override
+ public List<Event> transform(String string) {
+ if (string == null || string.trim().length() == 0) {
+ return new ArrayList<Event>();
+ }
+ ArrayList<Event> events = new ArrayList<Event>();
+ try {
+ JSONArray json = new JSONArray(string);
+ for (int i = 0; i < json.length(); i++) {
+ Event event = new Event();
+ JSONObject jsonObject = json.getJSONObject(i);
+ if (jsonObject.has("status")
+ && jsonObject.has("start_time")
+ && jsonObject.has("end_time")
+ && jsonObject.has("name")
+ && jsonObject.has("member")
+ && jsonObject.has("rooms")) {
+ if (jsonObject.getString("status").equals("approved")) {
+ SimpleDateFormat format = new SimpleDateFormat(
+ "yyyy-MM-dd'T'HH:mm:ss");
+ Date startDate = format.parse(jsonObject
+ .getString("start_time")); // hardcode date
+ // for parsing
+ Date endDate = format.parse(jsonObject
+ .getString("end_time"));
+ event.setStartDate(startDate);
+ event.setEndDate(endDate);
+ event.setTitle(jsonObject.getString("name"));
+ event.setLocation(jsonObject.getString("rooms"));
+ event.setHost(jsonObject.getString("member"));
+ if(jsonObject.has("estimated_size")) {
+ event.setSize(jsonObject.getInt("estimated_size"));
+ }
+ events.add(event);
+ }
+ }
+ }
+ } catch (JSONException e) {
+ Log.e(HackerDojoActivity.TAG, e.getMessage(), e);
+ } catch (ParseException e) {
+ Log.e(HackerDojoActivity.TAG, e.getMessage(), e);
+ }
+ Collections.sort(events);
+ return events;
+ }
+
+ @SuppressWarnings("unchecked")
+ public void onPostExecute(List<Event> events) {
+ EventActivity.events.set(events);
+ new UpdateEventsView(activity).execute(EventActivity.events.get());
+ }
+ }
+
+ private class UpdateEventsView extends
+ AsyncTask<List<Event>, Void, List<Event>> {
+
+ private Activity activity;
+
+ public UpdateEventsView(Activity activity) {
+ this.activity = activity;
+ }
+
+ @Override
+ protected List<Event> doInBackground(List<Event>... params) {
+ return params[0];
+ }
+
+ @Override
+ public void onPostExecute(List<Event> events) {
+ Log.e(HackerDojoActivity.TAG, "event size: " + events.size());
+ Calendar lastUpdated = Calendar.getInstance();
+
+ ArrayList<String> startDates = new ArrayList<String>();
+ ArrayList<String> endDates = new ArrayList<String>();
+ ArrayList<String> titles = new ArrayList<String>();
+ Date lastDate = new Date(0);
+ Date today = new Date();
+ SimpleDateFormat format = new SimpleDateFormat("EEEE, MM/dd/yyyy");
+ TextView emptyMessage = (TextView) findViewById(R.id.empty);
+ Map<Integer, Event> eventMapping = new HashMap<Integer, Event>();
+ if (events.size() == 0) {
+ emptyMessage.setText("Check your network connection.");
+ emptyMessage.setVisibility(View.VISIBLE);
+ lastUpdated.setTime(new Date(0));
+ } else {
+ emptyMessage.setVisibility(View.GONE);
+ }
+ for (int i = 0; i < events.size(); i++) {
+ Event event = events.get(i);
+ if (event.getEndDate().before(new Date())) {
+ continue;
+ }
+ if (event.getStartDate().getYear() > lastDate.getYear()
+ || event.getStartDate().getMonth() > lastDate
+ .getMonth()
+ || event.getStartDate().getDate() > lastDate.getDate()) {
+ startDates.add("");
+ endDates.add("");
+ Date date = event.getStartDate();
+ if (date.getYear() == today.getYear()
+ && date.getMonth() == today.getMonth()
+ && date.getDate() == today.getDate()) {
+ titles.add("Today");
+ } else {
+ String dateString = format.format(date);
+ titles.add(dateString);
+ }
+ lastDate = event.getStartDate();
+ }
+ String startMeridiem = "am";
+ String endMeridiem = "am";
+ int startHour = event.getStartDate().getHours();
+ int endHour = event.getEndDate().getHours();
+ if (startHour >= 12) {
+ startMeridiem = "pm";
+ startHour = startHour - 12;
+ }
+ if (endHour >= 12) {
+ endMeridiem = "pm";
+ endHour = endHour - 12;
+ }
+ if (startHour == 0) {
+ startHour = 12;
+ }
+ if (endHour == 0) {
+ endHour = 12;
+ }
+ startDates.add(String.format("%02d:%02d %s", startHour, event
+ .getStartDate().getMinutes(), startMeridiem));
+ endDates.add(String.format("%02d:%02d %s", endHour, event
+ .getEndDate().getMinutes(), endMeridiem));
+ titles.add(event.getTitle());
+ eventMapping.put(titles.size() - 1, event);
+ }
+
+ eventIndexes.set(eventMapping);
+
+ EventArrayAdapter adapter2 = new EventArrayAdapter(activity,
+ startDates, endDates, titles);
+ setListAdapter(adapter2);
+
+ ListView lv = getListView();
+ lv.setOnItemClickListener(new OnItemClickListener() {
+ @Override
+ public void onItemClick(AdapterView<?> parent, View view,
+ int position, long id) {
+ Map<Integer, Event> map = EventActivity.eventIndexes.get();
+ if(map.containsKey(position)) {
+ Event event = map.get(position);
+ AlertDialog.Builder builder = new AlertDialog.Builder(
+ activity);
+ builder.setCancelable(true);
+ builder.setTitle(event.getTitle());
+ String message = String.format(
+ "Starts:\n%s\n\nEnds:\n%s\n\nat:\n%s\n\nhosted by:\n%s",
+ event.getStartDate(), event.getEndDate(),
+ event.getLocation(), event.getHost());
+ if(event.getSize() > 0) {
+ message = message + "\n\nestimated size:\n" + event.getSize();
+ }
+ builder.setMessage(message);
+ builder.create().show();
+ }
+ }
+
+ });
+
+ TextView emptyText = (TextView) findViewById(R.id.empty);
+ // update calendar
+ if (events.size() > 0) {
+ emptyText.setVisibility(View.GONE);
+ Calendar cal = Calendar.getInstance();
+ lastChecked.set(cal);
+ } else {
+ emptyText.setText("Unable to load events.");
+ emptyText.setVisibility(View.VISIBLE);
+ Calendar cal = Calendar.getInstance();
+ cal.setTime(new Date(0));
+ lastChecked.set(cal);
+ }
+ }
}
}
View
101 src/com/hackerdojo/android/infoapp/HackerDojoActivity.java
@@ -1,77 +1,90 @@
package com.hackerdojo.android.infoapp;
+import java.util.concurrent.Executor;
+import java.util.concurrent.Executors;
+
import android.app.ListActivity;
+import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
+import android.view.Display;
import android.view.Surface;
import android.view.View;
import android.view.View.OnClickListener;
+import android.view.WindowManager;
import android.widget.Button;
-import android.widget.TextView;
+import android.widget.ImageView;
+
+public abstract class HackerDojoActivity extends ListActivity implements
+ OnClickListener {
+ public static final String TAG = "HackerDojoActivity";
-public abstract class HackerDojoActivity extends ListActivity implements OnClickListener {
- private static final String HACKER_DOJO = "HACKER_DOJO";
+ public static final Executor tasks = Executors.newSingleThreadExecutor();
/** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
-
- @Override
- public void onResume() {
- super.onResume();
- initializeView();
- Button evenButton = (Button) findViewById(R.id.eventsButton);
- evenButton.setOnClickListener(this);
- Button staffButton = (Button) findViewById(R.id.staffButton);
- staffButton.setOnClickListener(this);
- Button navigateButton = (Button) findViewById(R.id.navigateButton);
- navigateButton.setOnClickListener(this);
- }
-
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.main);
+ }
+
+ @Override
+ public void onResume() {
+ super.onResume();
+
+ Button eventsButton = (Button) findViewById(R.id.eventsButton);
+ eventsButton.setOnClickListener(this);
+
+ Button staffButton = (Button) findViewById(R.id.staffButton);
+ staffButton.setOnClickListener(this);
+
+ Button navigateButton = (Button) findViewById(R.id.navigateButton);
+ navigateButton.setOnClickListener(this);
+
+ // hide dojo logo when sideways, takes up a lot of room
+ View dojoLogo = findViewById(R.id.imageView1);
+ if(this.getWindowManager().getDefaultDisplay().getOrientation() == Surface.ROTATION_180 || this.getWindowManager().getDefaultDisplay().getOrientation() == Surface.ROTATION_0) {
+ dojoLogo.setVisibility(View.VISIBLE);
+ } else {
+ dojoLogo.setVisibility(View.GONE);
+ }
+
+ }
+
@Override
public void onClick(View v) {
- Log.e(HACKER_DOJO, "Button Click: " + v.getId());
- if(v.getId() == R.id.eventsButton) {
+ Log.i(TAG, "Button Click: " + v.getId());
+ switch (v.getId()) {
+ case R.id.eventsButton:
openEvents();
- } else if(v.getId() == R.id.staffButton) {
+ break;
+ case R.id.staffButton:
openStaff();
- } else if(v.getId() == R.id.navigateButton) {
+ break;
+ case R.id.navigateButton:
openNavigation();
+ break;
+ default:
+
}
}
-
+
private void openEvents() {
Intent intent = new Intent(HackerDojoActivity.this, EventActivity.class);
startActivity(intent);
}
-
+
private void openStaff() {
Intent intent = new Intent(HackerDojoActivity.this, StaffActivity.class);
startActivity(intent);
}
-
+
private void openNavigation() {
- Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=140%20whisman%20rd%2C%20mountain%20view%2C%20ca"));
+ Intent intent = new Intent(
+ Intent.ACTION_VIEW,
+ Uri.parse("google.navigation:q=140%20whisman%20rd%2C%20mountain%20view%2C%20ca"));
startActivity(intent);
}
-
- protected void setHeader(String headerString) {
- View dojoLogo = findViewById(R.id.imageView1);
- TextView header = (TextView) findViewById(R.id.header);
-// if(this.getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_180 || this.getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_0) {
- if(this.getWindowManager().getDefaultDisplay().getOrientation() == Surface.ROTATION_180 || this.getWindowManager().getDefaultDisplay().getOrientation() == Surface.ROTATION_0) {
- dojoLogo.setVisibility(View.VISIBLE);
- header.setText(headerString);
- } else {
- dojoLogo.setVisibility(View.GONE);
- header.setText("Hacker Dojo " + headerString);
- }
- }
-
- protected abstract void initializeView();
}
View
51 src/com/hackerdojo/android/infoapp/JsonUpdateTask.java
@@ -0,0 +1,51 @@
+package com.hackerdojo.android.infoapp;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.List;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.DefaultHttpClient;
+
+import android.os.AsyncTask;
+import android.util.Log;
+
+public abstract class JsonUpdateTask<T> extends AsyncTask<String, Long, List<T>> {
+ public String fetchFromUrl(String url) {
+ HttpClient client = new DefaultHttpClient();
+ StringBuffer sb = new StringBuffer();
+ try {
+ Log.i(HackerDojoActivity.TAG, "FETCHING: " + url);
+ HttpResponse response = client.execute(new HttpGet(url));
+ HttpEntity entity = response.getEntity();
+ char[] buf = new char[1024];
+ InputStream content = entity.getContent();
+ BufferedReader reader = new BufferedReader(new InputStreamReader(content));
+ int c=0;
+ while(c != -1) {
+ c = reader.read(buf);
+ if(c > 0) {
+ sb.append(buf, 0, c);
+ }
+ }
+ } catch (ClientProtocolException e) {
+ Log.e(HackerDojoActivity.TAG, e.getMessage(), e);
+ } catch (IOException e) {
+ Log.e(HackerDojoActivity.TAG, e.getMessage(), e);
+ }
+ return sb.toString();
+ }
+
+ @Override
+ protected List<T> doInBackground(String... params) {
+ return transform(fetchFromUrl(params[0]));
+ }
+
+ public abstract List<T> transform(String json);
+}
View
235 src/com/hackerdojo/android/infoapp/StaffActivity.java
@@ -1,9 +1,5 @@
package com.hackerdojo.android.infoapp;
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@@ -12,136 +8,147 @@
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
+import java.util.concurrent.atomic.AtomicReference;
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpResponse;
-import org.apache.http.client.ClientProtocolException;
-import org.apache.http.client.HttpClient;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
+import android.app.Activity;
+import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
-public class StaffActivity extends HackerDojoActivity implements OnClickListener {
- private static final String HACKER_DOJO = "HACKER_DOJO";
- private static List<Person> currentStaff;
- private static Calendar lastUpdated = Calendar.getInstance();
- private static List<String> names;
- private static List<String> timeHere;
- private static List<String> images;
- static {
- lastUpdated.setTime(new Date(0));
- }
+public class StaffActivity extends HackerDojoActivity implements
+ OnClickListener {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
-
- private List<Person> getStaff() {
- HttpClient client = new DefaultHttpClient();
- StringBuffer sb = new StringBuffer();
- try {
- Log.i(HACKER_DOJO, "FETCHING EVENTS");
- HttpResponse response = client.execute(new HttpGet("http://hackerdojo-signin.appspot.com/staffjson"));
- HttpEntity entity = response.getEntity();
- char[] buf = new char[1024];
- InputStream content = entity.getContent();
- BufferedReader reader = new BufferedReader(new InputStreamReader(content));
- int c=0;
- while(c != -1) {
- c = reader.read(buf);
- if(c > 0) {
- sb.append(buf, 0, c);
- }
- }
- } catch (ClientProtocolException e) {
- Log.e(HACKER_DOJO, e.getMessage(), e);
- } catch (IOException e) {
- Log.e(HACKER_DOJO, e.getMessage(), e);
- }
-
- ArrayList<Person> currentStaffBuilder = new ArrayList<Person>();
- try {
- JSONArray json = new JSONArray(sb.toString());
- for(int i=0; i<json.length(); i++) {
- Person person = new Person();
- JSONObject jsonObject = json.getJSONObject(i);
- if(jsonObject.has("name") && jsonObject.has("created") && jsonObject.has("image_url") && jsonObject.has("name")) {
- SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
-
- String name = jsonObject.getString("name");
- Log.e(HACKER_DOJO, "created: " + jsonObject.getString("created"));
- Date created = format.parse(jsonObject.getString("created")); // hardcode date for parsing
- String imageUrl = jsonObject.getString("image_url");
-
- person.setName(name);
- person.setCreated(created);
- person.setImageUrl(imageUrl);
- currentStaffBuilder.add(person);
- }
- }
- } catch (JSONException e) {
- Log.e(HACKER_DOJO, e.getMessage(), e);
- } catch (ParseException e) {
- Log.e(HACKER_DOJO, e.getMessage(), e);
+ private final static AtomicReference<Calendar> lastChecked = new AtomicReference<Calendar>();
+ private final static AtomicReference<List<Person>> staff = new AtomicReference<List<Person>>(new ArrayList<Person>());
+
+ static {
+ Calendar cal = Calendar.getInstance();
+ cal.setTime(new Date(0));
+ lastChecked.set(cal);
+ }
+
+ private final static String staffUrl = "http://hackerdojo-signin.appspot.com/staffjson";
+
+ @Override
+ public void onStart() {
+ super.onStart();
+ }
+
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ }
+
+ @Override
+ public void onResume() {
+ super.onResume();
+
+ Button staffButton = (Button) findViewById(R.id.staffButton);
+ staffButton.setVisibility(View.GONE);
+
+ // strange bug, events properly maintains state but staff doesn't
+
+ if (Calendar.getInstance().getTimeInMillis() - lastChecked.get().getTimeInMillis() > (1000 * 60 * 10)) { // 10 minutes
+ new UpdateStaffTask(this).execute(staffUrl);
+ TextView emptyText = (TextView) findViewById(R.id.empty);
+ emptyText.setText("Loading...");
+ emptyText.setVisibility(View.VISIBLE);
+ } else {
+ new UpdateStaffView(this).execute(StaffActivity.staff.get());
}
- Collections.sort(currentStaffBuilder);
- return currentStaffBuilder;
- }
-
+ }
+
@Override
public void onClick(View v) {
super.onClick(v);
}
- @Override
- protected void initializeView() {
- setHeader("Staff on Site");
- if(Calendar.getInstance().getTimeInMillis() - lastUpdated.getTimeInMillis() > 600000 /* 10 minutes */) {
- lastUpdated = Calendar.getInstance();
-
- currentStaff = getStaff();
+ private class UpdateStaffTask extends JsonUpdateTask<Person> {
+ private Activity activity;
+ public UpdateStaffTask(Activity activity) {
+ this.activity = activity;
+ }
+ @Override
+ public List<Person> transform(String string) {
+ if (string == null || string.trim().length() == 0) {
+ return new ArrayList<Person>();
+ }
+ ArrayList<Person> staff = new ArrayList<Person>();
+ try {
+ JSONArray json = new JSONArray(string);
+ for(int i=0; i<json.length(); i++) {
+ Person person = new Person();
+ JSONObject jsonObject = json.getJSONObject(i);
+ if(jsonObject.has("name") && jsonObject.has("created") && jsonObject.has("image_url") && jsonObject.has("name")) {
+ SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
+
+ String name = jsonObject.getString("name");
+ Log.e(TAG, "created: " + jsonObject.getString("created"));
+ Date created = format.parse(jsonObject.getString("created")); // hardcode date for parsing
+ String imageUrl = jsonObject.getString("image_url");
+
+ person.setName(name);
+ person.setCreated(created);
+ person.setImageUrl(imageUrl);
+ staff.add(person);
+ }
+ }
+ } catch (JSONException e) {
+ Log.e(TAG, e.getMessage(), e);
+ } catch (ParseException e) {
+ Log.e(TAG, e.getMessage(), e);
+ }
+ Collections.sort(staff);
+ return staff;
+ }
+
+ public void onPostExecute(List<Person> staff) {
+ StaffActivity.staff.set(staff);
+ new UpdateStaffView(activity).execute(StaffActivity.staff.get());
+ }
+ }
+
+ private class UpdateStaffView extends AsyncTask<List<Person>, Void, List<Person>> {
+ private Activity activity;
+ public UpdateStaffView(Activity activity) {
+ this.activity = activity;
+ }
+
+ protected List<Person> doInBackground(List<Person>... params) {
+ return params[0];
+ }
+
+ public void onPostExecute(List<Person> staff) {
+ Log.e(HackerDojoActivity.TAG, "staff size: " + staff.size());
+
+ Calendar lastUpdated = Calendar.getInstance();
- names = new ArrayList<String>();
- timeHere = new ArrayList<String>();
- images = new ArrayList<String>();
- TextView emptyMessage = (TextView) findViewById(R.id.empty);
- if(currentStaff.size() == 0) {
- emptyMessage.setText("Dojo is closed");
- emptyMessage.setVisibility(View.VISIBLE);
- lastUpdated.setTime(new Date(0));
- } else {
- emptyMessage.setVisibility(View.GONE);
- }
- for(int i=0; i<currentStaff.size(); i++) {
- Person person = currentStaff.get(i);
+ ArrayList<String> names = new ArrayList<String>();
+ ArrayList<String> timeHere = new ArrayList<String>();
+ ArrayList<String> images = new ArrayList<String>();
+ for(int i=0; i<staff.size(); i++) {
+ Person person = staff.get(i);
Calendar timeEntered = Calendar.getInstance();
timeEntered.setTime(person.getCreated());
long timeRadix = lastUpdated.getTimeInMillis() - timeEntered.getTimeInMillis();
// normalize for timezone
+
long offset = TimeZone.getDefault().getRawOffset();
timeRadix = timeRadix - offset;
timeRadix = timeRadix / 60;
timeRadix = timeRadix / 1000;
- Log.e(HACKER_DOJO, "lastUpdated: " + lastUpdated);
- Log.e(HACKER_DOJO, "timeEntered: " + timeEntered);
- Log.e(HACKER_DOJO, "radix: " + timeRadix);
- // time is UTC
+ // time is now UTC
long minutes = timeRadix % 60;
timeRadix = timeRadix / 60;
@@ -163,11 +170,23 @@ protected void initializeView() {
timeHere.add(time.toString());
images.add("[image]");
}
- }
- StaffArrayAdapter adapter2 = new StaffArrayAdapter(this, images, names, timeHere);
- setListAdapter(adapter2);
-
- Button staffButton = (Button)findViewById(R.id.staffButton);
- staffButton.setVisibility(View.GONE);
+
+ StaffArrayAdapter adapter2 = new StaffArrayAdapter(activity, images, names, timeHere);
+ setListAdapter(adapter2);
+
+ TextView emptyText = (TextView) findViewById(R.id.empty);
+ // update calendar
+ if (staff.size() > 0) {
+ emptyText.setVisibility(View.GONE);
+ Calendar cal = Calendar.getInstance();
+ lastChecked.set(cal);
+ } else {
+ emptyText.setText("Dojo is closed");
+ emptyText.setVisibility(View.VISIBLE);
+ Calendar cal = Calendar.getInstance();
+ cal.setTime(new Date(0));
+ lastChecked.set(cal);
+ }
+ }
}
}

0 comments on commit f54924b

Please sign in to comment.
Something went wrong with that request. Please try again.