10 |
import android.app.ProgressDialog; |
import android.app.ProgressDialog; |
11 |
import android.content.DialogInterface; |
import android.content.DialogInterface; |
12 |
import android.content.Intent; |
import android.content.Intent; |
13 |
|
import android.content.SharedPreferences; |
14 |
|
import android.content.SharedPreferences.Editor; |
15 |
import android.location.Address; |
import android.location.Address; |
16 |
import android.location.Geocoder; |
import android.location.Geocoder; |
17 |
import android.location.Location; |
import android.location.Location; |
20 |
import android.os.Handler; |
import android.os.Handler; |
21 |
import android.os.Message; |
import android.os.Message; |
22 |
import android.util.Log; |
import android.util.Log; |
23 |
|
import android.view.ContextMenu; |
24 |
import android.view.LayoutInflater; |
import android.view.LayoutInflater; |
25 |
import android.view.Menu; |
import android.view.Menu; |
26 |
import android.view.MenuItem; |
import android.view.MenuItem; |
27 |
import android.view.View; |
import android.view.View; |
28 |
|
import android.view.ContextMenu.ContextMenuInfo; |
29 |
|
import android.view.View.OnCreateContextMenuListener; |
30 |
|
import android.widget.AdapterView; |
31 |
import android.widget.EditText; |
import android.widget.EditText; |
32 |
import android.widget.ListView; |
import android.widget.ListView; |
33 |
|
import android.widget.Toast; |
34 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
import dk.thoerup.traininfo.provider.ProviderFactory; |
35 |
import dk.thoerup.traininfo.provider.StationProvider; |
import dk.thoerup.traininfo.provider.StationProvider; |
36 |
import dk.thoerup.traininfo.stationmap.GeoPair; |
import dk.thoerup.traininfo.stationmap.GeoPair; |
37 |
import dk.thoerup.traininfo.stationmap.StationMapView; |
import dk.thoerup.traininfo.stationmap.StationMapView; |
38 |
|
import dk.thoerup.traininfo.util.IntSet; |
39 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
40 |
|
|
41 |
public class StationList extends ListActivity { |
public class StationList extends ListActivity { |
48 |
public static final int OPTIONS_NAMESEARCH = 2002; |
public static final int OPTIONS_NAMESEARCH = 2002; |
49 |
public static final int OPTIONS_MAP = 2003; |
public static final int OPTIONS_MAP = 2003; |
50 |
public static final int OPTIONS_ABOUT = 2004; |
public static final int OPTIONS_ABOUT = 2004; |
51 |
|
public static final int OPTIONS_FAVORITES = 2005; |
52 |
|
|
53 |
|
|
54 |
|
|
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(); |
78 |
|
|
79 |
StationListAdapter adapter = null; |
StationListAdapter adapter = null; |
80 |
|
|
81 |
static enum LookupMethod { |
FavoritesMenu contextMenu = new FavoritesMenu(); |
82 |
ByLocation, |
IntSet favorites = new IntSet(); |
83 |
ByName, |
|
84 |
MethodNone |
boolean showingFavorites = false; |
85 |
} |
String dialogTitle; |
86 |
|
SharedPreferences prefs; |
87 |
|
|
88 |
/////////////////////////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////////////////////////// |
89 |
//Activity call backs |
//Activity call backs |
98 |
adapter = new StationListAdapter(this); |
adapter = new StationListAdapter(this); |
99 |
setListAdapter(adapter); |
setListAdapter(adapter); |
100 |
|
|
101 |
|
ListView lv = getListView(); |
102 |
|
lv.setOnCreateContextMenuListener(contextMenu); |
103 |
|
|
104 |
locator = new LocationLookup(this, stationsFetched); |
locator = new LocationLookup(this, stationsFetched); |
105 |
|
|
106 |
|
|
107 |
|
prefs = getSharedPreferences("TrainStation", 0); |
108 |
|
String favoriteString = prefs.getString("favorites", ""); |
109 |
|
if (! favoriteString.equals("") ) { |
110 |
|
favorites.fromString(favoriteString); |
111 |
|
} |
112 |
|
|
113 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
114 |
startLookup(); |
startLookup(); |
115 |
} else { |
} else { |
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 |
|
|
138 |
public boolean onCreateOptionsMenu(Menu menu) { |
public boolean onCreateOptionsMenu(Menu menu) { |
139 |
MenuItem item; |
MenuItem item; |
140 |
|
|
141 |
item = menu.add(0, OPTIONS_RESCAN, 0, "Find nearest stations"); |
item = menu.add(0, OPTIONS_RESCAN, 0, "Nearest stations"); |
142 |
item.setIcon(android.R.drawable.ic_menu_mylocation); |
item.setIcon(android.R.drawable.ic_menu_mylocation); |
143 |
|
|
144 |
item = menu.add(0, OPTIONS_NAMESEARCH, 0, "Search for station"); |
item = menu.add(0, OPTIONS_NAMESEARCH, 0, "Search for station"); |
145 |
item.setIcon(android.R.drawable.ic_menu_search); |
item.setIcon(android.R.drawable.ic_menu_search); |
146 |
|
|
147 |
item = menu.add(0, OPTIONS_MAP, 0, "Show station map"); |
item = menu.add(0, OPTIONS_FAVORITES, 0, "Favorites"); |
148 |
|
item.setIcon(android.R.drawable.ic_menu_agenda); |
149 |
|
|
150 |
|
item = menu.add(0, OPTIONS_MAP, 0, "Station map"); |
151 |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
152 |
|
|
153 |
item = menu.add(0, OPTIONS_ABOUT, 0, "About"); |
item = menu.add(0, OPTIONS_ABOUT, 0, "About"); |
167 |
case OPTIONS_NAMESEARCH: |
case OPTIONS_NAMESEARCH: |
168 |
showDialog(DLG_STATIONNAME); |
showDialog(DLG_STATIONNAME); |
169 |
break; |
break; |
170 |
|
case OPTIONS_FAVORITES: |
171 |
|
startFavoriteLookup(); |
172 |
|
break; |
173 |
case OPTIONS_MAP: |
case OPTIONS_MAP: |
174 |
|
|
175 |
Intent intent = new Intent(this,StationMapView.class); |
Intent intent = new Intent(this,StationMapView.class); |
204 |
|
|
205 |
return retval; |
return retval; |
206 |
} |
} |
207 |
|
|
208 |
|
|
209 |
|
|
210 |
|
@Override |
211 |
|
public boolean onContextItemSelected(MenuItem item) { |
212 |
|
contextMenu.onContextItemSelected(item); |
213 |
|
return true; |
214 |
|
|
215 |
|
|
216 |
|
} |
217 |
|
|
218 |
|
|
219 |
|
|
220 |
|
|
221 |
@Override |
@Override |
222 |
protected Dialog onCreateDialog(int id) { |
protected Dialog onCreateDialog(int id) { |
312 |
dialogMessage = "Finding stations by name"; |
dialogMessage = "Finding stations by name"; |
313 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
314 |
|
|
315 |
locatorTask = new LocatorTask(); |
findStationsTask = new FindStationsTask(); |
316 |
locatorTask.searchByName(name, locator.getLocation()); |
findStationsTask.searchByName(name, locator.getLocation()); |
317 |
locatorTask.execute(); |
findStationsTask.execute(); |
318 |
|
|
319 |
} |
} |
320 |
|
|
321 |
|
public void startFavoriteLookup() { |
322 |
|
|
323 |
|
if (favorites.size() > 0) { |
324 |
|
dialogMessage = "Loading favorites"; |
325 |
|
showDialog(DLG_PROGRESS); |
326 |
|
|
327 |
|
findStationsTask = new FindStationsTask(); |
328 |
|
findStationsTask.searchByIds(favorites.toString(), locator.getLocation()); |
329 |
|
findStationsTask.execute(); |
330 |
|
} else { |
331 |
|
MessageBox.showMessage(this, "Favorite list is empty"); |
332 |
|
} |
333 |
|
} |
334 |
|
|
335 |
|
|
336 |
|
|
339 |
dialogMessage = "Finding nearby stations"; |
dialogMessage = "Finding nearby stations"; |
340 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
341 |
|
|
342 |
locatorTask = new LocatorTask(); |
findStationsTask = new FindStationsTask(); |
343 |
locatorTask.searchByLocation( locator.getLocation() ); |
findStationsTask.searchByLocation( locator.getLocation() ); |
344 |
locatorTask.execute(); |
findStationsTask.execute(); |
345 |
} |
} |
346 |
|
|
347 |
|
|
401 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
402 |
|
|
403 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
404 |
builder.setMessage("GPS fix timed out"); |
builder.setMessage("Location fix timed out"); |
405 |
builder.setCancelable(true); |
builder.setCancelable(true); |
406 |
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
407 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
426 |
}; |
}; |
427 |
|
|
428 |
|
|
429 |
class LocatorTask extends AsyncTask<Void,Void,Void> { |
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
430 |
|
|
431 |
LookupMethod method = LookupMethod.MethodNone; |
LookupMethod method = LookupMethod.MethodNone; |
432 |
boolean success; |
boolean success; |
433 |
String name; |
String name; |
434 |
Location loc; |
Location loc; |
435 |
|
String ids; |
436 |
|
|
437 |
public void searchByName(String n, Location l) { |
public void searchByName(String n, Location l) { |
438 |
|
|
446 |
loc = l; |
loc = l; |
447 |
} |
} |
448 |
|
|
449 |
|
public void searchByIds(String id, Location l) { |
450 |
|
|
451 |
|
method = LookupMethod.ByList; |
452 |
|
loc = l; |
453 |
|
ids = id; |
454 |
|
} |
455 |
|
|
456 |
@Override |
@Override |
457 |
protected void onPreExecute() { |
protected void onPreExecute() { |
458 |
|
|
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 |
|
case ByName: |
472 |
|
success = stationProvider.lookupStationsByName(name); |
473 |
|
break; |
474 |
|
case ByList: |
475 |
|
success = stationProvider.lookupStationsByIds(ids); |
476 |
|
break; |
477 |
|
default: |
478 |
|
success = false; // not possible |
479 |
|
} |
480 |
|
|
|
if (method.equals(LookupMethod.ByName)) |
|
|
success = stationProvider.lookupStations(name); |
|
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 |
if (method.equals(LookupMethod.ByName) ) { |
|
490 |
dummy.setLatitude(station.getLatitude()); |
if (method.equals(LookupMethod.ByName) || method.equals(LookupMethod.ByList)) { |
491 |
dummy.setLongitude(station.getLongitude()); |
if (loc != null) { //only do the distance calc if we have a location |
492 |
station.setDistance( (int)loc.distanceTo(dummy) ); |
dummy.setLatitude(station.getLatitude()); |
493 |
|
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); |
558 |
} |
} |
559 |
} |
} |
560 |
} |
} |
561 |
|
|
562 |
|
|
563 |
|
class FavoritesMenu implements OnCreateContextMenuListener { |
564 |
|
private final static int FAVORITES_ADD = 9001; |
565 |
|
private final static int FAVORITES_REMOVE = 9002; |
566 |
|
|
567 |
|
private int selectedPosition; |
568 |
|
|
569 |
|
|
570 |
|
@Override |
571 |
|
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { |
572 |
|
|
573 |
|
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; |
574 |
|
selectedPosition = info.position; |
575 |
|
int stationID = stations.get(selectedPosition).getId(); |
576 |
|
|
577 |
|
if (!favorites.contains(stationID)) { |
578 |
|
menu.add(0, FAVORITES_ADD, 0, "Add to favorites"); |
579 |
|
} else { |
580 |
|
menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites"); |
581 |
|
} |
582 |
|
|
583 |
|
} |
584 |
|
|
585 |
|
public void onContextItemSelected(MenuItem item) { |
586 |
|
StationBean sb = stations.get(selectedPosition); |
587 |
|
|
588 |
|
int stationID = sb.getId(); |
589 |
|
if (item.getItemId() == FAVORITES_ADD) { |
590 |
|
favorites.add(stationID); |
591 |
|
Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show(); |
592 |
|
} else { |
593 |
|
|
594 |
|
favorites.remove(stationID); |
595 |
|
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(); |
603 |
|
ed.putString("favorites", favorites.toString()); |
604 |
|
ed.commit(); |
605 |
|
} |
606 |
|
} |
607 |
} |
} |