diff options
-rw-r--r-- | src/net/cbaines/suma/BusActivity.java | 71 | ||||
-rw-r--r-- | src/net/cbaines/suma/BusRoute.java | 116 | ||||
-rw-r--r-- | src/net/cbaines/suma/DataManager.java | 13 | ||||
-rw-r--r-- | src/net/cbaines/suma/RouteStops.java | 8 |
4 files changed, 156 insertions, 52 deletions
diff --git a/src/net/cbaines/suma/BusActivity.java b/src/net/cbaines/suma/BusActivity.java index f68c051..d632298 100644 --- a/src/net/cbaines/suma/BusActivity.java +++ b/src/net/cbaines/suma/BusActivity.java @@ -60,7 +60,7 @@ public class BusActivity extends OrmLiteBaseActivity<DatabaseHelper> implements private Context instance; // BusStops and if they are being updated by the handler - ArrayList<BusStop> busStops; + List<BusStop> busStops; ArrayList<Boolean> busStopsActive; private HashMap<BusStop, GetTimetableStopTask> tasks = new HashMap<BusStop, GetTimetableStopTask>(); @@ -89,14 +89,16 @@ public class BusActivity extends OrmLiteBaseActivity<DatabaseHelper> implements helper.getBusRouteDao().refresh(bus.route); - List<BusStop> busStops = helper.getBusStopDao().queryForEq(BusStop.ID_FIELD_NAME, busStopID); busStop = null; - if (busStops.size() == 0) { - Log.e(TAG, "BusStop " + busStopID + " not found!"); - } else if (busStops.size() == 1) { - busStop = busStops.get(0); - } else if (busStops.size() > 1) { - Log.e(TAG, "Found more than one busStop? " + busStopID); + if (busStopID != null) { + List<BusStop> busStops = helper.getBusStopDao().queryForEq(BusStop.ID_FIELD_NAME, busStopID); + if (busStops.size() == 0) { + Log.e(TAG, "BusStop " + busStopID + " not found!"); + } else if (busStops.size() == 1) { + busStop = busStops.get(0); + } else if (busStops.size() > 1) { + Log.e(TAG, "Found more than one busStop? " + busStopID); + } } U1RouteTextView = (TextView) findViewById(R.id.busActivityU1); @@ -151,11 +153,12 @@ public class BusActivity extends OrmLiteBaseActivity<DatabaseHelper> implements e.printStackTrace(); } - busStops = new ArrayList<BusStop>(); - busStops.add(busStop); - - busStopsActive = new ArrayList<Boolean>(); - busStopsActive.add(false); + busStops = bus.route.getRouteSection(instance, bus.direction); + Log.i(TAG, "Got " + busStops.size() + " bus stops for this bus"); + busStopsActive = new ArrayList<Boolean>(busStops.size()); + for (int i = 0; i < busStops.size(); i++) { + busStopsActive.add(false); + } if (bus.destination != null) { Log.i(TAG, "Bus destination is " + bus.destination); @@ -163,21 +166,15 @@ public class BusActivity extends OrmLiteBaseActivity<DatabaseHelper> implements Log.i(TAG, "Bus destination is null"); } - for (int i = 0;; i++) { - BusStop nextStop = bus.route.moveInRoute(instance, busStops.get(i), bus.direction, 1); - - if (nextStop.equals(busStop) || (bus.destination != null && bus.destination.equals(nextStop))) { - break; - } - - busStops.add(nextStop); - busStopsActive.add(false); - - if (busStops.size() > 50) { - Log.e(TAG, "Got more than 50 bus stops"); - break; - } - } + /* + * for (int i = 0;; i++) { BusStop nextStop = bus.route.moveInRoute(instance, busStops.get(i), bus.direction, 1); + * + * if (nextStop.equals(busStop) || (bus.destination != null && bus.destination.equals(nextStop))) { break; } + * + * busStops.add(nextStop); busStopsActive.add(false); + * + * if (busStops.size() > 50) { Log.e(TAG, "Got more than 50 bus stops"); break; } } + */ refreshData = new Runnable() { @Override @@ -189,20 +186,22 @@ public class BusActivity extends OrmLiteBaseActivity<DatabaseHelper> implements GetTimetableStopTask task = tasks.get(busStops.get(num)); if (stop.timeOfFetch == null || (stop.timeOfFetch.getTime() - System.currentTimeMillis()) > 20000) { + if (task != null) { + if (task.getStatus() == AsyncTask.Status.FINISHED) { + task = null; + } + } + if (task == null) { task = new GetTimetableStopTask(); BusStop[] str = { stop.busStop }; task.execute(str); - } else { - if (task.getStatus() == AsyncTask.Status.FINISHED) { - BusStop[] str = { stop.busStop }; - task.execute(str); - } + tasks.put(stop.busStop, task); } } } } - handler.postDelayed(refreshData, 20000); + handler.postDelayed(refreshData, 50000); } }; @@ -336,6 +335,10 @@ public class BusActivity extends OrmLiteBaseActivity<DatabaseHelper> implements } else { adapter = new BusSpecificTimetableAdapter(this, visibleTimetable); timetableView.setAdapter(adapter); + if (busStop != null) { + Log.i(TAG, "Moving to position of " + busStop.description + " which is " + busStops.indexOf(busStop)); + timetableView.setSelection(busStops.indexOf(busStop)); + } } busActivityContentLayout.setGravity(Gravity.TOP); } diff --git a/src/net/cbaines/suma/BusRoute.java b/src/net/cbaines/suma/BusRoute.java index cdb0e4d..244f56e 100644 --- a/src/net/cbaines/suma/BusRoute.java +++ b/src/net/cbaines/suma/BusRoute.java @@ -160,7 +160,7 @@ public class BusRoute { ArrayList<Integer> stopIndexs = new ArrayList<Integer>(); for (RouteStops routeStop : routeStopsFound) { - if (routeStop.stop.id.equals(busStop.id)) { + if (routeStop.busStop.id.equals(busStop.id)) { stopIndexs.add(routeStop.sequence - 1); } } @@ -175,7 +175,7 @@ public class BusRoute { stopWanted = stopWanted % (routeStopsFound.size() - 1); } Log.v(TAG, " Stop wanted " + stopWanted); - BusStop busStopWanted = routeStopsFound.get(stopWanted).stop; + BusStop busStopWanted = routeStopsFound.get(stopWanted).busStop; busStopDao.refresh(busStopWanted); @@ -189,12 +189,12 @@ public class BusRoute { stopWanted = routeStopsFound.size() - (Math.abs(stopWanted) % routeStopsFound.size()); } Log.v(TAG, "stopWanted " + stopWanted); - busStopDao.refresh(routeStopsFound.get(stopWanted).stop); + busStopDao.refresh(routeStopsFound.get(stopWanted).busStop); - Log.v(TAG, "Moving backwards " + moveAmount + " stops from " + busStop + " to " + routeStopsFound.get(stopWanted).stop + " in route " + Log.v(TAG, "Moving backwards " + moveAmount + " stops from " + busStop + " to " + routeStopsFound.get(stopWanted).busStop + " in route " + this); - busStops.add(routeStopsFound.get(stopWanted).stop); + busStops.add(routeStopsFound.get(stopWanted).busStop); } } @@ -246,7 +246,7 @@ public class BusRoute { int stopIndex = -1; for (RouteStops routeStop : routeStopsFound) { - if (routeStop.stop.id.equals(busStop.id)) { + if (routeStop.busStop.id.equals(busStop.id)) { if (stopIndex == -1) { stopIndex = routeStop.sequence - 1; } else { // ARGH, weird route @@ -333,7 +333,7 @@ public class BusRoute { stopWanted = stopWanted % (routeStopsFound.size() - 1); } Log.v(TAG, " Stop wanted " + stopWanted); - BusStop busStopWanted = routeStopsFound.get(stopWanted).stop; + BusStop busStopWanted = routeStopsFound.get(stopWanted).busStop; busStopDao.refresh(busStopWanted); @@ -347,11 +347,11 @@ public class BusRoute { stopWanted = routeStopsFound.size() - (Math.abs(stopWanted) % routeStopsFound.size()); } Log.v(TAG, "stopWanted " + stopWanted); - busStopDao.refresh(routeStopsFound.get(stopWanted).stop); + busStopDao.refresh(routeStopsFound.get(stopWanted).busStop); - Log.v(TAG, "Moving backwards " + moveAmount + " stops from " + busStop + " to " + routeStopsFound.get(stopWanted).stop + " in route " + this); + Log.v(TAG, "Moving backwards " + moveAmount + " stops from " + busStop + " to " + routeStopsFound.get(stopWanted).busStop + " in route " + this); - return routeStopsFound.get(stopWanted).stop; + return routeStopsFound.get(stopWanted).busStop; } } catch (SQLException e) { @@ -361,6 +361,102 @@ public class BusRoute { return null; } + /** + * Untested? + * + * @param context + * @param busStop + * @param moveAmount + * @return + */ + List<BusStop> getRouteSection(final Context context, String direction) { + + DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); + + if (forwardDirection != null) { + + if (direction != null) { + + if (direction.equals("E")) + direction = "A"; // Quick hack for U1E + } else { + throw new NullPointerException("direction is null"); + } + } + + List<BusStop> busStops = new ArrayList<BusStop>(); + + try { + + Dao<RouteStops, Integer> routeStopsDao = helper.getRouteStopsDao(); + Dao<BusStop, String> busStopDao = helper.getBusStopDao(); + + QueryBuilder<RouteStops, Integer> routeStopsQueryBuilder = routeStopsDao.queryBuilder(); + routeStopsQueryBuilder.where().eq(RouteStops.ROUTE_ID_FIELD_NAME, this.id); + PreparedQuery<RouteStops> routeStopsPreparedQuery = routeStopsQueryBuilder.prepare(); + + List<RouteStops> routeStopsFound = routeStopsDao.query(routeStopsPreparedQuery); + + int startStopSeq = -1; + int endStopSeq = -1; + + if (id == 326) { // U1 + if (direction.equals(forwardDirection)) { + startStopSeq = 1; + endStopSeq = 43; + } else if (direction.equals(reverseDirection)) { + startStopSeq = 44; + endStopSeq = 88; + } else { + Log.e(TAG, "Error, unrecognised direction " + direction); + } + } else if (id == 468) { // U1N + startStopSeq = 1; + endStopSeq = 29; + } else if (id == 329) { // U2 + if (direction.equals(forwardDirection)) { + startStopSeq = 1; + endStopSeq = 22; + } else if (direction.equals(reverseDirection)) { + startStopSeq = 23; + endStopSeq = 43; + } else { + Log.e(TAG, "Error, unrecognised direction " + direction); + } + } else if (id == 327) { // U6 + if (direction.equals(forwardDirection)) { + startStopSeq = 1; + endStopSeq = 44; + } else if (direction.equals(reverseDirection)) { + startStopSeq = 45; + endStopSeq = 93; + } else { + Log.e(TAG, "Error, unrecognised direction " + direction); + } + } else if (id == 354) { // U9 + startStopSeq = 1; + endStopSeq = 74; + } else { + Log.e(TAG, "Error, unrecognised route " + id); + } + + for (RouteStops routeStop : routeStopsFound) { + if (routeStop.sequence >= startStopSeq && routeStop.sequence <= endStopSeq) { + busStopDao.refresh(routeStop.busStop); + busStops.add(routeStop.busStop); + } + } + + return busStops; + + } catch (SQLException e) { + e.printStackTrace(); + } + Log.e(TAG, "Error moving in route"); + return null; + + } + @Override public int hashCode() { final int prime = 31; diff --git a/src/net/cbaines/suma/DataManager.java b/src/net/cbaines/suma/DataManager.java index 4959865..9ccc8f0 100644 --- a/src/net/cbaines/suma/DataManager.java +++ b/src/net/cbaines/suma/DataManager.java @@ -546,7 +546,12 @@ public class DataManager { PreparedQuery<RouteStops> routeStopsPreparedQuery = routeStopsQueryBuilder.prepare(); List<RouteStops> routeStops = routeStopsDao.query(routeStopsPreparedQuery); - Log.i(TAG, "Found " + routeStops.size() + " stops matching the destStop " + destStop + " on route " + route.code); + if (routeStops.size() > 0) { + Log.i(TAG, "Found " + routeStops.size() + " stops matching the destStop " + destStop + " on route " + route.code); + } else { + Log.e(TAG, "Found " + routeStops.size() + " stops matching the destStop " + destStop + " on route " + route.code); + return null; + } Date now = new Date(System.currentTimeMillis()); @@ -847,16 +852,16 @@ public class DataManager { for (int busStop = 0; busStop < routeStops.size() && busStop >= 0; busStop = busStop + moveAmount) { // if (routeStops.get(busStop).stop.equals(startBusStop)) // continue; - startBusStop = routeStops.get(busStop).stop; + startBusStop = routeStops.get(busStop).busStop; busStopDao.refresh(startBusStop); BusStop predictedNextStop = busRoute.moveInRoute(context, startBusStop, direction, moveAmount); BusStop nextStop; if (busStop == routeStops.size() - 1) { - nextStop = routeStops.get(0).stop; + nextStop = routeStops.get(0).busStop; } else { - nextStop = routeStops.get(busStop + 1).stop; + nextStop = routeStops.get(busStop + 1).busStop; } busStopDao.refresh(nextStop); diff --git a/src/net/cbaines/suma/RouteStops.java b/src/net/cbaines/suma/RouteStops.java index 1206153..29fb18c 100644 --- a/src/net/cbaines/suma/RouteStops.java +++ b/src/net/cbaines/suma/RouteStops.java @@ -40,18 +40,18 @@ public class RouteStops { // This is a foreign object which just stores the id from the User object in this table. @DatabaseField(foreign = true, columnName = STOP_ID_FIELD_NAME, indexName = "routestops_routestop_idx") - BusStop stop; + BusStop busStop; // This is a foreign object which just stores the id from the Post object in this table. @DatabaseField(foreign = true, columnName = ROUTE_ID_FIELD_NAME, indexName = "routestops_routestop_idx") - BusRoute route; + BusRoute busRoute; RouteStops() { } public RouteStops(BusStop stop, BusRoute route, int sequence) { - this.stop = stop; - this.route = route; + this.busStop = stop; + this.busRoute = route; this.sequence = sequence; } } |