81 |
FavoritesMenu contextMenu = new FavoritesMenu(); |
FavoritesMenu contextMenu = new FavoritesMenu(); |
82 |
IntSet favorites = new IntSet(); |
IntSet favorites = new IntSet(); |
83 |
|
|
84 |
|
boolean showingFavorites = false; |
85 |
|
String dialogTitle; |
86 |
SharedPreferences prefs; |
SharedPreferences prefs; |
87 |
|
|
88 |
/////////////////////////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////////////////////////// |
116 |
stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations"); |
stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations"); |
117 |
adapter.setStations(stations); |
adapter.setStations(stations); |
118 |
location = (GeoPair) savedInstanceState.getSerializable("location"); |
location = (GeoPair) savedInstanceState.getSerializable("location"); |
119 |
|
dialogTitle = savedInstanceState.getString("title"); |
120 |
|
setTitle(dialogTitle); |
121 |
} |
} |
122 |
} |
} |
123 |
|
|
129 |
dialog.dismiss(); |
dialog.dismiss(); |
130 |
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
131 |
outState.putSerializable("location", location); |
outState.putSerializable("location", location); |
132 |
|
outState.putString("title", dialogTitle); |
133 |
} |
} |
134 |
|
|
135 |
|
|
463 |
|
|
464 |
@Override |
@Override |
465 |
protected Void doInBackground(Void... params) { |
protected Void doInBackground(Void... params) { |
466 |
|
|
467 |
if (method.equals(LookupMethod.ByLocation)) |
switch (method) { |
468 |
|
case ByLocation: |
469 |
success = stationProvider.lookupStations(loc); |
success = stationProvider.lookupStations(loc); |
470 |
|
break; |
471 |
if (method.equals(LookupMethod.ByName)) |
case ByName: |
472 |
success = stationProvider.lookupStationsByName(name); |
success = stationProvider.lookupStationsByName(name); |
473 |
|
break; |
474 |
if (method.equals(LookupMethod.ByList)) |
case ByList: |
475 |
success = stationProvider.lookupStationsByIds(ids); |
success = stationProvider.lookupStationsByIds(ids); |
476 |
|
break; |
477 |
|
default: |
478 |
|
success = false; // not possible |
479 |
|
} |
480 |
|
|
481 |
|
|
482 |
Location dummy = new Location("gps"); |
Location dummy = new Location("gps"); |
483 |
List<StationBean> stations = stationProvider.getStations(); |
List<StationBean> stations = stationProvider.getStations(); |
486 |
String addr = lookupAddress(station.getLatitude(), station.getLongitude()); |
String addr = lookupAddress(station.getLatitude(), station.getLongitude()); |
487 |
station.setAddress(addr); |
station.setAddress(addr); |
488 |
|
|
489 |
|
|
490 |
if (method.equals(LookupMethod.ByName) || method.equals(LookupMethod.ByList)) { |
if (method.equals(LookupMethod.ByName) || method.equals(LookupMethod.ByList)) { |
491 |
dummy.setLatitude(station.getLatitude()); |
if (loc != null) { //only do the distance calc if we have a location |
492 |
dummy.setLongitude(station.getLongitude()); |
dummy.setLatitude(station.getLatitude()); |
493 |
station.setDistance( (int)loc.distanceTo(dummy) ); |
dummy.setLongitude(station.getLongitude()); |
494 |
|
station.setDistance( (int)loc.distanceTo(dummy) ); |
495 |
|
} else { |
496 |
|
station.setDistance(0); |
497 |
|
} |
498 |
} |
} |
499 |
|
|
500 |
} |
} |
501 |
|
|
502 |
return null; |
return null; |
507 |
super.onPostExecute(result); |
super.onPostExecute(result); |
508 |
dialog.dismiss(); |
dialog.dismiss(); |
509 |
|
|
510 |
|
//set title |
511 |
|
switch (method) { |
512 |
|
case ByLocation: |
513 |
|
dialogTitle = "Traininfo DK - Nearby stations"; |
514 |
|
break; |
515 |
|
case ByName: |
516 |
|
dialogTitle = "Traininfo DK - Search"; |
517 |
|
break; |
518 |
|
case ByList: |
519 |
|
dialogTitle = "Traininfo DK - Favorites"; |
520 |
|
break; |
521 |
|
default: |
522 |
|
dialogTitle = "";//not possible |
523 |
|
} |
524 |
|
|
525 |
|
StationList.this.setTitle(dialogTitle); |
526 |
|
//set title end |
527 |
|
|
528 |
if (success) { |
if (success) { |
529 |
if (stationProvider.getStations().size() == 0) |
if (stationProvider.getStations().size() == 0) |
530 |
MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! |
MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! |
531 |
stations = stationProvider.getStations(); |
stations = stationProvider.getStations(); |
532 |
adapter.setStations( stations ); |
adapter.setStations( stations ); |
533 |
|
|
534 |
|
showingFavorites = method.equals(LookupMethod.ByList); |
535 |
|
|
536 |
} else { //communication or parse errors |
} else { //communication or parse errors |
537 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
574 |
selectedPosition = info.position; |
selectedPosition = info.position; |
575 |
int stationID = stations.get(selectedPosition).getId(); |
int stationID = stations.get(selectedPosition).getId(); |
576 |
|
|
577 |
if (!favorites.hasInt(stationID)) { |
if (!favorites.contains(stationID)) { |
578 |
menu.add(0, FAVORITES_ADD, 0, "Add to favorites"); |
menu.add(0, FAVORITES_ADD, 0, "Add to favorites"); |
579 |
} else { |
} else { |
580 |
menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites"); |
menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites"); |
590 |
favorites.add(stationID); |
favorites.add(stationID); |
591 |
Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show(); |
Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show(); |
592 |
} else { |
} else { |
593 |
|
|
594 |
favorites.remove(stationID); |
favorites.remove(stationID); |
595 |
Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show(); |
Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show(); |
596 |
|
|
597 |
|
if (showingFavorites) { |
598 |
|
stations.remove(selectedPosition); |
599 |
|
adapter.notifyDataSetChanged(); |
600 |
|
} |
601 |
} |
} |
602 |
Editor ed = prefs.edit(); |
Editor ed = prefs.edit(); |
603 |
ed.putString("favorites", favorites.toString()); |
ed.putString("favorites", favorites.toString()); |