diff options
author | Christopher Baines <cbaines8@gmail.com> | 2012-01-25 13:53:22 +0000 |
---|---|---|
committer | Christopher Baines <cbaines8@gmail.com> | 2012-01-25 13:53:22 +0000 |
commit | abcbffd80637a8aea379e6a774aa5747fd5cd0c7 (patch) | |
tree | e6912fb2fe412fd36b989af26be1051dbbf1a27e /src | |
parent | de2979e0f22264ada8326833bfb39746e9bf78cc (diff) | |
download | southamptonuniversitymap-abcbffd80637a8aea379e6a774aa5747fd5cd0c7.tar southamptonuniversitymap-abcbffd80637a8aea379e6a774aa5747fd5cd0c7.tar.gz |
Lots of changes, need to regenerate the database, and add code to replace old databases with the current one.
Diffstat (limited to 'src')
-rw-r--r-- | src/net/cbaines/suma/Bus.java | 73 | ||||
-rw-r--r-- | src/net/cbaines/suma/BusRoute.java | 103 | ||||
-rw-r--r-- | src/net/cbaines/suma/BusStop.java | 24 | ||||
-rw-r--r-- | src/net/cbaines/suma/BusStopActivity.java | 49 | ||||
-rw-r--r-- | src/net/cbaines/suma/BusStopOverlay.java | 85 | ||||
-rw-r--r-- | src/net/cbaines/suma/DataManager.java | 173 | ||||
-rw-r--r-- | src/net/cbaines/suma/DatabaseHelper.java | 2 | ||||
-rw-r--r-- | src/net/cbaines/suma/Direction.java | 5 | ||||
-rw-r--r-- | src/net/cbaines/suma/SouthamptonUniversityMapActivity.java | 17 | ||||
-rw-r--r-- | src/net/cbaines/suma/Stop.java | 80 | ||||
-rw-r--r-- | src/net/cbaines/suma/StopView.java | 2 |
11 files changed, 291 insertions, 322 deletions
diff --git a/src/net/cbaines/suma/Bus.java b/src/net/cbaines/suma/Bus.java index 426ee3b..533c43c 100644 --- a/src/net/cbaines/suma/Bus.java +++ b/src/net/cbaines/suma/Bus.java @@ -22,43 +22,90 @@ package net.cbaines.suma; import com.j256.ormlite.field.DatabaseField; import com.j256.ormlite.table.DatabaseTable; +/** + * Represents a bus + * + * @author Christopher Baines <cbaines8@gmail.com> + * + */ @DatabaseTable(tableName = "buses") public class Bus { - @DatabaseField(id = true) - public int id; + final static String ID_FIELD_NAME = "id"; + final static String ROUTE_FIELD_NAME = "route"; + final static String DIRECTION_FIELD_NAME = "direction"; - @DatabaseField(canBeNull = true, foreign = true) - Stop lastKnownStop; + @DatabaseField(generatedId = true) + int gid; + + /** + * The identification number of the bus. + */ + @DatabaseField(canBeNull = true) + String id; + /** + * The route the bus is travelling. + */ + @DatabaseField(canBeNull = false, foreign = true) + BusRoute route; + + /** + * The direction which the bus is travelling. + */ @DatabaseField(canBeNull = true, foreign = true) - Stop firstKnownStop; + Direction direction; + /** + * The destination the bus is travelling towards. + */ @DatabaseField(canBeNull = false, foreign = true) - BusRoute lastKnownRoute; + BusStop destination; Bus() { } - Bus(int id, BusRoute lastKnownRoute, Stop lastKnownStop) { + /** + * Create a bus. + * + * @param id + * The identification number of the bus. + * @param route + * The route the bus is travelling. + * @param dir + * The direction which the bus is travelling. + */ + Bus(String id, BusRoute route, Direction dir) { this.id = id; - this.lastKnownRoute = lastKnownRoute; - this.lastKnownStop = lastKnownStop; + this.route = route; + this.direction = dir; } - public Bus(int id, BusRoute lastKnownRoute) { - this(id, lastKnownRoute, null); + /** + * Create a bus. + * + * @param id + * The identification number of the bus. + * @param route + * The route the bus is travelling. + */ + Bus(String id, BusRoute route) { + this(id, route, null); } public String toString() { - return String.valueOf(id); + return String.valueOf(id + " (" + route.code + direction + ")"); + } + + String getName() { + return route.code + direction; } @Override public int hashCode() { final int prime = 31; int result = 1; - result = prime * result + id; + result = prime * result + gid; return result; } diff --git a/src/net/cbaines/suma/BusRoute.java b/src/net/cbaines/suma/BusRoute.java index 43a5375..f095fb1 100644 --- a/src/net/cbaines/suma/BusRoute.java +++ b/src/net/cbaines/suma/BusRoute.java @@ -20,42 +20,47 @@ package net.cbaines.suma; import java.sql.SQLException; +import java.util.HashSet; import java.util.List; import android.content.Context; -import android.util.Log; import com.j256.ormlite.android.apptools.OpenHelperManager; import com.j256.ormlite.dao.Dao; import com.j256.ormlite.field.DatabaseField; +import com.j256.ormlite.field.ForeignCollectionField; import com.j256.ormlite.stmt.PreparedQuery; import com.j256.ormlite.stmt.QueryBuilder; import com.j256.ormlite.table.DatabaseTable; -@DatabaseTable(tableName = "busroutes") /** - * This class represents a bus route (U1, U1N, ..). + * This class represents a bus route (U1, U1N, ..). * * @author Christopher Baines <cbaines8@gmail.com> - * + * */ +@DatabaseTable(tableName = "busroutes") public class BusRoute { final static String ID_FIELD_NAME = "id"; final static String CODE_FIELD_NAME = "code"; final static String LABEL_FIELD_NAME = "label"; - private static final String TAG = "BusRoute"; - @DatabaseField(id = true) public int id; + /** + * The route code (U1, U1N, ...) + */ @DatabaseField public String code; @DatabaseField String label; + @ForeignCollectionField(eager = false) + HashSet<Direction> directions; + BusRoute() { } @@ -76,8 +81,8 @@ public class BusRoute { * @param stop * @return */ - BusStop getBusStopBefore(Context context, Stop stop) { - return moveInRoute(context, stop, -1); + BusStop getBusStopBefore(Context context, BusStop stop, Direction dir) { + return moveInRoute(context, stop, dir, -1); } /** @@ -87,8 +92,8 @@ public class BusRoute { * @param stop * @return */ - BusStop getStopAfter(Context context, Stop stop) { - return moveInRoute(context, stop, 1); + BusStop getStopAfter(Context context, BusStop stop, Direction dir) { + return moveInRoute(context, stop, dir, 1); } /** @@ -99,88 +104,40 @@ public class BusRoute { * @param moveAmount * @return */ - BusStop moveInRoute(Context context, Stop stop, int moveAmount) { + BusStop moveInRoute(Context context, BusStop stop, Direction dir, int moveAmount) { DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); try { Dao<RouteStops, Integer> routeStopsDao = helper.getRouteStopsDao(); - // Dao<Stop, Integer> stopDao = helper.getStopDao(); Dao<BusStop, String> busStopDao = helper.getBusStopDao(); - int stopSeq; - int beforeStopSeq = -1; - QueryBuilder<RouteStops, Integer> routeStopsQueryBuilder = routeStopsDao.queryBuilder(); - routeStopsQueryBuilder.where().eq(RouteStops.ROUTE_ID_FIELD_NAME, this.id).and().eq(RouteStops.STOP_ID_FIELD_NAME, stop.busStop.id); - PreparedQuery<RouteStops> routeStopsPreparedQuery = routeStopsQueryBuilder.prepare(); - - List<RouteStops> stopsFound = routeStopsDao.query(routeStopsPreparedQuery); - if (stopsFound.size() != 0) { - Log.e(TAG, "Wierd, found more than one stop"); - return null; - } - - long maxSeq = 0; - - routeStopsQueryBuilder = routeStopsDao.queryBuilder(); routeStopsQueryBuilder.where().eq(RouteStops.ROUTE_ID_FIELD_NAME, this.id); - routeStopsQueryBuilder.setCountOf(true); - routeStopsPreparedQuery = routeStopsQueryBuilder.prepare(); - - maxSeq = routeStopsDao.countOf(routeStopsPreparedQuery); - - if (maxSeq == 0) { - Log.e(TAG, "Something wierd has gone on, maxSeq equals 0"); - return null; - } - - if (id == 326) { // U1 - - if (stop.name.equals("U1C")) {// Seq 0 = End of route - - stopSeq = routeStopsDao.query(routeStopsPreparedQuery).get(0).sequence; - - beforeStopSeq = stopSeq + moveAmount; - - } else if (stop.name.equals("U1A")) { // seq 88 == end of route + PreparedQuery<RouteStops> routeStopsPreparedQuery = routeStopsQueryBuilder.prepare(); - stopSeq = routeStopsDao.query(routeStopsPreparedQuery).get(0).sequence; + List<RouteStops> routeStopsFound = routeStopsDao.query(routeStopsPreparedQuery); - beforeStopSeq = stopSeq - moveAmount; + if (moveAmount > 0) { + for (int i = 0; i < routeStopsFound.size(); i++) { + if (stop.equals(routeStopsFound.get(i))) { + int stopWanted = (i + moveAmount) % (routeStopsFound.size() + 1); + busStopDao.refresh(routeStopsFound.get(stopWanted).stop); - } else { - Log.e(TAG, "In route U1 but " + stop.name + " does not match U1A or U1C"); - return null; + return routeStopsFound.get(stopWanted).stop; + } } } else { - Log.e(TAG, "Route id not recognised " + id); - return null; - } + int maxSeq = routeStopsFound.size() - 1; + int stopWanted = maxSeq - (Math.abs(maxSeq) % (routeStopsFound.size() + 1)); + busStopDao.refresh(routeStopsFound.get(stopWanted).stop); - if (beforeStopSeq == -1) { - Log.e(TAG, "Something wierd has gone on, beforeStopSeq equals -1"); - return null; + return routeStopsFound.get(stopWanted).stop; } - routeStopsQueryBuilder = routeStopsDao.queryBuilder(); - routeStopsQueryBuilder.where().eq(RouteStops.ROUTE_ID_FIELD_NAME, this.id).and().eq(RouteStops.SEQUENCE_ID_FIELD_NAME, beforeStopSeq); - - routeStopsPreparedQuery = routeStopsQueryBuilder.prepare(); - - List<RouteStops> beforeStopsFound = routeStopsDao.query(routeStopsPreparedQuery); - if (stopsFound.size() != 0) { - Log.e(TAG, "Wierd, found more than one before stop"); - return null; - } - - busStopDao.refresh(beforeStopsFound.get(0).stop); - - return beforeStopsFound.get(0).stop; - } catch (SQLException e) { e.printStackTrace(); - return null; } + return null; } @Override diff --git a/src/net/cbaines/suma/BusStop.java b/src/net/cbaines/suma/BusStop.java index 21f2dd9..5ed208f 100644 --- a/src/net/cbaines/suma/BusStop.java +++ b/src/net/cbaines/suma/BusStop.java @@ -19,25 +19,43 @@ package net.cbaines.suma; +import java.util.HashSet; + import org.osmdroid.util.GeoPoint; import com.j256.ormlite.field.DatabaseField; +import com.j256.ormlite.field.ForeignCollectionField; import com.j256.ormlite.table.DatabaseTable; +/** + * Represents a physical bus stop on the map, + * + * @author Christopher Baines <cbaines8@gmail.com> + * + */ @DatabaseTable(tableName = "busstops") public class BusStop extends POI { public static final String DESCRIPTION_FIELD_NAME = "description"; public static final String BAY_FIELD_NAME = "bay"; public static final String ROUTES_FIELD_NAME = "bay"; + /** + * Description e.g. "Bournemouth Rd os Asda S" + */ @DatabaseField(canBeNull = true) public String description; + + /** + * Unknown, "" for all UniLink stops + */ @DatabaseField(canBeNull = true) public String bay; - // Used to speed up accessing the relevent uni link routes for a bus stop, if == 0, this is not a uni link stop - @DatabaseField(canBeNull = false) - public byte routes; + /** + * Used to speed up accessing the relevent uni link routes for a bus stop, this is not a uni link stop + */ + @ForeignCollectionField(eager = false) + public HashSet<BusRoute> routes; public BusStop(String location, String description, String bay, GeoPoint point) { this.id = location; diff --git a/src/net/cbaines/suma/BusStopActivity.java b/src/net/cbaines/suma/BusStopActivity.java index f9e224b..9d5c3c8 100644 --- a/src/net/cbaines/suma/BusStopActivity.java +++ b/src/net/cbaines/suma/BusStopActivity.java @@ -20,16 +20,11 @@ package net.cbaines.suma; import java.sql.SQLException; +import java.util.HashSet; import java.util.Iterator; -import org.osmdroid.util.GeoPoint; - -import android.app.AlertDialog; import android.content.Context; -import android.content.DialogInterface; -import android.content.Intent; import android.content.SharedPreferences; -import android.content.SharedPreferences.Editor; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; @@ -68,6 +63,7 @@ public class BusStopActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme private LinearLayout busTimeContentLayout; protected Timetable timetable; + private Timetable visibleTimetable; protected String busStopID; private String busStopName; @@ -309,6 +305,23 @@ public class BusStopActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme // Handle item selection switch (item.getItemId()) { case R.id.menu_previous_stop: + if (visibleTimetable.size() != 0) { + HashSet<BusRoute> routes = new HashSet<BusRoute>(); + + for (Stop stop : visibleTimetable) { + routes.add(stop.bus.route); + } + + // TODO: Most of the following + + if (routes.size() == 1) { + try { + routes.iterator().next().moveInRoute(this, getHelper().getBusStopDao().queryForId(busStopID), new Direction(), 1); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } break; case R.id.menu_next_stop: break; @@ -331,9 +344,9 @@ public class BusStopActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme } private void displayTimetable(Timetable timetable) { - Timetable timetableToDisplay = (Timetable) timetable.clone(); + visibleTimetable = (Timetable) timetable.clone(); - Log.i(TAG, "It contains " + timetableToDisplay.size() + " stops"); + Log.i(TAG, "It contains " + visibleTimetable.size() + " stops"); if (timetable.size() == 0) { busStopMessage.setText("No Busses"); @@ -341,33 +354,33 @@ public class BusStopActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme busTimeContentLayout.setGravity(Gravity.CENTER); } else { - for (Iterator<Stop> stopIter = timetableToDisplay.iterator(); stopIter.hasNext();) { + for (Iterator<Stop> stopIter = visibleTimetable.iterator(); stopIter.hasNext();) { Stop stop = stopIter.next(); - Log.i(TAG, "Begin filtering, looking at " + stop + " with route " + stop.route.code); - if (stop.route.code.equals("U1")) { + Log.i(TAG, "Begin filtering, looking at " + stop + " with route " + stop.bus.route.code); + if (stop.bus.route.code.equals("U1")) { if (!U1RouteRadioButton.isChecked()) { stopIter.remove(); } - } else if (stop.route.code.equals("U1N")) { + } else if (stop.bus.route.code.equals("U1N")) { if (!U1NRouteRadioButton.isChecked()) { stopIter.remove(); } - } else if (stop.route.code.equals("U2")) { + } else if (stop.bus.route.code.equals("U2")) { if (!U2RouteRadioButton.isChecked()) { stopIter.remove(); } - } else if (stop.route.code.equals("U6")) { + } else if (stop.bus.route.code.equals("U6")) { if (!U6RouteRadioButton.isChecked()) { stopIter.remove(); } - } else if (stop.route.code.equals("U9")) { + } else if (stop.bus.route.code.equals("U9")) { if (!U9RouteRadioButton.isChecked()) { stopIter.remove(); } } } - if (timetableToDisplay.size() == 0) { + if (visibleTimetable.size() == 0) { busTimeContentLayout.setGravity(Gravity.CENTER); busStopMessage.setText("No Busses (With the current enabled routes)"); busStopMessage.setVisibility(View.VISIBLE); @@ -377,9 +390,9 @@ public class BusStopActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme busStopMessage.setVisibility(View.GONE); TimetableAdapter adapter; if ((adapter = (TimetableAdapter) busTimeList.getAdapter()) != null) { - adapter.updateTimetable(timetableToDisplay); + adapter.updateTimetable(visibleTimetable); } else { - adapter = new TimetableAdapter(this, timetableToDisplay); + adapter = new TimetableAdapter(this, visibleTimetable); busTimeList.setAdapter(adapter); } busTimeContentLayout.setGravity(Gravity.TOP); diff --git a/src/net/cbaines/suma/BusStopOverlay.java b/src/net/cbaines/suma/BusStopOverlay.java index e77e862..bc315cb 100644 --- a/src/net/cbaines/suma/BusStopOverlay.java +++ b/src/net/cbaines/suma/BusStopOverlay.java @@ -21,9 +21,10 @@ package net.cbaines.suma; import java.sql.SQLException; import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; import java.util.List; - import org.osmdroid.views.MapView; import org.osmdroid.views.MapView.Projection; import org.osmdroid.views.overlay.Overlay; @@ -67,7 +68,7 @@ public class BusStopOverlay extends Overlay implements RouteColorConstants { private float userScale = 1f; - private boolean[] routes = new boolean[5]; + private HashMap<BusRoute, Boolean> routes = new HashMap<BusRoute, Boolean>(); public BusStopOverlay(Context context) throws SQLException { super(context); @@ -90,8 +91,8 @@ public class BusStopOverlay extends Overlay implements RouteColorConstants { Log.i(TAG, "Finished loading bus stops in to overlay " + (System.currentTimeMillis() - startTime)); } - void setRoutes(int route, boolean visible) { - routes[route] = visible; + void setRoutes(BusRoute route, boolean visible) { + routes.put(route, visible); } @Override @@ -118,21 +119,15 @@ public class BusStopOverlay extends Overlay implements RouteColorConstants { for (int stopNum = 0; stopNum < busStops.size(); stopNum++) { BusStop stop = busStops.get(stopNum); - final byte stopRoutes = stop.routes; byte routeNum = 0; - boolean drawing = false; - for (int i = 0; i < 5; i++) { - if ((stopRoutes & (1 << i)) != 0) { - routeNum++; - if (routes[i]) { - drawing = true; - } + for (Iterator<BusRoute> busRouteIter = stop.routes.iterator(); busRouteIter.hasNext();) { + BusRoute route = busRouteIter.next(); + if (routes.get(route)) { + break; } - } - - if (!drawing) continue; + } int yOfsetPerMarker = (int) (10 * scale); int markerYSize = (int) (8 * scale); @@ -159,35 +154,33 @@ public class BusStopOverlay extends Overlay implements RouteColorConstants { yOfsetPerMarker = (int) (8 * scale); } - for (int i = 0; i < 5; i++) { - if ((stopRoutes & (1 << i)) != 0) { - - // Log.i(TAG, "Route " + route + " is " + routes.get(route)); - - // Log.i(TAG, "Index is " + busRoutes.indexOf(route) + " busRoutes " + busRoutes); - - if (i == 0) { - paint.setColor(U1); - } else if (i == 1) { - paint.setColor(U1N); - } else if (i == 2) { - paint.setColor(U2); - } else if (i == 3) { - paint.setColor(U6); - } else if (i == 4) { - paint.setColor(U9); - } else { - Log.e(TAG, "Unknown route code"); - } + for (BusRoute route : stop.routes) { + + // Log.i(TAG, "Route " + route + " is " + routes.get(route)); - canvas.drawRect(rectLeft, mCurScreenCoords.y + ((yOfsetPerMarker * makersPlaced) - (45 * scale)), rectRight, mCurScreenCoords.y - + (yOfsetPerMarker * makersPlaced) - ((45 * scale) - markerYSize), paint); + // Log.i(TAG, "Index is " + busRoutes.indexOf(route) + " busRoutes " + busRoutes); - makersPlaced++; + if (route.code.equals("U1")) { + paint.setColor(U1); + } else if (route.code.equals("U1N")) { + paint.setColor(U1N); + } else if (route.code.equals("U2")) { + paint.setColor(U2); + } else if (route.code.equals("U6")) { + paint.setColor(U6); + } else if (route.code.equals("U9")) { + paint.setColor(U9); + } else { + Log.e(TAG, "Unknown route code"); } - } + canvas.drawRect(rectLeft, mCurScreenCoords.y + ((yOfsetPerMarker * makersPlaced) - (45 * scale)), rectRight, mCurScreenCoords.y + + (yOfsetPerMarker * makersPlaced) - ((45 * scale) - markerYSize), paint); + + makersPlaced++; + } } + } @Override @@ -291,17 +284,13 @@ public class BusStopOverlay extends Overlay implements RouteColorConstants { pj.toPixels(busStop.point, mItemPoint); if (marker.getBounds().contains(mTouchScreenPoint.x - mItemPoint.x, mTouchScreenPoint.y - mItemPoint.y)) { - boolean drawing = false; - for (int route = 0; route < 5; route++) { - if ((busStop.routes & (1 << route)) != 0) { - if (routes[route]) { - drawing = true; - break; - } + for (Iterator<BusRoute> busRouteIter = busStop.routes.iterator(); busRouteIter.hasNext();) { + BusRoute route = busRouteIter.next(); + if (routes.get(route)) { + break; } - } - if (!drawing) continue; + } return busStop; } diff --git a/src/net/cbaines/suma/DataManager.java b/src/net/cbaines/suma/DataManager.java index d54b25b..461189f 100644 --- a/src/net/cbaines/suma/DataManager.java +++ b/src/net/cbaines/suma/DataManager.java @@ -306,17 +306,8 @@ public class DataManager { routeStopsDao.create(new RouteStops(stop, route, sequence)); - if (route.id == 326) { // U1 - stop.routes = (byte) (stop.routes | 1); - } else if (route.id == 468) { // U1N - stop.routes = (byte) (stop.routes | (1 << 1)); - } else if (route.id == 329) { // U2 - stop.routes = (byte) (stop.routes | (1 << 2)); - } else if (route.id == 327) { // U6 - stop.routes = (byte) (stop.routes | (1 << 3)); - } else if (route.id == 354) { // U9 - stop.routes = (byte) (stop.routes | (1 << 4)); - } + stop.routes.add(route); + Log.v(TAG, "Stop routes " + stop.routes); busStopDao.update(stop); @@ -426,16 +417,13 @@ public class DataManager { if (stopDao == null) stopDao = helper.getStopDao(); - // Log.i(TAG, "Stop " + stopObj); - try { String time = stopObj.getString("time"); GregorianCalendar calender = new GregorianCalendar(); - if (!time.equals("Due")) { - Log.i(TAG, "Time: " + time + " current time " + calender.getTime()); + Log.v(TAG, "Time: " + time + " current time " + calender.getTime()); if (time.contains(":")) { String[] minAndHour = time.split(":"); @@ -446,29 +434,40 @@ public class DataManager { calender.add(Calendar.MINUTE, Integer.parseInt(time.substring(0, time.length() - 1))); } - Log.i(TAG, "Date: " + calender.getTime()); - + Log.v(TAG, "Date: " + calender.getTime()); } - Stop stop; - String name = stopObj.getString("name"); BusRoute route; + Direction dir = null; if (name.equals("U1N")) { route = busRoutes.queryForId(468); - } else if (name.startsWith("U1")) { - route = busRoutes.queryForId(326); - } else if (name.startsWith("U2")) { - route = busRoutes.queryForId(329); - } else if (name.startsWith("U6")) { - route = busRoutes.queryForId(327); } else if (name.startsWith("U9")) { route = busRoutes.queryForId(354); } else { - Log.e(TAG, "Error selecting route for " + name); - return null; + if (name.startsWith("U1")) { + route = busRoutes.queryForId(326); + } else if (name.startsWith("U2")) { + route = busRoutes.queryForId(329); + } else if (name.startsWith("U6")) { + route = busRoutes.queryForId(327); + } else { + Log.e(TAG, "Error detecting route " + name); + return null; + } + + for (Direction possibleDir : route.directions) { + if (name.charAt(3) == possibleDir.direction.charAt(0)) { + dir = possibleDir; + break; + } + } + if (dir.direction.length() == 0) { + Log.e(TAG, "Error detecting direction for " + name); + return null; + } } String destString = stopObj.getString("dest"); @@ -499,52 +498,36 @@ public class DataManager { return null; } + Date now = new Date(System.currentTimeMillis()); + + String busID = null; + Stop stop; + Bus bus; if (stopObj.has("vehicle")) { + busID = stopObj.getString("vehicle"); - int vehicle = Integer.parseInt(stopObj.getString("vehicle")); - // Log.i(TAG, "Looking at vehicle " + vehicle + " (" + stopObj.getString("vehicle") + ")"); - Bus bus = busDao.queryForId(vehicle); - if (bus == null) { - // Log.i(TAG, "Cant find vehicle, creating"); + QueryBuilder<Bus, Integer> busQueryBuilder = busDao.queryBuilder(); + busQueryBuilder.where().eq(Bus.ID_FIELD_NAME, busID); + PreparedQuery<Bus> busPreparedQuery = busQueryBuilder.prepare(); - // for (Bus gotBus : busDao) { - // Log.i(TAG, "Currently have bus " + gotBus.id); - // } + bus = busDao.queryForFirst(busPreparedQuery); - bus = new Bus(vehicle, route); - busDao.create(bus); + if (bus == null) { + bus = new Bus(busID, route, dir); + bus.destination = destStop; + } else { + bus.destination = destStop; + bus.route = route; + bus.direction = dir; } - Date now = new Date(System.currentTimeMillis()); - - stop = new Stop(stopObj.getString("name"), busStop, destStop, bus, calender.getTime(), now); - stop.route = route; - - /* - * if (bus.lastKnownStop != null) { stopDao.delete(bus.lastKnownStop); // TODO Crude, might delete useful data - * - * if (bus.lastKnownStop.arivalTime == null) { Log.e(TAG, " bus.lastKnownStop.arivalTime is null"); } else if (stop.arivalTime == null) { - * Log.e(TAG, " stop.arivalTime is null"); } - * - * if (bus.lastKnownStop.arivalTime.before(stop.arivalTime) && bus.lastKnownStop.arivalTime.after(now)) { bus.lastKnownStop = stop; - * - * if (bus.firstKnownStop == null) { bus.firstKnownStop = stop; - * - * } else if (!bus.firstKnownStop.busStop.equals(stop.busStop)) { stopDao.delete(bus.firstKnownStop); // TODO Crude, might delete useful data - * - * bus.firstKnownStop = stop; } - * - * } } - */ - - // busDao.update(bus); - } else { + bus = new Bus(null, route, dir); + } - stop = new Stop(stopObj.getString("name"), busStop, destStop, calender.getTime(), new Date(System.currentTimeMillis())); - stop.route = route; + busDao.update(bus); - } + stop = new Stop(bus, busStop, calender.getTime(), now); return stop; @@ -556,62 +539,6 @@ public class DataManager { } - public static boolean updateStop(BusStop busStop, boolean onlyUniLink) { - String file = getFileFromServer(busStopUrl + busStop.id + ".json"); - - try { - JSONObject data = new JSONObject(file); - - JSONArray stopsArray = data.getJSONArray("stops"); - - // Log.i(TAG, "Number of entries " + data.length()); - - // Log.i(TAG, "Stops: " + data.getJSONArray("stops")); - - for (int stopNum = 0; stopNum < stopsArray.length(); stopNum++) { - JSONObject stopObj = stopsArray.getJSONObject(stopNum); - - if (onlyUniLink && !stopObj.getString("name").startsWith("U")) { - continue; - } - - Stop stop = getStop(context, stopObj, busStop); - - if (stop == null) { - Log.w(TAG, "Null stop, skiping"); - continue; - } - - if (stop.bus != null) { - Log.i(TAG, "Found stop for " + stop.bus.id + " at " + stop.busStop.id + " at " + stop.arivalTime); - } else { - if (stop.name == null) { - Log.e(TAG, "Null name"); - } else if (stop.busStop == null) { - Log.e(TAG, "Null busStop"); - } else if (stop.arivalTime == null) { - Log.e(TAG, "Null arivalTime"); - } - Log.i(TAG, "Found stop for a unidentified " + stop.name + " at " + stop.busStop.id + " at " + stop.arivalTime); - } - - // stopDao.create(stop); - } - } catch (JSONException ex) { - Log.e(TAG, "", ex); - - return false; - } catch (SQLException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - - return false; - } - - return false; - - } - public static Timetable getTimetable(Context context, String busStop, boolean onlyUniLink) throws SQLException { if (helper == null) @@ -655,11 +582,7 @@ public class DataManager { continue; } - if (stop.bus != null) { - Log.i(TAG, "Found stop for " + stop.bus.id + " at " + stop.busStop.id + " at " + stop.arivalTime); - } else { - Log.i(TAG, "Found stop for a unidentified " + stop.name + " at " + stop.busStop.id + " at " + stop.arivalTime); - } + Log.i(TAG, "Found stop for a unidentified " + stop.bus.toString() + " at " + stop.busStop.id + " at " + stop.arivalTime); timetable.add(stop); } diff --git a/src/net/cbaines/suma/DatabaseHelper.java b/src/net/cbaines/suma/DatabaseHelper.java index 925b15f..00f441f 100644 --- a/src/net/cbaines/suma/DatabaseHelper.java +++ b/src/net/cbaines/suma/DatabaseHelper.java @@ -40,7 +40,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { private static final String DATABASE_PATH = "/data/data/net.cbaines.suma/databases/"; private static final String DATABASE_NAME = "data.db"; - private static final int DATABASE_VERSION = 36; + private static final int DATABASE_VERSION = 37; private static final String TAG = "DatabaseHelper"; diff --git a/src/net/cbaines/suma/Direction.java b/src/net/cbaines/suma/Direction.java new file mode 100644 index 0000000..698e352 --- /dev/null +++ b/src/net/cbaines/suma/Direction.java @@ -0,0 +1,5 @@ +package net.cbaines.suma; + +public class Direction { + String direction; +} diff --git a/src/net/cbaines/suma/SouthamptonUniversityMapActivity.java b/src/net/cbaines/suma/SouthamptonUniversityMapActivity.java index 0461fdf..c172cdc 100644 --- a/src/net/cbaines/suma/SouthamptonUniversityMapActivity.java +++ b/src/net/cbaines/suma/SouthamptonUniversityMapActivity.java @@ -27,7 +27,6 @@ import java.util.Collections; import java.util.Comparator; import java.util.HashMap; - import org.osmdroid.DefaultResourceProxyImpl; import org.osmdroid.ResourceProxy; import org.osmdroid.tileprovider.tilesource.TileSourceFactory; @@ -695,13 +694,17 @@ public class SouthamptonUniversityMapActivity extends OrmLiteBaseActivity<Databa Collections.sort(mapView.getOverlays(), comparator); } } + try { + Dao<BusRoute, Integer> busRouteDao = getHelper().getBusRouteDao(); - busStopOverlay.setRoutes(0, activityPrefs.getBoolean("Bus Stops:U1", BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - busStopOverlay.setRoutes(1, activityPrefs.getBoolean("Bus Stops:U1N", BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - busStopOverlay.setRoutes(2, activityPrefs.getBoolean("Bus Stops:U2", BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - busStopOverlay.setRoutes(3, activityPrefs.getBoolean("Bus Stops:U6", BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - busStopOverlay.setRoutes(4, activityPrefs.getBoolean("Bus Stops:U9", BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - + busStopOverlay.setRoutes(busRouteDao.queryForId(326), activityPrefs.getBoolean("Bus Stops:U1", BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + busStopOverlay.setRoutes(busRouteDao.queryForId(468), activityPrefs.getBoolean("Bus Stops:U1N", BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + busStopOverlay.setRoutes(busRouteDao.queryForId(329), activityPrefs.getBoolean("Bus Stops:U2", BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + busStopOverlay.setRoutes(busRouteDao.queryForId(327), activityPrefs.getBoolean("Bus Stops:U6", BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + busStopOverlay.setRoutes(busRouteDao.queryForId(354), activityPrefs.getBoolean("Bus Stops:U9", BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + } catch (SQLException e) { + e.printStackTrace(); + } mapView.postInvalidate(); Log.i(TAG, "Finished showing bus stop overlays at " + (System.currentTimeMillis() - startTime)); diff --git a/src/net/cbaines/suma/Stop.java b/src/net/cbaines/suma/Stop.java index 04eb118..8e2d466 100644 --- a/src/net/cbaines/suma/Stop.java +++ b/src/net/cbaines/suma/Stop.java @@ -23,54 +23,64 @@ import java.util.Date; import android.text.format.DateUtils; -import com.j256.ormlite.field.DatabaseField; -import com.j256.ormlite.table.DatabaseTable; - -@DatabaseTable(tableName = "stops") +/** + * Stop represents a Bus stopping at a time at a BusStop. + * + * @author Christopher Baines <cbaines8@gmail.com> + * + */ public class Stop { - @DatabaseField(generatedId = true) - int id; - - @DatabaseField(canBeNull = false) - public String name; + public static final String ID_FIELD_NAME = "id"; + public static final String BUS_FIELD_NAME = "bus"; + public static final String BUS_STOP_FIELD_NAME = "busStop"; + public static final String ARIVAL_TIME_FIELD_NAME = "arivalTime"; + public static final String FETCH_TIME_FIELD_NAME = "timeOfFetch"; - @DatabaseField(canBeNull = false, foreign = true) - BusStop destStop; + /** + * A generated id for the bus + */ + int id; - @DatabaseField(canBeNull = true, foreign = true) - public Bus bus; + /** + * The Bus stopping at the stop + */ + Bus bus; - @DatabaseField(canBeNull = false, foreign = true) - public BusStop busStop; + /** + * The busStop that the bus is stopping at + */ + BusStop busStop; - @DatabaseField(canBeNull = false) - public Date arivalTime; + /** + * The time that the bus is estimated to arrive + */ + Date arivalTime; - @DatabaseField(canBeNull = false) + /** + * The time this data was fetched from the server + */ Date timeOfFetch; - BusRoute route; - - Stop() { - - } - - public Stop(String name, BusStop busStop, BusStop dest, Bus bus, Date arivalTime, Date timeOfFetch) { - this.name = name; + /** + * + * @param bus + * @param busStop + * @param arivalTime + * @param timeOfFetch + */ + public Stop(Bus bus, BusStop busStop, Date arivalTime, Date timeOfFetch) { this.busStop = busStop; - this.destStop = dest; this.bus = bus; this.arivalTime = arivalTime; this.timeOfFetch = timeOfFetch; } - public Stop(String name, BusStop busStop, BusStop dest, Date arivalTime, Date timeOfFetch) { - this(name, busStop, dest, null, arivalTime, timeOfFetch); - } - + /** + * + * @return + */ public String getTimeToArival() { - if (arivalTime.getTime() - System.currentTimeMillis() <= 60000) { return "Due"; } else { @@ -89,12 +99,16 @@ public class Stop { return result; } + /** + * A printout of the stop data for debugging + */ @Override public String toString() { - return "Stop [id=" + id + ", bus=" + bus + ", busStop=" + busStop + ", arivalTime=" + arivalTime + "]"; + return "Stop [bus=" + bus + ", busStop=" + busStop + ", arivalTime=" + arivalTime + "]"; } @Override + // TODO: If this is used, the paramaters need to be checked? public boolean equals(Object obj) { if (this == obj) return true; diff --git a/src/net/cbaines/suma/StopView.java b/src/net/cbaines/suma/StopView.java index e36b3d9..d99c8ba 100644 --- a/src/net/cbaines/suma/StopView.java +++ b/src/net/cbaines/suma/StopView.java @@ -68,7 +68,7 @@ public class StopView extends LinearLayout implements OnClickListener { // Log.i(TAG, "Time of arival " + stop.arivalTime); - name.setText(stop.name); + name.setText(stop.bus.getName()); time.setText(stop.getTimeToArival()); DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); |