aboutsummaryrefslogtreecommitdiff
path: root/src/net/cbaines/suma/BusActivity.java
diff options
context:
space:
mode:
authorChristopher Baines <cbaines8@gmail.com>2012-02-16 10:41:27 +0000
committerChristopher Baines <cbaines8@gmail.com>2012-02-16 10:41:27 +0000
commite20303f3de0782c31a0a794d1be4a06e3b4a0c76 (patch)
treec8259328e0eb3e9bae3e7c57490d35fb41c5b059 /src/net/cbaines/suma/BusActivity.java
parentfcac217897640f23d0a528c02f861e1e2d15a212 (diff)
downloadsouthamptonuniversitymap-e20303f3de0782c31a0a794d1be4a06e3b4a0c76.tar
southamptonuniversitymap-e20303f3de0782c31a0a794d1be4a06e3b4a0c76.tar.gz
More changes to the BusActivity.
Diffstat (limited to 'src/net/cbaines/suma/BusActivity.java')
-rw-r--r--src/net/cbaines/suma/BusActivity.java96
1 files changed, 94 insertions, 2 deletions
diff --git a/src/net/cbaines/suma/BusActivity.java b/src/net/cbaines/suma/BusActivity.java
index be94bd3..eb512f8 100644
--- a/src/net/cbaines/suma/BusActivity.java
+++ b/src/net/cbaines/suma/BusActivity.java
@@ -7,6 +7,8 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
+import net.cbaines.suma.BusSpecificStopView.GetTimetableStopTask;
+
import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
@@ -44,6 +46,8 @@ public class BusActivity extends OrmLiteBaseActivity<DatabaseHelper> implements
private Bus bus;
private BusStop busStop;
+ private Runnable refreshData;
+
protected Timetable timetable;
private Timetable visibleTimetable;
@@ -51,7 +55,11 @@ public class BusActivity extends OrmLiteBaseActivity<DatabaseHelper> implements
private Context instance;
- private List<BusStop> busStops;
+ // BusStops and if they are being updated by the handler
+ ArrayList<BusStop> busStops;
+ ArrayList<Boolean> busStopsActive;
+
+ private HashMap<BusStop, GetTimetableStopTask> tasks = new HashMap<BusStop, GetTimetableStopTask>();
private Handler handler;
@@ -155,6 +163,34 @@ public class BusActivity extends OrmLiteBaseActivity<DatabaseHelper> implements
}
}
+ refreshData = new Runnable() {
+ @Override
+ public void run() {
+ for (int num = 0; num < timetable.size(); num++) {
+ if (busStopsActive.get(num)) {
+ Stop stop = timetable.get(num);
+
+ GetTimetableStopTask task = tasks.get(busStops.get(num));
+
+ if (stop.timeOfFetch == null || (stop.timeOfFetch.getTime() - System.currentTimeMillis()) > 20000) {
+ if (task == null) {
+ task = new GetTimetableStopTask();
+ BusStop[] str = { stop.busStop };
+ task.execute(str);
+ handler.postDelayed(refreshData, 20000);
+ } else {
+ if (task.getStatus() == AsyncTask.Status.FINISHED) {
+ BusStop[] str = { stop.busStop };
+ task.execute(str);
+ handler.postDelayed(refreshData, 20000);
+ }
+ }
+ }
+ }
+ }
+ }
+ };
+
}
public void onResume() {
@@ -190,7 +226,7 @@ public class BusActivity extends OrmLiteBaseActivity<DatabaseHelper> implements
public void onPause() {
if (handler != null) { // BusTimes are enabled
- handler.removeCallbacks(r)
+ handler.removeCallbacks(refreshData);
if (timetableStopTasks != null) { // Could happen if the handler has not created the timetableTask yet
for (GetTimetableStopTask task : timetableStopTasks.values()) {
if (task != null) {
@@ -204,6 +240,62 @@ public class BusActivity extends OrmLiteBaseActivity<DatabaseHelper> implements
super.onPause();
}
+ private class GetTimetableStopTask extends AsyncTask<BusStop, Integer, Stop> {
+ private String errorMessage;
+
+ private BusStop busStop;
+
+ private int position;
+
+ protected void onPreExecute() {
+ // progBar.setVisibility(View.VISIBLE);
+ }
+
+ protected Stop doInBackground(BusStop... busStopArray) {
+ busStop = busStopArray[0];
+ Stop stop = null;
+
+ try {
+ Log.i(TAG, "Fetching stop for busStop " + position);
+ stop = DataManager.getStop(context, stop.bus, busStop);
+ if (stop == null) {
+ stop = new Stop(stop.bus, busStop, null, null, false);
+ }
+ Log.i(TAG, "Finished fetching stop for busStop " + position);
+ } catch (SQLException e) {
+ errorMessage = "Error message regarding SQL?";
+ e.printStackTrace();
+ } catch (ClientProtocolException e) {
+ errorMessage = "ClientProtocolException!?!";
+ e.printStackTrace();
+ } catch (IOException e) {
+ errorMessage = "Error fetching bus times from server, are you connected to the internet?";
+ e.printStackTrace();
+ } catch (JSONException e) {
+ errorMessage = "Error parsing bus times";
+ e.printStackTrace();
+ }
+ return stop;
+ }
+
+ protected void onPostExecute(Stop stop) {
+ // Log.i(TAG, "Got timetable");
+ if (stop == null) {
+
+ // progBar.setVisibility(View.GONE);
+ // busContentMessage.setText(errorMessage);
+ // busContentMessage.setVisibility(View.VISIBLE);
+ } else {
+ // progBar.setVisibility(View.GONE);
+
+ }
+ }
+ }
+
+ void setStopUpdated(BusStop stop, boolean updated) {
+
+ }
+
private void displayTimetable(Timetable timetable) {
visibleTimetable = (Timetable) timetable.clone();