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; |
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.traininfo.provider.DepartureProvider; |
import dk.thoerup.traininfo.provider.DepartureProvider; |
31 |
|
import dk.thoerup.traininfo.provider.MetroProvider; |
32 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
import dk.thoerup.traininfo.provider.ProviderFactory; |
33 |
|
import dk.thoerup.traininfo.provider.MetroProvider.MetroBean; |
34 |
|
import dk.thoerup.traininfo.provider.MetroProvider.MetroEntry; |
35 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
36 |
|
|
37 |
public class DepartureList extends ListActivity { |
public class DepartureList extends ListActivity { |
45 |
DepartureProvider provider; |
DepartureProvider provider; |
46 |
DepartureBean departures; |
DepartureBean departures; |
47 |
|
|
48 |
|
MetroBean metroBean; |
49 |
|
MetroProvider metro; |
50 |
|
|
51 |
int selectedItemId; |
int selectedItemId; |
52 |
//DepartureBean currentDeparture; |
//DepartureBean currentDeparture; |
53 |
|
|
54 |
ProgressDialog pgDialog; |
ProgressDialog pgDialog; |
55 |
|
|
56 |
DepartureFetcher fetcher; |
DepartureFetcher fetcher; |
57 |
|
MetroFetcher metroFetcher; |
58 |
|
|
59 |
StationBean station; |
StationBean station; |
60 |
|
|
83 |
final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn); |
final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn); |
84 |
final Button metroBtn = (Button) findViewById(R.id.metrobtn); |
final Button metroBtn = (Button) findViewById(R.id.metrobtn); |
85 |
|
|
86 |
|
final View metroView = findViewById(R.id.metroonly); |
87 |
|
|
88 |
departureBtn.setOnClickListener( new OnClickListener() { |
departureBtn.setOnClickListener( new OnClickListener() { |
89 |
@Override |
@Override |
90 |
public void onClick(View arg0) { |
public void onClick(View arg0) { |
91 |
arrivalBtn.setBackgroundResource(R.drawable.custom_button); |
arrivalBtn.setBackgroundResource(R.drawable.custom_button); |
92 |
departureBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
departureBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
93 |
metroBtn.setBackgroundResource(R.drawable.custom_button); |
metroBtn.setBackgroundResource(R.drawable.custom_button); |
94 |
|
|
95 |
|
getListView().setVisibility( View.VISIBLE ); |
96 |
|
metroView.setVisibility( View.GONE ); |
97 |
arrival = false; |
arrival = false; |
98 |
startDepartureFetcher(); |
startDepartureFetcher(); |
99 |
} |
} |
104 |
arrivalBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
arrivalBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
105 |
departureBtn.setBackgroundResource(R.drawable.custom_button); |
departureBtn.setBackgroundResource(R.drawable.custom_button); |
106 |
metroBtn.setBackgroundResource(R.drawable.custom_button); |
metroBtn.setBackgroundResource(R.drawable.custom_button); |
107 |
|
|
108 |
|
getListView().setVisibility( View.VISIBLE ); |
109 |
|
metroView.setVisibility( View.GONE ); |
110 |
arrival = true; |
arrival = true; |
111 |
startDepartureFetcher(); |
startDepartureFetcher(); |
112 |
} |
} |
118 |
arrivalBtn.setBackgroundResource(R.drawable.custom_button); |
arrivalBtn.setBackgroundResource(R.drawable.custom_button); |
119 |
departureBtn.setBackgroundResource(R.drawable.custom_button); |
departureBtn.setBackgroundResource(R.drawable.custom_button); |
120 |
metroBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
metroBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
121 |
|
|
122 |
|
getListView().setVisibility( View.GONE ); |
123 |
|
metroView.setVisibility( View.VISIBLE ); |
124 |
|
startMetroFetcher(); |
125 |
} |
} |
126 |
}); |
}); |
127 |
|
|
149 |
metroBtn.setVisibility( View.GONE ); |
metroBtn.setVisibility( View.GONE ); |
150 |
} |
} |
151 |
|
|
152 |
|
metro = ProviderFactory.getMetroProvider(); |
153 |
|
|
154 |
if (station.isRegional() == false && station.isSTrain() == false) { |
if (station.isRegional() == false && station.isSTrain() == false) { |
155 |
getListView().setVisibility( View.GONE ); |
getListView().setVisibility( View.GONE ); |
156 |
findViewById(R.id.metroonly).setVisibility( View.VISIBLE ); |
metroView.setVisibility( View.VISIBLE ); |
157 |
|
|
158 |
departureBtn.setVisibility( View.GONE ); |
departureBtn.setVisibility( View.GONE ); |
159 |
arrivalBtn.setVisibility(View.GONE); |
arrivalBtn.setVisibility(View.GONE); |
160 |
metroBtn.setVisibility( View.GONE ); |
metroBtn.setVisibility( View.GONE ); |
161 |
|
|
162 |
|
if (savedInstanceState == null) { |
163 |
|
startMetroFetcher(); |
164 |
|
} else { |
165 |
|
metroBean = (MetroBean) savedInstanceState.getSerializable("metro"); |
166 |
|
loadMetroData(); |
167 |
|
} |
168 |
|
|
169 |
} else { |
} else { |
170 |
provider = ProviderFactory.getDepartureProvider(); |
provider = ProviderFactory.getDepartureProvider(); |
171 |
|
|
200 |
outState.putInt("selectedItemId", selectedItemId); |
outState.putInt("selectedItemId", selectedItemId); |
201 |
|
|
202 |
outState.putSerializable("departures", departures); |
outState.putSerializable("departures", departures); |
203 |
|
outState.putSerializable("metro", metroBean); |
204 |
} |
} |
205 |
|
|
206 |
|
|
212 |
if (fetcher != null) { |
if (fetcher != null) { |
213 |
fetcher.cancel(true); |
fetcher.cancel(true); |
214 |
} |
} |
215 |
|
|
216 |
|
if (metroFetcher != null) { |
217 |
|
metroFetcher.cancel(true); |
218 |
|
} |
219 |
} |
} |
220 |
|
|
221 |
@Override |
@Override |
309 |
fetcher.execute(station.getId()); |
fetcher.execute(station.getId()); |
310 |
} |
} |
311 |
|
|
312 |
|
void startMetroFetcher() { |
313 |
|
showDialog(DLG_PROGRESS); |
314 |
|
metroFetcher = new MetroFetcher(); |
315 |
|
metroFetcher.execute(station.getId()); |
316 |
|
} |
317 |
|
|
318 |
class DialogDismisser implements View.OnClickListener { |
class DialogDismisser implements View.OnClickListener { |
319 |
|
|
320 |
Dialog dlg; |
Dialog dlg; |
341 |
|
|
342 |
class DepartureFetcher extends AsyncTask<Integer, Void, Void> { |
class DepartureFetcher extends AsyncTask<Integer, Void, Void> { |
343 |
|
|
|
|
|
344 |
@Override |
@Override |
345 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
346 |
super.onPostExecute(result); |
super.onPostExecute(result); |
398 |
} |
} |
399 |
|
|
400 |
} |
} |
401 |
|
|
402 |
|
public void loadMetroData() { |
403 |
|
((TextView) findViewById(R.id.operations)).setText( metroBean.operationInfo ); |
404 |
|
((TextView) findViewById(R.id.plan)).setText( metroBean.plan ); |
405 |
|
|
406 |
|
|
407 |
|
TableLayout table = (TableLayout) findViewById(R.id.metrotable); |
408 |
|
table.removeAllViews(); |
409 |
|
|
410 |
|
TableRow head = new TableRow(this); |
411 |
|
|
412 |
|
TextView h1 = new TextView(this); |
413 |
|
h1.setText("Metro"); |
414 |
|
h1.setTypeface( Typeface.defaultFromStyle(Typeface.BOLD)); |
415 |
|
|
416 |
|
|
417 |
|
TableRow.LayoutParams params = new TableRow.LayoutParams(); |
418 |
|
params.span = 2; |
419 |
|
head.addView(h1, params); |
420 |
|
|
421 |
|
TextView h2 = new TextView(this); |
422 |
|
h2.setText("Om minutte"); |
423 |
|
h2.setTypeface( Typeface.defaultFromStyle(Typeface.BOLD)); |
424 |
|
head.addView(h2); |
425 |
|
|
426 |
|
|
427 |
|
|
428 |
|
table.addView(head); |
429 |
|
|
430 |
|
for (MetroEntry entry : metroBean.entries) { |
431 |
|
TableRow row = new TableRow(this); |
432 |
|
|
433 |
|
Log.e("Test", "" + entry.destination); |
434 |
|
|
435 |
|
TextView v1 = new TextView(this); |
436 |
|
v1.setText( entry.metro ); |
437 |
|
row.addView(v1); |
438 |
|
|
439 |
|
TextView v2 = new TextView(this); |
440 |
|
v2.setText( entry.destination ); |
441 |
|
row.addView(v2); |
442 |
|
|
443 |
|
TextView v3 = new TextView(this); |
444 |
|
v3.setText( entry.minutes ); |
445 |
|
row.addView(v3); |
446 |
|
|
447 |
|
table.addView(row); |
448 |
|
|
449 |
|
} |
450 |
|
findViewById(R.id.rootView).requestLayout(); |
451 |
|
} |
452 |
|
|
453 |
|
class MetroFetcher extends AsyncTask<Integer, Void, Void> { |
454 |
|
|
455 |
|
@Override |
456 |
|
protected void onPostExecute(Void result) { |
457 |
|
super.onPostExecute(result); |
458 |
|
|
459 |
|
|
460 |
|
|
461 |
|
pgDialog.dismiss(); |
462 |
|
|
463 |
|
if (metroBean != null) { |
464 |
|
loadMetroData(); |
465 |
|
} else { // communication or parse error |
466 |
|
commFailCounter++; |
467 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
468 |
|
builder.setMessage("Error finding metro data"); |
469 |
|
builder.setCancelable(true); |
470 |
|
if (commFailCounter < 3) { |
471 |
|
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
472 |
|
public void onClick(DialogInterface dialog, int id) { |
473 |
|
dialog.dismiss(); |
474 |
|
startMetroFetcher(); |
475 |
|
|
476 |
|
} |
477 |
|
}); |
478 |
|
} |
479 |
|
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
480 |
|
public void onClick(DialogInterface dialog, int id) { |
481 |
|
dialog.dismiss(); |
482 |
|
DepartureList.this.finish(); |
483 |
|
} |
484 |
|
}); |
485 |
|
|
486 |
|
try { |
487 |
|
builder.show(); |
488 |
|
} catch (android.view.WindowManager.BadTokenException e) { |
489 |
|
Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |
490 |
|
} |
491 |
|
} |
492 |
|
} |
493 |
|
|
494 |
|
@Override |
495 |
|
protected Void doInBackground(Integer... params) { |
496 |
|
metroBean = metro.lookupMetroInfo(params[0]); |
497 |
|
return null; |
498 |
|
} |
499 |
|
|
500 |
|
} |
501 |
|
|
502 |
} |
} |