From 56c74a2fa83916915587ae2de1b46a50f2c856ee Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Tue, 6 Mar 2012 22:10:47 +0000 Subject: Improved Bus activity, and U6 route overlay. --- gen/net/cbaines/suma/R.java | Bin 14743 -> 15019 bytes res/layout/bus_specific_stop_view.xml | 28 ++++++ res/raw/u6.gpx | 36 +++++--- src/net/cbaines/suma/BusActivity.java | 102 +++++++++++++++------ src/net/cbaines/suma/BusSpecificStopView.java | 53 ++++++----- .../cbaines/suma/BusSpecificTimetableAdapter.java | 41 ++++----- src/net/cbaines/suma/StopLoading.java | 11 +++ 7 files changed, 185 insertions(+), 86 deletions(-) create mode 100644 res/layout/bus_specific_stop_view.xml create mode 100644 src/net/cbaines/suma/StopLoading.java diff --git a/gen/net/cbaines/suma/R.java b/gen/net/cbaines/suma/R.java index 3163564..d65abc0 100644 Binary files a/gen/net/cbaines/suma/R.java and b/gen/net/cbaines/suma/R.java differ diff --git a/res/layout/bus_specific_stop_view.xml b/res/layout/bus_specific_stop_view.xml new file mode 100644 index 0000000..e48e2f1 --- /dev/null +++ b/res/layout/bus_specific_stop_view.xml @@ -0,0 +1,28 @@ + + + + + + + + + + \ No newline at end of file diff --git a/res/raw/u6.gpx b/res/raw/u6.gpx index 5356358..c2680e0 100644 --- a/res/raw/u6.gpx +++ b/res/raw/u6.gpx @@ -311,19 +311,21 @@ - + - + - + + + - + - + - + @@ -557,19 +559,27 @@ - + + + + + + + + + - + - + - + - + - + - + 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 { 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)); } } diff --git a/src/net/cbaines/suma/BusSpecificStopView.java b/src/net/cbaines/suma/BusSpecificStopView.java index fdb6f56..92ec8c7 100644 --- a/src/net/cbaines/suma/BusSpecificStopView.java +++ b/src/net/cbaines/suma/BusSpecificStopView.java @@ -28,19 +28,18 @@ import android.content.res.Resources; import android.net.Uri; import android.util.Log; import android.view.Gravity; -import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnLongClickListener; -import android.widget.LinearLayout; import android.widget.ProgressBar; +import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import com.j256.ormlite.android.apptools.OpenHelperManager; import com.j256.ormlite.dao.Dao; -public class BusSpecificStopView extends LinearLayout implements OnClickListener, OnLongClickListener { +public class BusSpecificStopView extends RelativeLayout implements OnClickListener, OnLongClickListener { private static final String TAG = "BusSpecificStopView"; @@ -63,14 +62,12 @@ public class BusSpecificStopView extends LinearLayout implements OnClickListener this.context = context; - this.setOrientation(HORIZONTAL); - location = new TextView(context); location.setTextSize(22f); + location.setGravity(Gravity.LEFT); time = new TextView(context); time.setTextSize(22f); - time.setGravity(Gravity.RIGHT); timeProgress = new ProgressBar(context, null, android.R.attr.progressBarStyleSmall); @@ -80,9 +77,22 @@ public class BusSpecificStopView extends LinearLayout implements OnClickListener setStop(stop); - addView(location, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); - addView(time, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); - addView(timeProgress, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); + RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, + LayoutParams.WRAP_CONTENT); + relativeParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT); + + RelativeLayout.LayoutParams relativeParams2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, + LayoutParams.FILL_PARENT); + relativeParams2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); + + addView(location, relativeParams); + addView(time, relativeParams2); + + // LinearLayout progressLayout = new LinearLayout(context); + // progressLayout.setOrientation(LinearLayout.VERTICAL); + // progressLayout.addView(timeProgress, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); + + addView(timeProgress, relativeParams2); } public void setStop(Stop stop) { @@ -96,14 +106,19 @@ public class BusSpecificStopView extends LinearLayout implements OnClickListener } else { location.setText(stop.busStop.description); // TODO } - if (stop.arivalTime != null) { + + if (stop.arivalTime != null) { // Time available time.setText(stop.getShortTimeToArival()); time.setVisibility(View.VISIBLE); timeProgress.setVisibility(View.GONE); - } else { - time.setVisibility(View.GONE); + } else if (stop instanceof StopLoading) { time.setText(""); + time.setVisibility(View.GONE); timeProgress.setVisibility(View.VISIBLE); + } else { // No time available (yet) and not currently loading + time.setVisibility(View.GONE); + time.setText(""); + timeProgress.setVisibility(View.GONE); } DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); @@ -122,19 +137,7 @@ public class BusSpecificStopView extends LinearLayout implements OnClickListener + DateFormat.getTimeInstance(DateFormat.SHORT).format(stop.arivalTime) + " (timetabled)"; } } else { - if (stop.bus.id != null) { - if (stop.live) { - onClickMessage = "Bus " + stop.bus.toString(); - } else { - onClickMessage = "Timetabled bus " + stop.bus.toString(); - } - } else { - if (stop.live) { - onClickMessage = "Unidentified bus (" + stop.bus.getName() + ")"; - } else { - onClickMessage = "Timetabled bus (" + stop.bus.getName() + ")"; - } - } + onClickMessage = stop.busStop.description + " on route, but arival time is not avalible"; } } catch (SQLException e) { e.printStackTrace(); diff --git a/src/net/cbaines/suma/BusSpecificTimetableAdapter.java b/src/net/cbaines/suma/BusSpecificTimetableAdapter.java index 96fe713..7b9a34a 100644 --- a/src/net/cbaines/suma/BusSpecificTimetableAdapter.java +++ b/src/net/cbaines/suma/BusSpecificTimetableAdapter.java @@ -21,16 +21,14 @@ package net.cbaines.suma; import android.view.View; import android.view.ViewGroup; -import android.view.animation.Animation; -import android.view.animation.AnimationUtils; import android.widget.BaseAdapter; public class BusSpecificTimetableAdapter extends BaseAdapter { private final BusActivity context; private Timetable timetable; - private final Animation a; - private boolean[] changed; + // private final Animation a; + // private boolean[] changed; private long timeOfLastForcedUpdate = System.currentTimeMillis(); @@ -39,7 +37,7 @@ public class BusSpecificTimetableAdapter extends BaseAdapter { public BusSpecificTimetableAdapter(BusActivity context, Timetable timetable) { this.context = context; this.timetable = timetable; - this.a = AnimationUtils.loadAnimation(context, R.anim.updated_stop_view); + // this.a = AnimationUtils.loadAnimation(context, R.anim.updated_stop_view); } public View getView(int position, View convertView, ViewGroup parent) { @@ -47,6 +45,7 @@ public class BusSpecificTimetableAdapter extends BaseAdapter { // timetable.get(position)); if (timeOfLastForcedUpdate + 1000 < System.currentTimeMillis()) { + context.handler.removeCallbacks(context.refreshData); context.handler.post(context.refreshData); timeOfLastForcedUpdate = System.currentTimeMillis(); } @@ -59,11 +58,11 @@ public class BusSpecificTimetableAdapter extends BaseAdapter { stopView.setStop(timetable.get(position)); } - if (changed == null || changed[position]) { - // a.reset(); - // stopView.startAnimation(a); - // Log.i(TAG, "Animating it"); - } + // if (changed == null || changed[position]) { + // a.reset(); + // stopView.startAnimation(a); + // Log.i(TAG, "Animating it"); + // } return stopView; } @@ -83,17 +82,17 @@ public class BusSpecificTimetableAdapter extends BaseAdapter { public void updateTimetable(Timetable newTimetable) { // Log.v(TAG, "Old timetable " + timetable); // Log.v(TAG, "Adaptor loading new timetable"); - changed = new boolean[newTimetable.size()]; - for (int i = 0; i < newTimetable.size(); i++) { - if (newTimetable.get(i).arivalTime != null && !timetable.contains(newTimetable.get(i), true)) { - changed[i] = true; - // Log.i(TAG, "Old timetable does not contain: " + - // newTimetable.get(i)); - } else { - // Log.i(TAG, "Old timetable contains: " + newTimetable.get(i)); - changed[i] = false; - } - } + // changed = new boolean[newTimetable.size()]; + // for (int i = 0; i < newTimetable.size(); i++) { + // if (newTimetable.get(i).arivalTime != null && !timetable.contains(newTimetable.get(i), true)) { + // changed[i] = true; + // // Log.i(TAG, "Old timetable does not contain: " + + // // newTimetable.get(i)); + // } else { + // // Log.i(TAG, "Old timetable contains: " + newTimetable.get(i)); + // changed[i] = false; + // } + // } timetable = newTimetable; this.notifyDataSetChanged(); } diff --git a/src/net/cbaines/suma/StopLoading.java b/src/net/cbaines/suma/StopLoading.java new file mode 100644 index 0000000..e8ca8e5 --- /dev/null +++ b/src/net/cbaines/suma/StopLoading.java @@ -0,0 +1,11 @@ +package net.cbaines.suma; + +import java.util.Date; + +public class StopLoading extends Stop { + + public StopLoading(Bus bus, BusStop busStop, Date arivalTime, Date timeOfFetch, boolean live) { + super(bus, busStop, arivalTime, timeOfFetch, live); + } + +} -- cgit v1.2.3