diff options
author | Christopher Baines <cbaines8@gmail.com> | 2012-03-15 22:26:39 +0000 |
---|---|---|
committer | Christopher Baines <cbaines8@gmail.com> | 2012-03-15 22:26:39 +0000 |
commit | b64d1a19c68a703526460bec8e9a0ea22448b1b5 (patch) | |
tree | 418fad5645493b35bfde45b1d5d7cdf5cf435b31 /src/net/cbaines/suma/BusActivity.java | |
parent | e667851ae9869167677aa5e6e4c20707417cc687 (diff) | |
download | southamptonuniversitymap-b64d1a19c68a703526460bec8e9a0ea22448b1b5.tar southamptonuniversitymap-b64d1a19c68a703526460bec8e9a0ea22448b1b5.tar.gz |
More Bus Activity work.
Diffstat (limited to 'src/net/cbaines/suma/BusActivity.java')
-rw-r--r-- | src/net/cbaines/suma/BusActivity.java | 147 |
1 files changed, 67 insertions, 80 deletions
diff --git a/src/net/cbaines/suma/BusActivity.java b/src/net/cbaines/suma/BusActivity.java index c043687..85ed9f7 100644 --- a/src/net/cbaines/suma/BusActivity.java +++ b/src/net/cbaines/suma/BusActivity.java @@ -2,6 +2,7 @@ package net.cbaines.suma; import java.io.IOException; import java.sql.SQLException; +import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; @@ -45,8 +46,7 @@ public class BusActivity extends ToastHelperActivity implements Preferences { Runnable refreshData; - protected Timetable timetable; - private Timetable visibleTimetable; + private Timetable timetable; private ListView timetableView; @@ -54,8 +54,9 @@ public class BusActivity extends ToastHelperActivity implements Preferences { // BusStops and if they are being updated by the handler List<BusStop> busStops; + ArrayList<Integer> busStopRoutePositions; - private HashMap<BusStop, GetTimetableStopTask> tasks = new HashMap<BusStop, GetTimetableStopTask>(); + private HashMap<Integer, GetTimetableStopTask> tasks = new HashMap<Integer, GetTimetableStopTask>(); Handler handler; @@ -126,8 +127,8 @@ public class BusActivity extends ToastHelperActivity implements Preferences { if (bus.destinationString != null) { // Log.i(TAG, "Bus destination string is " + // bus.destinationString); - busDestTextView.setText(getResources().getString(R.string.bus_activity_destination_label) - + bus.destinationString); + busDestTextView + .setText(getResources().getString(R.string.bus_activity_destination_label) + bus.destinationString); busDestTextView.setVisibility(View.VISIBLE); } else { // Log.i(TAG, "Bus destination string is null"); @@ -136,59 +137,32 @@ public class BusActivity extends ToastHelperActivity implements Preferences { } busStops = bus.route.getRouteBusStops(this); + busStopRoutePositions = new ArrayList<Integer>(); + for (int i = 0; i < busStops.size(); i++) { + busStopRoutePositions.add(i); + } // Log.i(TAG, "Got " + busStops.size() + " bus stops for this bus"); - if (bus.destination != null) { - Log.i(TAG, "Bus destination is " + bus.destination); - - // int index = busStops.indexOf(bus.destination); - // if (index != -1) { - // Log.v(TAG, "Found destination stop in busStops"); - // for (int i = index + 1; i < busStops.size(); i++) { - // Log.v(TAG, "Removing " + busStops.get(i)); - // busStops.remove(i); - // } - // } + if (bus.route.id == 326 && !bus.direction.equals("E")) { + Log.i(TAG, "Removing extra U1 stops"); + for (int i = 35; i < 50; i++) { + Log.i(TAG, "Removing " + busStops.get(35).description); + busStops.remove(35); + busStopRoutePositions.remove(35); + } } else { - Log.i(TAG, "Bus destination is null"); + Log.i(TAG, "Not removing extra U1 stops"); } refreshData = new Runnable() { public void run() { Log.v(TAG, "Refreshing data " + (System.currentTimeMillis() - timeOfLastRefresh)); timeOfLastRefresh = System.currentTimeMillis(); - for (int num = timetableView.getFirstVisiblePosition(); num <= timetableView - .getLastVisiblePosition(); num++) { + for (int num = timetableView.getFirstVisiblePosition(); num <= timetableView.getLastVisiblePosition(); num++) { final Stop stop = timetable.get(num); if (System.currentTimeMillis() - stop.timeOfFetch.getTime() > 20000) { - GetTimetableStopTask task = tasks.get(busStops.get(num)); - - if (task != null) { // If there is a taks - if (task.getStatus() == AsyncTask.Status.FINISHED) { // If - // its - // finished - task = null; // Delete it - } - } - - if (task == null) { // If there is now no task - // Log.v(TAG, "Updating " + busStops.get(num)); - - // Remove the old time from the timetable - synchronized (timetable) { - timetable.set( - num, - new StopLoading(bus, busStops.get(num), null, new Date(System - .currentTimeMillis() - 21000), false)); - displayTimetable(timetable); - } - - task = new GetTimetableStopTask(); - BusStop[] str = { stop.busStop }; - task.execute(str); - tasks.put(stop.busStop, task); - } + refreshBusStop(num); } else { // Log.v(TAG, "Not updating " + busStops.get(num)); } @@ -202,6 +176,35 @@ public class BusActivity extends ToastHelperActivity implements Preferences { } } + private void refreshBusStop(int index) { + GetTimetableStopTask task = tasks.get(index); + + if (task != null) { // If there is a taks + if (task.getStatus() == AsyncTask.Status.FINISHED) { // If + // its + // finished + task = null; // Delete it + } + } + + if (task == null) { // If there is now no task + // Log.v(TAG, "Updating " + busStops.get(num)); + + // Remove the old time from the timetable + synchronized (timetable) { + timetable.set( + index, + new StopLoading(bus, busStops.get(index), timetable.get(index).arivalTime, new Date(System + .currentTimeMillis() - 21000), false)); + displayTimetable(); + } + + task = new GetTimetableStopTask(); + task.execute(index); + tasks.put(index, task); + } + } + @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); @@ -226,14 +229,14 @@ public class BusActivity extends ToastHelperActivity implements Preferences { for (int i = 0; i < busStops.size(); i++) { // Add a loading stop, with a fetch time such that it will // be fetched - timetable.add(new StopLoading(bus, busStops.get(i), null, new Date( - System.currentTimeMillis() - 21000), false)); + timetable + .add(new StopLoading(bus, busStops.get(i), null, new Date(System.currentTimeMillis() - 21000), false)); } // Log.v(TAG, "Finished adding placeholder stops"); } else { Log.i(TAG, "Displaying previous timetable"); } - displayTimetable(timetable); + displayTimetable(); if (busStop != null) { Log.i(TAG, "Moving to position of " + busStop.description + " which is " + busStops.indexOf(busStop)); timetableView.setSelection(busStops.indexOf(busStop)); @@ -274,21 +277,7 @@ public class BusActivity extends ToastHelperActivity implements Preferences { } for (int num = timetableView.getFirstVisiblePosition(); num <= timetableView.getLastVisiblePosition(); num++) { - final Stop stop = timetable.get(num); - - // Log.v(TAG, "Updating " + busStops.get(num)); - - // Remove the old time from the timetable - synchronized (timetable) { - timetable.set(num, new StopLoading(bus, busStops.get(num), timetable.get(num).arivalTime, - new Date(System.currentTimeMillis() - 21000), false)); - displayTimetable(timetable); - } - - GetTimetableStopTask task = new GetTimetableStopTask(); - BusStop[] str = { stop.busStop }; - task.execute(str); - tasks.put(stop.busStop, task); + refreshBusStop(num); } handler.postDelayed(refreshData, 50000); @@ -302,29 +291,29 @@ public class BusActivity extends ToastHelperActivity implements Preferences { return true; } - private class GetTimetableStopTask extends AsyncTask<BusStop, Integer, Stop> { + private class GetTimetableStopTask extends AsyncTask<Integer, Integer, Stop> { // private String errorMessage; private BusStop busStop; - private int position; + private int busStopIndex; protected void onPreExecute() { // progBar.setVisibility(View.VISIBLE); } - protected Stop doInBackground(BusStop... busStopArray) { - busStop = busStopArray[0]; - position = busStops.indexOf(busStop); + protected Stop doInBackground(Integer... busStopIndexs) { + busStop = busStops.get(busStopIndexs[0]); + busStopIndex = busStopIndexs[0]; Stop stop = null; try { - // Log.i(TAG, "Fetching stop for busStop " + position); - stop = DataManager.getStop(instance, bus, position); + // Log.i(TAG, "Fetching stop for busStop " + busStop.description); + stop = DataManager.getStop(instance, bus, busStopRoutePositions.get(busStopIndex)); if (stop == null) { stop = new Stop(bus, busStop, null, new Date(System.currentTimeMillis()), false); } - // Log.i(TAG, "Finished fetching stop for busStop " + position); + // Log.i(TAG, "Finished fetching stop for busStop " + stop.busStop.description + " " + busStop.description); } catch (SQLException e) { // errorMessage = "Error message regarding SQL?"; e.printStackTrace(); @@ -346,8 +335,8 @@ public class BusActivity extends ToastHelperActivity implements Preferences { // Log.i(TAG, "Got timetable"); synchronized (timetable) { - timetable.set(position, stop); - displayTimetable(timetable); + timetable.set(busStopIndex, stop); + displayTimetable(); } } @@ -358,15 +347,13 @@ public class BusActivity extends ToastHelperActivity implements Preferences { return timetable; } - private void displayTimetable(Timetable timetable) { - visibleTimetable = (Timetable) timetable.clone(); - + private void displayTimetable() { if (timetable.size() == 0) { busContentMessage.setText("No Busses"); busContentMessage.setVisibility(View.VISIBLE); busActivityContentLayout.setGravity(Gravity.CENTER); } else { - if (visibleTimetable.size() == 0) { + if (timetable.size() == 0) { busActivityContentLayout.setGravity(Gravity.CENTER); busContentMessage.setText("No Busses (With the current enabled routes)"); busContentMessage.setVisibility(View.VISIBLE); @@ -376,9 +363,9 @@ public class BusActivity extends ToastHelperActivity implements Preferences { busContentMessage.setVisibility(View.GONE); BusSpecificTimetableAdapter adapter; if ((adapter = (BusSpecificTimetableAdapter) timetableView.getAdapter()) != null) { - adapter.updateTimetable(visibleTimetable); + adapter.updateTimetable(timetable); } else { - adapter = new BusSpecificTimetableAdapter(this, visibleTimetable); + adapter = new BusSpecificTimetableAdapter(this, timetable); timetableView.setAdapter(adapter); } busActivityContentLayout.setGravity(Gravity.TOP); |