From 2ada92db0a557de0cb1b169534e3207582b44ec9 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Mon, 13 Feb 2012 07:44:37 +0000 Subject: Added route labels to the POIView, and some more improvements in the preferences system. --- src/net/cbaines/suma/BusStopActivity.java | 8 +- src/net/cbaines/suma/DataManager.java | 214 ++++++------- src/net/cbaines/suma/POIView.java | 83 ++++- src/net/cbaines/suma/Preferences.java | 6 +- .../suma/SouthamptonUniversityMapActivity.java | 346 ++++++++++----------- 5 files changed, 363 insertions(+), 294 deletions(-) (limited to 'src/net') diff --git a/src/net/cbaines/suma/BusStopActivity.java b/src/net/cbaines/suma/BusStopActivity.java index 4c9ce52..52f1b92 100644 --- a/src/net/cbaines/suma/BusStopActivity.java +++ b/src/net/cbaines/suma/BusStopActivity.java @@ -170,7 +170,7 @@ public class BusStopActivity extends OrmLiteBaseActivity impleme } else { U9RouteRadioButton.setVisibility(View.GONE); } - } + } } busStopDao = helper.getBusStopDao(); @@ -309,6 +309,12 @@ public class BusStopActivity extends OrmLiteBaseActivity impleme } catch (JSONException e) { errorMessage = "Error parsing bus times"; e.printStackTrace(); + } catch (Exception e) { + if (THROW_ERROR_ON_EXCEPTION) { + throw new RuntimeException(e.getMessage()); + } else { + Log.e(TAG, e.getMessage()); + } } return newTimetable; } diff --git a/src/net/cbaines/suma/DataManager.java b/src/net/cbaines/suma/DataManager.java index aad8b86..f642d56 100644 --- a/src/net/cbaines/suma/DataManager.java +++ b/src/net/cbaines/suma/DataManager.java @@ -424,7 +424,7 @@ public class DataManager { Log.i(TAG, "Loaded sites from csv"); } - private static Stop getStop(Context context, JSONObject stopObj, BusStop busStop) throws SQLException { + private static Stop getStop(Context context, JSONObject stopObj, BusStop busStop) throws SQLException, JSONException { if (helper == null) helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); @@ -435,145 +435,135 @@ public class DataManager { if (busStopDao == null) busStopDao = helper.getBusStopDao(); - try { - String time = stopObj.getString("time"); - - GregorianCalendar calender = new GregorianCalendar(); - boolean live = true; - if (!time.equals("Due")) { + String time = stopObj.getString("time"); - Log.v(TAG, "Time: " + time + " current time " + calender.getTime()); + GregorianCalendar calender = new GregorianCalendar(); + boolean live = true; + if (!time.equals("Due")) { - if (time.contains(":")) { - String[] minAndHour = time.split(":"); - calender.set(Calendar.HOUR_OF_DAY, Integer.parseInt(minAndHour[0])); - calender.set(Calendar.MINUTE, Integer.parseInt(minAndHour[1])); - live = false; - } else { - // Log.i(TAG, "Parsing " + time.substring(0, time.length() - 1) + " for min"); - calender.add(Calendar.MINUTE, Integer.parseInt(time.substring(0, time.length() - 1))); - } + Log.v(TAG, "Time: " + time + " current time " + calender.getTime()); - Log.v(TAG, "Date: " + calender.getTime()); + if (time.contains(":")) { + String[] minAndHour = time.split(":"); + calender.set(Calendar.HOUR_OF_DAY, Integer.parseInt(minAndHour[0])); + calender.set(Calendar.MINUTE, Integer.parseInt(minAndHour[1])); + live = false; + } else { + // Log.i(TAG, "Parsing " + time.substring(0, time.length() - 1) + " for min"); + calender.add(Calendar.MINUTE, Integer.parseInt(time.substring(0, time.length() - 1))); } - String name = stopObj.getString("name"); + Log.v(TAG, "Date: " + calender.getTime()); + } - BusRoute route; - String dir = null; + String name = stopObj.getString("name"); - if (name.contains("U")) { - if (name.equals("U1N")) { - route = busRoutes.queryForId(468); - } else if (name.startsWith("U9")) { - route = busRoutes.queryForId(354); - } 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 { - Log.e(TAG, "Error finding Uni-Link route " + name); - return null; - } + BusRoute route; + String dir = null; - 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; - } + if (name.contains("U")) { + if (name.equals("U1N")) { + route = busRoutes.queryForId(468); + } else if (name.startsWith("U9")) { + route = busRoutes.queryForId(354); + } 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 { + throw new RuntimeException("Error finding Uni-Link route " + name); } - } else { - Log.v(TAG, "Route not Uni-Link"); - List routes = (List) busRoutes.queryForEq(BusRoute.CODE_FIELD_NAME, name); - if (routes.size() != 1) { - Log.e(TAG, "Found more than one non Uni-Link route?"); - for (BusRoute routeT : routes) { - Log.i(TAG, "Route: " + routeT); - } - return null; + if (route.forwardDirection.equals(name.substring(2))) { + dir = route.forwardDirection; + } else if (route.reverseDirection.equals(name.substring(2))) { + dir = route.reverseDirection; } else { - route = routes.get(0); - if (route == null) { - Log.e(TAG, "Could not find database entry for " + name); - return null; - } + throw new RuntimeException("Error detecting direction for " + name); } } - String destString = stopObj.getString("dest"); - BusStop destStop; - - if (destString.equals("Central Station")) { - destStop = busStopDao.queryForId("SNA19709"); - } else if (destString.equals("Civic Centre")) { - destStop = busStopDao.queryForId("SN120527"); - } else if (destString.equals("City DG4")) { - destStop = busStopDao.queryForId("HAA13579"); - } else if (destString.equals("Central Station")) { - destStop = busStopDao.queryForId("SN120520"); - } else if (destString.equals("Airport")) { - destStop = busStopDao.queryForId("HA030184"); - } else if (destString.equals("City, Town Quay")) { - destStop = busStopDao.queryForId("SNA13766"); - } else if (destString.equals("Dock Gate 4")) { - destStop = busStopDao.queryForId("MG1031"); - } else if (destString.equals("Eastleigh")) { - destStop = busStopDao.queryForId("HA030212"); - } else if (destString.equals("Crematorium")) { - destStop = busStopDao.queryForId("SN121009"); - } else if (destString.equals("General Hosp")) { - destStop = busStopDao.queryForId("SNA19482"); + } else { + Log.v(TAG, "Route not Uni-Link"); + List routes = (List) busRoutes.queryForEq(BusRoute.CODE_FIELD_NAME, name); + if (routes.size() != 1) { + Log.e(TAG, "Found more than one non Uni-Link route?"); + for (BusRoute routeT : routes) { + Log.i(TAG, "Route: " + routeT); + } + throw new RuntimeException("Found more than one non Uni-Link route?"); } else { - Log.e(TAG, "Unknown end dest " + destString + " for route " + route.code); - return null; + route = routes.get(0); + if (route == null) { + throw new RuntimeException("Could not find database entry for " + name); + } } + } - Date now = new Date(System.currentTimeMillis()); + String destString = stopObj.getString("dest"); + BusStop destStop; + + if (destString.equals("Central Station")) { + destStop = busStopDao.queryForId("SNA19709"); + } else if (destString.equals("Civic Centre")) { + destStop = busStopDao.queryForId("SN120527"); + } else if (destString.equals("City DG4")) { + destStop = busStopDao.queryForId("HAA13579"); + } else if (destString.equals("Central Station")) { + destStop = busStopDao.queryForId("SN120520"); + } else if (destString.equals("Airport")) { + destStop = busStopDao.queryForId("HA030184"); + } else if (destString.equals("City, Town Quay")) { + destStop = busStopDao.queryForId("SNA13766"); + } else if (destString.equals("City Centre")) { + destStop = busStopDao.queryForId("SNA13766"); + } else if (destString.equals("Dock Gate 4")) { + destStop = busStopDao.queryForId("MG1031"); + } else if (destString.equals("Eastleigh")) { + destStop = busStopDao.queryForId("HA030212"); + } else if (destString.equals("Crematorium")) { + destStop = busStopDao.queryForId("SN121009"); + } else if (destString.equals("General Hosp")) { + destStop = busStopDao.queryForId("SNA19482"); + } else { + throw new RuntimeException("Unknown end dest " + destString + " for route " + route.code); + } - String busID = null; - Stop stop; - Bus bus; - if (stopObj.has("vehicle")) { - busID = stopObj.getString("vehicle"); + Date now = new Date(System.currentTimeMillis()); - QueryBuilder busQueryBuilder = busDao.queryBuilder(); - busQueryBuilder.where().eq(Bus.ID_FIELD_NAME, busID); - PreparedQuery busPreparedQuery = busQueryBuilder.prepare(); + String busID = null; + Stop stop; + Bus bus; + if (stopObj.has("vehicle")) { + busID = stopObj.getString("vehicle"); - bus = busDao.queryForFirst(busPreparedQuery); + QueryBuilder busQueryBuilder = busDao.queryBuilder(); + busQueryBuilder.where().eq(Bus.ID_FIELD_NAME, busID); + PreparedQuery busPreparedQuery = busQueryBuilder.prepare(); - if (bus == null) { - bus = new Bus(busID, route, dir); - bus.destination = destStop; - } else { - bus.destination = destStop; - bus.route = route; - bus.direction = dir; - } + bus = busDao.queryForFirst(busPreparedQuery); + if (bus == null) { + bus = new Bus(busID, route, dir); + bus.destination = destStop; } else { - bus = new Bus(null, route, dir); + bus.destination = destStop; + bus.route = route; + bus.direction = dir; } - busDao.update(bus); - - stop = new Stop(bus, busStop, calender.getTime(), now, live); + } else { + bus = new Bus(null, route, dir); + } - return stop; + busDao.update(bus); - } catch (Exception e) { - // TODO Auto-generated catch block - Log.e(TAG, "Error parsing stop " + stopObj, e); - return null; - } + stop = new Stop(bus, busStop, calender.getTime(), now, live); + return stop; } public static Timetable getTimetable(Context context, String busStop, boolean keepUniLink, boolean keepNonUniLink) throws SQLException, diff --git a/src/net/cbaines/suma/POIView.java b/src/net/cbaines/suma/POIView.java index 733cd0f..0fb1496 100644 --- a/src/net/cbaines/suma/POIView.java +++ b/src/net/cbaines/suma/POIView.java @@ -23,6 +23,7 @@ import android.content.Context; import android.util.Log; import android.view.Display; import android.view.Gravity; +import android.view.View; import android.view.WindowManager; import android.widget.LinearLayout; import android.widget.TextView; @@ -34,6 +35,12 @@ public class POIView extends LinearLayout { private final TextView name; private final TextView dist; + private TextView u1; + private TextView u1n; + private TextView u2; + private TextView u6; + private TextView u9; + private LayoutParams textLayoutParams; final int width; @@ -52,18 +59,67 @@ public class POIView extends LinearLayout { this.setOrientation(HORIZONTAL); name = new TextView(context); - name.setTextSize(22f); + name.setTextSize(16f); name.setGravity(Gravity.LEFT); dist = new TextView(context); - dist.setTextSize(22f); + dist.setTextSize(16f); dist.setGravity(Gravity.RIGHT); + u1 = new TextView(context); + u1.setText(R.string.U1); + u1.setBackgroundResource(R.drawable.u1_back_selected); + u1n = new TextView(context); + u1n.setText(R.string.U1N); + u1n.setBackgroundResource(R.drawable.u1n_back_selected); + u2 = new TextView(context); + u2.setText(R.string.U2); + u2.setBackgroundResource(R.drawable.u2_back_selected); + u6 = new TextView(context); + u6.setText(R.string.U6); + u6.setBackgroundResource(R.drawable.u6_back_selected); + u9 = new TextView(context); + u9.setText(R.string.U9); + u9.setBackgroundResource(R.drawable.u9_back_selected); + textLayoutParams = new LayoutParams(width - (width / 4), LayoutParams.WRAP_CONTENT); LayoutParams distLayoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); setPOIAndDist(poi, distInM); + LayoutParams busRouteLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); + addView(u1, busRouteLayoutParams); + addView(u1n, busRouteLayoutParams); + addView(u2, busRouteLayoutParams); + addView(u6, busRouteLayoutParams); + addView(u9, busRouteLayoutParams); + + u1.setVisibility(View.GONE); + u1n.setVisibility(View.GONE); + u2.setVisibility(View.GONE); + u6.setVisibility(View.GONE); + u9.setVisibility(View.GONE); + + if (poi.type == POI.BUS_STOP) { + BusStop busStop = (BusStop) poi; + + if ((busStop.routes & (1 << 0)) != 0) { + u1.setVisibility(View.VISIBLE); + } + if ((busStop.routes & (1 << 1)) != 0) { + u1n.setVisibility(View.VISIBLE); + } + if ((busStop.routes & (1 << 2)) != 0) { + u2.setVisibility(View.VISIBLE); + } + if ((busStop.routes & (1 << 3)) != 0) { + u6.setVisibility(View.VISIBLE); + } + if ((busStop.routes & (1 << 4)) != 0) { + u9.setVisibility(View.VISIBLE); + } + } + addView(name, textLayoutParams); addView(dist, distLayoutParams); } @@ -76,6 +132,12 @@ public class POIView extends LinearLayout { // Log.i(TAG, "Looking at poi " + poi.id); + u1.setVisibility(View.GONE); + u1n.setVisibility(View.GONE); + u2.setVisibility(View.GONE); + u6.setVisibility(View.GONE); + u9.setVisibility(View.GONE); + if (poi.type == POI.BUILDING) { Building building = (Building) poi; // Log.i(TAG, "Its a building of name " + building.name); @@ -87,6 +149,23 @@ public class POIView extends LinearLayout { // Log.i(TAG, "Its a bus stop of description " + busStop.description); name.setText(busStop.description + " (" + busStop.id + ")"); + + if ((busStop.routes & (1 << 0)) != 0) { + u1.setVisibility(View.VISIBLE); + } + if ((busStop.routes & (1 << 1)) != 0) { + u1n.setVisibility(View.VISIBLE); + } + if ((busStop.routes & (1 << 2)) != 0) { + u2.setVisibility(View.VISIBLE); + } + if ((busStop.routes & (1 << 3)) != 0) { + u6.setVisibility(View.VISIBLE); + } + if ((busStop.routes & (1 << 4)) != 0) { + u9.setVisibility(View.VISIBLE); + } + } else if (poi.type == POI.SITE) { Site site = (Site) poi; diff --git a/src/net/cbaines/suma/Preferences.java b/src/net/cbaines/suma/Preferences.java index c92a86c..df44729 100644 --- a/src/net/cbaines/suma/Preferences.java +++ b/src/net/cbaines/suma/Preferences.java @@ -8,9 +8,9 @@ public interface Preferences { static final boolean UNI_LINK_BUS_TIMES_ENABLED_BY_DEFAULT = true; static final String NON_UNI_LINK_BUS_TIMES = "nonUniLinkLiveBusTimesEnabled"; static final boolean NON_UNI_LINK_BUS_TIMES_ENABLED_BY_DEFAULT = false; - static final String UNI_LINK_BUS_STOPS = "uniLinkBusStop"; - static final boolean UNI_LINK_BUS_STOPS_ENABLED_BY_DEFAULT = true; - static final String NON_UNI_LINK_BUS_STOPS = "nonUniLinkBusStop"; + static final String NON_UNI_LINK_BUS_STOPS = "nonUniLinkBusStops"; static final boolean NON_UNI_LINK_BUS_STOPS_ENABLED_BY_DEFAULT = false; + static final boolean THROW_ERROR_ON_EXCEPTION = false; + } diff --git a/src/net/cbaines/suma/SouthamptonUniversityMapActivity.java b/src/net/cbaines/suma/SouthamptonUniversityMapActivity.java index 323c7db..a313d12 100644 --- a/src/net/cbaines/suma/SouthamptonUniversityMapActivity.java +++ b/src/net/cbaines/suma/SouthamptonUniversityMapActivity.java @@ -74,7 +74,7 @@ import com.j256.ormlite.android.apptools.OrmLiteBaseActivity; import com.j256.ormlite.dao.Dao; public class SouthamptonUniversityMapActivity extends OrmLiteBaseActivity implements MapViewConstants, Runnable, RouteColorConstants, - OnChildClickListener, OnItemClickListener, OnItemLongClickListener, OnSharedPreferenceChangeListener, Preferences { + OnItemClickListener, OnItemLongClickListener, OnSharedPreferenceChangeListener, Preferences { private boolean useBundledDatabase = false; @@ -93,40 +93,52 @@ public class SouthamptonUniversityMapActivity extends OrmLiteBaseActivity siteOverlays = new HashMap(21); + private static final boolean SITE_OVERLAYS_ENABLED_BY_DEFAULT = false; + private static final int SITE_OVERLAYS_RANK = 1; // Route Overlays + private static final String ROUTE_OVERLAYS = "routeOverlays"; private HashMap routeOverlays = new HashMap(5); - - // View - private static final String RESIDENTIAL_BUILDING_OVERLAY = "Buildings:Residential"; - private static final String NON_RESIDENTIAL_BUILDING_OVERLAY = "Buildings:Non-Residential"; - private static final boolean SITE_OVERLAY_ENABLED_BY_DEFAULT = false; - private static final boolean ROUTE_OVERLAY_ENABLED_BY_DEFAULT = true; + private static final boolean ROUTE_OVERLAYS_ENABLED_BY_DEFAULT = true; + private static final int ROUTE_OVERLAYS_RANK = 1; private POIDialog favDialog; @@ -374,12 +386,13 @@ public class SouthamptonUniversityMapActivity extends OrmLiteBaseActivity busStops; Log.v(TAG, "Begin fetching BusStops at " + (System.currentTimeMillis() - startTime)); - if (activityPrefs.getBoolean(UNI_LINK_BUS_STOPS, UNI_LINK_BUS_STOPS_ENABLED_BY_DEFAULT)) { + if (activityPrefs.getBoolean(UNI_LINK_BUS_STOP_OVERLAY, UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)) { busStops = getHelper().getBusStopDao().queryForAll(); } else { busStops = getHelper().getBusStopDao().queryForEq(BusStop.UNI_LINK_FIELD_NAME, true); @@ -686,7 +693,7 @@ public class SouthamptonUniversityMapActivity extends OrmLiteBaseActivity busStops; - Log.v(TAG, "Begin fetching BusStops at " + (System.currentTimeMillis() - startTime)); + Log.v(TAG, "Begin fetching non Uni-Link BusStops at " + (System.currentTimeMillis() - startTime)); busStops = getHelper().getBusStopDao().queryForEq(BusStop.UNI_LINK_FIELD_NAME, false); - Log.v(TAG, "Finished fetching BusStops at " + (System.currentTimeMillis() - startTime)); + Log.v(TAG, "Finished fetching non Uni-Link BusStops at " + (System.currentTimeMillis() - startTime)); nonUniLinkBusStopOverlay = new BusStopOverlay(instance, busStops); } catch (SQLException e) { @@ -714,7 +742,7 @@ public class SouthamptonUniversityMapActivity extends OrmLiteBaseActivity { - private final SharedPreferences prefs; + // private final SharedPreferences prefs; OverlayRankComparator(SharedPreferences prefs) { - this.prefs = prefs; + // this.prefs = prefs; } public int compare(Overlay arg0, Overlay arg1) { return getRank(arg1) - getRank(arg0); } - private final int getRank(Overlay arg0) { // TODO: Dont hardcode the rank values - if (arg0 == scaleBarOverlay) { - return prefs.getInt("mScaleBarOverlay", 1); - } else if (arg0 == myLocationOverlay) { - return prefs.getInt("myLocationOverlay", 0); - } else if (arg0 == uniLinkBusStopOverlay) { - return prefs.getInt("busStopOverlay", 2); - } else if (arg0 == residentialBuildingOverlay) { - return prefs.getInt("residentialBuildingOverlay", 4); - } else if (arg0 == nonResidentialBuildingOverlay) { - return prefs.getInt("nonResidentialBuildingOverlay", 3); - } else if (siteOverlays != null && siteOverlays.values().contains(arg0)) { - return prefs.getInt("siteOverlays", 6); - } else if (routeOverlays != null && routeOverlays.values().contains(arg0)) { - return prefs.getInt("routeOverlays", 5); + private final int getRank(Overlay overlay) { // TODO: Dont hardcode the rank values + if (overlay == scaleBarOverlay) { + return SCALE_BAR_OVERLAY_RANK; + } else if (overlay == myLocationOverlay) { + return MY_LOCATION_OVERLAY_RANK; + } else if (overlay == uniLinkBusStopOverlay) { + return UNI_LINK_BUS_STOP_OVERLAY_RANK; + } else if (overlay == nonUniLinkBusStopOverlay) { + return NON_UNI_LINK_BUS_STOP_OVERLAY_RANK; + } else if (overlay == residentialBuildingOverlay) { + return RESIDENTIAL_BUILDING_OVERLAY_RANK; + } else if (overlay == nonResidentialBuildingOverlay) { + return NON_RESIDENTIAL_BUILDING_OVERLAY_RANK; + } else if (siteOverlays != null && siteOverlays.values().contains(overlay)) { + return SITE_OVERLAYS_RANK; + } else if (routeOverlays != null && routeOverlays.values().contains(overlay)) { + return ROUTE_OVERLAYS_RANK; } else { + Log.e(TAG, "Trying to rank unknown overlay " + overlay); return -1; } } @@ -1084,43 +1104,31 @@ public class SouthamptonUniversityMapActivity extends OrmLiteBaseActivity