diff options
Diffstat (limited to 'src/net/cbaines')
-rw-r--r-- | src/net/cbaines/suma/Bus.java | 22 | ||||
-rw-r--r-- | src/net/cbaines/suma/BusStopActivity.java (renamed from src/net/cbaines/suma/BusTimeActivity.java) | 167 | ||||
-rw-r--r-- | src/net/cbaines/suma/BusStopOverlay.java | 2 | ||||
-rw-r--r-- | src/net/cbaines/suma/DatabaseHelper.java | 1 | ||||
-rw-r--r-- | src/net/cbaines/suma/FindActivity.java | 2 | ||||
-rw-r--r-- | src/net/cbaines/suma/SouthamptonUniversityMapActivity.java | 2 | ||||
-rw-r--r-- | src/net/cbaines/suma/Stop.java | 46 | ||||
-rw-r--r-- | src/net/cbaines/suma/Timetable.java | 27 | ||||
-rw-r--r-- | src/net/cbaines/suma/TimetableAdapter.java | 31 |
9 files changed, 231 insertions, 69 deletions
diff --git a/src/net/cbaines/suma/Bus.java b/src/net/cbaines/suma/Bus.java index c73d6d7..ff71df4 100644 --- a/src/net/cbaines/suma/Bus.java +++ b/src/net/cbaines/suma/Bus.java @@ -54,4 +54,26 @@ public class Bus { return String.valueOf(id); } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + id; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Bus other = (Bus) obj; + if (id != other.id) + return false; + return true; + } + } diff --git a/src/net/cbaines/suma/BusTimeActivity.java b/src/net/cbaines/suma/BusStopActivity.java index be8b14a..1495258 100644 --- a/src/net/cbaines/suma/BusTimeActivity.java +++ b/src/net/cbaines/suma/BusStopActivity.java @@ -21,8 +21,11 @@ package net.cbaines.suma; import java.sql.SQLException; +import android.content.Context; import android.content.SharedPreferences; +import android.os.AsyncTask; import android.os.Bundle; +import android.os.Handler; import android.preference.PreferenceManager; import android.util.Log; import android.view.Gravity; @@ -40,7 +43,7 @@ import com.j256.ormlite.dao.Dao; import com.j256.ormlite.stmt.PreparedQuery; import com.j256.ormlite.stmt.QueryBuilder; -public class BusTimeActivity extends OrmLiteBaseActivity<DatabaseHelper> implements Runnable, OnCheckedChangeListener { +public class BusStopActivity extends OrmLiteBaseActivity<DatabaseHelper> implements OnCheckedChangeListener { final static String TAG = "BusTimeActivity"; @@ -54,22 +57,29 @@ public class BusTimeActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme private ProgressBar progBar; private LinearLayout busTimeContentLayout; - private TimetableAdapter adapter; + protected Timetable timetable; - private String busStopID; + protected String busStopID; private String busStopName; private Dao<BusStop, String> busStopDao; private BusStop busStop; - private Thread timetableThread; + private GetTimetableTask timetableTask; + + private Context instance; + + private Handler mHandler; + private Runnable refreshData; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.bustimes); - DatabaseHelper helper = getHelper(); + final DatabaseHelper helper = getHelper(); + + instance = this; busStopID = getIntent().getExtras().getString("busStopID"); busStopName = getIntent().getExtras().getString("busStopName"); @@ -153,12 +163,37 @@ public class BusTimeActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme busName.setText(busStopName); busID.setText(busStopID); + } + + public void onResume() { + super.onResume(); SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); if (sharedPrefs.getBoolean("liveBusTimesEnabled", false)) { - timetableThread = new Thread(this); - timetableThread.start(); + Log.i(TAG, "Live Times enabled"); + timetable = (Timetable) getLastNonConfigurationInstance(); + + refreshData = new Runnable() { + @Override + public void run() { + timetableTask = new GetTimetableTask(); + timetableTask.execute(busStopID); + mHandler.postDelayed(refreshData, 20000); + } + }; + + mHandler = new Handler(); + + if (timetable == null) { + Log.i(TAG, "No Previous timetable"); + mHandler.post(refreshData); + } else { + Log.i(TAG, "Displaying previous timetable"); + displayTimetable(timetable); + } + } else { + Log.i(TAG, "Live Times Disabled"); progBar.setVisibility(View.GONE); busStopMessage.setText("Live bus times disabled"); busStopMessage.setVisibility(View.VISIBLE); @@ -166,12 +201,17 @@ public class BusTimeActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme } - public void finish() { - Log.i(TAG, "Stopping BusTimeActivity thread"); - if (timetableThread != null) { // Could happen if live bus times are disabled - timetableThread.interrupt(); + public void onPause() { + if (mHandler != null) { // BusTimes are enabled + mHandler.removeCallbacks(refreshData); + timetableTask.cancel(true); + Log.i(TAG, "Stoping refreshing timetable data"); } + super.onPause(); + } + + public void finish() { if (dataChanged) { getIntent().putExtra("busStopChanged", busStopID); } @@ -181,67 +221,66 @@ public class BusTimeActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme super.finish(); } - public void run() { - while (true) { - try { - Timetable timetable = DataManager.getTimetable(this, busStopID, true); - - Log.i(TAG, "Got timetable for " + busStopID); - if (timetable == null) { - Log.i(TAG, "Its null"); - busTimeList.post(new Runnable() { - public void run() { - progBar.setVisibility(View.GONE); - busStopMessage.setText("Error fetching bus times"); - busStopMessage.setVisibility(View.VISIBLE); - } - }); - } else { - Log.i(TAG, "It contains " + timetable.size() + " stops"); - - if (timetable.size() == 0) { - busTimeList.post(new Runnable() { - public void run() { - progBar.setVisibility(View.GONE); - busStopMessage.setText("No Busses"); - busStopMessage.setVisibility(View.VISIBLE); - } - }); - } else { - - adapter = new TimetableAdapter(this, timetable); + public void onCheckedChanged(CompoundButton arg0, boolean arg1) { + busStop.favourite = arg1; + try { + busStopDao.update(busStop); + dataChanged = true; + } catch (SQLException e) { + e.printStackTrace(); + } + } - busTimeList.post(new Runnable() { - public void run() { - progBar.setVisibility(View.GONE); - busStopMessage.setVisibility(View.GONE); - busTimeList.setAdapter(adapter); - busTimeContentLayout.setGravity(Gravity.TOP); - } - }); - } - } + @Override + public Object onRetainNonConfigurationInstance() { + return timetable; + } - } catch (SQLException e1) { - e1.printStackTrace(); + private class GetTimetableTask extends AsyncTask<String, Integer, Timetable> { + protected Timetable doInBackground(String... activity) { + Timetable newTimetable = null; + try { + newTimetable = DataManager.getTimetable(instance, busStopID, true); + } catch (SQLException e) { + e.printStackTrace(); } + return newTimetable; + } - try { - Thread.sleep(20000); - } catch (InterruptedException e) { - Log.i(TAG, "Bus stop activity thread stoped"); - break; + protected void onPostExecute(Timetable newTimetable) { + Log.i(TAG, "Got timetable for " + busStopID); + if (newTimetable == null) { + Log.i(TAG, "Its null"); + + progBar.setVisibility(View.GONE); + busStopMessage.setText("Error fetching bus times"); + busStopMessage.setVisibility(View.VISIBLE); + } else { + timetable = newTimetable; + displayTimetable(timetable); } } } - public void onCheckedChanged(CompoundButton arg0, boolean arg1) { - busStop.favourite = arg1; - try { - busStopDao.update(busStop); - dataChanged = true; - } catch (SQLException e) { - e.printStackTrace(); + private void displayTimetable(Timetable timetable) { + Log.i(TAG, "It contains " + timetable.size() + " stops"); + + if (timetable.size() == 0) { + progBar.setVisibility(View.GONE); + busStopMessage.setText("No Busses"); + busStopMessage.setVisibility(View.VISIBLE); + } else { + progBar.setVisibility(View.GONE); + busStopMessage.setVisibility(View.GONE); + TimetableAdapter adapter; + if ((adapter = (TimetableAdapter) busTimeList.getAdapter()) != null) { + adapter.updateTimetable(timetable); + } else { + adapter = new TimetableAdapter(this, timetable); + busTimeList.setAdapter(adapter); + } + busTimeContentLayout.setGravity(Gravity.TOP); } } + } diff --git a/src/net/cbaines/suma/BusStopOverlay.java b/src/net/cbaines/suma/BusStopOverlay.java index 1b6a384..83b10c1 100644 --- a/src/net/cbaines/suma/BusStopOverlay.java +++ b/src/net/cbaines/suma/BusStopOverlay.java @@ -201,7 +201,7 @@ public class BusStopOverlay extends Overlay implements RouteColorConstants { } else { Log.i(TAG, "Pressed " + busStop.id); - Intent i = new Intent(context, BusTimeActivity.class); + Intent i = new Intent(context, BusStopActivity.class); i.putExtra("busStopID", busStop.id); i.putExtra("busStopName", busStop.description); ((Activity) context).startActivityForResult(i, 0); diff --git a/src/net/cbaines/suma/DatabaseHelper.java b/src/net/cbaines/suma/DatabaseHelper.java index 7ccc08d..925b15f 100644 --- a/src/net/cbaines/suma/DatabaseHelper.java +++ b/src/net/cbaines/suma/DatabaseHelper.java @@ -28,7 +28,6 @@ import java.sql.SQLException; import android.content.Context; import android.database.sqlite.SQLiteDatabase; -import android.database.sqlite.SQLiteException; import android.util.Log; import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper; diff --git a/src/net/cbaines/suma/FindActivity.java b/src/net/cbaines/suma/FindActivity.java index dbbff49..68db3c5 100644 --- a/src/net/cbaines/suma/FindActivity.java +++ b/src/net/cbaines/suma/FindActivity.java @@ -332,7 +332,7 @@ public class FindActivity extends OrmLiteBaseActivity<DatabaseHelper> implements BusStop busStop = (BusStop) poi; - Intent i = new Intent(FindActivity.this, BusTimeActivity.class); + Intent i = new Intent(FindActivity.this, BusStopActivity.class); i.putExtra("busStopID", busStop.id); i.putExtra("busStopName", busStop.description); startActivityForResult(i, 0); diff --git a/src/net/cbaines/suma/SouthamptonUniversityMapActivity.java b/src/net/cbaines/suma/SouthamptonUniversityMapActivity.java index b6fcc7b..5b134c6 100644 --- a/src/net/cbaines/suma/SouthamptonUniversityMapActivity.java +++ b/src/net/cbaines/suma/SouthamptonUniversityMapActivity.java @@ -951,7 +951,7 @@ public class SouthamptonUniversityMapActivity extends OrmLiteBaseActivity<Databa Log.i(TAG, "Pressed " + busStop.id); - Intent i = new Intent(this, BusTimeActivity.class); + Intent i = new Intent(this, BusStopActivity.class); i.putExtra("busStopID", busStop.id); i.putExtra("busStopName", busStop.description); startActivityForResult(i, 0); diff --git a/src/net/cbaines/suma/Stop.java b/src/net/cbaines/suma/Stop.java index 6e6bd39..2fa8d33 100644 --- a/src/net/cbaines/suma/Stop.java +++ b/src/net/cbaines/suma/Stop.java @@ -75,4 +75,50 @@ public class Stop { return (String) DateUtils.getRelativeTimeSpanString(arivalTime.getTime(), System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS); } } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((arivalTime == null) ? 0 : arivalTime.hashCode()); + result = prime * result + ((bus == null) ? 0 : bus.hashCode()); + result = prime * result + ((busStop == null) ? 0 : busStop.hashCode()); + result = prime * result + id; + return result; + } + + @Override + public String toString() { + return "Stop [id=" + id + ", bus=" + bus + ", busStop=" + busStop + ", arivalTime=" + arivalTime + "]"; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Stop other = (Stop) obj; + if (arivalTime == null) { + if (other.arivalTime != null) + return false; + } else if (!arivalTime.equals(other.arivalTime)) + return false; + if (bus == null) { + if (other.bus != null) + return false; + } else if (!bus.equals(other.bus)) + return false; + if (busStop == null) { + if (other.busStop != null) + return false; + } else if (!busStop.equals(other.busStop)) + return false; + if (id != other.id) + return false; + return true; + } + } diff --git a/src/net/cbaines/suma/Timetable.java b/src/net/cbaines/suma/Timetable.java index 466bdfe..758c95f 100644 --- a/src/net/cbaines/suma/Timetable.java +++ b/src/net/cbaines/suma/Timetable.java @@ -28,4 +28,31 @@ public class Timetable extends ArrayList<Stop> { */ private static final long serialVersionUID = -9021303378059511643L; + public String toString() { + StringBuilder sb = new StringBuilder(); + for (Stop stop : this) { + sb.append(stop + "\n"); + } + return sb.toString(); + } + + public boolean contains(Stop otherStop, boolean toTheMinute) { + if (toTheMinute) { + for (Stop stop : this) { + if (otherStop.bus != null && stop.bus != null && otherStop.bus.equals(stop.bus)) { + if (Math.abs(otherStop.arivalTime.getTime() - stop.arivalTime.getTime()) < 60000) { + return true; + } + } else if (otherStop.busStop.equals(stop.busStop)) { + if (Math.abs(otherStop.arivalTime.getTime() - stop.arivalTime.getTime()) < 60000) { + return true; + } + } + } + return false; + } else { + return this.contains(otherStop); + } + } + } diff --git a/src/net/cbaines/suma/TimetableAdapter.java b/src/net/cbaines/suma/TimetableAdapter.java index 8326856..19ca5a5 100644 --- a/src/net/cbaines/suma/TimetableAdapter.java +++ b/src/net/cbaines/suma/TimetableAdapter.java @@ -20,18 +20,26 @@ package net.cbaines.suma; import android.content.Context; +import android.util.Log; import android.view.View; import android.view.ViewGroup; +import android.view.animation.Animation; +import android.view.animation.AnimationUtils; import android.widget.BaseAdapter; public class TimetableAdapter extends BaseAdapter { private final Context context; - private final Timetable timetable; + private Timetable timetable; + private final Animation a; + private boolean[] changed; + + private static final String TAG = "TimetableAdapter"; public TimetableAdapter(Context context, Timetable timetable) { this.context = context; this.timetable = timetable; + this.a = AnimationUtils.loadAnimation(context, R.anim.updated_stop_view); } public View getView(int position, View convertView, ViewGroup parent) { @@ -43,6 +51,10 @@ public class TimetableAdapter extends BaseAdapter { stopView.setStop(timetable.get(position)); } + if (changed == null || changed[position]) { + stopView.startAnimation(a); + } + return stopView; } @@ -57,4 +69,21 @@ public class TimetableAdapter extends BaseAdapter { public long getItemId(int position) { return position; } + + 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 (!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(); + } } |