48 |
import android.os.Bundle; |
import android.os.Bundle; |
49 |
import android.os.Handler; |
import android.os.Handler; |
50 |
import android.os.Message; |
import android.os.Message; |
|
import android.util.Log; |
|
51 |
import android.view.ContextMenu; |
import android.view.ContextMenu; |
52 |
import android.view.LayoutInflater; |
import android.view.LayoutInflater; |
53 |
import android.view.Menu; |
import android.view.Menu; |
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 enum LookupStates { |
|
|
GOTLOCATION, |
|
|
GOTSTATIONLIST, |
|
|
NOPROVIDER, |
|
|
LOCATIONFIXTIMEOUT, |
|
|
GPS_SAT_COUNT |
|
|
} |
|
73 |
|
|
74 |
|
|
75 |
public static final int OPTIONS_MAP = 2003; |
public static final int OPTIONS_MAP = 2003; |
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(LookupStates.LOCATIONFIXTIMEOUT.ordinal(), 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() ); |
431 |
class StationsFetchedHandler extends Handler { |
class StationsFetchedHandler extends Handler { |
432 |
@Override |
@Override |
433 |
public void handleMessage(Message msg) { |
public void handleMessage(Message msg) { |
434 |
Log.e("Handler", "WHAT:" + msg.what); |
|
435 |
|
LocationLookup.LookupStates state = locationLookup.getState(); |
436 |
LookupStates state = LookupStates.values()[msg.what]; |
|
437 |
|
|
438 |
switch (state) { |
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 GPS_SAT_COUNT: |
} |
452 |
int count = msg.arg1; |
|
453 |
String dialogMessage = getString( stationlist_waitforlocation ) + "\n" + getString( stationlist_satellitecount ) + ": " + count; |
|
454 |
dialog.setMessage( dialogMessage ); |
|
455 |
return; //abort immediately |
if (locationLookup.elapsedTime() >= GPS_TIMEOUT_MS) { |
456 |
case LOCATIONFIXTIMEOUT: |
dismissDialog(DLG_PROGRESS); |
457 |
if (isRunning) { |
|
458 |
locationLookup.stopSearch(); |
locationLookup.stopSearch(); |
459 |
if (locationLookup.hasLocation()) { |
|
460 |
stationsFetched.sendEmptyMessage( LookupStates.GOTLOCATION.ordinal() ); |
if (locationLookup.hasLocation()) { |
461 |
} else { |
startLocatorTask(); |
462 |
dismissDialog(DLG_PROGRESS); |
} else { |
463 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
464 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
builder.setMessage( getString( stationlist_gpstimeout) ); |
465 |
builder.setMessage( getString( stationlist_gpstimeout) ); |
builder.setCancelable(true); |
466 |
builder.setCancelable(true); |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
467 |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
public void onClick(DialogInterface dialog, int id) { |
468 |
public void onClick(DialogInterface dialog, int id) { |
dialog.dismiss(); |
469 |
dialog.dismiss(); |
startLookup(); |
470 |
startLookup(); |
|
471 |
|
} |
472 |
} |
}); |
473 |
}); |
builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() { |
474 |
builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() { |
public void onClick(DialogInterface dialog, int id) { |
475 |
public void onClick(DialogInterface dialog, int id) { |
dialog.dismiss(); |
476 |
dialog.dismiss(); |
} |
477 |
} |
}); |
478 |
}); |
builder.show(); |
|
builder.show(); |
|
479 |
|
|
|
} |
|
480 |
} |
} |
481 |
break; |
} else { |
482 |
|
if (locationLookup.hasGps()) { |
483 |
|
int count = locationLookup.getSatCount(); |
484 |
|
String dialogMessage = getString( stationlist_waitforlocation ) + "\n" + getString( stationlist_satellitecount ) + ": " + count; |
485 |
|
dialog.setMessage( dialogMessage ); |
486 |
|
} |
487 |
|
this.sendEmptyMessageDelayed(0, 500); |
488 |
} |
} |
489 |
isRunning = false; |
|
490 |
} |
} |
491 |
}; |
} |
492 |
|
|
493 |
|
|
494 |
|
|
495 |
|
|
496 |
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
class FindStationsTask extends AsyncTask<Void,Void,Void> { |