aboutsummaryrefslogtreecommitdiff
path: root/src/net/cbaines/suma/BusStopActivity.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/net/cbaines/suma/BusStopActivity.java')
-rw-r--r--src/net/cbaines/suma/BusStopActivity.java107
1 files changed, 88 insertions, 19 deletions
diff --git a/src/net/cbaines/suma/BusStopActivity.java b/src/net/cbaines/suma/BusStopActivity.java
index 93f4ae6..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 {
+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("liveBusTimesEnabled", 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();
@@ -237,7 +246,8 @@ public class BusStopActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme
public void onPause() {
if (mHandler != null) { // BusTimes are enabled
mHandler.removeCallbacks(refreshData);
- timetableTask.cancel(true);
+ if (timetableTask != null) // Could happen if the handler has not created the timetableTask yet
+ timetableTask.cancel(true);
Log.i(TAG, "Stoping refreshing timetable data");
}
@@ -264,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);
+ }
}
}
@@ -287,12 +296,16 @@ public class BusStopActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme
protected Timetable doInBackground(String... activity) {
Timetable newTimetable = null;
try {
- newTimetable = DataManager.getTimetable(instance, busStopID, true);
+ 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 = "Insert error message here!";
+ errorMessage = "ClientProtocolException!?!";
e.printStackTrace();
} catch (IOException e) {
errorMessage = "Error fetching bus times from server, are you connected to the internet?";
@@ -300,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;
}
@@ -330,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();
@@ -353,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");
}
@@ -364,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");
}
@@ -441,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();
+ }
+
+ }
}