55 |
public static final int DLG_PROGRESS = 3001; |
public static final int DLG_PROGRESS = 3001; |
56 |
public static final int DLG_STATIONNAME = 3002; |
public static final int DLG_STATIONNAME = 3002; |
57 |
|
|
58 |
/** Called when the activity is first created. */ |
static enum LookupMethod { |
59 |
|
ByLocation, |
60 |
|
ByName, |
61 |
|
ByList, |
62 |
|
MethodNone |
63 |
|
} |
64 |
|
|
65 |
|
|
66 |
String dialogMessage = ""; |
String dialogMessage = ""; |
67 |
ProgressDialog dialog; |
ProgressDialog dialog; |
68 |
LocationLookup locator = null; |
LocationLookup locator = null; |
69 |
LocatorTask locatorTask; |
FindStationsTask findStationsTask; |
70 |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
71 |
|
|
72 |
GeoPair location = new GeoPair(); |
GeoPair location = new GeoPair(); |
81 |
FavoritesMenu contextMenu = new FavoritesMenu(); |
FavoritesMenu contextMenu = new FavoritesMenu(); |
82 |
IntSet favorites = new IntSet(); |
IntSet favorites = new IntSet(); |
83 |
|
|
84 |
static enum LookupMethod { |
boolean showingFavorites = false; |
|
ByLocation, |
|
|
ByName, |
|
|
ByList, |
|
|
MethodNone |
|
|
} |
|
85 |
|
|
86 |
SharedPreferences prefs; |
SharedPreferences prefs; |
87 |
|
|
309 |
dialogMessage = "Finding stations by name"; |
dialogMessage = "Finding stations by name"; |
310 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
311 |
|
|
312 |
locatorTask = new LocatorTask(); |
findStationsTask = new FindStationsTask(); |
313 |
locatorTask.searchByName(name, locator.getLocation()); |
findStationsTask.searchByName(name, locator.getLocation()); |
314 |
locatorTask.execute(); |
findStationsTask.execute(); |
315 |
|
|
316 |
} |
} |
317 |
|
|
318 |
public void startFavoriteLookup() { |
public void startFavoriteLookup() { |
319 |
|
|
320 |
if (favorites.toString().length() > 0) { |
if (favorites.size() > 0) { |
321 |
dialogMessage = "Loading favorites"; |
dialogMessage = "Loading favorites"; |
322 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
323 |
|
|
324 |
locatorTask = new LocatorTask(); |
findStationsTask = new FindStationsTask(); |
325 |
locatorTask.searchByIds(favorites.toString(), locator.getLocation()); |
findStationsTask.searchByIds(favorites.toString(), locator.getLocation()); |
326 |
locatorTask.execute(); |
findStationsTask.execute(); |
327 |
} else { |
} else { |
328 |
MessageBox.showMessage(this, "Favorite list is empty"); |
MessageBox.showMessage(this, "Favorite list is empty"); |
329 |
} |
} |
336 |
dialogMessage = "Finding nearby stations"; |
dialogMessage = "Finding nearby stations"; |
337 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
338 |
|
|
339 |
locatorTask = new LocatorTask(); |
findStationsTask = new FindStationsTask(); |
340 |
locatorTask.searchByLocation( locator.getLocation() ); |
findStationsTask.searchByLocation( locator.getLocation() ); |
341 |
locatorTask.execute(); |
findStationsTask.execute(); |
342 |
} |
} |
343 |
|
|
344 |
|
|
423 |
}; |
}; |
424 |
|
|
425 |
|
|
426 |
class LocatorTask extends AsyncTask<Void,Void,Void> { |
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
427 |
|
|
428 |
LookupMethod method = LookupMethod.MethodNone; |
LookupMethod method = LookupMethod.MethodNone; |
429 |
boolean success; |
boolean success; |
460 |
|
|
461 |
@Override |
@Override |
462 |
protected Void doInBackground(Void... params) { |
protected Void doInBackground(Void... params) { |
463 |
|
|
464 |
if (method.equals(LookupMethod.ByLocation)) |
switch (method) { |
465 |
|
case ByLocation: |
466 |
success = stationProvider.lookupStations(loc); |
success = stationProvider.lookupStations(loc); |
467 |
|
break; |
468 |
if (method.equals(LookupMethod.ByName)) |
case ByName: |
469 |
success = stationProvider.lookupStationsByName(name); |
success = stationProvider.lookupStationsByName(name); |
470 |
|
break; |
471 |
if (method.equals(LookupMethod.ByList)) |
case ByList: |
472 |
success = stationProvider.lookupStationsByIds(ids); |
success = stationProvider.lookupStationsByIds(ids); |
473 |
|
break; |
474 |
|
default: |
475 |
|
success = false; // not possible |
476 |
|
} |
477 |
|
|
478 |
|
|
479 |
Location dummy = new Location("gps"); |
Location dummy = new Location("gps"); |
480 |
List<StationBean> stations = stationProvider.getStations(); |
List<StationBean> stations = stationProvider.getStations(); |
498 |
super.onPostExecute(result); |
super.onPostExecute(result); |
499 |
dialog.dismiss(); |
dialog.dismiss(); |
500 |
|
|
501 |
|
//set title |
502 |
|
String title; |
503 |
|
switch (method) { |
504 |
|
case ByLocation: |
505 |
|
title = "Traininfo DK - Nearby stations"; |
506 |
|
break; |
507 |
|
case ByName: |
508 |
|
title = "Traininfo DK - Search"; |
509 |
|
break; |
510 |
|
case ByList: |
511 |
|
title = "Traininfo DK - Favorites"; |
512 |
|
break; |
513 |
|
default: |
514 |
|
title = "";//not possible |
515 |
|
} |
516 |
|
|
517 |
|
StationList.this.setTitle(title); |
518 |
|
//set title end |
519 |
|
|
520 |
if (success) { |
if (success) { |
521 |
if (stationProvider.getStations().size() == 0) |
if (stationProvider.getStations().size() == 0) |
522 |
MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! |
MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! |
523 |
stations = stationProvider.getStations(); |
stations = stationProvider.getStations(); |
524 |
adapter.setStations( stations ); |
adapter.setStations( stations ); |
525 |
|
|
526 |
|
showingFavorites = method.equals(LookupMethod.ByList); |
527 |
|
|
528 |
} else { //communication or parse errors |
} else { //communication or parse errors |
529 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
566 |
selectedPosition = info.position; |
selectedPosition = info.position; |
567 |
int stationID = stations.get(selectedPosition).getId(); |
int stationID = stations.get(selectedPosition).getId(); |
568 |
|
|
569 |
if (!favorites.hasInt(stationID)) { |
if (!favorites.contains(stationID)) { |
570 |
menu.add(0, FAVORITES_ADD, 0, "Add to favorites"); |
menu.add(0, FAVORITES_ADD, 0, "Add to favorites"); |
571 |
} else { |
} else { |
572 |
menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites"); |
menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites"); |
582 |
favorites.add(stationID); |
favorites.add(stationID); |
583 |
Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show(); |
Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show(); |
584 |
} else { |
} else { |
585 |
|
|
586 |
favorites.remove(stationID); |
favorites.remove(stationID); |
587 |
Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show(); |
Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show(); |
588 |
|
|
589 |
|
if (showingFavorites) { |
590 |
|
stations.remove(selectedPosition); |
591 |
|
adapter.notifyDataSetChanged(); |
592 |
|
} |
593 |
} |
} |
594 |
Editor ed = prefs.edit(); |
Editor ed = prefs.edit(); |
595 |
ed.putString("favorites", favorites.toString()); |
ed.putString("favorites", favorites.toString()); |