diff options
Diffstat (limited to 'src/net/cbaines/suma/BusStopActivity.java')
-rw-r--r-- | src/net/cbaines/suma/BusStopActivity.java | 104 |
1 files changed, 84 insertions, 20 deletions
diff --git a/src/net/cbaines/suma/BusStopActivity.java b/src/net/cbaines/suma/BusStopActivity.java index b711770..03dcbca 100644 --- a/src/net/cbaines/suma/BusStopActivity.java +++ b/src/net/cbaines/suma/BusStopActivity.java @@ -21,12 +21,15 @@ package net.cbaines.suma; import java.io.IOException; import java.sql.SQLException; +import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; +import java.util.Set; import org.apache.http.client.ClientProtocolException; import org.json.JSONException; +import android.app.Dialog; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; @@ -40,6 +43,8 @@ import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; +import android.widget.AdapterView; +import android.widget.AdapterView.OnItemClickListener; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; @@ -47,13 +52,14 @@ import android.widget.LinearLayout; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.TextView; +import android.widget.Toast; import com.j256.ormlite.android.apptools.OrmLiteBaseActivity; 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 { +public class BusStopActivity extends OrmLiteBaseActivity<DatabaseHelper> implements OnCheckedChangeListener, Preferences, OnItemClickListener { final static String TAG = "BusTimeActivity"; @@ -90,8 +96,13 @@ public class BusStopActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme 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); @@ -163,10 +174,7 @@ public class BusStopActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme } else { U9RouteRadioButton.setVisibility(View.GONE); } - } else { - Log.e(TAG, "Error unknown route " + route.code); } - } busStopDao = helper.getBusStopDao(); @@ -199,7 +207,8 @@ public class BusStopActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme super.onResume(); SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); - if (sharedPrefs.getBoolean(UNI_LINK_BUS_TIMES, false) || sharedPrefs.getBoolean(NON_UNI_LINK_BUS_TIMES, false)) { + 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(); @@ -265,11 +274,10 @@ public class BusStopActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme e.printStackTrace(); } } else { - Log.i(TAG, "Route radio button made " + checked); - - displayTimetable(timetable); - + if (timetable != null) { // If there is a timetable to display + displayTimetable(timetable); + } } } @@ -290,14 +298,14 @@ public class BusStopActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme try { final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(instance); - newTimetable = DataManager.getTimetable(instance, busStopID, sharedPrefs.getBoolean(SouthamptonUniversityMapActivity.UNI_LINK_BUS_TIMES, - SouthamptonUniversityMapActivity.UNI_LINK_BUS_TIMES_ENABLED_BY_DEFAULT), sharedPrefs.getBoolean( - SouthamptonUniversityMapActivity.NON_UNI_LINK_BUS_TIMES, SouthamptonUniversityMapActivity.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(); } catch (ClientProtocolException e) { - errorMessage = "Insert error message here!"; + errorMessage = "ClientProtocolException!?!"; e.printStackTrace(); } catch (IOException e) { errorMessage = "Error fetching bus times from server, are you connected to the internet?"; @@ -305,7 +313,10 @@ public class BusStopActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme } catch (JSONException e) { errorMessage = "Error parsing bus times"; e.printStackTrace(); + } catch (Exception e) { + Log.e(TAG, e.getMessage(), e.getCause()); } + return newTimetable; } @@ -335,19 +346,25 @@ public class BusStopActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle item selection - if (false) { // (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"); - HashSet<BusStop> busStops = new HashSet<BusStop>(); + ArrayList<POI> busStops = new ArrayList<POI>(); for (BusRoute route : routes) { try { - if (false) { // (item.getItemId() == R.id.menu_next_stop) { - busStops.add(route.moveInRoute(this, getHelper().getBusStopDao().queryForId(busStopID), null, 1)); + Set<BusStop> tmpStops; + if (item.getItemId() == R.id.menu_next_stop) { + tmpStops = route.moveInRoute(this, getHelper().getBusStopDao().queryForId(busStopID), 1); } else { - busStops.add(route.moveInRoute(this, getHelper().getBusStopDao().queryForId(busStopID), null, -1)); + 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(); @@ -358,7 +375,7 @@ public class BusStopActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme if (busStops.size() == 1) { Intent i = new Intent(this, BusStopActivity.class); - BusStop stop = busStops.iterator().next(); + BusStop stop = (BusStop) busStops.iterator().next(); if (stop == null) { Log.e(TAG, "stop == null"); } @@ -369,7 +386,16 @@ public class BusStopActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme i.putExtra("busStopName", stop.description); startActivity(i); } else { - // Show dialog + 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"); } @@ -446,4 +472,42 @@ public class BusStopActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme } } } + + @Override + protected Dialog onCreateDialog(int id) { + switch (id) { + case POI_DIALOG_ID: + busDialog = new POIDialog(instance); + busDialog.setOnItemClickListener(this); + return busDialog; + } + 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(); + } + + } } |