36 |
import dk.thoerup.traininfo.util.IntSet; |
import dk.thoerup.traininfo.util.IntSet; |
37 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
38 |
|
|
39 |
|
import static dk.thoerup.traininfo.R.string.*; |
40 |
|
|
41 |
public class StationList extends ListActivity { |
public class StationList extends ListActivity { |
42 |
public static final int GOTLOCATION = 1001; |
public static final int GOTLOCATION = 1001; |
43 |
public static final int GOTSTATIONLIST = 1002; |
public static final int GOTSTATIONLIST = 1002; |
135 |
|
|
136 |
} |
} |
137 |
protected void setTitle() { |
protected void setTitle() { |
138 |
String dialogTitle = getResources().getString(R.string.app_name); |
String dialogTitle = getResources().getString(app_name); |
139 |
switch (listType) { |
switch (listType) { |
140 |
case ListNearest: |
case ListNearest: |
141 |
dialogTitle += " - Nearby stations"; |
dialogTitle += " - " + getString(stationlist_nearbystations); |
142 |
break; |
break; |
143 |
case ListSearch: |
case ListSearch: |
144 |
dialogTitle += " - Search"; |
dialogTitle += " - " + getString(stationlist_search); |
145 |
break; |
break; |
146 |
case ListFavorites: |
case ListFavorites: |
147 |
dialogTitle += " - Favorites"; |
dialogTitle += " - " + getString(stationlist_favorites); |
148 |
break; |
break; |
149 |
default: |
default: |
150 |
dialogTitle = "";//not possible |
dialogTitle = "";//not possible |
171 |
@Override |
@Override |
172 |
public boolean onCreateOptionsMenu(Menu menu) { |
public boolean onCreateOptionsMenu(Menu menu) { |
173 |
MenuItem item; |
MenuItem item; |
174 |
|
|
175 |
item = menu.add(0, OPTIONS_MAP, 0, "Station map"); |
item = menu.add(0, OPTIONS_MAP, 0, getString(stationlist_stationmap)); |
176 |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
177 |
|
|
178 |
item = menu.add(0, OPTIONS_GPSINFO, 0, "GPS Info"); |
item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo)); |
179 |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
180 |
|
|
181 |
return true; |
return true; |
203 |
case OPTIONS_GPSINFO: |
case OPTIONS_GPSINFO: |
204 |
Location loc = locationLookup.getLocation(); |
Location loc = locationLookup.getLocation(); |
205 |
StringBuffer message = new StringBuffer(); |
StringBuffer message = new StringBuffer(); |
206 |
message.append("Location info:\n"); |
message.append( getString(stationlist_locationinfo) ).append(":\n"); |
207 |
message.append("-Obtained by: ").append(loc != null ? loc.getProvider() : "-").append("\n"); |
if (loc != null) { |
208 |
message.append("-Accuracy: ").append(loc != null ? (int)loc.getAccuracy() : "-").append("m\n"); |
message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n"); |
209 |
|
message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n"); |
210 |
|
message.append( getString(stationlist_latitude) ).append( loc.getLatitude()).append("\n"); |
211 |
|
message.append( getString(stationlist_longitude) ).append( loc.getLongitude() ).append("\n"); |
212 |
|
} else { |
213 |
|
message.append( getString(stationlist_nolocation) ); |
214 |
|
} |
215 |
|
|
216 |
MessageBox.showMessage(this, message.toString()); |
MessageBox.showMessage(this, message.toString()); |
217 |
break; |
break; |
218 |
default: |
default: |
231 |
|
|
232 |
|
|
233 |
} |
} |
234 |
|
|
235 |
|
public void showMessageAndClose(String message) { |
236 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
237 |
|
builder.setMessage(message) |
238 |
|
.setCancelable(false) |
239 |
|
.setPositiveButton("OK", new DialogInterface.OnClickListener() { |
240 |
|
public void onClick(DialogInterface dialog, int id) { |
241 |
|
dialog.dismiss(); |
242 |
|
StationList.this.finish(); |
243 |
|
} |
244 |
|
}) |
245 |
|
.show(); |
246 |
|
} |
247 |
|
|
248 |
|
|
249 |
|
|
253 |
switch (id) { |
switch (id) { |
254 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
255 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
256 |
dlg.setMessage("Wait for location fix"); |
dlg.setMessage( getString(stationlist_waitforlocation) ); |
257 |
dlg.setCancelable(false); |
dlg.setCancelable(false); |
258 |
return dlg; |
return dlg; |
259 |
case DLG_STATIONNAME: |
case DLG_STATIONNAME: |
263 |
|
|
264 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
265 |
|
|
266 |
builder.setTitle("Station search"); |
builder.setTitle( getString(stationlist_stationsearch) ); |
267 |
builder.setView(rootView); |
builder.setView(rootView); |
268 |
builder.setCancelable(true); |
builder.setCancelable(true); |
269 |
builder.setPositiveButton("Search", new DialogInterface.OnClickListener() { |
builder.setPositiveButton( getString(generic_search), new DialogInterface.OnClickListener() { |
270 |
public void onClick(DialogInterface dialog, int which) { |
public void onClick(DialogInterface dialog, int which) { |
271 |
EditText et = (EditText) rootView.findViewById(R.id.EditText); |
EditText et = (EditText) rootView.findViewById(R.id.EditText); |
272 |
dialog.dismiss(); |
dialog.dismiss(); |
273 |
if (et.getText().toString().length() >= 2) { |
if (et.getText().toString().length() >= 2) { |
274 |
startNameSearch(et.getText().toString()); |
startNameSearch(et.getText().toString()); |
275 |
} else { |
} else { |
276 |
MessageBox.showMessage(StationList.this, "Two characters minimum" ); |
showMessageAndClose( getString(stationlist_twocharmin) ); |
277 |
} |
} |
278 |
} |
} |
279 |
}); |
}); |
280 |
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
281 |
public void onClick(DialogInterface dialog, int which) { |
public void onClick(DialogInterface dialog, int which) { |
282 |
dialog.dismiss(); |
dialog.dismiss(); |
283 |
|
StationList.this.finish(); // Close this Activity |
284 |
} |
} |
285 |
}); |
}); |
286 |
return builder.create(); |
return builder.create(); |
311 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
312 |
|
|
313 |
StationBean station = stations.get(position); |
StationBean station = stations.get(position); |
|
|
|
|
double latitude = station.getLatitude(); |
|
|
double longitude = station.getLongitude(); |
|
|
|
|
|
|
|
314 |
|
|
315 |
Intent intent = new Intent(this, DepartureList.class); |
Intent intent = new Intent(this, DepartureList.class); |
316 |
intent.putExtra("name", station.getName()); |
intent.putExtra("stationbean", station); |
|
intent.putExtra("distance", station.getDistance()); |
|
|
intent.putExtra("latitude", latitude); |
|
|
intent.putExtra("longitude", longitude); |
|
|
intent.putExtra("stationid", station.getId()); |
|
|
intent.putExtra("address", station.getAddress()); |
|
317 |
startActivity(intent); |
startActivity(intent); |
318 |
} |
} |
319 |
|
|
321 |
// |
// |
322 |
|
|
323 |
public void startLookup() { |
public void startLookup() { |
324 |
isRunning = true; |
isRunning = true; |
325 |
dialogMessage = "Wait for location fix"; |
dialogMessage = getString( stationlist_waitforlocation ); |
326 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
327 |
|
|
328 |
locationLookup.locateStations(); |
locationLookup.locateStations(); |
330 |
} |
} |
331 |
|
|
332 |
void startNameSearch(String name) { |
void startNameSearch(String name) { |
333 |
dialogMessage = "Finding stations by name"; |
dialogMessage = getString( stationlist_findbyname ); |
334 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
335 |
|
|
336 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
342 |
public void startFavoriteLookup() { |
public void startFavoriteLookup() { |
343 |
|
|
344 |
if (favorites.size() > 0) { |
if (favorites.size() > 0) { |
345 |
dialogMessage = "Loading favorites"; |
dialogMessage = getString( stationlist_loadfavorites ); |
346 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
347 |
|
|
348 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
349 |
findStationsTask.searchByIds(favorites.toString(), locationLookup.getLocation()); |
findStationsTask.searchByIds(favorites.toString(), locationLookup.getLocation()); |
350 |
findStationsTask.execute(); |
findStationsTask.execute(); |
351 |
} else { |
} else { |
352 |
MessageBox.showMessage(this, "Favorite list is empty"); |
showMessageAndClose( getString( stationlist_nofavorites ) ); |
353 |
} |
} |
354 |
} |
} |
355 |
|
|
357 |
|
|
358 |
void startLocatorTask() |
void startLocatorTask() |
359 |
{ |
{ |
360 |
dialogMessage = "Finding nearby stations"; |
dialogMessage = getString( stationlist_findingnearby ); |
361 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
362 |
|
|
363 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
412 |
|
|
413 |
case NOPROVIDER: |
case NOPROVIDER: |
414 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
415 |
MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps."); |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) ); |
416 |
break; |
break; |
417 |
case LOCATIONFIXTIMEOUT: |
case LOCATIONFIXTIMEOUT: |
418 |
if (isRunning) { |
if (isRunning) { |
423 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
424 |
|
|
425 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
426 |
builder.setMessage("Location fix timed out"); |
builder.setMessage( getString( stationlist_gpstimeout) ); |
427 |
builder.setCancelable(true); |
builder.setCancelable(true); |
428 |
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
429 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
430 |
dialog.dismiss(); |
dialog.dismiss(); |
431 |
startLookup(); |
startLookup(); |
432 |
|
|
433 |
} |
} |
434 |
}); |
}); |
435 |
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() { |
436 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
437 |
dialog.dismiss(); |
dialog.dismiss(); |
438 |
} |
} |
529 |
|
|
530 |
if (success) { |
if (success) { |
531 |
if (stationProvider.getStations().size() == 0) |
if (stationProvider.getStations().size() == 0) |
532 |
MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! |
MessageBox.showMessage(StationList.this, getString(stationlist_nostations) ); // this should not be possible !?! |
533 |
stations = stationProvider.getStations(); |
stations = stationProvider.getStations(); |
534 |
adapter.setStations( stations ); |
adapter.setStations( stations ); |
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); |
538 |
builder.setMessage("Error on finding nearby stations"); |
builder.setMessage(getString(stationlist_nearbyerror)); |
539 |
builder.setCancelable(true); |
builder.setCancelable(true); |
540 |
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
541 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
542 |
dialog.dismiss(); |
dialog.dismiss(); |
543 |
|
|
549 |
}); |
}); |
550 |
} |
} |
551 |
}); |
}); |
552 |
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
553 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
554 |
dialog.dismiss(); |
dialog.dismiss(); |
555 |
} |
} |
575 |
int stationID = stations.get(selectedPosition).getId(); |
int stationID = stations.get(selectedPosition).getId(); |
576 |
|
|
577 |
if (!favorites.contains(stationID)) { |
if (!favorites.contains(stationID)) { |
578 |
menu.add(0, FAVORITES_ADD, 0, "Add to favorites"); |
menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) ); |
579 |
} else { |
} else { |
580 |
menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites"); |
menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_addfavorite) ); |
581 |
} |
} |
582 |
|
|
583 |
} |
} |
588 |
int stationID = sb.getId(); |
int stationID = sb.getId(); |
589 |
if (item.getItemId() == FAVORITES_ADD) { |
if (item.getItemId() == FAVORITES_ADD) { |
590 |
favorites.add(stationID); |
favorites.add(stationID); |
591 |
Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show(); |
Toast.makeText(StationList.this, getString(stationlist_stationadded), 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, getString(stationlist_stationremoved), Toast.LENGTH_SHORT).show(); |
596 |
|
|
597 |
|
|
598 |
if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) { |
if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) { |