23 |
import static dk.thoerup.traininfo.R.string.stationlist_nostations; |
import static dk.thoerup.traininfo.R.string.stationlist_nostations; |
24 |
import static dk.thoerup.traininfo.R.string.stationlist_obtainedby; |
import static dk.thoerup.traininfo.R.string.stationlist_obtainedby; |
25 |
import static dk.thoerup.traininfo.R.string.stationlist_removefavorite; |
import static dk.thoerup.traininfo.R.string.stationlist_removefavorite; |
26 |
|
import static dk.thoerup.traininfo.R.string.stationlist_satellitecount; |
27 |
import static dk.thoerup.traininfo.R.string.stationlist_search; |
import static dk.thoerup.traininfo.R.string.stationlist_search; |
28 |
import static dk.thoerup.traininfo.R.string.stationlist_stationadded; |
import static dk.thoerup.traininfo.R.string.stationlist_stationadded; |
29 |
import static dk.thoerup.traininfo.R.string.stationlist_stationmap; |
import static dk.thoerup.traininfo.R.string.stationlist_stationmap; |
33 |
import static dk.thoerup.traininfo.R.string.stationlist_waitforlocation; |
import static dk.thoerup.traininfo.R.string.stationlist_waitforlocation; |
34 |
|
|
35 |
import java.util.ArrayList; |
import java.util.ArrayList; |
|
import java.util.List; |
|
36 |
|
|
37 |
import android.app.Activity; |
import android.app.Activity; |
38 |
import android.app.AlertDialog; |
import android.app.AlertDialog; |
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; |
51 |
|
import android.util.Log; |
52 |
import android.view.ContextMenu; |
import android.view.ContextMenu; |
53 |
import android.view.LayoutInflater; |
import android.view.LayoutInflater; |
54 |
import android.view.Menu; |
import android.view.Menu; |
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 static final int GOTLOCATION = 1001; |
|
74 |
public static final int GOTSTATIONLIST = 1002; |
|
|
public static final int NOPROVIDER = 1003; |
|
|
public static final int LOCATIONFIXTIMEOUT = 1004; |
|
75 |
|
|
76 |
public static final int OPTIONS_MAP = 2003; |
public static final int OPTIONS_MAP = 2003; |
77 |
public static final int OPTIONS_GPSINFO = 2004; |
public static final int OPTIONS_GPSINFO = 2004; |
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 |
|
|
119 |
@SuppressWarnings("unchecked") |
|
120 |
@Override |
@Override |
121 |
public void onCreate(Bundle savedInstanceState) { |
public void onCreate(Bundle savedInstanceState) { |
122 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
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); |
150 |
|
|
151 |
switch (listType) { |
switch (listType) { |
152 |
case ListNearest: |
case ListNearest: |
153 |
startLookup(); |
startNearestLookup(); |
154 |
break; |
break; |
155 |
case ListSearch: |
case ListSearch: |
156 |
showDialog(DLG_STATIONNAME); |
showDialog(DLG_STATIONNAME); |
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 |
|
|
325 |
dialog.dismiss(); |
dialog.dismiss(); |
326 |
String search = et.getText().toString().trim(); |
String search = et.getText().toString().trim(); |
327 |
if (search.length() >= 2) { |
if (search.length() >= 2) { |
328 |
startNameSearch(search); |
startNameLookup(search); |
329 |
} else { |
} else { |
330 |
showMessageAndClose( getString(stationlist_twocharmin) ); |
showMessageAndClose( getString(stationlist_twocharmin) ); |
331 |
} |
} |
381 |
///////////////////////////////////////////////////////////// |
///////////////////////////////////////////////////////////// |
382 |
// |
// |
383 |
|
|
384 |
public void startLookup() { |
public void startNearestLookup() { |
|
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(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS); |
stationsFetched.sendEmptyMessageDelayed(0, 500); |
390 |
} |
} |
391 |
|
|
392 |
void startNameSearch(String name) { |
void startNameLookup(String name) { |
393 |
dialogMessage = getString( stationlist_findbyname ); |
dialogMessage = getString( stationlist_findbyname ); |
394 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
395 |
|
|
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 |
|
|
436 |
switch (msg.what) { |
LocationLookup.LookupStates state = locationLookup.getState(); |
437 |
|
|
438 |
|
|
439 |
|
switch (state) { |
440 |
case GOTLOCATION: |
case GOTLOCATION: |
441 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
442 |
|
|
443 |
startLocatorTask(); |
startLocatorTask(); |
444 |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
|
445 |
|
return; |
|
break; |
|
446 |
|
|
447 |
case NOPROVIDER: |
case NOPROVIDER: |
448 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
449 |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); |
450 |
//StationList.this.finish(); |
//StationList.this.finish(); |
451 |
break; |
return; |
452 |
case LOCATIONFIXTIMEOUT: |
case IDLE: |
453 |
if (isRunning) { |
Log.e("TrainInfo", "How did this happen ???"); |
454 |
locationLookup.stopSearch(); |
dismissDialog(DLG_PROGRESS); // how did we get here ?? |
455 |
if (locationLookup.hasLocation()) { |
return; |
456 |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
|
457 |
} else { |
} |
|
dismissDialog(DLG_PROGRESS); |
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
|
|
builder.setMessage( getString( stationlist_gpstimeout) ); |
|
|
builder.setCancelable(true); |
|
|
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
|
|
public void onClick(DialogInterface dialog, int id) { |
|
|
dialog.dismiss(); |
|
|
startLookup(); |
|
|
|
|
|
} |
|
|
}); |
|
|
builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() { |
|
|
public void onClick(DialogInterface dialog, int id) { |
|
|
dialog.dismiss(); |
|
|
} |
|
|
}); |
|
|
builder.show(); |
|
458 |
|
|
459 |
} |
|
460 |
|
|
461 |
|
if (locationLookup.elapsedTime() >= GPS_TIMEOUT_MS) { |
462 |
|
try { |
463 |
|
dismissDialog(DLG_PROGRESS); |
464 |
|
} catch (IllegalArgumentException ex) { |
465 |
|
// I get stacktraces that reports an exception is thrown here - |
466 |
|
// but i can not recreate the situation that causes said exception |
467 |
|
Log.e("TrainInfo", "Why the f*** is this exception thrown here ?? " + ex.getMessage() ); |
468 |
} |
} |
469 |
break; |
|
470 |
|
locationLookup.stopSearch(); |
471 |
|
|
472 |
|
if (locationLookup.hasLocation()) { |
473 |
|
startLocatorTask(); |
474 |
|
} else { |
475 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
476 |
|
builder.setMessage( getString( stationlist_gpstimeout) ); |
477 |
|
builder.setCancelable(true); |
478 |
|
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
479 |
|
public void onClick(DialogInterface dialog, int id) { |
480 |
|
dialog.dismiss(); |
481 |
|
startNearestLookup(); |
482 |
|
|
483 |
|
} |
484 |
|
}); |
485 |
|
builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() { |
486 |
|
public void onClick(DialogInterface dialog, int id) { |
487 |
|
dialog.dismiss(); |
488 |
|
StationList.this.finish(); // Close this Activity |
489 |
|
} |
490 |
|
}); |
491 |
|
builder.show(); |
492 |
|
|
493 |
|
} |
494 |
|
} else { |
495 |
|
if (locationLookup.hasGps()) { |
496 |
|
int count = locationLookup.getSatCount(); |
497 |
|
String dialogMessage = getString( stationlist_waitforlocation ) + "\n" + getString( stationlist_satellitecount ) + ": " + count; |
498 |
|
dialog.setMessage( dialogMessage ); |
499 |
|
} |
500 |
|
this.sendEmptyMessageDelayed(0, 500); |
501 |
} |
} |
502 |
isRunning = false; |
|
503 |
} |
} |
504 |
}; |
} |
505 |
|
|
506 |
|
|
507 |
|
|
508 |
|
|
509 |
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
543 |
|
|
544 |
switch (method) { |
switch (method) { |
545 |
case ByLocation: |
case ByLocation: |
546 |
stations = stationProvider.lookupStations(loc); |
stations = stationProvider.lookupStationsByLocation(loc); |
547 |
break; |
break; |
548 |
case ByName: |
case ByName: |
549 |
stations = stationProvider.lookupStationsByName(name); |
stations = stationProvider.lookupStationsByName(name); |
596 |
runner = new Runnable() { |
runner = new Runnable() { |
597 |
@Override |
@Override |
598 |
public void run() { |
public void run() { |
599 |
startNameSearch( FindStationsTask.this.name ); |
startNameLookup( FindStationsTask.this.name ); |
600 |
} |
} |
601 |
}; |
}; |
602 |
break; |
break; |