From b64d1a19c68a703526460bec8e9a0ea22448b1b5 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Thu, 15 Mar 2012 22:26:39 +0000 Subject: More Bus Activity work. --- src/net/cbaines/suma/BusActivity.java | 147 ++++++++++++-------------- src/net/cbaines/suma/BusSpecificStopView.java | 20 ++-- src/net/cbaines/suma/DataManager.java | 96 +++++++---------- 3 files changed, 113 insertions(+), 150 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 busStops; + ArrayList busStopRoutePositions; - private HashMap tasks = new HashMap(); + private HashMap tasks = new HashMap(); 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(); + 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 { + private class GetTimetableStopTask extends AsyncTask { // 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); diff --git a/src/net/cbaines/suma/BusSpecificStopView.java b/src/net/cbaines/suma/BusSpecificStopView.java index b140420..7252802 100644 --- a/src/net/cbaines/suma/BusSpecificStopView.java +++ b/src/net/cbaines/suma/BusSpecificStopView.java @@ -90,22 +90,20 @@ public class BusSpecificStopView extends LinearLayout implements OnClickListener this.stop = stop; - // if (stop.busStop.description.length() > 20) { - // location.setText(stop.busStop.description.substring(0, 20)); // TODO - // } else { - location.setText(stop.busStop.description); // TODO - // } + location.setText(stop.busStop.description); + + if (stop instanceof StopLoading) { + timeProgress.setVisibility(View.VISIBLE); + } else { + timeProgress.setVisibility(View.GONE); + } if (stop.arivalTime != null) { // Time available time.setText(stop.getShortTimeToArival()); time.setVisibility(View.VISIBLE); - timeProgress.setVisibility(View.GONE); - } else if (stop instanceof StopLoading) { - timeProgress.setVisibility(View.VISIBLE); } else { // No time available (yet) and not currently loading time.setVisibility(View.GONE); time.setText(""); - timeProgress.setVisibility(View.GONE); } DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); @@ -117,10 +115,10 @@ public class BusSpecificStopView extends LinearLayout implements OnClickListener if (stop.arivalTime != null) { if (stop.live) { - onClickMessage = stop.bus.getName() + " at " + onClickMessage = stop.bus.getName() + " at " + stop.busStop.description + " at " + DateFormat.getTimeInstance(DateFormat.SHORT).format(stop.arivalTime); } else { - onClickMessage = stop.bus.getName() + " at " + onClickMessage = stop.bus.getName() + " at " + stop.busStop.description + " at " + DateFormat.getTimeInstance(DateFormat.SHORT).format(stop.arivalTime) + " (timetabled)"; } } else { diff --git a/src/net/cbaines/suma/DataManager.java b/src/net/cbaines/suma/DataManager.java index 99da450..6f35453 100644 --- a/src/net/cbaines/suma/DataManager.java +++ b/src/net/cbaines/suma/DataManager.java @@ -187,13 +187,9 @@ public class DataManager { /* * Polygon poly = buildingPolys.get(dataBits[1]); * - * if (poly != null) { bdg.outline = poly; // Log.i(TAG, - * "Adding building " + key + " " + - * bdg.point.getLatitudeE6() + " " + - * bdg.point.getLongitudeE6() + " " + poly); } else { // - * Log.i(TAG, "Adding building " + key + " " + - * bdg.point.getLatitudeE6() + " " + - * bdg.point.getLongitudeE6()); } + * if (poly != null) { bdg.outline = poly; // Log.i(TAG, "Adding building " + key + " " + + * bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6() + " " + poly); } else { // Log.i(TAG, + * "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6()); } */ // Log.i(TAG, "Creating building " + bdg.id + " " + bdg.name @@ -217,13 +213,9 @@ public class DataManager { /* * Polygon poly = buildingPolys.get(dataBits[1]); * - * if (poly != null) { bdg.outline = poly; // Log.i(TAG, - * "Adding building " + key + " " + - * bdg.point.getLatitudeE6() + " " + - * bdg.point.getLongitudeE6() + " " + poly); } else { // - * Log.i(TAG, "Adding building " + key + " " + - * bdg.point.getLatitudeE6() + " " + - * bdg.point.getLongitudeE6()); } + * if (poly != null) { bdg.outline = poly; // Log.i(TAG, "Adding building " + key + " " + + * bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6() + " " + poly); } else { // Log.i(TAG, + * "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6()); } */ // Log.i(TAG, "Creating building " + bdg.id + " " + bdg.name @@ -242,17 +234,13 @@ public class DataManager { } /* - * for (Iterator iter = buildingPoints.keySet().iterator(); - * iter.hasNext();) { String key = iter.next(); + * for (Iterator iter = buildingPoints.keySet().iterator(); iter.hasNext();) { String key = iter.next(); * - * Building bdg = new Building(key, buildingPoints.get(key), false); - * Polygon poly = buildingPolys.get(key); + * Building bdg = new Building(key, buildingPoints.get(key), false); Polygon poly = buildingPolys.get(key); * - * if (poly != null) { bdg.outline = poly; // Log.i(TAG, - * "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + - * bdg.point.getLongitudeE6() + " " + poly); } else { // Log.i(TAG, - * "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + - * bdg.point.getLongitudeE6()); } + * if (poly != null) { bdg.outline = poly; // Log.i(TAG, "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + * + bdg.point.getLongitudeE6() + " " + poly); } else { // Log.i(TAG, "Adding building " + key + " " + + * bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6()); } * * buildingDao.create(bdg); } */ @@ -289,18 +277,18 @@ public class DataManager { // Log.i(TAG, "Whole " + dataBits[3] + " First bit " + // quBitsLat[0] + " last bit " + quBitsLat[1]); - double lat = Double.valueOf(quBitsLat[0]) - + Double.valueOf(quBitsLat[1].substring(0, quBitsLat[1].length() - 1)) / 60d; + double lat = Double.valueOf(quBitsLat[0]) + Double.valueOf(quBitsLat[1].substring(0, quBitsLat[1].length() - 1)) + / 60d; // Log.i(TAG, "Whole " + dataBits[4] + " First bit " + // quBitsLng[0] + " last bit " + quBitsLng[1]); - double lng = Double.valueOf(quBitsLng[0]) - + Double.valueOf(quBitsLng[1].substring(0, quBitsLng[1].length() - 1)) / 60d; + double lng = Double.valueOf(quBitsLng[0]) + Double.valueOf(quBitsLng[1].substring(0, quBitsLng[1].length() - 1)) + / 60d; GeoPoint point = new GeoPoint((int) (lat * 1e6), (int) (lng * -1e6)); // Log.i(TAG, "Lat " + point.getLatitudeE6() + " lng " + // point.getLongitudeE6()); - busStopDao.create(new BusStop(dataBits[0].replace("\"", ""), dataBits[1].replace("\"", ""), dataBits[2] - .replace("\"", ""), point)); + busStopDao.create(new BusStop(dataBits[0].replace("\"", ""), dataBits[1].replace("\"", ""), dataBits[2].replace( + "\"", ""), point)); } @@ -421,36 +409,26 @@ public class DataManager { } /* - * for (Iterator busStopIter = busStopDao.iterator(); - * busStopIter.hasNext();) { BusStop stop = busStopIter.next(); // - * Log.i(TAG, "Looking at stop " + stop.id); + * for (Iterator busStopIter = busStopDao.iterator(); busStopIter.hasNext();) { BusStop stop = + * busStopIter.next(); // Log.i(TAG, "Looking at stop " + stop.id); * * - * QueryBuilder routeStopsQueryBuilder = - * routeStopsDao.queryBuilder(); + * QueryBuilder routeStopsQueryBuilder = routeStopsDao.queryBuilder(); * routeStopsQueryBuilder.where().eq(columnName, value) * - * DeleteBuilder deleteBuilder = - * busStopDao.deleteBuilder(); // only delete the rows where password is - * null deleteBuilder.where().in(RouteStops.STOP_ID_FIELD_NAME, objects) - * accountDao.delete(deleteBuilder.prepare()); + * DeleteBuilder deleteBuilder = busStopDao.deleteBuilder(); // only delete the rows where password is + * null deleteBuilder.where().in(RouteStops.STOP_ID_FIELD_NAME, objects) accountDao.delete(deleteBuilder.prepare()); * * - * QueryBuilder routeStopsQueryBuilder = - * routeStopsDao.queryBuilder(); - * routeStopsQueryBuilder.setCountOf(true); - * routeStopsQueryBuilder.where().eq(RouteStops.STOP_ID_FIELD_NAME, - * stop); + * QueryBuilder routeStopsQueryBuilder = routeStopsDao.queryBuilder(); + * routeStopsQueryBuilder.setCountOf(true); routeStopsQueryBuilder.where().eq(RouteStops.STOP_ID_FIELD_NAME, stop); * - * PreparedQuery routeStopsPreparedQuery = - * routeStopsQueryBuilder.prepare(); List routeStops = - * routeStopsDao.query(routeStopsPreparedQuery); // long num = - * routeStopsDao.query(routeStopsPreparedQuery).size(); // Log.i(TAG, - * "Number is " + num); + * PreparedQuery routeStopsPreparedQuery = routeStopsQueryBuilder.prepare(); List routeStops = + * routeStopsDao.query(routeStopsPreparedQuery); // long num = routeStopsDao.query(routeStopsPreparedQuery).size(); // + * Log.i(TAG, "Number is " + num); * - * stop.uniLink = false; for (RouteStops routeStop : routeStops) { if - * (routeStop.busRoute.uniLink) { stop.uniLink = true; } } - * busStopDao.update(stop); } + * stop.uniLink = false; for (RouteStops routeStop : routeStops) { if (routeStop.busRoute.uniLink) { stop.uniLink = true; + * } } busStopDao.update(stop); } */ Log.i(TAG, "Finished loading bus data"); @@ -631,8 +609,7 @@ public class DataManager { // " stops matching the destStop " + destStop + " on route " + // route.code); } else { - Log.w(TAG, "Found " + routeStops.size() + " stops matching the destStop " + destStop + " on route " - + route.code); + Log.w(TAG, "Found " + routeStops.size() + " stops matching the destStop " + destStop + " on route " + route.code); } } @@ -719,8 +696,7 @@ public class DataManager { busRoutes.add(route); } else { Log.e(TAG, "Route not found " + key.substring(key.length() - 3, key.length()) + " " + key); - throw new RuntimeException("Route not found " + key.substring(key.length() - 3, key.length()) + " " - + key); + throw new RuntimeException("Route not found " + key.substring(key.length() - 3, key.length()) + " " + key); } } @@ -890,8 +866,8 @@ public class DataManager { return stop; } - public static Stop getStop(Context context, Bus bus, int busStopRouteIndex) throws SQLException, - ClientProtocolException, IOException, JSONException { + public static Stop getStop(Context context, Bus bus, int busStopRouteIndex) throws SQLException, ClientProtocolException, + IOException, JSONException { if (helper == null) helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); @@ -915,13 +891,15 @@ public class DataManager { if (bus.direction.equals("E")) { direction = "E"; } else { - if (busStopRouteIndex >= 1 && busStopRouteIndex <= 36) { + Log.i(TAG, "Direction for " + busStopRouteIndex + " is"); + if (busStopRouteIndex >= 0 && busStopRouteIndex <= 36) { direction = route.forwardDirection; - } else if (busStopRouteIndex >= 51 && busStopRouteIndex <= 88) { + } else if (busStopRouteIndex >= 50 && busStopRouteIndex <= 87) { direction = route.reverseDirection; } else { Log.e(TAG, "For U1 route, error with busStopRouteIndex " + busStopRouteIndex); } + Log.i(TAG, direction); } } else if (route.id == 468) { // U1N // Nothing to do -- cgit v1.2.3