diff options
Diffstat (limited to 'src/net/cbaines/suma/BusActivity.java')
-rw-r--r-- | src/net/cbaines/suma/BusActivity.java | 117 |
1 files changed, 84 insertions, 33 deletions
diff --git a/src/net/cbaines/suma/BusActivity.java b/src/net/cbaines/suma/BusActivity.java index 6a90d7f..392baf2 100644 --- a/src/net/cbaines/suma/BusActivity.java +++ b/src/net/cbaines/suma/BusActivity.java @@ -2,12 +2,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.List; import org.apache.http.client.ClientProtocolException; +import org.json.JSONArray; import org.json.JSONException; +import org.json.JSONObject; import android.content.Context; import android.content.SharedPreferences; @@ -29,11 +32,11 @@ import com.j256.ormlite.android.apptools.OrmLiteBaseActivity; public class BusActivity extends OrmLiteBaseActivity<DatabaseHelper> implements Preferences { final static String TAG = "BusActivity"; - private CheckBox U1RouteRadioButton; - private CheckBox U1NRouteRadioButton; - private CheckBox U2RouteRadioButton; - private CheckBox U6RouteRadioButton; - private CheckBox U9RouteRadioButton; + private TextView U1RouteRadioButton; + private TextView U1NRouteRadioButton; + private TextView U2RouteRadioButton; + private TextView U6RouteRadioButton; + private TextView U9RouteRadioButton; private Handler handler; private Runnable refreshData; @@ -56,6 +59,10 @@ public class BusActivity extends OrmLiteBaseActivity<DatabaseHelper> implements private Context instance; + private List<BusStop> busStops; + + int num = 15; + public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.bus_activity); @@ -86,17 +93,18 @@ public class BusActivity extends OrmLiteBaseActivity<DatabaseHelper> implements Log.e(TAG, "Found more than one busStop? " + busStopID); } - 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 = (TextView) findViewById(R.id.busActivityU1); + U1NRouteRadioButton = (TextView) findViewById(R.id.busActivityU1N); + U2RouteRadioButton = (TextView) findViewById(R.id.busActivityU2); + U6RouteRadioButton = (TextView) findViewById(R.id.busActivityU6); + U9RouteRadioButton = (TextView) findViewById(R.id.busActivityU9); busIDTextView = (TextView) findViewById(R.id.busActivityBusID); progBar = (ProgressBar) findViewById(R.id.busActivityLoadBar); 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"); @@ -149,6 +157,35 @@ public class BusActivity extends OrmLiteBaseActivity<DatabaseHelper> implements } catch (SQLException e) { e.printStackTrace(); } + + busStops = new ArrayList<BusStop>(num); + busStops.add(busStop); + + BusRoute route = bus.route; + + for (int i = 0; i < num; i++) { + BusStop nextStop = route.moveInRoute(instance, busStops.get(i), bus.direction, 1); + + if (nextStop != null) { + busStops.add(nextStop); + } else { + Log.e(TAG, "nextStop is null"); + } + } + + refreshData = new Runnable() { + @Override + public void run() { + for (int i = 0; i < num; i++) { + if (timetable.get(i).timeOfFetch == null || (timetable.get(i).timeOfFetch.getTime() - System.currentTimeMillis()) > 20000) { + GetTimetableStopTask timetableStopTask = new GetTimetableStopTask(); + BusStop[] str = { busStops.get(i) }; + timetableStopTask.execute(str); + handler.postDelayed(refreshData, 20000); + } + } + } + }; } public void onResume() { @@ -160,27 +197,19 @@ public class BusActivity extends OrmLiteBaseActivity<DatabaseHelper> implements Log.i(TAG, "Live Times enabled"); timetable = (Timetable) getLastNonConfigurationInstance(); - refreshData = new Runnable() { - @Override - public void run() { - GetTimetableStopTask timetableStopTask = new GetTimetableStopTask(); - timetableStopTask.execute(busStopID); - handler.postDelayed(refreshData, 20000); - } - }; - handler = new Handler(); if (timetable == null) { Log.i(TAG, "No Previous timetable"); - handler.post(refreshData); + timetable = new Timetable(); + for (int i = 0; i < num; i++) { + timetable.add(new Stop(bus, busStops.get(i), null, null, false)); + } } else { Log.i(TAG, "Displaying previous timetable"); displayTimetable(timetable); - if (System.currentTimeMillis() - timetable.fetchTime.getTime() > 20000) { - handler.post(refreshData); - } } + handler.post(refreshData); } else { Log.i(TAG, "Live Times Disabled"); @@ -191,39 +220,61 @@ public class BusActivity extends OrmLiteBaseActivity<DatabaseHelper> implements } - private class GetTimetableStopTask extends AsyncTask<String, Integer, Timetable> { + public void onPause() { + if (handler != null) { // BusTimes are enabled + handler.removeCallbacks(refreshData); + if (timetableStopTasks != null) { // Could happen if the handler has not created the timetableTask yet + for (GetTimetableStopTask task : timetableStopTasks) { + if (task != null) { + task.cancel(true); + } + } + } + Log.i(TAG, "Stoping refreshing timetable data"); + } + + super.onPause(); + } + + private class GetTimetableStopTask extends AsyncTask<BusStop, Integer, Timetable> { String errorMessage; protected void onPreExecute() { progBar.setVisibility(View.VISIBLE); } - protected Timetable doInBackground(String... busStopID) { - Timetable newTimetable = null; + protected Timetable doInBackground(BusStop... busStop) { + Timetable newTimetable = timetable; try { - final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(instance); - - newTimetable = DataManager.getTimetable(instance, bus, busStop, 10); + Log.i(TAG, "Fetching stop for busStop " + busStops.indexOf(busStop[0])); + Stop stop = DataManager.getStop(instance, bus, busStop[0]); + if (stop == null) { + stop = new Stop(bus, busStop[0], null, null, false); + } + Log.i(TAG, "Finished fetching stop for busStop " + busStops.indexOf(busStop[0])); + newTimetable.set(busStops.indexOf(busStop[0]), stop); } catch (SQLException e) { errorMessage = "Error message regarding SQL?"; e.printStackTrace(); + newTimetable = null; } catch (ClientProtocolException e) { errorMessage = "ClientProtocolException!?!"; e.printStackTrace(); + newTimetable = null; } catch (IOException e) { errorMessage = "Error fetching bus times from server, are you connected to the internet?"; e.printStackTrace(); + newTimetable = null; } catch (JSONException e) { errorMessage = "Error parsing bus times"; e.printStackTrace(); - } catch (Exception e) { - Log.e(TAG, e.getMessage(), e.getCause()); + newTimetable = null; } return newTimetable; } protected void onPostExecute(Timetable newTimetable) { - Log.i(TAG, "Got timetable"); + // Log.i(TAG, "Got timetable"); if (newTimetable == null) { Log.i(TAG, "Its null"); @@ -241,7 +292,7 @@ public class BusActivity extends OrmLiteBaseActivity<DatabaseHelper> implements private void displayTimetable(Timetable timetable) { visibleTimetable = (Timetable) timetable.clone(); - Log.i(TAG, "It contains " + visibleTimetable.size() + " stops"); + Log.i(TAG, "Displaying timetable, it contains " + visibleTimetable.size() + " stops"); if (timetable.size() == 0) { busContentMessage.setText("No Busses"); |