44 |
public static final int NOPROVIDER = 1003; |
public static final int NOPROVIDER = 1003; |
45 |
public static final int LOCATIONFIXTIMEOUT = 1004; |
public static final int LOCATIONFIXTIMEOUT = 1004; |
46 |
|
|
|
public static final int OPTIONS_RESCAN = 2001; |
|
|
public static final int OPTIONS_NAMESEARCH = 2002; |
|
47 |
public static final int OPTIONS_MAP = 2003; |
public static final int OPTIONS_MAP = 2003; |
48 |
public static final int OPTIONS_ABOUT = 2004; |
public static final int OPTIONS_GPSINFO = 2004; |
49 |
public static final int OPTIONS_FAVORITES = 2005; |
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
public static final int DLG_PROGRESS = 3001; |
public static final int DLG_PROGRESS = 3001; |
54 |
public static final int DLG_STATIONNAME = 3002; |
public static final int DLG_STATIONNAME = 3002; |
55 |
|
|
56 |
/** Called when the activity is first created. */ |
static enum LookupMethod { |
57 |
|
ByLocation, |
58 |
|
ByName, |
59 |
|
ByList, |
60 |
|
MethodNone |
61 |
|
} |
62 |
|
|
63 |
|
|
64 |
String dialogMessage = ""; |
String dialogMessage = ""; |
65 |
ProgressDialog dialog; |
ProgressDialog dialog; |
66 |
LocationLookup locator = null; |
LocationLookup locator = null; |
67 |
LocatorTask locatorTask; |
FindStationsTask findStationsTask; |
68 |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
69 |
|
|
70 |
GeoPair location = new GeoPair(); |
GeoPair location = new GeoPair(); |
78 |
|
|
79 |
FavoritesMenu contextMenu = new FavoritesMenu(); |
FavoritesMenu contextMenu = new FavoritesMenu(); |
80 |
IntSet favorites = new IntSet(); |
IntSet favorites = new IntSet(); |
81 |
|
|
82 |
static enum LookupMethod { |
WelcomeScreen.ListType listType; |
83 |
ByLocation, |
String dialogTitle; |
|
ByName, |
|
|
ByList, |
|
|
MethodNone |
|
|
} |
|
|
|
|
84 |
SharedPreferences prefs; |
SharedPreferences prefs; |
85 |
|
|
86 |
/////////////////////////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////////////////////////// |
90 |
@Override |
@Override |
91 |
public void onCreate(Bundle savedInstanceState) { |
public void onCreate(Bundle savedInstanceState) { |
92 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
93 |
setContentView(R.layout.main); |
setContentView(R.layout.stationlist); |
94 |
|
|
95 |
|
|
96 |
adapter = new StationListAdapter(this); |
adapter = new StationListAdapter(this); |
109 |
} |
} |
110 |
|
|
111 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
112 |
startLookup(); |
listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); |
113 |
|
switch (listType) { |
114 |
|
case ListNearest: |
115 |
|
startLookup(); |
116 |
|
break; |
117 |
|
case ListSearch: |
118 |
|
this.showDialog(DLG_STATIONNAME); |
119 |
|
break; |
120 |
|
case ListFavorites: |
121 |
|
startFavoriteLookup(); |
122 |
|
break; |
123 |
|
default: |
124 |
|
// Not possible !?! |
125 |
|
} |
126 |
|
|
127 |
} else { |
} else { |
128 |
stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations"); |
stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations"); |
129 |
adapter.setStations(stations); |
adapter.setStations(stations); |
130 |
location = (GeoPair) savedInstanceState.getSerializable("location"); |
location = (GeoPair) savedInstanceState.getSerializable("location"); |
131 |
|
dialogTitle = savedInstanceState.getString("title"); |
132 |
|
setTitle(dialogTitle); |
133 |
} |
} |
134 |
} |
} |
135 |
|
|
141 |
dialog.dismiss(); |
dialog.dismiss(); |
142 |
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
143 |
outState.putSerializable("location", location); |
outState.putSerializable("location", location); |
144 |
|
outState.putString("title", dialogTitle); |
145 |
} |
} |
146 |
|
|
147 |
|
|
149 |
@Override |
@Override |
150 |
public boolean onCreateOptionsMenu(Menu menu) { |
public boolean onCreateOptionsMenu(Menu menu) { |
151 |
MenuItem item; |
MenuItem item; |
152 |
|
|
153 |
item = menu.add(0, OPTIONS_RESCAN, 0, "Find nearest stations"); |
item = menu.add(0, OPTIONS_MAP, 0, "Station map"); |
|
item.setIcon(android.R.drawable.ic_menu_mylocation); |
|
|
|
|
|
item = menu.add(0, OPTIONS_NAMESEARCH, 0, "Search for station"); |
|
|
item.setIcon(android.R.drawable.ic_menu_search); |
|
|
|
|
|
item = menu.add(0, OPTIONS_FAVORITES, 0, "Favorites"); |
|
|
item.setIcon(android.R.drawable.ic_menu_agenda); |
|
|
|
|
|
item = menu.add(0, OPTIONS_MAP, 0, "Show station map"); |
|
154 |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
155 |
|
|
156 |
item = menu.add(0, OPTIONS_ABOUT, 0, "About"); |
item = menu.add(0, OPTIONS_GPSINFO, 0, "GPS Info"); |
157 |
item.setIcon(android.R.drawable.ic_menu_info_details); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
158 |
|
|
159 |
return true; |
return true; |
160 |
} |
} |
161 |
|
|
163 |
public boolean onOptionsItemSelected(MenuItem item) { |
public boolean onOptionsItemSelected(MenuItem item) { |
164 |
boolean retval = true; |
boolean retval = true; |
165 |
|
|
166 |
|
//TODO: Cleanup |
167 |
switch (item.getItemId()) { |
switch (item.getItemId()) { |
|
case OPTIONS_RESCAN: |
|
|
startLookup(); |
|
|
break; |
|
|
case OPTIONS_NAMESEARCH: |
|
|
showDialog(DLG_STATIONNAME); |
|
|
break; |
|
|
case OPTIONS_FAVORITES: |
|
|
startFavoriteLookup(); |
|
|
break; |
|
168 |
case OPTIONS_MAP: |
case OPTIONS_MAP: |
169 |
|
|
170 |
Intent intent = new Intent(this,StationMapView.class); |
Intent intent = new Intent(this,StationMapView.class); |
179 |
|
|
180 |
startActivity(intent); |
startActivity(intent); |
181 |
break; |
break; |
182 |
case OPTIONS_ABOUT: |
case OPTIONS_GPSINFO: |
|
String ver = this.getResources().getString(R.string.app_version); |
|
|
|
|
183 |
Location loc = locator.getLocation(); |
Location loc = locator.getLocation(); |
184 |
StringBuffer message = new StringBuffer(); |
StringBuffer message = new StringBuffer(); |
|
message.append("TrainInfo DK v").append(ver).append("\n"); |
|
|
message.append("By Torben Nielsen\n"); |
|
|
message.append("\n"); |
|
185 |
message.append("Location info:\n"); |
message.append("Location info:\n"); |
186 |
message.append("-Obtained by: ").append(loc != null ? loc.getProvider() : "-").append("\n"); |
message.append("-Obtained by: ").append(loc != null ? loc.getProvider() : "-").append("\n"); |
187 |
message.append("-Accuracy: ").append(loc != null ? (int)loc.getAccuracy() : "-").append("m\n"); |
message.append("-Accuracy: ").append(loc != null ? (int)loc.getAccuracy() : "-").append("m\n"); |
302 |
dialogMessage = "Finding stations by name"; |
dialogMessage = "Finding stations by name"; |
303 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
304 |
|
|
305 |
locatorTask = new LocatorTask(); |
findStationsTask = new FindStationsTask(); |
306 |
locatorTask.searchByName(name, locator.getLocation()); |
findStationsTask.searchByName(name, locator.getLocation()); |
307 |
locatorTask.execute(); |
findStationsTask.execute(); |
308 |
|
|
309 |
} |
} |
310 |
|
|
311 |
public void startFavoriteLookup() { |
public void startFavoriteLookup() { |
312 |
|
|
313 |
if (favorites.toString().length() > 0) { |
if (favorites.size() > 0) { |
314 |
dialogMessage = "Loading favorites"; |
dialogMessage = "Loading favorites"; |
315 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
316 |
|
|
317 |
locatorTask = new LocatorTask(); |
findStationsTask = new FindStationsTask(); |
318 |
locatorTask.searchByIds(favorites.toString(), locator.getLocation()); |
findStationsTask.searchByIds(favorites.toString(), locator.getLocation()); |
319 |
locatorTask.execute(); |
findStationsTask.execute(); |
320 |
} else { |
} else { |
321 |
MessageBox.showMessage(this, "Favorite list is empty"); |
MessageBox.showMessage(this, "Favorite list is empty"); |
322 |
} |
} |
329 |
dialogMessage = "Finding nearby stations"; |
dialogMessage = "Finding nearby stations"; |
330 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
331 |
|
|
332 |
locatorTask = new LocatorTask(); |
findStationsTask = new FindStationsTask(); |
333 |
locatorTask.searchByLocation( locator.getLocation() ); |
findStationsTask.searchByLocation( locator.getLocation() ); |
334 |
locatorTask.execute(); |
findStationsTask.execute(); |
335 |
} |
} |
336 |
|
|
337 |
|
|
416 |
}; |
}; |
417 |
|
|
418 |
|
|
419 |
class LocatorTask extends AsyncTask<Void,Void,Void> { |
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
420 |
|
|
421 |
LookupMethod method = LookupMethod.MethodNone; |
LookupMethod method = LookupMethod.MethodNone; |
422 |
boolean success; |
boolean success; |
453 |
|
|
454 |
@Override |
@Override |
455 |
protected Void doInBackground(Void... params) { |
protected Void doInBackground(Void... params) { |
456 |
|
|
457 |
if (method.equals(LookupMethod.ByLocation)) |
switch (method) { |
458 |
|
case ByLocation: |
459 |
success = stationProvider.lookupStations(loc); |
success = stationProvider.lookupStations(loc); |
460 |
|
break; |
461 |
if (method.equals(LookupMethod.ByName)) |
case ByName: |
462 |
success = stationProvider.lookupStationsByName(name); |
success = stationProvider.lookupStationsByName(name); |
463 |
|
break; |
464 |
if (method.equals(LookupMethod.ByList)) |
case ByList: |
465 |
success = stationProvider.lookupStationsByIds(ids); |
success = stationProvider.lookupStationsByIds(ids); |
466 |
|
break; |
467 |
|
default: |
468 |
|
success = false; // not possible |
469 |
|
} |
470 |
|
|
471 |
|
|
472 |
Location dummy = new Location("gps"); |
Location dummy = new Location("gps"); |
473 |
List<StationBean> stations = stationProvider.getStations(); |
List<StationBean> stations = stationProvider.getStations(); |
476 |
String addr = lookupAddress(station.getLatitude(), station.getLongitude()); |
String addr = lookupAddress(station.getLatitude(), station.getLongitude()); |
477 |
station.setAddress(addr); |
station.setAddress(addr); |
478 |
|
|
479 |
|
|
480 |
if (method.equals(LookupMethod.ByName) || method.equals(LookupMethod.ByList)) { |
if (method.equals(LookupMethod.ByName) || method.equals(LookupMethod.ByList)) { |
481 |
dummy.setLatitude(station.getLatitude()); |
if (loc != null) { //only do the distance calc if we have a location |
482 |
dummy.setLongitude(station.getLongitude()); |
dummy.setLatitude(station.getLatitude()); |
483 |
station.setDistance( (int)loc.distanceTo(dummy) ); |
dummy.setLongitude(station.getLongitude()); |
484 |
|
station.setDistance( (int)loc.distanceTo(dummy) ); |
485 |
|
} else { |
486 |
|
station.setDistance(0); |
487 |
|
} |
488 |
} |
} |
489 |
|
|
490 |
} |
} |
491 |
|
|
492 |
return null; |
return null; |
497 |
super.onPostExecute(result); |
super.onPostExecute(result); |
498 |
dialog.dismiss(); |
dialog.dismiss(); |
499 |
|
|
500 |
|
//set title |
501 |
|
switch (method) { |
502 |
|
case ByLocation: |
503 |
|
dialogTitle = "Traininfo DK - Nearby stations"; |
504 |
|
break; |
505 |
|
case ByName: |
506 |
|
dialogTitle = "Traininfo DK - Search"; |
507 |
|
break; |
508 |
|
case ByList: |
509 |
|
dialogTitle = "Traininfo DK - Favorites"; |
510 |
|
break; |
511 |
|
default: |
512 |
|
dialogTitle = "";//not possible |
513 |
|
} |
514 |
|
|
515 |
|
StationList.this.setTitle(dialogTitle); |
516 |
|
//set title end |
517 |
|
|
518 |
if (success) { |
if (success) { |
519 |
if (stationProvider.getStations().size() == 0) |
if (stationProvider.getStations().size() == 0) |
520 |
MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! |
MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! |
562 |
selectedPosition = info.position; |
selectedPosition = info.position; |
563 |
int stationID = stations.get(selectedPosition).getId(); |
int stationID = stations.get(selectedPosition).getId(); |
564 |
|
|
565 |
if (!favorites.hasInt(stationID)) { |
if (!favorites.contains(stationID)) { |
566 |
menu.add(0, FAVORITES_ADD, 0, "Add to favorites"); |
menu.add(0, FAVORITES_ADD, 0, "Add to favorites"); |
567 |
} else { |
} else { |
568 |
menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites"); |
menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites"); |
578 |
favorites.add(stationID); |
favorites.add(stationID); |
579 |
Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show(); |
Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show(); |
580 |
} else { |
} else { |
581 |
|
|
582 |
favorites.remove(stationID); |
favorites.remove(stationID); |
583 |
Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show(); |
Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show(); |
584 |
|
|
585 |
|
|
586 |
|
if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) { |
587 |
|
stations.remove(selectedPosition); |
588 |
|
adapter.notifyDataSetChanged(); |
589 |
|
} |
590 |
} |
} |
591 |
Editor ed = prefs.edit(); |
Editor ed = prefs.edit(); |
592 |
ed.putString("favorites", favorites.toString()); |
ed.putString("favorites", favorites.toString()); |