30 |
import static dk.thoerup.traininfo.R.string.stationlist_stationsearch; |
import static dk.thoerup.traininfo.R.string.stationlist_stationsearch; |
31 |
import static dk.thoerup.traininfo.R.string.stationlist_twocharmin; |
import static dk.thoerup.traininfo.R.string.stationlist_twocharmin; |
32 |
import static dk.thoerup.traininfo.R.string.stationlist_waitforlocation; |
import static dk.thoerup.traininfo.R.string.stationlist_waitforlocation; |
33 |
|
import static dk.thoerup.traininfo.R.string.stationlist_satellitecount; |
34 |
|
|
35 |
import java.util.ArrayList; |
import java.util.ArrayList; |
36 |
|
|
|
|
|
37 |
import android.app.Activity; |
import android.app.Activity; |
38 |
import android.app.AlertDialog; |
import android.app.AlertDialog; |
39 |
import android.app.Dialog; |
import android.app.Dialog; |
69 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
70 |
|
|
71 |
public class StationList extends ListActivity { |
public class StationList extends ListActivity { |
72 |
public static final int GOTLOCATION = 1001; |
|
73 |
public static final int GOTSTATIONLIST = 1002; |
|
|
public static final int NOPROVIDER = 1003; |
|
|
public static final int LOCATIONFIXTIMEOUT = 1004; |
|
74 |
|
|
75 |
public static final int OPTIONS_MAP = 2003; |
public static final int OPTIONS_MAP = 2003; |
76 |
public static final int OPTIONS_GPSINFO = 2004; |
public static final int OPTIONS_GPSINFO = 2004; |
96 |
FindStationsTask findStationsTask; |
FindStationsTask findStationsTask; |
97 |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
98 |
|
|
99 |
GeoPair location = new GeoPair(); |
//GeoPair location = new GeoPair(); |
100 |
|
|
101 |
boolean isLaunchedforShortcut; |
boolean isLaunchedforShortcut; |
102 |
boolean isRunning = false; |
|
103 |
StationBean stations = new StationBean(); |
StationBean stations = new StationBean(); |
104 |
|
|
105 |
StationProvider stationProvider = ProviderFactory.getStationProvider(); |
StationProvider stationProvider = ProviderFactory.getStationProvider(); |
111 |
|
|
112 |
WelcomeScreen.ListType listType; |
WelcomeScreen.ListType listType; |
113 |
SharedPreferences prefs; |
SharedPreferences prefs; |
114 |
|
|
115 |
/////////////////////////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////////////////////////// |
116 |
//Activity call backs |
//Activity call backs |
117 |
|
|
128 |
ListView lv = getListView(); |
ListView lv = getListView(); |
129 |
lv.setOnCreateContextMenuListener(contextMenu); |
lv.setOnCreateContextMenuListener(contextMenu); |
130 |
|
|
131 |
locationLookup = new LocationLookup(this, stationsFetched); |
locationLookup = new LocationLookup(this); |
132 |
|
|
133 |
|
|
134 |
prefs = getSharedPreferences("TrainStation", 0); |
prefs = getSharedPreferences("TrainStation", 0); |
164 |
} else { |
} else { |
165 |
stations = (StationBean) savedInstanceState.getSerializable("stations"); |
stations = (StationBean) savedInstanceState.getSerializable("stations"); |
166 |
adapter.setStations(stations); |
adapter.setStations(stations); |
|
location = (GeoPair) savedInstanceState.getSerializable("location"); |
|
167 |
} |
} |
168 |
|
|
169 |
} |
} |
177 |
protected void onDestroy() { |
protected void onDestroy() { |
178 |
super.onDestroy(); |
super.onDestroy(); |
179 |
|
|
|
isRunning = false; |
|
180 |
|
|
181 |
if (locationLookup != null) { |
if (locationLookup != null) { |
182 |
locationLookup.stopSearch(); |
locationLookup.stopSearch(); |
215 |
if (dialog != null && dialog.isShowing()) |
if (dialog != null && dialog.isShowing()) |
216 |
dialog.dismiss(); |
dialog.dismiss(); |
217 |
outState.putSerializable("stations", (StationBean) stations); |
outState.putSerializable("stations", (StationBean) stations); |
|
outState.putSerializable("location", location); |
|
218 |
|
|
219 |
} |
} |
220 |
|
|
381 |
// |
// |
382 |
|
|
383 |
public void startLookup() { |
public void startLookup() { |
|
isRunning = true; |
|
384 |
dialogMessage = getString( stationlist_waitforlocation ); |
dialogMessage = getString( stationlist_waitforlocation ); |
385 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
386 |
|
|
387 |
locationLookup.locateStations(); |
locationLookup.locateStations(); |
388 |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS); |
stationsFetched.sendEmptyMessageDelayed(0, 500); |
389 |
} |
} |
390 |
|
|
391 |
void startNameSearch(String name) { |
void startNameSearch(String name) { |
417 |
void startLocatorTask() |
void startLocatorTask() |
418 |
{ |
{ |
419 |
dialogMessage = getString( stationlist_findingnearby ); |
dialogMessage = getString( stationlist_findingnearby ); |
420 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
421 |
|
|
422 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
423 |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
432 |
@Override |
@Override |
433 |
public void handleMessage(Message msg) { |
public void handleMessage(Message msg) { |
434 |
|
|
435 |
switch (msg.what) { |
LocationLookup.LookupStates state = locationLookup.getState(); |
436 |
|
|
437 |
|
|
438 |
|
switch (state) { |
439 |
case GOTLOCATION: |
case GOTLOCATION: |
440 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
441 |
|
|
442 |
startLocatorTask(); |
startLocatorTask(); |
443 |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
|
444 |
|
return; |
|
break; |
|
445 |
|
|
446 |
case NOPROVIDER: |
case NOPROVIDER: |
447 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
448 |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); |
449 |
//StationList.this.finish(); |
//StationList.this.finish(); |
450 |
break; |
return; |
451 |
case LOCATIONFIXTIMEOUT: |
} |
452 |
if (isRunning) { |
|
453 |
locationLookup.stopSearch(); |
|
454 |
if (locationLookup.hasLocation()) { |
|
455 |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
if (locationLookup.elapsedTime() >= GPS_TIMEOUT_MS) { |
456 |
} else { |
dismissDialog(DLG_PROGRESS); |
457 |
dismissDialog(DLG_PROGRESS); |
|
458 |
|
locationLookup.stopSearch(); |
459 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
|
460 |
builder.setMessage( getString( stationlist_gpstimeout) ); |
if (locationLookup.hasLocation()) { |
461 |
builder.setCancelable(true); |
startLocatorTask(); |
462 |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
} else { |
463 |
public void onClick(DialogInterface dialog, int id) { |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
464 |
dialog.dismiss(); |
builder.setMessage( getString( stationlist_gpstimeout) ); |
465 |
startLookup(); |
builder.setCancelable(true); |
466 |
|
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
467 |
} |
public void onClick(DialogInterface dialog, int id) { |
468 |
}); |
dialog.dismiss(); |
469 |
builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() { |
startLookup(); |
470 |
public void onClick(DialogInterface dialog, int id) { |
|
471 |
dialog.dismiss(); |
} |
472 |
} |
}); |
473 |
}); |
builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() { |
474 |
builder.show(); |
public void onClick(DialogInterface dialog, int id) { |
475 |
|
dialog.dismiss(); |
476 |
|
StationList.this.finish(); // Close this Activity |
477 |
|
} |
478 |
|
}); |
479 |
|
builder.show(); |
480 |
|
|
|
} |
|
481 |
} |
} |
482 |
break; |
} else { |
483 |
|
if (locationLookup.hasGps()) { |
484 |
|
int count = locationLookup.getSatCount(); |
485 |
|
String dialogMessage = getString( stationlist_waitforlocation ) + "\n" + getString( stationlist_satellitecount ) + ": " + count; |
486 |
|
dialog.setMessage( dialogMessage ); |
487 |
|
} |
488 |
|
this.sendEmptyMessageDelayed(0, 500); |
489 |
} |
} |
490 |
isRunning = false; |
|
491 |
} |
} |
492 |
}; |
} |
493 |
|
|
494 |
|
|
495 |
|
|
496 |
|
|
497 |
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
531 |
|
|
532 |
switch (method) { |
switch (method) { |
533 |
case ByLocation: |
case ByLocation: |
534 |
stations = stationProvider.lookupStations(loc); |
stations = stationProvider.lookupStationsByLocation(loc); |
535 |
break; |
break; |
536 |
case ByName: |
case ByName: |
537 |
stations = stationProvider.lookupStationsByName(name); |
stations = stationProvider.lookupStationsByName(name); |