1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
3 |
import java.util.ArrayList; |
import static dk.thoerup.traininfo.R.string.app_name; |
4 |
import java.util.List; |
import static dk.thoerup.traininfo.R.string.generic_cancel; |
5 |
|
import static dk.thoerup.traininfo.R.string.generic_retry; |
6 |
|
import static dk.thoerup.traininfo.R.string.generic_search; |
7 |
|
import static dk.thoerup.traininfo.R.string.stationlist_accuracy; |
8 |
|
import static dk.thoerup.traininfo.R.string.stationlist_addfavorite; |
9 |
|
import static dk.thoerup.traininfo.R.string.stationlist_favorites; |
10 |
|
import static dk.thoerup.traininfo.R.string.stationlist_fetcherror; |
11 |
|
import static dk.thoerup.traininfo.R.string.stationlist_findbyname; |
12 |
|
import static dk.thoerup.traininfo.R.string.stationlist_findingnearby; |
13 |
|
import static dk.thoerup.traininfo.R.string.stationlist_gpsinfo; |
14 |
|
import static dk.thoerup.traininfo.R.string.stationlist_gpstimeout; |
15 |
|
import static dk.thoerup.traininfo.R.string.stationlist_latitude; |
16 |
|
import static dk.thoerup.traininfo.R.string.stationlist_loadfavorites; |
17 |
|
import static dk.thoerup.traininfo.R.string.stationlist_locationinfo; |
18 |
|
import static dk.thoerup.traininfo.R.string.stationlist_longitude; |
19 |
|
import static dk.thoerup.traininfo.R.string.stationlist_nearbystations; |
20 |
|
import static dk.thoerup.traininfo.R.string.stationlist_nofavorites; |
21 |
|
import static dk.thoerup.traininfo.R.string.stationlist_nolocation; |
22 |
|
import static dk.thoerup.traininfo.R.string.stationlist_nolocationprovider; |
23 |
|
import static dk.thoerup.traininfo.R.string.stationlist_nostations; |
24 |
|
import static dk.thoerup.traininfo.R.string.stationlist_obtainedby; |
25 |
|
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; |
28 |
|
import static dk.thoerup.traininfo.R.string.stationlist_stationadded; |
29 |
|
import static dk.thoerup.traininfo.R.string.stationlist_stationmap; |
30 |
|
import static dk.thoerup.traininfo.R.string.stationlist_stationremoved; |
31 |
|
import static dk.thoerup.traininfo.R.string.stationlist_stationsearch; |
32 |
|
import static dk.thoerup.traininfo.R.string.stationlist_twocharmin; |
33 |
|
import static dk.thoerup.traininfo.R.string.stationlist_waitforlocation; |
34 |
|
|
35 |
|
import java.util.ArrayList; |
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; |
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; |
64 |
|
import dk.thoerup.android.traininfo.common.StationBean.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; |
69 |
import dk.thoerup.traininfo.util.IntSet; |
import dk.thoerup.traininfo.util.IntSet; |
70 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
71 |
|
|
|
import static dk.thoerup.traininfo.R.string.*; |
|
|
|
|
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 |
List<StationBean> stations = new ArrayList<StationBean>(); |
StationBean stations = new StationBean(); |
105 |
|
|
106 |
StationProvider stationProvider = ProviderFactory.getStationProvider(); |
StationProvider stationProvider = ProviderFactory.getStationProvider(); |
107 |
|
|
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); |
163 |
} |
} |
164 |
|
|
165 |
} else { |
} else { |
166 |
stations = (ArrayList<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(); |
217 |
{ |
{ |
218 |
if (dialog != null && dialog.isShowing()) |
if (dialog != null && dialog.isShowing()) |
219 |
dialog.dismiss(); |
dialog.dismiss(); |
220 |
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
outState.putSerializable("stations", (StationBean) stations); |
|
outState.putSerializable("location", location); |
|
221 |
|
|
222 |
} |
} |
223 |
|
|
247 |
Intent intent = new Intent(this,StationMapView.class); |
Intent intent = new Intent(this,StationMapView.class); |
248 |
|
|
249 |
ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>(); |
ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>(); |
250 |
for (StationBean st : stations ) { |
for (StationEntry st : stations.entries ) { |
251 |
stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) ); |
stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) ); |
252 |
} |
} |
253 |
|
|
327 |
dialog.dismiss(); |
dialog.dismiss(); |
328 |
String search = et.getText().toString().trim(); |
String search = et.getText().toString().trim(); |
329 |
if (search.length() >= 2) { |
if (search.length() >= 2) { |
330 |
startNameSearch(search); |
startNameLookup(search); |
331 |
} else { |
} else { |
332 |
showMessageAndClose( getString(stationlist_twocharmin) ); |
showMessageAndClose( getString(stationlist_twocharmin) ); |
333 |
} |
} |
366 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
367 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
368 |
|
|
369 |
StationBean station = stations.get(position); |
StationEntry station = stations.entries.get(position); |
370 |
|
|
371 |
if (isLaunchedforShortcut == true) { |
if (isLaunchedforShortcut == true) { |
372 |
Intent i = new Intent(); |
Intent i = new Intent(); |
383 |
///////////////////////////////////////////////////////////// |
///////////////////////////////////////////////////////////// |
384 |
// |
// |
385 |
|
|
386 |
public void startLookup() { |
public void startNearestLookup() { |
|
isRunning = true; |
|
387 |
dialogMessage = getString( stationlist_waitforlocation ); |
dialogMessage = getString( stationlist_waitforlocation ); |
388 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
389 |
|
|
390 |
locationLookup.locateStations(); |
locationLookup.locateStations(); |
391 |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS); |
stationsFetched.sendEmptyMessageDelayed(0, 500); |
392 |
} |
} |
393 |
|
|
394 |
void startNameSearch(String name) { |
void startNameLookup(String name) { |
395 |
dialogMessage = getString( stationlist_findbyname ); |
dialogMessage = getString( stationlist_findbyname ); |
396 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
397 |
|
|
420 |
void startLocatorTask() |
void startLocatorTask() |
421 |
{ |
{ |
422 |
dialogMessage = getString( stationlist_findingnearby ); |
dialogMessage = getString( stationlist_findingnearby ); |
423 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
424 |
|
|
425 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
426 |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
435 |
@Override |
@Override |
436 |
public void handleMessage(Message msg) { |
public void handleMessage(Message msg) { |
437 |
|
|
438 |
switch (msg.what) { |
LocationLookup.LookupStates state = locationLookup.getState(); |
439 |
|
|
440 |
|
|
441 |
|
switch (state) { |
442 |
case GOTLOCATION: |
case GOTLOCATION: |
443 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
444 |
|
|
445 |
startLocatorTask(); |
startLocatorTask(); |
446 |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
|
447 |
|
return; |
|
break; |
|
448 |
|
|
449 |
case NOPROVIDER: |
case NOPROVIDER: |
450 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
451 |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); |
452 |
//StationList.this.finish(); |
//StationList.this.finish(); |
453 |
break; |
return; |
454 |
case LOCATIONFIXTIMEOUT: |
case IDLE: |
455 |
if (isRunning) { |
Log.e("TrainInfo", "How did this happen ???"); |
456 |
locationLookup.stopSearch(); |
dismissDialog(DLG_PROGRESS); // how did we get here ?? |
457 |
if (locationLookup.hasLocation()) { |
return; |
458 |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
|
459 |
} else { |
} |
460 |
dismissDialog(DLG_PROGRESS); |
|
461 |
|
|
462 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
|
463 |
builder.setMessage( getString( stationlist_gpstimeout) ); |
if (locationLookup.elapsedTime() >= GPS_TIMEOUT_MS) { |
464 |
builder.setCancelable(true); |
dismissDialog(DLG_PROGRESS); |
465 |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
|
466 |
public void onClick(DialogInterface dialog, int id) { |
|
467 |
dialog.dismiss(); |
locationLookup.stopSearch(); |
468 |
startLookup(); |
|
469 |
|
if (locationLookup.hasLocation()) { |
470 |
} |
startLocatorTask(); |
471 |
}); |
} else { |
472 |
builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() { |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
473 |
public void onClick(DialogInterface dialog, int id) { |
builder.setMessage( getString( stationlist_gpstimeout) ); |
474 |
dialog.dismiss(); |
builder.setCancelable(true); |
475 |
} |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
476 |
}); |
public void onClick(DialogInterface dialog, int id) { |
477 |
builder.show(); |
dialog.dismiss(); |
478 |
|
startNearestLookup(); |
479 |
|
|
480 |
|
} |
481 |
|
}); |
482 |
|
builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() { |
483 |
|
public void onClick(DialogInterface dialog, int id) { |
484 |
|
dialog.dismiss(); |
485 |
|
StationList.this.finish(); // Close this Activity |
486 |
|
} |
487 |
|
}); |
488 |
|
builder.show(); |
489 |
|
|
|
} |
|
490 |
} |
} |
491 |
break; |
} else { |
492 |
|
if (locationLookup.hasGps()) { |
493 |
|
int count = locationLookup.getSatCount(); |
494 |
|
String dialogMessage = getString( stationlist_waitforlocation ) + "\n" + getString( stationlist_satellitecount ) + ": " + count; |
495 |
|
dialog.setMessage( dialogMessage ); |
496 |
|
} |
497 |
|
this.sendEmptyMessageDelayed(0, 500); |
498 |
} |
} |
499 |
isRunning = false; |
|
500 |
} |
} |
501 |
}; |
} |
502 |
|
|
503 |
|
|
504 |
|
|
505 |
|
|
506 |
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
540 |
|
|
541 |
switch (method) { |
switch (method) { |
542 |
case ByLocation: |
case ByLocation: |
543 |
stations = stationProvider.lookupStations(loc); |
stations = stationProvider.lookupStationsByLocation(loc); |
544 |
break; |
break; |
545 |
case ByName: |
case ByName: |
546 |
stations = stationProvider.lookupStationsByName(name); |
stations = stationProvider.lookupStationsByName(name); |
563 |
|
|
564 |
|
|
565 |
if (stations != null) { |
if (stations != null) { |
566 |
if (stations.size() == 0) { |
if (stations.entries.size() == 0) { |
567 |
showMessageAndClose(getString(stationlist_nostations)); |
showMessageAndClose(getString(stationlist_nostations)); |
568 |
} |
} |
569 |
|
|
593 |
runner = new Runnable() { |
runner = new Runnable() { |
594 |
@Override |
@Override |
595 |
public void run() { |
public void run() { |
596 |
startNameSearch( FindStationsTask.this.name ); |
startNameLookup( FindStationsTask.this.name ); |
597 |
} |
} |
598 |
}; |
}; |
599 |
break; |
break; |
635 |
|
|
636 |
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; |
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; |
637 |
selectedPosition = info.position; |
selectedPosition = info.position; |
638 |
int stationID = stations.get(selectedPosition).getId(); |
int stationID = stations.entries.get(selectedPosition).getId(); |
639 |
|
|
640 |
if (!favorites.contains(stationID)) { |
if (!favorites.contains(stationID)) { |
641 |
menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) ); |
menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) ); |
646 |
} |
} |
647 |
|
|
648 |
public void onContextItemSelected(MenuItem item) { |
public void onContextItemSelected(MenuItem item) { |
649 |
StationBean sb = stations.get(selectedPosition); |
StationEntry sb = stations.entries.get(selectedPosition); |
650 |
|
|
651 |
int stationID = sb.getId(); |
int stationID = sb.getId(); |
652 |
if (item.getItemId() == FAVORITES_ADD) { |
if (item.getItemId() == FAVORITES_ADD) { |
659 |
|
|
660 |
|
|
661 |
if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) { |
if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) { |
662 |
stations.remove(selectedPosition); |
stations.entries.remove(selectedPosition); |
663 |
adapter.notifyDataSetChanged(); |
adapter.notifyDataSetChanged(); |
664 |
} |
} |
665 |
} |
} |