aboutsummaryrefslogtreecommitdiff
path: root/src/net/cbaines/suma/BuildingNumOverlay.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/net/cbaines/suma/BuildingNumOverlay.java')
-rw-r--r--src/net/cbaines/suma/BuildingNumOverlay.java92
1 files changed, 34 insertions, 58 deletions
diff --git a/src/net/cbaines/suma/BuildingNumOverlay.java b/src/net/cbaines/suma/BuildingNumOverlay.java
index 74688a0..887f8b7 100644
--- a/src/net/cbaines/suma/BuildingNumOverlay.java
+++ b/src/net/cbaines/suma/BuildingNumOverlay.java
@@ -31,6 +31,8 @@ import org.osmdroid.views.overlay.Overlay;
import android.app.Activity;
import android.content.Intent;
+import android.content.SharedPreferences;
+import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
@@ -42,10 +44,7 @@ import android.util.Log;
import android.view.MotionEvent;
import android.widget.Toast;
-import com.j256.ormlite.android.apptools.OpenHelperManager;
-import com.j256.ormlite.dao.Dao;
-
-public class BuildingNumOverlay extends Overlay {
+public class BuildingNumOverlay extends Overlay implements Preferences, OnSharedPreferenceChangeListener {
private ArrayList<Building> buildings;
@@ -64,8 +63,6 @@ public class BuildingNumOverlay extends Overlay {
private final MapActivity context;
- private Dao<Building, String> buildingDao;
-
private float userScale = 1f;
public BuildingNumOverlay(MapActivity context, List<Building> buildings) throws SQLException {
@@ -76,8 +73,8 @@ public class BuildingNumOverlay extends Overlay {
marker = context.getResources().getDrawable(R.drawable.building);
favMarker = context.getResources().getDrawable(R.drawable.building_fav);
- DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class);
- buildingDao = helper.getBuildingDao();
+ final SharedPreferences favouritesPrefs = context.getSharedPreferences(FAVOURITES_PREFERENCES, 0);
+ favouritesPrefs.registerOnSharedPreferenceChangeListener(this);
paint = new Paint();
paint.setColor(Color.BLACK);
@@ -91,33 +88,24 @@ public class BuildingNumOverlay extends Overlay {
}
/**
- * Draw a marker on each of our items. populate() must have been called
- * first.<br/>
+ * Draw a marker on each of our items. populate() must have been called first.<br/>
* <br/>
- * The marker will be drawn twice for each Item in the Overlay--once in the
- * shadow phase, skewed and darkened, then again in the non-shadow phase.
- * The bottom-center of the marker will be aligned with the geographical
- * coordinates of the Item.<br/>
+ * The marker will be drawn twice for each Item in the Overlay--once in the shadow phase, skewed and darkened, then again in
+ * the non-shadow phase. The bottom-center of the marker will be aligned with the geographical coordinates of the Item.<br/>
* <br/>
- * The order of drawing may be changed by overriding the getIndexToDraw(int)
- * method. An item may provide an alternate marker via its
- * OverlayItem.getMarker(int) method. If that method returns null, the
- * default marker is used.<br/>
+ * The order of drawing may be changed by overriding the getIndexToDraw(int) method. An item may provide an alternate marker
+ * via its OverlayItem.getMarker(int) method. If that method returns null, the default marker is used.<br/>
* <br/>
- * The focused item is always drawn last, which puts it visually on top of
- * the other items.<br/>
+ * The focused item is always drawn last, which puts it visually on top of the other items.<br/>
*
* @param canvas
- * the Canvas upon which to draw. Note that this may already have
- * a transformation applied, so be sure to leave it the way you
- * found it
+ * the Canvas upon which to draw. Note that this may already have a transformation applied, so be sure to leave it
+ * the way you found it
* @param mapView
- * the MapView that requested the draw. Use
- * MapView.getProjection() to convert between on-screen pixels
- * and latitude/longitude pairs
+ * the MapView that requested the draw. Use MapView.getProjection() to convert between on-screen pixels and
+ * latitude/longitude pairs
* @param shadow
- * if true, draw the shadow layer. If false, draw the overlay
- * contents.
+ * if true, draw the shadow layer. If false, draw the overlay contents.
*/
@Override
public void draw(final Canvas canvas, final MapView mapView, final boolean shadow) {
@@ -138,9 +126,10 @@ public class BuildingNumOverlay extends Overlay {
marker.setBounds(mRect);
favMarker.setBounds(mRect);
+ final SharedPreferences favouritesPrefs = context.getSharedPreferences(FAVOURITES_PREFERENCES, 0);
+
/*
- * Draw in backward cycle, so the items with the least index are on the
- * front.
+ * Draw in backward cycle, so the items with the least index are on the front.
*/
for (Iterator<Building> buildingIter = buildings.iterator(); buildingIter.hasNext();) {
final Building building = buildingIter.next();
@@ -148,9 +137,12 @@ public class BuildingNumOverlay extends Overlay {
// Log.i(TAG, "Looking at drawing stop " + stop.id);
pj.toMapPixels(building.point, mCurScreenCoords);
+ if (!pj.getBoundingBox().increaseByScale(1.2f).contains(building.point)) {
+ continue;
+ }
// draw it
- if (building.favourite) {
+ if (favouritesPrefs.getBoolean(building.id, false)) {
Overlay.drawAt(canvas, favMarker, mCurScreenCoords.x, mCurScreenCoords.y, false);
} else {
Overlay.drawAt(canvas, marker, mCurScreenCoords.x, mCurScreenCoords.y, false);
@@ -208,8 +200,7 @@ public class BuildingNumOverlay extends Overlay {
Log.v(TAG, "building Pressed " + building.id);
if (context.activityToast == null) {
- context.activityToast = Toast.makeText(context, building.name + " (" + building.id + ")",
- Toast.LENGTH_SHORT);
+ context.activityToast = Toast.makeText(context, building.name + " (" + building.id + ")", Toast.LENGTH_SHORT);
} else {
context.activityToast.setDuration(Toast.LENGTH_SHORT);
context.activityToast.setText(building.name + " (" + building.id + ")");
@@ -252,12 +243,13 @@ public class BuildingNumOverlay extends Overlay {
} else {
Log.v(TAG, "building Pressed " + building.id);
- if (building.favourite) {
- building.favourite = false;
+ final SharedPreferences favouritesPrefs = context.getSharedPreferences(FAVOURITES_PREFERENCES, 0);
+
+ if (favouritesPrefs.getBoolean(building.id, false)) {
+ favouritesPrefs.edit().remove(building.id).commit();
if (context.activityToast == null) {
- context.activityToast = Toast.makeText(context, building.id + " removed from favourites",
- Toast.LENGTH_SHORT);
+ context.activityToast = Toast.makeText(context, building.id + " removed from favourites", Toast.LENGTH_SHORT);
} else {
context.activityToast.setDuration(Toast.LENGTH_SHORT);
context.activityToast.setText(building.id + " removed from favourites");
@@ -266,25 +258,16 @@ public class BuildingNumOverlay extends Overlay {
} else {
if (context.activityToast == null) {
- context.activityToast = Toast.makeText(context, building.id + " made a favourite",
- Toast.LENGTH_SHORT);
+ context.activityToast = Toast.makeText(context, building.id + " made a favourite", Toast.LENGTH_SHORT);
} else {
context.activityToast.setDuration(Toast.LENGTH_SHORT);
context.activityToast.setText(building.id + " made a favourite");
}
context.activityToast.show();
- building.favourite = true;
+ favouritesPrefs.edit().putBoolean(building.id, true).commit();
}
- try {
- buildingDao.update(building);
- } catch (SQLException e) {
- e.printStackTrace();
- }
-
- Collections.sort(buildings, new POIFavouriteComparator());
-
mapView.invalidate();
return true;
@@ -313,17 +296,10 @@ public class BuildingNumOverlay extends Overlay {
return null;
}
- public void refresh() {
- for (int i = 0; i < buildings.size(); i++) {
- refresh(buildings.get(i));
- }
- }
+ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
+ Log.i(TAG, "Got a favourites change in the BuildingNumOverlay for key " + key);
- public void refresh(Building building) {
- if (building.favourite) {
- buildings.remove(building);
- buildings.add(building);
- }
+ Collections.sort(buildings, new POIFavouriteComparator(context.getSharedPreferences(FAVOURITES_PREFERENCES, 0)));
}
}