From fcac217897640f23d0a528c02f861e1e2d15a212 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Thu, 16 Feb 2012 07:58:41 +0000 Subject: Changes to the BusActivity, not working yet... --- src/net/cbaines/suma/BusActivity.java | 84 +-------------------- src/net/cbaines/suma/BusSpecificStopView.java | 87 +++++++++++++++++++++- .../cbaines/suma/BusSpecificTimetableAdapter.java | 8 +- 3 files changed, 94 insertions(+), 85 deletions(-) diff --git a/src/net/cbaines/suma/BusActivity.java b/src/net/cbaines/suma/BusActivity.java index 9b8b81f..be94bd3 100644 --- a/src/net/cbaines/suma/BusActivity.java +++ b/src/net/cbaines/suma/BusActivity.java @@ -35,9 +35,6 @@ public class BusActivity extends OrmLiteBaseActivity implements private TextView U6RouteTextView; private TextView U9RouteTextView; - private Handler handler; - private Runnable refreshData; - private TextView busIDTextView; private ProgressBar progBar; @@ -52,12 +49,12 @@ public class BusActivity extends OrmLiteBaseActivity implements private ListView timetableView; - private HashMap timetableStopTasks = new HashMap(); - private Context instance; private List busStops; + private Handler handler; + int num = 20; public void onCreate(Bundle savedInstanceState) { @@ -158,23 +155,6 @@ public class BusActivity extends OrmLiteBaseActivity implements } } - refreshData = new Runnable() { - @Override - public void run() { - for (int i = 0; i < num; i++) { - if (timetable.get(i).timeOfFetch == null || (timetable.get(i).timeOfFetch.getTime() - System.currentTimeMillis()) > 20000) { - GetTimetableStopTask previous = timetableStopTasks.get(busStops.get(i)); - if (previous == null) { - GetTimetableStopTask timetableStopTask = new GetTimetableStopTask(); - BusStop[] str = { busStops.get(i) }; - timetableStopTask.execute(str); - timetableStopTasks.put(busStops.get(i), timetableStopTask); - handler.postDelayed(refreshData, 20000); - } - } - } - } - }; } public void onResume() { @@ -198,7 +178,6 @@ public class BusActivity extends OrmLiteBaseActivity implements Log.i(TAG, "Displaying previous timetable"); displayTimetable(timetable); } - handler.post(refreshData); } else { Log.i(TAG, "Live Times Disabled"); @@ -211,7 +190,7 @@ public class BusActivity extends OrmLiteBaseActivity implements public void onPause() { if (handler != null) { // BusTimes are enabled - handler.removeCallbacks(refreshData); + handler.removeCallbacks(r) if (timetableStopTasks != null) { // Could happen if the handler has not created the timetableTask yet for (GetTimetableStopTask task : timetableStopTasks.values()) { if (task != null) { @@ -225,63 +204,6 @@ public class BusActivity extends OrmLiteBaseActivity implements super.onPause(); } - private class GetTimetableStopTask extends AsyncTask { - private String errorMessage; - - private BusStop busStop; - - private int position; - - protected void onPreExecute() { - progBar.setVisibility(View.VISIBLE); - } - - protected Stop doInBackground(BusStop... busStopArray) { - busStop = busStopArray[0]; - position = busStops.indexOf(busStop); - Stop stop = null; - - try { - 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); - } - Log.i(TAG, "Finished fetching stop for busStop " + position); - } catch (SQLException e) { - errorMessage = "Error message regarding SQL?"; - e.printStackTrace(); - } catch (ClientProtocolException e) { - errorMessage = "ClientProtocolException!?!"; - e.printStackTrace(); - } catch (IOException e) { - errorMessage = "Error fetching bus times from server, are you connected to the internet?"; - e.printStackTrace(); - } catch (JSONException e) { - errorMessage = "Error parsing bus times"; - e.printStackTrace(); - } - return stop; - } - - protected void onPostExecute(Stop stop) { - // Log.i(TAG, "Got timetable"); - if (stop == null) { - Log.i(TAG, "Its null"); - - progBar.setVisibility(View.GONE); - busContentMessage.setText(errorMessage); - busContentMessage.setVisibility(View.VISIBLE); - } else { - progBar.setVisibility(View.GONE); - synchronized (timetable) { - timetable.set(position, stop); - displayTimetable(timetable); - } - } - } - } - private void displayTimetable(Timetable timetable) { visibleTimetable = (Timetable) timetable.clone(); diff --git a/src/net/cbaines/suma/BusSpecificStopView.java b/src/net/cbaines/suma/BusSpecificStopView.java index 2688113..b13a6de 100644 --- a/src/net/cbaines/suma/BusSpecificStopView.java +++ b/src/net/cbaines/suma/BusSpecificStopView.java @@ -19,12 +19,19 @@ package net.cbaines.suma; +import java.io.IOException; import java.sql.SQLException; import java.text.DateFormat; +import org.apache.http.client.ClientProtocolException; +import org.json.JSONException; + import android.app.Activity; import android.content.Context; import android.content.Intent; +import android.os.AsyncTask; +import android.os.Handler; +import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; @@ -38,6 +45,12 @@ import com.j256.ormlite.dao.Dao; public class BusSpecificStopView extends LinearLayout implements OnClickListener, OnLongClickListener { + private static final String TAG = "BusSpecificStopView"; + + private Handler handler; + + private Runnable refreshData; + // private static final String TAG = "StopView"; private final TextView location; @@ -45,12 +58,15 @@ public class BusSpecificStopView extends LinearLayout implements OnClickListener private String onClickMessage = ""; private final Context context; + private GetTimetableStopTask task; + private Stop stop; - public BusSpecificStopView(Context context, Stop stop) { + public BusSpecificStopView(Context context, Stop newStop, Handler newHandler) { super(context); this.context = context; + this.handler = newHandler; this.setOrientation(HORIZONTAL); @@ -65,6 +81,75 @@ public class BusSpecificStopView extends LinearLayout implements OnClickListener addView(location, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); addView(time, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); + + refreshData = new Runnable() { + @Override + public void run() { + if (stop.timeOfFetch == null || (stop.timeOfFetch.getTime() - System.currentTimeMillis()) > 20000) { + if (task == null) { + task = new GetTimetableStopTask(); + BusStop[] str = { stop.busStop }; + task.execute(str); + handler.postDelayed(refreshData, 20000); + } + } + + } + }; + + handler.post(refreshData); + } + + private class GetTimetableStopTask extends AsyncTask { + private String errorMessage; + + private BusStop busStop; + + private int position; + + protected void onPreExecute() { + // progBar.setVisibility(View.VISIBLE); + } + + protected Stop doInBackground(BusStop... busStopArray) { + busStop = busStopArray[0]; + Stop stop = null; + + try { + Log.i(TAG, "Fetching stop for busStop " + position); + stop = DataManager.getStop(context, stop.bus, busStop); + if (stop == null) { + stop = new Stop(stop.bus, busStop, null, null, false); + } + Log.i(TAG, "Finished fetching stop for busStop " + position); + } catch (SQLException e) { + errorMessage = "Error message regarding SQL?"; + e.printStackTrace(); + } catch (ClientProtocolException e) { + errorMessage = "ClientProtocolException!?!"; + e.printStackTrace(); + } catch (IOException e) { + errorMessage = "Error fetching bus times from server, are you connected to the internet?"; + e.printStackTrace(); + } catch (JSONException e) { + errorMessage = "Error parsing bus times"; + e.printStackTrace(); + } + return stop; + } + + protected void onPostExecute(Stop stop) { + // Log.i(TAG, "Got timetable"); + if (stop == null) { + + // progBar.setVisibility(View.GONE); + // busContentMessage.setText(errorMessage); + // busContentMessage.setVisibility(View.VISIBLE); + } else { + // progBar.setVisibility(View.GONE); + + } + } } public void setStop(Stop stop) { diff --git a/src/net/cbaines/suma/BusSpecificTimetableAdapter.java b/src/net/cbaines/suma/BusSpecificTimetableAdapter.java index 263c6cb..0467d4a 100644 --- a/src/net/cbaines/suma/BusSpecificTimetableAdapter.java +++ b/src/net/cbaines/suma/BusSpecificTimetableAdapter.java @@ -20,7 +20,7 @@ package net.cbaines.suma; import android.content.Context; -import android.util.Log; +import android.os.Handler; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; @@ -33,12 +33,14 @@ public class BusSpecificTimetableAdapter extends BaseAdapter { private Timetable timetable; private final Animation a; private boolean[] changed; + private Handler handler; private static final String TAG = "BusSpecificTimetableAdapter"; - public BusSpecificTimetableAdapter(Context context, Timetable timetable) { + public BusSpecificTimetableAdapter(Context context, Timetable timetable, Handler handler) { this.context = context; this.timetable = timetable; + this.handler = handler; this.a = AnimationUtils.loadAnimation(context, R.anim.updated_stop_view); } @@ -47,7 +49,7 @@ public class BusSpecificTimetableAdapter extends BaseAdapter { BusSpecificStopView stopView; if (convertView == null) { - stopView = new BusSpecificStopView(context, timetable.get(position)); + stopView = new BusSpecificStopView(context, timetable.get(position), handler); } else { stopView = (BusSpecificStopView) convertView; stopView.setStop(timetable.get(position)); -- cgit v1.2.3