70 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
71 |
|
|
72 |
public class StationList extends ListActivity { |
public class StationList extends ListActivity { |
73 |
public enum LookupStates { |
|
|
GOTLOCATION, |
|
|
GOTSTATIONLIST, |
|
|
NOPROVIDER, |
|
|
LOCATIONFIXTIMEOUT, |
|
|
GPS_SAT_COUNT |
|
|
} |
|
74 |
|
|
75 |
|
|
76 |
public static final int OPTIONS_MAP = 2003; |
public static final int OPTIONS_MAP = 2003; |
97 |
FindStationsTask findStationsTask; |
FindStationsTask findStationsTask; |
98 |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
99 |
|
|
100 |
GeoPair location = new GeoPair(); |
//GeoPair location = new GeoPair(); |
101 |
|
|
102 |
boolean isLaunchedforShortcut; |
boolean isLaunchedforShortcut; |
103 |
boolean isRunning = false; |
|
104 |
StationBean stations = new StationBean(); |
StationBean stations = new StationBean(); |
105 |
|
|
106 |
StationProvider stationProvider = ProviderFactory.getStationProvider(); |
StationProvider stationProvider = ProviderFactory.getStationProvider(); |
112 |
|
|
113 |
WelcomeScreen.ListType listType; |
WelcomeScreen.ListType listType; |
114 |
SharedPreferences prefs; |
SharedPreferences prefs; |
115 |
|
|
116 |
/////////////////////////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////////////////////////// |
117 |
//Activity call backs |
//Activity call backs |
118 |
|
|
129 |
ListView lv = getListView(); |
ListView lv = getListView(); |
130 |
lv.setOnCreateContextMenuListener(contextMenu); |
lv.setOnCreateContextMenuListener(contextMenu); |
131 |
|
|
132 |
locationLookup = new LocationLookup(this, stationsFetched); |
locationLookup = new LocationLookup(this); |
133 |
|
|
134 |
|
|
135 |
prefs = getSharedPreferences("TrainStation", 0); |
prefs = getSharedPreferences("TrainStation", 0); |
165 |
} else { |
} else { |
166 |
stations = (StationBean) savedInstanceState.getSerializable("stations"); |
stations = (StationBean) savedInstanceState.getSerializable("stations"); |
167 |
adapter.setStations(stations); |
adapter.setStations(stations); |
|
location = (GeoPair) savedInstanceState.getSerializable("location"); |
|
168 |
} |
} |
169 |
|
|
170 |
} |
} |
178 |
protected void onDestroy() { |
protected void onDestroy() { |
179 |
super.onDestroy(); |
super.onDestroy(); |
180 |
|
|
|
isRunning = false; |
|
181 |
|
|
182 |
if (locationLookup != null) { |
if (locationLookup != null) { |
183 |
locationLookup.stopSearch(); |
locationLookup.stopSearch(); |
216 |
if (dialog != null && dialog.isShowing()) |
if (dialog != null && dialog.isShowing()) |
217 |
dialog.dismiss(); |
dialog.dismiss(); |
218 |
outState.putSerializable("stations", (StationBean) stations); |
outState.putSerializable("stations", (StationBean) stations); |
|
outState.putSerializable("location", location); |
|
219 |
|
|
220 |
} |
} |
221 |
|
|
382 |
// |
// |
383 |
|
|
384 |
public void startLookup() { |
public void startLookup() { |
|
isRunning = true; |
|
385 |
dialogMessage = getString( stationlist_waitforlocation ); |
dialogMessage = getString( stationlist_waitforlocation ); |
386 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
387 |
|
|
388 |
locationLookup.locateStations(); |
locationLookup.locateStations(); |
389 |
stationsFetched.sendEmptyMessageDelayed(LookupStates.LOCATIONFIXTIMEOUT.ordinal(), GPS_TIMEOUT_MS); |
stationsFetched.sendEmptyMessageDelayed(0, 500); |
390 |
} |
} |
391 |
|
|
392 |
void startNameSearch(String name) { |
void startNameSearch(String name) { |
418 |
void startLocatorTask() |
void startLocatorTask() |
419 |
{ |
{ |
420 |
dialogMessage = getString( stationlist_findingnearby ); |
dialogMessage = getString( stationlist_findingnearby ); |
421 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
422 |
|
|
423 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
424 |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
433 |
@Override |
@Override |
434 |
public void handleMessage(Message msg) { |
public void handleMessage(Message msg) { |
435 |
Log.e("Handler", "WHAT:" + msg.what); |
Log.e("Handler", "WHAT:" + msg.what); |
436 |
|
|
437 |
LookupStates state = LookupStates.values()[msg.what]; |
LocationLookup.LookupStates state = locationLookup.getState(); |
438 |
|
|
439 |
|
|
440 |
switch (state) { |
switch (state) { |
441 |
case GOTLOCATION: |
case GOTLOCATION: |
442 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
443 |
|
|
444 |
startLocatorTask(); |
startLocatorTask(); |
445 |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
|
446 |
|
return; |
|
break; |
|
447 |
|
|
448 |
case NOPROVIDER: |
case NOPROVIDER: |
449 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
450 |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); |
451 |
//StationList.this.finish(); |
//StationList.this.finish(); |
452 |
break; |
return; |
453 |
case GPS_SAT_COUNT: |
} |
454 |
int count = msg.arg1; |
|
455 |
String dialogMessage = getString( stationlist_waitforlocation ) + "\n" + getString( stationlist_satellitecount ) + ": " + count; |
|
456 |
dialog.setMessage( dialogMessage ); |
|
457 |
return; //abort immediately |
if (locationLookup.elapsedTime() >= GPS_TIMEOUT_MS) { |
458 |
case LOCATIONFIXTIMEOUT: |
dismissDialog(DLG_PROGRESS); |
459 |
if (isRunning) { |
|
460 |
locationLookup.stopSearch(); |
locationLookup.stopSearch(); |
461 |
if (locationLookup.hasLocation()) { |
|
462 |
stationsFetched.sendEmptyMessage( LookupStates.GOTLOCATION.ordinal() ); |
if (locationLookup.hasLocation()) { |
463 |
} else { |
startLocatorTask(); |
464 |
dismissDialog(DLG_PROGRESS); |
} else { |
465 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
466 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
builder.setMessage( getString( stationlist_gpstimeout) ); |
467 |
builder.setMessage( getString( stationlist_gpstimeout) ); |
builder.setCancelable(true); |
468 |
builder.setCancelable(true); |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
469 |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
public void onClick(DialogInterface dialog, int id) { |
470 |
public void onClick(DialogInterface dialog, int id) { |
dialog.dismiss(); |
471 |
dialog.dismiss(); |
startLookup(); |
472 |
startLookup(); |
|
473 |
|
} |
474 |
} |
}); |
475 |
}); |
builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() { |
476 |
builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() { |
public void onClick(DialogInterface dialog, int id) { |
477 |
public void onClick(DialogInterface dialog, int id) { |
dialog.dismiss(); |
478 |
dialog.dismiss(); |
} |
479 |
} |
}); |
480 |
}); |
builder.show(); |
|
builder.show(); |
|
481 |
|
|
|
} |
|
482 |
} |
} |
483 |
break; |
} else { |
484 |
|
if (locationLookup.hasGps()) { |
485 |
|
int count = locationLookup.getSatCount(); |
486 |
|
String dialogMessage = getString( stationlist_waitforlocation ) + "\n" + getString( stationlist_satellitecount ) + ": " + count; |
487 |
|
dialog.setMessage( dialogMessage ); |
488 |
|
} |
489 |
|
this.sendEmptyMessageDelayed(0, 500); |
490 |
} |
} |
491 |
isRunning = false; |
|
492 |
} |
} |
493 |
}; |
} |
494 |
|
|
495 |
|
|
496 |
|
|
497 |
|
|
498 |
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
class FindStationsTask extends AsyncTask<Void,Void,Void> { |