aboutsummaryrefslogtreecommitdiff
path: root/src/net/cbaines/suma/BusActivity.java
blob: ad41a6ebd4efad954d61fab470aa61a8cb912e2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package net.cbaines.suma;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

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.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class BusActivity extends ToastHelperActivity implements Preferences {
	final static String TAG = "BusActivity";

	private TextView busIDTextView;
	private TextView busDestTextView;

	private TextView busContentMessage;
	private LinearLayout busActivityContentLayout;

	/**
	 * The bus this activity is focused on
	 */
	private Bus bus;
	/**
	 * The bus stop this activity is working from
	 */
	private BusStop busStop;

	Runnable refreshData;

	protected Timetable timetable;
	private Timetable visibleTimetable;

	private ListView timetableView;

	private Context instance;

	// BusStops and if they are being updated by the handler
	List<BusStop> busStops;

	private HashMap<BusStop, GetTimetableStopTask> tasks = new HashMap<BusStop, GetTimetableStopTask>();

	Handler handler;

	long timeOfLastRefresh = 0;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bus_activity);
		instance = this;

		String busID;

		if (getIntent().getDataString().startsWith("http://data")) {
			String[] uriParts = getIntent().getDataString().split("/");
			busID = uriParts[uriParts.length - 1].replace(".html", "");
		} else {
			String[] uriParts = getIntent().getDataString().split("/");
			busID = uriParts[uriParts.length - 1];
		}

		String busStopID = getIntent().getExtras().getString("busStopID");

		try {
			final DatabaseHelper helper = getHelper();
			List<Bus> buses = helper.getBusDao().queryForEq(Bus.ID_FIELD_NAME, busID);

			bus = null;
			if (buses.size() == 0) {
				Log.e(TAG, "Bus " + busID + " not found!");
			} else if (buses.size() == 1) {
				bus = buses.get(0);
			} else if (buses.size() > 1) {
				Log.e(TAG, "Found more than one bus? " + busID);
			}

			helper.getBusRouteDao().refresh(bus.route);

			busStop = null;
			if (busStopID != null) {
				List<BusStop> busStops = helper.getBusStopDao().queryForEq(BusStop.ID_FIELD_NAME, busStopID);
				if (busStops.size() == 0) {
					Log.e(TAG, "BusStop " + busStopID + " not found!");
				} else if (busStops.size() == 1) {
					busStop = busStops.get(0);
				} else if (busStops.size() > 1) {
					Log.e(TAG, "Found more than one busStop? " + busStopID);
				}
			}

			busIDTextView = (TextView) findViewById(R.id.busActivityBusID);
			busDestTextView = (TextView) findViewById(R.id.busActivityBusDestination);

			busContentMessage = (TextView) findViewById(R.id.busActivityMessage);
			busActivityContentLayout = (LinearLayout) findViewById(R.id.busActivityContentLayout);
			timetableView = (ListView) findViewById(R.id.busActivityTimes);

			if (bus.id != null) {
				Log.i(TAG, "Bus id is not null (" + bus.id + ") setting busIDTextView");
				busIDTextView.setText(bus.id + " " + bus.getName());
			} else {
				Log.w(TAG, "Bus id is null?");
				// Might not ever happen
				busIDTextView.setText("Unidentified");
			}

			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.setVisibility(View.VISIBLE);
			} else {
				Log.i(TAG, "Bus destination string is null");
				busDestTextView.setText(getResources().getString(R.string.bus_activity_no_destination_message));
				busDestTextView.setVisibility(View.VISIBLE);
			}

			busStops = bus.route.getRouteSection(instance, bus.direction);
			Log.i(TAG, "Got " + busStops.size() + " bus stops for this bus");

			if (bus.destination != null) {
				Log.i(TAG, "Bus destination is " + bus.destination);
			} else {
				Log.i(TAG, "Bus destination is null");
			}

			refreshData = new Runnable() {
				public void run() {
					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);

						if (System.currentTimeMillis() - stop.timeOfFetch.getTime() > 20000) {
							GetTimetableStopTask task = tasks.get(busStops.get(num));

							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 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));
						}

					}
					handler.postDelayed(refreshData, 50000);
				}
			};
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.bus_menu, menu);
		return true;
	}

	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();

			handler = new Handler();

			if (timetable == null) {
				Log.i(TAG, "No Previous timetable");
				timetable = new Timetable();
				for (int i = 0; i < busStops.size(); i++) {
					// 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);

		} else {
			Log.i(TAG, "Live Times Disabled");
			busContentMessage.setText("Live bus times disabled");
			busContentMessage.setVisibility(View.VISIBLE);
		}

	}

	public void onPause() {
		if (handler != null) { // BusTimes are enabled
			handler.removeCallbacks(refreshData);
			for (GetTimetableStopTask task : tasks.values()) {
				if (task != null) {
					task.cancel(true);
				}
			}

			Log.i(TAG, "Stoping refreshing timetable data");
		}

		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;

		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, new Date(System.currentTimeMillis()), 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");

			synchronized (timetable) {
				timetable.set(position, stop);
				displayTimetable(timetable);
			}
		}

	}

	@Override
	public Object onRetainNonConfigurationInstance() {
		return timetable;
	}

	private void displayTimetable(Timetable timetable) {
		visibleTimetable = (Timetable) timetable.clone();

		// Log.i(TAG, "Displaying timetable, it contains " +
		// visibleTimetable.size() + " stops");

		if (timetable.size() == 0) {
			busContentMessage.setText("No Busses");
			busContentMessage.setVisibility(View.VISIBLE);
			busActivityContentLayout.setGravity(Gravity.CENTER);
		} else {
			if (visibleTimetable.size() == 0) {
				busActivityContentLayout.setGravity(Gravity.CENTER);
				busContentMessage.setText("No Busses (With the current enabled routes)");
				busContentMessage.setVisibility(View.VISIBLE);
				timetableView.setVisibility(View.GONE);
			} else {
				timetableView.setVisibility(View.VISIBLE);
				busContentMessage.setVisibility(View.GONE);
				BusSpecificTimetableAdapter adapter;
				if ((adapter = (BusSpecificTimetableAdapter) timetableView.getAdapter()) != null) {
					adapter.updateTimetable(visibleTimetable);
				} else {
					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));
						timetableView.setSelection(busStops.indexOf(busStop));
					}
				}
				busActivityContentLayout.setGravity(Gravity.TOP);
			}
		}
	}
}