2 |
|
|
3 |
import java.util.ArrayList; |
import java.util.ArrayList; |
4 |
import java.util.List; |
import java.util.List; |
5 |
import java.util.Locale; |
|
6 |
|
|
7 |
import android.app.AlertDialog; |
import android.app.AlertDialog; |
8 |
import android.app.Dialog; |
import android.app.Dialog; |
12 |
import android.content.Intent; |
import android.content.Intent; |
13 |
import android.content.SharedPreferences; |
import android.content.SharedPreferences; |
14 |
import android.content.SharedPreferences.Editor; |
import android.content.SharedPreferences.Editor; |
|
import android.location.Address; |
|
|
import android.location.Geocoder; |
|
15 |
import android.location.Location; |
import android.location.Location; |
16 |
import android.os.AsyncTask; |
import android.os.AsyncTask; |
17 |
import android.os.Bundle; |
import android.os.Bundle; |
18 |
import android.os.Handler; |
import android.os.Handler; |
19 |
import android.os.Message; |
import android.os.Message; |
20 |
|
|
21 |
import android.util.Log; |
import android.util.Log; |
22 |
import android.view.ContextMenu; |
import android.view.ContextMenu; |
23 |
import android.view.LayoutInflater; |
import android.view.LayoutInflater; |
37 |
import dk.thoerup.traininfo.util.IntSet; |
import dk.thoerup.traininfo.util.IntSet; |
38 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
39 |
|
|
40 |
|
import static dk.thoerup.traininfo.R.string.*; |
41 |
|
|
42 |
public class StationList extends ListActivity { |
public class StationList extends ListActivity { |
43 |
public static final int GOTLOCATION = 1001; |
public static final int GOTLOCATION = 1001; |
44 |
public static final int GOTSTATIONLIST = 1002; |
public static final int GOTSTATIONLIST = 1002; |
136 |
|
|
137 |
} |
} |
138 |
protected void setTitle() { |
protected void setTitle() { |
139 |
String dialogTitle; |
String dialogTitle = getResources().getString(app_name); |
140 |
switch (listType) { |
switch (listType) { |
141 |
case ListNearest: |
case ListNearest: |
142 |
dialogTitle = "TrainInfo DK - Nearby stations"; |
dialogTitle += " - " + getString(stationlist_nearbystations); |
143 |
break; |
break; |
144 |
case ListSearch: |
case ListSearch: |
145 |
dialogTitle = "TrainInfo DK - Search"; |
dialogTitle += " - " + getString(stationlist_search); |
146 |
break; |
break; |
147 |
case ListFavorites: |
case ListFavorites: |
148 |
dialogTitle = "TrainInfo DK - Favorites"; |
dialogTitle += " - " + getString(stationlist_favorites); |
149 |
break; |
break; |
150 |
default: |
default: |
151 |
dialogTitle = "";//not possible |
dialogTitle = "";//not possible |
172 |
@Override |
@Override |
173 |
public boolean onCreateOptionsMenu(Menu menu) { |
public boolean onCreateOptionsMenu(Menu menu) { |
174 |
MenuItem item; |
MenuItem item; |
175 |
|
|
176 |
item = menu.add(0, OPTIONS_MAP, 0, "Station map"); |
item = menu.add(0, OPTIONS_MAP, 0, getString(stationlist_stationmap)); |
177 |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
178 |
|
|
179 |
item = menu.add(0, OPTIONS_GPSINFO, 0, "GPS Info"); |
item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo)); |
180 |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
181 |
|
|
182 |
return true; |
return true; |
191 |
case OPTIONS_MAP: |
case OPTIONS_MAP: |
192 |
|
|
193 |
Intent intent = new Intent(this,StationMapView.class); |
Intent intent = new Intent(this,StationMapView.class); |
|
intent.putExtra("userlocation", location ); |
|
194 |
|
|
195 |
ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>(); |
ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>(); |
196 |
for (StationBean st : stations ) { |
for (StationBean st : stations ) { |
204 |
case OPTIONS_GPSINFO: |
case OPTIONS_GPSINFO: |
205 |
Location loc = locationLookup.getLocation(); |
Location loc = locationLookup.getLocation(); |
206 |
StringBuffer message = new StringBuffer(); |
StringBuffer message = new StringBuffer(); |
207 |
message.append("Location info:\n"); |
message.append( getString(stationlist_locationinfo) ).append(":\n"); |
208 |
message.append("-Obtained by: ").append(loc != null ? loc.getProvider() : "-").append("\n"); |
if (loc != null) { |
209 |
message.append("-Accuracy: ").append(loc != null ? (int)loc.getAccuracy() : "-").append("m\n"); |
message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n"); |
210 |
|
message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n"); |
211 |
|
message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n"); |
212 |
|
message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n"); |
213 |
|
} else { |
214 |
|
message.append( getString(stationlist_nolocation) ); |
215 |
|
} |
216 |
|
|
217 |
MessageBox.showMessage(this, message.toString()); |
MessageBox.showMessage(this, message.toString()); |
218 |
break; |
break; |
219 |
default: |
default: |
232 |
|
|
233 |
|
|
234 |
} |
} |
235 |
|
|
236 |
|
public void showMessageAndClose(String message) { |
237 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
238 |
|
builder.setMessage(message) |
239 |
|
.setCancelable(false) |
240 |
|
.setPositiveButton("OK", new DialogInterface.OnClickListener() { |
241 |
|
public void onClick(DialogInterface dialog, int id) { |
242 |
|
dialog.dismiss(); |
243 |
|
StationList.this.finish(); |
244 |
|
} |
245 |
|
}) |
246 |
|
.show(); |
247 |
|
} |
248 |
|
|
249 |
|
|
250 |
|
|
254 |
switch (id) { |
switch (id) { |
255 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
256 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
257 |
dlg.setMessage("Wait for location fix"); |
dlg.setMessage( getString(stationlist_waitforlocation) ); |
258 |
dlg.setCancelable(false); |
dlg.setCancelable(false); |
259 |
return dlg; |
return dlg; |
260 |
case DLG_STATIONNAME: |
case DLG_STATIONNAME: |
264 |
|
|
265 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
266 |
|
|
267 |
builder.setTitle("Station search"); |
builder.setTitle( getString(stationlist_stationsearch) ); |
268 |
builder.setView(rootView); |
builder.setView(rootView); |
269 |
builder.setCancelable(true); |
builder.setCancelable(true); |
270 |
builder.setPositiveButton("Search", new DialogInterface.OnClickListener() { |
builder.setPositiveButton( getString(generic_search), new DialogInterface.OnClickListener() { |
271 |
public void onClick(DialogInterface dialog, int which) { |
public void onClick(DialogInterface dialog, int which) { |
272 |
EditText et = (EditText) rootView.findViewById(R.id.EditText); |
EditText et = (EditText) rootView.findViewById(R.id.EditText); |
273 |
dialog.dismiss(); |
dialog.dismiss(); |
274 |
if (et.getText().toString().length() >= 2) { |
if (et.getText().toString().length() >= 2) { |
275 |
startNameSearch(et.getText().toString()); |
startNameSearch(et.getText().toString()); |
276 |
} else { |
} else { |
277 |
MessageBox.showMessage(StationList.this, "Two characters minimum" ); |
showMessageAndClose( getString(stationlist_twocharmin) ); |
278 |
} |
} |
279 |
} |
} |
280 |
}); |
}); |
281 |
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
282 |
public void onClick(DialogInterface dialog, int which) { |
public void onClick(DialogInterface dialog, int which) { |
283 |
dialog.dismiss(); |
dialog.dismiss(); |
284 |
|
StationList.this.finish(); // Close this Activity |
285 |
} |
} |
286 |
}); |
}); |
287 |
return builder.create(); |
return builder.create(); |
312 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
313 |
|
|
314 |
StationBean station = stations.get(position); |
StationBean station = stations.get(position); |
|
|
|
|
double latitude = station.getLatitude(); |
|
|
double longitude = station.getLongitude(); |
|
|
|
|
|
|
|
315 |
|
|
316 |
Intent intent = new Intent(this, DepartureList.class); |
Intent intent = new Intent(this, DepartureList.class); |
317 |
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()); |
|
318 |
startActivity(intent); |
startActivity(intent); |
319 |
} |
} |
320 |
|
|
322 |
// |
// |
323 |
|
|
324 |
public void startLookup() { |
public void startLookup() { |
325 |
isRunning = true; |
isRunning = true; |
326 |
dialogMessage = "Wait for location fix"; |
dialogMessage = getString( stationlist_waitforlocation ); |
327 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
328 |
|
|
329 |
locationLookup.locateStations(); |
locationLookup.locateStations(); |
331 |
} |
} |
332 |
|
|
333 |
void startNameSearch(String name) { |
void startNameSearch(String name) { |
334 |
dialogMessage = "Finding stations by name"; |
dialogMessage = getString( stationlist_findbyname ); |
335 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
336 |
|
|
337 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
338 |
findStationsTask.searchByName(name, locationLookup.getLocation()); |
findStationsTask.searchByName(name); |
339 |
findStationsTask.execute(); |
findStationsTask.execute(); |
340 |
|
|
341 |
} |
} |
343 |
public void startFavoriteLookup() { |
public void startFavoriteLookup() { |
344 |
|
|
345 |
if (favorites.size() > 0) { |
if (favorites.size() > 0) { |
346 |
dialogMessage = "Loading favorites"; |
dialogMessage = getString( stationlist_loadfavorites ); |
347 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
348 |
|
|
349 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
350 |
findStationsTask.searchByIds(favorites.toString(), locationLookup.getLocation()); |
findStationsTask.searchByIds( favorites.toString() ); |
351 |
findStationsTask.execute(); |
findStationsTask.execute(); |
352 |
} else { |
} else { |
353 |
MessageBox.showMessage(this, "Favorite list is empty"); |
showMessageAndClose( getString( stationlist_nofavorites ) ); |
354 |
} |
} |
355 |
} |
} |
356 |
|
|
358 |
|
|
359 |
void startLocatorTask() |
void startLocatorTask() |
360 |
{ |
{ |
361 |
dialogMessage = "Finding nearby stations"; |
dialogMessage = getString( stationlist_findingnearby ); |
362 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
363 |
|
|
364 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
365 |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
366 |
findStationsTask.execute(); |
findStationsTask.execute(); |
367 |
} |
} |
|
|
|
|
|
|
|
String lookupAddress(double latitude, double longitude) { |
|
|
|
|
|
Geocoder coder = new Geocoder(this, new Locale("da")); |
|
|
StringBuilder sb = new StringBuilder(); |
|
|
Log.i("lookupaddr", "" + latitude + "/" + longitude); |
|
|
try { |
|
|
List<Address> addressList = coder.getFromLocation(latitude, longitude, 1); |
|
|
Address addr = addressList.get(0); |
|
|
|
|
|
|
|
|
int max = addr.getMaxAddressLineIndex(); |
|
|
for (int i=0; i<max; i++) { |
|
|
if (i>0) |
|
|
sb.append(", "); |
|
|
|
|
|
sb.append(addr.getAddressLine(i)); |
|
|
} |
|
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
Log.e("DepartureList", "geocoder failed", e); |
|
|
} |
|
368 |
|
|
|
return sb.toString(); |
|
|
} |
|
|
|
|
369 |
|
|
370 |
//////////////////////////////////////////////////////////////////////////// |
//////////////////////////////////////////////////////////////////////////// |
371 |
// Inner classes |
// Inner classes |
385 |
|
|
386 |
case NOPROVIDER: |
case NOPROVIDER: |
387 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
388 |
MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps."); |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) ); |
389 |
break; |
break; |
390 |
case LOCATIONFIXTIMEOUT: |
case LOCATIONFIXTIMEOUT: |
391 |
if (isRunning) { |
if (isRunning) { |
396 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
397 |
|
|
398 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
399 |
builder.setMessage("Location fix timed out"); |
builder.setMessage( getString( stationlist_gpstimeout) ); |
400 |
builder.setCancelable(true); |
builder.setCancelable(true); |
401 |
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
402 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
403 |
dialog.dismiss(); |
dialog.dismiss(); |
404 |
startLookup(); |
startLookup(); |
405 |
|
|
406 |
} |
} |
407 |
}); |
}); |
408 |
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() { |
409 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
410 |
dialog.dismiss(); |
dialog.dismiss(); |
411 |
} |
} |
429 |
Location loc; |
Location loc; |
430 |
String ids; |
String ids; |
431 |
|
|
432 |
public void searchByName(String n, Location l) { |
public void searchByName(String n) { |
433 |
|
|
434 |
method = LookupMethod.ByName; |
method = LookupMethod.ByName; |
|
loc = l; |
|
435 |
name = n; |
name = n; |
436 |
} |
} |
437 |
|
|
440 |
loc = l; |
loc = l; |
441 |
} |
} |
442 |
|
|
443 |
public void searchByIds(String id, Location l) { |
public void searchByIds(String id) { |
444 |
|
|
445 |
method = LookupMethod.ByList; |
method = LookupMethod.ByList; |
|
loc = l; |
|
446 |
ids = id; |
ids = id; |
447 |
} |
} |
448 |
|
|
472 |
} |
} |
473 |
|
|
474 |
|
|
|
Location dummy = new Location("gps"); |
|
|
List<StationBean> stations = stationProvider.getStations(); |
|
|
|
|
|
for (StationBean station : stations) { |
|
|
String addr = lookupAddress(station.getLatitude(), station.getLongitude()); |
|
|
station.setAddress(addr); |
|
|
|
|
|
|
|
|
if (method.equals(LookupMethod.ByName) || method.equals(LookupMethod.ByList)) { |
|
|
if (loc != null) { //only do the distance calc if we have a location |
|
|
dummy.setLatitude(station.getLatitude()); |
|
|
dummy.setLongitude(station.getLongitude()); |
|
|
station.setDistance( (int)loc.distanceTo(dummy) ); |
|
|
} else { |
|
|
station.setDistance(0); |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
475 |
return null; |
return null; |
476 |
} |
} |
477 |
|
|
482 |
|
|
483 |
|
|
484 |
if (success) { |
if (success) { |
485 |
if (stationProvider.getStations().size() == 0) |
if (stationProvider.getStations().size() == 0) { |
486 |
MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! |
showMessageAndClose(getString(stationlist_nostations)); |
487 |
|
} |
488 |
stations = stationProvider.getStations(); |
stations = stationProvider.getStations(); |
489 |
adapter.setStations( stations ); |
adapter.setStations( stations ); |
490 |
|
|
491 |
} else { //communication or parse errors |
} else { //communication or parse errors |
492 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
493 |
builder.setMessage("Error on finding nearby stations"); |
builder.setMessage(getString(stationlist_nearbyerror)); |
494 |
builder.setCancelable(true); |
builder.setCancelable(true); |
495 |
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
496 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
497 |
dialog.dismiss(); |
dialog.dismiss(); |
498 |
|
|
504 |
}); |
}); |
505 |
} |
} |
506 |
}); |
}); |
507 |
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
508 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
509 |
dialog.dismiss(); |
dialog.dismiss(); |
510 |
} |
} |
511 |
}); |
}); |
512 |
builder.show(); |
try { |
513 |
|
builder.show(); |
514 |
|
} catch (android.view.WindowManager.BadTokenException e) { |
515 |
|
Log.i("StationList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |
516 |
|
} |
517 |
} |
} |
518 |
} |
} |
519 |
} |
} |
534 |
int stationID = stations.get(selectedPosition).getId(); |
int stationID = stations.get(selectedPosition).getId(); |
535 |
|
|
536 |
if (!favorites.contains(stationID)) { |
if (!favorites.contains(stationID)) { |
537 |
menu.add(0, FAVORITES_ADD, 0, "Add to favorites"); |
menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) ); |
538 |
} else { |
} else { |
539 |
menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites"); |
menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_removefavorite) ); |
540 |
} |
} |
541 |
|
|
542 |
} |
} |
547 |
int stationID = sb.getId(); |
int stationID = sb.getId(); |
548 |
if (item.getItemId() == FAVORITES_ADD) { |
if (item.getItemId() == FAVORITES_ADD) { |
549 |
favorites.add(stationID); |
favorites.add(stationID); |
550 |
Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show(); |
Toast.makeText(StationList.this, getString(stationlist_stationadded), Toast.LENGTH_SHORT).show(); |
551 |
} else { |
} else { |
552 |
|
|
553 |
favorites.remove(stationID); |
favorites.remove(stationID); |
554 |
Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show(); |
Toast.makeText(StationList.this, getString(stationlist_stationremoved), Toast.LENGTH_SHORT).show(); |
555 |
|
|
556 |
|
|
557 |
if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) { |
if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) { |