From 46143c64dbdcbbfb478b2df4949c617682427d63 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Wed, 8 Feb 2012 12:01:04 +0000 Subject: Moved rdf stuff to new branch --- src/net/cbaines/suma/BuildingActivity.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/net/cbaines/suma/BuildingActivity.java b/src/net/cbaines/suma/BuildingActivity.java index 7637439..987d139 100644 --- a/src/net/cbaines/suma/BuildingActivity.java +++ b/src/net/cbaines/suma/BuildingActivity.java @@ -25,9 +25,6 @@ import java.util.HashSet; import android.content.Context; import android.os.Bundle; -import com.hp.hpl.jena.rdf.model.Model; -import com.hp.hpl.jena.rdf.model.ModelFactory; -import com.hp.hpl.jena.util.FileManager; import com.j256.ormlite.android.apptools.OrmLiteBaseActivity; public class BuildingActivity extends OrmLiteBaseActivity { @@ -47,7 +44,7 @@ public class BuildingActivity extends OrmLiteBaseActivity { final DatabaseHelper helper = getHelper(); // create an empty model - Model model = ModelFactory.createDefaultModel(); + // Model model = ModelFactory.createDefaultModel(); // use the FileManager to find the input file InputStream in = getResources().openRawResource(R.raw.u9); @@ -56,7 +53,7 @@ public class BuildingActivity extends OrmLiteBaseActivity { } // read the RDF/XML file - model.read(in, null); + // model.read(in, null); -- cgit v1.2.3 From 420be2795802a4a828c2d0063b9a9cdf382130e1 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Mon, 20 Feb 2012 10:29:21 +0000 Subject: Added libs to folder libs/ --- src/net/cbaines/suma/BusActivity.java | 576 +++++++++--------- src/net/cbaines/suma/BusSpecificStopView.java | 256 ++++---- src/net/cbaines/suma/BusStopActivity.java | 836 +++++++++++++------------- src/net/cbaines/suma/StopView.java | 247 ++++---- 4 files changed, 1005 insertions(+), 910 deletions(-) (limited to 'src') diff --git a/src/net/cbaines/suma/BusActivity.java b/src/net/cbaines/suma/BusActivity.java index b802cfb..2b76176 100644 --- a/src/net/cbaines/suma/BusActivity.java +++ b/src/net/cbaines/suma/BusActivity.java @@ -24,320 +24,338 @@ import android.widget.Toast; import com.j256.ormlite.android.apptools.OrmLiteBaseActivity; -public class BusActivity extends OrmLiteBaseActivity implements Preferences { - final static String TAG = "BusActivity"; - - private TextView U1RouteTextView; - private TextView U1NRouteTextView; - private TextView U2RouteTextView; - private TextView U6RouteTextView; - private TextView U9RouteTextView; - - private TextView busIDTextView; - - private TextView busContentMessage; - private LinearLayout busActivityContentLayout; - - Toast activityToast; - - /** - * The bus this activity is focused on - */ - private Bus bus; - /** - * The bus stop this activity is working from - */ - private BusStop busStop; - - Runnable refreshData; - - protected Timetable timetable; - private Timetable visibleTimetable; - - private ListView timetableView; - - private Context instance; - - // BusStops and if they are being updated by the handler - List busStops; - - private HashMap tasks = new HashMap(); - - Handler handler; - - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.bus_activity); - instance = this; - - String busID = getIntent().getExtras().getString("busID"); - String busStopID = getIntent().getExtras().getString("busStopID"); - final DatabaseHelper helper = getHelper(); - - try { - List buses = helper.getBusDao().queryForEq(Bus.ID_FIELD_NAME, busID); - bus = null; - if (buses.size() == 0) { - Log.e(TAG, "Bus " + busID + " not found!"); - } else if (buses.size() == 1) { - bus = buses.get(0); - } else if (buses.size() > 1) { - Log.e(TAG, "Found more than one bus? " + busID); - } - - helper.getBusRouteDao().refresh(bus.route); - - busStop = null; - if (busStopID != null) { - List busStops = helper.getBusStopDao().queryForEq(BusStop.ID_FIELD_NAME, busStopID); - if (busStops.size() == 0) { - Log.e(TAG, "BusStop " + busStopID + " not found!"); - } else if (busStops.size() == 1) { - busStop = busStops.get(0); - } else if (busStops.size() > 1) { - Log.e(TAG, "Found more than one busStop? " + busStopID); - } - } - - U1RouteTextView = (TextView) findViewById(R.id.busActivityU1); - U1NRouteTextView = (TextView) findViewById(R.id.busActivityU1N); - U2RouteTextView = (TextView) findViewById(R.id.busActivityU2); - U6RouteTextView = (TextView) findViewById(R.id.busActivityU6); - U9RouteTextView = (TextView) findViewById(R.id.busActivityU9); - - busIDTextView = (TextView) findViewById(R.id.busActivityBusID); - - busContentMessage = (TextView) findViewById(R.id.busActivityMessage); - busActivityContentLayout = (LinearLayout) findViewById(R.id.busActivityContentLayout); - timetableView = (ListView) findViewById(R.id.busActivityTimes); - - if (bus.id != null) { - Log.i(TAG, "Bus id is not null (" + bus.id + ") setting busIDTextView"); - busIDTextView.setText(bus.id + " " + bus.getName()); - } else { - Log.w(TAG, "Bus id is null?"); - // Might not ever happen - busIDTextView.setText("Unidentified"); - } - - U1RouteTextView.setVisibility(View.GONE); - U1NRouteTextView.setVisibility(View.GONE); - U2RouteTextView.setVisibility(View.GONE); - U6RouteTextView.setVisibility(View.GONE); - U9RouteTextView.setVisibility(View.GONE); - - // if (bus.route.uniLink) { - Log.i(TAG, "Bus is uniLink"); - if (bus.route.code.equals("U1")) { - U1RouteTextView.setVisibility(View.VISIBLE); - } else if (bus.route.code.equals("U1N")) { - U1NRouteTextView.setVisibility(View.VISIBLE); - } else if (bus.route.code.equals("U2")) { - U2RouteTextView.setVisibility(View.VISIBLE); - } else if (bus.route.code.equals("U6")) { - U6RouteTextView.setVisibility(View.VISIBLE); - } else if (bus.route.code.equals("U9")) { - U9RouteTextView.setVisibility(View.VISIBLE); - } else { - Log.e(TAG, "Route not found " + bus.route.code); - } - // } else { - // Log.i(TAG, "Bus is not uniLink"); - // } - - } catch (NumberFormatException e) { - e.printStackTrace(); - } catch (SQLException e) { - e.printStackTrace(); - } +public class BusActivity extends OrmLiteBaseActivity implements + Preferences { + final static String TAG = "BusActivity"; - busStops = bus.route.getRouteSection(instance, bus.direction); - Log.i(TAG, "Got " + busStops.size() + " bus stops for this bus"); + private TextView U1RouteTextView; + private TextView U1NRouteTextView; + private TextView U2RouteTextView; + private TextView U6RouteTextView; + private TextView U9RouteTextView; - if (bus.destination != null) { - Log.i(TAG, "Bus destination is " + bus.destination); - } else { - Log.i(TAG, "Bus destination is null"); - } + private TextView busIDTextView; + + private TextView busContentMessage; + private LinearLayout busActivityContentLayout; + + Toast activityToast; - /* - * for (int i = 0;; i++) { BusStop nextStop = bus.route.moveInRoute(instance, busStops.get(i), bus.direction, - * 1); - * - * if (nextStop.equals(busStop) || (bus.destination != null && bus.destination.equals(nextStop))) { break; } - * - * busStops.add(nextStop); busStopsActive.add(false); - * - * if (busStops.size() > 50) { Log.e(TAG, "Got more than 50 bus stops"); break; } } + /** + * The bus this activity is focused on */ + private Bus bus; + /** + * The bus stop this activity is working from + */ + private BusStop busStop; + + Runnable refreshData; + + protected Timetable timetable; + private Timetable visibleTimetable; + + private ListView timetableView; - refreshData = new Runnable() { - @Override - public void run() { - for (int num = timetableView.getFirstVisiblePosition(); num < timetableView.getLastVisiblePosition(); num++) { - Stop stop = timetable.get(num); + private Context instance; - GetTimetableStopTask task = tasks.get(busStops.get(num)); + // BusStops and if they are being updated by the handler + List busStops; - if (stop.timeOfFetch == null || (stop.timeOfFetch.getTime() - System.currentTimeMillis()) > 20000) { - if (task != null) { - if (task.getStatus() == AsyncTask.Status.FINISHED) { - task = null; - } + private HashMap tasks = new HashMap(); + + Handler handler; + + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.bus_activity); + instance = this; + + String busID = getIntent().getExtras().getString("busID"); + String busStopID = getIntent().getExtras().getString("busStopID"); + final DatabaseHelper helper = getHelper(); + + try { + List buses = helper.getBusDao().queryForEq(Bus.ID_FIELD_NAME, + busID); + bus = null; + if (buses.size() == 0) { + Log.e(TAG, "Bus " + busID + " not found!"); + } else if (buses.size() == 1) { + bus = buses.get(0); + } else if (buses.size() > 1) { + Log.e(TAG, "Found more than one bus? " + busID); } - if (task == null) { - task = new GetTimetableStopTask(); - BusStop[] str = { stop.busStop }; - task.execute(str); - tasks.put(stop.busStop, task); + helper.getBusRouteDao().refresh(bus.route); + + busStop = null; + if (busStopID != null) { + List busStops = helper.getBusStopDao().queryForEq( + BusStop.ID_FIELD_NAME, busStopID); + if (busStops.size() == 0) { + Log.e(TAG, "BusStop " + busStopID + " not found!"); + } else if (busStops.size() == 1) { + busStop = busStops.get(0); + } else if (busStops.size() > 1) { + Log.e(TAG, "Found more than one busStop? " + busStopID); + } } - } - } - handler.postDelayed(refreshData, 50000); - } - }; + U1RouteTextView = (TextView) findViewById(R.id.busActivityU1); + U1NRouteTextView = (TextView) findViewById(R.id.busActivityU1N); + U2RouteTextView = (TextView) findViewById(R.id.busActivityU2); + U6RouteTextView = (TextView) findViewById(R.id.busActivityU6); + U9RouteTextView = (TextView) findViewById(R.id.busActivityU9); + + busIDTextView = (TextView) findViewById(R.id.busActivityBusID); + + busContentMessage = (TextView) findViewById(R.id.busActivityMessage); + busActivityContentLayout = (LinearLayout) findViewById(R.id.busActivityContentLayout); + timetableView = (ListView) findViewById(R.id.busActivityTimes); + + if (bus.id != null) { + Log.i(TAG, "Bus id is not null (" + bus.id + + ") setting busIDTextView"); + busIDTextView.setText(bus.id + " " + bus.getName()); + } else { + Log.w(TAG, "Bus id is null?"); + // Might not ever happen + busIDTextView.setText("Unidentified"); + } - } + U1RouteTextView.setVisibility(View.GONE); + U1NRouteTextView.setVisibility(View.GONE); + U2RouteTextView.setVisibility(View.GONE); + U6RouteTextView.setVisibility(View.GONE); + U9RouteTextView.setVisibility(View.GONE); + + // if (bus.route.uniLink) { + Log.i(TAG, "Bus is uniLink"); + if (bus.route.code.equals("U1")) { + U1RouteTextView.setVisibility(View.VISIBLE); + } else if (bus.route.code.equals("U1N")) { + U1NRouteTextView.setVisibility(View.VISIBLE); + } else if (bus.route.code.equals("U2")) { + U2RouteTextView.setVisibility(View.VISIBLE); + } else if (bus.route.code.equals("U6")) { + U6RouteTextView.setVisibility(View.VISIBLE); + } else if (bus.route.code.equals("U9")) { + U9RouteTextView.setVisibility(View.VISIBLE); + } else { + Log.e(TAG, "Route not found " + bus.route.code); + } + // } else { + // Log.i(TAG, "Bus is not uniLink"); + // } + + } catch (NumberFormatException e) { + e.printStackTrace(); + } catch (SQLException e) { + e.printStackTrace(); + } - public void onResume() { - super.onResume(); + busStops = bus.route.getRouteSection(instance, bus.direction); + Log.i(TAG, "Got " + busStops.size() + " bus stops for this bus"); - SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); - if (sharedPrefs.getBoolean(UNI_LINK_BUS_TIMES, UNI_LINK_BUS_TIMES_ENABLED_BY_DEFAULT) - || sharedPrefs.getBoolean(NON_UNI_LINK_BUS_TIMES, NON_UNI_LINK_BUS_TIMES_ENABLED_BY_DEFAULT)) { - Log.i(TAG, "Live Times enabled"); - timetable = (Timetable) getLastNonConfigurationInstance(); + if (bus.destination != null) { + Log.i(TAG, "Bus destination is " + bus.destination); + } else { + Log.i(TAG, "Bus destination is null"); + } - handler = new Handler(); + /* + * for (int i = 0;; i++) { BusStop nextStop = + * bus.route.moveInRoute(instance, busStops.get(i), bus.direction, 1); + * + * if (nextStop.equals(busStop) || (bus.destination != null && + * bus.destination.equals(nextStop))) { break; } + * + * busStops.add(nextStop); busStopsActive.add(false); + * + * if (busStops.size() > 50) { Log.e(TAG, "Got more than 50 bus stops"); + * break; } } + */ + + refreshData = new Runnable() { + public void run() { + for (int num = timetableView.getFirstVisiblePosition(); num < timetableView + .getLastVisiblePosition(); num++) { + Stop stop = timetable.get(num); + + GetTimetableStopTask task = tasks.get(busStops.get(num)); + + if (stop.timeOfFetch == null + || (stop.timeOfFetch.getTime() - System + .currentTimeMillis()) > 20000) { + if (task != null) { + if (task.getStatus() == AsyncTask.Status.FINISHED) { + task = null; + } + } + + if (task == null) { + task = new GetTimetableStopTask(); + BusStop[] str = { stop.busStop }; + task.execute(str); + tasks.put(stop.busStop, task); + } + } + + } + handler.postDelayed(refreshData, 50000); + } + }; - if (timetable == null) { - Log.i(TAG, "No Previous timetable"); - timetable = new Timetable(); - for (int i = 0; i < busStops.size(); i++) { - timetable.add(new Stop(bus, busStops.get(i), null, null, false)); - } - Log.v(TAG, "Finished adding placeholder stops"); - } else { - Log.i(TAG, "Displaying previous timetable"); - - } - displayTimetable(timetable); - handler.postDelayed(refreshData, 500); - - } else { - Log.i(TAG, "Live Times Disabled"); - busContentMessage.setText("Live bus times disabled"); - busContentMessage.setVisibility(View.VISIBLE); } - } + public void onResume() { + super.onResume(); + + SharedPreferences sharedPrefs = PreferenceManager + .getDefaultSharedPreferences(this); + if (sharedPrefs.getBoolean(UNI_LINK_BUS_TIMES, + UNI_LINK_BUS_TIMES_ENABLED_BY_DEFAULT) + || sharedPrefs.getBoolean(NON_UNI_LINK_BUS_TIMES, + NON_UNI_LINK_BUS_TIMES_ENABLED_BY_DEFAULT)) { + Log.i(TAG, "Live Times enabled"); + timetable = (Timetable) getLastNonConfigurationInstance(); + + handler = new Handler(); + + if (timetable == null) { + Log.i(TAG, "No Previous timetable"); + timetable = new Timetable(); + for (int i = 0; i < busStops.size(); i++) { + timetable.add(new Stop(bus, busStops.get(i), null, null, + false)); + } + Log.v(TAG, "Finished adding placeholder stops"); + } else { + Log.i(TAG, "Displaying previous timetable"); - public void onPause() { - if (handler != null) { // BusTimes are enabled - handler.removeCallbacks(refreshData); - for (GetTimetableStopTask task : tasks.values()) { - if (task != null) { - task.cancel(true); + } + displayTimetable(timetable); + handler.postDelayed(refreshData, 500); + + } else { + Log.i(TAG, "Live Times Disabled"); + busContentMessage.setText("Live bus times disabled"); + busContentMessage.setVisibility(View.VISIBLE); } - } - Log.i(TAG, "Stoping refreshing timetable data"); } - super.onPause(); - } + public void onPause() { + if (handler != null) { // BusTimes are enabled + handler.removeCallbacks(refreshData); + for (GetTimetableStopTask task : tasks.values()) { + if (task != null) { + task.cancel(true); + } + } - private class GetTimetableStopTask extends AsyncTask { - private String errorMessage; + Log.i(TAG, "Stoping refreshing timetable data"); + } - private BusStop busStop; + super.onPause(); + } - private int position; + private class GetTimetableStopTask extends + AsyncTask { + private String errorMessage; - protected void onPreExecute() { - // progBar.setVisibility(View.VISIBLE); - } + private BusStop busStop; - protected Stop doInBackground(BusStop... busStopArray) { - busStop = busStopArray[0]; - position = busStops.indexOf(busStop); - Stop stop = null; + private int position; - try { - Log.i(TAG, "Fetching stop for busStop " + position); - stop = DataManager.getStop(instance, bus, busStop); - if (stop == null) { - stop = new Stop(bus, busStop, null, null, false); + protected void onPreExecute() { + // progBar.setVisibility(View.VISIBLE); } - Log.i(TAG, "Finished fetching stop for busStop " + position); - } catch (SQLException e) { - errorMessage = "Error message regarding SQL?"; - e.printStackTrace(); - } catch (ClientProtocolException e) { - errorMessage = "ClientProtocolException!?!"; - e.printStackTrace(); - } catch (IOException e) { - errorMessage = "Error fetching bus times from server, are you connected to the internet?"; - e.printStackTrace(); - } catch (JSONException e) { - errorMessage = "Error parsing bus times"; - e.printStackTrace(); - } - return stop; - } - protected void onPostExecute(Stop stop) { - // Log.i(TAG, "Got timetable"); - if (stop == null) { - Log.i(TAG, "Its null"); - - busContentMessage.setText(errorMessage); - busContentMessage.setVisibility(View.VISIBLE); - } else { - synchronized (timetable) { - timetable.set(position, stop); - displayTimetable(timetable); + protected Stop doInBackground(BusStop... busStopArray) { + busStop = busStopArray[0]; + position = busStops.indexOf(busStop); + Stop stop = null; + + try { + Log.i(TAG, "Fetching stop for busStop " + position); + stop = DataManager.getStop(instance, bus, busStop); + if (stop == null) { + stop = new Stop(bus, busStop, null, null, false); + } + Log.i(TAG, "Finished fetching stop for busStop " + position); + } catch (SQLException e) { + errorMessage = "Error message regarding SQL?"; + e.printStackTrace(); + } catch (ClientProtocolException e) { + errorMessage = "ClientProtocolException!?!"; + e.printStackTrace(); + } catch (IOException e) { + errorMessage = "Error fetching bus times from server, are you connected to the internet?"; + e.printStackTrace(); + } catch (JSONException e) { + errorMessage = "Error parsing bus times"; + e.printStackTrace(); + } + return stop; + } + + protected void onPostExecute(Stop stop) { + // Log.i(TAG, "Got timetable"); + if (stop == null) { + Log.i(TAG, "Its null"); + + busContentMessage.setText(errorMessage); + busContentMessage.setVisibility(View.VISIBLE); + } else { + synchronized (timetable) { + timetable.set(position, stop); + displayTimetable(timetable); + } + } } - } + } - } - - private void displayTimetable(Timetable timetable) { - visibleTimetable = (Timetable) timetable.clone(); - - // Log.i(TAG, "Displaying timetable, it contains " + visibleTimetable.size() + " stops"); - - if (timetable.size() == 0) { - busContentMessage.setText("No Busses"); - busContentMessage.setVisibility(View.VISIBLE); - busActivityContentLayout.setGravity(Gravity.CENTER); - } else { - if (visibleTimetable.size() == 0) { - busActivityContentLayout.setGravity(Gravity.CENTER); - busContentMessage.setText("No Busses (With the current enabled routes)"); - busContentMessage.setVisibility(View.VISIBLE); - timetableView.setVisibility(View.GONE); - } else { - timetableView.setVisibility(View.VISIBLE); - busContentMessage.setVisibility(View.GONE); - BusSpecificTimetableAdapter adapter; - if ((adapter = (BusSpecificTimetableAdapter) timetableView.getAdapter()) != null) { - adapter.updateTimetable(visibleTimetable); + private void displayTimetable(Timetable timetable) { + visibleTimetable = (Timetable) timetable.clone(); + + // Log.i(TAG, "Displaying timetable, it contains " + + // visibleTimetable.size() + " stops"); + + if (timetable.size() == 0) { + busContentMessage.setText("No Busses"); + busContentMessage.setVisibility(View.VISIBLE); + busActivityContentLayout.setGravity(Gravity.CENTER); } else { - adapter = new BusSpecificTimetableAdapter(this, visibleTimetable); - timetableView.setAdapter(adapter); - if (busStop != null) { - Log.i(TAG, - "Moving to position of " + busStop.description + " which is " - + busStops.indexOf(busStop)); - timetableView.setSelection(busStops.indexOf(busStop)); - } + if (visibleTimetable.size() == 0) { + busActivityContentLayout.setGravity(Gravity.CENTER); + busContentMessage + .setText("No Busses (With the current enabled routes)"); + busContentMessage.setVisibility(View.VISIBLE); + timetableView.setVisibility(View.GONE); + } else { + timetableView.setVisibility(View.VISIBLE); + busContentMessage.setVisibility(View.GONE); + BusSpecificTimetableAdapter adapter; + if ((adapter = (BusSpecificTimetableAdapter) timetableView + .getAdapter()) != null) { + adapter.updateTimetable(visibleTimetable); + } else { + adapter = new BusSpecificTimetableAdapter(this, + visibleTimetable); + timetableView.setAdapter(adapter); + if (busStop != null) { + Log.i(TAG, + "Moving to position of " + busStop.description + + " which is " + + busStops.indexOf(busStop)); + timetableView.setSelection(busStops.indexOf(busStop)); + } + } + busActivityContentLayout.setGravity(Gravity.TOP); + } } - busActivityContentLayout.setGravity(Gravity.TOP); - } } - } } diff --git a/src/net/cbaines/suma/BusSpecificStopView.java b/src/net/cbaines/suma/BusSpecificStopView.java index 77601ff..bdebcba 100644 --- a/src/net/cbaines/suma/BusSpecificStopView.java +++ b/src/net/cbaines/suma/BusSpecificStopView.java @@ -36,152 +36,178 @@ import android.widget.Toast; import com.j256.ormlite.android.apptools.OpenHelperManager; import com.j256.ormlite.dao.Dao; -public class BusSpecificStopView extends LinearLayout implements OnClickListener, OnLongClickListener { +public class BusSpecificStopView extends LinearLayout implements + OnClickListener, OnLongClickListener { - private static final String TAG = "BusSpecificStopView"; + private static final String TAG = "BusSpecificStopView"; - // private static final String TAG = "StopView"; + // private static final String TAG = "StopView"; - private final TextView location; - private final TextView time; - private String onClickMessage = ""; - private final BusActivity context; + private final TextView location; + private final TextView time; + private String onClickMessage = ""; + private final BusActivity context; - private Stop stop; + private Stop stop; - public BusSpecificStopView(BusActivity context, Stop stop) { - super(context); + public BusSpecificStopView(BusActivity context, Stop stop) { + super(context); - this.context = context; + this.context = context; - this.setOrientation(HORIZONTAL); + this.setOrientation(HORIZONTAL); - location = new TextView(context); - location.setTextSize(22f); + location = new TextView(context); + location.setTextSize(22f); - time = new TextView(context); - time.setTextSize(22f); - time.setGravity(Gravity.RIGHT); + time = new TextView(context); + time.setTextSize(22f); + time.setGravity(Gravity.RIGHT); - setStop(stop); + setStop(stop); - addView(location, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); - addView(time, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); + addView(location, new LayoutParams(LayoutParams.WRAP_CONTENT, + LayoutParams.WRAP_CONTENT)); + addView(time, new LayoutParams(LayoutParams.FILL_PARENT, + LayoutParams.WRAP_CONTENT)); - } - - public void setStop(Stop stop) { - - // Log.i(TAG, "Time of arival " + stop.arivalTime); - - this.stop = stop; - - if (stop == null) { - Log.e(TAG, "stop == null"); - } - if (stop.busStop == null) { - Log.e(TAG, "stop.busStop == null"); - } - if (stop.busStop.description == null) { - Log.e(TAG, "stop.busStop.description == null"); } - if (stop.busStop.description.length() > 20) { - location.setText(stop.busStop.description.substring(0, 20)); // TODO - } else { - location.setText(stop.busStop.description); // TODO - } - if (stop.arivalTime != null) { - time.setText(stop.getShortTimeToArival()); - } else { - time.setText(""); - } + public void setStop(Stop stop) { - DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); + // Log.i(TAG, "Time of arival " + stop.arivalTime); - try { - Dao busDao = helper.getBusDao(); + this.stop = stop; - busDao.refresh(stop.bus); - - if (stop.arivalTime != null) { + if (stop == null) { + Log.e(TAG, "stop == null"); + } + if (stop.busStop == null) { + Log.e(TAG, "stop.busStop == null"); + } + if (stop.busStop.description == null) { + Log.e(TAG, "stop.busStop.description == null"); + } - if (stop.bus.id != null) { - if (stop.live) { - onClickMessage = "Bus " + stop.bus.toString() + " at " + DateFormat.getTimeInstance(DateFormat.SHORT).format(stop.arivalTime); - } else { - onClickMessage = "Timetabled bus " + stop.bus.toString() + " at " - + DateFormat.getTimeInstance(DateFormat.SHORT).format(stop.arivalTime); - } + if (stop.busStop.description.length() > 20) { + location.setText(stop.busStop.description.substring(0, 20)); // TODO } else { - if (stop.live) { - onClickMessage = "Unidentified bus (" + stop.bus.getName() + ") at " - + DateFormat.getTimeInstance(DateFormat.SHORT).format(stop.arivalTime); - } else { - onClickMessage = "Timetabled bus (" + stop.bus.getName() + ") at " - + DateFormat.getTimeInstance(DateFormat.SHORT).format(stop.arivalTime); - } + location.setText(stop.busStop.description); // TODO } - } else { - if (stop.bus.id != null) { - if (stop.live) { - onClickMessage = "Bus " + stop.bus.toString(); - } else { - onClickMessage = "Timetabled bus " + stop.bus.toString(); - } + if (stop.arivalTime != null) { + time.setText(stop.getShortTimeToArival()); } else { - if (stop.live) { - onClickMessage = "Unidentified bus (" + stop.bus.getName() + ")"; - } else { - onClickMessage = "Timetabled bus (" + stop.bus.getName() + ")"; - } + time.setText(""); } - } - } catch (SQLException e) { - e.printStackTrace(); - } - this.setOnClickListener(this); - this.setOnLongClickListener(this); - } + DatabaseHelper helper = OpenHelperManager.getHelper(context, + DatabaseHelper.class); + + try { + Dao busDao = helper.getBusDao(); + + busDao.refresh(stop.bus); + + if (stop.arivalTime != null) { + + if (stop.bus.id != null) { + if (stop.live) { + onClickMessage = "Bus " + + stop.bus.toString() + + " at " + + DateFormat.getTimeInstance(DateFormat.SHORT) + .format(stop.arivalTime); + } else { + onClickMessage = "Timetabled bus " + + stop.bus.toString() + + " at " + + DateFormat.getTimeInstance(DateFormat.SHORT) + .format(stop.arivalTime); + } + } else { + if (stop.live) { + onClickMessage = "Unidentified bus (" + + stop.bus.getName() + + ") at " + + DateFormat.getTimeInstance(DateFormat.SHORT) + .format(stop.arivalTime); + } else { + onClickMessage = "Timetabled bus (" + + stop.bus.getName() + + ") at " + + DateFormat.getTimeInstance(DateFormat.SHORT) + .format(stop.arivalTime); + } + } + } else { + if (stop.bus.id != null) { + if (stop.live) { + onClickMessage = "Bus " + stop.bus.toString(); + } else { + onClickMessage = "Timetabled bus " + + stop.bus.toString(); + } + } else { + if (stop.live) { + onClickMessage = "Unidentified bus (" + + stop.bus.getName() + ")"; + } else { + onClickMessage = "Timetabled bus (" + + stop.bus.getName() + ")"; + } + } + } + } catch (SQLException e) { + e.printStackTrace(); + } - public void onClick(View v) { - if (context.activityToast == null) { - context.activityToast = Toast.makeText(context, onClickMessage, Toast.LENGTH_SHORT); - } else { - context.activityToast.setText(onClickMessage); - context.activityToast.setDuration(Toast.LENGTH_SHORT); + this.setOnClickListener(this); + this.setOnLongClickListener(this); } - context.activityToast.show(); - } - - @Override - public boolean onLongClick(View v) { // TODO - DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); - try { - Dao busDao = helper.getBusDao(); - - busDao.refresh(stop.bus); - - if (stop.bus.id != null) { - Intent i = new Intent(context, MapActivity.class); - i.putExtra("poiPoint", stop.busStop.point.toDoubleString()); - ((Activity) context).startActivityForResult(i, 0); - } else { + public void onClick(View v) { if (context.activityToast == null) { - context.activityToast = Toast.makeText(context, "Arival prediction not avalible for timetabled buses", Toast.LENGTH_SHORT); + context.activityToast = Toast.makeText(context, onClickMessage, + Toast.LENGTH_SHORT); } else { - context.activityToast.setText("Arival prediction not avalible for timetabled buses"); - context.activityToast.setDuration(Toast.LENGTH_SHORT); + context.activityToast.setText(onClickMessage); + context.activityToast.setDuration(Toast.LENGTH_SHORT); } context.activityToast.show(); - } + } - } catch (SQLException e) { - e.printStackTrace(); + public boolean onLongClick(View v) { // TODO + DatabaseHelper helper = OpenHelperManager.getHelper(context, + DatabaseHelper.class); + + try { + Dao busDao = helper.getBusDao(); + + busDao.refresh(stop.bus); + + if (stop.bus.id != null) { + Intent i = new Intent(context, MapActivity.class); + i.putExtra("poiPoint", stop.busStop.point.toDoubleString()); + ((Activity) context).startActivityForResult(i, 0); + } else { + if (context.activityToast == null) { + context.activityToast = Toast + .makeText( + context, + "Arival prediction not avalible for timetabled buses", + Toast.LENGTH_SHORT); + } else { + context.activityToast + .setText("Arival prediction not avalible for timetabled buses"); + context.activityToast.setDuration(Toast.LENGTH_SHORT); + } + context.activityToast.show(); + } + + } catch (SQLException e) { + e.printStackTrace(); + } + return false; } - return false; - } } diff --git a/src/net/cbaines/suma/BusStopActivity.java b/src/net/cbaines/suma/BusStopActivity.java index 03dcbca..d9eaed3 100644 --- a/src/net/cbaines/suma/BusStopActivity.java +++ b/src/net/cbaines/suma/BusStopActivity.java @@ -59,455 +59,481 @@ import com.j256.ormlite.dao.Dao; import com.j256.ormlite.stmt.PreparedQuery; import com.j256.ormlite.stmt.QueryBuilder; -public class BusStopActivity extends OrmLiteBaseActivity implements OnCheckedChangeListener, Preferences, OnItemClickListener { +public class BusStopActivity extends OrmLiteBaseActivity + implements OnCheckedChangeListener, Preferences, OnItemClickListener { - final static String TAG = "BusTimeActivity"; - - private boolean dataChanged; - - private ListView busTimeList; - private TextView busName; - private TextView busID; - private CheckBox busFavourite; - private TextView busStopMessage; - private ProgressBar progBar; - private LinearLayout busTimeContentLayout; - - protected Timetable timetable; - private Timetable visibleTimetable; - - protected String busStopID; - private String busStopName; - - private Dao busStopDao; - - private BusStop busStop; - - private GetTimetableTask timetableTask; - - private Context instance; - - private Handler mHandler; - private Runnable refreshData; - - private CheckBox U1RouteRadioButton; - private CheckBox U1NRouteRadioButton; - private CheckBox U2RouteRadioButton; - private CheckBox U6RouteRadioButton; - private CheckBox U9RouteRadioButton; - - private static final int POI_DIALOG_ID = 0; - private POIDialog busDialog; - - private HashSet routes = new HashSet(); - - Toast activityToast; - - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.bustimes); - - final DatabaseHelper helper = getHelper(); - - instance = this; - - busStopID = getIntent().getExtras().getString("busStopID"); - busStopName = getIntent().getExtras().getString("busStopName"); - - U1RouteRadioButton = (CheckBox) findViewById(R.id.radio_u1); - U1NRouteRadioButton = (CheckBox) findViewById(R.id.radio_u1n); - U2RouteRadioButton = (CheckBox) findViewById(R.id.radio_u2); - U6RouteRadioButton = (CheckBox) findViewById(R.id.radio_u6); - U9RouteRadioButton = (CheckBox) findViewById(R.id.radio_u9); - - U1RouteRadioButton.setOnCheckedChangeListener(this); - U1NRouteRadioButton.setOnCheckedChangeListener(this); - U2RouteRadioButton.setOnCheckedChangeListener(this); - U6RouteRadioButton.setOnCheckedChangeListener(this); - U9RouteRadioButton.setOnCheckedChangeListener(this); - - try { - Dao busRouteDao = helper.getBusRouteDao(); - Dao routeStopsDao = helper.getRouteStopsDao(); - - for (BusRoute route : busRouteDao) { - QueryBuilder queryBuilder = routeStopsDao.queryBuilder(); - - queryBuilder.where().eq(RouteStops.ROUTE_ID_FIELD_NAME, route.id).and().eq(RouteStops.STOP_ID_FIELD_NAME, busStopID); - queryBuilder.setCountOf(true); - PreparedQuery preparedQuery = queryBuilder.prepare(); - - long count = routeStopsDao.countOf(preparedQuery); - - if (route.code.equals("U1")) { - if (count != 0) { - U1RouteRadioButton.setVisibility(View.VISIBLE); - routes.add(route); - } else { - U1RouteRadioButton.setVisibility(View.GONE); - } - } else if (route.code.equals("U1N")) { - if (count != 0) { - U1NRouteRadioButton.setVisibility(View.VISIBLE); - routes.add(route); - } else { - U1NRouteRadioButton.setVisibility(View.GONE); - } - } else if (route.code.equals("U2")) { - if (count != 0) { - U2RouteRadioButton.setVisibility(View.VISIBLE); - routes.add(route); - } else { - U2RouteRadioButton.setVisibility(View.GONE); - } - } else if (route.code.equals("U6")) { - if (count != 0) { - U6RouteRadioButton.setVisibility(View.VISIBLE); - routes.add(route); - } else { - U6RouteRadioButton.setVisibility(View.GONE); - } - } else if (route.code.equals("U9")) { - if (count != 0) { - U9RouteRadioButton.setVisibility(View.VISIBLE); - routes.add(route); - } else { - U9RouteRadioButton.setVisibility(View.GONE); - } - } - } + final static String TAG = "BusTimeActivity"; - busStopDao = helper.getBusStopDao(); + private boolean dataChanged; - busStop = busStopDao.queryForId(busStopID); + private ListView busTimeList; + private TextView busName; + private TextView busID; + private CheckBox busFavourite; + private TextView busStopMessage; + private ProgressBar progBar; + private LinearLayout busTimeContentLayout; - busFavourite = (CheckBox) findViewById(R.id.favouriteCheckBox); - busFavourite.setChecked(busStop.favourite); - busFavourite.setOnCheckedChangeListener(this); + protected Timetable timetable; + private Timetable visibleTimetable; - } catch (SQLException e) { - e.printStackTrace(); - } + protected String busStopID; + private String busStopName; - busName = (TextView) findViewById(R.id.busStopName); - busID = (TextView) findViewById(R.id.busStopID); + private Dao busStopDao; - busStopMessage = (TextView) findViewById(R.id.busStopMessage); - progBar = (ProgressBar) findViewById(R.id.busStopLoadBar); - busTimeList = (ListView) findViewById(R.id.busStopTimes); - busTimeContentLayout = (LinearLayout) findViewById(R.id.busTimeContentLayout); + private BusStop busStop; - Log.i(TAG, "Got busstop id " + busStopID); + private GetTimetableTask timetableTask; - busName.setText(busStopName); - busID.setText(busStopID); - } + private Context instance; - public void onResume() { - super.onResume(); + private Handler mHandler; + private Runnable refreshData; - SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); - if (sharedPrefs.getBoolean(UNI_LINK_BUS_TIMES, UNI_LINK_BUS_TIMES_ENABLED_BY_DEFAULT) - || sharedPrefs.getBoolean(NON_UNI_LINK_BUS_TIMES, NON_UNI_LINK_BUS_TIMES_ENABLED_BY_DEFAULT)) { - Log.i(TAG, "Live Times enabled"); - timetable = (Timetable) getLastNonConfigurationInstance(); + private CheckBox U1RouteRadioButton; + private CheckBox U1NRouteRadioButton; + private CheckBox U2RouteRadioButton; + private CheckBox U6RouteRadioButton; + private CheckBox U9RouteRadioButton; - refreshData = new Runnable() { - @Override - public void run() { - timetableTask = new GetTimetableTask(); - timetableTask.execute(busStopID); - mHandler.postDelayed(refreshData, 20000); - } - }; - - mHandler = new Handler(); - - if (timetable == null) { - Log.i(TAG, "No Previous timetable"); - mHandler.post(refreshData); - } else { - Log.i(TAG, "Displaying previous timetable"); - displayTimetable(timetable); - if (System.currentTimeMillis() - timetable.fetchTime.getTime() > 20000) { - mHandler.post(refreshData); + private static final int POI_DIALOG_ID = 0; + private POIDialog busDialog; + + private HashSet routes = new HashSet(); + + Toast activityToast; + + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.bustimes); + + final DatabaseHelper helper = getHelper(); + + instance = this; + + busStopID = getIntent().getExtras().getString("busStopID"); + busStopName = getIntent().getExtras().getString("busStopName"); + + U1RouteRadioButton = (CheckBox) findViewById(R.id.radio_u1); + U1NRouteRadioButton = (CheckBox) findViewById(R.id.radio_u1n); + U2RouteRadioButton = (CheckBox) findViewById(R.id.radio_u2); + U6RouteRadioButton = (CheckBox) findViewById(R.id.radio_u6); + U9RouteRadioButton = (CheckBox) findViewById(R.id.radio_u9); + + U1RouteRadioButton.setOnCheckedChangeListener(this); + U1NRouteRadioButton.setOnCheckedChangeListener(this); + U2RouteRadioButton.setOnCheckedChangeListener(this); + U6RouteRadioButton.setOnCheckedChangeListener(this); + U9RouteRadioButton.setOnCheckedChangeListener(this); + + try { + Dao busRouteDao = helper.getBusRouteDao(); + Dao routeStopsDao = helper.getRouteStopsDao(); + + for (BusRoute route : busRouteDao) { + QueryBuilder queryBuilder = routeStopsDao + .queryBuilder(); + + queryBuilder.where() + .eq(RouteStops.ROUTE_ID_FIELD_NAME, route.id).and() + .eq(RouteStops.STOP_ID_FIELD_NAME, busStopID); + queryBuilder.setCountOf(true); + PreparedQuery preparedQuery = queryBuilder + .prepare(); + + long count = routeStopsDao.countOf(preparedQuery); + + if (route.code.equals("U1")) { + if (count != 0) { + U1RouteRadioButton.setVisibility(View.VISIBLE); + routes.add(route); + } else { + U1RouteRadioButton.setVisibility(View.GONE); + } + } else if (route.code.equals("U1N")) { + if (count != 0) { + U1NRouteRadioButton.setVisibility(View.VISIBLE); + routes.add(route); + } else { + U1NRouteRadioButton.setVisibility(View.GONE); + } + } else if (route.code.equals("U2")) { + if (count != 0) { + U2RouteRadioButton.setVisibility(View.VISIBLE); + routes.add(route); + } else { + U2RouteRadioButton.setVisibility(View.GONE); + } + } else if (route.code.equals("U6")) { + if (count != 0) { + U6RouteRadioButton.setVisibility(View.VISIBLE); + routes.add(route); + } else { + U6RouteRadioButton.setVisibility(View.GONE); + } + } else if (route.code.equals("U9")) { + if (count != 0) { + U9RouteRadioButton.setVisibility(View.VISIBLE); + routes.add(route); + } else { + U9RouteRadioButton.setVisibility(View.GONE); + } + } + } + + busStopDao = helper.getBusStopDao(); + + busStop = busStopDao.queryForId(busStopID); + + busFavourite = (CheckBox) findViewById(R.id.favouriteCheckBox); + busFavourite.setChecked(busStop.favourite); + busFavourite.setOnCheckedChangeListener(this); + + } catch (SQLException e) { + e.printStackTrace(); } - } - } else { - Log.i(TAG, "Live Times Disabled"); - progBar.setVisibility(View.GONE); - busStopMessage.setText("Live bus times disabled"); - busStopMessage.setVisibility(View.VISIBLE); - } + busName = (TextView) findViewById(R.id.busStopName); + busID = (TextView) findViewById(R.id.busStopID); + + busStopMessage = (TextView) findViewById(R.id.busStopMessage); + progBar = (ProgressBar) findViewById(R.id.busStopLoadBar); + busTimeList = (ListView) findViewById(R.id.busStopTimes); + busTimeContentLayout = (LinearLayout) findViewById(R.id.busTimeContentLayout); - } + Log.i(TAG, "Got busstop id " + busStopID); - public void onPause() { - if (mHandler != null) { // BusTimes are enabled - mHandler.removeCallbacks(refreshData); - if (timetableTask != null) // Could happen if the handler has not created the timetableTask yet - timetableTask.cancel(true); - Log.i(TAG, "Stoping refreshing timetable data"); + busName.setText(busStopName); + busID.setText(busStopID); } - super.onPause(); - } + public void onResume() { + super.onResume(); + + SharedPreferences sharedPrefs = PreferenceManager + .getDefaultSharedPreferences(this); + if (sharedPrefs.getBoolean(UNI_LINK_BUS_TIMES, + UNI_LINK_BUS_TIMES_ENABLED_BY_DEFAULT) + || sharedPrefs.getBoolean(NON_UNI_LINK_BUS_TIMES, + NON_UNI_LINK_BUS_TIMES_ENABLED_BY_DEFAULT)) { + Log.i(TAG, "Live Times enabled"); + timetable = (Timetable) getLastNonConfigurationInstance(); + + refreshData = new Runnable() { + public void run() { + timetableTask = new GetTimetableTask(); + timetableTask.execute(busStopID); + mHandler.postDelayed(refreshData, 20000); + } + }; + + mHandler = new Handler(); + + if (timetable == null) { + Log.i(TAG, "No Previous timetable"); + mHandler.post(refreshData); + } else { + Log.i(TAG, "Displaying previous timetable"); + displayTimetable(timetable); + if (System.currentTimeMillis() - timetable.fetchTime.getTime() > 20000) { + mHandler.post(refreshData); + } + } + + } else { + Log.i(TAG, "Live Times Disabled"); + progBar.setVisibility(View.GONE); + busStopMessage.setText("Live bus times disabled"); + busStopMessage.setVisibility(View.VISIBLE); + } - public void finish() { - if (dataChanged) { - getIntent().putExtra("busStopChanged", busStopID); } - setResult(RESULT_OK, getIntent()); - - super.finish(); - } - - public void onCheckedChanged(CompoundButton button, boolean checked) { - if (button.equals(busFavourite)) { - busStop.favourite = checked; - try { - busStopDao.update(busStop); - dataChanged = true; - } catch (SQLException e) { - e.printStackTrace(); - } - } else { - Log.i(TAG, "Route radio button made " + checked); - if (timetable != null) { // If there is a timetable to display - displayTimetable(timetable); - } + public void onPause() { + if (mHandler != null) { // BusTimes are enabled + mHandler.removeCallbacks(refreshData); + if (timetableTask != null) // Could happen if the handler has not + // created the timetableTask yet + timetableTask.cancel(true); + Log.i(TAG, "Stoping refreshing timetable data"); + } + + super.onPause(); } - } - @Override - public Object onRetainNonConfigurationInstance() { - return timetable; - } + public void finish() { + if (dataChanged) { + getIntent().putExtra("busStopChanged", busStopID); + } - private class GetTimetableTask extends AsyncTask { - String errorMessage; + setResult(RESULT_OK, getIntent()); - protected void onPreExecute() { - progBar.setVisibility(View.VISIBLE); + super.finish(); } - protected Timetable doInBackground(String... activity) { - Timetable newTimetable = null; - try { - final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(instance); - - newTimetable = DataManager.getTimetable(instance, busStopID, sharedPrefs.getBoolean(MapActivity.UNI_LINK_BUS_TIMES, - MapActivity.UNI_LINK_BUS_TIMES_ENABLED_BY_DEFAULT), sharedPrefs.getBoolean( - MapActivity.NON_UNI_LINK_BUS_TIMES, MapActivity.NON_UNI_LINK_BUS_TIMES_ENABLED_BY_DEFAULT)); - } catch (SQLException e) { - errorMessage = "Error message regarding SQL?"; - e.printStackTrace(); - } catch (ClientProtocolException e) { - errorMessage = "ClientProtocolException!?!"; - e.printStackTrace(); - } catch (IOException e) { - errorMessage = "Error fetching bus times from server, are you connected to the internet?"; - e.printStackTrace(); - } catch (JSONException e) { - errorMessage = "Error parsing bus times"; - e.printStackTrace(); - } catch (Exception e) { - Log.e(TAG, e.getMessage(), e.getCause()); - } - - return newTimetable; + public void onCheckedChanged(CompoundButton button, boolean checked) { + if (button.equals(busFavourite)) { + busStop.favourite = checked; + try { + busStopDao.update(busStop); + dataChanged = true; + } catch (SQLException e) { + e.printStackTrace(); + } + } else { + Log.i(TAG, "Route radio button made " + checked); + if (timetable != null) { // If there is a timetable to display + displayTimetable(timetable); + } + } } - protected void onPostExecute(Timetable newTimetable) { - Log.i(TAG, "Got timetable for " + busStopID); - if (newTimetable == null) { - Log.i(TAG, "Its null"); - - progBar.setVisibility(View.GONE); - busStopMessage.setText(errorMessage); - busStopMessage.setVisibility(View.VISIBLE); - } else { - progBar.setVisibility(View.GONE); - timetable = newTimetable; - displayTimetable(timetable); - } + @Override + public Object onRetainNonConfigurationInstance() { + return timetable; } - } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - MenuInflater inflater = getMenuInflater(); - inflater.inflate(R.menu.stop_menu, menu); - return true; - } + private class GetTimetableTask extends + AsyncTask { + String errorMessage; - @Override - public boolean onOptionsItemSelected(MenuItem item) { - // Handle item selection - 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"); + protected void onPreExecute() { + progBar.setVisibility(View.VISIBLE); + } - Log.v(TAG, routes.size() + " routes avalible from this stop"); + protected Timetable doInBackground(String... activity) { + Timetable newTimetable = null; + try { + final SharedPreferences sharedPrefs = PreferenceManager + .getDefaultSharedPreferences(instance); + + newTimetable = DataManager + .getTimetable( + instance, + busStopID, + sharedPrefs + .getBoolean( + MapActivity.UNI_LINK_BUS_TIMES, + MapActivity.UNI_LINK_BUS_TIMES_ENABLED_BY_DEFAULT), + sharedPrefs + .getBoolean( + MapActivity.NON_UNI_LINK_BUS_TIMES, + MapActivity.NON_UNI_LINK_BUS_TIMES_ENABLED_BY_DEFAULT)); + } catch (SQLException e) { + errorMessage = "Error message regarding SQL?"; + e.printStackTrace(); + } catch (ClientProtocolException e) { + errorMessage = "ClientProtocolException!?!"; + e.printStackTrace(); + } catch (IOException e) { + errorMessage = "Error fetching bus times from server, are you connected to the internet?"; + e.printStackTrace(); + } catch (JSONException e) { + errorMessage = "Error parsing bus times"; + e.printStackTrace(); + } catch (Exception e) { + Log.e(TAG, e.getMessage(), e.getCause()); + } - ArrayList busStops = new ArrayList(); + return newTimetable; + } - for (BusRoute route : routes) { - try { - Set tmpStops; - if (item.getItemId() == R.id.menu_next_stop) { - tmpStops = route.moveInRoute(this, getHelper().getBusStopDao().queryForId(busStopID), 1); - } else { - tmpStops = route.moveInRoute(this, getHelper().getBusStopDao().queryForId(busStopID), -1); - } - for (BusStop busStop : tmpStops) { - if (!busStops.contains(busStop)) { - busStops.add(busStop); + protected void onPostExecute(Timetable newTimetable) { + Log.i(TAG, "Got timetable for " + busStopID); + if (newTimetable == null) { + Log.i(TAG, "Its null"); + + progBar.setVisibility(View.GONE); + busStopMessage.setText(errorMessage); + busStopMessage.setVisibility(View.VISIBLE); + } else { + progBar.setVisibility(View.GONE); + timetable = newTimetable; + displayTimetable(timetable); } - } - } catch (SQLException e) { - e.printStackTrace(); } - } + } - Log.i(TAG, "stops " + busStops); + @Override + public boolean onCreateOptionsMenu(Menu menu) { + MenuInflater inflater = getMenuInflater(); + inflater.inflate(R.menu.stop_menu, menu); + return true; + } - if (busStops.size() == 1) { - Intent i = new Intent(this, BusStopActivity.class); - BusStop stop = (BusStop) busStops.iterator().next(); - if (stop == null) { - Log.e(TAG, "stop == null"); - } - if (stop.id == null) { - Log.e(TAG, "stop.id == null"); - } - i.putExtra("busStopID", stop.id); - i.putExtra("busStopName", stop.description); - startActivity(i); - } else { - showDialog(POI_DIALOG_ID); - if (busDialog == null) { - Log.e(TAG, "Very wierd, just tried to launch the favourite's dialog, but its null?"); - return false; - } + @Override + public boolean onOptionsItemSelected(MenuItem item) { + // Handle item selection + 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"); + + Log.v(TAG, routes.size() + " routes avalible from this stop"); + + ArrayList busStops = new ArrayList(); + + for (BusRoute route : routes) { + try { + Set tmpStops; + if (item.getItemId() == R.id.menu_next_stop) { + tmpStops = route.moveInRoute(this, getHelper() + .getBusStopDao().queryForId(busStopID), 1); + } else { + tmpStops = route.moveInRoute(this, getHelper() + .getBusStopDao().queryForId(busStopID), -1); + } + for (BusStop busStop : tmpStops) { + if (!busStops.contains(busStop)) { + busStops.add(busStop); + } + } + } catch (SQLException e) { + e.printStackTrace(); + } + } - busDialog.setMessage(""); - busDialog.setItems(busStops); - busDialog.setTitle("Choose Bus Stop"); - - Log.i(TAG, "Showing dialog"); - } - - } else if (item.getItemId() == R.id.menu_refresh_stop) { - if (mHandler != null) { // BusTimes are enabled - mHandler.removeCallbacks(refreshData); - timetableTask.cancel(true); - Log.i(TAG, "Stoping refreshing timetable data"); - - mHandler.post(refreshData); - } else { - // TODO: Toast here... - } - } else { - Log.e(TAG, "No known menu option selected"); - return super.onOptionsItemSelected(item); - } - return true; - } - - private void displayTimetable(Timetable timetable) { - visibleTimetable = (Timetable) timetable.clone(); - - Log.i(TAG, "It contains " + visibleTimetable.size() + " stops"); - - if (timetable.size() == 0) { - busStopMessage.setText("No Busses"); - busStopMessage.setVisibility(View.VISIBLE); - busTimeContentLayout.setGravity(Gravity.CENTER); - } else { - - for (Iterator stopIter = visibleTimetable.iterator(); stopIter.hasNext();) { - Stop stop = stopIter.next(); - Log.i(TAG, "Begin filtering, looking at " + stop + " with route " + stop.bus.route.code); - if (stop.bus.route.code.equals("U1")) { - if (!U1RouteRadioButton.isChecked()) { - stopIter.remove(); - } - } else if (stop.bus.route.code.equals("U1N")) { - if (!U1NRouteRadioButton.isChecked()) { - stopIter.remove(); - } - } else if (stop.bus.route.code.equals("U2")) { - if (!U2RouteRadioButton.isChecked()) { - stopIter.remove(); - } - } else if (stop.bus.route.code.equals("U6")) { - if (!U6RouteRadioButton.isChecked()) { - stopIter.remove(); - } - } else if (stop.bus.route.code.equals("U9")) { - if (!U9RouteRadioButton.isChecked()) { - stopIter.remove(); - } - } - } - - if (visibleTimetable.size() == 0) { - busTimeContentLayout.setGravity(Gravity.CENTER); - busStopMessage.setText("No Busses (With the current enabled routes)"); - busStopMessage.setVisibility(View.VISIBLE); - busTimeList.setVisibility(View.GONE); - } else { - busTimeList.setVisibility(View.VISIBLE); - busStopMessage.setVisibility(View.GONE); - TimetableAdapter adapter; - if ((adapter = (TimetableAdapter) busTimeList.getAdapter()) != null) { - adapter.updateTimetable(visibleTimetable); + Log.i(TAG, "stops " + busStops); + + if (busStops.size() == 1) { + Intent i = new Intent(this, BusStopActivity.class); + BusStop stop = (BusStop) busStops.iterator().next(); + if (stop == null) { + Log.e(TAG, "stop == null"); + } + if (stop.id == null) { + Log.e(TAG, "stop.id == null"); + } + i.putExtra("busStopID", stop.id); + i.putExtra("busStopName", stop.description); + startActivity(i); + } else { + showDialog(POI_DIALOG_ID); + if (busDialog == null) { + Log.e(TAG, + "Very wierd, just tried to launch the favourite's dialog, but its null?"); + return false; + } + + busDialog.setMessage(""); + busDialog.setItems(busStops); + busDialog.setTitle("Choose Bus Stop"); + + Log.i(TAG, "Showing dialog"); + } + + } else if (item.getItemId() == R.id.menu_refresh_stop) { + if (mHandler != null) { // BusTimes are enabled + mHandler.removeCallbacks(refreshData); + timetableTask.cancel(true); + Log.i(TAG, "Stoping refreshing timetable data"); + + mHandler.post(refreshData); + } else { + // TODO: Toast here... + } } else { - adapter = new TimetableAdapter(this, visibleTimetable); - busTimeList.setAdapter(adapter); + Log.e(TAG, "No known menu option selected"); + return super.onOptionsItemSelected(item); } - busTimeContentLayout.setGravity(Gravity.TOP); - } + return true; } - } - - @Override - protected Dialog onCreateDialog(int id) { - switch (id) { - case POI_DIALOG_ID: - busDialog = new POIDialog(instance); - busDialog.setOnItemClickListener(this); - return busDialog; + + private void displayTimetable(Timetable timetable) { + visibleTimetable = (Timetable) timetable.clone(); + + Log.i(TAG, "It contains " + visibleTimetable.size() + " stops"); + + if (timetable.size() == 0) { + busStopMessage.setText("No Busses"); + busStopMessage.setVisibility(View.VISIBLE); + busTimeContentLayout.setGravity(Gravity.CENTER); + } else { + + for (Iterator stopIter = visibleTimetable.iterator(); stopIter + .hasNext();) { + Stop stop = stopIter.next(); + Log.i(TAG, "Begin filtering, looking at " + stop + + " with route " + stop.bus.route.code); + if (stop.bus.route.code.equals("U1")) { + if (!U1RouteRadioButton.isChecked()) { + stopIter.remove(); + } + } else if (stop.bus.route.code.equals("U1N")) { + if (!U1NRouteRadioButton.isChecked()) { + stopIter.remove(); + } + } else if (stop.bus.route.code.equals("U2")) { + if (!U2RouteRadioButton.isChecked()) { + stopIter.remove(); + } + } else if (stop.bus.route.code.equals("U6")) { + if (!U6RouteRadioButton.isChecked()) { + stopIter.remove(); + } + } else if (stop.bus.route.code.equals("U9")) { + if (!U9RouteRadioButton.isChecked()) { + stopIter.remove(); + } + } + } + + if (visibleTimetable.size() == 0) { + busTimeContentLayout.setGravity(Gravity.CENTER); + busStopMessage + .setText("No Busses (With the current enabled routes)"); + busStopMessage.setVisibility(View.VISIBLE); + busTimeList.setVisibility(View.GONE); + } else { + busTimeList.setVisibility(View.VISIBLE); + busStopMessage.setVisibility(View.GONE); + TimetableAdapter adapter; + if ((adapter = (TimetableAdapter) busTimeList.getAdapter()) != null) { + adapter.updateTimetable(visibleTimetable); + } else { + adapter = new TimetableAdapter(this, visibleTimetable); + busTimeList.setAdapter(adapter); + } + busTimeContentLayout.setGravity(Gravity.TOP); + } + } } - return null; - } - - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - Log.i(TAG, "OnItemClick pos " + position + " id " + id); - - String poiId = busDialog.adapter.getItemStringId(position); - - Log.i(TAG, "POI " + poiId + " selected"); - - Intent i = new Intent(this, BusStopActivity.class); - try { - busStop = busStopDao.queryForId(poiId); - - if (busStop == null) { - Log.e(TAG, "stop == null"); - } - if (busStop.id == null) { - Log.e(TAG, "stop.id == null"); - } - i.putExtra("busStopID", busStop.id); - i.putExtra("busStopName", busStop.description); - startActivity(i); - } catch (SQLException e) { - e.printStackTrace(); + + @Override + protected Dialog onCreateDialog(int id) { + switch (id) { + case POI_DIALOG_ID: + busDialog = new POIDialog(instance); + busDialog.setOnItemClickListener(this); + return busDialog; + } + return null; } - } + public void onItemClick(AdapterView parent, View view, int position, + long id) { + Log.i(TAG, "OnItemClick pos " + position + " id " + id); + + String poiId = busDialog.adapter.getItemStringId(position); + + Log.i(TAG, "POI " + poiId + " selected"); + + Intent i = new Intent(this, BusStopActivity.class); + try { + busStop = busStopDao.queryForId(poiId); + + if (busStop == null) { + Log.e(TAG, "stop == null"); + } + if (busStop.id == null) { + Log.e(TAG, "stop.id == null"); + } + i.putExtra("busStopID", busStop.id); + i.putExtra("busStopName", busStop.description); + startActivity(i); + } catch (SQLException e) { + e.printStackTrace(); + } + + } } diff --git a/src/net/cbaines/suma/StopView.java b/src/net/cbaines/suma/StopView.java index 764072e..f942137 100644 --- a/src/net/cbaines/suma/StopView.java +++ b/src/net/cbaines/suma/StopView.java @@ -36,135 +36,160 @@ import android.widget.Toast; import com.j256.ormlite.android.apptools.OpenHelperManager; import com.j256.ormlite.dao.Dao; -public class StopView extends LinearLayout implements OnClickListener, OnLongClickListener { +public class StopView extends LinearLayout implements OnClickListener, + OnLongClickListener { - // private final ImageView icon; + // private final ImageView icon; - // private static final String TAG = "StopView"; + // private static final String TAG = "StopView"; - private final TextView name; - private final TextView time; - private String onClickMessage = ""; - private final BusStopActivity context; + private final TextView name; + private final TextView time; + private String onClickMessage = ""; + private final BusStopActivity context; - private Stop stop; + private Stop stop; - public StopView(BusStopActivity context, Stop stop) { - super(context); + public StopView(BusStopActivity context, Stop stop) { + super(context); - this.context = context; + this.context = context; - this.setOrientation(HORIZONTAL); + this.setOrientation(HORIZONTAL); - name = new TextView(context); - name.setTextSize(22f); + name = new TextView(context); + name.setTextSize(22f); - time = new TextView(context); - time.setTextSize(22f); - time.setGravity(Gravity.RIGHT); + time = new TextView(context); + time.setTextSize(22f); + time.setGravity(Gravity.RIGHT); - setStop(stop); + setStop(stop); - addView(name, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); - addView(time, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); - } - - public void setStop(Stop stop) { - - // Log.i(TAG, "Time of arival " + stop.arivalTime); - - this.stop = stop; - - name.setText(stop.bus.getName()); - time.setText(stop.getTimeToArival()); - - DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); - - try { - Dao busDao = helper.getBusDao(); - - busDao.refresh(stop.bus); - - if (stop.bus.id != null) { - if (stop.live) { - onClickMessage = "Bus " + stop.bus.toString() + " at " - + DateFormat.getTimeInstance(DateFormat.SHORT).format(stop.arivalTime); - } else { - onClickMessage = "Timetabled bus " + stop.bus.toString() + " at " - + DateFormat.getTimeInstance(DateFormat.SHORT).format(stop.arivalTime); - } - } else { - if (stop.live) { - onClickMessage = "Unidentified bus (" + stop.bus.getName() + ") at " - + DateFormat.getTimeInstance(DateFormat.SHORT).format(stop.arivalTime); - } else { - onClickMessage = "Timetabled bus (" + stop.bus.getName() + ") at " - + DateFormat.getTimeInstance(DateFormat.SHORT).format(stop.arivalTime); - } - } - } catch (SQLException e) { - e.printStackTrace(); + addView(name, new LayoutParams(LayoutParams.WRAP_CONTENT, + LayoutParams.WRAP_CONTENT)); + addView(time, new LayoutParams(LayoutParams.FILL_PARENT, + LayoutParams.WRAP_CONTENT)); } - this.setOnClickListener(this); - this.setOnLongClickListener(this); - } + public void setStop(Stop stop) { + + // Log.i(TAG, "Time of arival " + stop.arivalTime); + + this.stop = stop; + + name.setText(stop.bus.getName()); + time.setText(stop.getTimeToArival()); + + DatabaseHelper helper = OpenHelperManager.getHelper(context, + DatabaseHelper.class); + + try { + Dao busDao = helper.getBusDao(); + + busDao.refresh(stop.bus); + + if (stop.bus.id != null) { + if (stop.live) { + onClickMessage = "Bus " + + stop.bus.toString() + + " at " + + DateFormat.getTimeInstance(DateFormat.SHORT) + .format(stop.arivalTime); + } else { + onClickMessage = "Timetabled bus " + + stop.bus.toString() + + " at " + + DateFormat.getTimeInstance(DateFormat.SHORT) + .format(stop.arivalTime); + } + } else { + if (stop.live) { + onClickMessage = "Unidentified bus (" + + stop.bus.getName() + + ") at " + + DateFormat.getTimeInstance(DateFormat.SHORT) + .format(stop.arivalTime); + } else { + onClickMessage = "Timetabled bus (" + + stop.bus.getName() + + ") at " + + DateFormat.getTimeInstance(DateFormat.SHORT) + .format(stop.arivalTime); + } + } + } catch (SQLException e) { + e.printStackTrace(); + } - public void onClick(View v) { - if (context.activityToast == null) { - context.activityToast = Toast.makeText(context, onClickMessage, Toast.LENGTH_SHORT); - } else { - context.activityToast.setText(onClickMessage); - context.activityToast.setDuration(Toast.LENGTH_SHORT); + this.setOnClickListener(this); + this.setOnLongClickListener(this); } - context.activityToast.show(); - - } - - @Override - public boolean onLongClick(View v) { - DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); - - try { - Dao busDao = helper.getBusDao(); - Dao busRouteDao = helper.getBusRouteDao(); - - busDao.refresh(stop.bus); - busRouteDao.refresh(stop.bus.route); - - Log.i("StopView", "Bus route " + stop.bus.route + " Uni-Link " + stop.bus.route.uniLink + " Bus ID " - + stop.bus.id); - - if (stop.bus.id != null && stop.bus.route.uniLink) { - Intent i = new Intent(context, BusActivity.class); - i.putExtra("busID", stop.bus.id); - i.putExtra("busStopID", stop.busStop.id); - ((Activity) context).startActivityForResult(i, 0); - } else { - if (!stop.bus.route.uniLink) { - if (context.activityToast == null) { - context.activityToast = Toast.makeText(context, - "Bus schedules only avalible for Uni-Link buses", Toast.LENGTH_SHORT); - } else { - context.activityToast.setText("Bus schedules only avalible for Uni-Link buses"); - context.activityToast.setDuration(Toast.LENGTH_SHORT); - } - context.activityToast.show(); + + public void onClick(View v) { + if (context.activityToast == null) { + context.activityToast = Toast.makeText(context, onClickMessage, + Toast.LENGTH_SHORT); } else { - if (context.activityToast == null) { - context.activityToast = Toast.makeText(context, - "Bus schedules not avalible for unidentified buses", Toast.LENGTH_SHORT); - } else { - context.activityToast.setText("Bus schedules not avalible for unidentified buses"); + context.activityToast.setText(onClickMessage); context.activityToast.setDuration(Toast.LENGTH_SHORT); - } - context.activityToast.show(); } - } + context.activityToast.show(); + + } - } catch (SQLException e) { - e.printStackTrace(); + public boolean onLongClick(View v) { + DatabaseHelper helper = OpenHelperManager.getHelper(context, + DatabaseHelper.class); + + try { + Dao busDao = helper.getBusDao(); + Dao busRouteDao = helper.getBusRouteDao(); + + busDao.refresh(stop.bus); + busRouteDao.refresh(stop.bus.route); + + Log.i("StopView", "Bus route " + stop.bus.route + " Uni-Link " + + stop.bus.route.uniLink + " Bus ID " + stop.bus.id); + + if (stop.bus.id != null && stop.bus.route.uniLink) { + Intent i = new Intent(context, BusActivity.class); + i.putExtra("busID", stop.bus.id); + i.putExtra("busStopID", stop.busStop.id); + ((Activity) context).startActivityForResult(i, 0); + } else { + if (!stop.bus.route.uniLink) { + if (context.activityToast == null) { + context.activityToast = Toast + .makeText( + context, + "Bus schedules only avalible for Uni-Link buses", + Toast.LENGTH_SHORT); + } else { + context.activityToast + .setText("Bus schedules only avalible for Uni-Link buses"); + context.activityToast.setDuration(Toast.LENGTH_SHORT); + } + context.activityToast.show(); + } else { + if (context.activityToast == null) { + context.activityToast = Toast + .makeText( + context, + "Bus schedules not avalible for unidentified buses", + Toast.LENGTH_SHORT); + } else { + context.activityToast + .setText("Bus schedules not avalible for unidentified buses"); + context.activityToast.setDuration(Toast.LENGTH_SHORT); + } + context.activityToast.show(); + } + } + + } catch (SQLException e) { + e.printStackTrace(); + } + return true; } - return true; - } } -- cgit v1.2.3 From 6e971428e780adc781c3d8146168590dabec7ada Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Fri, 24 Feb 2012 20:47:11 +0000 Subject: Added a BuildingActivity, with a photo, and am begining to use rdf data :) --- src/net/cbaines/suma/BuildingActivity.java | 157 +- src/net/cbaines/suma/BuildingNumOverlay.java | 445 +++--- src/net/cbaines/suma/BusStopActivity.java | 72 +- src/net/cbaines/suma/BusStopOverlay.java | 480 +++--- src/net/cbaines/suma/DataManager.java | 1445 ++++++++++-------- src/net/cbaines/suma/MapActivity.java | 2119 +++++++++++++------------- 6 files changed, 2466 insertions(+), 2252 deletions(-) (limited to 'src') diff --git a/src/net/cbaines/suma/BuildingActivity.java b/src/net/cbaines/suma/BuildingActivity.java index 987d139..930c2d1 100644 --- a/src/net/cbaines/suma/BuildingActivity.java +++ b/src/net/cbaines/suma/BuildingActivity.java @@ -19,67 +19,148 @@ package net.cbaines.suma; +import java.io.IOException; import java.io.InputStream; -import java.util.HashSet; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Iterator; import android.content.Context; +import android.graphics.drawable.Drawable; import android.os.Bundle; - +import android.util.Log; +import android.view.View; +import android.widget.CheckBox; +import android.widget.CompoundButton; +import android.widget.CompoundButton.OnCheckedChangeListener; +import android.widget.ImageButton; +import android.widget.TextView; + +import com.hp.hpl.jena.query.Query; +import com.hp.hpl.jena.query.QueryExecution; +import com.hp.hpl.jena.query.QueryExecutionFactory; +import com.hp.hpl.jena.query.QueryFactory; +import com.hp.hpl.jena.query.QuerySolution; +import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.rdf.model.Literal; +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.RDFNode; +import com.hp.hpl.jena.rdf.model.Resource; +import com.hp.hpl.jena.tdb.TDBFactory; import com.j256.ormlite.android.apptools.OrmLiteBaseActivity; -public class BuildingActivity extends OrmLiteBaseActivity { +public class BuildingActivity extends OrmLiteBaseActivity implements OnCheckedChangeListener { - final static String TAG = "BusTimeActivity"; + final static String TAG = "BusTimeActivity"; - private boolean dataChanged; + private boolean dataChanged; - private Context instance; + private Context instance; - private HashSet routes = new HashSet(); + private ImageButton imageButton; - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.bustimes); + private TextView buildingName; + private TextView buildingID; - final DatabaseHelper helper = getHelper(); + private CheckBox favouritesCheckBox; - // create an empty model - // Model model = ModelFactory.createDefaultModel(); + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.building_activity); + instance = this; - // use the FileManager to find the input file - InputStream in = getResources().openRawResource(R.raw.u9); - if (in == null) { - throw new IllegalArgumentException("File not found"); - } + String ID = getIntent().getExtras().getString("buildingID"); + + favouritesCheckBox = (CheckBox) findViewById(R.id.buildingActivityFavouriteCheckBox); + // favouritesCheckBox.setChecked(busStop.favourite); + favouritesCheckBox.setOnCheckedChangeListener(this); + + buildingName = (TextView) findViewById(R.id.buildingActivityName); + buildingID = (TextView) findViewById(R.id.buildingActivityName); + + Log.i(TAG, "Building id " + ID); + + buildingID.setText(ID); + + try { + URL url = new URL("http://data.southampton.ac.uk/images/buildings/600/" + ID + ".jpg"); + + InputStream is = (InputStream) url.getContent(); + Drawable image = Drawable.createFromStream(is, "src"); + + imageButton = new ImageButton(this); + imageButton = (ImageButton) findViewById(R.id.buildingActivityImage); + imageButton.setImageDrawable(image); + imageButton.setOnClickListener(new View.OnClickListener() { + public void onClick(View v) { + + } + }); + + } catch (MalformedURLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } - // read the RDF/XML file - // model.read(in, null); - - + String directory = "/data/data/net.cbaines.suma/databases"; + Model model = TDBFactory.createModel(directory); - instance = this; + String queryString = ID; + Query query = QueryFactory.create(queryString); + QueryExecution qexec = QueryExecutionFactory.create(query, model); + try { + ResultSet results = qexec.execSelect(); + for (; results.hasNext();) { + QuerySolution soln = results.nextSolution(); - } + Log.i(TAG, "solution " + soln.toString()); - public void onResume() { - super.onResume(); + for (Iterator iter = soln.varNames(); iter.hasNext();) { + Log.i(TAG, "solnVarName " + iter.next()); + } - } + // RDFNode x = soln.get("varName"); // Get a result variable by + // name. + // Resource r = soln.getResource("VarR"); // Get a result + // variable + // - must be a resource + // Literal l = soln.getLiteral("VarL"); // Get a result variable + // - + // must be a literal + } + } finally { + qexec.close(); + } - public void onPause() { + } + + public void onResume() { + super.onResume(); + + } - super.onPause(); - } + public void onPause() { - public void finish() { - setResult(RESULT_OK, getIntent()); + super.onPause(); + } - super.finish(); - } + public void finish() { + setResult(RESULT_OK, getIntent()); - @Override - public Object onRetainNonConfigurationInstance() { - return null; - } + super.finish(); + } + + @Override + public Object onRetainNonConfigurationInstance() { + return null; + } + + public void onCheckedChanged(CompoundButton arg0, boolean arg1) { + // TODO Auto-generated method stub + + } } diff --git a/src/net/cbaines/suma/BuildingNumOverlay.java b/src/net/cbaines/suma/BuildingNumOverlay.java index 32616a7..74688a0 100644 --- a/src/net/cbaines/suma/BuildingNumOverlay.java +++ b/src/net/cbaines/suma/BuildingNumOverlay.java @@ -29,6 +29,8 @@ import org.osmdroid.views.MapView; import org.osmdroid.views.MapView.Projection; import org.osmdroid.views.overlay.Overlay; +import android.app.Activity; +import android.content.Intent; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; @@ -45,260 +47,283 @@ import com.j256.ormlite.dao.Dao; public class BuildingNumOverlay extends Overlay { - private ArrayList buildings; + private ArrayList buildings; - private final Point mCurScreenCoords = new Point(); - private final Point mTouchScreenPoint = new Point(); - private final Point mItemPoint = new Point(); + private final Point mCurScreenCoords = new Point(); + private final Point mTouchScreenPoint = new Point(); + private final Point mItemPoint = new Point(); - private final Rect mRect = new Rect(); + private final Rect mRect = new Rect(); - private final Drawable marker; - private final Drawable favMarker; + private final Drawable marker; + private final Drawable favMarker; - private final Paint paint; + private final Paint paint; - private static final String TAG = "BuildingNumOverlay"; + private static final String TAG = "BuildingNumOverlay"; - private final MapActivity context; + private final MapActivity context; - private Dao buildingDao; + private Dao buildingDao; - private float userScale = 1f; + private float userScale = 1f; - public BuildingNumOverlay(MapActivity context, List buildings) throws SQLException { - super(context); + public BuildingNumOverlay(MapActivity context, List buildings) throws SQLException { + super(context); - this.context = context; + this.context = context; - marker = context.getResources().getDrawable(R.drawable.building); - favMarker = context.getResources().getDrawable(R.drawable.building_fav); + 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(); + DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); + buildingDao = helper.getBuildingDao(); - paint = new Paint(); - paint.setColor(Color.BLACK); - paint.setAntiAlias(true); - paint.setStyle(Style.FILL); - paint.setAlpha(120); - paint.setStrokeWidth(6); - paint.setTextAlign(Paint.Align.CENTER); + paint = new Paint(); + paint.setColor(Color.BLACK); + paint.setAntiAlias(true); + paint.setStyle(Style.FILL); + paint.setAlpha(120); + paint.setStrokeWidth(6); + paint.setTextAlign(Paint.Align.CENTER); - this.buildings = (ArrayList) buildings; - } - - /** - * Draw a marker on each of our items. populate() must have been called first.
- *
- * 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.
- *
- * 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.
- *
- * The focused item is always drawn last, which puts it visually on top of the other items.
- * - * @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 - * @param mapView - * 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. - */ - @Override - public void draw(final Canvas canvas, final MapView mapView, final boolean shadow) { - - if (shadow || !isEnabled()) { - return; + this.buildings = (ArrayList) buildings; } - float scale = mScale * userScale; - - final Projection pj = mapView.getProjection(); - - final int markerWidth = (int) (marker.getIntrinsicWidth() * userScale); - final int markerHeight = (int) (marker.getIntrinsicHeight() * userScale); - - mRect.set(0, 0, 0 + markerWidth, 0 + markerHeight); - mRect.offset(-markerWidth / 2, -markerHeight); - marker.setBounds(mRect); - favMarker.setBounds(mRect); - - /* Draw in backward cycle, so the items with the least index are on the front. */ - for (Iterator buildingIter = buildings.iterator(); buildingIter.hasNext();) { - final Building building = buildingIter.next(); - - // Log.i(TAG, "Looking at drawing stop " + stop.id); - - pj.toMapPixels(building.point, mCurScreenCoords); - - // draw it - if (building.favourite) { - Overlay.drawAt(canvas, favMarker, mCurScreenCoords.x, mCurScreenCoords.y, false); - } else { - Overlay.drawAt(canvas, marker, mCurScreenCoords.x, mCurScreenCoords.y, false); - } - - String idString = String.valueOf(building.id); - - int yOfset = 10; - switch (idString.length()) { - case 1: - paint.setTextSize(25 * scale); - yOfset = 18; - break; - case 2: - paint.setTextSize(24 * scale); - yOfset = 18; - break; - case 3: - paint.setTextSize(17 * scale); - yOfset = 20; - break; - case 4: - paint.setTextSize(14 * scale); - yOfset = 23; - break; - case 5: - paint.setTextSize(10 * scale); - yOfset = 20; - break; - case 6: - paint.setTextSize(9 * scale); - yOfset = 24; - break; - default: - Log.w(TAG, "Reverting to default text size for length " + idString.length()); - paint.setTextSize(15 * scale); - break; - } - canvas.drawText(idString, mCurScreenCoords.x, mCurScreenCoords.y - (yOfset * scale), paint); - } - } + /** + * Draw a marker on each of our items. populate() must have been called + * first.
+ *
+ * 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.
+ *
+ * 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.
+ *
+ * The focused item is always drawn last, which puts it visually on top of + * the other items.
+ * + * @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 + * @param mapView + * 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. + */ + @Override + public void draw(final Canvas canvas, final MapView mapView, final boolean shadow) { + + if (shadow || !isEnabled()) { + return; + } - @Override - public boolean onSingleTapConfirmed(final MotionEvent event, final MapView mapView) { + float scale = mScale * userScale; + + final Projection pj = mapView.getProjection(); + + final int markerWidth = (int) (marker.getIntrinsicWidth() * userScale); + final int markerHeight = (int) (marker.getIntrinsicHeight() * userScale); + + mRect.set(0, 0, 0 + markerWidth, 0 + markerHeight); + mRect.offset(-markerWidth / 2, -markerHeight); + marker.setBounds(mRect); + favMarker.setBounds(mRect); + + /* + * Draw in backward cycle, so the items with the least index are on the + * front. + */ + for (Iterator buildingIter = buildings.iterator(); buildingIter.hasNext();) { + final Building building = buildingIter.next(); + + // Log.i(TAG, "Looking at drawing stop " + stop.id); + + pj.toMapPixels(building.point, mCurScreenCoords); + + // draw it + if (building.favourite) { + Overlay.drawAt(canvas, favMarker, mCurScreenCoords.x, mCurScreenCoords.y, false); + } else { + Overlay.drawAt(canvas, marker, mCurScreenCoords.x, mCurScreenCoords.y, false); + } + + String idString = String.valueOf(building.id); + + int yOfset = 10; + switch (idString.length()) { + case 1: + paint.setTextSize(25 * scale); + yOfset = 18; + break; + case 2: + paint.setTextSize(24 * scale); + yOfset = 18; + break; + case 3: + paint.setTextSize(17 * scale); + yOfset = 20; + break; + case 4: + paint.setTextSize(14 * scale); + yOfset = 23; + break; + case 5: + paint.setTextSize(10 * scale); + yOfset = 20; + break; + case 6: + paint.setTextSize(9 * scale); + yOfset = 24; + break; + default: + Log.w(TAG, "Reverting to default text size for length " + idString.length()); + paint.setTextSize(15 * scale); + break; + } + canvas.drawText(idString, mCurScreenCoords.x, mCurScreenCoords.y - (yOfset * scale), paint); + } + } - if (!this.isEnabled()) - return false; + @Override + public boolean onSingleTapConfirmed(final MotionEvent event, final MapView mapView) { - final Building building = getSelectedItem(event, mapView); + if (!this.isEnabled()) + return false; - if (building == null) { - // Log.v(TAG, "No building pressed"); - return false; - } else { - Log.v(TAG, "building Pressed " + building.id); + final Building building = getSelectedItem(event, mapView); - if (context.activityToast == null) { - 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 + ")"); - } - context.activityToast.show(); + if (building == null) { + // Log.v(TAG, "No building pressed"); + return false; + } else { + Log.v(TAG, "building Pressed " + building.id); + + if (context.activityToast == null) { + 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 + ")"); + } + context.activityToast.show(); + + return true; + } - return true; } - } - - public boolean onDoubleTap(final MotionEvent e, final MapView mapView) { - return false; - } + public boolean onDoubleTap(final MotionEvent event, final MapView mapView) { + Building building = getSelectedItem(event, mapView); - @Override - public boolean onLongPress(final MotionEvent event, final MapView mapView) { + if (building == null) { + // Log.v(TAG, "No busStop pressed"); + return false; + } else { + Log.i(TAG, "Pressed " + building.id); - if (!this.isEnabled()) - return false; + Intent i = new Intent(context, BuildingActivity.class); + i.putExtra("buildingID", building.id); + ((Activity) context).startActivityForResult(i, 0); - final Building building = getSelectedItem(event, mapView); + return true; + } + } - if (building == null) { - // Log.v(TAG, "No building pressed"); - return false; - } else { - Log.v(TAG, "building Pressed " + building.id); + @Override + public boolean onLongPress(final MotionEvent event, final MapView mapView) { - if (building.favourite) { - building.favourite = false; + if (!this.isEnabled()) + return false; - if (context.activityToast == null) { - 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"); - } - context.activityToast.show(); + final Building building = getSelectedItem(event, mapView); - } else { - if (context.activityToast == null) { - context.activityToast = Toast.makeText(context, building.id + " made a favourite", - Toast.LENGTH_SHORT); + if (building == null) { + // Log.v(TAG, "No building pressed"); + return false; } else { - context.activityToast.setDuration(Toast.LENGTH_SHORT); - context.activityToast.setText(building.id + " made a favourite"); + Log.v(TAG, "building Pressed " + building.id); + + if (building.favourite) { + building.favourite = false; + + if (context.activityToast == null) { + 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"); + } + context.activityToast.show(); + + } else { + if (context.activityToast == null) { + 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; + } + + try { + buildingDao.update(building); + } catch (SQLException e) { + e.printStackTrace(); + } + + Collections.sort(buildings, new POIFavouriteComparator()); + + mapView.invalidate(); + + return true; } - context.activityToast.show(); - - building.favourite = true; - } - - try { - buildingDao.update(building); - } catch (SQLException e) { - e.printStackTrace(); - } - - Collections.sort(buildings, new POIFavouriteComparator()); - mapView.invalidate(); - - return true; } - } - - private Building getSelectedItem(final MotionEvent event, final MapView mapView) { - final Projection pj = mapView.getProjection(); - final int eventX = (int) event.getX(); - final int eventY = (int) event.getY(); + private Building getSelectedItem(final MotionEvent event, final MapView mapView) { + final Projection pj = mapView.getProjection(); + final int eventX = (int) event.getX(); + final int eventY = (int) event.getY(); - /* These objects are created to avoid construct new ones every cycle. */ - pj.fromMapPixels(eventX, eventY, mTouchScreenPoint); + /* These objects are created to avoid construct new ones every cycle. */ + pj.fromMapPixels(eventX, eventY, mTouchScreenPoint); - // Iterate back through the array to properly deal with overlap - for (int i = buildings.size() - 1; i > 0; i--) { - final Building building = buildings.get(i); + // Iterate back through the array to properly deal with overlap + for (int i = buildings.size() - 1; i > 0; i--) { + final Building building = buildings.get(i); - pj.toPixels(building.point, mItemPoint); + pj.toPixels(building.point, mItemPoint); - if (marker.getBounds().contains(mTouchScreenPoint.x - mItemPoint.x, mTouchScreenPoint.y - mItemPoint.y)) { - return building; - } + if (marker.getBounds().contains(mTouchScreenPoint.x - mItemPoint.x, mTouchScreenPoint.y - mItemPoint.y)) { + return building; + } + } + return null; } - return null; - } - public void refresh() { - for (int i = 0; i < buildings.size(); i++) { - refresh(buildings.get(i)); + public void refresh() { + for (int i = 0; i < buildings.size(); i++) { + refresh(buildings.get(i)); + } } - } - public void refresh(Building building) { - if (building.favourite) { - buildings.remove(building); - buildings.add(building); + public void refresh(Building building) { + if (building.favourite) { + buildings.remove(building); + buildings.add(building); + } } - } } diff --git a/src/net/cbaines/suma/BusStopActivity.java b/src/net/cbaines/suma/BusStopActivity.java index d9eaed3..0d4dea4 100644 --- a/src/net/cbaines/suma/BusStopActivity.java +++ b/src/net/cbaines/suma/BusStopActivity.java @@ -59,8 +59,8 @@ import com.j256.ormlite.dao.Dao; import com.j256.ormlite.stmt.PreparedQuery; import com.j256.ormlite.stmt.QueryBuilder; -public class BusStopActivity extends OrmLiteBaseActivity - implements OnCheckedChangeListener, Preferences, OnItemClickListener { +public class BusStopActivity extends OrmLiteBaseActivity implements OnCheckedChangeListener, + Preferences, OnItemClickListener { final static String TAG = "BusTimeActivity"; @@ -106,7 +106,7 @@ public class BusStopActivity extends OrmLiteBaseActivity public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.bustimes); + setContentView(R.layout.bus_stop_activity); final DatabaseHelper helper = getHelper(); @@ -132,15 +132,12 @@ public class BusStopActivity extends OrmLiteBaseActivity Dao routeStopsDao = helper.getRouteStopsDao(); for (BusRoute route : busRouteDao) { - QueryBuilder queryBuilder = routeStopsDao - .queryBuilder(); + QueryBuilder queryBuilder = routeStopsDao.queryBuilder(); - queryBuilder.where() - .eq(RouteStops.ROUTE_ID_FIELD_NAME, route.id).and() + queryBuilder.where().eq(RouteStops.ROUTE_ID_FIELD_NAME, route.id).and() .eq(RouteStops.STOP_ID_FIELD_NAME, busStopID); queryBuilder.setCountOf(true); - PreparedQuery preparedQuery = queryBuilder - .prepare(); + PreparedQuery preparedQuery = queryBuilder.prepare(); long count = routeStopsDao.countOf(preparedQuery); @@ -211,12 +208,9 @@ public class BusStopActivity extends OrmLiteBaseActivity public void onResume() { super.onResume(); - SharedPreferences sharedPrefs = PreferenceManager - .getDefaultSharedPreferences(this); - if (sharedPrefs.getBoolean(UNI_LINK_BUS_TIMES, - UNI_LINK_BUS_TIMES_ENABLED_BY_DEFAULT) - || sharedPrefs.getBoolean(NON_UNI_LINK_BUS_TIMES, - NON_UNI_LINK_BUS_TIMES_ENABLED_BY_DEFAULT)) { + SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); + if (sharedPrefs.getBoolean(UNI_LINK_BUS_TIMES, UNI_LINK_BUS_TIMES_ENABLED_BY_DEFAULT) + || sharedPrefs.getBoolean(NON_UNI_LINK_BUS_TIMES, NON_UNI_LINK_BUS_TIMES_ENABLED_BY_DEFAULT)) { Log.i(TAG, "Live Times enabled"); timetable = (Timetable) getLastNonConfigurationInstance(); @@ -294,8 +288,7 @@ public class BusStopActivity extends OrmLiteBaseActivity return timetable; } - private class GetTimetableTask extends - AsyncTask { + private class GetTimetableTask extends AsyncTask { String errorMessage; protected void onPreExecute() { @@ -305,21 +298,12 @@ public class BusStopActivity extends OrmLiteBaseActivity protected Timetable doInBackground(String... activity) { Timetable newTimetable = null; try { - final SharedPreferences sharedPrefs = PreferenceManager - .getDefaultSharedPreferences(instance); - - newTimetable = DataManager - .getTimetable( - instance, - busStopID, - sharedPrefs - .getBoolean( - MapActivity.UNI_LINK_BUS_TIMES, - MapActivity.UNI_LINK_BUS_TIMES_ENABLED_BY_DEFAULT), - sharedPrefs - .getBoolean( - MapActivity.NON_UNI_LINK_BUS_TIMES, - MapActivity.NON_UNI_LINK_BUS_TIMES_ENABLED_BY_DEFAULT)); + final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(instance); + + newTimetable = DataManager.getTimetable(instance, busStopID, sharedPrefs.getBoolean( + MapActivity.UNI_LINK_BUS_TIMES, MapActivity.UNI_LINK_BUS_TIMES_ENABLED_BY_DEFAULT), sharedPrefs + .getBoolean(MapActivity.NON_UNI_LINK_BUS_TIMES, + MapActivity.NON_UNI_LINK_BUS_TIMES_ENABLED_BY_DEFAULT)); } catch (SQLException e) { errorMessage = "Error message regarding SQL?"; e.printStackTrace(); @@ -365,8 +349,7 @@ public class BusStopActivity extends OrmLiteBaseActivity @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle item selection - if (item.getItemId() == R.id.menu_previous_stop - || item.getItemId() == R.id.menu_next_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"); Log.v(TAG, routes.size() + " routes avalible from this stop"); @@ -377,11 +360,9 @@ public class BusStopActivity extends OrmLiteBaseActivity try { Set tmpStops; if (item.getItemId() == R.id.menu_next_stop) { - tmpStops = route.moveInRoute(this, getHelper() - .getBusStopDao().queryForId(busStopID), 1); + tmpStops = route.moveInRoute(this, getHelper().getBusStopDao().queryForId(busStopID), 1); } else { - tmpStops = route.moveInRoute(this, getHelper() - .getBusStopDao().queryForId(busStopID), -1); + tmpStops = route.moveInRoute(this, getHelper().getBusStopDao().queryForId(busStopID), -1); } for (BusStop busStop : tmpStops) { if (!busStops.contains(busStop)) { @@ -410,8 +391,7 @@ public class BusStopActivity extends OrmLiteBaseActivity } else { showDialog(POI_DIALOG_ID); if (busDialog == null) { - Log.e(TAG, - "Very wierd, just tried to launch the favourite's dialog, but its null?"); + Log.e(TAG, "Very wierd, just tried to launch the favourite's dialog, but its null?"); return false; } @@ -450,11 +430,9 @@ public class BusStopActivity extends OrmLiteBaseActivity busTimeContentLayout.setGravity(Gravity.CENTER); } else { - for (Iterator stopIter = visibleTimetable.iterator(); stopIter - .hasNext();) { + for (Iterator stopIter = visibleTimetable.iterator(); stopIter.hasNext();) { Stop stop = stopIter.next(); - Log.i(TAG, "Begin filtering, looking at " + stop - + " with route " + stop.bus.route.code); + Log.i(TAG, "Begin filtering, looking at " + stop + " with route " + stop.bus.route.code); if (stop.bus.route.code.equals("U1")) { if (!U1RouteRadioButton.isChecked()) { stopIter.remove(); @@ -480,8 +458,7 @@ public class BusStopActivity extends OrmLiteBaseActivity if (visibleTimetable.size() == 0) { busTimeContentLayout.setGravity(Gravity.CENTER); - busStopMessage - .setText("No Busses (With the current enabled routes)"); + busStopMessage.setText("No Busses (With the current enabled routes)"); busStopMessage.setVisibility(View.VISIBLE); busTimeList.setVisibility(View.GONE); } else { @@ -510,8 +487,7 @@ public class BusStopActivity extends OrmLiteBaseActivity return null; } - public void onItemClick(AdapterView parent, View view, int position, - long id) { + public void onItemClick(AdapterView parent, View view, int position, long id) { Log.i(TAG, "OnItemClick pos " + position + " id " + id); String poiId = busDialog.adapter.getItemStringId(position); diff --git a/src/net/cbaines/suma/BusStopOverlay.java b/src/net/cbaines/suma/BusStopOverlay.java index 06a1729..060ad47 100644 --- a/src/net/cbaines/suma/BusStopOverlay.java +++ b/src/net/cbaines/suma/BusStopOverlay.java @@ -44,308 +44,314 @@ import com.j256.ormlite.dao.Dao; public class BusStopOverlay extends Overlay implements RouteColorConstants { - List busStops; + List busStops; - private final Point mCurScreenCoords = new Point(); - private final Point mTouchScreenPoint = new Point(); - private final Point mItemPoint = new Point(); + private final Point mCurScreenCoords = new Point(); + private final Point mTouchScreenPoint = new Point(); + private final Point mItemPoint = new Point(); - private final Rect mRect = new Rect(); + private final Rect mRect = new Rect(); - private final Drawable marker; - private final Drawable favMarker; + private final Drawable marker; + private final Drawable favMarker; - private final Paint paint; + private final Paint paint; - private static final String TAG = "BusStopOverlay"; + private static final String TAG = "BusStopOverlay"; - private final MapActivity context; + private final MapActivity context; - private Dao busStopDao; + private Dao busStopDao; - private float userScale = 1f; + private float userScale = 1f; - private boolean[] routes = new boolean[5]; + private boolean[] routes = new boolean[5]; - public BusStopOverlay(MapActivity context, List busStops) throws SQLException { - super(context); + public BusStopOverlay(MapActivity context, List busStops) throws SQLException { + super(context); - this.context = context; + this.context = context; - marker = context.getResources().getDrawable(R.drawable.busstop); - favMarker = context.getResources().getDrawable(R.drawable.busstop_fav); + marker = context.getResources().getDrawable(R.drawable.busstop); + favMarker = context.getResources().getDrawable(R.drawable.busstop_fav); - DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); - busStopDao = helper.getBusStopDao(); + DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); + busStopDao = helper.getBusStopDao(); - paint = new Paint(); - paint.setStyle(Style.FILL); - paint.setStrokeWidth(6); + paint = new Paint(); + paint.setStyle(Style.FILL); + paint.setStrokeWidth(6); - this.busStops = busStops; - } - - void setRoutes(int route, boolean visible) { - routes[route] = visible; - } - - @Override - public void draw(final Canvas canvas, final MapView mapView, final boolean shadow) { - - if (shadow) { - return; + this.busStops = busStops; } - float scale = mScale * userScale; + void setRoutes(int route, boolean visible) { + routes[route] = visible; + } - final Projection pj = mapView.getProjection(); + @Override + public void draw(final Canvas canvas, final MapView mapView, final boolean shadow) { - final int markerWidth = (int) (marker.getIntrinsicWidth() * userScale); - final int markerHeight = (int) (marker.getIntrinsicHeight() * userScale); + if (shadow) { + return; + } - mRect.set(0, 0, 0 + markerWidth, 0 + markerHeight); - mRect.offset(-markerWidth / 2, -markerHeight); - marker.setBounds(mRect); - favMarker.setBounds(mRect); + float scale = mScale * userScale; - /* Draw in backward cycle, so the items with the least index are on the front. */ + final Projection pj = mapView.getProjection(); - for (int stopNum = 0; stopNum < busStops.size(); stopNum++) { - BusStop stop = busStops.get(stopNum); + final int markerWidth = (int) (marker.getIntrinsicWidth() * userScale); + final int markerHeight = (int) (marker.getIntrinsicHeight() * userScale); - byte routeNum = 0; - final byte stopRoutes = stop.routes; + mRect.set(0, 0, 0 + markerWidth, 0 + markerHeight); + mRect.offset(-markerWidth / 2, -markerHeight); + marker.setBounds(mRect); + favMarker.setBounds(mRect); - boolean drawing = false; + /* + * Draw in backward cycle, so the items with the least index are on the + * front. + */ - if (stop.uniLink) { + for (int stopNum = 0; stopNum < busStops.size(); stopNum++) { + BusStop stop = busStops.get(stopNum); - for (int i = 0; i < 5; i++) { - if ((stopRoutes & (1 << i)) != 0) { - routeNum++; - if (routes[i]) { - drawing = true; - } - } - } + byte routeNum = 0; + final byte stopRoutes = stop.routes; - if (!drawing) - continue; + boolean drawing = false; - } + if (stop.uniLink) { - pj.toMapPixels(stop.point, mCurScreenCoords); + for (int i = 0; i < 5; i++) { + if ((stopRoutes & (1 << i)) != 0) { + routeNum++; + if (routes[i]) { + drawing = true; + } + } + } - if (stop.favourite) { - Overlay.drawAt(canvas, favMarker, mCurScreenCoords.x, mCurScreenCoords.y, false); - } else { - Overlay.drawAt(canvas, marker, mCurScreenCoords.x, mCurScreenCoords.y, false); - } + if (!drawing) + continue; - if (stop.uniLink) { + } - int makersPlaced = 0; - int yOfsetPerMarker = (int) (10 * scale); - int markerYSize = (int) (8 * scale); + pj.toMapPixels(stop.point, mCurScreenCoords); - float rectLeft = mCurScreenCoords.x + (8.8f * scale); - float rectRight = rectLeft + markerYSize; + if (stop.favourite) { + Overlay.drawAt(canvas, favMarker, mCurScreenCoords.x, mCurScreenCoords.y, false); + } else { + Overlay.drawAt(canvas, marker, mCurScreenCoords.x, mCurScreenCoords.y, false); + } - if (routeNum == 5) { - markerYSize = (int) (5 * scale); - yOfsetPerMarker = (int) (7 * scale); - } else if (routeNum == 4) { - markerYSize = (int) (6.5f * scale); - yOfsetPerMarker = (int) (8 * scale); + if (stop.uniLink) { + + int makersPlaced = 0; + int yOfsetPerMarker = (int) (10 * scale); + int markerYSize = (int) (8 * scale); + + float rectLeft = mCurScreenCoords.x + (8.8f * scale); + float rectRight = rectLeft + markerYSize; + + if (routeNum == 5) { + markerYSize = (int) (5 * scale); + yOfsetPerMarker = (int) (7 * scale); + } else if (routeNum == 4) { + markerYSize = (int) (6.5f * scale); + yOfsetPerMarker = (int) (8 * scale); + } + + for (int i = 0; i < 5; i++) { + if ((stopRoutes & (1 << i)) != 0) { + + // Log.i(TAG, "Route " + route + " is " + + // routes.get(route)); + + // Log.i(TAG, "Index is " + busRoutes.indexOf(route) + + // " busRoutes " + busRoutes); + + if (i == 0) { + paint.setColor(U1); + } else if (i == 1) { + paint.setColor(U1N); + } else if (i == 2) { + paint.setColor(U2); + } else if (i == 3) { + paint.setColor(U6); + } else if (i == 4) { + paint.setColor(U9); + } else { + Log.e(TAG, "Unknown route code"); + } + + canvas.drawRect(rectLeft, mCurScreenCoords.y + + ((yOfsetPerMarker * makersPlaced) - (45 * scale)), rectRight, mCurScreenCoords.y + + (yOfsetPerMarker * makersPlaced) - ((45 * scale) - markerYSize), paint); + + makersPlaced++; + } + } + } } + } - for (int i = 0; i < 5; i++) { - if ((stopRoutes & (1 << i)) != 0) { + @Override + public boolean onSingleTapConfirmed(final MotionEvent event, final MapView mapView) { - // Log.i(TAG, "Route " + route + " is " + routes.get(route)); + BusStop busStop = getSelectedItem(event, mapView); - // Log.i(TAG, "Index is " + busRoutes.indexOf(route) + " busRoutes " + busRoutes); + if (busStop == null) { + // Log.v(TAG, "No busStop pressed"); + return false; + } else { + Log.i(TAG, "busStop Pressed " + busStop.id); - if (i == 0) { - paint.setColor(U1); - } else if (i == 1) { - paint.setColor(U1N); - } else if (i == 2) { - paint.setColor(U2); - } else if (i == 3) { - paint.setColor(U6); - } else if (i == 4) { - paint.setColor(U9); + if (context.activityToast == null) { + context.activityToast = Toast.makeText(context, busStop.description + " (" + busStop.id + ")", + Toast.LENGTH_SHORT); } else { - Log.e(TAG, "Unknown route code"); + context.activityToast.setDuration(Toast.LENGTH_SHORT); + context.activityToast.setText(busStop.description + " (" + busStop.id + ")"); } + context.activityToast.show(); - canvas.drawRect(rectLeft, mCurScreenCoords.y - + ((yOfsetPerMarker * makersPlaced) - (45 * scale)), rectRight, mCurScreenCoords.y - + (yOfsetPerMarker * makersPlaced) - ((45 * scale) - markerYSize), paint); - - makersPlaced++; - } + return true; } - } } - } - - @Override - public boolean onSingleTapConfirmed(final MotionEvent event, final MapView mapView) { - BusStop busStop = getSelectedItem(event, mapView); + public boolean onDoubleTap(final MotionEvent event, final MapView mapView) { - if (busStop == null) { - // Log.v(TAG, "No busStop pressed"); - return false; - } else { - Log.i(TAG, "busStop Pressed " + busStop.id); + BusStop busStop = getSelectedItem(event, mapView); - if (context.activityToast == null) { - context.activityToast = Toast.makeText(context, busStop.description + " (" + busStop.id + ")", - Toast.LENGTH_SHORT); - } else { - context.activityToast.setDuration(Toast.LENGTH_SHORT); - context.activityToast.setText(busStop.description + " (" + busStop.id + ")"); - } - context.activityToast.show(); - - return true; - } - } - - public boolean onDoubleTap(final MotionEvent event, final MapView mapView) { - - BusStop busStop = getSelectedItem(event, mapView); + if (busStop == null) { + // Log.v(TAG, "No busStop pressed"); + return false; + } else { + Log.i(TAG, "Pressed " + busStop.id); - if (busStop == null) { - // Log.v(TAG, "No busStop pressed"); - return false; - } else { - Log.i(TAG, "Pressed " + busStop.id); + Intent i = new Intent(context, BusStopActivity.class); + i.putExtra("busStopID", busStop.id); + i.putExtra("busStopName", busStop.description); + ((Activity) context).startActivityForResult(i, 0); - Intent i = new Intent(context, BusStopActivity.class); - i.putExtra("busStopID", busStop.id); - i.putExtra("busStopName", busStop.description); - ((Activity) context).startActivityForResult(i, 0); + return true; + } - return true; } - } - - @Override - public boolean onLongPress(final MotionEvent event, final MapView mapView) { - - BusStop busStop = getSelectedItem(event, mapView); + @Override + public boolean onLongPress(final MotionEvent event, final MapView mapView) { - if (busStop == null) { - // Log.v(TAG, "No busStop pressed"); - return false; - } else { - Log.i(TAG, "Pressed " + busStop.id); + BusStop busStop = getSelectedItem(event, mapView); - if (busStop.favourite) { - busStop.favourite = false; - - if (context.activityToast == null) { - context.activityToast = Toast.makeText(context, busStop.id + " removed from favourites", - Toast.LENGTH_SHORT); - } else { - context.activityToast.setDuration(Toast.LENGTH_SHORT); - context.activityToast.setText(busStop.id + " removed from favourites"); - } - context.activityToast.show(); - } else { - if (context.activityToast == null) { - context.activityToast = Toast.makeText(context, busStop.id + " made a favourite", - Toast.LENGTH_SHORT); + if (busStop == null) { + // Log.v(TAG, "No busStop pressed"); + return false; } else { - context.activityToast.setDuration(Toast.LENGTH_SHORT); - context.activityToast.setText(busStop.id + " made a favourite"); - } - context.activityToast.show(); - - busStop.favourite = true; - } + Log.i(TAG, "Pressed " + busStop.id); + + if (busStop.favourite) { + busStop.favourite = false; + + if (context.activityToast == null) { + context.activityToast = Toast.makeText(context, busStop.id + " removed from favourites", + Toast.LENGTH_SHORT); + } else { + context.activityToast.setDuration(Toast.LENGTH_SHORT); + context.activityToast.setText(busStop.id + " removed from favourites"); + } + context.activityToast.show(); + } else { + if (context.activityToast == null) { + context.activityToast = Toast.makeText(context, busStop.id + " made a favourite", + Toast.LENGTH_SHORT); + } else { + context.activityToast.setDuration(Toast.LENGTH_SHORT); + context.activityToast.setText(busStop.id + " made a favourite"); + } + context.activityToast.show(); + + busStop.favourite = true; + } - try { - busStopDao.update(busStop); - } catch (SQLException e) { - e.printStackTrace(); - } + try { + busStopDao.update(busStop); + } catch (SQLException e) { + e.printStackTrace(); + } - Collections.sort(busStops, new POIFavouriteComparator()); + Collections.sort(busStops, new POIFavouriteComparator()); - mapView.invalidate(); + mapView.invalidate(); - return true; - } - } - - public void refresh() { - try { - for (int i = 0; i < busStops.size(); i++) { - BusStop busStop = busStops.get(i); - busStopDao.refresh(busStop); - if (busStop.favourite) { - busStops.remove(i); - busStops.add(busStop); - } else { - busStops.set(i, busStop); + return true; } - } - } catch (SQLException e) { - e.printStackTrace(); } - } - - /** - * Replaces any bus stops that equal the argument in the overlay with the argument - * - * @param busStop - */ - public void refresh(BusStop busStop) { - for (int i = 0; i < busStops.size(); i++) { - if (busStop.equals(busStops.get(i))) { - busStops.set(i, busStop); - } + + public void refresh() { + try { + for (int i = 0; i < busStops.size(); i++) { + BusStop busStop = busStops.get(i); + busStopDao.refresh(busStop); + if (busStop.favourite) { + busStops.remove(i); + busStops.add(busStop); + } else { + busStops.set(i, busStop); + } + } + } catch (SQLException e) { + e.printStackTrace(); + } } - } - - private BusStop getSelectedItem(final MotionEvent event, final MapView mapView) { - final Projection pj = mapView.getProjection(); - final int eventX = (int) event.getX(); - final int eventY = (int) event.getY(); - - /* These objects are created to avoid construct new ones every cycle. */ - pj.fromMapPixels(eventX, eventY, mTouchScreenPoint); - - for (int i = busStops.size() - 1; i > 0; i--) { - BusStop busStop = busStops.get(i); - - pj.toPixels(busStop.point, mItemPoint); - - if (marker.getBounds().contains(mTouchScreenPoint.x - mItemPoint.x, mTouchScreenPoint.y - mItemPoint.y)) { - if (busStop.uniLink) { - boolean drawing = false; - for (int route = 0; route < 5; route++) { - if ((busStop.routes & (1 << route)) != 0) { - if (routes[route]) { - drawing = true; - break; - } + + /** + * Replaces any bus stops that equal the argument in the overlay with the + * argument + * + * @param busStop + */ + public void refresh(BusStop busStop) { + for (int i = 0; i < busStops.size(); i++) { + if (busStop.equals(busStops.get(i))) { + busStops.set(i, busStop); } - } - if (!drawing) - continue; } + } - return busStop; - } + private BusStop getSelectedItem(final MotionEvent event, final MapView mapView) { + final Projection pj = mapView.getProjection(); + final int eventX = (int) event.getX(); + final int eventY = (int) event.getY(); + + /* These objects are created to avoid construct new ones every cycle. */ + pj.fromMapPixels(eventX, eventY, mTouchScreenPoint); + + for (int i = busStops.size() - 1; i > 0; i--) { + BusStop busStop = busStops.get(i); + + pj.toPixels(busStop.point, mItemPoint); + + if (marker.getBounds().contains(mTouchScreenPoint.x - mItemPoint.x, mTouchScreenPoint.y - mItemPoint.y)) { + if (busStop.uniLink) { + boolean drawing = false; + for (int route = 0; route < 5; route++) { + if ((busStop.routes & (1 << route)) != 0) { + if (routes[route]) { + drawing = true; + break; + } + } + } + if (!drawing) + continue; + } + + return busStop; + } + } + return null; } - return null; - } } diff --git a/src/net/cbaines/suma/DataManager.java b/src/net/cbaines/suma/DataManager.java index c6a3a36..56dac5c 100644 --- a/src/net/cbaines/suma/DataManager.java +++ b/src/net/cbaines/suma/DataManager.java @@ -23,6 +23,8 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.io.StringWriter; import java.sql.SQLException; import java.util.ArrayList; import java.util.Calendar; @@ -58,6 +60,10 @@ import org.xml.sax.XMLReader; import android.content.Context; import android.util.Log; +import com.hp.hpl.jena.query.Dataset; +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.ModelFactory; +import com.hp.hpl.jena.tdb.TDBFactory; import com.j256.ormlite.android.apptools.OpenHelperManager; import com.j256.ormlite.dao.Dao; import com.j256.ormlite.stmt.PreparedQuery; @@ -66,823 +72,914 @@ import com.j256.ormlite.table.TableUtils; public class DataManager { - private final static String TAG = "DataManager"; + private final static String TAG = "DataManager"; - private final static String busStopUrl = "http://data.southampton.ac.uk/bus-stop/"; + private final static String busStopUrl = "http://data.southampton.ac.uk/bus-stop/"; - private static DatabaseHelper helper; - private static Dao busRouteDao; - private static Dao busDao; - private static Dao busStopDao; + private static DatabaseHelper helper; + private static Dao busRouteDao; + private static Dao busDao; + private static Dao busStopDao; - public static void loadBuildings(Context context) throws SQLException, IOException { - DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); - Dao buildingDao = helper.getBuildingDao(); + public static void loadBuildings(Context context) throws SQLException, IOException { + DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); + Dao buildingDao = helper.getBuildingDao(); - TableUtils.clearTable(helper.getConnectionSource(), Building.class); + TableUtils.clearTable(helper.getConnectionSource(), Building.class); - Log.i(TAG, "Begining loading buildings from csv"); + Log.i(TAG, "Begining loading buildings from csv"); - HashMap buildingPoints = new HashMap(); + HashMap buildingPoints = new HashMap(); - InputStream inputStream = context.getAssets().open("buildings_points.csv"); - BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); - String strLine; - try { - String def = bufferedReader.readLine(); - // Log.i(TAG, "Reading the definition " + def); + InputStream inputStream = context.getAssets().open("buildings_points.csv"); + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); + String strLine; + try { + String def = bufferedReader.readLine(); + // Log.i(TAG, "Reading the definition " + def); - while ((strLine = bufferedReader.readLine()) != null) { - // Log.i(TAG, "Data: " + strLine); - String[] dataBits = strLine.split(","); - GeoPoint point = Util.csLatLongToGeoPoint(dataBits[2], dataBits[1]); - // Log.i(TAG, "Creating building with id " + dataBits[0] + " and " + point.getLatitudeE6() + " " + - // point.getLongitudeE6()); - buildingPoints.put(dataBits[0], point); - } + while ((strLine = bufferedReader.readLine()) != null) { + // Log.i(TAG, "Data: " + strLine); + String[] dataBits = strLine.split(","); + GeoPoint point = Util.csLatLongToGeoPoint(dataBits[2], dataBits[1]); + // Log.i(TAG, "Creating building with id " + dataBits[0] + + // " and " + point.getLatitudeE6() + " " + + // point.getLongitudeE6()); + buildingPoints.put(dataBits[0], point); + } + + bufferedReader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + // Log.i(TAG, "Number of building points " + buildingPoints.size()); + + inputStream = context.getAssets().open("building_estates.csv"); + bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); + + try { + String def = bufferedReader.readLine(); + // Log.i(TAG, "Reading the definition " + def); + + while ((strLine = bufferedReader.readLine()) != null) { + // Log.i(TAG, "Data: " + strLine); + if (strLine.startsWith("\"")) { + String[] quoteBits = strLine.split("\""); + // Log.i(TAG, "QuoteBits " + quoteBits[0] + " | " + + // quoteBits[1]); + String[] dataBits = quoteBits[2].split(","); + // Log.i(TAG, "dataBits " + dataBits[0] + " | " + + // dataBits[1]); + + if (buildingPoints.get(dataBits[1]) == null) { + // Log.w(TAG, "Building " + dataBits[1] + + // " has a null point"); + continue; + } + + Building bdg = new Building(dataBits[1], buildingPoints.get(dataBits[1]), dataBits[3].equals("R"), + quoteBits[0]); + /* + * Polygon poly = buildingPolys.get(dataBits[1]); + * + * if (poly != null) { bdg.outline = poly; // Log.i(TAG, + * "Adding building " + key + " " + + * bdg.point.getLatitudeE6() + " " + + * bdg.point.getLongitudeE6() + " " + poly); } else { // + * Log.i(TAG, "Adding building " + key + " " + + * bdg.point.getLatitudeE6() + " " + + * bdg.point.getLongitudeE6()); } + */ + + // Log.i(TAG, "Creating building " + bdg.id + " " + bdg.name + // + " " + bdg.point + " " + + // bdg.residential + " " + bdg.outline); + + buildingDao.create(bdg); + + } else { + + String[] dataBits = strLine.split(","); + + if (buildingPoints.get(dataBits[1]) == null) { + // Log.w(TAG, "Building " + dataBits[1] + + // " has a null point"); + continue; + } + + Building bdg = new Building(dataBits[1], buildingPoints.get(dataBits[1]), dataBits[3].equals("R"), + dataBits[0]); + /* + * Polygon poly = buildingPolys.get(dataBits[1]); + * + * if (poly != null) { bdg.outline = poly; // Log.i(TAG, + * "Adding building " + key + " " + + * bdg.point.getLatitudeE6() + " " + + * bdg.point.getLongitudeE6() + " " + poly); } else { // + * Log.i(TAG, "Adding building " + key + " " + + * bdg.point.getLatitudeE6() + " " + + * bdg.point.getLongitudeE6()); } + */ + + // Log.i(TAG, "Creating building " + bdg.id + " " + bdg.name + // + " " + bdg.point + " " + + // bdg.residential + " " + bdg.outline); + + buildingDao.create(bdg); + + } + } + + bufferedReader.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + /* + * for (Iterator iter = buildingPoints.keySet().iterator(); + * iter.hasNext();) { String key = iter.next(); + * + * Building bdg = new Building(key, buildingPoints.get(key), false); + * Polygon poly = buildingPolys.get(key); + * + * if (poly != null) { bdg.outline = poly; // Log.i(TAG, + * "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + + * bdg.point.getLongitudeE6() + " " + poly); } else { // Log.i(TAG, + * "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + + * bdg.point.getLongitudeE6()); } + * + * buildingDao.create(bdg); } + */ - bufferedReader.close(); - } catch (IOException e) { - e.printStackTrace(); } - // Log.i(TAG, "Number of building points " + buildingPoints.size()); + public static void loadBuildingPhotos(Context context) { + // Direct way: Make a TDB-backed Jena model in the named directory. + String directory = "/data/data/net.cbaines.suma/databases"; + Model model = TDBFactory.createModel(directory); - inputStream = context.getAssets().open("building_estates.csv"); - bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); + Log.i(TAG, "Created the model"); - try { - String def = bufferedReader.readLine(); - // Log.i(TAG, "Reading the definition " + def); + // write it to standard out + StringWriter sw = new StringWriter(); + model.write(new PrintWriter(sw)); + Log.v(TAG, sw.toString()); - while ((strLine = bufferedReader.readLine()) != null) { - // Log.i(TAG, "Data: " + strLine); - if (strLine.startsWith("\"")) { - String[] quoteBits = strLine.split("\""); - // Log.i(TAG, "QuoteBits " + quoteBits[0] + " | " + quoteBits[1]); - String[] dataBits = quoteBits[2].split(","); - // Log.i(TAG, "dataBits " + dataBits[0] + " | " + dataBits[1]); + Log.i(TAG, "Finished the first output"); - if (buildingPoints.get(dataBits[1]) == null) { - // Log.w(TAG, "Building " + dataBits[1] + " has a null point"); - continue; - } + if (model.isEmpty()) { - Building bdg = new Building(dataBits[1], buildingPoints.get(dataBits[1]), dataBits[3].equals("R"), - quoteBits[0]); - /* - * Polygon poly = buildingPolys.get(dataBits[1]); - * - * if (poly != null) { bdg.outline = poly; // Log.i(TAG, "Adding building " + key + " " + - * bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6() + " " + poly); } else { // - * Log.i(TAG, "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + - * bdg.point.getLongitudeE6()); } - */ + Log.i(TAG, "Model is empty"); - // Log.i(TAG, "Creating building " + bdg.id + " " + bdg.name + " " + bdg.point + " " + - // bdg.residential + " " + bdg.outline); + // use the FileManager to find the input file + InputStream in; + try { - buildingDao.create(bdg); + String[] datasets = { "photos.rdf", "places.rdf" }; - } else { + for (String dataset : datasets) { + + in = context.getAssets().open(dataset); + + if (in == null) { + throw new IllegalArgumentException(dataset + " not found"); + } - String[] dataBits = strLine.split(","); + // read the RDF/XML file + model.read(in, null); - if (buildingPoints.get(dataBits[1]) == null) { - // Log.w(TAG, "Building " + dataBits[1] + " has a null point"); - continue; - } + Log.i(TAG, "Finished the read of " + dataset); - Building bdg = new Building(dataBits[1], buildingPoints.get(dataBits[1]), dataBits[3].equals("R"), - dataBits[0]); - /* - * Polygon poly = buildingPolys.get(dataBits[1]); - * - * if (poly != null) { bdg.outline = poly; // Log.i(TAG, "Adding building " + key + " " + - * bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6() + " " + poly); } else { // - * Log.i(TAG, "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + - * bdg.point.getLongitudeE6()); } - */ + } - // Log.i(TAG, "Creating building " + bdg.id + " " + bdg.name + " " + bdg.point + " " + - // bdg.residential + " " + bdg.outline); + // write it to standard out + sw = new StringWriter(); + model.write(new PrintWriter(sw)); + Log.i(TAG, sw.toString()); - buildingDao.create(bdg); + Log.i(TAG, "Finished the second output"); + + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } - } - bufferedReader.close(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + model.close(); - /* - * for (Iterator iter = buildingPoints.keySet().iterator(); iter.hasNext();) { String key = iter.next(); - * - * Building bdg = new Building(key, buildingPoints.get(key), false); Polygon poly = buildingPolys.get(key); - * - * if (poly != null) { bdg.outline = poly; // Log.i(TAG, "Adding building " + key + " " + - * bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6() + " " + poly); } else { // Log.i(TAG, - * "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6()); } - * - * buildingDao.create(bdg); } - */ - - } - - public static void loadBusData(Context context, boolean onlyUniLink) throws SQLException, IOException { - DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); - - Dao busStopDao = helper.getBusStopDao(); - Dao busRouteDao = helper.getBusRouteDao(); - Dao routeStopsDao = helper.getRouteStopsDao(); - - TableUtils.clearTable(helper.getConnectionSource(), BusStop.class); - TableUtils.clearTable(helper.getConnectionSource(), BusRoute.class); - TableUtils.clearTable(helper.getConnectionSource(), RouteStops.class); - - Log.i(TAG, "Loading busstops from csv"); - - InputStream inputStream = context.getAssets().open("bus_stops.csv"); - BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); - String strLine = ""; - - try { - String def = bufferedReader.readLine(); - // Log.i(TAG, "Reading the definition " +def ); - - while ((strLine = bufferedReader.readLine()) != null) { - // Log.i(TAG, "Data: " + strLine); - String[] dataBits = strLine.split(","); - - String[] quBitsLat = dataBits[3].substring(1, dataBits[3].length() - 1).split(" "); - String[] quBitsLng = dataBits[4].substring(1, dataBits[4].length() - 1).split(" "); - - // Log.i(TAG, "Whole " + dataBits[3] + " First bit " + quBitsLat[0] + " last bit " + quBitsLat[1]); - double lat = Double.valueOf(quBitsLat[0]) - + Double.valueOf(quBitsLat[1].substring(0, quBitsLat[1].length() - 1)) / 60d; - // Log.i(TAG, "Whole " + dataBits[4] + " First bit " + quBitsLng[0] + " last bit " + quBitsLng[1]); - double lng = Double.valueOf(quBitsLng[0]) - + Double.valueOf(quBitsLng[1].substring(0, quBitsLng[1].length() - 1)) / 60d; - GeoPoint point = new GeoPoint((int) (lat * 1e6), (int) (lng * -1e6)); - // Log.i(TAG, "Lat " + point.getLatitudeE6() + " lng " + point.getLongitudeE6()); - - busStopDao.create(new BusStop(dataBits[0].replace("\"", ""), dataBits[1].replace("\"", ""), dataBits[2] - .replace("\"", ""), point)); - - } - - bufferedReader.close(); - } catch (IOException e) { - Log.e(TAG, "Line: " + strLine); - e.printStackTrace(); + Log.i(TAG, "Closed the model"); } - Log.i(TAG, "Finished loading busstops, now loading routes"); + public static void loadBusData(Context context, boolean onlyUniLink) throws SQLException, IOException { + DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); - inputStream = context.getAssets().open("routes.csv"); - bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); + Dao busStopDao = helper.getBusStopDao(); + Dao busRouteDao = helper.getBusRouteDao(); + Dao routeStopsDao = helper.getRouteStopsDao(); - try { - String def = bufferedReader.readLine(); - // Log.i(TAG, "Reading the definition " + def); + TableUtils.clearTable(helper.getConnectionSource(), BusStop.class); + TableUtils.clearTable(helper.getConnectionSource(), BusRoute.class); + TableUtils.clearTable(helper.getConnectionSource(), RouteStops.class); - while ((strLine = bufferedReader.readLine()) != null) { - // Log.i(TAG, "Data: " + strLine); - String[] dataBits = strLine.split(","); + Log.i(TAG, "Loading busstops from csv"); - BusRoute route; + InputStream inputStream = context.getAssets().open("bus_stops.csv"); + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); + String strLine = ""; - boolean uniLink; - int id = Integer.parseInt(dataBits[0]); - if (id == 326 || id == 468 || id == 327 || id == 329 || id == 354) { - uniLink = true; - } else { - uniLink = false; - } + try { + String def = bufferedReader.readLine(); + // Log.i(TAG, "Reading the definition " +def ); + + while ((strLine = bufferedReader.readLine()) != null) { + // Log.i(TAG, "Data: " + strLine); + String[] dataBits = strLine.split(","); + + String[] quBitsLat = dataBits[3].substring(1, dataBits[3].length() - 1).split(" "); + String[] quBitsLng = dataBits[4].substring(1, dataBits[4].length() - 1).split(" "); + + // Log.i(TAG, "Whole " + dataBits[3] + " First bit " + + // quBitsLat[0] + " last bit " + quBitsLat[1]); + double lat = Double.valueOf(quBitsLat[0]) + + Double.valueOf(quBitsLat[1].substring(0, quBitsLat[1].length() - 1)) / 60d; + // Log.i(TAG, "Whole " + dataBits[4] + " First bit " + + // quBitsLng[0] + " last bit " + quBitsLng[1]); + double lng = Double.valueOf(quBitsLng[0]) + + Double.valueOf(quBitsLng[1].substring(0, quBitsLng[1].length() - 1)) / 60d; + GeoPoint point = new GeoPoint((int) (lat * 1e6), (int) (lng * -1e6)); + // Log.i(TAG, "Lat " + point.getLatitudeE6() + " lng " + + // point.getLongitudeE6()); + + busStopDao.create(new BusStop(dataBits[0].replace("\"", ""), dataBits[1].replace("\"", ""), dataBits[2] + .replace("\"", ""), point)); + + } - route = new BusRoute(id, dataBits[1], dataBits[2].replace("\"", ""), uniLink); - - if (id == 326) { // U1 - route.forwardDirection = "A"; - route.reverseDirection = "C"; - } else if (id == 329) { // U2 - route.forwardDirection = "B"; - route.reverseDirection = "C"; - } else if (id == 327) { // U6 - route.forwardDirection = "H"; - route.reverseDirection = "C"; + bufferedReader.close(); + } catch (IOException e) { + Log.e(TAG, "Line: " + strLine); + e.printStackTrace(); } - // Log.i(TAG, "Loaded route " + route.id + " " + route.code + " " + route.label); - busRouteDao.create(route); + Log.i(TAG, "Finished loading busstops, now loading routes"); - } + inputStream = context.getAssets().open("routes.csv"); + bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); - bufferedReader.close(); - } catch (IOException e) { - Log.e(TAG, "Line: " + strLine); - e.printStackTrace(); - } + try { + String def = bufferedReader.readLine(); + // Log.i(TAG, "Reading the definition " + def); - Log.i(TAG, "Finished loading routes, now loading routestops"); + while ((strLine = bufferedReader.readLine()) != null) { + // Log.i(TAG, "Data: " + strLine); + String[] dataBits = strLine.split(","); - inputStream = context.getAssets().open("routestops.csv"); - bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); + BusRoute route; - try { - String def = bufferedReader.readLine(); - // Log.i(TAG, "Reading the definition " + def); + boolean uniLink; + int id = Integer.parseInt(dataBits[0]); + if (id == 326 || id == 468 || id == 327 || id == 329 || id == 354) { + uniLink = true; + } else { + uniLink = false; + } - while ((strLine = bufferedReader.readLine()) != null) { - // Log.i(TAG, "Data: " + strLine); - String[] dataBits = strLine.split(","); + route = new BusRoute(id, dataBits[1], dataBits[2].replace("\"", ""), uniLink); - BusStop busStop = busStopDao.queryForId(dataBits[2]); - if (busStop != null) { - // Log.i(TAG, "Found stop " + stop.id); - } else { - Log.w(TAG, "No stop found for " + dataBits[2]); - continue; - } + if (id == 326) { // U1 + route.forwardDirection = "A"; + route.reverseDirection = "C"; + } else if (id == 329) { // U2 + route.forwardDirection = "B"; + route.reverseDirection = "C"; + } else if (id == 327) { // U6 + route.forwardDirection = "H"; + route.reverseDirection = "C"; + } - BusRoute busRoute = busRouteDao.queryForId(Integer.parseInt(dataBits[0])); - if (busRoute != null) { - // Log.i(TAG, "Found route " + route.id); - } else { - Log.w(TAG, "No route found for " + dataBits[0]); - continue; - } + // Log.i(TAG, "Loaded route " + route.id + " " + route.code + + // " " + route.label); + busRouteDao.create(route); - int sequence = Integer.parseInt(dataBits[1]); - Log.i(TAG, "Creating RouteStop " + busStop.id + " " + busRoute.code + " " + sequence); - - routeStopsDao.create(new RouteStops(busStop, busRoute, sequence)); - - if (busRoute.id == 326) { // U1 - busStop.routes = (byte) (busStop.routes | 1); - busStop.uniLink = true; - } else if (busRoute.id == 468) { // U1N - busStop.routes = (byte) (busStop.routes | (1 << 1)); - busStop.uniLink = true; - } else if (busRoute.id == 329) { // U2 - busStop.routes = (byte) (busStop.routes | (1 << 2)); - busStop.uniLink = true; - } else if (busRoute.id == 327) { // U6 - busStop.routes = (byte) (busStop.routes | (1 << 3)); - busStop.uniLink = true; - } else if (busRoute.id == 354) { // U9 - busStop.routes = (byte) (busStop.routes | (1 << 4)); - busStop.uniLink = true; - } else { - busStop.routes = 0; + } + + bufferedReader.close(); + } catch (IOException e) { + Log.e(TAG, "Line: " + strLine); + e.printStackTrace(); } - // Log.v(TAG, "Stop routes " + busStop.routes); - busStopDao.update(busStop); + Log.i(TAG, "Finished loading routes, now loading routestops"); + + inputStream = context.getAssets().open("routestops.csv"); + bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); + + try { + String def = bufferedReader.readLine(); + // Log.i(TAG, "Reading the definition " + def); + + while ((strLine = bufferedReader.readLine()) != null) { + // Log.i(TAG, "Data: " + strLine); + String[] dataBits = strLine.split(","); + + BusStop busStop = busStopDao.queryForId(dataBits[2]); + if (busStop != null) { + // Log.i(TAG, "Found stop " + stop.id); + } else { + Log.w(TAG, "No stop found for " + dataBits[2]); + continue; + } + + BusRoute busRoute = busRouteDao.queryForId(Integer.parseInt(dataBits[0])); + if (busRoute != null) { + // Log.i(TAG, "Found route " + route.id); + } else { + Log.w(TAG, "No route found for " + dataBits[0]); + continue; + } + + int sequence = Integer.parseInt(dataBits[1]); + Log.i(TAG, "Creating RouteStop " + busStop.id + " " + busRoute.code + " " + sequence); + + routeStopsDao.create(new RouteStops(busStop, busRoute, sequence)); + + if (busRoute.id == 326) { // U1 + busStop.routes = (byte) (busStop.routes | 1); + busStop.uniLink = true; + } else if (busRoute.id == 468) { // U1N + busStop.routes = (byte) (busStop.routes | (1 << 1)); + busStop.uniLink = true; + } else if (busRoute.id == 329) { // U2 + busStop.routes = (byte) (busStop.routes | (1 << 2)); + busStop.uniLink = true; + } else if (busRoute.id == 327) { // U6 + busStop.routes = (byte) (busStop.routes | (1 << 3)); + busStop.uniLink = true; + } else if (busRoute.id == 354) { // U9 + busStop.routes = (byte) (busStop.routes | (1 << 4)); + busStop.uniLink = true; + } else { + busStop.routes = 0; + } + + // Log.v(TAG, "Stop routes " + busStop.routes); + busStopDao.update(busStop); - } + } - bufferedReader.close(); - } catch (IOException e) { - Log.e(TAG, "Line: " + strLine); - e.printStackTrace(); + bufferedReader.close(); + } catch (IOException e) { + Log.e(TAG, "Line: " + strLine); + e.printStackTrace(); + } + + /* + * for (Iterator busStopIter = busStopDao.iterator(); + * busStopIter.hasNext();) { BusStop stop = busStopIter.next(); // + * Log.i(TAG, "Looking at stop " + stop.id); + * + * + * QueryBuilder routeStopsQueryBuilder = + * routeStopsDao.queryBuilder(); + * routeStopsQueryBuilder.where().eq(columnName, value) + * + * DeleteBuilder deleteBuilder = + * busStopDao.deleteBuilder(); // only delete the rows where password is + * null deleteBuilder.where().in(RouteStops.STOP_ID_FIELD_NAME, objects) + * accountDao.delete(deleteBuilder.prepare()); + * + * + * QueryBuilder routeStopsQueryBuilder = + * routeStopsDao.queryBuilder(); + * routeStopsQueryBuilder.setCountOf(true); + * routeStopsQueryBuilder.where().eq(RouteStops.STOP_ID_FIELD_NAME, + * stop); + * + * PreparedQuery routeStopsPreparedQuery = + * routeStopsQueryBuilder.prepare(); List routeStops = + * routeStopsDao.query(routeStopsPreparedQuery); // long num = + * routeStopsDao.query(routeStopsPreparedQuery).size(); // Log.i(TAG, + * "Number is " + num); + * + * stop.uniLink = false; for (RouteStops routeStop : routeStops) { if + * (routeStop.busRoute.uniLink) { stop.uniLink = true; } } + * busStopDao.update(stop); } + */ + + Log.i(TAG, "Finished loading bus data"); } - /* - * for (Iterator busStopIter = busStopDao.iterator(); busStopIter.hasNext();) { BusStop stop = - * busStopIter.next(); // Log.i(TAG, "Looking at stop " + stop.id); - * - * - * QueryBuilder routeStopsQueryBuilder = routeStopsDao.queryBuilder(); - * routeStopsQueryBuilder.where().eq(columnName, value) - * - * DeleteBuilder deleteBuilder = busStopDao.deleteBuilder(); // only delete the rows where - * password is null deleteBuilder.where().in(RouteStops.STOP_ID_FIELD_NAME, objects) - * accountDao.delete(deleteBuilder.prepare()); - * - * - * QueryBuilder routeStopsQueryBuilder = routeStopsDao.queryBuilder(); - * routeStopsQueryBuilder.setCountOf(true); routeStopsQueryBuilder.where().eq(RouteStops.STOP_ID_FIELD_NAME, - * stop); - * - * PreparedQuery routeStopsPreparedQuery = routeStopsQueryBuilder.prepare(); List - * routeStops = routeStopsDao.query(routeStopsPreparedQuery); // long num = - * routeStopsDao.query(routeStopsPreparedQuery).size(); // Log.i(TAG, "Number is " + num); - * - * stop.uniLink = false; for (RouteStops routeStop : routeStops) { if (routeStop.busRoute.uniLink) { - * stop.uniLink = true; } } busStopDao.update(stop); } - */ - - Log.i(TAG, "Finished loading bus data"); - } - - public static void loadSiteData(Context context) throws SQLException, IOException { - Log.i(TAG, "Begining loading site data"); - - DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); - - TableUtils.clearTable(helper.getConnectionSource(), Site.class); - - Dao siteDao = helper.getSiteDao(); - - InputStream inputStream = context.getAssets().open("sites.csv"); - BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); - String strLine = null; - - try { - String def = bufferedReader.readLine(); - // Log.i(TAG, "Reading the site definition " + def); - - while ((strLine = bufferedReader.readLine()) != null) { - // Log.i(TAG, "Site Data: " + strLine); - String[] dataBits = strLine.split(","); - - GeoPoint point = null; - if (dataBits[2].length() > 1 && dataBits[3].length() > 1) { - point = Util.csLatLongToGeoPoint(dataBits[2], dataBits[3]); - } else { - point = new GeoPoint(0, 0); - } + public static void loadSiteData(Context context) throws SQLException, IOException { + Log.i(TAG, "Begining loading site data"); - Polygon poly = Util.csPolygonToPolygon(strLine.split("\"")[1]); - // Log.i(TAG, "Polygon: " + poly); + DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); - siteDao.create(new Site(dataBits[0], dataBits[1], point, poly)); - } + TableUtils.clearTable(helper.getConnectionSource(), Site.class); - bufferedReader.close(); - } catch (Exception e) { - // TODO Auto-generated catch block - Log.e(TAG, "Site Line: " + strLine); - e.printStackTrace(); - } + Dao siteDao = helper.getSiteDao(); - Log.i(TAG, "Loaded sites from csv"); - } - - private static Stop getStop(Context context, JSONObject stopObj, Set routes, BusStop busStop) - throws SQLException, JSONException { - - if (helper == null) - helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); - if (busRouteDao == null) - busRouteDao = helper.getBusRouteDao(); - Dao routeStopsDao = null; - if (routeStopsDao == null) - routeStopsDao = helper.getRouteStopsDao(); - if (busDao == null) - busDao = helper.getBusDao(); - if (busStopDao == null) - busStopDao = helper.getBusStopDao(); - - String time = stopObj.getString("time"); - - GregorianCalendar calender = new GregorianCalendar(); - boolean live = true; - if (!time.equals("Due")) { - - // Log.v(TAG, "Time: " + time + " current time " + 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))); - } - - // Log.v(TAG, "Date: " + calender.getTime()); - } + InputStream inputStream = context.getAssets().open("sites.csv"); + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); + String strLine = null; - String name = stopObj.getString("name"); + try { + String def = bufferedReader.readLine(); + // Log.i(TAG, "Reading the site definition " + def); - BusRoute route = null; - String dir = ""; + while ((strLine = bufferedReader.readLine()) != null) { + // Log.i(TAG, "Site Data: " + strLine); + String[] dataBits = strLine.split(","); - for (BusRoute tempRoute : routes) { - if (name.contains("U")) { - if (name.equals("U1N")) { - if (tempRoute.code.equals(name)) { - route = tempRoute; - dir = null; - } - } else { - if (tempRoute == null) { - Log.e(TAG, "tempRoute == null"); - } - if (tempRoute.code == null) { - Log.e(TAG, "tempRoute.code == null"); - } - if (tempRoute.code.equals(name.substring(0, 2))) { - route = tempRoute; - 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); - dir = null; - return null; + GeoPoint point = null; + if (dataBits[2].length() > 1 && dataBits[3].length() > 1) { + point = Util.csLatLongToGeoPoint(dataBits[2], dataBits[3]); + } else { + point = new GeoPoint(0, 0); + } + + Polygon poly = Util.csPolygonToPolygon(strLine.split("\"")[1]); + // Log.i(TAG, "Polygon: " + poly); + + siteDao.create(new Site(dataBits[0], dataBits[1], point, poly)); } - } - } - } else { - if (tempRoute.code.equals(name)) { - route = tempRoute; - dir = null; + + bufferedReader.close(); + } catch (Exception e) { + // TODO Auto-generated catch block + Log.e(TAG, "Site Line: " + strLine); + e.printStackTrace(); } - } - } - if (route == null) { - Log.e(TAG, "Route not found (route == null) " + name); - return null; + Log.i(TAG, "Loaded sites from csv"); } - if (dir != null && dir.equals("")) { - Log.e(TAG, "Error detecting direction for " + name); - return null; - } + private static Stop getStop(Context context, JSONObject stopObj, Set routes, BusStop busStop) + throws SQLException, JSONException { + + if (helper == null) + helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); + if (busRouteDao == null) + busRouteDao = helper.getBusRouteDao(); + Dao routeStopsDao = null; + if (routeStopsDao == null) + routeStopsDao = helper.getRouteStopsDao(); + if (busDao == null) + busDao = helper.getBusDao(); + if (busStopDao == null) + busStopDao = helper.getBusStopDao(); + + String time = stopObj.getString("time"); + + GregorianCalendar calender = new GregorianCalendar(); + boolean live = true; + if (!time.equals("Due")) { + + // Log.v(TAG, "Time: " + time + " current time " + + // 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 destString = stopObj.getString("dest"); - BusStop destStop = null; - - if (destString.equals("Central Station")) { - destStop = busStopDao.queryForId("SN120520"); - } else if (destString.equals("Civic Centre")) { - destStop = busStopDao.queryForId("SN120527"); - } else if (destString.equals("City DG4")) { - destStop = busStopDao.queryForId("HAA13579"); - } 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 if (destString.equals("Wessex Lane")) { - destStop = busStopDao.queryForId("SNA19780"); - } else { - Log.e(TAG, "Unknown end dest " + destString + " for route " + route.code); - } + // Log.v(TAG, "Date: " + calender.getTime()); + } - if (destStop != null) { - - QueryBuilder routeStopsQueryBuilder = routeStopsDao.queryBuilder(); - routeStopsQueryBuilder.where().eq(RouteStops.ROUTE_ID_FIELD_NAME, route.id).and() - .eq(RouteStops.STOP_ID_FIELD_NAME, destStop.id); - PreparedQuery routeStopsPreparedQuery = routeStopsQueryBuilder.prepare(); - - List routeStops = routeStopsDao.query(routeStopsPreparedQuery); - if (routeStops.size() > 0) { - Log.i(TAG, "Found " + routeStops.size() + " stops matching the destStop " + destStop + " on route " - + route.code); - } else { - Log.w(TAG, "Found " + routeStops.size() + " stops matching the destStop " + destStop + " on route " - + route.code); - } - } + String name = stopObj.getString("name"); + + BusRoute route = null; + String dir = ""; + + for (BusRoute tempRoute : routes) { + if (name.contains("U")) { + if (name.equals("U1N")) { + if (tempRoute.code.equals(name)) { + route = tempRoute; + dir = null; + } + } else { + if (tempRoute == null) { + Log.e(TAG, "tempRoute == null"); + } + if (tempRoute.code == null) { + Log.e(TAG, "tempRoute.code == null"); + } + if (tempRoute.code.equals(name.substring(0, 2))) { + route = tempRoute; + 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); + dir = null; + return null; + } + } + } + } else { + if (tempRoute.code.equals(name)) { + route = tempRoute; + dir = null; + } + } + } - Date now = new Date(System.currentTimeMillis()); - - String busID = null; - Stop stop; - Bus bus; - if (stopObj.has("vehicle")) { - busID = stopObj.getString("vehicle"); - - QueryBuilder busQueryBuilder = busDao.queryBuilder(); - busQueryBuilder.where().eq(Bus.ID_FIELD_NAME, busID); - PreparedQuery busPreparedQuery = busQueryBuilder.prepare(); - - bus = busDao.queryForFirst(busPreparedQuery); - - if (bus == null) { - bus = new Bus(busID, route, dir); - bus.destination = destStop; - busDao.create(bus); - } else { - bus.destination = destStop; - bus.route = route; - bus.direction = dir; - busDao.update(bus); - } - - } else { - bus = new Bus(null, route, dir); - busDao.create(bus); - } + if (route == null) { + Log.e(TAG, "Route not found (route == null) " + name); + return null; + } - stop = new Stop(bus, busStop, calender.getTime(), now, live); + if (dir != null && dir.equals("")) { + Log.e(TAG, "Error detecting direction for " + name); + return null; + } + + String destString = stopObj.getString("dest"); + BusStop destStop = null; + + if (destString.equals("Central Station")) { + destStop = busStopDao.queryForId("SN120520"); + } else if (destString.equals("Civic Centre")) { + destStop = busStopDao.queryForId("SN120527"); + } else if (destString.equals("City DG4")) { + destStop = busStopDao.queryForId("HAA13579"); + } 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 if (destString.equals("Wessex Lane")) { + destStop = busStopDao.queryForId("SNA19780"); + } else { + Log.e(TAG, "Unknown end dest " + destString + " for route " + route.code); + } - return stop; - } + if (destStop != null) { - public static Timetable getTimetable(Context context, String busStop, boolean keepUniLink, boolean keepNonUniLink) - throws SQLException, ClientProtocolException, IOException, JSONException { + QueryBuilder routeStopsQueryBuilder = routeStopsDao.queryBuilder(); + routeStopsQueryBuilder.where().eq(RouteStops.ROUTE_ID_FIELD_NAME, route.id).and() + .eq(RouteStops.STOP_ID_FIELD_NAME, destStop.id); + PreparedQuery routeStopsPreparedQuery = routeStopsQueryBuilder.prepare(); + + List routeStops = routeStopsDao.query(routeStopsPreparedQuery); + if (routeStops.size() > 0) { + Log.i(TAG, "Found " + routeStops.size() + " stops matching the destStop " + destStop + " on route " + + route.code); + } else { + Log.w(TAG, "Found " + routeStops.size() + " stops matching the destStop " + destStop + " on route " + + route.code); + } + } - if (helper == null) - helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); - if (busRouteDao == null) - busRouteDao = helper.getBusRouteDao(); - if (busStopDao == null) - busStopDao = helper.getBusStopDao(); + Date now = new Date(System.currentTimeMillis()); - Timetable timetable = new Timetable(); + String busID = null; + Stop stop; + Bus bus; + if (stopObj.has("vehicle")) { + busID = stopObj.getString("vehicle"); - String file = getFileFromServer(busStopUrl + busStop + ".json"); + QueryBuilder busQueryBuilder = busDao.queryBuilder(); + busQueryBuilder.where().eq(Bus.ID_FIELD_NAME, busID); + PreparedQuery busPreparedQuery = busQueryBuilder.prepare(); - JSONObject data = new JSONObject(file); - JSONArray stopsArray = data.getJSONArray("stops"); - JSONObject routesObject = data.getJSONObject("routes"); + bus = busDao.queryForFirst(busPreparedQuery); - HashSet busRoutes = new HashSet(); - for (Iterator keyIter = routesObject.keys(); keyIter.hasNext();) { - String key = keyIter.next(); + if (bus == null) { + bus = new Bus(busID, route, dir); + bus.destination = destStop; + busDao.create(bus); + } else { + bus.destination = destStop; + bus.route = route; + bus.direction = dir; + busDao.update(bus); + } - Log.v(TAG, "Route Key: " + key); + } else { + bus = new Bus(null, route, dir); + busDao.create(bus); + } - BusRoute route = busRouteDao.queryForId(Integer.parseInt(key.substring(key.length() - 3, key.length()))); + stop = new Stop(bus, busStop, calender.getTime(), now, live); - if (route != null) { - busRoutes.add(route); - } else { - throw new RuntimeException("Route not found " + key.substring(key.length() - 3, key.length()) + " " - + key); - } + return stop; } - Log.i(TAG, "Number of entries " + data.length()); + public static Timetable getTimetable(Context context, String busStop, boolean keepUniLink, boolean keepNonUniLink) + throws SQLException, ClientProtocolException, IOException, JSONException { - Log.i(TAG, "Stops: " + data.getJSONArray("stops")); + if (helper == null) + helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); + if (busRouteDao == null) + busRouteDao = helper.getBusRouteDao(); + if (busStopDao == null) + busStopDao = helper.getBusStopDao(); - for (int stopNum = 0; stopNum < stopsArray.length(); stopNum++) { - JSONObject stopObj = stopsArray.getJSONObject(stopNum); + Timetable timetable = new Timetable(); - if (!keepNonUniLink && !stopObj.getString("name").startsWith("U")) { - Log.v(TAG, "Skipping non uni-link stop " + stopObj.getString("name")); - continue; - } + String file = getFileFromServer(busStopUrl + busStop + ".json"); - if (!keepUniLink && stopObj.getString("name").startsWith("U")) { - Log.v(TAG, "Skipping uni-link stop " + stopObj.getString("name")); - continue; - } + JSONObject data = new JSONObject(file); + JSONArray stopsArray = data.getJSONArray("stops"); + JSONObject routesObject = data.getJSONObject("routes"); - BusStop busStopObj = busStopDao.queryForId(busStop); - if (busStopObj == null) { - Log.e(TAG, "BusStopObj == null"); - } + HashSet busRoutes = new HashSet(); + for (Iterator keyIter = routesObject.keys(); keyIter.hasNext();) { + String key = keyIter.next(); - Stop stop = getStop(context, stopObj, busRoutes, busStopObj); + Log.v(TAG, "Route Key: " + key); - if (stop == null) { - Log.w(TAG, "Null stop, skiping"); - continue; - } + BusRoute route = busRouteDao.queryForId(Integer.parseInt(key.substring(key.length() - 3, key.length()))); - Log.v(TAG, "Found stop for a unidentified " + stop.bus.toString() + " at " + stop.busStop.id + " at " - + stop.arivalTime); + if (route != null) { + busRoutes.add(route); + } else { + throw new RuntimeException("Route not found " + key.substring(key.length() - 3, key.length()) + " " + + key); + } + } - timetable.add(stop); - } + Log.i(TAG, "Number of entries " + data.length()); - timetable.fetchTime = new Date(System.currentTimeMillis()); - return timetable; - } + Log.i(TAG, "Stops: " + data.getJSONArray("stops")); - public static Timetable getTimetable(Context context, Bus bus, BusStop startStop, int num) throws SQLException, - ClientProtocolException, IOException, JSONException { + for (int stopNum = 0; stopNum < stopsArray.length(); stopNum++) { + JSONObject stopObj = stopsArray.getJSONObject(stopNum); - if (helper == null) - helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); - if (busRouteDao == null) - busRouteDao = helper.getBusRouteDao(); - if (busStopDao == null) - busStopDao = helper.getBusStopDao(); + if (!keepNonUniLink && !stopObj.getString("name").startsWith("U")) { + Log.v(TAG, "Skipping non uni-link stop " + stopObj.getString("name")); + continue; + } - Timetable timetable = new Timetable(); + if (!keepUniLink && stopObj.getString("name").startsWith("U")) { + Log.v(TAG, "Skipping uni-link stop " + stopObj.getString("name")); + continue; + } - List busStops = new ArrayList(num); - busStops.add(startStop); + BusStop busStopObj = busStopDao.queryForId(busStop); + if (busStopObj == null) { + Log.e(TAG, "BusStopObj == null"); + } + + Stop stop = getStop(context, stopObj, busRoutes, busStopObj); - BusRoute route = bus.route; + if (stop == null) { + Log.w(TAG, "Null stop, skiping"); + continue; + } - for (int i = 0; i < num; i++) { - BusStop nextStop = route.moveInRoute(context, busStops.get(i), bus.direction, 1); + Log.v(TAG, "Found stop for a unidentified " + stop.bus.toString() + " at " + stop.busStop.id + " at " + + stop.arivalTime); - if (nextStop != null) { - busStops.add(nextStop); - } else { - Log.e(TAG, "nextStop is null"); - } + timetable.add(stop); + } + + timetable.fetchTime = new Date(System.currentTimeMillis()); + return timetable; } - for (BusStop busStop : busStops) { + public static Timetable getTimetable(Context context, Bus bus, BusStop startStop, int num) throws SQLException, + ClientProtocolException, IOException, JSONException { + + if (helper == null) + helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); + if (busRouteDao == null) + busRouteDao = helper.getBusRouteDao(); + if (busStopDao == null) + busStopDao = helper.getBusStopDao(); + + Timetable timetable = new Timetable(); + + List busStops = new ArrayList(num); + busStops.add(startStop); + + BusRoute route = bus.route; - String file = getFileFromServer(busStopUrl + busStop.id + ".json"); + for (int i = 0; i < num; i++) { + BusStop nextStop = route.moveInRoute(context, busStops.get(i), bus.direction, 1); - JSONObject data = new JSONObject(file); - JSONArray stopsArray = data.getJSONArray("stops"); + if (nextStop != null) { + busStops.add(nextStop); + } else { + Log.e(TAG, "nextStop is null"); + } + } + + for (BusStop busStop : busStops) { - HashSet busRoutes = new HashSet(); - busRoutes.add(bus.route); + String file = getFileFromServer(busStopUrl + busStop.id + ".json"); - Log.v(TAG, "Number of entries " + data.length()); + JSONObject data = new JSONObject(file); + JSONArray stopsArray = data.getJSONArray("stops"); - Log.v(TAG, "Stops: " + data.getJSONArray("stops")); + HashSet busRoutes = new HashSet(); + busRoutes.add(bus.route); - for (int stopNum = 0; stopNum < stopsArray.length(); stopNum++) { - JSONObject stopObj = stopsArray.getJSONObject(stopNum); + Log.v(TAG, "Number of entries " + data.length()); - if (stopObj.getString("vehicle").equals(bus.id)) { + Log.v(TAG, "Stops: " + data.getJSONArray("stops")); - Stop stop = getStop(context, stopObj, busRoutes, busStop); + for (int stopNum = 0; stopNum < stopsArray.length(); stopNum++) { + JSONObject stopObj = stopsArray.getJSONObject(stopNum); - if (stop == null) { - Log.w(TAG, "Null stop, skiping"); - continue; - } + if (stopObj.getString("vehicle").equals(bus.id)) { - Log.v(TAG, "Found stop for a unidentified " + stop.bus.toString() + " at " + stop.busStop.id - + " at " + stop.arivalTime); + Stop stop = getStop(context, stopObj, busRoutes, busStop); - timetable.add(stop); + if (stop == null) { + Log.w(TAG, "Null stop, skiping"); + continue; + } + Log.v(TAG, "Found stop for a unidentified " + stop.bus.toString() + " at " + stop.busStop.id + + " at " + stop.arivalTime); + + timetable.add(stop); + + } + } } - } - } - timetable.fetchTime = new Date(System.currentTimeMillis()); + timetable.fetchTime = new Date(System.currentTimeMillis()); - return timetable; - } + return timetable; + } - public static Stop getStop(Context context, Bus bus, BusStop busStop) throws SQLException, ClientProtocolException, - IOException, JSONException { + public static Stop getStop(Context context, Bus bus, BusStop busStop) throws SQLException, ClientProtocolException, + IOException, JSONException { - if (helper == null) - helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); - if (busRouteDao == null) - busRouteDao = helper.getBusRouteDao(); - if (busStopDao == null) - busStopDao = helper.getBusStopDao(); + if (helper == null) + helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); + if (busRouteDao == null) + busRouteDao = helper.getBusRouteDao(); + if (busStopDao == null) + busStopDao = helper.getBusStopDao(); - String file = getFileFromServer(busStopUrl + busStop.id + ".json"); + String file = getFileFromServer(busStopUrl + busStop.id + ".json"); - JSONObject data = new JSONObject(file); - JSONArray stopsArray = data.getJSONArray("stops"); + JSONObject data = new JSONObject(file); + JSONArray stopsArray = data.getJSONArray("stops"); - HashSet busRoutes = new HashSet(); - busRouteDao.refresh(bus.route); - busRoutes.add(bus.route); + HashSet busRoutes = new HashSet(); + busRouteDao.refresh(bus.route); + busRoutes.add(bus.route); - Stop stop = null; + Stop stop = null; - // Log.v(TAG, "Number of entries " + data.length()); + // Log.v(TAG, "Number of entries " + data.length()); - // Log.v(TAG, "Stops: " + data.getJSONArray("stops")); + // Log.v(TAG, "Stops: " + data.getJSONArray("stops")); - for (int stopNum = 0; stopNum < stopsArray.length(); stopNum++) { - JSONObject stopObj = stopsArray.getJSONObject(stopNum); + for (int stopNum = 0; stopNum < stopsArray.length(); stopNum++) { + JSONObject stopObj = stopsArray.getJSONObject(stopNum); - // Log.v(TAG, "stopObj: " + stopObj); - if (stopObj.has("vehicle") && stopObj.getString("vehicle").equals(bus.id)) { + // Log.v(TAG, "stopObj: " + stopObj); + if (stopObj.has("vehicle") && stopObj.getString("vehicle").equals(bus.id)) { - stop = getStop(context, stopObj, busRoutes, busStop); - break; + stop = getStop(context, stopObj, busRoutes, busStop); + break; - // Log.v(TAG, "Found stop for a unidentified " + stop.bus.toString() + " at " + stop.busStop.id + " at " - // + stop.arivalTime); - } + // Log.v(TAG, "Found stop for a unidentified " + + // stop.bus.toString() + " at " + stop.busStop.id + " at " + // + stop.arivalTime); + } + } + + return stop; } - return stop; - } + static PathOverlay getRoutePath(InputStream routeResource, int colour, ResourceProxy resProxy) { + PathOverlay data = null; - static PathOverlay getRoutePath(InputStream routeResource, int colour, ResourceProxy resProxy) { - PathOverlay data = null; + // sax stuff + try { + SAXParserFactory spf = SAXParserFactory.newInstance(); + SAXParser sp = spf.newSAXParser(); - // sax stuff - try { - SAXParserFactory spf = SAXParserFactory.newInstance(); - SAXParser sp = spf.newSAXParser(); + XMLReader xr = sp.getXMLReader(); - XMLReader xr = sp.getXMLReader(); + DataHandler dataHandler = new DataHandler(colour, resProxy); + xr.setContentHandler(dataHandler); - DataHandler dataHandler = new DataHandler(colour, resProxy); - xr.setContentHandler(dataHandler); + xr.parse(new InputSource(routeResource)); - xr.parse(new InputSource(routeResource)); + data = dataHandler.getData(); - data = dataHandler.getData(); + } catch (ParserConfigurationException pce) { + Log.e("SAX XML", "sax parse error", pce); + } catch (SAXException se) { + Log.e("SAX XML", "sax error", se); + } catch (IOException ioe) { + Log.e("SAX XML", "sax parse io error", ioe); + } - } catch (ParserConfigurationException pce) { - Log.e("SAX XML", "sax parse error", pce); - } catch (SAXException se) { - Log.e("SAX XML", "sax error", se); - } catch (IOException ioe) { - Log.e("SAX XML", "sax parse io error", ioe); + return data; } - return data; - } - - public static String getFileFromServer(String request) throws ClientProtocolException, IOException { - StringBuilder builder = new StringBuilder(); - HttpClient client = new DefaultHttpClient(); - HttpGet httpGet = new HttpGet(request); - Log.v("Util.getFileFromServer", "Request used: " + request); - - HttpResponse response = client.execute(httpGet); - StatusLine statusLine = response.getStatusLine(); - int statusCode = statusLine.getStatusCode(); - if (statusCode == 200) { - HttpEntity entity = response.getEntity(); - InputStream content = entity.getContent(); - BufferedReader reader = new BufferedReader(new InputStreamReader(content)); - String line; - while ((line = reader.readLine()) != null) { - builder.append(line); - } - } else { - Log.e("", "Failed to download file"); + public static String getFileFromServer(String request) throws ClientProtocolException, IOException { + StringBuilder builder = new StringBuilder(); + HttpClient client = new DefaultHttpClient(); + HttpGet httpGet = new HttpGet(request); + Log.v("Util.getFileFromServer", "Request used: " + request); + + HttpResponse response = client.execute(httpGet); + StatusLine statusLine = response.getStatusLine(); + int statusCode = statusLine.getStatusCode(); + if (statusCode == 200) { + HttpEntity entity = response.getEntity(); + InputStream content = entity.getContent(); + BufferedReader reader = new BufferedReader(new InputStreamReader(content)); + String line; + while ((line = reader.readLine()) != null) { + builder.append(line); + } + } else { + Log.e("", "Failed to download file"); + } + + return builder.toString(); } - return builder.toString(); - } - - public static void routeMovementTest(Context context) throws SQLException { - - Dao routeStopsDao = null; - - if (helper == null) - helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); - if (routeStopsDao == null) - routeStopsDao = helper.getRouteStopsDao(); - if (busRouteDao == null) - busRouteDao = helper.getBusRouteDao(); - if (busStopDao == null) - busStopDao = helper.getBusStopDao(); - - for (BusRoute busRoute : busRouteDao) { - if (!busRoute.code.startsWith("U") || busRoute.code.equals("U9")) { - continue; - } - List routeStops = routeStopsDao.queryForEq(RouteStops.ROUTE_ID_FIELD_NAME, busRoute.id); - - ArrayList directions = new ArrayList(); - if (busRoute.forwardDirection != null) { - directions.add(busRoute.forwardDirection); - } - if (busRoute.reverseDirection != null) { - directions.add(busRoute.reverseDirection); - } - if (directions.size() == 0) { - directions.add(null); - } - - BusStop startBusStop = null; - - for (int moveAmount = 1; moveAmount >= -1; moveAmount = moveAmount - 2) { - for (String direction : directions) { - for (int busStop = 0; busStop < routeStops.size() && busStop >= 0; busStop = busStop + moveAmount) { - // if (routeStops.get(busStop).stop.equals(startBusStop)) - // continue; - startBusStop = routeStops.get(busStop).busStop; - busStopDao.refresh(startBusStop); - - BusStop predictedNextStop = busRoute.moveInRoute(context, startBusStop, direction, moveAmount); - - BusStop nextStop; - if (busStop == routeStops.size() - 1) { - nextStop = routeStops.get(0).busStop; - } else { - nextStop = routeStops.get(busStop + 1).busStop; + public static void routeMovementTest(Context context) throws SQLException { + + Dao routeStopsDao = null; + + if (helper == null) + helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); + if (routeStopsDao == null) + routeStopsDao = helper.getRouteStopsDao(); + if (busRouteDao == null) + busRouteDao = helper.getBusRouteDao(); + if (busStopDao == null) + busStopDao = helper.getBusStopDao(); + + for (BusRoute busRoute : busRouteDao) { + if (!busRoute.code.startsWith("U") || busRoute.code.equals("U9")) { + continue; } - busStopDao.refresh(nextStop); + List routeStops = routeStopsDao.queryForEq(RouteStops.ROUTE_ID_FIELD_NAME, busRoute.id); - if (!nextStop.equals(predictedNextStop) && !startBusStop.equals(nextStop)) { - Log.e(TAG, "predicted: " + predictedNextStop + " next: " + nextStop); - Log.e(TAG, startBusStop.id + " " + nextStop.id); - return; + ArrayList directions = new ArrayList(); + if (busRoute.forwardDirection != null) { + directions.add(busRoute.forwardDirection); + } + if (busRoute.reverseDirection != null) { + directions.add(busRoute.reverseDirection); + } + if (directions.size() == 0) { + directions.add(null); + } + + BusStop startBusStop = null; + + for (int moveAmount = 1; moveAmount >= -1; moveAmount = moveAmount - 2) { + for (String direction : directions) { + for (int busStop = 0; busStop < routeStops.size() && busStop >= 0; busStop = busStop + moveAmount) { + // if + // (routeStops.get(busStop).stop.equals(startBusStop)) + // continue; + startBusStop = routeStops.get(busStop).busStop; + busStopDao.refresh(startBusStop); + + BusStop predictedNextStop = busRoute.moveInRoute(context, startBusStop, direction, moveAmount); + + BusStop nextStop; + if (busStop == routeStops.size() - 1) { + nextStop = routeStops.get(0).busStop; + } else { + nextStop = routeStops.get(busStop + 1).busStop; + } + busStopDao.refresh(nextStop); + + if (!nextStop.equals(predictedNextStop) && !startBusStop.equals(nextStop)) { + Log.e(TAG, "predicted: " + predictedNextStop + " next: " + nextStop); + Log.e(TAG, startBusStop.id + " " + nextStop.id); + return; + } + } + } } - } } - } - } - } + } } diff --git a/src/net/cbaines/suma/MapActivity.java b/src/net/cbaines/suma/MapActivity.java index cad7567..ff61c4e 100644 --- a/src/net/cbaines/suma/MapActivity.java +++ b/src/net/cbaines/suma/MapActivity.java @@ -71,1243 +71,1272 @@ import com.j256.ormlite.dao.Dao; * */ public class MapActivity extends OrmLiteBaseActivity implements MapViewConstants, Runnable, - RouteColorConstants, OnItemClickListener, OnItemLongClickListener, OnSharedPreferenceChangeListener, - Preferences { + RouteColorConstants, OnItemClickListener, OnItemLongClickListener, OnSharedPreferenceChangeListener, + Preferences { - /** - * Enable to use the database in the assets folder, if its not enabled, the database is built from the csv files in - * the assets folder - */ - private boolean useBundledDatabase = true; - - private MapView mapView; - private MapController mapController; - private ResourceProxy mResourceProxy; - - private long startTime; - - static final int VIEW_DIALOG_ID = 0; - static final int FAVOURITE_DIALOG_ID = 1; - private POIDialog favDialog; - - private HashMap overlays = new HashMap(); - private HashMap pastOverlays; - - // Overlays - - // -- Building Overlays - static final String BUILDING_OVERLAYS = "buildingOverlays:"; - - // ---- Residential Building Overlay - private static final String RESIDENTIAL_BUILDING_OVERLAY = "residentialBuildingOverlay"; - private BuildingNumOverlay residentialBuildingOverlay; - static final boolean RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT = true; - private static final int RESIDENTIAL_BUILDING_OVERLAY_RANK = 6; - - // ---- Non-Residential Building Overlay - private static final String NON_RESIDENTIAL_BUILDING_OVERLAY = "nonResidentialBuildingOverlay"; - private BuildingNumOverlay nonResidentialBuildingOverlay; - static final boolean NON_RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT = true; - private static final int NON_RESIDENTIAL_BUILDING_OVERLAY_RANK = 5; - - static final String[] BUILDING_TYPES = { RESIDENTIAL_BUILDING_OVERLAY, NON_RESIDENTIAL_BUILDING_OVERLAY }; - - // -- Bus Stop Overlays - static final String BUS_STOP_OVERLAYS = "busStopOverlays:"; - - // ---- Uni-Link Bus Stop Overlay - private static final String UNI_LINK_BUS_STOP_OVERLAY = "uniLinkBusStopOverlay"; - private BusStopOverlay uniLinkBusStopOverlay; - static final boolean UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT = true; - private static final int UNI_LINK_BUS_STOP_OVERLAY_RANK = 3; + /** + * Enable to use the database in the assets folder, if its not enabled, the + * database is built from the csv files in the assets folder + */ + private boolean useBundledDatabase = true; - // ---- Non Uni-Link Bus Stop Overlay - private BusStopOverlay nonUniLinkBusStopOverlay; - private static final int NON_UNI_LINK_BUS_STOP_OVERLAY_RANK = 4; + private MapView mapView; + private MapController mapController; + private ResourceProxy mResourceProxy; - // -- Site Overlays - static final String[] SITE_NAMES = { "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" }; + private long startTime; - private static final String SITE_OVERLAYS = "siteOverlays:"; - private HashMap siteOverlays = new HashMap(21); - static final boolean SITE_OVERLAYS_ENABLED_BY_DEFAULT = false; - private static final int SITE_OVERLAYS_RANK = 8; + static final int VIEW_DIALOG_ID = 0; + static final int FAVOURITE_DIALOG_ID = 1; + private POIDialog favDialog; - // -- Route Overlays - private static final String BUS_ROUTE_OVERLAYS = "routeOverlays:"; - private HashMap busRouteOverlays = new HashMap(5); - static final boolean BUS_ROUTE_OVERLAYS_ENABLED_BY_DEFAULT = true; - private static final int BUS_ROUTE_OVERLAYS_RANK = 7; + private HashMap overlays = new HashMap(); + private HashMap pastOverlays; - // -- Other - static final String OTHER_OVERLAYS = "otherOverlay:"; + // Overlays - // ---- Scale Bar Overlay - private static final String SCALE_BAR_OVERLAY = "scaleBarOverlay"; - private ScaleBarOverlay scaleBarOverlay; - static final boolean SCALE_BAR_OVERLAY_ENABLED_BY_DEFAULT = true; - private static final int SCALE_BAR_OVERLAY_RANK = 1; + // -- Building Overlays + static final String BUILDING_OVERLAYS = "buildingOverlays:"; - // ---- My Location Overlay - private static final String MY_LOCATION_OVERLAY = "myLocationOverlay"; - private static final String MY_LOCATION_OVERLAY_COMPASS = "myLocationOverlayCompass"; - private MyLocationOverlay myLocationOverlay; - static final boolean MY_LOCATION_OVERLAY_ENABLED_BY_DEFAULT = true; - static final boolean MY_LOCATION_OVERLAY_COMPASS_ENABLED_BY_DEFAULT = true; - private static final int MY_LOCATION_OVERLAY_RANK = 2; + // ---- Residential Building Overlay + private static final String RESIDENTIAL_BUILDING_OVERLAY = "residentialBuildingOverlay"; + private BuildingNumOverlay residentialBuildingOverlay; + static final boolean RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT = true; + private static final int RESIDENTIAL_BUILDING_OVERLAY_RANK = 6; - static final String[] OTHER_OVERLAY_NAMES = { SCALE_BAR_OVERLAY, MY_LOCATION_OVERLAY, MY_LOCATION_OVERLAY_COMPASS }; + // ---- Non-Residential Building Overlay + private static final String NON_RESIDENTIAL_BUILDING_OVERLAY = "nonResidentialBuildingOverlay"; + private BuildingNumOverlay nonResidentialBuildingOverlay; + static final boolean NON_RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT = true; + private static final int NON_RESIDENTIAL_BUILDING_OVERLAY_RANK = 5; - // Other bits + static final String[] BUILDING_TYPES = { RESIDENTIAL_BUILDING_OVERLAY, NON_RESIDENTIAL_BUILDING_OVERLAY }; - // Uni-Link routes - static final String[] UNI_LINK_ROUTES = { "U1", "U1N", "U2", "U6", "U9" }; + // -- Bus Stop Overlays + static final String BUS_STOP_OVERLAYS = "busStopOverlays:"; - static final String[] PREFERENCES_GROUPS = { BUS_STOP_OVERLAYS, BUS_ROUTE_OVERLAYS, BUILDING_OVERLAYS, - SITE_OVERLAYS, OTHER_OVERLAYS }; + // ---- Uni-Link Bus Stop Overlay + private static final String UNI_LINK_BUS_STOP_OVERLAY = "uniLinkBusStopOverlay"; + private BusStopOverlay uniLinkBusStopOverlay; + static final boolean UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT = true; + private static final int UNI_LINK_BUS_STOP_OVERLAY_RANK = 3; - static final String[][] PREFERENCES_CHILDREN = { UNI_LINK_ROUTES, UNI_LINK_ROUTES, BUILDING_TYPES, SITE_NAMES, - OTHER_OVERLAY_NAMES }; + // ---- Non Uni-Link Bus Stop Overlay + private BusStopOverlay nonUniLinkBusStopOverlay; + private static final int NON_UNI_LINK_BUS_STOP_OVERLAY_RANK = 4; - /** - * The toast for this activity, storing the toast centrally allows it to be changed quickly, instead of a queue - * building up - */ - Toast activityToast; + // -- Site Overlays + static final String[] SITE_NAMES = { "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" }; - private MapActivity instance; + private static final String SITE_OVERLAYS = "siteOverlays:"; + private HashMap siteOverlays = new HashMap(21); + static final boolean SITE_OVERLAYS_ENABLED_BY_DEFAULT = false; + private static final int SITE_OVERLAYS_RANK = 8; - private static final String TAG = "SUM"; + // -- Route Overlays + private static final String BUS_ROUTE_OVERLAYS = "routeOverlays:"; + private HashMap busRouteOverlays = new HashMap(5); + static final boolean BUS_ROUTE_OVERLAYS_ENABLED_BY_DEFAULT = true; + private static final int BUS_ROUTE_OVERLAYS_RANK = 7; - @SuppressWarnings("unchecked") - public void onCreate(Bundle savedInstanceState) { - startTime = System.currentTimeMillis(); + // -- Other + static final String OTHER_OVERLAYS = "otherOverlay:"; - super.onCreate(savedInstanceState); + // ---- Scale Bar Overlay + private static final String SCALE_BAR_OVERLAY = "scaleBarOverlay"; + private ScaleBarOverlay scaleBarOverlay; + static final boolean SCALE_BAR_OVERLAY_ENABLED_BY_DEFAULT = true; + private static final int SCALE_BAR_OVERLAY_RANK = 1; - instance = this; + // ---- My Location Overlay + private static final String MY_LOCATION_OVERLAY = "myLocationOverlay"; + private static final String MY_LOCATION_OVERLAY_COMPASS = "myLocationOverlayCompass"; + private MyLocationOverlay myLocationOverlay; + static final boolean MY_LOCATION_OVERLAY_ENABLED_BY_DEFAULT = true; + static final boolean MY_LOCATION_OVERLAY_COMPASS_ENABLED_BY_DEFAULT = true; + private static final int MY_LOCATION_OVERLAY_RANK = 2; - Thread databaseThread = new Thread(this); // Start the database thread - databaseThread.start(); + static final String[] OTHER_OVERLAY_NAMES = { SCALE_BAR_OVERLAY, MY_LOCATION_OVERLAY, MY_LOCATION_OVERLAY_COMPASS }; - setContentView(R.layout.main); + // Other bits - Log.i(TAG, "Finished setting content view " + (System.currentTimeMillis() - startTime)); + // Uni-Link routes + static final String[] UNI_LINK_ROUTES = { "U1", "U1N", "U2", "U6", "U9" }; - mapView = (MapView) this.findViewById(R.id.mapview); - mapView.setTileSource(TileSourceFactory.MAPQUESTOSM); - mapView.setBuiltInZoomControls(true); - mapView.setMultiTouchControls(true); + static final String[] PREFERENCES_GROUPS = { BUS_STOP_OVERLAYS, BUS_ROUTE_OVERLAYS, BUILDING_OVERLAYS, + SITE_OVERLAYS, OTHER_OVERLAYS }; - pastOverlays = (HashMap) getLastNonConfigurationInstance(); + static final String[][] PREFERENCES_CHILDREN = { UNI_LINK_ROUTES, UNI_LINK_ROUTES, BUILDING_TYPES, SITE_NAMES, + OTHER_OVERLAY_NAMES }; - /* - * SensorManager mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); This code in the - * following constructor causes problems in some emulators, disable sensors to fix. + /** + * The toast for this activity, storing the toast centrally allows it to be + * changed quickly, instead of a queue building up */ - Log.i(TAG, "Starting creating myLocationOverlay"); - myLocationOverlay = new MyLocationOverlay(instance, mapView); - Log.i(TAG, "Finished creating myLocationOverlay"); + Toast activityToast; - while (databaseThread.isAlive()) { - Thread.yield(); - } + private MapActivity instance; - new Thread(new Runnable() { - public void run() { - Thread.currentThread().setPriority(Thread.MAX_PRIORITY); - try { - showOverlays(); - } catch (SQLException e) { - e.printStackTrace(); - } - } - }).start(); - - Log.i(TAG, "Started loading thread " + (System.currentTimeMillis() - startTime)); - - mapController = mapView.getController(); - mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext()); - - GeoPoint userLocation = null; + private static final String TAG = "SUM"; - Bundle extras = getIntent().getExtras(); - if (extras != null && extras.containsKey("poiPoint")) { - String poiPoint = getIntent().getExtras().getString("poiPoint"); - Log.i(TAG, "poiPoint " + poiPoint); + @SuppressWarnings("unchecked") + public void onCreate(Bundle savedInstanceState) { + startTime = System.currentTimeMillis(); - String[] bits = poiPoint.split(","); - - userLocation = new GeoPoint(Double.valueOf(bits[0]), Double.valueOf(bits[1])); - mapController.setZoom(20); - - } else { - userLocation = myLocationOverlay.getMyLocation(); - if (userLocation == null) { - userLocation = new GeoPoint(50935551, -1393488); // ECS - } - mapController.setZoom(15); - } + super.onCreate(savedInstanceState); - mapController.setCenter(userLocation); - - Log.i(TAG, "Finished onCreate " + (System.currentTimeMillis() - startTime)); - } - - public void onResume() { - super.onResume(); - Log.i(TAG, "OnResume"); - if (myLocationOverlay != null) { - final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); - final SharedPreferences activityPrefs = getPreferences(0); - - if (activityPrefs.getBoolean(OTHER_OVERLAYS + MY_LOCATION_OVERLAY_COMPASS, false)) { - myLocationOverlay.enableCompass(); - } else { - myLocationOverlay.disableCompass(); - } - - if (activityPrefs.getBoolean(OTHER_OVERLAYS + MY_LOCATION_OVERLAY, false) - && sharedPrefs.getBoolean(GPS_ENABLED, false)) { - myLocationOverlay.enableMyLocation(); - } else { - myLocationOverlay.disableMyLocation(); - } - - sharedPrefs.registerOnSharedPreferenceChangeListener(this); - activityPrefs.registerOnSharedPreferenceChangeListener(this); - } - } - - public void onPause() { - super.onResume(); - Log.i(TAG, "OnPause"); - if (myLocationOverlay != null) { - myLocationOverlay.disableMyLocation(); - myLocationOverlay.disableCompass(); - } - } + instance = this; - public void finish() { - super.finish(); - } + Thread databaseThread = new Thread(this); // Start the database thread + databaseThread.start(); - @Override - public Object onRetainNonConfigurationInstance() { - return overlays; - } + setContentView(R.layout.main); - public void run() { - Log.i(TAG, "Begining loading database " + (System.currentTimeMillis() - startTime)); + Log.i(TAG, "Finished setting content view " + (System.currentTimeMillis() - startTime)); - DatabaseHelper helper = getHelper(); - Log.i(TAG, "Got the helper at " + (System.currentTimeMillis() - startTime)); + mapView = (MapView) this.findViewById(R.id.mapview); + mapView.setTileSource(TileSourceFactory.MAPQUESTOSM); + mapView.setBuiltInZoomControls(true); + mapView.setMultiTouchControls(true); - boolean dbExist = helper.checkDataBase(); - Log.i(TAG, "Finished checking the database at " + (System.currentTimeMillis() - startTime)); + pastOverlays = (HashMap) getLastNonConfigurationInstance(); - if (dbExist) { - // do nothing - database already exist - } else { + /* + * SensorManager mSensorManager = (SensorManager) + * getSystemService(Context.SENSOR_SERVICE); This code in the following + * constructor causes problems in some emulators, disable sensors to + * fix. + */ + Log.i(TAG, "Starting creating myLocationOverlay"); + myLocationOverlay = new MyLocationOverlay(instance, mapView); + Log.i(TAG, "Finished creating myLocationOverlay"); - if (useBundledDatabase) { - try { - // By calling this method and empty database will be created into the default system path - // of your application so we are gonna be able to overwrite that database with our database. - Log.i(TAG, "GetReadableDatabase"); - helper.getWritableDatabase().close(); - - helper.copyDataBase(); - Log.i(TAG, "Out of copy database"); - } catch (IOException ioe) { - throw new Error("Unable to create database"); + while (databaseThread.isAlive()) { + Thread.yield(); } - } else { - Thread buildingThread = null; - Thread busStopThread = null; - Thread siteThread = null; - - Log.i(TAG, "Begining loading databases " + (System.currentTimeMillis() - startTime)); - try { - Dao buildingDao; - - buildingDao = helper.getBuildingDao(); - long buildingCount = buildingDao.countOf(); - Log.i(TAG, "Building count " + buildingCount); - if (buildingCount < 260) { - buildingThread = new Thread(new Runnable() { - public void run() { + new Thread(new Runnable() { + public void run() { + Thread.currentThread().setPriority(Thread.MAX_PRIORITY); try { - DataManager.loadBuildings(instance); - Log.i(TAG, "Loaded building database " + (System.currentTimeMillis() - startTime)); + showOverlays(); } catch (SQLException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); + e.printStackTrace(); } - } - }); - - buildingThread.start(); - } - - Dao busStopDao = helper.getBusStopDao(); - Dao busRouteDao = helper.getBusRouteDao(); - Dao routeStopsDao = helper.getRouteStopsDao(); - - long busStopCount = busStopDao.countOf(); - long busRouteCount = busRouteDao.countOf(); - long routeStopsCount = routeStopsDao.countOf(); - - Log.i(TAG, "BusStop count " + busStopCount); - Log.i(TAG, "BusRoute count " + busRouteCount); - Log.i(TAG, "RouteStops count " + routeStopsCount); - if (busStopCount < 217 || busRouteCount < 5 || routeStopsCount < 327) { - busStopThread = new Thread(new Runnable() { - public void run() { - try { - DataManager.loadBusData(instance, true); - Log.i(TAG, "Loaded bus stop database " + (System.currentTimeMillis() - startTime)); - } catch (SQLException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - } - }); - - busStopThread.start(); - } + } + }).start(); - Dao siteDao = helper.getSiteDao(); + Log.i(TAG, "Started loading thread " + (System.currentTimeMillis() - startTime)); - long siteCount = siteDao.countOf(); - Log.i(TAG, "Sites count " + siteCount); - if (siteCount < 21) { - siteThread = new Thread(new Runnable() { - public void run() { - try { - DataManager.loadSiteData(instance); - Log.i(TAG, "Loaded site database " + (System.currentTimeMillis() - startTime)); - } catch (SQLException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - } - }); + mapController = mapView.getController(); + mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext()); - siteThread.start(); - } + GeoPoint userLocation = null; - while (true) { - if ((buildingThread == null || !buildingThread.isAlive()) - && (busStopThread == null || !busStopThread.isAlive()) - && (siteThread == null || !siteThread.isAlive())) - break; + Bundle extras = getIntent().getExtras(); + if (extras != null && extras.containsKey("poiPoint")) { + String poiPoint = getIntent().getExtras().getString("poiPoint"); + Log.i(TAG, "poiPoint " + poiPoint); - Thread.yield(); - } + String[] bits = poiPoint.split(","); - Log.i(TAG, "Finished loading databases " + (System.currentTimeMillis() - startTime)); + userLocation = new GeoPoint(Double.valueOf(bits[0]), Double.valueOf(bits[1])); + mapController.setZoom(20); - } catch (SQLException e1) { - e1.printStackTrace(); + } else { + userLocation = myLocationOverlay.getMyLocation(); + if (userLocation == null) { + userLocation = new GeoPoint(50935551, -1393488); // ECS + } + mapController.setZoom(15); } - } - - } - Log.i(TAG, "Begining setting up the static values " + (System.currentTimeMillis() - startTime)); + mapController.setCenter(userLocation); - Log.i(TAG, "Finished the database thread " + (System.currentTimeMillis() - startTime)); - } + Log.i(TAG, "Finished onCreate " + (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"); - } else { - Log.i(TAG, "Unable to recover overlays"); + new Thread() { + public void run() { + DataManager.loadBuildingPhotos(instance); + } + }.start(); } - final SharedPreferences activityPrefs = getPreferences(0); + public void onResume() { + super.onResume(); + Log.i(TAG, "OnResume"); + if (myLocationOverlay != null) { + final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); + final SharedPreferences activityPrefs = getPreferences(0); - showUtilityOverlays(); + if (activityPrefs.getBoolean(OTHER_OVERLAYS + MY_LOCATION_OVERLAY_COMPASS, false)) { + myLocationOverlay.enableCompass(); + } else { + myLocationOverlay.disableCompass(); + } - showUniLinkBusStopOverlay(); + if (activityPrefs.getBoolean(OTHER_OVERLAYS + MY_LOCATION_OVERLAY, false) + && sharedPrefs.getBoolean(GPS_ENABLED, false)) { + myLocationOverlay.enableMyLocation(); + } else { + myLocationOverlay.disableMyLocation(); + } - showNonUniLinkBusStopOverlay(); + sharedPrefs.registerOnSharedPreferenceChangeListener(this); + activityPrefs.registerOnSharedPreferenceChangeListener(this); + } + } - if (activityPrefs.getBoolean(BUILDING_OVERLAYS + RESIDENTIAL_BUILDING_OVERLAY, - RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT) - || activityPrefs.getBoolean(BUILDING_OVERLAYS + NON_RESIDENTIAL_BUILDING_OVERLAY, - NON_RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT)) { - showBuildingOverlays(); + public void onPause() { + super.onResume(); + Log.i(TAG, "OnPause"); + if (myLocationOverlay != null) { + myLocationOverlay.disableMyLocation(); + myLocationOverlay.disableCompass(); + } } - Log.i(TAG, "Begining to show the route overlays at " + (System.currentTimeMillis() - startTime)); - for (BusRoute busRoute : getHelper().getBusRouteDao()) { - if (!busRoute.uniLink) { - Log.v(TAG, "Bus route " + busRoute.code + "(" + busRoute.id + ") is not unilink"); - continue; - } - Log.v(TAG, "Looking at showing " + busRoute.code + " route overlay"); - if (activityPrefs.getBoolean(BUS_ROUTE_OVERLAYS + busRoute.code, BUS_ROUTE_OVERLAYS_ENABLED_BY_DEFAULT)) { - showRouteOverlay(busRoute); - } + public void finish() { + super.finish(); } - Log.i(TAG, "Finished loading routes " + (System.currentTimeMillis() - startTime)); - - Log.i(TAG, "Begining to show the site overlays at " + (System.currentTimeMillis() - startTime)); - try { - for (Site site : getHelper().getSiteDao()) { - Log.v(TAG, "Looking at showing " + site.name + " site overlay"); - if (activityPrefs.getBoolean(SITE_OVERLAYS + site.name, SITE_OVERLAYS_ENABLED_BY_DEFAULT)) { - showSiteOverlay(site); - } - } - } catch (SQLException e) { - e.printStackTrace(); + + @Override + public Object onRetainNonConfigurationInstance() { + return overlays; } - Log.i(TAG, "Finished showing the site overlays " + (System.currentTimeMillis() - startTime)); - Log.i(TAG, "Finished showing all the overlays " + (System.currentTimeMillis() - startTime)); - } + public void run() { + Log.i(TAG, "Begining loading database " + (System.currentTimeMillis() - startTime)); - private void showUtilityOverlays() { - new Thread(new Runnable() { - public void run() { - Log.i(TAG, "Begining showing the utility overlays " + (System.currentTimeMillis() - startTime)); + DatabaseHelper helper = getHelper(); + Log.i(TAG, "Got the helper at " + (System.currentTimeMillis() - startTime)); - final SharedPreferences activityPrefs = getPreferences(0); - final OverlayRankComparator comparator = new OverlayRankComparator(getPreferences(0)); + boolean dbExist = helper.checkDataBase(); + Log.i(TAG, "Finished checking the database at " + (System.currentTimeMillis() - startTime)); - if (scaleBarOverlay != null) { - Log.v(TAG, "ScaleBarOverlay is already created"); + if (dbExist) { + // do nothing - database already exist } else { - if (pastOverlays != null - && (scaleBarOverlay = (ScaleBarOverlay) pastOverlays.get(SCALE_BAR_OVERLAY)) != null) { - Log.i(TAG, "Finished restoring utility overlays " + (System.currentTimeMillis() - startTime)); - } else { - scaleBarOverlay = new ScaleBarOverlay(instance); - } - overlays.put(SCALE_BAR_OVERLAY, scaleBarOverlay); + if (useBundledDatabase) { + try { + // By calling this method and empty database will be created + // into the default system path + // of your application so we are gonna be able to overwrite + // that database with our database. + Log.i(TAG, "GetReadableDatabase"); + helper.getWritableDatabase().close(); + + helper.copyDataBase(); + Log.i(TAG, "Out of copy database"); + } catch (IOException ioe) { + throw new Error("Unable to create database"); + } + } else { + Thread buildingThread = null; + Thread busStopThread = null; + Thread siteThread = null; - synchronized (mapView.getOverlays()) { - mapView.getOverlays().add(scaleBarOverlay); - mapView.getOverlays().add(myLocationOverlay); - Collections.sort(mapView.getOverlays(), comparator); - } + Log.i(TAG, "Begining loading databases " + (System.currentTimeMillis() - startTime)); + try { + Dao buildingDao; + + buildingDao = helper.getBuildingDao(); + + long buildingCount = buildingDao.countOf(); + Log.i(TAG, "Building count " + buildingCount); + if (buildingCount < 260) { + buildingThread = new Thread(new Runnable() { + public void run() { + try { + DataManager.loadBuildings(instance); + Log.i(TAG, "Loaded building database " + (System.currentTimeMillis() - startTime)); + } catch (SQLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + }); + + buildingThread.start(); + } + + Dao busStopDao = helper.getBusStopDao(); + Dao busRouteDao = helper.getBusRouteDao(); + Dao routeStopsDao = helper.getRouteStopsDao(); + + long busStopCount = busStopDao.countOf(); + long busRouteCount = busRouteDao.countOf(); + long routeStopsCount = routeStopsDao.countOf(); + + Log.i(TAG, "BusStop count " + busStopCount); + Log.i(TAG, "BusRoute count " + busRouteCount); + Log.i(TAG, "RouteStops count " + routeStopsCount); + if (busStopCount < 217 || busRouteCount < 5 || routeStopsCount < 327) { + busStopThread = new Thread(new Runnable() { + public void run() { + try { + DataManager.loadBusData(instance, true); + Log.i(TAG, "Loaded bus stop database " + (System.currentTimeMillis() - startTime)); + } catch (SQLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + }); + + busStopThread.start(); + } + + Dao siteDao = helper.getSiteDao(); + + long siteCount = siteDao.countOf(); + Log.i(TAG, "Sites count " + siteCount); + if (siteCount < 21) { + siteThread = new Thread(new Runnable() { + public void run() { + try { + DataManager.loadSiteData(instance); + Log.i(TAG, "Loaded site database " + (System.currentTimeMillis() - startTime)); + } catch (SQLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + }); + + siteThread.start(); + } + + while (true) { + if ((buildingThread == null || !buildingThread.isAlive()) + && (busStopThread == null || !busStopThread.isAlive()) + && (siteThread == null || !siteThread.isAlive())) + break; + + Thread.yield(); + } + + Log.i(TAG, "Finished loading databases " + (System.currentTimeMillis() - startTime)); + + } catch (SQLException e1) { + e1.printStackTrace(); + } + } } - scaleBarOverlay.setEnabled(activityPrefs.getBoolean(OTHER_OVERLAYS + SCALE_BAR_OVERLAY, - SCALE_BAR_OVERLAY_ENABLED_BY_DEFAULT)); - - mapView.postInvalidate(); - - Log.i(TAG, "Finished showing utility overlays " + (System.currentTimeMillis() - startTime)); - } - }).start(); - } + Log.i(TAG, "Begining setting up the static values " + (System.currentTimeMillis() - startTime)); - 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)); + Log.i(TAG, "Finished the database thread " + (System.currentTimeMillis() - startTime)); + } - final SharedPreferences activityPrefs = getPreferences(0); - final OverlayRankComparator comparator = new OverlayRankComparator(getPreferences(0)); + private void showOverlays() throws SQLException { + Log.i(TAG, "Began showing overlays at " + (System.currentTimeMillis() - startTime)); - PathOverlay routeOverlay; - if ((routeOverlay = busRouteOverlays.get(route)) != null) { - Log.v(TAG, route.code + " route overlay already existed"); + if (pastOverlays != null) { + Log.i(TAG, "Able to recover some/all of the overlays from a previous activity"); } else { - if (pastOverlays != null - && (routeOverlay = (PathOverlay) pastOverlays.get(BUS_ROUTE_OVERLAYS + route.code)) != null) { - Log.v(TAG, "Restored " + route.code + " route overlay"); - if (route.code.equals("U1")) { - PathOverlay routeOverlayU1E = (PathOverlay) pastOverlays.get(BUS_ROUTE_OVERLAYS + "U1E"); - overlays.put(BUS_ROUTE_OVERLAYS + "U1E", routeOverlayU1E); - } - } else { - InputStream resource = null; - int colour = 0; - if (route.code.equals("U1")) { - resource = getResources().openRawResource(R.raw.u1); - colour = U1; - - // TODO Is this a route like U1N or, something else, this hack works somewhat for now? - PathOverlay routeOverlayU1E = DataManager.getRoutePath( - getResources().openRawResource(R.raw.u1e), colour, mResourceProxy); - routeOverlayU1E.getPaint().setAntiAlias(true); - routeOverlayU1E.getPaint().setAlpha(145); - routeOverlayU1E.getPaint().setStrokeWidth(12); - routeOverlayU1E.getPaint().setPathEffect(new DashPathEffect(new float[] { 20, 16 }, 0)); - routeOverlayU1E.setEnabled(activityPrefs.getBoolean("Bus Routes:" + route.code, true)); - - busRouteOverlays.put(new BusRoute(1000, "U1E", "U1E Route Label", true), routeOverlayU1E); - overlays.put(BUS_ROUTE_OVERLAYS + route.code + "E", routeOverlayU1E); - } else if (route.code.equals("U1N")) { - resource = getResources().openRawResource(R.raw.u1n); - colour = U1N; - } else if (route.code.equals("U2")) { - resource = getResources().openRawResource(R.raw.u2); - colour = U2; - } else if (route.code.equals("U6")) { - resource = getResources().openRawResource(R.raw.u6); - colour = U6; - } else if (route.code.equals("U9")) { - resource = getResources().openRawResource(R.raw.u9); - colour = U9; - } else { - Log.w(TAG, "Wierd route " + route); - } + Log.i(TAG, "Unable to recover overlays"); + } - routeOverlay = DataManager.getRoutePath(resource, colour, mResourceProxy); + final SharedPreferences activityPrefs = getPreferences(0); - Log.v(TAG, "Path overlay has " + routeOverlay.getNumberOfPoints() + " points"); + showUtilityOverlays(); - routeOverlay.getPaint().setAntiAlias(true); - routeOverlay.getPaint().setAlpha(145); - routeOverlay.getPaint().setStrokeWidth(12); - } + showUniLinkBusStopOverlay(); - busRouteOverlays.put(route, routeOverlay); - overlays.put(BUS_ROUTE_OVERLAYS + route.code, routeOverlay); + showNonUniLinkBusStopOverlay(); - synchronized (mapView.getOverlays()) { - mapView.getOverlays().add(routeOverlay); - Collections.sort(mapView.getOverlays(), comparator); - } + if (activityPrefs.getBoolean(BUILDING_OVERLAYS + RESIDENTIAL_BUILDING_OVERLAY, + RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT) + || activityPrefs.getBoolean(BUILDING_OVERLAYS + NON_RESIDENTIAL_BUILDING_OVERLAY, + NON_RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT)) { + showBuildingOverlays(); + } + Log.i(TAG, "Begining to show the route overlays at " + (System.currentTimeMillis() - startTime)); + for (BusRoute busRoute : getHelper().getBusRouteDao()) { + if (!busRoute.uniLink) { + Log.v(TAG, "Bus route " + busRoute.code + "(" + busRoute.id + ") is not unilink"); + continue; + } + Log.v(TAG, "Looking at showing " + busRoute.code + " route overlay"); + if (activityPrefs.getBoolean(BUS_ROUTE_OVERLAYS + busRoute.code, BUS_ROUTE_OVERLAYS_ENABLED_BY_DEFAULT)) { + showRouteOverlay(busRoute); + } } + Log.i(TAG, "Finished loading routes " + (System.currentTimeMillis() - startTime)); - routeOverlay.setEnabled(activityPrefs.getBoolean(BUS_ROUTE_OVERLAYS + route.code, - BUS_ROUTE_OVERLAYS_ENABLED_BY_DEFAULT)); - if (route.code.equals("U1")) { - overlays.get(BUS_ROUTE_OVERLAYS + "U1E").setEnabled( - activityPrefs.getBoolean(BUS_ROUTE_OVERLAYS + "U1", BUS_ROUTE_OVERLAYS_ENABLED_BY_DEFAULT)); + Log.i(TAG, "Begining to show the site overlays at " + (System.currentTimeMillis() - startTime)); + try { + for (Site site : getHelper().getSiteDao()) { + Log.v(TAG, "Looking at showing " + site.name + " site overlay"); + if (activityPrefs.getBoolean(SITE_OVERLAYS + site.name, SITE_OVERLAYS_ENABLED_BY_DEFAULT)) { + showSiteOverlay(site); + } + } + } catch (SQLException e) { + e.printStackTrace(); } + Log.i(TAG, "Finished showing the site overlays " + (System.currentTimeMillis() - startTime)); - mapView.postInvalidate(); + Log.i(TAG, "Finished showing all the overlays " + (System.currentTimeMillis() - startTime)); + } - Log.i(TAG, "Finished showing route " + route.code + " overlay at " - + (System.currentTimeMillis() - startTime)); - } - }).start(); - } + private void showUtilityOverlays() { + new Thread(new Runnable() { + public void run() { + Log.i(TAG, "Begining showing the utility overlays " + (System.currentTimeMillis() - startTime)); - private void showSiteOverlay(final Site site) { + final SharedPreferences activityPrefs = getPreferences(0); + final OverlayRankComparator comparator = new OverlayRankComparator(getPreferences(0)); - new Thread(new Runnable() { - public void run() { - Log.i(TAG, "Begining showing site " + site.name + " overlay at " - + (System.currentTimeMillis() - startTime)); + if (scaleBarOverlay != null) { + Log.v(TAG, "ScaleBarOverlay is already created"); + } else { + if (pastOverlays != null + && (scaleBarOverlay = (ScaleBarOverlay) pastOverlays.get(SCALE_BAR_OVERLAY)) != null) { + Log.i(TAG, "Finished restoring utility overlays " + (System.currentTimeMillis() - startTime)); + } else { + scaleBarOverlay = new ScaleBarOverlay(instance); + } - final SharedPreferences activityPrefs = getPreferences(0); - final OverlayRankComparator comparator = new OverlayRankComparator(getPreferences(0)); + overlays.put(SCALE_BAR_OVERLAY, scaleBarOverlay); - PathOverlay siteOverlay; - if ((siteOverlay = siteOverlays.get(site)) != null) { + synchronized (mapView.getOverlays()) { + mapView.getOverlays().add(scaleBarOverlay); + mapView.getOverlays().add(myLocationOverlay); + Collections.sort(mapView.getOverlays(), comparator); + } - } else { - if (pastOverlays != null - && (siteOverlay = (PathOverlay) pastOverlays.get(SITE_OVERLAYS + site.name)) != null) { - Log.i(TAG, "Restored " + site.name + " site overlay"); - } else { - - 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++) { - siteOverlay.addPoint(site.outline.points[i]); - } - siteOverlay.addPoint(site.outline.points[0]); - - } + } - siteOverlays.put(site, siteOverlay); - overlays.put(SITE_OVERLAYS + site.name, siteOverlay); + scaleBarOverlay.setEnabled(activityPrefs.getBoolean(OTHER_OVERLAYS + SCALE_BAR_OVERLAY, + SCALE_BAR_OVERLAY_ENABLED_BY_DEFAULT)); - Log.v(TAG, "Applyed the site overlay, now sorting them"); + mapView.postInvalidate(); - synchronized (mapView.getOverlays()) { - mapView.getOverlays().add(siteOverlay); - Collections.sort(mapView.getOverlays(), comparator); - } - } + Log.i(TAG, "Finished showing utility overlays " + (System.currentTimeMillis() - startTime)); + } + }).start(); + } - siteOverlay.setEnabled(activityPrefs.getBoolean(SITE_OVERLAYS + site.name, - SITE_OVERLAYS_ENABLED_BY_DEFAULT)); + 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)); - mapView.postInvalidate(); + final SharedPreferences activityPrefs = getPreferences(0); + final OverlayRankComparator comparator = new OverlayRankComparator(getPreferences(0)); - Log.i(TAG, "Finished showing site " + site.name + " overlay at " - + (System.currentTimeMillis() - startTime)); - } - }).start(); - } + PathOverlay routeOverlay; + if ((routeOverlay = busRouteOverlays.get(route)) != null) { + Log.v(TAG, route.code + " route overlay already existed"); + } else { + if (pastOverlays != null + && (routeOverlay = (PathOverlay) pastOverlays.get(BUS_ROUTE_OVERLAYS + route.code)) != null) { + Log.v(TAG, "Restored " + route.code + " route overlay"); + if (route.code.equals("U1")) { + PathOverlay routeOverlayU1E = (PathOverlay) pastOverlays.get(BUS_ROUTE_OVERLAYS + "U1E"); + overlays.put(BUS_ROUTE_OVERLAYS + "U1E", routeOverlayU1E); + } + } else { + InputStream resource = null; + int colour = 0; + if (route.code.equals("U1")) { + resource = getResources().openRawResource(R.raw.u1); + colour = U1; + + // TODO Is this a route like U1N or, something else, + // this hack works somewhat for now? + PathOverlay routeOverlayU1E = DataManager.getRoutePath( + getResources().openRawResource(R.raw.u1e), colour, mResourceProxy); + routeOverlayU1E.getPaint().setAntiAlias(true); + routeOverlayU1E.getPaint().setAlpha(145); + routeOverlayU1E.getPaint().setStrokeWidth(12); + routeOverlayU1E.getPaint().setPathEffect(new DashPathEffect(new float[] { 20, 16 }, 0)); + routeOverlayU1E.setEnabled(activityPrefs.getBoolean("Bus Routes:" + route.code, true)); + + busRouteOverlays.put(new BusRoute(1000, "U1E", "U1E Route Label", true), routeOverlayU1E); + overlays.put(BUS_ROUTE_OVERLAYS + route.code + "E", routeOverlayU1E); + } else if (route.code.equals("U1N")) { + resource = getResources().openRawResource(R.raw.u1n); + colour = U1N; + } else if (route.code.equals("U2")) { + resource = getResources().openRawResource(R.raw.u2); + colour = U2; + } else if (route.code.equals("U6")) { + resource = getResources().openRawResource(R.raw.u6); + colour = U6; + } else if (route.code.equals("U9")) { + resource = getResources().openRawResource(R.raw.u9); + colour = U9; + } else { + Log.w(TAG, "Wierd route " + route); + } + + routeOverlay = DataManager.getRoutePath(resource, colour, mResourceProxy); + + Log.v(TAG, "Path overlay has " + routeOverlay.getNumberOfPoints() + " points"); + + routeOverlay.getPaint().setAntiAlias(true); + routeOverlay.getPaint().setAlpha(145); + routeOverlay.getPaint().setStrokeWidth(12); + } + + busRouteOverlays.put(route, routeOverlay); + overlays.put(BUS_ROUTE_OVERLAYS + route.code, routeOverlay); + + synchronized (mapView.getOverlays()) { + mapView.getOverlays().add(routeOverlay); + Collections.sort(mapView.getOverlays(), comparator); + } - private void showBuildingOverlays() { - new Thread(new Runnable() { - public void run() { - Log.i(TAG, "Begining showing building overlays at " + (System.currentTimeMillis() - startTime)); + } - final SharedPreferences activityPrefs = getPreferences(0); - final OverlayRankComparator comparator = new OverlayRankComparator(getPreferences(0)); + routeOverlay.setEnabled(activityPrefs.getBoolean(BUS_ROUTE_OVERLAYS + route.code, + BUS_ROUTE_OVERLAYS_ENABLED_BY_DEFAULT)); + if (route.code.equals("U1")) { + overlays.get(BUS_ROUTE_OVERLAYS + "U1E").setEnabled( + activityPrefs.getBoolean(BUS_ROUTE_OVERLAYS + "U1", BUS_ROUTE_OVERLAYS_ENABLED_BY_DEFAULT)); + } - if (residentialBuildingOverlay != null) { + mapView.postInvalidate(); - } else { - if (pastOverlays != null - && (residentialBuildingOverlay = (BuildingNumOverlay) pastOverlays - .get(RESIDENTIAL_BUILDING_OVERLAY)) != null) { - nonResidentialBuildingOverlay = (BuildingNumOverlay) pastOverlays - .get(NON_RESIDENTIAL_BUILDING_OVERLAY); - - Log.i(TAG, "Restored building overlays"); - } else { - try { + Log.i(TAG, "Finished showing route " + route.code + " overlay at " + + (System.currentTimeMillis() - startTime)); + } + }).start(); + } - Log.v(TAG, "Begining the creation of the building overlays"); + private void showSiteOverlay(final Site site) { - ArrayList residentialBuildings = new ArrayList(); - ArrayList nonResidentialBuildings = new ArrayList(); + new Thread(new Runnable() { + public void run() { + Log.i(TAG, "Begining showing site " + site.name + " overlay at " + + (System.currentTimeMillis() - startTime)); - Dao buildingDao; + final SharedPreferences activityPrefs = getPreferences(0); + final OverlayRankComparator comparator = new OverlayRankComparator(getPreferences(0)); - buildingDao = getHelper().getBuildingDao(); + PathOverlay siteOverlay; + if ((siteOverlay = siteOverlays.get(site)) != null) { - for (Building building : buildingDao) { - if (building.residential == true) { - if (building.favourite) { - residentialBuildings.add(building); - } else { - residentialBuildings.add(0, building); - } } else { - if (building.favourite) { - nonResidentialBuildings.add(building); - } else { - nonResidentialBuildings.add(0, building); - } + if (pastOverlays != null + && (siteOverlay = (PathOverlay) pastOverlays.get(SITE_OVERLAYS + site.name)) != null) { + Log.i(TAG, "Restored " + site.name + " site overlay"); + } else { + + 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++) { + siteOverlay.addPoint(site.outline.points[i]); + } + siteOverlay.addPoint(site.outline.points[0]); + + } + + siteOverlays.put(site, siteOverlay); + overlays.put(SITE_OVERLAYS + site.name, siteOverlay); + + Log.v(TAG, "Applyed the site overlay, now sorting them"); + + synchronized (mapView.getOverlays()) { + mapView.getOverlays().add(siteOverlay); + Collections.sort(mapView.getOverlays(), comparator); + } } - } - residentialBuildingOverlay = new BuildingNumOverlay(instance, residentialBuildings); - nonResidentialBuildingOverlay = new BuildingNumOverlay(instance, nonResidentialBuildings); + siteOverlay.setEnabled(activityPrefs.getBoolean(SITE_OVERLAYS + site.name, + SITE_OVERLAYS_ENABLED_BY_DEFAULT)); - Log.v(TAG, "Applyed the site overlay, now sorting them"); + mapView.postInvalidate(); - } catch (SQLException e) { - e.printStackTrace(); + Log.i(TAG, "Finished showing site " + site.name + " overlay at " + + (System.currentTimeMillis() - startTime)); } - } + }).start(); + } - overlays.put(RESIDENTIAL_BUILDING_OVERLAY, residentialBuildingOverlay); - overlays.put(NON_RESIDENTIAL_BUILDING_OVERLAY, nonResidentialBuildingOverlay); + private void showBuildingOverlays() { + new Thread(new Runnable() { + public void run() { + Log.i(TAG, "Begining showing building overlays at " + (System.currentTimeMillis() - startTime)); - synchronized (mapView.getOverlays()) { - mapView.getOverlays().add(residentialBuildingOverlay); - mapView.getOverlays().add(nonResidentialBuildingOverlay); - Collections.sort(mapView.getOverlays(), comparator); - } - } + final SharedPreferences activityPrefs = getPreferences(0); + final OverlayRankComparator comparator = new OverlayRankComparator(getPreferences(0)); - residentialBuildingOverlay.setEnabled(activityPrefs.getBoolean(BUILDING_OVERLAYS - + RESIDENTIAL_BUILDING_OVERLAY, RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT)); - nonResidentialBuildingOverlay.setEnabled(activityPrefs.getBoolean(BUILDING_OVERLAYS - + NON_RESIDENTIAL_BUILDING_OVERLAY, NON_RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT)); + if (residentialBuildingOverlay != null) { - mapView.postInvalidate(); + } else { + if (pastOverlays != null + && (residentialBuildingOverlay = (BuildingNumOverlay) pastOverlays + .get(RESIDENTIAL_BUILDING_OVERLAY)) != null) { + nonResidentialBuildingOverlay = (BuildingNumOverlay) pastOverlays + .get(NON_RESIDENTIAL_BUILDING_OVERLAY); + + Log.i(TAG, "Restored building overlays"); + } else { + try { + + Log.v(TAG, "Begining the creation of the building overlays"); + + ArrayList residentialBuildings = new ArrayList(); + ArrayList nonResidentialBuildings = new ArrayList(); + + Dao buildingDao; + + buildingDao = getHelper().getBuildingDao(); + + for (Building building : buildingDao) { + if (building.residential == true) { + if (building.favourite) { + residentialBuildings.add(building); + } else { + residentialBuildings.add(0, building); + } + } else { + if (building.favourite) { + nonResidentialBuildings.add(building); + } else { + nonResidentialBuildings.add(0, building); + } + } + } + + residentialBuildingOverlay = new BuildingNumOverlay(instance, residentialBuildings); + nonResidentialBuildingOverlay = new BuildingNumOverlay(instance, nonResidentialBuildings); + + Log.v(TAG, "Applyed the site overlay, now sorting them"); + + } catch (SQLException e) { + e.printStackTrace(); + } + } + + overlays.put(RESIDENTIAL_BUILDING_OVERLAY, residentialBuildingOverlay); + overlays.put(NON_RESIDENTIAL_BUILDING_OVERLAY, nonResidentialBuildingOverlay); + + synchronized (mapView.getOverlays()) { + mapView.getOverlays().add(residentialBuildingOverlay); + mapView.getOverlays().add(nonResidentialBuildingOverlay); + Collections.sort(mapView.getOverlays(), comparator); + } + } - Log.i(TAG, "Finished showing building overlays at " + (System.currentTimeMillis() - startTime)); - } - }).start(); - } + residentialBuildingOverlay.setEnabled(activityPrefs.getBoolean(BUILDING_OVERLAYS + + RESIDENTIAL_BUILDING_OVERLAY, RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT)); + nonResidentialBuildingOverlay.setEnabled(activityPrefs.getBoolean(BUILDING_OVERLAYS + + NON_RESIDENTIAL_BUILDING_OVERLAY, NON_RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT)); - private void showUniLinkBusStopOverlay() { - new Thread(new Runnable() { - public void run() { - Log.i(TAG, "Begining showing bus stop overlays at " + (System.currentTimeMillis() - startTime)); + mapView.postInvalidate(); - final SharedPreferences activityPrefs = getPreferences(0); - final OverlayRankComparator comparator = new OverlayRankComparator(getPreferences(0)); - - if (uniLinkBusStopOverlay != null) { - if (!activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)) { - Log.i(TAG, "Uni-Link bus stop overlay not needed"); - overlays.remove(UNI_LINK_BUS_STOP_OVERLAY); - - synchronized (mapView.getOverlays()) { - mapView.getOverlays().remove(uniLinkBusStopOverlay); - Collections.sort(mapView.getOverlays(), comparator); + Log.i(TAG, "Finished showing building overlays at " + (System.currentTimeMillis() - startTime)); } + }).start(); + } - uniLinkBusStopOverlay = null; - } else { - uniLinkBusStopOverlay.setRoutes(0, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(1, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(2, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(3, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(4, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - } - } else { - if (activityPrefs - .getBoolean(BUS_STOP_OVERLAYS + "U1", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)) { - if (pastOverlays != null - && (uniLinkBusStopOverlay = (BusStopOverlay) pastOverlays - .get(UNI_LINK_BUS_STOP_OVERLAY)) != null) { - Log.i(TAG, "Restored Uni-Link bus stop overlay"); - } else { - - try { - List busStops; - Log.v(TAG, "Begin fetching BusStops at " + (System.currentTimeMillis() - startTime)); - - busStops = getHelper().getBusStopDao().queryForEq(BusStop.UNI_LINK_FIELD_NAME, true); + private void showUniLinkBusStopOverlay() { + new Thread(new Runnable() { + public void run() { + Log.i(TAG, "Begining showing bus stop overlays at " + (System.currentTimeMillis() - startTime)); + + final SharedPreferences activityPrefs = getPreferences(0); + final OverlayRankComparator comparator = new OverlayRankComparator(getPreferences(0)); + + if (uniLinkBusStopOverlay != null) { + if (!activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)) { + Log.i(TAG, "Uni-Link bus stop overlay not needed"); + overlays.remove(UNI_LINK_BUS_STOP_OVERLAY); + + synchronized (mapView.getOverlays()) { + mapView.getOverlays().remove(uniLinkBusStopOverlay); + Collections.sort(mapView.getOverlays(), comparator); + } + + uniLinkBusStopOverlay = null; + } else { + uniLinkBusStopOverlay.setRoutes(0, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(1, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(2, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(3, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(4, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + } + } else { + if (activityPrefs + .getBoolean(BUS_STOP_OVERLAYS + "U1", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)) { + if (pastOverlays != null + && (uniLinkBusStopOverlay = (BusStopOverlay) pastOverlays + .get(UNI_LINK_BUS_STOP_OVERLAY)) != null) { + Log.i(TAG, "Restored Uni-Link bus stop overlay"); + } else { + + try { + List busStops; + Log.v(TAG, "Begin fetching BusStops at " + (System.currentTimeMillis() - startTime)); + + busStops = getHelper().getBusStopDao().queryForEq(BusStop.UNI_LINK_FIELD_NAME, true); + + Log.v(TAG, "Finished fetching BusStops at " + (System.currentTimeMillis() - startTime)); + + uniLinkBusStopOverlay = new BusStopOverlay(instance, busStops); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + uniLinkBusStopOverlay.setRoutes(0, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(1, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(2, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(3, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(4, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + + overlays.put(UNI_LINK_BUS_STOP_OVERLAY, uniLinkBusStopOverlay); + + Log.v(TAG, "Applyed the site overlay, now sorting them"); + + synchronized (mapView.getOverlays()) { + mapView.getOverlays().add(uniLinkBusStopOverlay); + Collections.sort(mapView.getOverlays(), comparator); + } + } + } - Log.v(TAG, "Finished fetching BusStops at " + (System.currentTimeMillis() - startTime)); + mapView.postInvalidate(); - uniLinkBusStopOverlay = new BusStopOverlay(instance, busStops); - } catch (SQLException e) { - e.printStackTrace(); - } + Log.i(TAG, "Finished showing bus stop overlays at " + (System.currentTimeMillis() - startTime)); } + }).start(); + } - uniLinkBusStopOverlay.setRoutes(0, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(1, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(2, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(3, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(4, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - - overlays.put(UNI_LINK_BUS_STOP_OVERLAY, uniLinkBusStopOverlay); + private void showNonUniLinkBusStopOverlay() { + new Thread(new Runnable() { + public void run() { + Log.i(TAG, "Begining showing non uni link bus stop overlays at " + + (System.currentTimeMillis() - startTime)); + + // final SharedPreferences activityPrefs = getPreferences(0); + final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(instance); + final OverlayRankComparator comparator = new OverlayRankComparator(getPreferences(0)); + + if (nonUniLinkBusStopOverlay != null) { + nonUniLinkBusStopOverlay.setEnabled(sharedPrefs.getBoolean(NON_UNI_LINK_BUS_STOPS_OVERLAY, + NON_UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + + Log.i(TAG, + "nonUniLinkBusStopOverlay enabled ? " + + sharedPrefs.getBoolean(NON_UNI_LINK_BUS_STOPS_OVERLAY, + NON_UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + } else if (sharedPrefs.getBoolean(NON_UNI_LINK_BUS_STOPS_OVERLAY, + NON_UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)) { + if (pastOverlays != null + && (nonUniLinkBusStopOverlay = (BusStopOverlay) pastOverlays + .get(NON_UNI_LINK_BUS_STOPS_OVERLAY)) != null) { + Log.i(TAG, "Restored non Uni-Link bus stop overlays"); + } else { + try { + List busStops; + 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.size() + " non Uni-Link BusStops at " + + (System.currentTimeMillis() - startTime)); + + nonUniLinkBusStopOverlay = new BusStopOverlay(instance, busStops); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + overlays.put(NON_UNI_LINK_BUS_STOPS_OVERLAY, nonUniLinkBusStopOverlay); + + Log.v(TAG, "Applyed the site overlay, now sorting them"); + + synchronized (mapView.getOverlays()) { + mapView.getOverlays().add(nonUniLinkBusStopOverlay); + Collections.sort(mapView.getOverlays(), comparator); + } + } - Log.v(TAG, "Applyed the site overlay, now sorting them"); + if (nonUniLinkBusStopOverlay != null) { - synchronized (mapView.getOverlays()) { - mapView.getOverlays().add(uniLinkBusStopOverlay); - Collections.sort(mapView.getOverlays(), comparator); - } - } - } + } - mapView.postInvalidate(); - - Log.i(TAG, "Finished showing bus stop overlays at " + (System.currentTimeMillis() - startTime)); - } - }).start(); - } - - private void showNonUniLinkBusStopOverlay() { - new Thread(new Runnable() { - public void run() { - Log.i(TAG, "Begining showing non uni link bus stop overlays at " - + (System.currentTimeMillis() - startTime)); - - // final SharedPreferences activityPrefs = getPreferences(0); - final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(instance); - final OverlayRankComparator comparator = new OverlayRankComparator(getPreferences(0)); - - if (nonUniLinkBusStopOverlay != null) { - nonUniLinkBusStopOverlay.setEnabled(sharedPrefs.getBoolean(NON_UNI_LINK_BUS_STOPS_OVERLAY, - NON_UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - - Log.i(TAG, - "nonUniLinkBusStopOverlay enabled ? " - + sharedPrefs.getBoolean(NON_UNI_LINK_BUS_STOPS_OVERLAY, - NON_UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - } else if (sharedPrefs.getBoolean(NON_UNI_LINK_BUS_STOPS_OVERLAY, - NON_UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)) { - if (pastOverlays != null - && (nonUniLinkBusStopOverlay = (BusStopOverlay) pastOverlays - .get(NON_UNI_LINK_BUS_STOPS_OVERLAY)) != null) { - Log.i(TAG, "Restored non Uni-Link bus stop overlays"); - } else { - try { - List busStops; - Log.v(TAG, "Begin fetching non Uni-Link BusStops at " - + (System.currentTimeMillis() - startTime)); + mapView.postInvalidate(); - busStops = getHelper().getBusStopDao().queryForEq(BusStop.UNI_LINK_FIELD_NAME, false); + Log.i(TAG, "Finished showing non Uni-Link bus stop overlays at " + + (System.currentTimeMillis() - startTime)); + } + }).start(); + } - Log.v(TAG, - "Finished fetching " + busStops.size() + " non Uni-Link BusStops at " - + (System.currentTimeMillis() - startTime)); + @Override + public boolean onCreateOptionsMenu(Menu menu) { + MenuInflater inflater = getMenuInflater(); + inflater.inflate(R.menu.map_menu, menu); + return true; + } - nonUniLinkBusStopOverlay = new BusStopOverlay(instance, busStops); - } catch (SQLException e) { - e.printStackTrace(); + @Override + public boolean onOptionsItemSelected(MenuItem item) { + // Handle item selection + switch (item.getItemId()) { + case R.id.menu_find: + Intent i = new Intent(MapActivity.this, FindActivity.class); + startActivityForResult(i, 0); + return true; + case R.id.menu_preferences: + Intent settingsActivity = new Intent(getBaseContext(), PreferencesActivity.class); + startActivity(settingsActivity); + return true; + case R.id.menu_find_my_location: + final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); + if (sharedPrefs.getBoolean("GPSEnabled", false)) { + GeoPoint userLocation = myLocationOverlay.getMyLocation(); + if (userLocation != null) { + Log.i(TAG, "Found user location, scrolling to " + userLocation); + mapController.animateTo(userLocation); + myLocationOverlay.enableFollowLocation(); + } + } else { + DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + switch (which) { + case DialogInterface.BUTTON_POSITIVE: + Editor editor = sharedPrefs.edit(); + editor.putBoolean("GPSEnabled", true); + editor.commit(); + break; + + case DialogInterface.BUTTON_NEGATIVE: + // No button clicked + break; + } + } + }; + + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setMessage("GPS is not enabled, do you wish to enable it?") + .setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener) + .show(); } - } - overlays.put(NON_UNI_LINK_BUS_STOPS_OVERLAY, nonUniLinkBusStopOverlay); + return true; + case R.id.menu_view: + Log.i(TAG, "Showing view dialog"); + showDialog(VIEW_DIALOG_ID); + return false; + case R.id.menu_favourites: + Log.i(TAG, "Showing favourite dialog"); + + showDialog(FAVOURITE_DIALOG_ID); + if (favDialog == null) { + Log.e(TAG, "Very wierd, just tried to launch the favourite's dialog, but its null?"); + return false; + } - Log.v(TAG, "Applyed the site overlay, now sorting them"); + refreshFavouriteDialog(); - synchronized (mapView.getOverlays()) { - mapView.getOverlays().add(nonUniLinkBusStopOverlay); - Collections.sort(mapView.getOverlays(), comparator); - } + return false; + case R.id.menu_about: + Intent aboutIntent = new Intent(MapActivity.this, AboutActivity.class); + startActivityForResult(aboutIntent, 0); + return true; + default: + Log.e(TAG, "No known menu option selected"); + return super.onOptionsItemSelected(item); } + } - if (nonUniLinkBusStopOverlay != null) { + private void refreshFavouriteDialog() { + ArrayList newFavouriteItems = new ArrayList(); + try { + 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)); + } catch (SQLException e) { + e.printStackTrace(); } - mapView.postInvalidate(); - - Log.i(TAG, "Finished showing non Uni-Link bus stop overlays at " - + (System.currentTimeMillis() - startTime)); - } - }).start(); - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - MenuInflater inflater = getMenuInflater(); - inflater.inflate(R.menu.map_menu, menu); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - // Handle item selection - switch (item.getItemId()) { - case R.id.menu_find: - Intent i = new Intent(MapActivity.this, FindActivity.class); - startActivityForResult(i, 0); - return true; - case R.id.menu_preferences: - Intent settingsActivity = new Intent(getBaseContext(), PreferencesActivity.class); - startActivity(settingsActivity); - return true; - case R.id.menu_find_my_location: - final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); - if (sharedPrefs.getBoolean("GPSEnabled", false)) { - GeoPoint userLocation = myLocationOverlay.getMyLocation(); - if (userLocation != null) { - Log.i(TAG, "Found user location, scrolling to " + userLocation); - mapController.animateTo(userLocation); - myLocationOverlay.enableFollowLocation(); + Log.i(TAG, "There are " + newFavouriteItems.size() + " favourites"); + if (newFavouriteItems.size() == 0) { + Log.i(TAG, "Favourite dialog has no favourites, displaying message"); + favDialog.setMessage(getResources().getString(R.string.favourites_dialog_message)); + favDialog.setItems(null); + } else { + favDialog.setMessage(""); + favDialog.setItems(newFavouriteItems); } - } else { - DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int which) { - switch (which) { - case DialogInterface.BUTTON_POSITIVE: - Editor editor = sharedPrefs.edit(); - editor.putBoolean("GPSEnabled", true); - editor.commit(); - break; - - case DialogInterface.BUTTON_NEGATIVE: - // No button clicked - break; - } - } - }; - - AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setMessage("GPS is not enabled, do you wish to enable it?") - .setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener) - .show(); - } - - return true; - case R.id.menu_view: - Log.i(TAG, "Showing view dialog"); - showDialog(VIEW_DIALOG_ID); - return false; - case R.id.menu_favourites: - Log.i(TAG, "Showing favourite dialog"); - - showDialog(FAVOURITE_DIALOG_ID); - if (favDialog == null) { - Log.e(TAG, "Very wierd, just tried to launch the favourite's dialog, but its null?"); - return false; - } - - refreshFavouriteDialog(); - - return false; - case R.id.menu_about: - Intent aboutIntent = new Intent(MapActivity.this, AboutActivity.class); - startActivityForResult(aboutIntent, 0); - return true; - default: - Log.e(TAG, "No known menu option selected"); - return super.onOptionsItemSelected(item); } - } - - private void refreshFavouriteDialog() { - ArrayList newFavouriteItems = new ArrayList(); - try { - 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)); - } catch (SQLException e) { - e.printStackTrace(); + @Override + public boolean onSearchRequested() { + Intent i = new Intent(MapActivity.this, FindActivity.class); + startActivityForResult(i, 0); + return false; } - Log.i(TAG, "There are " + newFavouriteItems.size() + " favourites"); - if (newFavouriteItems.size() == 0) { - Log.i(TAG, "Favourite dialog has no favourites, displaying message"); - favDialog.setMessage(getResources().getString(R.string.favourites_dialog_message)); - favDialog.setItems(null); - } else { - favDialog.setMessage(""); - favDialog.setItems(newFavouriteItems); - } - } - - @Override - public boolean onSearchRequested() { - Intent i = new Intent(MapActivity.this, FindActivity.class); - startActivityForResult(i, 0); - return false; - } - - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - Log.i(TAG, "Got activity result"); - if (resultCode == RESULT_OK) { - - POI poi = null; - Bundle bundle = data.getExtras(); - if (bundle == null) { - Log.i(TAG, "Bundle is null"); - } else { - String poiId = (String) bundle.get("poi"); - if (poiId != null) { - Log.i(TAG, "Got id " + poiId); - try { - poi = getHelper().getBuildingDao().queryForId(poiId); - if (poi == null) { - poi = getHelper().getBusStopDao().queryForId(poiId); - } - } catch (SQLException e) { - e.printStackTrace(); - } - - if (poi == null) { - Log.e(TAG, "Could not find poi " + poiId + " in onActivityResult"); - } else { - if (myLocationOverlay != null) { - // It could be null if it has not been enabled - myLocationOverlay.disableFollowLocation(); - } - mapController.setZoom(20); - mapController.setCenter(poi.point); + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + Log.i(TAG, "Got activity result"); + if (resultCode == RESULT_OK) { - } - } else { - Log.i(TAG, "Got null poi id"); + POI poi = null; + Bundle bundle = data.getExtras(); + if (bundle == null) { + Log.i(TAG, "Bundle is null"); + } else { + String poiId = (String) bundle.get("poi"); + if (poiId != null) { + Log.i(TAG, "Got id " + poiId); + try { + poi = getHelper().getBuildingDao().queryForId(poiId); + if (poi == null) { + poi = getHelper().getBusStopDao().queryForId(poiId); + } + } catch (SQLException e) { + e.printStackTrace(); + } + + if (poi == null) { + Log.e(TAG, "Could not find poi " + poiId + " in onActivityResult"); + } else { + if (myLocationOverlay != null) { + // It could be null if it has not been enabled + myLocationOverlay.disableFollowLocation(); + } + mapController.setZoom(20); + mapController.setCenter(poi.point); + + } + } else { + Log.i(TAG, "Got null poi id"); - // mapController.setZoom(15); - // mapController.setCenter(new GeoPoint(50935551, -1393488)); - } + // mapController.setZoom(15); + // mapController.setCenter(new GeoPoint(50935551, + // -1393488)); + } - // This handles the possible change in favourite state caused by the user within the BusTimeActivity - try { - String busStopID = bundle.getString("busStopChanged"); - if (busStopID != null && busStopID.length() != 0) { - Log.v(TAG, "Got a busStop id back from the BusTimeActivity " + busStopID); - BusStop busStop = getHelper().getBusStopDao().queryForId(busStopID); - - if (busStop.uniLink) { - uniLinkBusStopOverlay.refresh(busStop); // This does not invalidate the map, but it seems to - // make the changes appear - } else { - nonUniLinkBusStopOverlay.refresh(busStop); // This does not invalidate the map, but it seems - // to - // make the changes appear + // This handles the possible change in favourite state caused by + // the user within the BusTimeActivity + try { + String busStopID = bundle.getString("busStopChanged"); + if (busStopID != null && busStopID.length() != 0) { + Log.v(TAG, "Got a busStop id back from the BusTimeActivity " + busStopID); + BusStop busStop = getHelper().getBusStopDao().queryForId(busStopID); + + if (busStop.uniLink) { + uniLinkBusStopOverlay.refresh(busStop); // This does + // not + // invalidate + // the map, + // but it + // seems to + // make the changes appear + } else { + nonUniLinkBusStopOverlay.refresh(busStop); // This + // does + // not + // invalidate + // the + // map, + // but + // it + // seems + // to + // make the changes appear + } + } + } catch (SQLException e) { + e.printStackTrace(); + } + + if (favDialog != null) { + refreshFavouriteDialog(); + } } - } - } catch (SQLException e) { - e.printStackTrace(); } - if (favDialog != null) { - refreshFavouriteDialog(); - } - } } - } - - /* - * public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { - * - * mapView.post(new Runnable() { public void run() { // updateEnabledOverlays(); TODO Fix whatever this did? - * mapView.invalidate(); } }); - * - * return true; } - */ - - protected Dialog onCreateDialog(int id) { - switch (id) { - case VIEW_DIALOG_ID: - ViewDialog viewDialog = new ViewDialog(instance); - return viewDialog; - case FAVOURITE_DIALOG_ID: - favDialog = new POIDialog(instance); - favDialog.setOnItemClickListener(this); - favDialog.setOnItemLongClickListener(this); - favDialog.setTitle(R.string.favourites_dialog_title); - return favDialog; + /* + * public boolean onChildClick(ExpandableListView parent, View v, int + * groupPosition, int childPosition, long id) { + * + * mapView.post(new Runnable() { public void run() { // + * updateEnabledOverlays(); TODO Fix whatever this did? + * mapView.invalidate(); } }); + * + * return true; } + */ + + protected Dialog onCreateDialog(int id) { + switch (id) { + case VIEW_DIALOG_ID: + ViewDialog viewDialog = new ViewDialog(instance); + return viewDialog; + case FAVOURITE_DIALOG_ID: + favDialog = new POIDialog(instance); + favDialog.setOnItemClickListener(this); + favDialog.setOnItemLongClickListener(this); + favDialog.setTitle(R.string.favourites_dialog_title); + return favDialog; + } + return null; } - return null; - } - public void onItemClick(AdapterView parent, View view, int position, long id) { - Log.i(TAG, "OnItemClick pos " + position + " id " + id); + public void onItemClick(AdapterView parent, View view, int position, long id) { + Log.i(TAG, "OnItemClick pos " + position + " id " + id); - String poiId = favDialog.adapter.getItemStringId(position); + String poiId = favDialog.adapter.getItemStringId(position); - Log.i(TAG, "POI " + poiId + " selected"); + Log.i(TAG, "POI " + poiId + " selected"); - POI poi = null; + POI poi = null; - if (poiId != null) { - Log.i(TAG, "Got id " + poiId); - try { - poi = getHelper().getBuildingDao().queryForId(poiId); - if (poi == null) { - poi = getHelper().getBusStopDao().queryForId(poiId); - } - } catch (SQLException e) { - e.printStackTrace(); - } + if (poiId != null) { + Log.i(TAG, "Got id " + poiId); + try { + poi = getHelper().getBuildingDao().queryForId(poiId); + if (poi == null) { + poi = getHelper().getBusStopDao().queryForId(poiId); + } + } catch (SQLException e) { + e.printStackTrace(); + } - if (poi == null) { - Log.e(TAG, "Could not find poi " + poiId + " in onActivityResult"); - } else { - if (myLocationOverlay != null) { - myLocationOverlay.disableFollowLocation(); - } - mapController.setZoom(20); - mapController.setCenter(poi.point); + if (poi == null) { + Log.e(TAG, "Could not find poi " + poiId + " in onActivityResult"); + } else { + if (myLocationOverlay != null) { + myLocationOverlay.disableFollowLocation(); + } + mapController.setZoom(20); + mapController.setCenter(poi.point); - favDialog.dismiss(); + favDialog.dismiss(); + + } + } else { + Log.i(TAG, "Got null poi id"); - } - } else { - Log.i(TAG, "Got null poi id"); + // mapController.setZoom(15); + // mapController.setCenter(new GeoPoint(50935551, -1393488)); + } - // mapController.setZoom(15); - // mapController.setCenter(new GeoPoint(50935551, -1393488)); } - } + /** + * Long click on a item in the favourites menu + */ + public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { + + Log.i(TAG, "OnItemClick pos " + position + " id " + id); - /** - * Long click on a item in the favourites menu - */ - public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { + String poiId = favDialog.adapter.getItemStringId(position); - Log.i(TAG, "OnItemClick pos " + position + " id " + id); + Log.i(TAG, "POI " + poiId + " selected"); - String poiId = favDialog.adapter.getItemStringId(position); + POI poi = null; - Log.i(TAG, "POI " + poiId + " selected"); + if (poiId != null) { + Log.i(TAG, "Got id " + poiId); + try { + poi = getHelper().getBuildingDao().queryForId(poiId); + if (poi == null) { + poi = getHelper().getBusStopDao().queryForId(poiId); + } + } catch (SQLException e) { + e.printStackTrace(); + } - POI poi = null; + if (poi == null) { + Log.e(TAG, "Could not find poi " + poiId + " in onActivityResult"); + } else { + if (poi.type == POI.BUS_STOP) { + BusStop busStop = (BusStop) poi; - if (poiId != null) { - Log.i(TAG, "Got id " + poiId); - try { - poi = getHelper().getBuildingDao().queryForId(poiId); - if (poi == null) { - poi = getHelper().getBusStopDao().queryForId(poiId); - } - } catch (SQLException e) { - e.printStackTrace(); - } + Log.i(TAG, "Pressed " + busStop.id); - if (poi == null) { - Log.e(TAG, "Could not find poi " + poiId + " in onActivityResult"); - } else { - if (poi.type == POI.BUS_STOP) { - BusStop busStop = (BusStop) poi; + Intent i = new Intent(this, BusStopActivity.class); + i.putExtra("busStopID", busStop.id); + i.putExtra("busStopName", busStop.description); + startActivityForResult(i, 0); - Log.i(TAG, "Pressed " + busStop.id); + return true; - Intent i = new Intent(this, BusStopActivity.class); - i.putExtra("busStopID", busStop.id); - i.putExtra("busStopName", busStop.description); - startActivityForResult(i, 0); + } else { - return true; + myLocationOverlay.disableFollowLocation(); + mapController.setZoom(20); + mapController.setCenter(poi.point); + favDialog.dismiss(); + favDialog = null; + } + } } else { + Log.i(TAG, "Got null poi id"); - myLocationOverlay.disableFollowLocation(); - mapController.setZoom(20); - mapController.setCenter(poi.point); - - favDialog.dismiss(); - favDialog = null; + // mapController.setZoom(15); + // mapController.setCenter(new GeoPoint(50935551, -1393488)); } - } - } else { - Log.i(TAG, "Got null poi id"); - // mapController.setZoom(15); - // mapController.setCenter(new GeoPoint(50935551, -1393488)); + return true; } - return true; - } + private class OverlayRankComparator implements Comparator { + // private final SharedPreferences prefs; - private class OverlayRankComparator implements Comparator { - // private final SharedPreferences prefs; + OverlayRankComparator(SharedPreferences prefs) { + // this.prefs = prefs; + } - OverlayRankComparator(SharedPreferences prefs) { - // this.prefs = prefs; - } + public int compare(Overlay arg0, Overlay arg1) { + return getRank(arg1) - getRank(arg0); + } - public int compare(Overlay arg0, Overlay arg1) { - return getRank(arg1) - getRank(arg0); + 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.equals(residentialBuildingOverlay)) { + return RESIDENTIAL_BUILDING_OVERLAY_RANK; + } else if (overlay.equals(nonResidentialBuildingOverlay)) { + return NON_RESIDENTIAL_BUILDING_OVERLAY_RANK; + } else if (siteOverlays != null && siteOverlays.values().contains(overlay)) { + return SITE_OVERLAYS_RANK; + } else if (busRouteOverlays != null && busRouteOverlays.values().contains(overlay)) { + return BUS_ROUTE_OVERLAYS_RANK; + } else { + Log.e(TAG, "Trying to rank unknown overlay " + overlay); + return -1; + } + } } - 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.equals(residentialBuildingOverlay)) { - return RESIDENTIAL_BUILDING_OVERLAY_RANK; - } else if (overlay.equals(nonResidentialBuildingOverlay)) { - return NON_RESIDENTIAL_BUILDING_OVERLAY_RANK; - } else if (siteOverlays != null && siteOverlays.values().contains(overlay)) { - return SITE_OVERLAYS_RANK; - } else if (busRouteOverlays != null && busRouteOverlays.values().contains(overlay)) { - return BUS_ROUTE_OVERLAYS_RANK; - } else { - Log.e(TAG, "Trying to rank unknown overlay " + overlay); - return -1; - } - } - } - - /** - * Handles all changes in preferences - */ - public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { - Log.v(TAG, "Got shared prefs changed event for key " + key); - - // Shared Preferences - if (key.equals(GPS_ENABLED)) { - final SharedPreferences activityPrefs = getPreferences(0); - - if (activityPrefs.getBoolean("Other:Compass", false) && prefs.getBoolean("GPSEnabled", false)) { - myLocationOverlay.enableMyLocation(); - } else { - myLocationOverlay.disableMyLocation(); - } - } else if (key.equals(NON_UNI_LINK_BUS_TIMES)) { - // Noting to do here atm - } else if (key.equals(UNI_LINK_BUS_TIMES)) { - // Noting to do here atm - } else if (key.startsWith(BUS_STOP_OVERLAYS)) { - showUniLinkBusStopOverlay(); - } else if (key.equals(NON_UNI_LINK_BUS_STOPS_OVERLAY)) { - showNonUniLinkBusStopOverlay(); - } else if (key.startsWith(BUS_ROUTE_OVERLAYS)) { - try { - String routeName = key.substring(BUS_ROUTE_OVERLAYS.length(), key.length()); - for (BusRoute route : getHelper().getBusRouteDao()) { - Log.v(TAG, route.code + " " + routeName); - if (route.code.equals(routeName)) { - showRouteOverlay(route); - } - } - } catch (SQLException e) { - e.printStackTrace(); - } - } else if (key.startsWith(BUILDING_OVERLAYS)) { - showBuildingOverlays(); - } else if (key.startsWith(SITE_OVERLAYS)) { - String siteName = key.substring(SITE_OVERLAYS.length(), key.length()); - try { - for (Site site : getHelper().getSiteDao()) { - if (site.name.equals(siteName)) { - showSiteOverlay(site); - } - } - } catch (SQLException e) { - e.printStackTrace(); - } - } else if (key.startsWith(OTHER_OVERLAYS)) { - if (key.substring(OTHER_OVERLAYS.length(), key.length()).equals(SCALE_BAR_OVERLAY)) { - showUtilityOverlays(); - } else if (key.substring(OTHER_OVERLAYS.length(), key.length()).equals(MY_LOCATION_OVERLAY_COMPASS)) { - if (prefs.getBoolean(key, MY_LOCATION_OVERLAY_COMPASS_ENABLED_BY_DEFAULT)) { - myLocationOverlay.enableCompass(); - } else { - myLocationOverlay.disableCompass(); - } - } else if (key.substring(OTHER_OVERLAYS.length(), key.length()).equals(MY_LOCATION_OVERLAY)) { - final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); + /** + * Handles all changes in preferences + */ + public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { + Log.v(TAG, "Got shared prefs changed event for key " + key); + + // Shared Preferences + if (key.equals(GPS_ENABLED)) { + final SharedPreferences activityPrefs = getPreferences(0); + + if (activityPrefs.getBoolean("Other:Compass", false) && prefs.getBoolean("GPSEnabled", false)) { + myLocationOverlay.enableMyLocation(); + } else { + myLocationOverlay.disableMyLocation(); + } + } else if (key.equals(NON_UNI_LINK_BUS_TIMES)) { + // Noting to do here atm + } else if (key.equals(UNI_LINK_BUS_TIMES)) { + // Noting to do here atm + } else if (key.startsWith(BUS_STOP_OVERLAYS)) { + showUniLinkBusStopOverlay(); + } else if (key.equals(NON_UNI_LINK_BUS_STOPS_OVERLAY)) { + showNonUniLinkBusStopOverlay(); + } else if (key.startsWith(BUS_ROUTE_OVERLAYS)) { + try { + String routeName = key.substring(BUS_ROUTE_OVERLAYS.length(), key.length()); + for (BusRoute route : getHelper().getBusRouteDao()) { + Log.v(TAG, route.code + " " + routeName); + if (route.code.equals(routeName)) { + showRouteOverlay(route); + } + } + } catch (SQLException e) { + e.printStackTrace(); + } + } else if (key.startsWith(BUILDING_OVERLAYS)) { + showBuildingOverlays(); + } else if (key.startsWith(SITE_OVERLAYS)) { + String siteName = key.substring(SITE_OVERLAYS.length(), key.length()); + try { + for (Site site : getHelper().getSiteDao()) { + if (site.name.equals(siteName)) { + showSiteOverlay(site); + } + } + } catch (SQLException e) { + e.printStackTrace(); + } + } else if (key.startsWith(OTHER_OVERLAYS)) { + if (key.substring(OTHER_OVERLAYS.length(), key.length()).equals(SCALE_BAR_OVERLAY)) { + showUtilityOverlays(); + } else if (key.substring(OTHER_OVERLAYS.length(), key.length()).equals(MY_LOCATION_OVERLAY_COMPASS)) { + if (prefs.getBoolean(key, MY_LOCATION_OVERLAY_COMPASS_ENABLED_BY_DEFAULT)) { + myLocationOverlay.enableCompass(); + } else { + myLocationOverlay.disableCompass(); + } + } else if (key.substring(OTHER_OVERLAYS.length(), key.length()).equals(MY_LOCATION_OVERLAY)) { + final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); - if (prefs.getBoolean(key, MY_LOCATION_OVERLAY_ENABLED_BY_DEFAULT) - && sharedPrefs.getBoolean(GPS_ENABLED, GPS_ENABLED_BY_DEFAULT)) { - myLocationOverlay.enableMyLocation(); + if (prefs.getBoolean(key, MY_LOCATION_OVERLAY_ENABLED_BY_DEFAULT) + && sharedPrefs.getBoolean(GPS_ENABLED, GPS_ENABLED_BY_DEFAULT)) { + myLocationOverlay.enableMyLocation(); + } else { + myLocationOverlay.disableMyLocation(); + } + } else { + Log.e(TAG, "Unhandled preference key " + key); + } } else { - myLocationOverlay.disableMyLocation(); + Log.e(TAG, "Unhandled preference key " + key); } - } else { - Log.e(TAG, "Unhandled preference key " + key); - } - } else { - Log.e(TAG, "Unhandled preference key " + key); } - } } -- cgit v1.2.3 From b88f5cabd4cf4876c3e5ba32acb2a7e59dd03edc Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Fri, 24 Feb 2012 22:35:05 +0000 Subject: Fix for bug with bus stops. --- src/net/cbaines/suma/DataManager.java | 8 ++++---- src/net/cbaines/suma/MapActivity.java | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/net/cbaines/suma/DataManager.java b/src/net/cbaines/suma/DataManager.java index 56dac5c..b4a0e3d 100644 --- a/src/net/cbaines/suma/DataManager.java +++ b/src/net/cbaines/suma/DataManager.java @@ -60,9 +60,7 @@ import org.xml.sax.XMLReader; import android.content.Context; import android.util.Log; -import com.hp.hpl.jena.query.Dataset; import com.hp.hpl.jena.rdf.model.Model; -import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.tdb.TDBFactory; import com.j256.ormlite.android.apptools.OpenHelperManager; import com.j256.ormlite.dao.Dao; @@ -421,8 +419,10 @@ public class DataManager { } else if (busRoute.id == 354) { // U9 busStop.routes = (byte) (busStop.routes | (1 << 4)); busStop.uniLink = true; - } else { - busStop.routes = 0; + } + + if ((busStop.routes != 0) != busStop.uniLink || busStop.id.equals("SN120553")) { + Log.e(TAG, busStop.id + " " + busStop.routes + " " + busStop.uniLink); } // Log.v(TAG, "Stop routes " + busStop.routes); diff --git a/src/net/cbaines/suma/MapActivity.java b/src/net/cbaines/suma/MapActivity.java index ff61c4e..334f4d2 100644 --- a/src/net/cbaines/suma/MapActivity.java +++ b/src/net/cbaines/suma/MapActivity.java @@ -78,7 +78,7 @@ public class MapActivity extends OrmLiteBaseActivity implements * Enable to use the database in the assets folder, if its not enabled, the * database is built from the csv files in the assets folder */ - private boolean useBundledDatabase = true; + private boolean useBundledDatabase = false; private MapView mapView; private MapController mapController; -- cgit v1.2.3 From 731a2b26410f6fa6427acdf675376742793c6332 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Sun, 26 Feb 2012 17:09:58 +0000 Subject: Optimised the custom overlays. --- src/net/cbaines/suma/BuildingActivity.java | 120 +++++++------- src/net/cbaines/suma/BuildingNumOverlay.java | 92 ++++------- src/net/cbaines/suma/BusStopActivity.java | 35 ++-- src/net/cbaines/suma/BusStopOverlay.java | 109 ++++--------- src/net/cbaines/suma/DataManager.java | 62 ------- src/net/cbaines/suma/MapActivity.java | 195 ++++++++--------------- src/net/cbaines/suma/POI.java | 3 - src/net/cbaines/suma/POIFavouriteComparator.java | 32 ++-- src/net/cbaines/suma/Preferences.java | 2 + 9 files changed, 235 insertions(+), 415 deletions(-) (limited to 'src') diff --git a/src/net/cbaines/suma/BuildingActivity.java b/src/net/cbaines/suma/BuildingActivity.java index 930c2d1..45babbc 100644 --- a/src/net/cbaines/suma/BuildingActivity.java +++ b/src/net/cbaines/suma/BuildingActivity.java @@ -23,38 +23,30 @@ import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; -import java.util.Iterator; +import java.sql.SQLException; import android.content.Context; +import android.content.SharedPreferences; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.util.Log; +import android.view.Display; import android.view.View; +import android.view.WindowManager; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.ImageButton; +import android.widget.ImageView.ScaleType; import android.widget.TextView; -import com.hp.hpl.jena.query.Query; -import com.hp.hpl.jena.query.QueryExecution; -import com.hp.hpl.jena.query.QueryExecutionFactory; -import com.hp.hpl.jena.query.QueryFactory; -import com.hp.hpl.jena.query.QuerySolution; -import com.hp.hpl.jena.query.ResultSet; -import com.hp.hpl.jena.rdf.model.Literal; -import com.hp.hpl.jena.rdf.model.Model; -import com.hp.hpl.jena.rdf.model.RDFNode; -import com.hp.hpl.jena.rdf.model.Resource; -import com.hp.hpl.jena.tdb.TDBFactory; import com.j256.ormlite.android.apptools.OrmLiteBaseActivity; +import com.j256.ormlite.dao.Dao; -public class BuildingActivity extends OrmLiteBaseActivity implements OnCheckedChangeListener { +public class BuildingActivity extends OrmLiteBaseActivity implements OnCheckedChangeListener, Preferences { final static String TAG = "BusTimeActivity"; - private boolean dataChanged; - private Context instance; private ImageButton imageButton; @@ -62,6 +54,8 @@ public class BuildingActivity extends OrmLiteBaseActivity implem private TextView buildingName; private TextView buildingID; + private Building building; + private CheckBox favouritesCheckBox; public void onCreate(Bundle savedInstanceState) { @@ -71,8 +65,10 @@ public class BuildingActivity extends OrmLiteBaseActivity implem String ID = getIntent().getExtras().getString("buildingID"); + final SharedPreferences favouritesPrefs = instance.getSharedPreferences(FAVOURITES_PREFERENCES, 0); + favouritesCheckBox = (CheckBox) findViewById(R.id.buildingActivityFavouriteCheckBox); - // favouritesCheckBox.setChecked(busStop.favourite); + favouritesCheckBox.setChecked(favouritesPrefs.getBoolean(ID, false)); favouritesCheckBox.setOnCheckedChangeListener(this); buildingName = (TextView) findViewById(R.id.buildingActivityName); @@ -80,17 +76,61 @@ public class BuildingActivity extends OrmLiteBaseActivity implem Log.i(TAG, "Building id " + ID); - buildingID.setText(ID); + try { + Dao buildingDao = getHelper().getBuildingDao(); + + building = buildingDao.queryForId(ID); + + buildingName.setText(building.name); + } catch (SQLException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + + buildingID.setText(ID); try { - URL url = new URL("http://data.southampton.ac.uk/images/buildings/600/" + ID + ".jpg"); - InputStream is = (InputStream) url.getContent(); + /* First, get the Display from the WindowManager */ + Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); + + /* Now we can retrieve all display-related infos */ + int orientation = display.getOrientation(); + Log.i(TAG, "orientation " + orientation); + Log.i(TAG, "width " + display.getWidth() + " height " + display.getHeight()); + int width; + // if (orientation == 0) { + width = display.getWidth(); + // } else { + // width = display.getHeight(); + // } + + URL imageURL; + Log.i(TAG, "Screen width " + width); + if (width >= 1000) { + imageURL = new URL("http://data.southampton.ac.uk/images/buildings/1000/" + ID + ".jpg"); + } else if (width >= 800) { + imageURL = new URL("http://data.southampton.ac.uk/images/buildings/1000/" + ID + ".jpg"); + } else if (width >= 600) { + imageURL = new URL("http://data.southampton.ac.uk/images/buildings/600/" + ID + ".jpg"); + } else if (width >= 400) { + imageURL = new URL("http://data.southampton.ac.uk/images/buildings/600/" + ID + ".jpg"); + } else if (width >= 300) { + imageURL = new URL("http://data.southampton.ac.uk/images/buildings/300/" + ID + ".jpg"); + } else if (width >= 200) { + imageURL = new URL("http://data.southampton.ac.uk/images/buildings/200/" + ID + ".jpg"); + } else { + imageURL = new URL("http://data.southampton.ac.uk/images/buildings/100/" + ID + ".jpg"); + } + Log.i(TAG, "URL " + imageURL); + + InputStream is = (InputStream) imageURL.getContent(); Drawable image = Drawable.createFromStream(is, "src"); imageButton = new ImageButton(this); imageButton = (ImageButton) findViewById(R.id.buildingActivityImage); imageButton.setImageDrawable(image); + imageButton.setScaleType(ScaleType.CENTER_INSIDE); imageButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { @@ -105,51 +145,18 @@ public class BuildingActivity extends OrmLiteBaseActivity implem e.printStackTrace(); } - String directory = "/data/data/net.cbaines.suma/databases"; - Model model = TDBFactory.createModel(directory); - - String queryString = ID; - Query query = QueryFactory.create(queryString); - QueryExecution qexec = QueryExecutionFactory.create(query, model); - try { - ResultSet results = qexec.execSelect(); - for (; results.hasNext();) { - QuerySolution soln = results.nextSolution(); - - Log.i(TAG, "solution " + soln.toString()); - - for (Iterator iter = soln.varNames(); iter.hasNext();) { - Log.i(TAG, "solnVarName " + iter.next()); - } - - // RDFNode x = soln.get("varName"); // Get a result variable by - // name. - // Resource r = soln.getResource("VarR"); // Get a result - // variable - // - must be a resource - // Literal l = soln.getLiteral("VarL"); // Get a result variable - // - - // must be a literal - } - } finally { - qexec.close(); - } - } public void onResume() { super.onResume(); - } public void onPause() { - super.onPause(); } public void finish() { setResult(RESULT_OK, getIntent()); - super.finish(); } @@ -158,9 +165,14 @@ public class BuildingActivity extends OrmLiteBaseActivity implem return null; } - public void onCheckedChanged(CompoundButton arg0, boolean arg1) { - // TODO Auto-generated method stub + public void onCheckedChanged(CompoundButton button, boolean checked) { + SharedPreferences favouritesPreferences = getSharedPreferences(FAVOURITES_PREFERENCES, 0); + if (checked) { + favouritesPreferences.edit().putBoolean(building.id, true).commit(); + } else { + favouritesPreferences.edit().remove(building.id).commit(); + } } } 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 buildings; @@ -64,8 +63,6 @@ public class BuildingNumOverlay extends Overlay { private final MapActivity context; - private Dao buildingDao; - private float userScale = 1f; public BuildingNumOverlay(MapActivity context, List 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.
+ * Draw a marker on each of our items. populate() must have been called first.
*
- * 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.
+ * 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.
*
- * 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.
+ * 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.
*
- * The focused item is always drawn last, which puts it visually on top of - * the other items.
+ * The focused item is always drawn last, which puts it visually on top of the other items.
* * @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 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))); } } diff --git a/src/net/cbaines/suma/BusStopActivity.java b/src/net/cbaines/suma/BusStopActivity.java index 0d4dea4..bc8e22e 100644 --- a/src/net/cbaines/suma/BusStopActivity.java +++ b/src/net/cbaines/suma/BusStopActivity.java @@ -59,13 +59,11 @@ import com.j256.ormlite.dao.Dao; import com.j256.ormlite.stmt.PreparedQuery; import com.j256.ormlite.stmt.QueryBuilder; -public class BusStopActivity extends OrmLiteBaseActivity implements OnCheckedChangeListener, - Preferences, OnItemClickListener { +public class BusStopActivity extends OrmLiteBaseActivity implements OnCheckedChangeListener, Preferences, + OnItemClickListener { final static String TAG = "BusTimeActivity"; - private boolean dataChanged; - private ListView busTimeList; private TextView busName; private TextView busID; @@ -110,6 +108,8 @@ public class BusStopActivity extends OrmLiteBaseActivity impleme final DatabaseHelper helper = getHelper(); + SharedPreferences favouritesPreferences = getSharedPreferences(FAVOURITES_PREFERENCES, 0); + instance = this; busStopID = getIntent().getExtras().getString("busStopID"); @@ -184,7 +184,7 @@ public class BusStopActivity extends OrmLiteBaseActivity impleme busStop = busStopDao.queryForId(busStopID); busFavourite = (CheckBox) findViewById(R.id.favouriteCheckBox); - busFavourite.setChecked(busStop.favourite); + busFavourite.setChecked(favouritesPreferences.getBoolean(busStop.id, false)); busFavourite.setOnCheckedChangeListener(this); } catch (SQLException e) { @@ -257,23 +257,18 @@ public class BusStopActivity extends OrmLiteBaseActivity impleme } public void finish() { - if (dataChanged) { - getIntent().putExtra("busStopChanged", busStopID); - } - setResult(RESULT_OK, getIntent()); - super.finish(); } public void onCheckedChanged(CompoundButton button, boolean checked) { if (button.equals(busFavourite)) { - busStop.favourite = checked; - try { - busStopDao.update(busStop); - dataChanged = true; - } catch (SQLException e) { - e.printStackTrace(); + SharedPreferences favouritesPreferences = getSharedPreferences(FAVOURITES_PREFERENCES, 0); + + if (checked) { + favouritesPreferences.edit().putBoolean(busStop.id, true).commit(); + } else { + favouritesPreferences.edit().remove(busStop.id).commit(); } } else { Log.i(TAG, "Route radio button made " + checked); @@ -300,10 +295,10 @@ public class BusStopActivity extends OrmLiteBaseActivity impleme try { final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(instance); - newTimetable = DataManager.getTimetable(instance, busStopID, sharedPrefs.getBoolean( - MapActivity.UNI_LINK_BUS_TIMES, MapActivity.UNI_LINK_BUS_TIMES_ENABLED_BY_DEFAULT), sharedPrefs - .getBoolean(MapActivity.NON_UNI_LINK_BUS_TIMES, - MapActivity.NON_UNI_LINK_BUS_TIMES_ENABLED_BY_DEFAULT)); + newTimetable = DataManager + .getTimetable(instance, busStopID, sharedPrefs.getBoolean(MapActivity.UNI_LINK_BUS_TIMES, + MapActivity.UNI_LINK_BUS_TIMES_ENABLED_BY_DEFAULT), sharedPrefs.getBoolean( + MapActivity.NON_UNI_LINK_BUS_TIMES, MapActivity.NON_UNI_LINK_BUS_TIMES_ENABLED_BY_DEFAULT)); } catch (SQLException e) { errorMessage = "Error message regarding SQL?"; e.printStackTrace(); diff --git a/src/net/cbaines/suma/BusStopOverlay.java b/src/net/cbaines/suma/BusStopOverlay.java index 060ad47..176bd4f 100644 --- a/src/net/cbaines/suma/BusStopOverlay.java +++ b/src/net/cbaines/suma/BusStopOverlay.java @@ -29,6 +29,7 @@ import org.osmdroid.views.overlay.Overlay; import android.app.Activity; import android.content.Intent; +import android.content.SharedPreferences; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Paint.Style; @@ -39,10 +40,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 BusStopOverlay extends Overlay implements RouteColorConstants { +public class BusStopOverlay extends Overlay implements RouteColorConstants, Preferences { List busStops; @@ -61,8 +59,6 @@ public class BusStopOverlay extends Overlay implements RouteColorConstants { private final MapActivity context; - private Dao busStopDao; - private float userScale = 1f; private boolean[] routes = new boolean[5]; @@ -75,9 +71,6 @@ public class BusStopOverlay extends Overlay implements RouteColorConstants { marker = context.getResources().getDrawable(R.drawable.busstop); favMarker = context.getResources().getDrawable(R.drawable.busstop_fav); - DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); - busStopDao = helper.getBusStopDao(); - paint = new Paint(); paint.setStyle(Style.FILL); paint.setStrokeWidth(6); @@ -96,7 +89,7 @@ public class BusStopOverlay extends Overlay implements RouteColorConstants { return; } - float scale = mScale * userScale; + final float scale = mScale * userScale; final Projection pj = mapView.getProjection(); @@ -108,14 +101,20 @@ public class BusStopOverlay extends Overlay implements RouteColorConstants { marker.setBounds(mRect); favMarker.setBounds(mRect); + final SharedPreferences favouritesPreferences = 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 (int stopNum = 0; stopNum < busStops.size(); stopNum++) { BusStop stop = busStops.get(stopNum); + pj.toMapPixels(stop.point, mCurScreenCoords); + if (!pj.getBoundingBox().increaseByScale(1.2f).contains(stop.point)) { + continue; + } + byte routeNum = 0; final byte stopRoutes = stop.routes; @@ -137,9 +136,7 @@ public class BusStopOverlay extends Overlay implements RouteColorConstants { } - pj.toMapPixels(stop.point, mCurScreenCoords); - - if (stop.favourite) { + if (favouritesPreferences.getBoolean(stop.id, false)) { Overlay.drawAt(canvas, favMarker, mCurScreenCoords.x, mCurScreenCoords.y, false); } else { Overlay.drawAt(canvas, marker, mCurScreenCoords.x, mCurScreenCoords.y, false); @@ -148,12 +145,13 @@ public class BusStopOverlay extends Overlay implements RouteColorConstants { if (stop.uniLink) { int makersPlaced = 0; + + final float rectLeft = mCurScreenCoords.x + (8.8f * scale); + final float rectRight = rectLeft + (int) (8 * scale); + int yOfsetPerMarker = (int) (10 * scale); int markerYSize = (int) (8 * scale); - float rectLeft = mCurScreenCoords.x + (8.8f * scale); - float rectRight = rectLeft + markerYSize; - if (routeNum == 5) { markerYSize = (int) (5 * scale); yOfsetPerMarker = (int) (7 * scale); @@ -165,12 +163,6 @@ public class BusStopOverlay extends Overlay implements RouteColorConstants { for (int i = 0; i < 5; i++) { if ((stopRoutes & (1 << i)) != 0) { - // Log.i(TAG, "Route " + route + " is " + - // routes.get(route)); - - // Log.i(TAG, "Index is " + busRoutes.indexOf(route) + - // " busRoutes " + busRoutes); - if (i == 0) { paint.setColor(U1); } else if (i == 1) { @@ -185,9 +177,9 @@ public class BusStopOverlay extends Overlay implements RouteColorConstants { Log.e(TAG, "Unknown route code"); } - canvas.drawRect(rectLeft, mCurScreenCoords.y - + ((yOfsetPerMarker * makersPlaced) - (45 * scale)), rectRight, mCurScreenCoords.y - + (yOfsetPerMarker * makersPlaced) - ((45 * scale) - markerYSize), paint); + canvas.drawRect(rectLeft, mCurScreenCoords.y + ((yOfsetPerMarker * makersPlaced) - (45 * scale)), + rectRight, mCurScreenCoords.y + (yOfsetPerMarker * makersPlaced) - ((45 * scale) - markerYSize), + paint); makersPlaced++; } @@ -208,8 +200,8 @@ public class BusStopOverlay extends Overlay implements RouteColorConstants { Log.i(TAG, "busStop Pressed " + busStop.id); if (context.activityToast == null) { - context.activityToast = Toast.makeText(context, busStop.description + " (" + busStop.id + ")", - Toast.LENGTH_SHORT); + context.activityToast = Toast + .makeText(context, busStop.description + " (" + busStop.id + ")", Toast.LENGTH_SHORT); } else { context.activityToast.setDuration(Toast.LENGTH_SHORT); context.activityToast.setText(busStop.description + " (" + busStop.id + ")"); @@ -251,12 +243,13 @@ public class BusStopOverlay extends Overlay implements RouteColorConstants { } else { Log.i(TAG, "Pressed " + busStop.id); - if (busStop.favourite) { - busStop.favourite = false; + SharedPreferences favouritesPreferences = context.getSharedPreferences(FAVOURITES_PREFERENCES, 0); + + if (favouritesPreferences.getBoolean(busStop.id, false)) { + favouritesPreferences.edit().putBoolean(busStop.id, false).commit(); if (context.activityToast == null) { - context.activityToast = Toast.makeText(context, busStop.id + " removed from favourites", - Toast.LENGTH_SHORT); + context.activityToast = Toast.makeText(context, busStop.id + " removed from favourites", Toast.LENGTH_SHORT); } else { context.activityToast.setDuration(Toast.LENGTH_SHORT); context.activityToast.setText(busStop.id + " removed from favourites"); @@ -264,62 +257,22 @@ public class BusStopOverlay extends Overlay implements RouteColorConstants { context.activityToast.show(); } else { if (context.activityToast == null) { - context.activityToast = Toast.makeText(context, busStop.id + " made a favourite", - Toast.LENGTH_SHORT); + context.activityToast = Toast.makeText(context, busStop.id + " made a favourite", Toast.LENGTH_SHORT); } else { context.activityToast.setDuration(Toast.LENGTH_SHORT); context.activityToast.setText(busStop.id + " made a favourite"); } context.activityToast.show(); - busStop.favourite = true; + favouritesPreferences.edit().putBoolean(busStop.id, true).commit(); } - try { - busStopDao.update(busStop); - } catch (SQLException e) { - e.printStackTrace(); - } - - Collections.sort(busStops, new POIFavouriteComparator()); - mapView.invalidate(); return true; } } - public void refresh() { - try { - for (int i = 0; i < busStops.size(); i++) { - BusStop busStop = busStops.get(i); - busStopDao.refresh(busStop); - if (busStop.favourite) { - busStops.remove(i); - busStops.add(busStop); - } else { - busStops.set(i, busStop); - } - } - } catch (SQLException e) { - e.printStackTrace(); - } - } - - /** - * Replaces any bus stops that equal the argument in the overlay with the - * argument - * - * @param busStop - */ - public void refresh(BusStop busStop) { - for (int i = 0; i < busStops.size(); i++) { - if (busStop.equals(busStops.get(i))) { - busStops.set(i, busStop); - } - } - } - private BusStop getSelectedItem(final MotionEvent event, final MapView mapView) { final Projection pj = mapView.getProjection(); final int eventX = (int) event.getX(); @@ -354,4 +307,10 @@ public class BusStopOverlay extends Overlay implements RouteColorConstants { return null; } + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + Log.i(TAG, "Got a favourites change in the BuildingNumOverlay for key " + key); + + Collections.sort(busStops, new POIFavouriteComparator(context.getSharedPreferences(FAVOURITES_PREFERENCES, 0))); + } + } diff --git a/src/net/cbaines/suma/DataManager.java b/src/net/cbaines/suma/DataManager.java index b4a0e3d..fbede76 100644 --- a/src/net/cbaines/suma/DataManager.java +++ b/src/net/cbaines/suma/DataManager.java @@ -23,8 +23,6 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.io.PrintWriter; -import java.io.StringWriter; import java.sql.SQLException; import java.util.ArrayList; import java.util.Calendar; @@ -60,8 +58,6 @@ import org.xml.sax.XMLReader; import android.content.Context; import android.util.Log; -import com.hp.hpl.jena.rdf.model.Model; -import com.hp.hpl.jena.tdb.TDBFactory; import com.j256.ormlite.android.apptools.OpenHelperManager; import com.j256.ormlite.dao.Dao; import com.j256.ormlite.stmt.PreparedQuery; @@ -213,64 +209,6 @@ public class DataManager { } - public static void loadBuildingPhotos(Context context) { - // Direct way: Make a TDB-backed Jena model in the named directory. - String directory = "/data/data/net.cbaines.suma/databases"; - Model model = TDBFactory.createModel(directory); - - Log.i(TAG, "Created the model"); - - // write it to standard out - StringWriter sw = new StringWriter(); - model.write(new PrintWriter(sw)); - Log.v(TAG, sw.toString()); - - Log.i(TAG, "Finished the first output"); - - if (model.isEmpty()) { - - Log.i(TAG, "Model is empty"); - - // use the FileManager to find the input file - InputStream in; - try { - - String[] datasets = { "photos.rdf", "places.rdf" }; - - for (String dataset : datasets) { - - in = context.getAssets().open(dataset); - - if (in == null) { - throw new IllegalArgumentException(dataset + " not found"); - } - - // read the RDF/XML file - model.read(in, null); - - Log.i(TAG, "Finished the read of " + dataset); - - } - - // write it to standard out - sw = new StringWriter(); - model.write(new PrintWriter(sw)); - Log.i(TAG, sw.toString()); - - Log.i(TAG, "Finished the second output"); - - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - } - - model.close(); - - Log.i(TAG, "Closed the model"); - } - public static void loadBusData(Context context, boolean onlyUniLink) throws SQLException, IOException { DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); diff --git a/src/net/cbaines/suma/MapActivity.java b/src/net/cbaines/suma/MapActivity.java index 334f4d2..33b12cd 100644 --- a/src/net/cbaines/suma/MapActivity.java +++ b/src/net/cbaines/suma/MapActivity.java @@ -70,9 +70,8 @@ import com.j256.ormlite.dao.Dao; * @author Christopher Baines * */ -public class MapActivity extends OrmLiteBaseActivity implements MapViewConstants, Runnable, - RouteColorConstants, OnItemClickListener, OnItemLongClickListener, OnSharedPreferenceChangeListener, - Preferences { +public class MapActivity extends OrmLiteBaseActivity implements MapViewConstants, Runnable, RouteColorConstants, + OnItemClickListener, OnItemLongClickListener, OnSharedPreferenceChangeListener, Preferences { /** * Enable to use the database in the assets folder, if its not enabled, the @@ -126,11 +125,10 @@ public class MapActivity extends OrmLiteBaseActivity implements private static final int NON_UNI_LINK_BUS_STOP_OVERLAY_RANK = 4; // -- Site Overlays - static final String[] SITE_NAMES = { "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", + static final String[] SITE_NAMES = { "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" }; private static final String SITE_OVERLAYS = "siteOverlays:"; @@ -168,8 +166,8 @@ public class MapActivity extends OrmLiteBaseActivity implements // Uni-Link routes static final String[] UNI_LINK_ROUTES = { "U1", "U1N", "U2", "U6", "U9" }; - static final String[] PREFERENCES_GROUPS = { BUS_STOP_OVERLAYS, BUS_ROUTE_OVERLAYS, BUILDING_OVERLAYS, - SITE_OVERLAYS, OTHER_OVERLAYS }; + static final String[] PREFERENCES_GROUPS = { BUS_STOP_OVERLAYS, BUS_ROUTE_OVERLAYS, BUILDING_OVERLAYS, SITE_OVERLAYS, + OTHER_OVERLAYS }; static final String[][] PREFERENCES_CHILDREN = { UNI_LINK_ROUTES, UNI_LINK_ROUTES, BUILDING_TYPES, SITE_NAMES, OTHER_OVERLAY_NAMES }; @@ -259,12 +257,6 @@ public class MapActivity extends OrmLiteBaseActivity implements mapController.setCenter(userLocation); Log.i(TAG, "Finished onCreate " + (System.currentTimeMillis() - startTime)); - - new Thread() { - public void run() { - DataManager.loadBuildingPhotos(instance); - } - }.start(); } public void onResume() { @@ -503,8 +495,7 @@ public class MapActivity extends OrmLiteBaseActivity implements if (scaleBarOverlay != null) { Log.v(TAG, "ScaleBarOverlay is already created"); } else { - if (pastOverlays != null - && (scaleBarOverlay = (ScaleBarOverlay) pastOverlays.get(SCALE_BAR_OVERLAY)) != null) { + if (pastOverlays != null && (scaleBarOverlay = (ScaleBarOverlay) pastOverlays.get(SCALE_BAR_OVERLAY)) != null) { Log.i(TAG, "Finished restoring utility overlays " + (System.currentTimeMillis() - startTime)); } else { scaleBarOverlay = new ScaleBarOverlay(instance); @@ -533,8 +524,7 @@ public class MapActivity extends OrmLiteBaseActivity implements 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)); + Log.i(TAG, "Begining showing route " + route.code + " overlay at " + (System.currentTimeMillis() - startTime)); final SharedPreferences activityPrefs = getPreferences(0); final OverlayRankComparator comparator = new OverlayRankComparator(getPreferences(0)); @@ -559,8 +549,8 @@ public class MapActivity extends OrmLiteBaseActivity implements // TODO Is this a route like U1N or, something else, // this hack works somewhat for now? - PathOverlay routeOverlayU1E = DataManager.getRoutePath( - getResources().openRawResource(R.raw.u1e), colour, mResourceProxy); + PathOverlay routeOverlayU1E = DataManager.getRoutePath(getResources().openRawResource(R.raw.u1e), + colour, mResourceProxy); routeOverlayU1E.getPaint().setAntiAlias(true); routeOverlayU1E.getPaint().setAlpha(145); routeOverlayU1E.getPaint().setStrokeWidth(12); @@ -613,8 +603,7 @@ public class MapActivity extends OrmLiteBaseActivity implements mapView.postInvalidate(); - Log.i(TAG, "Finished showing route " + route.code + " overlay at " - + (System.currentTimeMillis() - startTime)); + Log.i(TAG, "Finished showing route " + route.code + " overlay at " + (System.currentTimeMillis() - startTime)); } }).start(); } @@ -623,8 +612,7 @@ public class MapActivity extends OrmLiteBaseActivity implements new Thread(new Runnable() { public void run() { - Log.i(TAG, "Begining showing site " + site.name + " overlay at " - + (System.currentTimeMillis() - startTime)); + Log.i(TAG, "Begining showing site " + site.name + " overlay at " + (System.currentTimeMillis() - startTime)); final SharedPreferences activityPrefs = getPreferences(0); final OverlayRankComparator comparator = new OverlayRankComparator(getPreferences(0)); @@ -633,8 +621,7 @@ public class MapActivity extends OrmLiteBaseActivity implements if ((siteOverlay = siteOverlays.get(site)) != null) { } else { - if (pastOverlays != null - && (siteOverlay = (PathOverlay) pastOverlays.get(SITE_OVERLAYS + site.name)) != null) { + if (pastOverlays != null && (siteOverlay = (PathOverlay) pastOverlays.get(SITE_OVERLAYS + site.name)) != null) { Log.i(TAG, "Restored " + site.name + " site overlay"); } else { @@ -660,13 +647,11 @@ public class MapActivity extends OrmLiteBaseActivity implements } } - siteOverlay.setEnabled(activityPrefs.getBoolean(SITE_OVERLAYS + site.name, - SITE_OVERLAYS_ENABLED_BY_DEFAULT)); + siteOverlay.setEnabled(activityPrefs.getBoolean(SITE_OVERLAYS + site.name, SITE_OVERLAYS_ENABLED_BY_DEFAULT)); mapView.postInvalidate(); - Log.i(TAG, "Finished showing site " + site.name + " overlay at " - + (System.currentTimeMillis() - startTime)); + Log.i(TAG, "Finished showing site " + site.name + " overlay at " + (System.currentTimeMillis() - startTime)); } }).start(); } @@ -683,10 +668,8 @@ public class MapActivity extends OrmLiteBaseActivity implements } else { if (pastOverlays != null - && (residentialBuildingOverlay = (BuildingNumOverlay) pastOverlays - .get(RESIDENTIAL_BUILDING_OVERLAY)) != null) { - nonResidentialBuildingOverlay = (BuildingNumOverlay) pastOverlays - .get(NON_RESIDENTIAL_BUILDING_OVERLAY); + && (residentialBuildingOverlay = (BuildingNumOverlay) pastOverlays.get(RESIDENTIAL_BUILDING_OVERLAY)) != null) { + nonResidentialBuildingOverlay = (BuildingNumOverlay) pastOverlays.get(NON_RESIDENTIAL_BUILDING_OVERLAY); Log.i(TAG, "Restored building overlays"); } else { @@ -701,15 +684,17 @@ public class MapActivity extends OrmLiteBaseActivity implements buildingDao = getHelper().getBuildingDao(); + final SharedPreferences favouritesPrefs = getSharedPreferences(FAVOURITES_PREFERENCES, 0); + for (Building building : buildingDao) { if (building.residential == true) { - if (building.favourite) { + if (favouritesPrefs.getBoolean(building.id, false)) { residentialBuildings.add(building); } else { residentialBuildings.add(0, building); } } else { - if (building.favourite) { + if (favouritesPrefs.getBoolean(building.id, false)) { nonResidentialBuildings.add(building); } else { nonResidentialBuildings.add(0, building); @@ -737,8 +722,8 @@ public class MapActivity extends OrmLiteBaseActivity implements } } - residentialBuildingOverlay.setEnabled(activityPrefs.getBoolean(BUILDING_OVERLAYS - + RESIDENTIAL_BUILDING_OVERLAY, RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT)); + residentialBuildingOverlay.setEnabled(activityPrefs.getBoolean(BUILDING_OVERLAYS + RESIDENTIAL_BUILDING_OVERLAY, + RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT)); nonResidentialBuildingOverlay.setEnabled(activityPrefs.getBoolean(BUILDING_OVERLAYS + NON_RESIDENTIAL_BUILDING_OVERLAY, NON_RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT)); @@ -758,16 +743,11 @@ public class MapActivity extends OrmLiteBaseActivity implements final OverlayRankComparator comparator = new OverlayRankComparator(getPreferences(0)); if (uniLinkBusStopOverlay != null) { - if (!activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)) { + if (!activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)) { Log.i(TAG, "Uni-Link bus stop overlay not needed"); overlays.remove(UNI_LINK_BUS_STOP_OVERLAY); @@ -778,31 +758,25 @@ public class MapActivity extends OrmLiteBaseActivity implements uniLinkBusStopOverlay = null; } else { - uniLinkBusStopOverlay.setRoutes(0, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(1, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(2, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(3, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(4, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(0, + activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(1, + activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(2, + activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(3, + activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(4, + activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); } } else { - if (activityPrefs - .getBoolean(BUS_STOP_OVERLAYS + "U1", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)) { + if (activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)) { if (pastOverlays != null - && (uniLinkBusStopOverlay = (BusStopOverlay) pastOverlays - .get(UNI_LINK_BUS_STOP_OVERLAY)) != null) { + && (uniLinkBusStopOverlay = (BusStopOverlay) pastOverlays.get(UNI_LINK_BUS_STOP_OVERLAY)) != null) { Log.i(TAG, "Restored Uni-Link bus stop overlay"); } else { @@ -820,16 +794,16 @@ public class MapActivity extends OrmLiteBaseActivity implements } } - uniLinkBusStopOverlay.setRoutes(0, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(1, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(2, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(3, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(4, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(0, + activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(1, + activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(2, + activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(3, + activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(4, + activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); overlays.put(UNI_LINK_BUS_STOP_OVERLAY, uniLinkBusStopOverlay); @@ -852,8 +826,7 @@ public class MapActivity extends OrmLiteBaseActivity implements private void showNonUniLinkBusStopOverlay() { new Thread(new Runnable() { public void run() { - Log.i(TAG, "Begining showing non uni link bus stop overlays at " - + (System.currentTimeMillis() - startTime)); + Log.i(TAG, "Begining showing non uni link bus stop overlays at " + (System.currentTimeMillis() - startTime)); // final SharedPreferences activityPrefs = getPreferences(0); final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(instance); @@ -870,14 +843,12 @@ public class MapActivity extends OrmLiteBaseActivity implements } else if (sharedPrefs.getBoolean(NON_UNI_LINK_BUS_STOPS_OVERLAY, NON_UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)) { if (pastOverlays != null - && (nonUniLinkBusStopOverlay = (BusStopOverlay) pastOverlays - .get(NON_UNI_LINK_BUS_STOPS_OVERLAY)) != null) { + && (nonUniLinkBusStopOverlay = (BusStopOverlay) pastOverlays.get(NON_UNI_LINK_BUS_STOPS_OVERLAY)) != null) { Log.i(TAG, "Restored non Uni-Link bus stop overlays"); } else { try { List busStops; - Log.v(TAG, "Begin fetching non Uni-Link 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); @@ -907,8 +878,7 @@ public class MapActivity extends OrmLiteBaseActivity implements mapView.postInvalidate(); - Log.i(TAG, "Finished showing non Uni-Link bus stop overlays at " - + (System.currentTimeMillis() - startTime)); + Log.i(TAG, "Finished showing non Uni-Link bus stop overlays at " + (System.currentTimeMillis() - startTime)); } }).start(); } @@ -959,9 +929,8 @@ public class MapActivity extends OrmLiteBaseActivity implements }; AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setMessage("GPS is not enabled, do you wish to enable it?") - .setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener) - .show(); + builder.setMessage("GPS is not enabled, do you wish to enable it?").setPositiveButton("Yes", dialogClickListener) + .setNegativeButton("No", dialogClickListener).show(); } return true; @@ -1056,49 +1025,13 @@ public class MapActivity extends OrmLiteBaseActivity implements } } else { Log.i(TAG, "Got null poi id"); - - // mapController.setZoom(15); - // mapController.setCenter(new GeoPoint(50935551, - // -1393488)); - } - - // This handles the possible change in favourite state caused by - // the user within the BusTimeActivity - try { - String busStopID = bundle.getString("busStopChanged"); - if (busStopID != null && busStopID.length() != 0) { - Log.v(TAG, "Got a busStop id back from the BusTimeActivity " + busStopID); - BusStop busStop = getHelper().getBusStopDao().queryForId(busStopID); - - if (busStop.uniLink) { - uniLinkBusStopOverlay.refresh(busStop); // This does - // not - // invalidate - // the map, - // but it - // seems to - // make the changes appear - } else { - nonUniLinkBusStopOverlay.refresh(busStop); // This - // does - // not - // invalidate - // the - // map, - // but - // it - // seems - // to - // make the changes appear - } - } - } catch (SQLException e) { - e.printStackTrace(); } if (favDialog != null) { refreshFavouriteDialog(); } + + mapView.invalidate(); } } diff --git a/src/net/cbaines/suma/POI.java b/src/net/cbaines/suma/POI.java index 244198d..e815731 100644 --- a/src/net/cbaines/suma/POI.java +++ b/src/net/cbaines/suma/POI.java @@ -48,9 +48,6 @@ public abstract class POI { @DatabaseField(id = true) public String id; - @DatabaseField(canBeNull = false) - public boolean favourite; // This field is not assessed by equals - public int distTo = -1; // Used by the comparator to store distances, then later by the gui to display them. public String type; diff --git a/src/net/cbaines/suma/POIFavouriteComparator.java b/src/net/cbaines/suma/POIFavouriteComparator.java index 94148d4..341ff4c 100644 --- a/src/net/cbaines/suma/POIFavouriteComparator.java +++ b/src/net/cbaines/suma/POIFavouriteComparator.java @@ -21,20 +21,28 @@ package net.cbaines.suma; import java.util.Comparator; +import android.content.SharedPreferences; + public class POIFavouriteComparator implements Comparator { - public int compare(POI poi1, POI poi2) { - if (poi1.favourite) { - if (poi2.favourite) { - return 0; - } else { - return 1; - } - } else if (poi2.favourite) { - return -1; - } else { - return 0; + SharedPreferences favourites; + + POIFavouriteComparator(SharedPreferences favourites) { + this.favourites = favourites; + } + + public int compare(POI poi1, POI poi2) { + if (favourites.getBoolean(poi1.id, false)) { + if (favourites.getBoolean(poi2.id, false)) { + return 0; + } else { + return 1; + } + } else if (favourites.getBoolean(poi2.id, false)) { + return -1; + } else { + return 0; + } } - } } diff --git a/src/net/cbaines/suma/Preferences.java b/src/net/cbaines/suma/Preferences.java index a978dd9..84e1d27 100644 --- a/src/net/cbaines/suma/Preferences.java +++ b/src/net/cbaines/suma/Preferences.java @@ -10,4 +10,6 @@ public interface Preferences { static final boolean NON_UNI_LINK_BUS_TIMES_ENABLED_BY_DEFAULT = false; static final String NON_UNI_LINK_BUS_STOPS_OVERLAY = "nonUniLinkBusStops"; static final boolean NON_UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT = false; + + static final String FAVOURITES_PREFERENCES = "favourites"; } -- cgit v1.2.3 From 81c21ccf040f6c050525de99a8a8078bd8ef03ac Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Sun, 26 Feb 2012 18:16:40 +0000 Subject: Move to version 0.4 --- src/net/cbaines/suma/BuildingActivity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/net/cbaines/suma/BuildingActivity.java b/src/net/cbaines/suma/BuildingActivity.java index 45babbc..6b03164 100644 --- a/src/net/cbaines/suma/BuildingActivity.java +++ b/src/net/cbaines/suma/BuildingActivity.java @@ -72,7 +72,7 @@ public class BuildingActivity extends OrmLiteBaseActivity implem favouritesCheckBox.setOnCheckedChangeListener(this); buildingName = (TextView) findViewById(R.id.buildingActivityName); - buildingID = (TextView) findViewById(R.id.buildingActivityName); + buildingID = (TextView) findViewById(R.id.buildingActivityID); Log.i(TAG, "Building id " + ID); -- cgit v1.2.3 From b640f758e603305a3c32c283c9237dbe47f2cdee Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Sun, 26 Feb 2012 19:48:31 +0000 Subject: Fixed another bug to do with a unreactive bus stop, improved the U1 route overlay. --- src/net/cbaines/suma/BusStopOverlay.java | 4 +- src/net/cbaines/suma/DataManager.java | 112 +++++++++++-------------------- src/net/cbaines/suma/DatabaseHelper.java | 2 +- src/net/cbaines/suma/MapActivity.java | 27 ++++---- 4 files changed, 57 insertions(+), 88 deletions(-) (limited to 'src') diff --git a/src/net/cbaines/suma/BusStopOverlay.java b/src/net/cbaines/suma/BusStopOverlay.java index 176bd4f..7894be2 100644 --- a/src/net/cbaines/suma/BusStopOverlay.java +++ b/src/net/cbaines/suma/BusStopOverlay.java @@ -274,6 +274,8 @@ public class BusStopOverlay extends Overlay implements RouteColorConstants, Pref } private BusStop getSelectedItem(final MotionEvent event, final MapView mapView) { + Log.i(TAG, "Getting selected item"); + final Projection pj = mapView.getProjection(); final int eventX = (int) event.getX(); final int eventY = (int) event.getY(); @@ -281,7 +283,7 @@ public class BusStopOverlay extends Overlay implements RouteColorConstants, Pref /* These objects are created to avoid construct new ones every cycle. */ pj.fromMapPixels(eventX, eventY, mTouchScreenPoint); - for (int i = busStops.size() - 1; i > 0; i--) { + for (int i = busStops.size() - 1; i >= 0; i--) { BusStop busStop = busStops.get(i); pj.toPixels(busStop.point, mItemPoint); diff --git a/src/net/cbaines/suma/DataManager.java b/src/net/cbaines/suma/DataManager.java index fbede76..2234328 100644 --- a/src/net/cbaines/suma/DataManager.java +++ b/src/net/cbaines/suma/DataManager.java @@ -137,13 +137,9 @@ public class DataManager { /* * Polygon poly = buildingPolys.get(dataBits[1]); * - * if (poly != null) { bdg.outline = poly; // Log.i(TAG, - * "Adding building " + key + " " + - * bdg.point.getLatitudeE6() + " " + - * bdg.point.getLongitudeE6() + " " + poly); } else { // - * Log.i(TAG, "Adding building " + key + " " + - * bdg.point.getLatitudeE6() + " " + - * bdg.point.getLongitudeE6()); } + * if (poly != null) { bdg.outline = poly; // Log.i(TAG, "Adding building " + key + " " + + * bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6() + " " + poly); } else { // Log.i(TAG, + * "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6()); } */ // Log.i(TAG, "Creating building " + bdg.id + " " + bdg.name @@ -167,13 +163,9 @@ public class DataManager { /* * Polygon poly = buildingPolys.get(dataBits[1]); * - * if (poly != null) { bdg.outline = poly; // Log.i(TAG, - * "Adding building " + key + " " + - * bdg.point.getLatitudeE6() + " " + - * bdg.point.getLongitudeE6() + " " + poly); } else { // - * Log.i(TAG, "Adding building " + key + " " + - * bdg.point.getLatitudeE6() + " " + - * bdg.point.getLongitudeE6()); } + * if (poly != null) { bdg.outline = poly; // Log.i(TAG, "Adding building " + key + " " + + * bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6() + " " + poly); } else { // Log.i(TAG, + * "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6()); } */ // Log.i(TAG, "Creating building " + bdg.id + " " + bdg.name @@ -192,17 +184,13 @@ public class DataManager { } /* - * for (Iterator iter = buildingPoints.keySet().iterator(); - * iter.hasNext();) { String key = iter.next(); + * for (Iterator iter = buildingPoints.keySet().iterator(); iter.hasNext();) { String key = iter.next(); * - * Building bdg = new Building(key, buildingPoints.get(key), false); - * Polygon poly = buildingPolys.get(key); + * Building bdg = new Building(key, buildingPoints.get(key), false); Polygon poly = buildingPolys.get(key); * - * if (poly != null) { bdg.outline = poly; // Log.i(TAG, - * "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + - * bdg.point.getLongitudeE6() + " " + poly); } else { // Log.i(TAG, - * "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + - * bdg.point.getLongitudeE6()); } + * if (poly != null) { bdg.outline = poly; // Log.i(TAG, "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + * + bdg.point.getLongitudeE6() + " " + poly); } else { // Log.i(TAG, "Adding building " + key + " " + + * bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6()); } * * buildingDao.create(bdg); } */ @@ -239,18 +227,18 @@ public class DataManager { // Log.i(TAG, "Whole " + dataBits[3] + " First bit " + // quBitsLat[0] + " last bit " + quBitsLat[1]); - double lat = Double.valueOf(quBitsLat[0]) - + Double.valueOf(quBitsLat[1].substring(0, quBitsLat[1].length() - 1)) / 60d; + double lat = Double.valueOf(quBitsLat[0]) + Double.valueOf(quBitsLat[1].substring(0, quBitsLat[1].length() - 1)) + / 60d; // Log.i(TAG, "Whole " + dataBits[4] + " First bit " + // quBitsLng[0] + " last bit " + quBitsLng[1]); - double lng = Double.valueOf(quBitsLng[0]) - + Double.valueOf(quBitsLng[1].substring(0, quBitsLng[1].length() - 1)) / 60d; + double lng = Double.valueOf(quBitsLng[0]) + Double.valueOf(quBitsLng[1].substring(0, quBitsLng[1].length() - 1)) + / 60d; GeoPoint point = new GeoPoint((int) (lat * 1e6), (int) (lng * -1e6)); // Log.i(TAG, "Lat " + point.getLatitudeE6() + " lng " + // point.getLongitudeE6()); - busStopDao.create(new BusStop(dataBits[0].replace("\"", ""), dataBits[1].replace("\"", ""), dataBits[2] - .replace("\"", ""), point)); + busStopDao.create(new BusStop(dataBits[0].replace("\"", ""), dataBits[1].replace("\"", ""), dataBits[2].replace( + "\"", ""), point)); } @@ -359,10 +347,6 @@ public class DataManager { busStop.uniLink = true; } - if ((busStop.routes != 0) != busStop.uniLink || busStop.id.equals("SN120553")) { - Log.e(TAG, busStop.id + " " + busStop.routes + " " + busStop.uniLink); - } - // Log.v(TAG, "Stop routes " + busStop.routes); busStopDao.update(busStop); @@ -375,36 +359,26 @@ public class DataManager { } /* - * for (Iterator busStopIter = busStopDao.iterator(); - * busStopIter.hasNext();) { BusStop stop = busStopIter.next(); // - * Log.i(TAG, "Looking at stop " + stop.id); + * for (Iterator busStopIter = busStopDao.iterator(); busStopIter.hasNext();) { BusStop stop = + * busStopIter.next(); // Log.i(TAG, "Looking at stop " + stop.id); * * - * QueryBuilder routeStopsQueryBuilder = - * routeStopsDao.queryBuilder(); + * QueryBuilder routeStopsQueryBuilder = routeStopsDao.queryBuilder(); * routeStopsQueryBuilder.where().eq(columnName, value) * - * DeleteBuilder deleteBuilder = - * busStopDao.deleteBuilder(); // only delete the rows where password is - * null deleteBuilder.where().in(RouteStops.STOP_ID_FIELD_NAME, objects) - * accountDao.delete(deleteBuilder.prepare()); + * DeleteBuilder deleteBuilder = busStopDao.deleteBuilder(); // only delete the rows where password is + * null deleteBuilder.where().in(RouteStops.STOP_ID_FIELD_NAME, objects) accountDao.delete(deleteBuilder.prepare()); * * - * QueryBuilder routeStopsQueryBuilder = - * routeStopsDao.queryBuilder(); - * routeStopsQueryBuilder.setCountOf(true); - * routeStopsQueryBuilder.where().eq(RouteStops.STOP_ID_FIELD_NAME, - * stop); + * QueryBuilder routeStopsQueryBuilder = routeStopsDao.queryBuilder(); + * routeStopsQueryBuilder.setCountOf(true); routeStopsQueryBuilder.where().eq(RouteStops.STOP_ID_FIELD_NAME, stop); * - * PreparedQuery routeStopsPreparedQuery = - * routeStopsQueryBuilder.prepare(); List routeStops = - * routeStopsDao.query(routeStopsPreparedQuery); // long num = - * routeStopsDao.query(routeStopsPreparedQuery).size(); // Log.i(TAG, - * "Number is " + num); + * PreparedQuery routeStopsPreparedQuery = routeStopsQueryBuilder.prepare(); List routeStops = + * routeStopsDao.query(routeStopsPreparedQuery); // long num = routeStopsDao.query(routeStopsPreparedQuery).size(); // + * Log.i(TAG, "Number is " + num); * - * stop.uniLink = false; for (RouteStops routeStop : routeStops) { if - * (routeStop.busRoute.uniLink) { stop.uniLink = true; } } - * busStopDao.update(stop); } + * stop.uniLink = false; for (RouteStops routeStop : routeStops) { if (routeStop.busRoute.uniLink) { stop.uniLink = true; + * } } busStopDao.update(stop); } */ Log.i(TAG, "Finished loading bus data"); @@ -454,8 +428,8 @@ public class DataManager { Log.i(TAG, "Loaded sites from csv"); } - private static Stop getStop(Context context, JSONObject stopObj, Set routes, BusStop busStop) - throws SQLException, JSONException { + private static Stop getStop(Context context, JSONObject stopObj, Set routes, BusStop busStop) throws SQLException, + JSONException { if (helper == null) helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); @@ -504,13 +478,12 @@ public class DataManager { route = tempRoute; dir = null; } - } else { - if (tempRoute == null) { - Log.e(TAG, "tempRoute == null"); - } - if (tempRoute.code == null) { - Log.e(TAG, "tempRoute.code == null"); + } else if (name.equals("U1E")) { + if (tempRoute.code.equals("U1")) { + route = tempRoute; + dir = "E"; } + } else { if (tempRoute.code.equals(name.substring(0, 2))) { route = tempRoute; if (route.forwardDirection.equals(name.substring(2))) { @@ -580,11 +553,9 @@ public class DataManager { List routeStops = routeStopsDao.query(routeStopsPreparedQuery); if (routeStops.size() > 0) { - Log.i(TAG, "Found " + routeStops.size() + " stops matching the destStop " + destStop + " on route " - + route.code); + Log.i(TAG, "Found " + routeStops.size() + " stops matching the destStop " + destStop + " on route " + route.code); } else { - Log.w(TAG, "Found " + routeStops.size() + " stops matching the destStop " + destStop + " on route " - + route.code); + Log.w(TAG, "Found " + routeStops.size() + " stops matching the destStop " + destStop + " on route " + route.code); } } @@ -652,8 +623,7 @@ public class DataManager { if (route != null) { busRoutes.add(route); } else { - throw new RuntimeException("Route not found " + key.substring(key.length() - 3, key.length()) + " " - + key); + throw new RuntimeException("Route not found " + key.substring(key.length() - 3, key.length()) + " " + key); } } @@ -749,8 +719,8 @@ public class DataManager { continue; } - Log.v(TAG, "Found stop for a unidentified " + stop.bus.toString() + " at " + stop.busStop.id - + " at " + stop.arivalTime); + Log.v(TAG, "Found stop for a unidentified " + stop.bus.toString() + " at " + stop.busStop.id + " at " + + stop.arivalTime); timetable.add(stop); diff --git a/src/net/cbaines/suma/DatabaseHelper.java b/src/net/cbaines/suma/DatabaseHelper.java index 2d8f9d2..4563d7c 100644 --- a/src/net/cbaines/suma/DatabaseHelper.java +++ b/src/net/cbaines/suma/DatabaseHelper.java @@ -40,7 +40,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { private static final String DATABASE_PATH = "/data/data/net.cbaines.suma/databases/"; private static final String DATABASE_NAME = "data.db"; - private static final int DATABASE_VERSION = 39; + private static final int DATABASE_VERSION = 40; private static final String TAG = "DatabaseHelper"; diff --git a/src/net/cbaines/suma/MapActivity.java b/src/net/cbaines/suma/MapActivity.java index 33b12cd..14a1dd4 100644 --- a/src/net/cbaines/suma/MapActivity.java +++ b/src/net/cbaines/suma/MapActivity.java @@ -74,10 +74,10 @@ public class MapActivity extends OrmLiteBaseActivity implements OnItemClickListener, OnItemLongClickListener, OnSharedPreferenceChangeListener, Preferences { /** - * Enable to use the database in the assets folder, if its not enabled, the - * database is built from the csv files in the assets folder + * Enable to use the database in the assets folder, if its not enabled, the database is built from the csv files in the assets + * folder */ - private boolean useBundledDatabase = false; + private boolean useBundledDatabase = true; private MapView mapView; private MapController mapController; @@ -173,8 +173,7 @@ public class MapActivity extends OrmLiteBaseActivity implements OTHER_OVERLAY_NAMES }; /** - * The toast for this activity, storing the toast centrally allows it to be - * changed quickly, instead of a queue building up + * The toast for this activity, storing the toast centrally allows it to be changed quickly, instead of a queue building up */ Toast activityToast; @@ -205,10 +204,8 @@ public class MapActivity extends OrmLiteBaseActivity implements pastOverlays = (HashMap) getLastNonConfigurationInstance(); /* - * SensorManager mSensorManager = (SensorManager) - * getSystemService(Context.SENSOR_SERVICE); This code in the following - * constructor causes problems in some emulators, disable sensors to - * fix. + * SensorManager mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); This code in the following + * constructor causes problems in some emulators, disable sensors to fix. */ Log.i(TAG, "Starting creating myLocationOverlay"); myLocationOverlay = new MyLocationOverlay(instance, mapView); @@ -555,10 +552,12 @@ public class MapActivity extends OrmLiteBaseActivity implements routeOverlayU1E.getPaint().setAlpha(145); routeOverlayU1E.getPaint().setStrokeWidth(12); routeOverlayU1E.getPaint().setPathEffect(new DashPathEffect(new float[] { 20, 16 }, 0)); - routeOverlayU1E.setEnabled(activityPrefs.getBoolean("Bus Routes:" + route.code, true)); busRouteOverlays.put(new BusRoute(1000, "U1E", "U1E Route Label", true), routeOverlayU1E); - overlays.put(BUS_ROUTE_OVERLAYS + route.code + "E", routeOverlayU1E); + overlays.put(BUS_ROUTE_OVERLAYS + "U1E", routeOverlayU1E); + synchronized (mapView.getOverlays()) { + mapView.getOverlays().add(routeOverlayU1E); + } } else if (route.code.equals("U1N")) { resource = getResources().openRawResource(R.raw.u1n); colour = U1N; @@ -1038,11 +1037,9 @@ public class MapActivity extends OrmLiteBaseActivity implements } /* - * public boolean onChildClick(ExpandableListView parent, View v, int - * groupPosition, int childPosition, long id) { + * public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { * - * mapView.post(new Runnable() { public void run() { // - * updateEnabledOverlays(); TODO Fix whatever this did? + * mapView.post(new Runnable() { public void run() { // updateEnabledOverlays(); TODO Fix whatever this did? * mapView.invalidate(); } }); * * return true; } -- cgit v1.2.3 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