From e20303f3de0782c31a0a794d1be4a06e3b4a0c76 Mon Sep 17 00:00:00 2001
From: Christopher Baines <cbaines8@gmail.com>
Date: Thu, 16 Feb 2012 10:41:27 +0000
Subject: More changes to the BusActivity.

---
 src/net/cbaines/suma/BusActivity.java              | 96 +++++++++++++++++++++-
 src/net/cbaines/suma/BusSpecificStopView.java      | 67 ---------------
 .../cbaines/suma/BusSpecificTimetableAdapter.java  |  4 +-
 3 files changed, 96 insertions(+), 71 deletions(-)

(limited to 'src')

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();
 
diff --git a/src/net/cbaines/suma/BusSpecificStopView.java b/src/net/cbaines/suma/BusSpecificStopView.java
index b13a6de..69b2aee 100644
--- a/src/net/cbaines/suma/BusSpecificStopView.java
+++ b/src/net/cbaines/suma/BusSpecificStopView.java
@@ -82,76 +82,9 @@ public class BusSpecificStopView extends LinearLayout implements OnClickListener
 	addView(location, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
 	addView(time, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
 
-	refreshData = new Runnable() {
-	    @Override
-	    public void run() {
-		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);
-		    }
-		}
-
-	    }
-	};
-
 	handler.post(refreshData);
     }
 
-    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);
-
-	    }
-	}
-    }
-
     public void setStop(Stop stop) {
 
 	// Log.i(TAG, "Time of arival " + stop.arivalTime);
diff --git a/src/net/cbaines/suma/BusSpecificTimetableAdapter.java b/src/net/cbaines/suma/BusSpecificTimetableAdapter.java
index 0467d4a..c5b81d3 100644
--- a/src/net/cbaines/suma/BusSpecificTimetableAdapter.java
+++ b/src/net/cbaines/suma/BusSpecificTimetableAdapter.java
@@ -29,7 +29,7 @@ import android.widget.BaseAdapter;
 
 public class BusSpecificTimetableAdapter extends BaseAdapter {
 
-    private final Context context;
+    private final BusActivity context;
     private Timetable timetable;
     private final Animation a;
     private boolean[] changed;
@@ -37,7 +37,7 @@ public class BusSpecificTimetableAdapter extends BaseAdapter {
 
     private static final String TAG = "BusSpecificTimetableAdapter";
 
-    public BusSpecificTimetableAdapter(Context context, Timetable timetable, Handler handler) {
+    public BusSpecificTimetableAdapter(BusActivity context, Timetable timetable, Handler handler) {
 	this.context = context;
 	this.timetable = timetable;
 	this.handler = handler;
-- 
cgit v1.2.3