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.ContextMenu.ContextMenuInfo; |
54 |
import android.view.LayoutInflater; |
import android.view.LayoutInflater; |
55 |
import android.view.Menu; |
import android.view.Menu; |
56 |
import android.view.MenuItem; |
import android.view.MenuItem; |
57 |
import android.view.View; |
import android.view.View; |
|
import android.view.ContextMenu.ContextMenuInfo; |
|
58 |
import android.view.View.OnCreateContextMenuListener; |
import android.view.View.OnCreateContextMenuListener; |
59 |
import android.widget.AdapterView; |
import android.widget.AdapterView; |
60 |
import android.widget.EditText; |
import android.widget.EditText; |
61 |
import android.widget.ListView; |
import android.widget.ListView; |
62 |
import android.widget.Toast; |
import android.widget.Toast; |
63 |
import dk.thoerup.android.traininfo.common.StationBean; |
import dk.thoerup.android.traininfo.common.StationBean; |
64 |
import dk.thoerup.android.traininfo.common.StationBean.StationEntry; |
import dk.thoerup.android.traininfo.common.StationEntry; |
65 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
import dk.thoerup.traininfo.provider.ProviderFactory; |
66 |
import dk.thoerup.traininfo.provider.StationProvider; |
import dk.thoerup.traininfo.provider.StationProvider; |
67 |
import dk.thoerup.traininfo.stationmap.GeoPair; |
import dk.thoerup.traininfo.stationmap.GeoPair; |
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; |
|
|
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; |
|
76 |
public static final int OPTIONS_GPSINFO = 2004; |
public static final int OPTIONS_MAP = 103; |
77 |
|
public static final int OPTIONS_GPSINFO = 104; |
78 |
|
|
79 |
public static final int DLG_PROGRESS = 3001; |
public static final int DLG_PROGRESS = 3001; |
80 |
public static final int DLG_STATIONNAME = 3002; |
public static final int DLG_STATIONNAME = 3002; |
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 |
|
|
181 |
isRunning = false; |
stationsFetched.removeMessages(0); |
182 |
|
|
183 |
|
|
184 |
if (locationLookup != null) { |
if (locationLookup != null) { |
185 |
locationLookup.stopSearch(); |
locationLookup.stopSearch(); |
218 |
if (dialog != null && dialog.isShowing()) |
if (dialog != null && dialog.isShowing()) |
219 |
dialog.dismiss(); |
dialog.dismiss(); |
220 |
outState.putSerializable("stations", (StationBean) stations); |
outState.putSerializable("stations", (StationBean) stations); |
|
outState.putSerializable("location", location); |
|
221 |
|
|
222 |
} |
} |
223 |
|
|
231 |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
232 |
|
|
233 |
item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo)); |
item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo)); |
234 |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
item.setIcon(android.R.drawable.ic_menu_info_details); |
235 |
|
boolean hasLoc = (locationLookup.getLocation() != null); |
236 |
|
item.setEnabled(hasLoc); |
237 |
|
|
238 |
return true; |
return true; |
239 |
} |
} |
240 |
|
|
241 |
|
|
242 |
|
|
243 |
|
|
244 |
@Override |
@Override |
245 |
public boolean onOptionsItemSelected(MenuItem item) { |
public boolean onOptionsItemSelected(MenuItem item) { |
246 |
boolean retval = true; |
boolean retval = true; |
247 |
|
|
248 |
//TODO: Cleanup |
|
249 |
switch (item.getItemId()) { |
switch (item.getItemId()) { |
250 |
case OPTIONS_MAP: |
case OPTIONS_MAP: |
251 |
|
|
252 |
|
if ( stations == null || stations.entries == null || stations.entries.size() == 0 ) { |
253 |
|
Toast.makeText(this, "No stations to show on map", Toast.LENGTH_SHORT); //TODO: Translate |
254 |
|
return true; |
255 |
|
} |
256 |
|
|
257 |
Intent intent = new Intent(this,StationMapView.class); |
Intent intent = new Intent(this,StationMapView.class); |
258 |
|
|
259 |
ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>(); |
ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>(); |
269 |
Location loc = locationLookup.getLocation(); |
Location loc = locationLookup.getLocation(); |
270 |
StringBuffer message = new StringBuffer(); |
StringBuffer message = new StringBuffer(); |
271 |
message.append( getString(stationlist_locationinfo) ).append(":\n"); |
message.append( getString(stationlist_locationinfo) ).append(":\n"); |
272 |
if (loc != null) { |
|
273 |
message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n"); |
message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n"); |
274 |
message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n"); |
message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n"); |
275 |
message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n"); |
message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n"); |
276 |
message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n"); |
message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n"); |
277 |
} else { |
|
|
message.append( getString(stationlist_nolocation) ); |
|
|
} |
|
278 |
|
|
279 |
MessageBox.showMessage(this, message.toString(), false); |
MessageBox.showMessage(this, message.toString(), false); |
280 |
break; |
break; |
335 |
dialog.dismiss(); |
dialog.dismiss(); |
336 |
String search = et.getText().toString().trim(); |
String search = et.getText().toString().trim(); |
337 |
if (search.length() >= 2) { |
if (search.length() >= 2) { |
338 |
startNameSearch(search); |
startNameLookup(search); |
339 |
} else { |
} else { |
340 |
showMessageAndClose( getString(stationlist_twocharmin) ); |
showMessageAndClose( getString(stationlist_twocharmin) ); |
341 |
} |
} |
391 |
///////////////////////////////////////////////////////////// |
///////////////////////////////////////////////////////////// |
392 |
// |
// |
393 |
|
|
394 |
public void startLookup() { |
public void startNearestLookup() { |
|
isRunning = true; |
|
395 |
dialogMessage = getString( stationlist_waitforlocation ); |
dialogMessage = getString( stationlist_waitforlocation ); |
396 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
397 |
|
|
398 |
locationLookup.locateStations(); |
locationLookup.locateStations(); |
399 |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS); |
stationsFetched.sendEmptyMessageDelayed(0, 500); |
400 |
} |
} |
401 |
|
|
402 |
void startNameSearch(String name) { |
void startNameLookup(String name) { |
403 |
dialogMessage = getString( stationlist_findbyname ); |
dialogMessage = getString( stationlist_findbyname ); |
404 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
405 |
|
|
428 |
void startLocatorTask() |
void startLocatorTask() |
429 |
{ |
{ |
430 |
dialogMessage = getString( stationlist_findingnearby ); |
dialogMessage = getString( stationlist_findingnearby ); |
431 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
432 |
|
|
433 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
434 |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
443 |
@Override |
@Override |
444 |
public void handleMessage(Message msg) { |
public void handleMessage(Message msg) { |
445 |
|
|
446 |
switch (msg.what) { |
LocationLookup.LookupStates state = locationLookup.getState(); |
447 |
|
|
448 |
|
|
449 |
|
switch (state) { |
450 |
case GOTLOCATION: |
case GOTLOCATION: |
451 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
452 |
|
|
453 |
startLocatorTask(); |
startLocatorTask(); |
454 |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
|
455 |
|
return; |
|
break; |
|
456 |
|
|
457 |
case NOPROVIDER: |
case NOPROVIDER: |
458 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
459 |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); |
460 |
//StationList.this.finish(); |
//StationList.this.finish(); |
461 |
break; |
return; |
462 |
case LOCATIONFIXTIMEOUT: |
case IDLE: |
463 |
if (isRunning) { |
Log.e("TrainInfo", "How did this happen ???"); |
464 |
locationLookup.stopSearch(); |
dismissDialog(DLG_PROGRESS); // how did we get here ?? |
465 |
if (locationLookup.hasLocation()) { |
return; |
466 |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
|
467 |
} else { |
} |
468 |
dismissDialog(DLG_PROGRESS); |
|
469 |
|
|
470 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
|
471 |
builder.setMessage( getString( stationlist_gpstimeout) ); |
if (locationLookup.elapsedTime() >= GPS_TIMEOUT_MS) { |
472 |
builder.setCancelable(true); |
dismissDialog(DLG_PROGRESS); |
473 |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
|
474 |
public void onClick(DialogInterface dialog, int id) { |
|
475 |
dialog.dismiss(); |
locationLookup.stopSearch(); |
476 |
startLookup(); |
|
477 |
|
if (locationLookup.hasLocation()) { |
478 |
} |
startLocatorTask(); |
479 |
}); |
} else { |
480 |
builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() { |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
481 |
public void onClick(DialogInterface dialog, int id) { |
builder.setMessage( getString( stationlist_gpstimeout) ); |
482 |
dialog.dismiss(); |
builder.setCancelable(true); |
483 |
} |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
484 |
}); |
public void onClick(DialogInterface dialog, int id) { |
485 |
builder.show(); |
dialog.dismiss(); |
486 |
|
startNearestLookup(); |
487 |
|
|
488 |
|
} |
489 |
|
}); |
490 |
|
builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() { |
491 |
|
public void onClick(DialogInterface dialog, int id) { |
492 |
|
dialog.dismiss(); |
493 |
|
StationList.this.finish(); // Close this Activity |
494 |
|
} |
495 |
|
}); |
496 |
|
builder.show(); |
497 |
|
|
|
} |
|
498 |
} |
} |
499 |
break; |
} else { |
500 |
|
if (locationLookup.hasGps()) { |
501 |
|
int count = locationLookup.getSatCount(); |
502 |
|
String dialogMessage = getString( stationlist_waitforlocation ) + "\n" + getString( stationlist_satellitecount ) + ": " + count; |
503 |
|
dialog.setMessage( dialogMessage ); |
504 |
|
} |
505 |
|
this.sendEmptyMessageDelayed(0, 500); |
506 |
} |
} |
507 |
isRunning = false; |
|
508 |
} |
} |
509 |
}; |
} |
510 |
|
|
511 |
|
|
512 |
|
|
513 |
|
|
514 |
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
548 |
|
|
549 |
switch (method) { |
switch (method) { |
550 |
case ByLocation: |
case ByLocation: |
551 |
stations = stationProvider.lookupStations(loc); |
stations = stationProvider.lookupStationsByLocation(loc); |
552 |
break; |
break; |
553 |
case ByName: |
case ByName: |
554 |
stations = stationProvider.lookupStationsByName(name); |
stations = stationProvider.lookupStationsByName(name); |
601 |
runner = new Runnable() { |
runner = new Runnable() { |
602 |
@Override |
@Override |
603 |
public void run() { |
public void run() { |
604 |
startNameSearch( FindStationsTask.this.name ); |
startNameLookup( FindStationsTask.this.name ); |
605 |
} |
} |
606 |
}; |
}; |
607 |
break; |
break; |