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.ActivityNotFoundException; |
15 |
import android.content.DialogInterface; |
import android.content.DialogInterface; |
16 |
import android.content.Intent; |
import android.content.Intent; |
17 |
|
import android.content.SharedPreferences; |
18 |
import android.graphics.Typeface; |
import android.graphics.Typeface; |
19 |
import android.net.Uri; |
import android.net.Uri; |
20 |
import android.os.AsyncTask; |
import android.os.AsyncTask; |
21 |
import android.os.Bundle; |
import android.os.Bundle; |
22 |
|
import android.preference.PreferenceManager; |
23 |
import android.util.Log; |
import android.util.Log; |
24 |
import android.view.Menu; |
import android.view.Menu; |
25 |
import android.view.MenuItem; |
import android.view.MenuItem; |
30 |
import android.widget.TableLayout; |
import android.widget.TableLayout; |
31 |
import android.widget.TableRow; |
import android.widget.TableRow; |
32 |
import android.widget.TextView; |
import android.widget.TextView; |
33 |
|
import android.widget.Toast; |
34 |
import dk.thoerup.android.traininfo.common.DepartureBean; |
import dk.thoerup.android.traininfo.common.DepartureBean; |
35 |
import dk.thoerup.android.traininfo.common.DepartureEntry; |
import dk.thoerup.android.traininfo.common.DepartureEntry; |
36 |
import dk.thoerup.android.traininfo.common.MetroBean; |
import dk.thoerup.android.traininfo.common.MetroBean; |
37 |
import dk.thoerup.android.traininfo.common.MetroBean.MetroEntry; |
import dk.thoerup.android.traininfo.common.MetroBean.MetroEntry; |
38 |
import dk.thoerup.android.traininfo.common.StationBean.StationEntry; |
import dk.thoerup.android.traininfo.common.StationEntry; |
39 |
import dk.thoerup.traininfo.provider.DepartureProvider; |
import dk.thoerup.traininfo.provider.DepartureProvider; |
40 |
import dk.thoerup.traininfo.provider.MetroProvider; |
import dk.thoerup.traininfo.provider.MetroProvider; |
41 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
import dk.thoerup.traininfo.provider.ProviderFactory; |
46 |
public static final int DLG_PROGRESS = 1; |
public static final int DLG_PROGRESS = 1; |
47 |
static final int MENU_MAP = 100; |
static final int MENU_MAP = 100; |
48 |
static final int MENU_NOTIFICATIONS = 101; |
static final int MENU_NOTIFICATIONS = 101; |
49 |
static final int MENU_METROMAP= 102; |
static final int MENU_METROMAP = 102; |
50 |
|
static final int MENU_TOGGLEDETAILS= 103; |
51 |
|
|
52 |
|
|
53 |
DepartureListAdapter adapter; |
DepartureListAdapter adapter; |
69 |
|
|
70 |
String trainType = "REGIONAL"; |
String trainType = "REGIONAL"; |
71 |
|
|
72 |
boolean arrival = false; |
boolean arrival = false; |
73 |
|
|
74 |
int commFailCounter = 0; |
int commFailCounter = 0; |
75 |
|
|
80 |
|
|
81 |
adapter = new DepartureListAdapter(this); |
adapter = new DepartureListAdapter(this); |
82 |
setListAdapter(adapter); |
setListAdapter(adapter); |
83 |
|
|
84 |
Intent launchedBy = getIntent(); |
Intent launchedBy = getIntent(); |
85 |
|
|
86 |
station = (StationEntry) launchedBy.getSerializableExtra("stationbean"); |
station = (StationEntry) launchedBy.getSerializableExtra("stationbean"); |
197 |
|
|
198 |
Log.e("Station", station.toCSV() ); |
Log.e("Station", station.toCSV() ); |
199 |
|
|
200 |
|
|
201 |
|
|
202 |
if (station.isMetro() == false) { |
if (station.isMetro() == false) { |
203 |
metroBtn.setVisibility( View.GONE ); |
metroBtn.setVisibility( View.GONE ); |
204 |
} |
} |
227 |
trainType = "STOG"; |
trainType = "STOG"; |
228 |
|
|
229 |
} |
} |
230 |
|
//Both enabled - use preferred from preferences |
231 |
|
if (station.isRegional() == true && station.isStrain() == true ) { |
232 |
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); |
233 |
|
trainType = prefs.getString("traintype", "REGIONAL"); //default value is gps |
234 |
|
|
235 |
|
if (trainType.equals("STOG") ) { |
236 |
|
stogBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
237 |
|
regionalBtn.setBackgroundResource(R.drawable.custom_button); |
238 |
|
} |
239 |
|
} |
240 |
|
|
241 |
|
|
242 |
if (station.isRegional() == false && station.isStrain() == false) { |
if (station.isRegional() == false && station.isStrain() == false) { |
277 |
} |
} |
278 |
} |
} |
279 |
|
|
280 |
|
|
281 |
|
|
282 |
boolean hasNotifications() { |
boolean hasNotifications() { |
283 |
return (departures != null && departures.notifications.size() > 0); |
return (departures != null && departures.notifications.size() > 0); |
284 |
} |
} |
314 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
315 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
316 |
|
|
317 |
|
//how can this happen ?? |
318 |
|
if (departures == null || departures.entries == null || departures.entries.size() == 0) { |
319 |
|
Toast.makeText(this, "No departures in list ?!?", Toast.LENGTH_LONG).show(); //TODO: translate |
320 |
|
return; |
321 |
|
} |
322 |
|
|
323 |
selectedItemId = position; |
selectedItemId = position; |
324 |
|
|
325 |
DepartureEntry dep = departures.entries.get(selectedItemId); |
DepartureEntry dep = departures.entries.get(selectedItemId); |
360 |
|
|
361 |
|
|
362 |
|
|
363 |
|
@Override |
364 |
|
public boolean onPrepareOptionsMenu(Menu menu) { |
365 |
|
super.onPrepareOptionsMenu(menu); |
366 |
|
|
367 |
|
MenuItem item = menu.findItem( MENU_NOTIFICATIONS ); |
368 |
|
boolean notifEnabled = hasNotifications(); |
369 |
|
item.setEnabled(notifEnabled); |
370 |
|
|
371 |
|
return true; |
372 |
|
} |
373 |
|
|
374 |
@Override |
@Override |
375 |
public boolean onCreateOptionsMenu(Menu menu) { |
public boolean onCreateOptionsMenu(Menu menu) { |
376 |
MenuItem item; |
MenuItem item; |
377 |
|
|
378 |
|
|
379 |
|
item = menu.add(0, MENU_TOGGLEDETAILS, 0, getString(R.string.departurelist_toggledetails)); |
380 |
|
item.setIcon(android.R.drawable.ic_menu_view); |
381 |
|
|
382 |
item = menu.add(0, MENU_MAP, 0, getString(R.string.departurelist_showonmap) ); |
item = menu.add(0, MENU_MAP, 0, getString(R.string.departurelist_showonmap) ); |
383 |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
384 |
|
|
391 |
if (station.isMetro()) { |
if (station.isMetro()) { |
392 |
item = menu.add(0, MENU_METROMAP, 0, "Metro" ); //TODO:translate!?! |
item = menu.add(0, MENU_METROMAP, 0, "Metro" ); //TODO:translate!?! |
393 |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
394 |
} |
} |
|
|
|
395 |
|
|
396 |
return true; |
return true; |
397 |
} |
} |
398 |
|
|
399 |
@Override |
@Override |
400 |
public boolean onOptionsItemSelected(MenuItem item) { |
public boolean onOptionsItemSelected(MenuItem item) { |
401 |
boolean res; |
boolean res = true; |
402 |
switch(item.getItemId()) { |
switch(item.getItemId()) { |
403 |
case MENU_MAP: |
case MENU_MAP: |
404 |
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude() + "?z=16"); |
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude() + "?z=16"); |
405 |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
|
406 |
res = true; |
try { |
407 |
|
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
408 |
|
} catch (ActivityNotFoundException anfe) { |
409 |
|
Toast.makeText(this, "Could not launch google maps", Toast.LENGTH_LONG).show(); |
410 |
|
} |
411 |
|
|
412 |
break; |
break; |
413 |
case MENU_NOTIFICATIONS: |
case MENU_NOTIFICATIONS: |
414 |
Intent i = new Intent(this,dk.thoerup.traininfo.NotificationList.class); |
Intent i = new Intent(this,dk.thoerup.traininfo.NotificationList.class); |
415 |
i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications); |
i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications); |
416 |
startActivity(i); |
startActivity(i); |
|
res = true; |
|
417 |
break; |
break; |
418 |
case MENU_METROMAP: |
case MENU_METROMAP: |
419 |
Intent metroMap = new Intent(this,dk.thoerup.traininfo.MetroMap.class); |
Intent metroMap = new Intent(this,dk.thoerup.traininfo.MetroMap.class); |
420 |
startActivity(metroMap); |
startActivity(metroMap); |
|
res = true; |
|
421 |
break; |
break; |
422 |
|
case MENU_TOGGLEDETAILS: |
423 |
|
adapter.toggleShowDetails(); |
424 |
|
break; |
425 |
default: |
default: |
426 |
res = super.onOptionsItemSelected(item); |
res = super.onOptionsItemSelected(item); |
427 |
} |
} |
472 |
|
|
473 |
|
|
474 |
pgDialog.dismiss(); |
pgDialog.dismiss(); |
475 |
//TODO: differentiate whether it was a communication error with my backend |
|
476 |
//or that the backend failed because bane.dk was unavailable |
if (departures != null && departures.errorCode == null) { |
|
if (departures != null) { |
|
477 |
commFailCounter = 0; |
commFailCounter = 0; |
478 |
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 |
479 |
adapter.setDepartures(departures.entries); |
adapter.setDepartures(departures.entries); |
480 |
DepartureList.this.getListView().setVisibility(View.VISIBLE); |
DepartureList.this.getListView().setVisibility(View.VISIBLE); |
481 |
|
|
482 |
|
|
483 |
// handle notification icon. |
// handle notification icon. |
484 |
if ( hasNotifications() ) { |
View notifIcon = findViewById(R.id.notifIcon); |
485 |
View notifIcon = findViewById(R.id.notifIcon); |
if ( hasNotifications() ) { |
486 |
notifIcon.setVisibility(View.VISIBLE); |
notifIcon.setVisibility(View.VISIBLE); |
487 |
notifIcon.setClickable(true); |
notifIcon.setClickable(true); |
488 |
notifIcon.setOnClickListener( new View.OnClickListener() { |
notifIcon.setOnClickListener( new View.OnClickListener() { |
493 |
startActivity(i); |
startActivity(i); |
494 |
} |
} |
495 |
}); |
}); |
496 |
} |
} else { |
497 |
|
notifIcon.setVisibility(View.INVISIBLE); |
498 |
|
} |
499 |
|
|
500 |
if (departures.entries.size() != 0) { |
if (departures.entries.size() == 0) { |
501 |
int msgId = (arrival==false) ? R.string.departurelist_nodepartures : R.string.departurelist_noarrivals; |
int msgId = (arrival==false) ? R.string.departurelist_nodepartures : R.string.departurelist_noarrivals; |
502 |
MessageBox.showMessage(DepartureList.this, getString(msgId), false); |
MessageBox.showMessage(DepartureList.this, getString(msgId), false); |
503 |
} |
} |
504 |
} else { // communication or parse error |
} else { // communication or parse error |
505 |
commFailCounter++; |
commFailCounter++; |
506 |
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
507 |
builder.setMessage("Error finding departures"); |
|
508 |
|
if (departures != null && departures.errorCode != null ) { //got an error xml back |
509 |
|
commFailCounter = 10; |
510 |
|
builder.setMessage( getString(R.string.no_backend) ); |
511 |
|
} else { |
512 |
|
builder.setMessage( getString(R.string.departurelist_fetcherror) ); |
513 |
|
} |
514 |
builder.setCancelable(true); |
builder.setCancelable(true); |
515 |
|
|
516 |
if (commFailCounter < 3) { |
if (commFailCounter < 3) { |
517 |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
518 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |