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.java102
1 files changed, 75 insertions, 27 deletions
diff --git a/src/net/cbaines/suma/BusActivity.java b/src/net/cbaines/suma/BusActivity.java
index f2cf133..834ce2f 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;
@@ -18,6 +19,7 @@ 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;
@@ -57,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);
@@ -119,8 +123,8 @@ public class BusActivity extends ToastHelperActivity implements Preferences {
if (bus.destinationString != null) {
Log.i(TAG, "Bus destination string is " + bus.destinationString);
- busDestTextView.setText(getResources().getString(R.string.bus_activity_destination_label)
- + 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");
@@ -139,26 +143,37 @@ public class BusActivity extends ToastHelperActivity implements Preferences {
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));
}
}
@@ -192,12 +207,13 @@ 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");
} else {
Log.i(TAG, "Displaying previous timetable");
-
}
displayTimetable(timetable);
handler.postDelayed(refreshData, 500);
@@ -225,6 +241,46 @@ 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);
+ }
+ Log.i(TAG, "Stoping refreshing timetable data");
+
+ 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;
@@ -245,7 +301,7 @@ public class BusActivity extends ToastHelperActivity implements Preferences {
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);
} catch (SQLException e) {
@@ -266,16 +322,10 @@ 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);
}
}
@@ -307,9 +357,7 @@ public class BusActivity extends ToastHelperActivity implements Preferences {
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));
+ Log.i(TAG, "Moving to position of " + busStop.description + " which is " + busStops.indexOf(busStop));
timetableView.setSelection(busStops.indexOf(busStop));
}
}