From 9b09f1286a8a003f0d24f745f648748cd3c2f2e2 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Sun, 26 Feb 2012 23:21:13 +0000 Subject: More bug fixes, with the location overlay. --- src/net/cbaines/suma/BuildingNumOverlay.java | 2 + src/net/cbaines/suma/BusStopOverlay.java | 4 +- src/net/cbaines/suma/MapActivity.java | 43 ++- src/net/cbaines/suma/POI.java | 1 - src/net/cbaines/suma/ViewDialog.java | 422 ++++++++++++++------------- 5 files changed, 252 insertions(+), 220 deletions(-) (limited to 'src') diff --git a/src/net/cbaines/suma/BuildingNumOverlay.java b/src/net/cbaines/suma/BuildingNumOverlay.java index 887f8b7..cc20003 100644 --- a/src/net/cbaines/suma/BuildingNumOverlay.java +++ b/src/net/cbaines/suma/BuildingNumOverlay.java @@ -268,6 +268,8 @@ public class BuildingNumOverlay extends Overlay implements Preferences, OnShared favouritesPrefs.edit().putBoolean(building.id, true).commit(); } + Collections.sort(buildings, new POIFavouriteComparator(context.getSharedPreferences(FAVOURITES_PREFERENCES, 0))); + mapView.invalidate(); return true; diff --git a/src/net/cbaines/suma/BusStopOverlay.java b/src/net/cbaines/suma/BusStopOverlay.java index 7894be2..4705d1d 100644 --- a/src/net/cbaines/suma/BusStopOverlay.java +++ b/src/net/cbaines/suma/BusStopOverlay.java @@ -246,7 +246,7 @@ public class BusStopOverlay extends Overlay implements RouteColorConstants, Pref SharedPreferences favouritesPreferences = context.getSharedPreferences(FAVOURITES_PREFERENCES, 0); if (favouritesPreferences.getBoolean(busStop.id, false)) { - favouritesPreferences.edit().putBoolean(busStop.id, false).commit(); + favouritesPreferences.edit().remove(busStop.id).commit(); if (context.activityToast == null) { context.activityToast = Toast.makeText(context, busStop.id + " removed from favourites", Toast.LENGTH_SHORT); @@ -267,6 +267,8 @@ public class BusStopOverlay extends Overlay implements RouteColorConstants, Pref favouritesPreferences.edit().putBoolean(busStop.id, true).commit(); } + Collections.sort(busStops, new POIFavouriteComparator(context.getSharedPreferences(FAVOURITES_PREFERENCES, 0))); + mapView.invalidate(); return true; diff --git a/src/net/cbaines/suma/MapActivity.java b/src/net/cbaines/suma/MapActivity.java index 14a1dd4..07806fa 100644 --- a/src/net/cbaines/suma/MapActivity.java +++ b/src/net/cbaines/suma/MapActivity.java @@ -159,7 +159,7 @@ public class MapActivity extends OrmLiteBaseActivity implements static final boolean MY_LOCATION_OVERLAY_COMPASS_ENABLED_BY_DEFAULT = true; private static final int MY_LOCATION_OVERLAY_RANK = 2; - static final String[] OTHER_OVERLAY_NAMES = { SCALE_BAR_OVERLAY, MY_LOCATION_OVERLAY, MY_LOCATION_OVERLAY_COMPASS }; + static final String[] OTHER_OVERLAY_NAMES = { SCALE_BAR_OVERLAY, MY_LOCATION_OVERLAY_COMPASS, MY_LOCATION_OVERLAY }; // Other bits @@ -209,6 +209,7 @@ public class MapActivity extends OrmLiteBaseActivity implements */ Log.i(TAG, "Starting creating myLocationOverlay"); myLocationOverlay = new MyLocationOverlay(instance, mapView); + myLocationOverlay.setEnabled(true); Log.i(TAG, "Finished creating myLocationOverlay"); while (databaseThread.isAlive()) { @@ -263,14 +264,15 @@ public class MapActivity extends OrmLiteBaseActivity implements final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); final SharedPreferences activityPrefs = getPreferences(0); - if (activityPrefs.getBoolean(OTHER_OVERLAYS + MY_LOCATION_OVERLAY_COMPASS, false)) { + if (activityPrefs.getBoolean(OTHER_OVERLAYS + MY_LOCATION_OVERLAY_COMPASS, + MY_LOCATION_OVERLAY_COMPASS_ENABLED_BY_DEFAULT)) { myLocationOverlay.enableCompass(); } else { myLocationOverlay.disableCompass(); } - if (activityPrefs.getBoolean(OTHER_OVERLAYS + MY_LOCATION_OVERLAY, false) - && sharedPrefs.getBoolean(GPS_ENABLED, false)) { + if (activityPrefs.getBoolean(OTHER_OVERLAYS + MY_LOCATION_OVERLAY, MY_LOCATION_OVERLAY_ENABLED_BY_DEFAULT) + && sharedPrefs.getBoolean(GPS_ENABLED, GPS_ENABLED_BY_DEFAULT)) { myLocationOverlay.enableMyLocation(); } else { myLocationOverlay.disableMyLocation(); @@ -683,7 +685,7 @@ public class MapActivity extends OrmLiteBaseActivity implements buildingDao = getHelper().getBuildingDao(); - final SharedPreferences favouritesPrefs = getSharedPreferences(FAVOURITES_PREFERENCES, 0); + final SharedPreferences favouritesPrefs = getSharedPreferences(FAVOURITES_PREFERENCES, MODE_PRIVATE); for (Building building : buildingDao) { if (building.residential == true) { @@ -903,8 +905,10 @@ public class MapActivity extends OrmLiteBaseActivity implements return true; case R.id.menu_find_my_location: final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); - if (sharedPrefs.getBoolean("GPSEnabled", false)) { + if (sharedPrefs.getBoolean(GPS_ENABLED, GPS_ENABLED_BY_DEFAULT)) { + myLocationOverlay.enableMyLocation(); GeoPoint userLocation = myLocationOverlay.getMyLocation(); + sharedPrefs.edit().putBoolean(MY_LOCATION_OVERLAY, true).commit(); if (userLocation != null) { Log.i(TAG, "Found user location, scrolling to " + userLocation); mapController.animateTo(userLocation); @@ -916,8 +920,17 @@ public class MapActivity extends OrmLiteBaseActivity implements switch (which) { case DialogInterface.BUTTON_POSITIVE: Editor editor = sharedPrefs.edit(); - editor.putBoolean("GPSEnabled", true); + editor.putBoolean(GPS_ENABLED, true); editor.commit(); + + myLocationOverlay.enableMyLocation(); + GeoPoint userLocation = myLocationOverlay.getMyLocation(); + sharedPrefs.edit().putBoolean(MY_LOCATION_OVERLAY, true).commit(); + if (userLocation != null) { + Log.i(TAG, "Found user location, scrolling to " + userLocation); + mapController.animateTo(userLocation); + myLocationOverlay.enableFollowLocation(); + } break; case DialogInterface.BUTTON_NEGATIVE: @@ -966,8 +979,18 @@ public class MapActivity extends OrmLiteBaseActivity implements Dao buildingDao = getHelper().getBuildingDao(); Dao busStopDao = getHelper().getBusStopDao(); - newFavouriteItems.addAll(buildingDao.queryForEq(POI.FAVOURITE_FIELD_NAME, true)); - newFavouriteItems.addAll(busStopDao.queryForEq(POI.FAVOURITE_FIELD_NAME, true)); + final SharedPreferences favouritesPrefs = getSharedPreferences(FAVOURITES_PREFERENCES, MODE_PRIVATE); + for (String id : favouritesPrefs.getAll().keySet()) { + Building building; + BusStop busStop; + if ((building = buildingDao.queryForId(id)) != null) { + newFavouriteItems.add(building); + } else if ((busStop = busStopDao.queryForId(id)) != null) { + newFavouriteItems.add(busStop); + } else { + Log.e(TAG, "Item in favourites " + id + " cannot be found"); + } + } } catch (SQLException e) { e.printStackTrace(); } @@ -1253,6 +1276,7 @@ public class MapActivity extends OrmLiteBaseActivity implements } else { myLocationOverlay.disableCompass(); } + mapView.invalidate(); } else if (key.substring(OTHER_OVERLAYS.length(), key.length()).equals(MY_LOCATION_OVERLAY)) { final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); @@ -1262,6 +1286,7 @@ public class MapActivity extends OrmLiteBaseActivity implements } else { myLocationOverlay.disableMyLocation(); } + mapView.invalidate(); } else { Log.e(TAG, "Unhandled preference key " + key); } diff --git a/src/net/cbaines/suma/POI.java b/src/net/cbaines/suma/POI.java index e815731..485845c 100644 --- a/src/net/cbaines/suma/POI.java +++ b/src/net/cbaines/suma/POI.java @@ -32,7 +32,6 @@ public abstract class POI { public static final String ID_FIELD_NAME = "id"; public static final String POINT_FIELD_NAME = "point"; - public static final String FAVOURITE_FIELD_NAME = "favourite"; POI() { } diff --git a/src/net/cbaines/suma/ViewDialog.java b/src/net/cbaines/suma/ViewDialog.java index 432fec1..e97cbdf 100644 --- a/src/net/cbaines/suma/ViewDialog.java +++ b/src/net/cbaines/suma/ViewDialog.java @@ -36,239 +36,243 @@ import android.widget.TextView; class ViewDialog extends Dialog implements OnChildClickListener { - private final ExpandableListView epView; - - private static final String TAG = "ViewDialog"; - - private final MyExpandableListAdapter mAdapter; - - private OnChildClickListener listener; - - private String[] busRoutes; - private String[] buildingTypes; - private String[] other; - private String[] groupHeadings; - private String[] siteNames; - - protected MapActivity context; - - public ViewDialog(MapActivity context) { - super(context); - - this.context = context; - - busRoutes = context.getResources().getStringArray(R.array.uniLinkBusRoutes); - buildingTypes = context.getResources().getStringArray(R.array.buildingTypes); - other = context.getResources().getStringArray(R.array.utilityOverlays); - groupHeadings = context.getResources().getStringArray(R.array.preferencesHeadings); - siteNames = MapActivity.SITE_NAMES; // TODO: Temp hack, should be included in the strings res for translation - // purposes? - - setContentView(R.layout.view_dialog); - setTitle("Select the map elements to display"); - - WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); - lp.copyFrom(this.getWindow().getAttributes()); - lp.width = WindowManager.LayoutParams.FILL_PARENT; - lp.height = WindowManager.LayoutParams.FILL_PARENT; - - this.getWindow().setAttributes(lp); - - epView = (ExpandableListView) findViewById(R.id.view_list); - mAdapter = new MyExpandableListAdapter(context); - epView.setAdapter(mAdapter); - epView.setOnChildClickListener(this); - - } - - public void setOnItemClickListener(OnChildClickListener onChildClickListener) { - Log.i(TAG, "Listener set for dialog"); - listener = onChildClickListener; - } - - class MyExpandableListAdapter extends BaseExpandableListAdapter implements Preferences { - - private LayoutInflater inflater; - - private static final String TAG = "MyExpandableListAdapter"; - - // Bus Stops - // |_ U1 - // |_ U1N - // |_ U2 - // |_ U6 - // |_ U9 - // Bus Routes - // |_ U1 - // |_ U1N - // |_ U2 - // |_ U6 - // |_ U9 - // Buildings - // |_ Residential - // |_ Non-Residential - // Site Outlines - // |_ Highfield Campus - // |_ Boldrewood Campus - // |_ Avenue Campus - // |_ Winchester School of Art - // |_ The University of Southampton Science Park - // |_ National Oceanography Centre Campus - // |_ Boat House - // |_ Southampton General Hospital - // |_ Royal South Hants Hospital - // |_ Belgrave Industrial Site - // |_ Highfield Hall - // |_ Glen Eyre Hall - // |_ South Hill Hall - // |_ Chamberlain Hall - // |_ Hartley Grove - // |_ Bencraft Hall - // |_ Connaught Hall - // |_ Montefiore Hall - // |_ Stoneham Hall - // |_ Erasmus Park - // Other - // |_ Scale Bar - // |_ Compass - // |_ My Location - - MyExpandableListAdapter(Context context) { - inflater = LayoutInflater.from(context); - } + private final ExpandableListView epView; + + private static final String TAG = "ViewDialog"; + + private final MyExpandableListAdapter mAdapter; + + private OnChildClickListener listener; + + private String[] busRoutes; + private String[] buildingTypes; + private String[] other; + private String[] groupHeadings; + private String[] siteNames; + + protected MapActivity context; + + public ViewDialog(MapActivity context) { + super(context); + + this.context = context; + + busRoutes = context.getResources().getStringArray(R.array.uniLinkBusRoutes); + buildingTypes = context.getResources().getStringArray(R.array.buildingTypes); + other = context.getResources().getStringArray(R.array.utilityOverlays); + groupHeadings = context.getResources().getStringArray(R.array.preferencesHeadings); + siteNames = MapActivity.SITE_NAMES; // TODO: Temp hack, should be included in the strings res for translation + // purposes? + + setContentView(R.layout.view_dialog); + setTitle("Select the map elements to display"); + + WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); + lp.copyFrom(this.getWindow().getAttributes()); + lp.width = WindowManager.LayoutParams.FILL_PARENT; + lp.height = WindowManager.LayoutParams.FILL_PARENT; + + this.getWindow().setAttributes(lp); + + epView = (ExpandableListView) findViewById(R.id.view_list); + mAdapter = new MyExpandableListAdapter(context); + epView.setAdapter(mAdapter); + epView.setOnChildClickListener(this); - public Object getChild(int groupPosition, int childPosition) { - if (groupPosition == 0 || groupPosition == 1) { - return busRoutes[childPosition]; - } else if (groupPosition == 2) { - return buildingTypes[childPosition]; - } else if (groupPosition == 3) { - return siteNames[childPosition]; - } else if (groupPosition == 4) { - return other[childPosition]; - } else { - Log.e(TAG, "Unrecognised groupPosition " + groupPosition); - return null; - } } - public long getChildId(int groupPosition, int childPosition) { - return groupPosition * 50 + childPosition; + public void setOnItemClickListener(OnChildClickListener onChildClickListener) { + Log.i(TAG, "Listener set for dialog"); + listener = onChildClickListener; } - public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, - ViewGroup parent) { - View v = null; - if (convertView != null) - v = convertView; - else - v = inflater.inflate(R.layout.view_child_row, parent, false); - String c = (String) getChild(groupPosition, childPosition); - TextView childName = (TextView) v.findViewById(R.id.childname); - if (childName != null) - childName.setText(c); - CheckBox cb = (CheckBox) v.findViewById(R.id.check1); - cb.setClickable(false); - cb.setFocusable(false); - if (context == null) { - Log.e(TAG, "context == null"); - } - SharedPreferences activityPrefs = context.getPreferences(0); - - String str = MapActivity.PREFERENCES_GROUPS[groupPosition] - + MapActivity.PREFERENCES_CHILDREN[groupPosition][childPosition]; - - if (groupPosition == 0) { - cb.setChecked(activityPrefs.getBoolean(str, MapActivity.UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - } else if (groupPosition == 1) { - cb.setChecked(activityPrefs.getBoolean(str, MapActivity.BUS_ROUTE_OVERLAYS_ENABLED_BY_DEFAULT)); - } else if (groupPosition == 2) { - if (childPosition == 0) { - cb.setChecked(activityPrefs.getBoolean(str, - MapActivity.RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT)); - } else { - cb.setChecked(activityPrefs.getBoolean(str, - MapActivity.NON_RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT)); + class MyExpandableListAdapter extends BaseExpandableListAdapter implements Preferences { + + private LayoutInflater inflater; + + private static final String TAG = "MyExpandableListAdapter"; + + // Bus Stops + // |_ U1 + // |_ U1N + // |_ U2 + // |_ U6 + // |_ U9 + // Bus Routes + // |_ U1 + // |_ U1N + // |_ U2 + // |_ U6 + // |_ U9 + // Buildings + // |_ Residential + // |_ Non-Residential + // Site Outlines + // |_ Highfield Campus + // |_ Boldrewood Campus + // |_ Avenue Campus + // |_ Winchester School of Art + // |_ The University of Southampton Science Park + // |_ National Oceanography Centre Campus + // |_ Boat House + // |_ Southampton General Hospital + // |_ Royal South Hants Hospital + // |_ Belgrave Industrial Site + // |_ Highfield Hall + // |_ Glen Eyre Hall + // |_ South Hill Hall + // |_ Chamberlain Hall + // |_ Hartley Grove + // |_ Bencraft Hall + // |_ Connaught Hall + // |_ Montefiore Hall + // |_ Stoneham Hall + // |_ Erasmus Park + // Other + // |_ Scale Bar + // |_ Compass + // |_ My Location + + MyExpandableListAdapter(Context context) { + inflater = LayoutInflater.from(context); } - } else if (groupPosition == 3) { - cb.setChecked(activityPrefs.getBoolean(str, MapActivity.SITE_OVERLAYS_ENABLED_BY_DEFAULT)); - } else if (groupPosition == 4) { - cb.setChecked(activityPrefs.getBoolean(str, MapActivity.SCALE_BAR_OVERLAY_ENABLED_BY_DEFAULT)); - } - return v; - } - public int getChildrenCount(int groupPosition) { - if (groupPosition == 0 || groupPosition == 1) { - return busRoutes.length; - } else if (groupPosition == 2) { - return buildingTypes.length; - } else if (groupPosition == 3) { - return siteNames.length; - } else if (groupPosition == 4) { - return other.length; - } - return 0; - } + public Object getChild(int groupPosition, int childPosition) { + if (groupPosition == 0 || groupPosition == 1) { + return busRoutes[childPosition]; + } else if (groupPosition == 2) { + return buildingTypes[childPosition]; + } else if (groupPosition == 3) { + return siteNames[childPosition]; + } else if (groupPosition == 4) { + return other[childPosition]; + } else { + Log.e(TAG, "Unrecognised groupPosition " + groupPosition); + return null; + } + } - public Object getGroup(int groupPosition) { - return groupHeadings[groupPosition]; - } + public long getChildId(int groupPosition, int childPosition) { + return groupPosition * 50 + childPosition; + } - public int getGroupCount() { - return groupHeadings.length; - } + public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { + View v = null; + if (convertView != null) + v = convertView; + else + v = inflater.inflate(R.layout.view_child_row, parent, false); + String c = (String) getChild(groupPosition, childPosition); + TextView childName = (TextView) v.findViewById(R.id.childname); + if (childName != null) + childName.setText(c); + CheckBox cb = (CheckBox) v.findViewById(R.id.check1); + cb.setClickable(false); + cb.setFocusable(false); + if (context == null) { + Log.e(TAG, "context == null"); + } + SharedPreferences activityPrefs = context.getPreferences(0); + + String str = MapActivity.PREFERENCES_GROUPS[groupPosition] + + MapActivity.PREFERENCES_CHILDREN[groupPosition][childPosition]; + + if (groupPosition == 0) { + cb.setChecked(activityPrefs.getBoolean(str, MapActivity.UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + } else if (groupPosition == 1) { + cb.setChecked(activityPrefs.getBoolean(str, MapActivity.BUS_ROUTE_OVERLAYS_ENABLED_BY_DEFAULT)); + } else if (groupPosition == 2) { + if (childPosition == 0) { + cb.setChecked(activityPrefs.getBoolean(str, MapActivity.RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT)); + } else { + cb.setChecked(activityPrefs.getBoolean(str, MapActivity.NON_RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT)); + } + } else if (groupPosition == 3) { + cb.setChecked(activityPrefs.getBoolean(str, MapActivity.SITE_OVERLAYS_ENABLED_BY_DEFAULT)); + } else if (groupPosition == 4) { + if (childPosition == 0) { + cb.setChecked(activityPrefs.getBoolean(str, MapActivity.SCALE_BAR_OVERLAY_ENABLED_BY_DEFAULT)); + } else if (childPosition == 1) { + cb.setChecked(activityPrefs.getBoolean(str, MapActivity.MY_LOCATION_OVERLAY_COMPASS_ENABLED_BY_DEFAULT)); + } else if (childPosition == 2) { + cb.setChecked(activityPrefs.getBoolean(str, MapActivity.MY_LOCATION_OVERLAY_ENABLED_BY_DEFAULT)); + } + } + return v; + } - public long getGroupId(int groupPosition) { - return groupPosition * 5; - } + public int getChildrenCount(int groupPosition) { + if (groupPosition == 0 || groupPosition == 1) { + return busRoutes.length; + } else if (groupPosition == 2) { + return buildingTypes.length; + } else if (groupPosition == 3) { + return siteNames.length; + } else if (groupPosition == 4) { + return other.length; + } + return 0; + } - public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { - View v = null; - if (convertView != null) - v = convertView; - else - v = inflater.inflate(R.layout.view_group_row, parent, false); - String gt = (String) getGroup(groupPosition); - TextView colorGroup = (TextView) v.findViewById(R.id.childname); - if (gt != null) - colorGroup.setText(gt); - return v; - } + public Object getGroup(int groupPosition) { + return groupHeadings[groupPosition]; + } - public boolean hasStableIds() { - return true; - } + public int getGroupCount() { + return groupHeadings.length; + } + + public long getGroupId(int groupPosition) { + return groupPosition * 5; + } + + public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { + View v = null; + if (convertView != null) + v = convertView; + else + v = inflater.inflate(R.layout.view_group_row, parent, false); + String gt = (String) getGroup(groupPosition); + TextView colorGroup = (TextView) v.findViewById(R.id.childname); + if (gt != null) + colorGroup.setText(gt); + return v; + } + + public boolean hasStableIds() { + return true; + } + + public boolean isChildSelectable(int groupPosition, int childPosition) { + return true; + } - public boolean isChildSelectable(int groupPosition, int childPosition) { - return true; } - } + public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { + Log.i(TAG, "Got view dialog click at " + groupPosition + ":" + childPosition); - public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { - Log.i(TAG, "Got view dialog click at " + groupPosition + ":" + childPosition); + SharedPreferences activityPrefs = context.getPreferences(0); - SharedPreferences activityPrefs = context.getPreferences(0); + Editor editor = activityPrefs.edit(); - Editor editor = activityPrefs.edit(); + CheckBox cb = (CheckBox) v.findViewById(R.id.check1); - CheckBox cb = (CheckBox) v.findViewById(R.id.check1); + String str = MapActivity.PREFERENCES_GROUPS[groupPosition] + + MapActivity.PREFERENCES_CHILDREN[groupPosition][childPosition]; - String str = MapActivity.PREFERENCES_GROUPS[groupPosition] - + MapActivity.PREFERENCES_CHILDREN[groupPosition][childPosition]; + Log.i(TAG, "Putting " + str + " to " + (!cb.isChecked())); + editor.putBoolean(str, !cb.isChecked()); - editor.putBoolean(str, !cb.isChecked()); + editor.commit(); - editor.commit(); + mAdapter.notifyDataSetInvalidated(); - mAdapter.notifyDataSetInvalidated(); + if (listener != null) { + listener.onChildClick(parent, v, groupPosition, childPosition, id); + } - if (listener != null) { - listener.onChildClick(parent, v, groupPosition, childPosition, id); + return true; } - return true; - } - } \ No newline at end of file -- cgit v1.2.3