aboutsummaryrefslogtreecommitdiff
path: root/src/net/cbaines/suma
diff options
context:
space:
mode:
Diffstat (limited to 'src/net/cbaines/suma')
-rw-r--r--src/net/cbaines/suma/Bus.java8
-rw-r--r--src/net/cbaines/suma/BusRoute.java68
-rw-r--r--src/net/cbaines/suma/BusStopActivity.java76
-rw-r--r--src/net/cbaines/suma/DataManager.java36
-rw-r--r--src/net/cbaines/suma/DatabaseHelper.java13
-rw-r--r--src/net/cbaines/suma/Direction.java62
-rw-r--r--src/net/cbaines/suma/SouthamptonUniversityMapActivity.java2
-rw-r--r--src/net/cbaines/suma/Timetable.java3
8 files changed, 94 insertions, 174 deletions
diff --git a/src/net/cbaines/suma/Bus.java b/src/net/cbaines/suma/Bus.java
index 533c43c..d8d96f3 100644
--- a/src/net/cbaines/suma/Bus.java
+++ b/src/net/cbaines/suma/Bus.java
@@ -53,8 +53,8 @@ public class Bus {
/**
* The direction which the bus is travelling.
*/
- @DatabaseField(canBeNull = true, foreign = true)
- Direction direction;
+ @DatabaseField(canBeNull = false)
+ String direction;
/**
* The destination the bus is travelling towards.
@@ -75,10 +75,10 @@ public class Bus {
* @param dir
* The direction which the bus is travelling.
*/
- Bus(String id, BusRoute route, Direction dir) {
+ Bus(String id, BusRoute route, String direction) {
this.id = id;
this.route = route;
- this.direction = dir;
+ this.direction = direction;
}
/**
diff --git a/src/net/cbaines/suma/BusRoute.java b/src/net/cbaines/suma/BusRoute.java
index 11ff798..e3acddf 100644
--- a/src/net/cbaines/suma/BusRoute.java
+++ b/src/net/cbaines/suma/BusRoute.java
@@ -20,7 +20,6 @@
package net.cbaines.suma;
import java.sql.SQLException;
-import java.util.Collection;
import java.util.List;
import android.content.Context;
@@ -29,7 +28,6 @@ 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;
@@ -56,19 +54,28 @@ public class BusRoute {
@DatabaseField
public String code;
- @DatabaseField
+ @DatabaseField(canBeNull = false)
String label;
- @ForeignCollectionField(eager = false)
- Collection<Direction> directions;
+ @DatabaseField(canBeNull = true)
+ String forwardDirection;
+
+ @DatabaseField(canBeNull = true)
+ String reverseDirection;
BusRoute() {
}
- public BusRoute(Integer id, String code, String label) {
+ public BusRoute(Integer id, String code, String label, String forwardDirection, String reverseDirection) {
this.id = id.intValue();
this.code = code;
this.label = label;
+ this.forwardDirection = forwardDirection;
+ this.reverseDirection = reverseDirection;
+ }
+
+ public BusRoute(Integer id, String code, String label) {
+ this(id, code, label, null, null);
}
public String toString() {
@@ -82,7 +89,7 @@ public class BusRoute {
* @param stop
* @return
*/
- BusStop getBusStopBefore(Context context, BusStop stop, Direction dir) {
+ BusStop getBusStopBefore(Context context, BusStop stop, String dir) {
return moveInRoute(context, stop, dir, -1);
}
@@ -93,7 +100,7 @@ public class BusRoute {
* @param stop
* @return
*/
- BusStop getStopAfter(Context context, BusStop stop, Direction dir) {
+ BusStop getStopAfter(Context context, BusStop stop, String dir) {
return moveInRoute(context, stop, dir, 1);
}
@@ -105,9 +112,31 @@ public class BusRoute {
* @param moveAmount
* @return
*/
- BusStop moveInRoute(Context context, BusStop stop, Direction dir, int moveAmount) {
+ BusStop moveInRoute(Context context, BusStop stop, String direction, int moveAmount) {
+ if (moveAmount == 0) {
+ return stop;
+ }
+
DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class);
+ if (forwardDirection != null) {
+
+ if (direction == null) {
+ return 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;
+ }
+
+ }
+
try {
Dao<RouteStops, Integer> routeStopsDao = helper.getRouteStopsDao();
Dao<BusStop, String> busStopDao = helper.getBusStopDao();
@@ -117,19 +146,22 @@ public class BusRoute {
PreparedQuery<RouteStops> routeStopsPreparedQuery = routeStopsQueryBuilder.prepare();
List<RouteStops> routeStopsFound = routeStopsDao.query(routeStopsPreparedQuery);
+ Log.v("BusRoute", "Found " + routeStopsFound.size() + " stops");
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);
+ Log.v("BusRoute", "Moving forward in direction " + direction + " " + moveAmount + " stops");
- return routeStopsFound.get(stopWanted).stop;
- }
- }
+ int stopWanted = (routeStopsFound.indexOf(stop) + moveAmount) % (routeStopsFound.size() + 1);
+ busStopDao.refresh(routeStopsFound.get(stopWanted).stop);
+
+ return routeStopsFound.get(stopWanted).stop;
} else {
- int maxSeq = routeStopsFound.size() - 1;
- int stopWanted = maxSeq - (Math.abs(maxSeq) % (routeStopsFound.size() + 1));
+ Log.v("BusRoute", "Moving backwards in direction " + direction + " " + moveAmount + " stops");
+
+ int stopWanted = routeStopsFound.indexOf(stop) + moveAmount;
+ if (stopWanted < 0) {
+ stopWanted = routeStopsFound.size() - (Math.abs(stopWanted) % routeStopsFound.size());
+ }
busStopDao.refresh(routeStopsFound.get(stopWanted).stop);
return routeStopsFound.get(stopWanted).stop;
diff --git a/src/net/cbaines/suma/BusStopActivity.java b/src/net/cbaines/suma/BusStopActivity.java
index 97492ec..699ec2d 100644
--- a/src/net/cbaines/suma/BusStopActivity.java
+++ b/src/net/cbaines/suma/BusStopActivity.java
@@ -209,6 +209,9 @@ public class BusStopActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme
} else {
Log.i(TAG, "Displaying previous timetable");
displayTimetable(timetable);
+ if (System.currentTimeMillis() - timetable.fetchTime.getTime() > 20000) {
+ mHandler.post(refreshData);
+ }
}
} else {
@@ -304,9 +307,8 @@ public class BusStopActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
- switch (item.getItemId()) {
- case R.id.menu_previous_stop:
- Log.v(TAG, "Got a request for the previous stop");
+ if (item.getItemId() == R.id.menu_previous_stop || item.getItemId() == R.id.menu_next_stop) {
+ Log.v(TAG, "Got a request for the stop movement");
if (visibleTimetable.size() != 0) {
HashSet<BusRoute> routes = new HashSet<BusRoute>();
@@ -317,7 +319,7 @@ public class BusStopActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme
Log.v(TAG, routes.size() + " routes in the visible timetable");
if (routes.size() == 1) {
- HashSet<Direction> directions = new HashSet<Direction>();
+ HashSet<String> directions = new HashSet<String>();
for (Stop stop : visibleTimetable) {
directions.add(stop.bus.direction);
@@ -325,55 +327,22 @@ public class BusStopActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme
Log.v(TAG, directions.size() + " directions in the visible timetable");
- if (directions.size() == 1) {
+ if (directions.size() <= 1) {
try {
- BusStop previous = routes.iterator().next()
- .moveInRoute(this, getHelper().getBusStopDao().queryForId(busStopID), directions.iterator().next(), 1);
- Intent i = new Intent(this, BusStopActivity.class);
- i.putExtra("busStopID", previous.id);
- i.putExtra("busStopName", previous.description);
- startActivity(i);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- } else {
- // Show dialog
- }
- } else {
- // Show dialog
- }
- }
- break;
- case R.id.menu_next_stop:
- Log.v(TAG, "Got a request for the next stop");
- if (visibleTimetable.size() != 0) {
- HashSet<BusRoute> routes = new HashSet<BusRoute>();
-
- for (Stop stop : visibleTimetable) {
- routes.add(stop.bus.route);
- }
-
- Log.v(TAG, routes.size() + " routes in the visible timetable");
-
- if (routes.size() == 1) {
- HashSet<Direction> directions = new HashSet<Direction>();
-
- for (Stop stop : visibleTimetable) {
- directions.add(stop.bus.direction);
- }
-
- Log.v(TAG, directions.size() + " directions in the visible timetable");
-
- if (directions.size() == 1) {
- try {
- BusStop next = routes.iterator().next()
- .moveInRoute(this, getHelper().getBusStopDao().queryForId(busStopID), directions.iterator().next(), 1);
+ BusStop stop;
+ if (item.getItemId() == R.id.menu_next_stop) {
+ stop = routes.iterator().next()
+ .moveInRoute(this, getHelper().getBusStopDao().queryForId(busStopID), directions.iterator().next(), 1);
+ } else {
+ stop = routes.iterator().next()
+ .moveInRoute(this, getHelper().getBusStopDao().queryForId(busStopID), directions.iterator().next(), -1);
+ }
Intent i = new Intent(this, BusStopActivity.class);
- if (next.id == null) {
+ if (stop.id == null) {
Log.e(TAG, "next.id == null");
}
- i.putExtra("busStopID", next.id);
- i.putExtra("busStopName", next.description);
+ i.putExtra("busStopID", stop.id);
+ i.putExtra("busStopName", stop.description);
startActivity(i);
} catch (SQLException e) {
e.printStackTrace();
@@ -382,11 +351,13 @@ public class BusStopActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme
// Show dialog
}
} else {
+ // if (all routes point to the same (next/previous) stop) {
+ // move to that stop
+ // else
// Show dialog
}
}
- break;
- case R.id.menu_refresh_stop:
+ } else if (item.getItemId() == R.id.menu_refresh_stop) {
if (mHandler != null) { // BusTimes are enabled
mHandler.removeCallbacks(refreshData);
timetableTask.cancel(true);
@@ -396,8 +367,7 @@ public class BusStopActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme
} else {
// TODO: Toast here...
}
- break;
- default:
+ } else {
Log.e(TAG, "No known menu option selected");
return super.onOptionsItemSelected(item);
}
diff --git a/src/net/cbaines/suma/DataManager.java b/src/net/cbaines/suma/DataManager.java
index b82d31f..bc82b59 100644
--- a/src/net/cbaines/suma/DataManager.java
+++ b/src/net/cbaines/suma/DataManager.java
@@ -65,13 +65,10 @@ public class DataManager {
final static String busStopUrl = "http://data.southampton.ac.uk/bus-stop/";
- private static Context context;
-
private static DatabaseHelper helper;
private static Dao<BusRoute, Integer> busRoutes;
private static Dao<Bus, Integer> busDao;
private static Dao<BusStop, String> busStopDao;
- private static Dao<Stop, Integer> stopDao;
public static void loadBuildings(Context context) throws SQLException, IOException {
DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class);
@@ -82,7 +79,6 @@ public class DataManager {
Log.i(TAG, "Loading buildings from csv");
HashMap<String, GeoPoint> buildingPoints = new HashMap<String, GeoPoint>();
- HashMap<String, Polygon> buildingPolys = new HashMap<String, Polygon>();
InputStream inputStream = context.getAssets().open("buildings_points.csv");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
@@ -205,12 +201,10 @@ public class DataManager {
Dao<BusStop, String> busStopDao = helper.getBusStopDao();
Dao<BusRoute, Integer> busRouteDao = helper.getBusRouteDao();
Dao<RouteStops, Integer> routeStopsDao = helper.getRouteStopsDao();
- Dao<Direction, Integer> directionDao = helper.getDirectionDao();
TableUtils.clearTable(helper.getConnectionSource(), BusStop.class);
TableUtils.clearTable(helper.getConnectionSource(), BusRoute.class);
TableUtils.clearTable(helper.getConnectionSource(), RouteStops.class);
- TableUtils.clearTable(helper.getConnectionSource(), Direction.class);
Log.i(TAG, "Loading busstops from csv");
@@ -260,7 +254,12 @@ public class DataManager {
// Log.i(TAG, "Data: " + strLine);
String[] dataBits = strLine.split(",");
- BusRoute route = new BusRoute(Integer.parseInt(dataBits[0]), dataBits[1], dataBits[2].replace("\"", ""));
+ BusRoute route;
+ if (dataBits.length > 3) {
+ route = new BusRoute(Integer.parseInt(dataBits[0]), dataBits[1], dataBits[2].replace("\"", ""), dataBits[3], dataBits[4]);
+ } else {
+ route = new BusRoute(Integer.parseInt(dataBits[0]), dataBits[1], dataBits[2].replace("\"", ""));
+ }
// Log.i(TAG, "Loaded route " + route.id + " " + route.code + " " + route.label);
busRouteDao.create(route);
@@ -273,14 +272,6 @@ public class DataManager {
e.printStackTrace();
}
- directionDao.create(new Direction("A", busRouteDao.queryForId(326)));
- directionDao.create(new Direction("C", busRouteDao.queryForId(326)));
- directionDao.create(new Direction("E", busRouteDao.queryForId(326)));
- directionDao.create(new Direction("B", busRouteDao.queryForId(329)));
- directionDao.create(new Direction("C", busRouteDao.queryForId(329)));
- directionDao.create(new Direction("C", busRouteDao.queryForId(327)));
- directionDao.create(new Direction("H", busRouteDao.queryForId(327)));
-
Log.i(TAG, "Finished loading routes, now loading routestops");
inputStream = context.getAssets().open("routestops.csv");
@@ -457,7 +448,7 @@ public class DataManager {
String name = stopObj.getString("name");
BusRoute route;
- Direction dir = null;
+ String dir = null;
if (name.equals("U1N")) {
route = busRoutes.queryForId(468);
@@ -475,13 +466,11 @@ public class DataManager {
return null;
}
- for (Direction possibleDir : route.directions) {
- if (name.charAt(2) == possibleDir.direction.charAt(0)) {
- dir = possibleDir;
- break;
- }
- }
- if (dir.direction.length() == 0) {
+ if (route.forwardDirection.equals(name.substring(2))) {
+ dir = route.forwardDirection;
+ } else if (route.reverseDirection.equals(name.substring(2))) {
+ dir = route.reverseDirection;
+ } else {
Log.e(TAG, "Error detecting direction for " + name);
return null;
}
@@ -607,6 +596,7 @@ public class DataManager {
return null;
}
+ timetable.fetchTime = new Date(System.currentTimeMillis());
return timetable;
}
diff --git a/src/net/cbaines/suma/DatabaseHelper.java b/src/net/cbaines/suma/DatabaseHelper.java
index d5790a3..37ed06e 100644
--- a/src/net/cbaines/suma/DatabaseHelper.java
+++ b/src/net/cbaines/suma/DatabaseHelper.java
@@ -51,7 +51,6 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
private Dao<RouteStops, Integer> routeStopsDao = null;
private Dao<Site, String> siteDao = null;
private Dao<Bus, Integer> busDao = null;
- private Dao<Direction, Integer> directionDao = null;
private Context context;
@@ -71,7 +70,6 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
TableUtils.createTable(connectionSource, RouteStops.class);
TableUtils.createTable(connectionSource, Site.class);
TableUtils.createTable(connectionSource, Bus.class);
- TableUtils.createTable(connectionSource, Direction.class);
} catch (SQLException e) {
Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
throw new RuntimeException(e);
@@ -143,16 +141,6 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
/**
* Returns the Database Access Object (DAO) for our SimpleData class. It will create it or just give the cached value.
*/
- public Dao<Direction, Integer> getDirectionDao() throws SQLException {
- if (directionDao == null) {
- directionDao = getDao(Direction.class);
- }
- return directionDao;
- }
-
- /**
- * Returns the Database Access Object (DAO) for our SimpleData class. It will create it or just give the cached value.
- */
public Dao<Bus, Integer> getBusDao() throws SQLException {
if (busDao == null) {
busDao = getDao(Bus.class);
@@ -267,6 +255,5 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
routeStopsDao = null;
siteDao = null;
busDao = null;
- directionDao = null;
}
} \ No newline at end of file
diff --git a/src/net/cbaines/suma/Direction.java b/src/net/cbaines/suma/Direction.java
deleted file mode 100644
index 0bc3396..0000000
--- a/src/net/cbaines/suma/Direction.java
+++ /dev/null
@@ -1,62 +0,0 @@
-package net.cbaines.suma;
-
-import com.j256.ormlite.field.DatabaseField;
-import com.j256.ormlite.table.DatabaseTable;
-
-@DatabaseTable(tableName = "directions")
-public class Direction {
-
- public final static String ROUTE_ID_FIELD_NAME = "route_id";
-
- @DatabaseField(generatedId = true)
- int id;
-
- @DatabaseField(canBeNull = false)
- String direction;
-
- @DatabaseField(foreign = true, columnName = ROUTE_ID_FIELD_NAME)
- BusRoute route;
-
- Direction() {
- }
-
- Direction(String dir, BusRoute route) {
- this.direction = dir;
- this.route = route;
- }
-
- public String toString() {
- return direction;
- }
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((direction == null) ? 0 : direction.hashCode());
- result = prime * result + ((route == null) ? 0 : route.hashCode());
- return result;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- Direction other = (Direction) obj;
- if (direction == null) {
- if (other.direction != null)
- return false;
- } else if (!direction.equals(other.direction))
- return false;
- if (route == null) {
- if (other.route != null)
- return false;
- } else if (!route.equals(other.route))
- return false;
- return true;
- }
-}
diff --git a/src/net/cbaines/suma/SouthamptonUniversityMapActivity.java b/src/net/cbaines/suma/SouthamptonUniversityMapActivity.java
index 5e8caa8..d4f7f2b 100644
--- a/src/net/cbaines/suma/SouthamptonUniversityMapActivity.java
+++ b/src/net/cbaines/suma/SouthamptonUniversityMapActivity.java
@@ -75,7 +75,7 @@ import com.j256.ormlite.dao.Dao;
public class SouthamptonUniversityMapActivity extends OrmLiteBaseActivity<DatabaseHelper> implements MapViewConstants, Runnable, RouteColorConstants,
OnChildClickListener, OnItemClickListener, OnItemLongClickListener, OnSharedPreferenceChangeListener {
- private boolean useBundledDatabase = false;
+ private boolean useBundledDatabase = true;
private MapView mapView;
private MapController mapController;
diff --git a/src/net/cbaines/suma/Timetable.java b/src/net/cbaines/suma/Timetable.java
index 758c95f..6e3db1e 100644
--- a/src/net/cbaines/suma/Timetable.java
+++ b/src/net/cbaines/suma/Timetable.java
@@ -20,6 +20,7 @@
package net.cbaines.suma;
import java.util.ArrayList;
+import java.util.Date;
public class Timetable extends ArrayList<Stop> {
@@ -27,6 +28,8 @@ public class Timetable extends ArrayList<Stop> {
*
*/
private static final long serialVersionUID = -9021303378059511643L;
+
+ Date fetchTime;
public String toString() {
StringBuilder sb = new StringBuilder();