diff options
Diffstat (limited to 'src/net/cbaines/suma')
-rw-r--r-- | src/net/cbaines/suma/BusActivity.java | 576 | ||||
-rw-r--r-- | src/net/cbaines/suma/BusSpecificStopView.java | 256 | ||||
-rw-r--r-- | src/net/cbaines/suma/BusStopActivity.java | 836 | ||||
-rw-r--r-- | src/net/cbaines/suma/StopView.java | 247 |
4 files changed, 1005 insertions, 910 deletions
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<DatabaseHelper> 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<BusStop> busStops; - - private HashMap<BusStop, GetTimetableStopTask> tasks = new HashMap<BusStop, GetTimetableStopTask>(); - - 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<Bus> 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<BusStop> 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<DatabaseHelper> 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<BusStop> busStops; - if (stop.timeOfFetch == null || (stop.timeOfFetch.getTime() - System.currentTimeMillis()) > 20000) { - if (task != null) { - if (task.getStatus() == AsyncTask.Status.FINISHED) { - task = null; - } + private HashMap<BusStop, GetTimetableStopTask> tasks = new HashMap<BusStop, GetTimetableStopTask>(); + + 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<Bus> 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<BusStop> 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<BusStop, Integer, Stop> { - private String errorMessage; + Log.i(TAG, "Stoping refreshing timetable data"); + } - private BusStop busStop; + super.onPause(); + } - private int position; + private class GetTimetableStopTask extends + AsyncTask<BusStop, Integer, Stop> { + 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<Bus, Integer> 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<Bus, Integer> 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<Bus, Integer> 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<Bus, Integer> 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<DatabaseHelper> implements OnCheckedChangeListener, Preferences, OnItemClickListener { +public class BusStopActivity extends OrmLiteBaseActivity<DatabaseHelper> + 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<BusStop, String> 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<BusRoute> routes = new HashSet<BusRoute>(); - - 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<BusRoute, Integer> busRouteDao = helper.getBusRouteDao(); - Dao<RouteStops, Integer> routeStopsDao = helper.getRouteStopsDao(); - - for (BusRoute route : busRouteDao) { - QueryBuilder<RouteStops, Integer> 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<RouteStops> 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<BusStop, String> 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<BusRoute> routes = new HashSet<BusRoute>(); + + 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<BusRoute, Integer> busRouteDao = helper.getBusRouteDao(); + Dao<RouteStops, Integer> routeStopsDao = helper.getRouteStopsDao(); + + for (BusRoute route : busRouteDao) { + QueryBuilder<RouteStops, Integer> 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<RouteStops> 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, Integer, Timetable> { - 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, Integer, Timetable> { + 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<POI> busStops = new ArrayList<POI>(); + return newTimetable; + } - for (BusRoute route : routes) { - try { - Set<BusStop> 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<POI> busStops = new ArrayList<POI>(); + + for (BusRoute route : routes) { + try { + Set<BusStop> 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<Stop> 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<Stop> 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<Bus, Integer> 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<Bus, Integer> 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<Bus, Integer> busDao = helper.getBusDao(); - Dao<BusRoute, Integer> 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<Bus, Integer> busDao = helper.getBusDao(); + Dao<BusRoute, Integer> 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; - } } |