diff options
Diffstat (limited to 'src/net/cbaines')
-rw-r--r-- | src/net/cbaines/suma/BusActivity.java | 95 | ||||
-rw-r--r-- | src/net/cbaines/suma/BusSpecificTimetableAdapter.java | 93 | ||||
-rw-r--r-- | src/net/cbaines/suma/DataManager.java | 43 | ||||
-rw-r--r-- | src/net/cbaines/suma/StopView.java | 1 |
4 files changed, 196 insertions, 36 deletions
diff --git a/src/net/cbaines/suma/BusActivity.java b/src/net/cbaines/suma/BusActivity.java index 44faaa7..6a90d7f 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.HashSet; import java.util.Iterator; import java.util.List; @@ -12,18 +13,20 @@ 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; import android.view.View; import android.widget.CheckBox; import android.widget.LinearLayout; +import android.widget.ListView; import android.widget.ProgressBar; import android.widget.TextView; import com.j256.ormlite.android.apptools.OrmLiteBaseActivity; -public class BusActivity extends OrmLiteBaseActivity<DatabaseHelper> { +public class BusActivity extends OrmLiteBaseActivity<DatabaseHelper> implements Preferences { final static String TAG = "BusActivity"; private CheckBox U1RouteRadioButton; @@ -32,6 +35,9 @@ public class BusActivity extends OrmLiteBaseActivity<DatabaseHelper> { private CheckBox U6RouteRadioButton; private CheckBox U9RouteRadioButton; + private Handler handler; + private Runnable refreshData; + private TextView busIDTextView; private ProgressBar progBar; @@ -44,6 +50,10 @@ public class BusActivity extends OrmLiteBaseActivity<DatabaseHelper> { protected Timetable timetable; private Timetable visibleTimetable; + private ListView timetableView; + + private HashSet<GetTimetableStopTask> timetableStopTasks; + private Context instance; public void onCreate(Bundle savedInstanceState) { @@ -84,7 +94,7 @@ public class BusActivity extends OrmLiteBaseActivity<DatabaseHelper> { busIDTextView = (TextView) findViewById(R.id.busActivityBusID); - progBar = (ProgressBar) findViewById(R.id.busActivityCenterLoadBar); + progBar = (ProgressBar) findViewById(R.id.busActivityLoadBar); busContentMessage = (TextView) findViewById(R.id.busActivityMessage); busActivityContentLayout = (LinearLayout) findViewById(R.id.busActivityContentLayout); @@ -141,7 +151,47 @@ public class BusActivity extends OrmLiteBaseActivity<DatabaseHelper> { } } - private class GetTimetableTask extends AsyncTask<String, Integer, Timetable> { + 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"); + 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); + } else { + Log.i(TAG, "Displaying previous timetable"); + displayTimetable(timetable); + if (System.currentTimeMillis() - timetable.fetchTime.getTime() > 20000) { + handler.post(refreshData); + } + } + + } else { + Log.i(TAG, "Live Times Disabled"); + progBar.setVisibility(View.GONE); + busContentMessage.setText("Live bus times disabled"); + busContentMessage.setVisibility(View.VISIBLE); + } + + } + + private class GetTimetableStopTask extends AsyncTask<String, Integer, Timetable> { String errorMessage; protected void onPreExecute() { @@ -198,47 +248,20 @@ public class BusActivity extends OrmLiteBaseActivity<DatabaseHelper> { busContentMessage.setVisibility(View.VISIBLE); busActivityContentLayout.setGravity(Gravity.CENTER); } else { - - for (Iterator<Stop> stopIter = visibleTimetable.iterator(); stopIter.hasNext();) { - Stop stop = stopIter.next(); - Log.i(TAG, "Begin filtering, looking at " + stop + " with route " + stop.bus.route.code); - if (stop.bus.route.code.equals("U1")) { - if (!U1RouteRadioButton.isChecked()) { - stopIter.remove(); - } - } else if (stop.bus.route.code.equals("U1N")) { - if (!U1NRouteRadioButton.isChecked()) { - stopIter.remove(); - } - } else if (stop.bus.route.code.equals("U2")) { - if (!U2RouteRadioButton.isChecked()) { - stopIter.remove(); - } - } else if (stop.bus.route.code.equals("U6")) { - if (!U6RouteRadioButton.isChecked()) { - stopIter.remove(); - } - } else if (stop.bus.route.code.equals("U9")) { - if (!U9RouteRadioButton.isChecked()) { - stopIter.remove(); - } - } - } - if (visibleTimetable.size() == 0) { busActivityContentLayout.setGravity(Gravity.CENTER); busContentMessage.setText("No Busses (With the current enabled routes)"); busContentMessage.setVisibility(View.VISIBLE); - busTimeList.setVisibility(View.GONE); + timetableView.setVisibility(View.GONE); } else { - busTimeList.setVisibility(View.VISIBLE); + timetableView.setVisibility(View.VISIBLE); busContentMessage.setVisibility(View.GONE); - TimetableAdapter adapter; - if ((adapter = (TimetableAdapter) busTimeList.getAdapter()) != null) { + BusSpecificTimetableAdapter adapter; + if ((adapter = (BusSpecificTimetableAdapter) timetableView.getAdapter()) != null) { adapter.updateTimetable(visibleTimetable); } else { - adapter = new TimetableAdapter(this, visibleTimetable); - busTimeList.setAdapter(adapter); + adapter = new BusSpecificTimetableAdapter(this, visibleTimetable); + timetableView.setAdapter(adapter); } busActivityContentLayout.setGravity(Gravity.TOP); } diff --git a/src/net/cbaines/suma/BusSpecificTimetableAdapter.java b/src/net/cbaines/suma/BusSpecificTimetableAdapter.java new file mode 100644 index 0000000..6356e00 --- /dev/null +++ b/src/net/cbaines/suma/BusSpecificTimetableAdapter.java @@ -0,0 +1,93 @@ +/* + * Southampton University Map App + * Copyright (C) 2011 Christopher Baines + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +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 BusSpecificTimetableAdapter extends BaseAdapter { + + private final Context context; + private Timetable timetable; + private final Animation a; + private boolean[] changed; + + private static final String TAG = "TimetableAdapter"; + + public BusSpecificTimetableAdapter(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) { + Log.i(TAG, "Returning stop " + position + " " + timetable.get(position)); + + BusSpecificStopView stopView; + if (convertView == null) { + stopView = new BusSpecificStopView(context, timetable.get(position)); + } else { + stopView = (BusSpecificStopView) convertView; + stopView.setStop(timetable.get(position)); + } + + if (changed == null || changed[position]) { + a.reset(); + stopView.startAnimation(a); + Log.i(TAG, "Animating it"); + } + + return stopView; + } + + public int getCount() { + return timetable.size(); + } + + public Object getItem(int position) { + return position; + } + + 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(); + } +} diff --git a/src/net/cbaines/suma/DataManager.java b/src/net/cbaines/suma/DataManager.java index 634415d..a1530b7 100644 --- a/src/net/cbaines/suma/DataManager.java +++ b/src/net/cbaines/suma/DataManager.java @@ -704,6 +704,49 @@ public class DataManager { return timetable; } + public static Stop getStop(Context context, Bus bus, BusStop busStop) throws SQLException, ClientProtocolException, IOException, JSONException { + + if (helper == null) + helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); + if (busRouteDao == null) + busRouteDao = helper.getBusRouteDao(); + if (busStopDao == null) + busStopDao = helper.getBusStopDao(); + + String file = getFileFromServer(busStopUrl + busStop + ".json"); + + JSONObject data = new JSONObject(file); + JSONArray stopsArray = data.getJSONArray("stops"); + + HashSet<BusRoute> busRoutes = new HashSet<BusRoute>(); + busRoutes.add(bus.route); + + Stop stop = null; + + Log.i(TAG, "Number of entries " + data.length()); + + Log.i(TAG, "Stops: " + data.getJSONArray("stops")); + + for (int stopNum = 0; stopNum < stopsArray.length(); stopNum++) { + JSONObject stopObj = stopsArray.getJSONObject(stopNum); + + if (stopObj.getString("vehicle").equals(bus.id)) { + + stop = getStop(context, stopObj, busRoutes, busStop); + + if (stop == null) { + Log.w(TAG, "Null stop, skiping"); + continue; + } + + Log.i(TAG, "Found stop for a unidentified " + stop.bus.toString() + " at " + stop.busStop.id + " at " + stop.arivalTime); + + } + } + + return stop; + } + static PathOverlay getRoutePath(InputStream routeResource, int colour, ResourceProxy resProxy) { PathOverlay data = null; diff --git a/src/net/cbaines/suma/StopView.java b/src/net/cbaines/suma/StopView.java index 0fe3aa1..cc60280 100644 --- a/src/net/cbaines/suma/StopView.java +++ b/src/net/cbaines/suma/StopView.java @@ -123,6 +123,7 @@ public class StopView extends LinearLayout implements OnClickListener, OnLongCli if (stop.bus.id != null) { Intent i = new Intent(context, BusActivity.class); i.putExtra("busID", stop.bus.id); + i.putExtra("busStopID", stop.busStop.id); ((Activity) context).startActivityForResult(i, 0); } else { Toast.makeText(context, "Arival prediction not avalible for timetabled buses", Toast.LENGTH_SHORT).show(); |