From 3f10264e8dba0cd972647fbe1379f148b1ee6847 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Thu, 16 Feb 2012 22:35:53 +0000 Subject: More progress on the route movement stuff, identified the bus stops that need addressing manualy, however I need to redefine the concept of direction. As all of the routes that direction applies are circular, direction is only relevent for dealing with the bus stops that are present in the route more than once. --- src/net/cbaines/suma/BusActivity.java | 12 +-- src/net/cbaines/suma/BusRoute.java | 114 +++++++++++++++++---- src/net/cbaines/suma/BusSpecificStopView.java | 21 ++-- .../cbaines/suma/BusSpecificTimetableAdapter.java | 9 ++ src/net/cbaines/suma/DataManager.java | 76 ++++++++++++-- .../suma/SouthamptonUniversityMapActivity.java | 6 ++ src/net/cbaines/suma/Stop.java | 1 + 7 files changed, 197 insertions(+), 42 deletions(-) diff --git a/src/net/cbaines/suma/BusActivity.java b/src/net/cbaines/suma/BusActivity.java index a71cbd3..25f373a 100644 --- a/src/net/cbaines/suma/BusActivity.java +++ b/src/net/cbaines/suma/BusActivity.java @@ -20,7 +20,6 @@ import android.view.Gravity; import android.view.View; import android.widget.LinearLayout; import android.widget.ListView; -import android.widget.ProgressBar; import android.widget.TextView; import com.j256.ormlite.android.apptools.OrmLiteBaseActivity; @@ -48,7 +47,7 @@ public class BusActivity extends OrmLiteBaseActivity implements */ private BusStop busStop; - private Runnable refreshData; + Runnable refreshData; protected Timetable timetable; private Timetable visibleTimetable; @@ -63,7 +62,7 @@ public class BusActivity extends OrmLiteBaseActivity implements private HashMap tasks = new HashMap(); - private Handler handler; + Handler handler; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -180,17 +179,16 @@ public class BusActivity extends OrmLiteBaseActivity implements 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); } } } } } + handler.postDelayed(refreshData, 20000); } }; @@ -213,12 +211,13 @@ public class BusActivity extends OrmLiteBaseActivity implements for (int i = 0; i < busStops.size(); i++) { timetable.add(new Stop(bus, busStops.get(i), null, null, false)); } + Log.v(TAG, "Finished adding placeholder stops"); } else { Log.i(TAG, "Displaying previous timetable"); } displayTimetable(timetable); - handler.post(refreshData); + handler.postDelayed(refreshData, 500); } else { Log.i(TAG, "Live Times Disabled"); @@ -256,6 +255,7 @@ public class BusActivity extends OrmLiteBaseActivity implements protected Stop doInBackground(BusStop... busStopArray) { busStop = busStopArray[0]; + position = busStops.indexOf(busStop); Stop stop = null; try { diff --git a/src/net/cbaines/suma/BusRoute.java b/src/net/cbaines/suma/BusRoute.java index 0101ae4..94e8e37 100644 --- a/src/net/cbaines/suma/BusRoute.java +++ b/src/net/cbaines/suma/BusRoute.java @@ -113,13 +113,18 @@ public class BusRoute { * Untested? * * @param context - * @param stop + * @param busStop * @param moveAmount * @return */ - BusStop moveInRoute(Context context, BusStop stop, String direction, int moveAmount) { + BusStop moveInRoute(final Context context, final BusStop busStop, String direction, int moveAmount) { + + if (!code.startsWith("U") || code.equals("U9")) { + throw new RuntimeException("Route Movement not supported for " + code); + } + if (moveAmount == 0) { - return stop; + return busStop; } DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); @@ -128,15 +133,8 @@ public class BusRoute { if (direction != null) { - if (forwardDirection.equals(direction)) { - - } else if (reverseDirection.equals(direction)) { - moveAmount = -moveAmount; - } else { - Log.e("BusRoute", "Direction (" + direction + ") doesnt match either the forward direction (" + forwardDirection - + ") or reverse direction (" + reverseDirection + ")"); - return null; - } + if (direction.equals("E")) + direction = "A"; // Quick hack for U1E } } @@ -150,29 +148,102 @@ public class BusRoute { List routeStopsFound = routeStopsDao.query(routeStopsPreparedQuery); - int stopIndex = 0; + int stopIndex = -1; for (RouteStops routeStop : routeStopsFound) { - if (routeStop.stop.id.equals(stop.id)) { - stopIndex = routeStop.sequence - 1; + if (routeStop.stop.id.equals(busStop.id)) { + if (stopIndex == -1) { + stopIndex = routeStop.sequence - 1; + } else { // ARGH, weird route + if (busStop.id.equals("HAA13651") && id == 327) { // U6 by Wessex Lane + if (direction.equals(forwardDirection)) { + stopIndex = 23; + } else { + stopIndex = 68; + } + } else if (busStop.id.equals("SN120134") && id == 327) { // U6 opposite the Stile + if (direction.equals(forwardDirection)) { + stopIndex = 30; + } else { + stopIndex = 59; + } + } else if (busStop.id.equals("SN120163") && id == 327) { // U6 just up past wessex lane + if (direction.equals(forwardDirection)) { + stopIndex = 22; + } else { + stopIndex = 67; + } + } else if (busStop.id.equals("SNA19482") && id == 327) { // U6 General Hosp West Door + if (moveAmount > 0) { + stopIndex = 44; + } else { + stopIndex = 43; + } + } else if (busStop.id.equals("SN120134") && id == 329) { // U2 opposite the Stile + if (direction.equals(forwardDirection)) { + stopIndex = 13; + } else { + stopIndex = 30; + } + } else if (busStop.id.equals("SN120527") && id == 329) { // U2 Civic Centre Rd os stop AO Civic Ctr E + if (moveAmount > 0) { + stopIndex = 0; + } else { + stopIndex = 42; + } + } else if (busStop.id.equals("SNA09298") && id == 329) { // U2 Bassett Green Rd nr Bassett Green Cl SE + if (moveAmount > 0) { + stopIndex = 22; + } else { + stopIndex = 21; + } + } else if (busStop.id.equals("SN120520") && id == 326) { // U1 By the station + if (direction.equals(forwardDirection)) { + stopIndex = 7; + } else { + stopIndex = 80; + } + } else if (busStop.id.equals("HA030183") && id == 326) { // U1 Up past Wessex Lane + if (direction.equals(forwardDirection)) { + stopIndex = 35; + } else { + stopIndex = 50; + } + } else if (busStop.id.equals("HA030212") && id == 326) { // U1 At Eastleigh + if (moveAmount > 0) { + stopIndex = 43; + } else { + stopIndex = 42; + } + } else if (busStop.id.equals("SN120171") && id == 354) { // U9 + if (moveAmount > 0) { + stopIndex = 0; + } else { + stopIndex = 73; + } + } else { + Log.e(TAG, "Error, unknown bus stop " + busStop.id + " (" + busStop.description + ") that appears mutiple times in " + toString()); + throw new RuntimeException("Error, unknown bus stop " + busStop.id + " that appears mutiple times in " + toString()); + } + } } } if (moveAmount > 0) { Log.v(TAG, - "Moving forward in direction " + direction + " " + moveAmount + " stops from " + stop + " (" + stopIndex + "/" + routeStopsFound.size() - + ")"); + "Moving forward in direction " + direction + " " + moveAmount + " stops from " + busStop + " (" + stopIndex + "/" + + routeStopsFound.size() + ")"); int stopWanted = stopIndex + moveAmount; if ((stopWanted + 1) > routeStopsFound.size()) { Log.v(TAG, "Off the end of the route"); stopWanted = stopWanted % (routeStopsFound.size() - 1); } - Log.v(TAG, "Stop wanted " + stopWanted); + Log.v(TAG, " Stop wanted " + stopWanted); BusStop busStopWanted = routeStopsFound.get(stopWanted).stop; busStopDao.refresh(busStopWanted); - Log.v(TAG, "Moving to " + busStopWanted + " (" + stopWanted + ") in route " + this); + Log.v(TAG, " Moving to " + busStopWanted + " (" + stopWanted + ") in route " + this); return busStopWanted; } else { @@ -184,8 +255,9 @@ public class BusRoute { Log.v(TAG, "stopWanted " + stopWanted); busStopDao.refresh(routeStopsFound.get(stopWanted).stop); - Log.v(TAG, "Moving backwards in direction " + direction + " " + moveAmount + " stops from " + stop + " to " - + routeStopsFound.get(stopWanted).stop + " in route " + this); + Log.v(TAG, + "Moving backwards in direction " + direction + " " + moveAmount + " stops from " + busStop + " to " + + routeStopsFound.get(stopWanted).stop + " in route " + this); return routeStopsFound.get(stopWanted).stop; } diff --git a/src/net/cbaines/suma/BusSpecificStopView.java b/src/net/cbaines/suma/BusSpecificStopView.java index 88a403c..cad7524 100644 --- a/src/net/cbaines/suma/BusSpecificStopView.java +++ b/src/net/cbaines/suma/BusSpecificStopView.java @@ -25,7 +25,7 @@ import java.text.DateFormat; import android.app.Activity; import android.content.Context; import android.content.Intent; -import android.os.Handler; +import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; @@ -39,11 +39,7 @@ import com.j256.ormlite.dao.Dao; public class BusSpecificStopView extends LinearLayout implements OnClickListener, OnLongClickListener { - // private static final String TAG = "BusSpecificStopView"; - - private Handler handler; - - private Runnable refreshData; + private static final String TAG = "BusSpecificStopView"; // private static final String TAG = "StopView"; @@ -54,7 +50,7 @@ public class BusSpecificStopView extends LinearLayout implements OnClickListener private Stop stop; - public BusSpecificStopView(Context context, Stop newStop) { + public BusSpecificStopView(Context context, Stop stop) { super(context); this.context = context; @@ -73,7 +69,6 @@ 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)); - handler.post(refreshData); } public void setStop(Stop stop) { @@ -82,6 +77,16 @@ public class BusSpecificStopView extends LinearLayout implements OnClickListener this.stop = stop; + if (stop == null) { + Log.e(TAG, "stop == null"); + } + if (stop.busStop == null) { + Log.e(TAG, "stop.busStop == null"); + } + if (stop.busStop.description == null) { + Log.e(TAG, "stop.busStop.description == null"); + } + if (stop.busStop.description.length() > 20) { location.setText(stop.busStop.description.substring(0, 20)); // TODO } else { diff --git a/src/net/cbaines/suma/BusSpecificTimetableAdapter.java b/src/net/cbaines/suma/BusSpecificTimetableAdapter.java index 4cc894f..469fa56 100644 --- a/src/net/cbaines/suma/BusSpecificTimetableAdapter.java +++ b/src/net/cbaines/suma/BusSpecificTimetableAdapter.java @@ -32,6 +32,8 @@ public class BusSpecificTimetableAdapter extends BaseAdapter { private final Animation a; private boolean[] changed; + private long timeOfLastForcedUpdate = System.currentTimeMillis(); + // private static final String TAG = "BusSpecificTimetableAdapter"; public BusSpecificTimetableAdapter(BusActivity context, Timetable timetable) { @@ -43,6 +45,13 @@ public class BusSpecificTimetableAdapter extends BaseAdapter { public View getView(int position, View convertView, ViewGroup parent) { // Log.i(TAG, "Returning stop " + position + " " + timetable.get(position)); + context.busStopsActive.set(position, true); + + if (timeOfLastForcedUpdate + 1000 < System.currentTimeMillis()) { + context.handler.post(context.refreshData); + timeOfLastForcedUpdate = System.currentTimeMillis(); + } + BusSpecificStopView stopView; if (convertView == null) { stopView = new BusSpecificStopView(context, timetable.get(position)); diff --git a/src/net/cbaines/suma/DataManager.java b/src/net/cbaines/suma/DataManager.java index 2af4b52..f065b07 100644 --- a/src/net/cbaines/suma/DataManager.java +++ b/src/net/cbaines/suma/DataManager.java @@ -269,13 +269,13 @@ public class DataManager { route = new BusRoute(id, dataBits[1], dataBits[2].replace("\"", ""), uniLink); - if (id == 326) { - route.forwardDirection = "C"; - route.reverseDirection = "A"; - } else if (id == 329) { - route.forwardDirection = "C"; - route.reverseDirection = "B"; - } else if (id == 327) { + if (id == 326) { // U1 + route.forwardDirection = "A"; + route.reverseDirection = "C"; + } else if (id == 329) { // U2 + route.forwardDirection = "B"; + route.reverseDirection = "C"; + } else if (id == 327) { // U6 route.forwardDirection = "H"; route.reverseDirection = "C"; } @@ -801,4 +801,66 @@ public class DataManager { return builder.toString(); } + + public static void routeMovementTest(Context context) throws SQLException { + + Dao routeStopsDao = null; + + if (helper == null) + helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); + if (routeStopsDao == null) + routeStopsDao = helper.getRouteStopsDao(); + if (busRouteDao == null) + busRouteDao = helper.getBusRouteDao(); + if (busStopDao == null) + busStopDao = helper.getBusStopDao(); + + for (BusRoute busRoute : busRouteDao) { + if (!busRoute.code.startsWith("U") || busRoute.code.equals("U9")) { + continue; + } + List routeStops = routeStopsDao.queryForEq(RouteStops.ROUTE_ID_FIELD_NAME, busRoute.id); + + ArrayList directions = new ArrayList(); + if (busRoute.forwardDirection != null) { + directions.add(busRoute.forwardDirection); + } + if (busRoute.reverseDirection != null) { + directions.add(busRoute.reverseDirection); + } + if (directions.size() == 0) { + directions.add(null); + } + + BusStop startBusStop = null; + + for (int moveAmount = 1; moveAmount >= -1; moveAmount = moveAmount - 2) { + for (String direction : directions) { + 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; + busStopDao.refresh(startBusStop); + + BusStop predictedNextStop = busRoute.moveInRoute(context, startBusStop, direction, moveAmount); + + BusStop nextStop; + if (busStop == routeStops.size() - 1) { + nextStop = routeStops.get(0).stop; + } else { + nextStop = routeStops.get(busStop + 1).stop; + } + busStopDao.refresh(nextStop); + + if (!nextStop.equals(predictedNextStop) && !startBusStop.equals(nextStop)) { + Log.e(TAG, "predicted: " + predictedNextStop + " next: " + nextStop); + Log.e(TAG, startBusStop.id + " " + nextStop.id); + return; + } + } + } + } + } + + } } diff --git a/src/net/cbaines/suma/SouthamptonUniversityMapActivity.java b/src/net/cbaines/suma/SouthamptonUniversityMapActivity.java index 26996a0..d4754a4 100644 --- a/src/net/cbaines/suma/SouthamptonUniversityMapActivity.java +++ b/src/net/cbaines/suma/SouthamptonUniversityMapActivity.java @@ -384,6 +384,12 @@ public class SouthamptonUniversityMapActivity extends OrmLiteBaseActivity