From f06b62c092ae6b9532982eb4729a911c378ccfbd Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Thu, 5 Jan 2012 19:20:56 +0000 Subject: Changed the overlay management, and speeded up the database check process. --- src/net/cbaines/suma/DatabaseHelper.java | 40 +- .../suma/SouthamptonUniversityMapActivity.java | 578 +++++++++------------ 2 files changed, 255 insertions(+), 363 deletions(-) diff --git a/src/net/cbaines/suma/DatabaseHelper.java b/src/net/cbaines/suma/DatabaseHelper.java index 28ad6a5..7ccc08d 100644 --- a/src/net/cbaines/suma/DatabaseHelper.java +++ b/src/net/cbaines/suma/DatabaseHelper.java @@ -177,25 +177,27 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { public boolean checkDataBase() { Log.i(TAG, "Check database"); - SQLiteDatabase checkDB = null; - - try { - String myPath = DATABASE_PATH + DATABASE_NAME; - checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); - } catch (SQLiteException e) { - - // database does't exist yet. - - } - - if (checkDB != null) { - - checkDB.close(); - - } - - Log.i(TAG, "Finished checking database"); - return checkDB != null ? true : false; + /* + * SQLiteDatabase checkDB = null; + * + * try { String myPath = DATABASE_PATH + DATABASE_NAME; checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); } catch + * (SQLiteException e) { + * + * // database does't exist yet. + * + * } + * + * if (checkDB != null) { + * + * checkDB.close(); + * + * } + * + * Log.i(TAG, "Finished checking database"); return checkDB != null ? true : false; + */ + + File dbFile = new File(DATABASE_PATH + DATABASE_NAME); + return dbFile.exists(); } /** diff --git a/src/net/cbaines/suma/SouthamptonUniversityMapActivity.java b/src/net/cbaines/suma/SouthamptonUniversityMapActivity.java index 430bd4f..4bfba52 100644 --- a/src/net/cbaines/suma/SouthamptonUniversityMapActivity.java +++ b/src/net/cbaines/suma/SouthamptonUniversityMapActivity.java @@ -216,9 +216,10 @@ public class SouthamptonUniversityMapActivity extends OrmLiteBaseActivity sites = new ArrayList(size); - - try { - sites.addAll(getHelper().getSiteDao().queryForAll()); - } catch (SQLException e) { - e.printStackTrace(); - } - siteNames = new String[size]; - for (int i = 0; i < size; i++) { - siteNames[i] = sites.get(i).name; - } - - SharedPreferences activityPrefs = getPreferences(0); - Editor editor = activityPrefs.edit(); - - for (int heading = 0; heading < groupHeadings.length; heading++) { - if (heading == 0 || heading == 1) { - for (int child = 0; child < busRoutes.length; child++) { - if (!activityPrefs.contains(groupHeadings[heading] + ":" + busRoutes[child])) { - editor.putBoolean(groupHeadings[heading] + ":" + busRoutes[child], true); - } - } - } else if (heading == 2) { - for (int child = 0; child < buildingTypes.length; child++) { - if (!activityPrefs.contains(groupHeadings[heading] + ":" + buildingTypes[child])) { - editor.putBoolean(groupHeadings[heading] + ":" + buildingTypes[child], true); - } - } - } else if (heading == 3) { - for (int child = 0; child < sites.size(); child++) { - if (!activityPrefs.contains(groupHeadings[heading] + ":" + sites.get(child))) { - editor.putBoolean(groupHeadings[heading] + ":" + sites.get(child), true); - } - } - } else if (heading == 4) { - for (int child = 0; child < other.length; child++) { - if (!activityPrefs.contains(groupHeadings[heading] + ":" + other[child])) { - editor.putBoolean(groupHeadings[heading] + ":" + other[child], true); - } - } - } - } - - editor.commit(); - - activityPrefs.registerOnSharedPreferenceChangeListener(this); - sharedPrefs.registerOnSharedPreferenceChangeListener(this); - - Log.i(TAG, "Finished setting up preferences"); - } - - private void createOverlays() { - Log.i(TAG, "Began creating overlays at " + (System.currentTimeMillis() - startTime)); + private void showOverlays() throws SQLException { + Log.i(TAG, "Began showing overlays at " + (System.currentTimeMillis() - startTime)); if (pastOverlays != null) { Log.i(TAG, "Able to recover some/all of the overlays from a previous activity"); @@ -421,83 +358,97 @@ public class SouthamptonUniversityMapActivity extends OrmLiteBaseActivity(5); + Dao busRouteDao = getHelper().getBusRouteDao(); + for (Iterator routeIter = busRouteDao.iterator(); routeIter.hasNext();) { + BusRoute route = routeIter.next(); + if (activityPrefs.getBoolean("Bus Routes:" + route.code, false)) { + showRouteOverlay(route); + } + } + Log.i(TAG, "Finished loading routes " + (System.currentTimeMillis() - startTime)); - Log.i(TAG, "Finished creating utility overlays " + (System.currentTimeMillis() - startTime)); + Log.i(TAG, "Begining to show the site overlays at " + (System.currentTimeMillis() - startTime)); + try { + Dao siteDao = getHelper().getSiteDao(); + siteOverlays = new HashMap((int) siteDao.countOf()); + for (Site site : siteDao) { + if (activityPrefs.getBoolean("Sites:" + site.name, false)) { + showSiteOverlay(site); + } } - }); + } catch (SQLException e) { + e.printStackTrace(); + } + Log.i(TAG, "Finished showing the site overlays " + (System.currentTimeMillis() - startTime)); - utilityOverlayCreation.start(); + } - Runnable utilityOverlayApplication = new Runnable() { + private void showUtilityOverlays() { + new Thread(new Runnable() { public void run() { - Log.i(TAG, "Begining the application of the utility overlays"); + Log.i(TAG, "Begining showing the utility overlays " + (System.currentTimeMillis() - startTime)); - mapView.getOverlays().add(scaleBarOverlay); + final SharedPreferences activityPrefs = getPreferences(0); + final OverlayRankComparator comparator = new OverlayRankComparator(getPreferences(0)); - mapView.getOverlays().add(myLocationOverlay); + if (scaleBarOverlay != null) { + Log.v(TAG, "ScaleBarOverlay is already created"); + } else { + if (pastOverlays != null && (scaleBarOverlay = (ScaleBarOverlay) pastOverlays.get("Other:Scale Bar")) != null) { + Log.i(TAG, "Finished restoring utility overlays " + (System.currentTimeMillis() - startTime)); + } else { + scaleBarOverlay = new ScaleBarOverlay(instance); + } - Log.v(TAG, "Applyed the utility overlays, now sorting them"); + overlays.put("Other:Scale Bar", scaleBarOverlay); - Collections.sort(mapView.getOverlays(), comparator); + synchronized (mapView.getOverlays()) { + mapView.getOverlays().add(scaleBarOverlay); + mapView.getOverlays().add(myLocationOverlay); + Collections.sort(mapView.getOverlays(), comparator); + } - Log.v(TAG, "Finished sorting the utility overlays them, now applying them"); + } - mapView.invalidate(); + scaleBarOverlay.setEnabled(activityPrefs.getBoolean("Other:Scale Bar", true)); - Log.i(TAG, "Finished loading utility overlays " + (System.currentTimeMillis() - startTime)); + mapView.postInvalidate(); + Log.i(TAG, "Finished showing utility overlays " + (System.currentTimeMillis() - startTime)); } - }; + }).start(); + } - Thread routeOverlayCreation = new Thread(new Runnable() { + private void showRouteOverlay(final BusRoute route) { + new Thread(new Runnable() { public void run() { + Log.i(TAG, "Begining showing route " + route.code + " overlay at " + (System.currentTimeMillis() - startTime)); - try { - Log.i(TAG, "Begining to create the route overlays"); - - SharedPreferences mainPrefs = getPreferences(0); - - routeOverlays = new HashMap(5); - - Dao busRouteDao = getHelper().getBusRouteDao(); - - for (Iterator routeIter = busRouteDao.iterator(); routeIter.hasNext();) { - BusRoute route = routeIter.next(); - - Log.v(TAG, "Looking at route " + route.code); - - if (pastOverlays != null) { - PathOverlay routeOverlay = (PathOverlay) pastOverlays.get("Bus Routes:" + route.code); - if (routeOverlay != null) { - Log.i(TAG, "Restored " + route.code + " route overlay"); - routeOverlays.put(route, routeOverlay); - overlays.put("Bus Routes:" + route.code, routeOverlay); - continue; - } - } + final SharedPreferences activityPrefs = getPreferences(0); + final OverlayRankComparator comparator = new OverlayRankComparator(getPreferences(0)); + PathOverlay routeOverlay; + if ((routeOverlay = routeOverlays.get(route)) != null) { + Log.v(TAG, route.code + " route overlay already existed"); + } else { + if (pastOverlays != null && (routeOverlay = (PathOverlay) pastOverlays.get("Bus Routes:" + route.code)) != null) { + Log.v(TAG, "Restored " + route.code + " route overlay"); + } else { InputStream resource = null; int colour = 0; if (route.code.equals("U1")) { @@ -510,7 +461,7 @@ public class SouthamptonUniversityMapActivity extends OrmLiteBaseActivity siteDao = getHelper().getSiteDao(); - siteOverlays = new HashMap((int) siteDao.countOf()); + final SharedPreferences activityPrefs = getPreferences(0); + final OverlayRankComparator comparator = new OverlayRankComparator(getPreferences(0)); - for (Site site : siteDao) { + PathOverlay siteOverlay; + if ((siteOverlay = siteOverlays.get(site)) != null) { - if (pastOverlays != null) { - PathOverlay overlay = (PathOverlay) pastOverlays.get("Site Outlines:" + site.name); - if (overlay != null) { - Log.i(TAG, "Restored " + site.name + " site overlay"); - siteOverlays.put(site, overlay); - overlays.put("Site Outlines:" + site.name, overlay); - continue; - } - } + } else { + if (pastOverlays != null && (siteOverlay = (PathOverlay) pastOverlays.get("Site Outlines:" + site.name)) != null) { + Log.i(TAG, "Restored " + site.name + " site overlay"); + } else { - PathOverlay overlay = new PathOverlay(Color.BLUE, instance); - Paint paint = overlay.getPaint(); + siteOverlay = new PathOverlay(Color.BLUE, instance); + Paint paint = siteOverlay.getPaint(); paint.setAntiAlias(true); paint.setStrokeWidth(1.5f); for (int i = 0; i < site.outline.points.length; i++) { - overlay.addPoint(site.outline.points[i]); + siteOverlay.addPoint(site.outline.points[i]); } - overlay.addPoint(site.outline.points[0]); - - overlay.setEnabled(mainPrefs.getBoolean("Site Outlines:" + site.name, true)); + siteOverlay.addPoint(site.outline.points[0]); - siteOverlays.put(site, overlay); - overlays.put("Site Outlines:" + site.name, overlay); } - } catch (SQLException e) { - e.printStackTrace(); - } - - Log.i(TAG, "Finished creating site overlays " + (System.currentTimeMillis() - startTime)); - - } - }); - siteOverlayCreation.start(); + siteOverlays.put(site, siteOverlay); + overlays.put("Site Outlines:" + site.name, siteOverlay); - Runnable siteOverlayApplication = new Runnable() { - public void run() { - Log.i(TAG, "Begining applying the site overlays, number of site overlays = " + siteOverlays.size()); + Log.v(TAG, "Applyed the site overlay, now sorting them"); - for (PathOverlay siteOverlay : siteOverlays.values()) { - Log.d(TAG, "Added site overlay"); - mapView.getOverlays().add(siteOverlay); + synchronized (mapView.getOverlays()) { + mapView.getOverlays().add(siteOverlay); + Collections.sort(mapView.getOverlays(), comparator); + } } - Log.v(TAG, "Added the site overlays, now sorting them"); + siteOverlay.setEnabled(activityPrefs.getBoolean("Site Outlines:" + site.name, true)); - Collections.sort(mapView.getOverlays(), comparator); - - Log.v(TAG, "Finished sorting the site overlays them, now applying them"); - - mapView.invalidate(); + mapView.postInvalidate(); - Log.i(TAG, "Finished loading site overlays " + (System.currentTimeMillis() - startTime)); + Log.i(TAG, "Finished showing site " + site.name + " overlay at " + (System.currentTimeMillis() - startTime)); } - }; + }).start(); + } - Thread buildingOverlayCreation = new Thread(new Runnable() { + private void showBuildingOverlay(boolean residential) { + new Thread(new Runnable() { public void run() { - Log.i(TAG, "Begining the creation of the building overlays"); - try { - if (pastOverlays != null) { - residentialBuildingOverlay = (BuildingNumOverlay) pastOverlays.get("Buildings:Residential"); - nonResidentialBuildingOverlay = (BuildingNumOverlay) pastOverlays.get("Buildings:Non-Residential"); - if (residentialBuildingOverlay != null && nonResidentialBuildingOverlay != null) { - overlays.put("Buildings:" + buildingTypes[0], residentialBuildingOverlay); - overlays.put("Buildings:" + buildingTypes[1], nonResidentialBuildingOverlay); - - Log.i(TAG, "Restored building overlays"); - return; - } - } + Log.i(TAG, "Begining showing building overlays at " + (System.currentTimeMillis() - startTime)); - SharedPreferences mainPrefs = getPreferences(0); + final SharedPreferences activityPrefs = getPreferences(0); + final OverlayRankComparator comparator = new OverlayRankComparator(getPreferences(0)); - ArrayList residentialBuildings = new ArrayList(); - ArrayList nonResidentialBuildings = new ArrayList(); + if (residentialBuildingOverlay != null) { - Dao buildingDao = getHelper().getBuildingDao(); + } else { + try { + Log.v(TAG, "Begining the creation of the building overlays"); - for (Building building : buildingDao) { - // Log.v(TAG, "Looking at building " + building.id); - if (building.residential == true) { - // Log.v(TAG, "Its residential"); - if (building.favourite) { - // Log.v(TAG, "Its residential and a favourite"); - residentialBuildings.add(building); - } else { - // Log.v(TAG, "Its residential and not a favourite"); - residentialBuildings.add(0, building); - } - } else { - if (building.favourite) { - // Log.v(TAG, "Its not residential and a favourite"); - nonResidentialBuildings.add(building); - } else { - // Log.v(TAG, "Its not residential and not a favourite"); - nonResidentialBuildings.add(0, building); + if (pastOverlays != null) { + residentialBuildingOverlay = (BuildingNumOverlay) pastOverlays.get("Buildings:Residential"); + nonResidentialBuildingOverlay = (BuildingNumOverlay) pastOverlays.get("Buildings:Non-Residential"); + if (residentialBuildingOverlay != null && nonResidentialBuildingOverlay != null) { + overlays.put("Buildings:" + buildingTypes[0], residentialBuildingOverlay); + overlays.put("Buildings:" + buildingTypes[1], nonResidentialBuildingOverlay); + + Log.i(TAG, "Restored building overlays"); + return; } } - } - - residentialBuildingOverlay = new BuildingNumOverlay(instance, residentialBuildings); - nonResidentialBuildingOverlay = new BuildingNumOverlay(instance, nonResidentialBuildings); - - residentialBuildingOverlay.setEnabled(mainPrefs.getBoolean("Buildings:Residential", false)); - nonResidentialBuildingOverlay.setEnabled(mainPrefs.getBoolean("Buildings:Non-Residential", false)); - - overlays.put("Buildings:" + buildingTypes[0], residentialBuildingOverlay); - overlays.put("Buildings:" + buildingTypes[1], nonResidentialBuildingOverlay); - - Log.i(TAG, "Finished creating building overlays " + (System.currentTimeMillis() - startTime)); - } catch (SQLException e) { - e.printStackTrace(); - } - - } - }); - buildingOverlayCreation.start(); + ArrayList residentialBuildings = new ArrayList(); + ArrayList nonResidentialBuildings = new ArrayList(); - Runnable buildingOverlayApplication = new Runnable() { - public void run() { - Log.i(TAG, "Begining applying the building overlays"); - - mapView.getOverlays().add(residentialBuildingOverlay); - mapView.getOverlays().add(nonResidentialBuildingOverlay); - - Log.v(TAG, "Added the building overlays, now sorting them"); + Dao buildingDao; - Collections.sort(mapView.getOverlays(), comparator); + buildingDao = getHelper().getBuildingDao(); - Log.v(TAG, "Finished sorting the building overlays, now applying them"); - - mapView.invalidate(); + for (Building building : buildingDao) { + // Log.v(TAG, "Looking at building " + building.id); + if (building.residential == true) { + // Log.v(TAG, "Its residential"); + if (building.favourite) { + // Log.v(TAG, "Its residential and a favourite"); + residentialBuildings.add(building); + } else { + // Log.v(TAG, "Its residential and not a favourite"); + residentialBuildings.add(0, building); + } + } else { + if (building.favourite) { + // Log.v(TAG, "Its not residential and a favourite"); + nonResidentialBuildings.add(building); + } else { + // Log.v(TAG, "Its not residential and not a favourite"); + nonResidentialBuildings.add(0, building); + } + } + } - Log.i(TAG, "Finished loading building overlays " + (System.currentTimeMillis() - startTime)); - } - }; + residentialBuildingOverlay = new BuildingNumOverlay(instance, residentialBuildings); + nonResidentialBuildingOverlay = new BuildingNumOverlay(instance, nonResidentialBuildings); - Thread busStopOverlayCreation = new Thread(new Runnable() { + overlays.put("Buildings:" + buildingTypes[0], residentialBuildingOverlay); + overlays.put("Buildings:" + buildingTypes[1], nonResidentialBuildingOverlay); - public void run() { - Log.i(TAG, "Begining the creation of the bus stop overlay"); + Log.v(TAG, "Applyed the site overlay, now sorting them"); - if (pastOverlays != null) { - busStopOverlay = (BusStopOverlay) pastOverlays.get("BusStops"); - if (busStopOverlay != null) { - overlays.put("BusStops", busStopOverlay); + synchronized (mapView.getOverlays()) { + mapView.getOverlays().add(residentialBuildingOverlay); + mapView.getOverlays().add(nonResidentialBuildingOverlay); + Collections.sort(mapView.getOverlays(), comparator); + } - Log.i(TAG, "Restored bus stop overlays"); - return; + } catch (SQLException e) { + e.printStackTrace(); } } - try { - SharedPreferences mainPrefs = getPreferences(0); - - busStopOverlay = new BusStopOverlay(instance); - busStopOverlay.setRoutes(0, mainPrefs.getBoolean("Bus Stops:U1", false)); - busStopOverlay.setRoutes(1, mainPrefs.getBoolean("Bus Stops:U1N", false)); - busStopOverlay.setRoutes(2, mainPrefs.getBoolean("Bus Stops:U2", false)); - busStopOverlay.setRoutes(3, mainPrefs.getBoolean("Bus Stops:U6", false)); - busStopOverlay.setRoutes(4, mainPrefs.getBoolean("Bus Stops:U9", false)); + residentialBuildingOverlay.setEnabled(activityPrefs.getBoolean("Buildings:Residential", false)); + nonResidentialBuildingOverlay.setEnabled(activityPrefs.getBoolean("Buildings:Non-Residential", false)); - overlays.put("BusStops", busStopOverlay); - } catch (SQLException e) { - e.printStackTrace(); - } + mapView.postInvalidate(); - Log.i(TAG, "Finished creating the bus stops overlay " + (System.currentTimeMillis() - startTime)); + Log.i(TAG, "Finished showing building overlays at " + (System.currentTimeMillis() - startTime)); } - }); - - busStopOverlayCreation.start(); + }).start(); + } - Runnable busStopOverlayApplication = new Runnable() { + private void showBusStopOverlay() { + new Thread(new Runnable() { public void run() { - Log.i(TAG, "Begining applying the bus stop overlay"); + Log.i(TAG, "Begining showing bus stop overlays at " + (System.currentTimeMillis() - startTime)); - mapView.getOverlays().add(busStopOverlay); + final SharedPreferences activityPrefs = getPreferences(0); + final OverlayRankComparator comparator = new OverlayRankComparator(getPreferences(0)); - Log.v(TAG, "Added the bus stop overlay, now sorting them"); + if (busStopOverlay != null) { - Collections.sort(mapView.getOverlays(), comparator); + } else { + if (pastOverlays != null && (busStopOverlay = (BusStopOverlay) pastOverlays.get("BusStops")) != null) { + Log.i(TAG, "Restored bus stop overlays"); + } else { + try { + busStopOverlay = new BusStopOverlay(instance); + } catch (SQLException e) { + e.printStackTrace(); + } + } - Log.v(TAG, "Finished sorting the bus stop overlay, now applying them"); + overlays.put("BusStops", busStopOverlay); - mapView.invalidate(); + Log.v(TAG, "Applyed the site overlay, now sorting them"); - Log.i(TAG, "Finished loading bus stop overlay " + (System.currentTimeMillis() - startTime)); - } - }; + synchronized (mapView.getOverlays()) { + mapView.getOverlays().add(busStopOverlay); + Collections.sort(mapView.getOverlays(), comparator); + } + } - while (utilityOverlayCreation != null || routeOverlayCreation != null || siteOverlayCreation != null || buildingOverlayCreation != null - || busStopOverlayCreation != null) { - if (utilityOverlayCreation != null && !utilityOverlayCreation.isAlive()) { - mapView.post(utilityOverlayApplication); - utilityOverlayCreation = null; - } + Log.i(TAG, "Begining the creation of the bus stop overlay"); - if (routeOverlayCreation != null && !routeOverlayCreation.isAlive()) { - mapView.post(routeOverlayApplication); - routeOverlayCreation = null; - } + busStopOverlay.setRoutes(0, activityPrefs.getBoolean("Bus Stops:U1", false)); + busStopOverlay.setRoutes(1, activityPrefs.getBoolean("Bus Stops:U1N", false)); + busStopOverlay.setRoutes(2, activityPrefs.getBoolean("Bus Stops:U2", false)); + busStopOverlay.setRoutes(3, activityPrefs.getBoolean("Bus Stops:U6", false)); + busStopOverlay.setRoutes(4, activityPrefs.getBoolean("Bus Stops:U9", false)); - if (siteOverlayCreation != null && !siteOverlayCreation.isAlive()) { - mapView.post(siteOverlayApplication); - siteOverlayCreation = null; - } + Log.i(TAG, "Finished creating the bus stops overlay " + (System.currentTimeMillis() - startTime)); - if (buildingOverlayCreation != null && !buildingOverlayCreation.isAlive()) { - mapView.post(buildingOverlayApplication); - buildingOverlayCreation = null; - } + mapView.postInvalidate(); - if (busStopOverlayCreation != null && !busStopOverlayCreation.isAlive()) { - mapView.post(busStopOverlayApplication); - busStopOverlayCreation = null; + Log.i(TAG, "Finished showing bus stop overlays at " + (System.currentTimeMillis() - startTime)); } - - Thread.yield(); - } - + }).start(); } @Override @@ -1132,38 +1011,31 @@ public class SouthamptonUniversityMapActivity extends OrmLiteBaseActivity sites = new ArrayList(size); + + try { + sites.addAll(getHelper().getSiteDao().queryForAll()); + } catch (SQLException e) { + e.printStackTrace(); + } + siteNames = new String[size]; + for (int i = 0; i < size; i++) { + siteNames[i] = sites.get(i).name; + } + } catch (SQLException e1) { + e1.printStackTrace(); + } } public void setOnItemClickListener(OnChildClickListener onChildClickListener) { -- cgit v1.2.3