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 { |
39 |
public static final int DLG_PROGRESS = 1; |
public static final int DLG_PROGRESS = 1; |
40 |
static final int MENU_MAP = 100; |
static final int MENU_MAP = 100; |
41 |
static final int MENU_NOTIFICATIONS = 101; |
static final int MENU_NOTIFICATIONS = 101; |
42 |
|
static final int MENU_METROMAP= 102; |
43 |
|
|
44 |
|
|
45 |
DepartureListAdapter adapter; |
DepartureListAdapter adapter; |
46 |
DepartureProvider provider; |
DepartureProvider provider; |
47 |
DepartureBean departures; |
DepartureBean departures; |
48 |
|
|
49 |
|
MetroBean metroBean; |
50 |
|
MetroProvider metro; |
51 |
|
|
52 |
int selectedItemId; |
int selectedItemId; |
53 |
//DepartureBean currentDeparture; |
//DepartureBean currentDeparture; |
54 |
|
|
55 |
ProgressDialog pgDialog; |
ProgressDialog pgDialog; |
56 |
|
|
57 |
DepartureFetcher fetcher; |
DepartureFetcher fetcher; |
58 |
|
MetroFetcher metroFetcher; |
59 |
|
|
60 |
StationBean station; |
StationBean station; |
61 |
|
|
84 |
final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn); |
final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn); |
85 |
final Button metroBtn = (Button) findViewById(R.id.metrobtn); |
final Button metroBtn = (Button) findViewById(R.id.metrobtn); |
86 |
|
|
87 |
|
final View metroView = findViewById(R.id.metroonly); |
88 |
|
|
89 |
departureBtn.setOnClickListener( new OnClickListener() { |
departureBtn.setOnClickListener( new OnClickListener() { |
90 |
@Override |
@Override |
91 |
public void onClick(View arg0) { |
public void onClick(View arg0) { |
92 |
arrivalBtn.setBackgroundResource(R.drawable.custom_button); |
arrivalBtn.setBackgroundResource(R.drawable.custom_button); |
93 |
departureBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
departureBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
94 |
metroBtn.setBackgroundResource(R.drawable.custom_button); |
metroBtn.setBackgroundResource(R.drawable.custom_button); |
95 |
|
|
96 |
|
getListView().setVisibility( View.VISIBLE ); |
97 |
|
metroView.setVisibility( View.GONE ); |
98 |
arrival = false; |
arrival = false; |
99 |
startDepartureFetcher(); |
startDepartureFetcher(); |
100 |
} |
} |
105 |
arrivalBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
arrivalBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
106 |
departureBtn.setBackgroundResource(R.drawable.custom_button); |
departureBtn.setBackgroundResource(R.drawable.custom_button); |
107 |
metroBtn.setBackgroundResource(R.drawable.custom_button); |
metroBtn.setBackgroundResource(R.drawable.custom_button); |
108 |
|
|
109 |
|
getListView().setVisibility( View.VISIBLE ); |
110 |
|
metroView.setVisibility( View.GONE ); |
111 |
arrival = true; |
arrival = true; |
112 |
startDepartureFetcher(); |
startDepartureFetcher(); |
113 |
} |
} |
119 |
arrivalBtn.setBackgroundResource(R.drawable.custom_button); |
arrivalBtn.setBackgroundResource(R.drawable.custom_button); |
120 |
departureBtn.setBackgroundResource(R.drawable.custom_button); |
departureBtn.setBackgroundResource(R.drawable.custom_button); |
121 |
metroBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
metroBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
122 |
|
|
123 |
|
getListView().setVisibility( View.GONE ); |
124 |
|
metroView.setVisibility( View.VISIBLE ); |
125 |
|
startMetroFetcher(); |
126 |
} |
} |
127 |
}); |
}); |
128 |
|
|
150 |
metroBtn.setVisibility( View.GONE ); |
metroBtn.setVisibility( View.GONE ); |
151 |
} |
} |
152 |
|
|
153 |
|
metro = ProviderFactory.getMetroProvider(); |
154 |
|
|
155 |
if (station.isRegional() == false && station.isSTrain() == false) { |
if (station.isRegional() == false && station.isSTrain() == false) { |
156 |
getListView().setVisibility( View.GONE ); |
getListView().setVisibility( View.GONE ); |
157 |
findViewById(R.id.metroonly).setVisibility( View.VISIBLE ); |
metroView.setVisibility( View.VISIBLE ); |
158 |
|
|
159 |
departureBtn.setVisibility( View.GONE ); |
departureBtn.setVisibility( View.GONE ); |
160 |
arrivalBtn.setVisibility(View.GONE); |
arrivalBtn.setVisibility(View.GONE); |
161 |
metroBtn.setVisibility( View.GONE ); |
metroBtn.setVisibility( View.GONE ); |
162 |
|
|
163 |
|
if (savedInstanceState == null) { |
164 |
|
startMetroFetcher(); |
165 |
|
} else { |
166 |
|
metroBean = (MetroBean) savedInstanceState.getSerializable("metro"); |
167 |
|
loadMetroData(); |
168 |
|
} |
169 |
|
|
170 |
} else { |
} else { |
171 |
provider = ProviderFactory.getDepartureProvider(); |
provider = ProviderFactory.getDepartureProvider(); |
172 |
|
|
201 |
outState.putInt("selectedItemId", selectedItemId); |
outState.putInt("selectedItemId", selectedItemId); |
202 |
|
|
203 |
outState.putSerializable("departures", departures); |
outState.putSerializable("departures", departures); |
204 |
|
outState.putSerializable("metro", metroBean); |
205 |
} |
} |
206 |
|
|
207 |
|
|
213 |
if (fetcher != null) { |
if (fetcher != null) { |
214 |
fetcher.cancel(true); |
fetcher.cancel(true); |
215 |
} |
} |
216 |
|
|
217 |
|
if (metroFetcher != null) { |
218 |
|
metroFetcher.cancel(true); |
219 |
|
} |
220 |
} |
} |
221 |
|
|
222 |
@Override |
@Override |
273 |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
274 |
|
|
275 |
item = menu.add(0, MENU_NOTIFICATIONS, 0, getString(R.string.departurelist_notifications) ); |
item = menu.add(0, MENU_NOTIFICATIONS, 0, getString(R.string.departurelist_notifications) ); |
276 |
item.setIcon(android.R.drawable.ic_menu_info_details); |
item.setIcon(android.R.drawable.ic_menu_info_details); |
|
|
|
277 |
|
|
278 |
boolean notifEnabled = hasNotifications(); |
boolean notifEnabled = hasNotifications(); |
279 |
item.setEnabled(notifEnabled); |
item.setEnabled(notifEnabled); |
280 |
|
|
281 |
|
if (station.isMetro()) { |
282 |
|
item = menu.add(0, MENU_METROMAP, 0, "Metro" ); //TODO:translate!?! |
283 |
|
item.setIcon(android.R.drawable.ic_menu_mapmode); |
284 |
|
} |
285 |
|
|
286 |
|
|
287 |
return true; |
return true; |
288 |
} |
} |
302 |
startActivity(i); |
startActivity(i); |
303 |
res = true; |
res = true; |
304 |
break; |
break; |
305 |
|
case MENU_METROMAP: |
306 |
|
Intent metroMap = new Intent(this,dk.thoerup.traininfo.MetroMap.class); |
307 |
|
startActivity(metroMap); |
308 |
|
res = true; |
309 |
|
break; |
310 |
default: |
default: |
311 |
res = super.onOptionsItemSelected(item); |
res = super.onOptionsItemSelected(item); |
312 |
} |
} |
319 |
fetcher.execute(station.getId()); |
fetcher.execute(station.getId()); |
320 |
} |
} |
321 |
|
|
322 |
|
void startMetroFetcher() { |
323 |
|
showDialog(DLG_PROGRESS); |
324 |
|
metroFetcher = new MetroFetcher(); |
325 |
|
metroFetcher.execute(station.getId()); |
326 |
|
} |
327 |
|
|
328 |
class DialogDismisser implements View.OnClickListener { |
class DialogDismisser implements View.OnClickListener { |
329 |
|
|
330 |
Dialog dlg; |
Dialog dlg; |
351 |
|
|
352 |
class DepartureFetcher extends AsyncTask<Integer, Void, Void> { |
class DepartureFetcher extends AsyncTask<Integer, Void, Void> { |
353 |
|
|
|
|
|
354 |
@Override |
@Override |
355 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
356 |
super.onPostExecute(result); |
super.onPostExecute(result); |
408 |
} |
} |
409 |
|
|
410 |
} |
} |
411 |
|
|
412 |
|
public void loadMetroData() { |
413 |
|
((TextView) findViewById(R.id.operations)).setText( metroBean.operationInfo ); |
414 |
|
((TextView) findViewById(R.id.plan)).setText( metroBean.plan ); |
415 |
|
|
416 |
|
|
417 |
|
TableLayout table = (TableLayout) findViewById(R.id.metrotable); |
418 |
|
table.removeAllViews(); |
419 |
|
|
420 |
|
TableRow head = new TableRow(this); |
421 |
|
|
422 |
|
TextView h1 = new TextView(this); |
423 |
|
h1.setText("Metro"); |
424 |
|
h1.setTypeface( Typeface.defaultFromStyle(Typeface.BOLD)); |
425 |
|
|
426 |
|
|
427 |
|
TableRow.LayoutParams params = new TableRow.LayoutParams(); |
428 |
|
params.span = 2; |
429 |
|
head.addView(h1, params); |
430 |
|
|
431 |
|
|
432 |
|
|
433 |
|
TextView h2 = new TextView(this); |
434 |
|
h2.setTypeface( Typeface.defaultFromStyle(Typeface.BOLD)); |
435 |
|
h2.setText("Om minutter"); |
436 |
|
|
437 |
|
head.addView(h2,params); |
438 |
|
|
439 |
|
|
440 |
|
|
441 |
|
table.addView(head); |
442 |
|
|
443 |
|
for (MetroEntry entry : metroBean.entries) { |
444 |
|
TableRow row = new TableRow(this); |
445 |
|
|
446 |
|
Log.e("Test", "" + entry.destination); |
447 |
|
|
448 |
|
TextView v1 = new TextView(this); |
449 |
|
v1.setText( entry.metro ); |
450 |
|
row.addView(v1); |
451 |
|
|
452 |
|
TextView v2 = new TextView(this); |
453 |
|
v2.setText( entry.destination ); |
454 |
|
row.addView(v2); |
455 |
|
|
456 |
|
TextView v3 = new TextView(this); |
457 |
|
v3.setText( entry.minutes ); |
458 |
|
row.addView(v3); |
459 |
|
|
460 |
|
table.addView(row); |
461 |
|
|
462 |
|
} |
463 |
|
findViewById(R.id.rootView).requestLayout(); |
464 |
|
} |
465 |
|
|
466 |
|
class MetroFetcher extends AsyncTask<Integer, Void, Void> { |
467 |
|
|
468 |
|
@Override |
469 |
|
protected void onPostExecute(Void result) { |
470 |
|
super.onPostExecute(result); |
471 |
|
|
472 |
|
|
473 |
|
|
474 |
|
pgDialog.dismiss(); |
475 |
|
|
476 |
|
if (metroBean != null) { |
477 |
|
loadMetroData(); |
478 |
|
} else { // communication or parse error |
479 |
|
commFailCounter++; |
480 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
481 |
|
builder.setMessage("Error finding metro data"); |
482 |
|
builder.setCancelable(true); |
483 |
|
if (commFailCounter < 3) { |
484 |
|
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
485 |
|
public void onClick(DialogInterface dialog, int id) { |
486 |
|
dialog.dismiss(); |
487 |
|
startMetroFetcher(); |
488 |
|
|
489 |
|
} |
490 |
|
}); |
491 |
|
} |
492 |
|
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
493 |
|
public void onClick(DialogInterface dialog, int id) { |
494 |
|
dialog.dismiss(); |
495 |
|
DepartureList.this.finish(); |
496 |
|
} |
497 |
|
}); |
498 |
|
|
499 |
|
try { |
500 |
|
builder.show(); |
501 |
|
} catch (android.view.WindowManager.BadTokenException e) { |
502 |
|
Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |
503 |
|
} |
504 |
|
} |
505 |
|
} |
506 |
|
|
507 |
|
@Override |
508 |
|
protected Void doInBackground(Integer... params) { |
509 |
|
metroBean = metro.lookupMetroInfo(params[0]); |
510 |
|
return null; |
511 |
|
} |
512 |
|
|
513 |
|
} |
514 |
|
|
515 |
} |
} |