aboutsummaryrefslogtreecommitdiff
path: root/src/net/cbaines/suma/BusActivity.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/net/cbaines/suma/BusActivity.java')
-rw-r--r--src/net/cbaines/suma/BusActivity.java149
1 files changed, 112 insertions, 37 deletions
diff --git a/src/net/cbaines/suma/BusActivity.java b/src/net/cbaines/suma/BusActivity.java
index ae209f4..04fe6cf 100644
--- a/src/net/cbaines/suma/BusActivity.java
+++ b/src/net/cbaines/suma/BusActivity.java
@@ -2,6 +2,7 @@ package net.cbaines.suma;
import java.io.IOException;
import java.sql.SQLException;
+import java.util.Date;
import java.util.HashMap;
import java.util.List;
@@ -16,6 +17,9 @@ import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Gravity;
+import android.view.Menu;
+import android.view.MenuInflater;
+import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
@@ -55,6 +59,8 @@ public class BusActivity extends ToastHelperActivity implements Preferences {
Handler handler;
+ long timeOfLastRefresh = 0;
+
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bus_activity);
@@ -107,7 +113,7 @@ public class BusActivity extends ToastHelperActivity implements Preferences {
timetableView = (ListView) findViewById(R.id.busActivityTimes);
if (bus.id != null) {
- Log.i(TAG, "Bus id is not null (" + bus.id + ") setting busIDTextView");
+ // 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?");
@@ -116,45 +122,68 @@ public class BusActivity extends ToastHelperActivity implements Preferences {
}
if (bus.destinationString != null) {
- Log.i(TAG, "Bus destination string is " + bus.destinationString);
+ // Log.i(TAG, "Bus destination string is " + bus.destinationString);
busDestTextView
.setText(getResources().getString(R.string.bus_activity_destination_label) + bus.destinationString);
busDestTextView.setVisibility(View.VISIBLE);
} else {
- Log.i(TAG, "Bus destination string is null");
+ // Log.i(TAG, "Bus destination string is null");
busDestTextView.setText(getResources().getString(R.string.bus_activity_no_destination_message));
busDestTextView.setVisibility(View.VISIBLE);
}
busStops = bus.route.getRouteSection(instance, bus.direction);
- Log.i(TAG, "Got " + busStops.size() + " bus stops for this bus");
+ // Log.i(TAG, "Got " + busStops.size() + " bus stops for this bus");
if (bus.destination != null) {
Log.i(TAG, "Bus destination is " + bus.destination);
+
+ int index = busStops.indexOf(bus.destination);
+ if (index != -1) {
+ Log.v(TAG, "Found destination stop in busStops");
+ for (int i = index + 1; i < busStops.size(); i++) {
+ Log.v(TAG, "Removing " + busStops.get(i));
+ busStops.remove(i);
+ }
+ }
} else {
Log.i(TAG, "Bus destination is null");
}
refreshData = new Runnable() {
public void run() {
- for (int num = timetableView.getFirstVisiblePosition(); num < timetableView.getLastVisiblePosition(); num++) {
- Stop stop = timetable.get(num);
+ Log.v(TAG, "Refreshing data " + (System.currentTimeMillis() - timeOfLastRefresh));
+ timeOfLastRefresh = System.currentTimeMillis();
+ for (int num = timetableView.getFirstVisiblePosition(); num <= timetableView.getLastVisiblePosition(); num++) {
+ final Stop stop = timetable.get(num);
- GetTimetableStopTask task = tasks.get(busStops.get(num));
+ if (System.currentTimeMillis() - stop.timeOfFetch.getTime() > 20000) {
+ 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) { // If there is a taks
+ if (task.getStatus() == AsyncTask.Status.FINISHED) { // If its finished
+ task = null; // Delete it
}
}
- if (task == null) {
+ if (task == null) { // If there is now no task
+ // Log.v(TAG, "Updating " + busStops.get(num));
+
+ // Remove the old time from the timetable
+ synchronized (timetable) {
+ timetable.set(num,
+ new StopLoading(bus, busStops.get(num), null, new Date(
+ System.currentTimeMillis() - 21000), false));
+ displayTimetable(timetable);
+ }
+
task = new GetTimetableStopTask();
BusStop[] str = { stop.busStop };
task.execute(str);
tasks.put(stop.busStop, task);
}
+ } else {
+ // Log.v(TAG, "Not updating " + busStops.get(num));
}
}
@@ -166,13 +195,20 @@ public class BusActivity extends ToastHelperActivity implements Preferences {
}
}
+ @Override
+ public boolean onCreateOptionsMenu(Menu menu) {
+ MenuInflater inflater = getMenuInflater();
+ inflater.inflate(R.menu.bus_menu, menu);
+ return true;
+ }
+
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");
+ // Log.i(TAG, "Live Times enabled");
timetable = (Timetable) getLastNonConfigurationInstance();
handler = new Handler();
@@ -181,14 +217,19 @@ public class BusActivity extends ToastHelperActivity implements Preferences {
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));
+ // Add a loading stop, with a fetch time such that it will be fetched
+ timetable
+ .add(new StopLoading(bus, busStops.get(i), null, new Date(System.currentTimeMillis() - 21000), false));
}
- Log.v(TAG, "Finished adding placeholder stops");
+ // Log.v(TAG, "Finished adding placeholder stops");
} else {
Log.i(TAG, "Displaying previous timetable");
-
}
displayTimetable(timetable);
+ if (busStop != null) {
+ Log.i(TAG, "Moving to position of " + busStop.description + " which is " + busStops.indexOf(busStop));
+ timetableView.setSelection(busStops.indexOf(busStop));
+ }
handler.postDelayed(refreshData, 500);
} else {
@@ -214,8 +255,47 @@ public class BusActivity extends ToastHelperActivity implements Preferences {
super.onPause();
}
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+
+ if (item.getItemId() == R.id.menu_refresh_bus) {
+ if (handler != null) { // BusTimes are enabled
+ handler.removeCallbacks(refreshData);
+ for (GetTimetableStopTask task : tasks.values()) {
+ task.cancel(true);
+ }
+
+ for (int num = timetableView.getFirstVisiblePosition(); num <= timetableView.getLastVisiblePosition(); num++) {
+ final Stop stop = timetable.get(num);
+
+ // Log.v(TAG, "Updating " + busStops.get(num));
+
+ // Remove the old time from the timetable
+ synchronized (timetable) {
+ timetable.set(num, new StopLoading(bus, busStops.get(num), null, new Date(
+ System.currentTimeMillis() - 21000), false));
+ displayTimetable(timetable);
+ }
+
+ GetTimetableStopTask task = new GetTimetableStopTask();
+ BusStop[] str = { stop.busStop };
+ task.execute(str);
+ tasks.put(stop.busStop, task);
+ }
+
+ handler.postDelayed(refreshData, 50000);
+ } else {
+ // TODO: Toast here...
+ }
+ } else {
+ Log.e(TAG, "No known menu option selected");
+ return super.onOptionsItemSelected(item);
+ }
+ return true;
+ }
+
private class GetTimetableStopTask extends AsyncTask<BusStop, Integer, Stop> {
- private String errorMessage;
+ // private String errorMessage;
private BusStop busStop;
@@ -231,23 +311,23 @@ public class BusActivity extends ToastHelperActivity implements Preferences {
Stop stop = null;
try {
- Log.i(TAG, "Fetching stop for busStop " + position);
+ // 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);
+ stop = new Stop(bus, busStop, null, new Date(System.currentTimeMillis()), false);
}
- Log.i(TAG, "Finished fetching stop for busStop " + position);
+ // Log.i(TAG, "Finished fetching stop for busStop " + position);
} catch (SQLException e) {
- errorMessage = "Error message regarding SQL?";
+ // errorMessage = "Error message regarding SQL?";
e.printStackTrace();
} catch (ClientProtocolException e) {
- errorMessage = "ClientProtocolException!?!";
+ // errorMessage = "ClientProtocolException!?!";
e.printStackTrace();
} catch (IOException e) {
- errorMessage = "Error fetching bus times from server, are you connected to the internet?";
+ // errorMessage = "Error fetching bus times from server, are you connected to the internet?";
e.printStackTrace();
} catch (JSONException e) {
- errorMessage = "Error parsing bus times";
+ // errorMessage = "Error parsing bus times";
e.printStackTrace();
}
return stop;
@@ -255,21 +335,20 @@ public class BusActivity extends ToastHelperActivity implements Preferences {
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);
- }
+ synchronized (timetable) {
+ timetable.set(position, stop);
+ displayTimetable(timetable);
}
}
}
+ @Override
+ public Object onRetainNonConfigurationInstance() {
+ return timetable;
+ }
+
private void displayTimetable(Timetable timetable) {
visibleTimetable = (Timetable) timetable.clone();
@@ -292,10 +371,6 @@ public class BusActivity extends ToastHelperActivity implements Preferences {
} 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);
}