1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
|
import static dk.thoerup.traininfo.R.string.departurelist_fetchdepartures; |
|
3 |
import static dk.thoerup.traininfo.R.string.departurelist_fetcharrivals; |
import static dk.thoerup.traininfo.R.string.departurelist_fetcharrivals; |
4 |
|
import static dk.thoerup.traininfo.R.string.departurelist_fetchdepartures; |
5 |
import static dk.thoerup.traininfo.R.string.generic_cancel; |
import static dk.thoerup.traininfo.R.string.generic_cancel; |
6 |
import static dk.thoerup.traininfo.R.string.generic_retry; |
import static dk.thoerup.traininfo.R.string.generic_retry; |
7 |
|
|
|
|
|
8 |
import java.text.NumberFormat; |
import java.text.NumberFormat; |
9 |
|
|
|
|
|
10 |
import android.app.AlertDialog; |
import android.app.AlertDialog; |
11 |
import android.app.Dialog; |
import android.app.Dialog; |
12 |
import android.app.ListActivity; |
import android.app.ListActivity; |
13 |
import android.app.ProgressDialog; |
import android.app.ProgressDialog; |
14 |
import android.content.DialogInterface; |
import android.content.DialogInterface; |
15 |
import android.content.Intent; |
import android.content.Intent; |
16 |
|
import android.graphics.Typeface; |
17 |
import android.net.Uri; |
import android.net.Uri; |
18 |
import android.os.AsyncTask; |
import android.os.AsyncTask; |
19 |
import android.os.Bundle; |
import android.os.Bundle; |
20 |
import android.util.Log; |
import android.util.Log; |
21 |
|
import android.view.Menu; |
22 |
|
import android.view.MenuItem; |
23 |
import android.view.View; |
import android.view.View; |
24 |
import android.view.View.OnClickListener; |
import android.view.View.OnClickListener; |
25 |
import android.widget.Button; |
import android.widget.Button; |
26 |
import android.widget.ListView; |
import android.widget.ListView; |
27 |
|
import android.widget.TableLayout; |
28 |
|
import android.widget.TableRow; |
29 |
import android.widget.TextView; |
import android.widget.TextView; |
30 |
|
import dk.thoerup.android.traininfo.common.DepartureBean; |
31 |
|
import dk.thoerup.android.traininfo.common.DepartureEntry; |
32 |
|
import dk.thoerup.android.traininfo.common.MetroBean; |
33 |
|
import dk.thoerup.android.traininfo.common.MetroBean.MetroEntry; |
34 |
|
import dk.thoerup.android.traininfo.common.StationBean.StationEntry; |
35 |
import dk.thoerup.traininfo.provider.DepartureProvider; |
import dk.thoerup.traininfo.provider.DepartureProvider; |
36 |
|
import dk.thoerup.traininfo.provider.MetroProvider; |
37 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
import dk.thoerup.traininfo.provider.ProviderFactory; |
38 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
39 |
|
|
40 |
public class DepartureList extends ListActivity { |
public class DepartureList extends ListActivity { |
41 |
|
|
42 |
public static final int DLG_PROGRESS = 1; |
public static final int DLG_PROGRESS = 1; |
43 |
|
static final int MENU_MAP = 100; |
44 |
|
static final int MENU_NOTIFICATIONS = 101; |
45 |
|
static final int MENU_METROMAP= 102; |
46 |
|
|
47 |
|
|
48 |
DepartureListAdapter adapter; |
DepartureListAdapter adapter; |
49 |
DepartureProvider provider; |
DepartureProvider provider; |
50 |
DepartureBean departures; |
DepartureBean departures; |
51 |
|
|
52 |
|
MetroBean metroBean; |
53 |
|
MetroProvider metro; |
54 |
|
|
55 |
int selectedItemId; |
int selectedItemId; |
56 |
//DepartureBean currentDeparture; |
//DepartureBean currentDeparture; |
57 |
|
|
58 |
ProgressDialog pgDialog; |
ProgressDialog pgDialog; |
59 |
|
|
60 |
DepartureFetcher fetcher; |
DepartureFetcher fetcher; |
61 |
|
MetroFetcher metroFetcher; |
62 |
|
|
63 |
StationBean station; |
StationEntry station; |
64 |
|
|
65 |
boolean arrival = false; |
boolean arrival = false; |
66 |
|
|
67 |
|
int commFailCounter = 0; |
68 |
|
|
69 |
@Override |
@Override |
70 |
protected void onCreate(Bundle savedInstanceState) { |
protected void onCreate(Bundle savedInstanceState) { |
76 |
|
|
77 |
Intent launchedBy = getIntent(); |
Intent launchedBy = getIntent(); |
78 |
|
|
79 |
station = (StationBean) launchedBy.getSerializableExtra("stationbean"); |
station = (StationEntry) launchedBy.getSerializableExtra("stationbean"); |
80 |
|
|
81 |
((TextView) findViewById(R.id.stationName)).setText( station.getName() ); |
((TextView) findViewById(R.id.stationName)).setText( station.getName() ); |
82 |
|
|
85 |
|
|
86 |
final Button departureBtn = (Button) findViewById(R.id.departurebtn); |
final Button departureBtn = (Button) findViewById(R.id.departurebtn); |
87 |
final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn); |
final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn); |
88 |
|
final Button metroBtn = (Button) findViewById(R.id.metrobtn); |
89 |
|
|
90 |
|
final View metroView = findViewById(R.id.metroonly); |
91 |
|
|
92 |
departureBtn.setOnClickListener( new OnClickListener() { |
departureBtn.setOnClickListener( new OnClickListener() { |
93 |
@Override |
@Override |
94 |
public void onClick(View arg0) { |
public void onClick(View arg0) { |
95 |
arrivalBtn.setBackgroundResource(R.drawable.custom_button); |
arrivalBtn.setBackgroundResource(R.drawable.custom_button); |
96 |
departureBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
departureBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
97 |
|
metroBtn.setBackgroundResource(R.drawable.custom_button); |
98 |
|
|
99 |
|
getListView().setVisibility( View.VISIBLE ); |
100 |
|
metroView.setVisibility( View.GONE ); |
101 |
arrival = false; |
arrival = false; |
102 |
startDepartureFetcher(); |
startDepartureFetcher(); |
103 |
} |
} |
107 |
public void onClick(View arg0) { |
public void onClick(View arg0) { |
108 |
arrivalBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
arrivalBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
109 |
departureBtn.setBackgroundResource(R.drawable.custom_button); |
departureBtn.setBackgroundResource(R.drawable.custom_button); |
110 |
|
metroBtn.setBackgroundResource(R.drawable.custom_button); |
111 |
|
|
112 |
|
getListView().setVisibility( View.VISIBLE ); |
113 |
|
metroView.setVisibility( View.GONE ); |
114 |
arrival = true; |
arrival = true; |
115 |
startDepartureFetcher(); |
startDepartureFetcher(); |
116 |
} |
} |
117 |
}); |
}); |
118 |
|
|
119 |
|
metroBtn.setOnClickListener( new OnClickListener() { |
120 |
|
@Override |
121 |
|
public void onClick(View v) { |
122 |
|
arrivalBtn.setBackgroundResource(R.drawable.custom_button); |
123 |
|
departureBtn.setBackgroundResource(R.drawable.custom_button); |
124 |
|
metroBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
125 |
|
|
126 |
|
getListView().setVisibility( View.GONE ); |
127 |
|
metroView.setVisibility( View.VISIBLE ); |
128 |
|
startMetroFetcher(); |
129 |
|
} |
130 |
|
}); |
131 |
|
|
132 |
|
|
133 |
|
|
|
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
|
134 |
|
|
135 |
int distance = station.getDistance(); |
// findViewById(R.id.header).setOnClickListener( mapLauncher ); |
136 |
|
|
137 |
|
int distance = station.getCalcdist(); |
138 |
if (distance != 0) { |
if (distance != 0) { |
139 |
NumberFormat format = NumberFormat.getNumberInstance(); |
NumberFormat format = NumberFormat.getNumberInstance(); |
140 |
format.setMaximumFractionDigits(1); |
format.setMaximumFractionDigits(1); |
144 |
} else { |
} else { |
145 |
((TextView) findViewById(R.id.stationDistance)).setVisibility(View.GONE); |
((TextView) findViewById(R.id.stationDistance)).setVisibility(View.GONE); |
146 |
} |
} |
147 |
|
|
148 |
|
ProviderFactory.purgeOldEntries(); //cleanup before fetching more data |
149 |
|
|
150 |
|
Log.e("Station", station.toCSV() ); |
151 |
|
|
152 |
|
if (station.isMetro() == false) { |
153 |
|
metroBtn.setVisibility( View.GONE ); |
154 |
|
} |
155 |
|
|
156 |
if (station.isRegional() == false && station.isSTrain() == false) { |
metro = ProviderFactory.getMetroProvider(); |
157 |
|
|
158 |
|
if (station.isRegional() == false && station.isStrain() == false) { |
159 |
getListView().setVisibility( View.GONE ); |
getListView().setVisibility( View.GONE ); |
160 |
findViewById(R.id.metroonly).setVisibility( View.VISIBLE ); |
metroView.setVisibility( View.VISIBLE ); |
161 |
|
|
162 |
departureBtn.setVisibility( View.GONE ); |
departureBtn.setVisibility( View.GONE ); |
163 |
arrivalBtn.setVisibility(View.GONE); |
arrivalBtn.setVisibility(View.GONE); |
164 |
|
metroBtn.setVisibility( View.GONE ); |
165 |
|
|
166 |
|
if (savedInstanceState == null) { |
167 |
|
startMetroFetcher(); |
168 |
|
} else { |
169 |
|
metroBean = (MetroBean) savedInstanceState.getSerializable("metro"); |
170 |
|
loadMetroData(); |
171 |
|
} |
172 |
|
|
173 |
} else { |
} else { |
174 |
provider = ProviderFactory.getDepartureProvider(); |
provider = ProviderFactory.getDepartureProvider(); |
177 |
startDepartureFetcher(); |
startDepartureFetcher(); |
178 |
} else { |
} else { |
179 |
departures = (DepartureBean) savedInstanceState.getSerializable("departures"); |
departures = (DepartureBean) savedInstanceState.getSerializable("departures"); |
180 |
adapter.setDepartures(departures.entries); |
|
181 |
selectedItemId = savedInstanceState.getInt("selectedItemId"); |
if ( (departures != null) && (departures.entries != null) ) { |
182 |
|
adapter.setDepartures(departures.entries); |
183 |
|
} |
184 |
|
selectedItemId = savedInstanceState.getInt("selectedItemId"); |
185 |
|
|
186 |
|
if ( hasNotifications() ) { |
187 |
|
findViewById(R.id.notifIcon).setVisibility(View.VISIBLE); |
188 |
|
} |
189 |
|
|
190 |
} |
} |
191 |
} |
} |
192 |
} |
} |
193 |
|
|
194 |
|
boolean hasNotifications() { |
195 |
|
return (departures != null && departures.notifications.size() > 0); |
196 |
|
} |
197 |
|
|
198 |
@Override |
@Override |
199 |
public void onSaveInstanceState(Bundle outState) |
public void onSaveInstanceState(Bundle outState) |
200 |
{ |
{ |
204 |
outState.putInt("selectedItemId", selectedItemId); |
outState.putInt("selectedItemId", selectedItemId); |
205 |
|
|
206 |
outState.putSerializable("departures", departures); |
outState.putSerializable("departures", departures); |
207 |
|
outState.putSerializable("metro", metroBean); |
208 |
} |
} |
209 |
|
|
210 |
|
|
216 |
if (fetcher != null) { |
if (fetcher != null) { |
217 |
fetcher.cancel(true); |
fetcher.cancel(true); |
218 |
} |
} |
219 |
|
|
220 |
|
if (metroFetcher != null) { |
221 |
|
metroFetcher.cancel(true); |
222 |
|
} |
223 |
} |
} |
224 |
|
|
225 |
@Override |
@Override |
263 |
return super.onCreateDialog(id); |
return super.onCreateDialog(id); |
264 |
} |
} |
265 |
} |
} |
266 |
|
|
267 |
|
|
268 |
|
|
269 |
|
|
270 |
|
|
271 |
|
@Override |
272 |
|
public boolean onCreateOptionsMenu(Menu menu) { |
273 |
|
MenuItem item; |
274 |
|
|
275 |
|
item = menu.add(0, MENU_MAP, 0, getString(R.string.departurelist_showonmap) ); |
276 |
|
item.setIcon(android.R.drawable.ic_menu_mapmode); |
277 |
|
|
278 |
|
item = menu.add(0, MENU_NOTIFICATIONS, 0, getString(R.string.departurelist_notifications) ); |
279 |
|
item.setIcon(android.R.drawable.ic_menu_info_details); |
280 |
|
|
281 |
|
boolean notifEnabled = hasNotifications(); |
282 |
|
item.setEnabled(notifEnabled); |
283 |
|
|
284 |
|
if (station.isMetro()) { |
285 |
|
item = menu.add(0, MENU_METROMAP, 0, "Metro" ); //TODO:translate!?! |
286 |
|
item.setIcon(android.R.drawable.ic_menu_mapmode); |
287 |
|
} |
288 |
|
|
289 |
|
|
290 |
|
return true; |
291 |
|
} |
292 |
|
|
293 |
|
@Override |
294 |
|
public boolean onOptionsItemSelected(MenuItem item) { |
295 |
|
boolean res; |
296 |
|
switch(item.getItemId()) { |
297 |
|
case MENU_MAP: |
298 |
|
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude()); |
299 |
|
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
300 |
|
res = true; |
301 |
|
break; |
302 |
|
case MENU_NOTIFICATIONS: |
303 |
|
Intent i = new Intent(this,dk.thoerup.traininfo.NotificationList.class); |
304 |
|
i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications); |
305 |
|
startActivity(i); |
306 |
|
res = true; |
307 |
|
break; |
308 |
|
case MENU_METROMAP: |
309 |
|
Intent metroMap = new Intent(this,dk.thoerup.traininfo.MetroMap.class); |
310 |
|
startActivity(metroMap); |
311 |
|
res = true; |
312 |
|
break; |
313 |
|
default: |
314 |
|
res = super.onOptionsItemSelected(item); |
315 |
|
} |
316 |
|
return res; |
317 |
|
} |
318 |
|
|
319 |
void startDepartureFetcher() { |
void startDepartureFetcher() { |
320 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
322 |
fetcher.execute(station.getId()); |
fetcher.execute(station.getId()); |
323 |
} |
} |
324 |
|
|
325 |
|
void startMetroFetcher() { |
326 |
|
showDialog(DLG_PROGRESS); |
327 |
|
metroFetcher = new MetroFetcher(); |
328 |
|
metroFetcher.execute(station.getId()); |
329 |
|
} |
330 |
|
|
331 |
class DialogDismisser implements View.OnClickListener { |
class DialogDismisser implements View.OnClickListener { |
332 |
|
|
333 |
Dialog dlg; |
Dialog dlg; |
342 |
} |
} |
343 |
} |
} |
344 |
|
|
345 |
View.OnClickListener mapLauncher = new View.OnClickListener() { |
/*View.OnClickListener mapLauncher = new View.OnClickListener() { |
346 |
@Override |
@Override |
347 |
public void onClick(View v) { |
public void onClick(View v) { |
348 |
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude()); |
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude()); |
349 |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
350 |
} |
} |
351 |
}; |
};*/ |
352 |
|
|
353 |
|
|
354 |
|
|
355 |
class DepartureFetcher extends AsyncTask<Integer, Void, Void> { |
class DepartureFetcher extends AsyncTask<Integer, Void, Void> { |
356 |
|
|
|
boolean success; |
|
|
|
|
357 |
@Override |
@Override |
358 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
359 |
super.onPostExecute(result); |
super.onPostExecute(result); |
361 |
|
|
362 |
pgDialog.dismiss(); |
pgDialog.dismiss(); |
363 |
|
|
364 |
if (success) { |
if (departures != null) { |
365 |
|
commFailCounter = 0; |
366 |
DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html |
DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html |
367 |
adapter.setDepartures(departures.entries); |
adapter.setDepartures(departures.entries); |
368 |
DepartureList.this.getListView().setVisibility(View.VISIBLE); |
DepartureList.this.getListView().setVisibility(View.VISIBLE); |
369 |
|
|
370 |
|
|
371 |
|
if ( hasNotifications() ) { |
372 |
|
findViewById(R.id.notifIcon).setVisibility(View.VISIBLE); |
373 |
|
} |
374 |
|
|
375 |
if (departures.entries.size() == 0) { |
if (departures.entries.size() == 0) { |
376 |
MessageBox.showMessage(DepartureList.this, "No departures found", true); |
MessageBox.showMessage(DepartureList.this, "No departures found", true); |
377 |
} |
} |
378 |
} else { // communication or parse error |
} else { // communication or parse error |
379 |
|
commFailCounter++; |
380 |
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
381 |
builder.setMessage("Error finding departures"); |
builder.setMessage("Error finding departures"); |
382 |
builder.setCancelable(true); |
builder.setCancelable(true); |
383 |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
if (commFailCounter < 3) { |
384 |
|
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
385 |
|
public void onClick(DialogInterface dialog, int id) { |
386 |
|
dialog.dismiss(); |
387 |
|
startDepartureFetcher(); |
388 |
|
|
389 |
|
} |
390 |
|
}); |
391 |
|
} |
392 |
|
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
393 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
394 |
dialog.dismiss(); |
dialog.dismiss(); |
395 |
startDepartureFetcher(); |
DepartureList.this.finish(); |
396 |
|
} |
|
} |
|
397 |
}); |
}); |
398 |
|
|
399 |
|
try { |
400 |
|
builder.show(); |
401 |
|
} catch (android.view.WindowManager.BadTokenException e) { |
402 |
|
Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |
403 |
|
} |
404 |
|
} |
405 |
|
} |
406 |
|
|
407 |
|
@Override |
408 |
|
protected Void doInBackground(Integer... params) { |
409 |
|
departures = provider.lookupDepartures(params[0], DepartureList.this.arrival); |
410 |
|
return null; |
411 |
|
} |
412 |
|
|
413 |
|
} |
414 |
|
|
415 |
|
public void loadMetroData() { |
416 |
|
((TextView) findViewById(R.id.operations)).setText( metroBean.operationInfo ); |
417 |
|
((TextView) findViewById(R.id.plan)).setText( metroBean.plan ); |
418 |
|
|
419 |
|
|
420 |
|
TableLayout table = (TableLayout) findViewById(R.id.metrotable); |
421 |
|
table.removeAllViews(); |
422 |
|
|
423 |
|
TableRow head = new TableRow(this); |
424 |
|
|
425 |
|
TextView h1 = new TextView(this); |
426 |
|
h1.setText("Metro"); |
427 |
|
h1.setTypeface( Typeface.defaultFromStyle(Typeface.BOLD)); |
428 |
|
|
429 |
|
|
430 |
|
TableRow.LayoutParams params = new TableRow.LayoutParams(); |
431 |
|
params.span = 2; |
432 |
|
head.addView(h1, params); |
433 |
|
|
434 |
|
|
435 |
|
|
436 |
|
TextView h2 = new TextView(this); |
437 |
|
h2.setTypeface( Typeface.defaultFromStyle(Typeface.BOLD)); |
438 |
|
h2.setText("Om minutter"); |
439 |
|
|
440 |
|
head.addView(h2,params); |
441 |
|
|
442 |
|
|
443 |
|
|
444 |
|
table.addView(head); |
445 |
|
|
446 |
|
for (MetroEntry entry : metroBean.entries) { |
447 |
|
TableRow row = new TableRow(this); |
448 |
|
|
449 |
|
Log.e("Test", "" + entry.destination); |
450 |
|
|
451 |
|
TextView v1 = new TextView(this); |
452 |
|
v1.setText( entry.metro ); |
453 |
|
row.addView(v1); |
454 |
|
|
455 |
|
TextView v2 = new TextView(this); |
456 |
|
v2.setText( entry.destination ); |
457 |
|
row.addView(v2); |
458 |
|
|
459 |
|
TextView v3 = new TextView(this); |
460 |
|
v3.setText( entry.minutes ); |
461 |
|
row.addView(v3); |
462 |
|
|
463 |
|
table.addView(row); |
464 |
|
|
465 |
|
} |
466 |
|
findViewById(R.id.rootView).requestLayout(); |
467 |
|
} |
468 |
|
|
469 |
|
class MetroFetcher extends AsyncTask<Integer, Void, Void> { |
470 |
|
|
471 |
|
@Override |
472 |
|
protected void onPostExecute(Void result) { |
473 |
|
super.onPostExecute(result); |
474 |
|
|
475 |
|
|
476 |
|
|
477 |
|
pgDialog.dismiss(); |
478 |
|
|
479 |
|
if (metroBean != null) { |
480 |
|
loadMetroData(); |
481 |
|
} else { // communication or parse error |
482 |
|
commFailCounter++; |
483 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
484 |
|
builder.setMessage("Error finding metro data"); |
485 |
|
builder.setCancelable(true); |
486 |
|
if (commFailCounter < 3) { |
487 |
|
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
488 |
|
public void onClick(DialogInterface dialog, int id) { |
489 |
|
dialog.dismiss(); |
490 |
|
startMetroFetcher(); |
491 |
|
|
492 |
|
} |
493 |
|
}); |
494 |
|
} |
495 |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
496 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
497 |
dialog.dismiss(); |
dialog.dismiss(); |
508 |
} |
} |
509 |
|
|
510 |
@Override |
@Override |
511 |
protected Void doInBackground(Integer... params) { |
protected Void doInBackground(Integer... params) { |
512 |
success = provider.lookupDepartures(params[0], DepartureList.this.arrival); |
metroBean = metro.lookupMetroInfo(params[0]); |
|
departures = provider.getDepartures(params[0], DepartureList.this.arrival); |
|
513 |
return null; |
return null; |
514 |
} |
} |
515 |
|
|
516 |
} |
} |
517 |
|
|
518 |
} |
} |