1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
3 |
|
import static dk.thoerup.traininfo.R.string.app_name; |
4 |
|
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_search; |
27 |
|
import static dk.thoerup.traininfo.R.string.stationlist_stationadded; |
28 |
|
import static dk.thoerup.traininfo.R.string.stationlist_stationmap; |
29 |
|
import static dk.thoerup.traininfo.R.string.stationlist_stationremoved; |
30 |
|
import static dk.thoerup.traininfo.R.string.stationlist_stationsearch; |
31 |
|
import static dk.thoerup.traininfo.R.string.stationlist_twocharmin; |
32 |
|
import static dk.thoerup.traininfo.R.string.stationlist_waitforlocation; |
33 |
|
|
34 |
import java.util.ArrayList; |
import java.util.ArrayList; |
35 |
import java.util.List; |
import java.util.List; |
36 |
|
|
37 |
|
import android.app.Activity; |
38 |
import android.app.AlertDialog; |
import android.app.AlertDialog; |
39 |
import android.app.Dialog; |
import android.app.Dialog; |
40 |
import android.app.ListActivity; |
import android.app.ListActivity; |
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; |
59 |
import android.widget.EditText; |
import android.widget.EditText; |
60 |
import android.widget.ListView; |
import android.widget.ListView; |
61 |
import android.widget.Toast; |
import android.widget.Toast; |
62 |
|
import dk.thoerup.android.traininfo.common.StationBean; |
63 |
|
import dk.thoerup.android.traininfo.common.StationBean.StationEntry; |
64 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
import dk.thoerup.traininfo.provider.ProviderFactory; |
65 |
import dk.thoerup.traininfo.provider.StationProvider; |
import dk.thoerup.traininfo.provider.StationProvider; |
66 |
import dk.thoerup.traininfo.stationmap.GeoPair; |
import dk.thoerup.traininfo.stationmap.GeoPair; |
68 |
import dk.thoerup.traininfo.util.IntSet; |
import dk.thoerup.traininfo.util.IntSet; |
69 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
70 |
|
|
|
import static dk.thoerup.traininfo.R.string.*; |
|
|
|
|
71 |
public class StationList extends ListActivity { |
public class StationList extends ListActivity { |
72 |
public static final int GOTLOCATION = 1001; |
public static final int GOTLOCATION = 1001; |
73 |
public static final int GOTSTATIONLIST = 1002; |
public static final int GOTSTATIONLIST = 1002; |
76 |
|
|
77 |
public static final int OPTIONS_MAP = 2003; |
public static final int OPTIONS_MAP = 2003; |
78 |
public static final int OPTIONS_GPSINFO = 2004; |
public static final int OPTIONS_GPSINFO = 2004; |
|
|
|
|
|
|
|
|
|
79 |
|
|
80 |
public static final int DLG_PROGRESS = 3001; |
public static final int DLG_PROGRESS = 3001; |
81 |
public static final int DLG_STATIONNAME = 3002; |
public static final int DLG_STATIONNAME = 3002; |
82 |
|
|
83 |
|
|
84 |
|
public static final int GPS_TIMEOUT_MS = 15000; //how long are we willing to wait for gps fix -in milliseconds |
85 |
|
|
86 |
|
|
87 |
static enum LookupMethod { |
static enum LookupMethod { |
88 |
ByLocation, |
ByLocation, |
89 |
ByName, |
ByName, |
99 |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
100 |
|
|
101 |
GeoPair location = new GeoPair(); |
GeoPair location = new GeoPair(); |
102 |
|
|
103 |
|
boolean isLaunchedforShortcut; |
104 |
boolean isRunning = false; |
boolean isRunning = false; |
105 |
List<StationBean> stations = new ArrayList<StationBean>(); |
StationBean stations = new StationBean(); |
106 |
|
|
107 |
StationProvider stationProvider = ProviderFactory.getStationProvider(); |
StationProvider stationProvider = ProviderFactory.getStationProvider(); |
108 |
|
|
142 |
listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); |
listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); |
143 |
setTitle(); |
setTitle(); |
144 |
|
|
145 |
|
isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false); |
146 |
|
|
147 |
|
ProviderFactory.purgeOldEntries(); //cleanup before fetching more data |
148 |
|
|
149 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
150 |
|
|
151 |
|
|
153 |
case ListNearest: |
case ListNearest: |
154 |
startLookup(); |
startLookup(); |
155 |
break; |
break; |
156 |
case ListSearch: |
case ListSearch: |
157 |
this.showDialogSafe(DLG_STATIONNAME); |
showDialog(DLG_STATIONNAME); |
158 |
break; |
break; |
159 |
case ListFavorites: |
case ListFavorites: |
160 |
startFavoriteLookup(); |
startFavoriteLookup(); |
164 |
} |
} |
165 |
|
|
166 |
} else { |
} else { |
167 |
stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations"); |
stations = (StationBean) savedInstanceState.getSerializable("stations"); |
168 |
adapter.setStations(stations); |
adapter.setStations(stations); |
169 |
location = (GeoPair) savedInstanceState.getSerializable("location"); |
location = (GeoPair) savedInstanceState.getSerializable("location"); |
170 |
} |
} |
171 |
|
|
172 |
} |
} |
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
@Override |
180 |
|
protected void onDestroy() { |
181 |
|
super.onDestroy(); |
182 |
|
|
183 |
|
isRunning = false; |
184 |
|
|
185 |
|
if (locationLookup != null) { |
186 |
|
locationLookup.stopSearch(); |
187 |
|
} |
188 |
|
if (findStationsTask != null) { |
189 |
|
findStationsTask.cancel(true); |
190 |
|
} |
191 |
|
} |
192 |
|
|
193 |
|
|
194 |
protected void setTitle() { |
protected void setTitle() { |
195 |
String dialogTitle = getResources().getString(app_name); |
String dialogTitle = getResources().getString(app_name); |
196 |
switch (listType) { |
switch (listType) { |
211 |
|
|
212 |
} |
} |
213 |
|
|
|
|
|
|
/* these 3 dialogs helper functions are very rude and ugly hack |
|
|
* to remove these auto-reported exceptions |
|
|
* - android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@436aaef8 is not valid; is your activity running? |
|
|
* - java.lang.IllegalArgumentException: View not attached to window manager |
|
|
*/ |
|
|
|
|
|
public void showDialogSafe(int id) { |
|
|
try { |
|
|
showDialog(id); |
|
|
} catch (Exception e) { |
|
|
Log.e("StationList", "showDialog failed", e); |
|
|
} |
|
|
} |
|
|
|
|
|
public void dismissDialogSafe(int id) { |
|
|
try { |
|
|
dismissDialog(id); |
|
|
} catch (Exception e) { |
|
|
Log.e("StationList", "dismissDialog failed", e); |
|
|
} |
|
|
} |
|
|
|
|
|
public void builderShowSafe(AlertDialog.Builder builder) { |
|
|
try { |
|
|
builder.show(); |
|
|
} catch (Exception e) { |
|
|
Log.e("StationList", "builder.show() failed", e); |
|
|
} |
|
214 |
|
|
|
} |
|
|
/* EOF rude and ugly dialog hack */ |
|
|
|
|
|
|
|
215 |
|
|
216 |
@Override |
@Override |
217 |
public void onSaveInstanceState(Bundle outState) |
public void onSaveInstanceState(Bundle outState) |
218 |
{ |
{ |
219 |
if (dialog != null && dialog.isShowing()) |
if (dialog != null && dialog.isShowing()) |
220 |
dialog.dismiss(); |
dialog.dismiss(); |
221 |
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
outState.putSerializable("stations", (StationBean) stations); |
222 |
outState.putSerializable("location", location); |
outState.putSerializable("location", location); |
223 |
|
|
224 |
} |
} |
249 |
Intent intent = new Intent(this,StationMapView.class); |
Intent intent = new Intent(this,StationMapView.class); |
250 |
|
|
251 |
ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>(); |
ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>(); |
252 |
for (StationBean st : stations ) { |
for (StationEntry st : stations.entries ) { |
253 |
stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) ); |
stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) ); |
254 |
} |
} |
255 |
|
|
270 |
message.append( getString(stationlist_nolocation) ); |
message.append( getString(stationlist_nolocation) ); |
271 |
} |
} |
272 |
|
|
273 |
MessageBox.showMessage(this, message.toString()); |
MessageBox.showMessage(this, message.toString(), false); |
274 |
break; |
break; |
275 |
default: |
default: |
276 |
retval = super.onOptionsItemSelected(item); |
retval = super.onOptionsItemSelected(item); |
327 |
public void onClick(DialogInterface dialog, int which) { |
public void onClick(DialogInterface dialog, int which) { |
328 |
EditText et = (EditText) rootView.findViewById(R.id.EditText); |
EditText et = (EditText) rootView.findViewById(R.id.EditText); |
329 |
dialog.dismiss(); |
dialog.dismiss(); |
330 |
if (et.getText().toString().length() >= 2) { |
String search = et.getText().toString().trim(); |
331 |
startNameSearch(et.getText().toString()); |
if (search.length() >= 2) { |
332 |
|
startNameSearch(search); |
333 |
} else { |
} else { |
334 |
showMessageAndClose( getString(stationlist_twocharmin) ); |
showMessageAndClose( getString(stationlist_twocharmin) ); |
335 |
} |
} |
368 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
369 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
370 |
|
|
371 |
StationBean station = stations.get(position); |
StationEntry station = stations.entries.get(position); |
372 |
|
|
373 |
Intent intent = new Intent(this, DepartureList.class); |
if (isLaunchedforShortcut == true) { |
374 |
intent.putExtra("stationbean", station); |
Intent i = new Intent(); |
375 |
startActivity(intent); |
i.putExtra("station", station); |
376 |
|
setResult(Activity.RESULT_OK, i); |
377 |
|
finish(); |
378 |
|
} else { |
379 |
|
Intent intent = new Intent(this, DepartureList.class); |
380 |
|
intent.putExtra("stationbean", station); |
381 |
|
startActivity(intent); |
382 |
|
} |
383 |
} |
} |
384 |
|
|
385 |
///////////////////////////////////////////////////////////// |
///////////////////////////////////////////////////////////// |
388 |
public void startLookup() { |
public void startLookup() { |
389 |
isRunning = true; |
isRunning = true; |
390 |
dialogMessage = getString( stationlist_waitforlocation ); |
dialogMessage = getString( stationlist_waitforlocation ); |
391 |
showDialogSafe(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
392 |
|
|
393 |
locationLookup.locateStations(); |
locationLookup.locateStations(); |
394 |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS); |
395 |
} |
} |
396 |
|
|
397 |
void startNameSearch(String name) { |
void startNameSearch(String name) { |
398 |
dialogMessage = getString( stationlist_findbyname ); |
dialogMessage = getString( stationlist_findbyname ); |
399 |
showDialogSafe(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
400 |
|
|
401 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
402 |
findStationsTask.searchByName(name); |
findStationsTask.searchByName(name); |
408 |
|
|
409 |
if (favorites.size() > 0) { |
if (favorites.size() > 0) { |
410 |
dialogMessage = getString( stationlist_loadfavorites ); |
dialogMessage = getString( stationlist_loadfavorites ); |
411 |
showDialogSafe(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
412 |
|
|
413 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
414 |
findStationsTask.searchByIds( favorites.toString() ); |
findStationsTask.searchByIds( favorites.toString() ); |
423 |
void startLocatorTask() |
void startLocatorTask() |
424 |
{ |
{ |
425 |
dialogMessage = getString( stationlist_findingnearby ); |
dialogMessage = getString( stationlist_findingnearby ); |
426 |
showDialogSafe(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
427 |
|
|
428 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
429 |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
440 |
|
|
441 |
switch (msg.what) { |
switch (msg.what) { |
442 |
case GOTLOCATION: |
case GOTLOCATION: |
443 |
dismissDialogSafe(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
444 |
|
|
445 |
startLocatorTask(); |
startLocatorTask(); |
446 |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
448 |
break; |
break; |
449 |
|
|
450 |
case NOPROVIDER: |
case NOPROVIDER: |
451 |
dismissDialogSafe(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
452 |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) ); |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); |
453 |
|
//StationList.this.finish(); |
454 |
break; |
break; |
455 |
case LOCATIONFIXTIMEOUT: |
case LOCATIONFIXTIMEOUT: |
456 |
if (isRunning) { |
if (isRunning) { |
458 |
if (locationLookup.hasLocation()) { |
if (locationLookup.hasLocation()) { |
459 |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
460 |
} else { |
} else { |
461 |
dismissDialogSafe(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
462 |
|
|
463 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
464 |
builder.setMessage( getString( stationlist_gpstimeout) ); |
builder.setMessage( getString( stationlist_gpstimeout) ); |
475 |
dialog.dismiss(); |
dialog.dismiss(); |
476 |
} |
} |
477 |
}); |
}); |
478 |
builderShowSafe(builder); // builder.show() |
builder.show(); |
479 |
|
|
480 |
} |
} |
481 |
} |
} |
489 |
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
490 |
|
|
491 |
LookupMethod method = LookupMethod.MethodNone; |
LookupMethod method = LookupMethod.MethodNone; |
|
boolean success; |
|
492 |
String name; |
String name; |
493 |
Location loc; |
Location loc; |
494 |
String ids; |
String ids; |
523 |
|
|
524 |
switch (method) { |
switch (method) { |
525 |
case ByLocation: |
case ByLocation: |
526 |
success = stationProvider.lookupStations(loc); |
stations = stationProvider.lookupStations(loc); |
527 |
break; |
break; |
528 |
case ByName: |
case ByName: |
529 |
success = stationProvider.lookupStationsByName(name); |
stations = stationProvider.lookupStationsByName(name); |
530 |
break; |
break; |
531 |
case ByList: |
case ByList: |
532 |
success = stationProvider.lookupStationsByIds(ids); |
stations = stationProvider.lookupStationsByIds(ids); |
533 |
break; |
break; |
534 |
default: |
default: |
535 |
success = false; // not possible |
stations = null; // not possible |
536 |
} |
} |
537 |
|
|
538 |
|
|
545 |
dialog.dismiss(); |
dialog.dismiss(); |
546 |
|
|
547 |
|
|
548 |
if (success) { |
if (stations != null) { |
549 |
if (stationProvider.getStations().size() == 0) { |
if (stations.entries.size() == 0) { |
550 |
showMessageAndClose(getString(stationlist_nostations)); |
showMessageAndClose(getString(stationlist_nostations)); |
551 |
} |
} |
552 |
stations = stationProvider.getStations(); |
|
553 |
|
StationList.this.getListView().invalidateViews(); |
554 |
adapter.setStations( stations ); |
adapter.setStations( stations ); |
555 |
|
|
556 |
|
|
557 |
} else { //communication or parse errors |
} else { //communication or parse errors |
558 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
559 |
builder.setMessage(getString(stationlist_nearbyerror)); |
builder.setMessage(getString(stationlist_fetcherror)); |
560 |
builder.setCancelable(true); |
builder.setCancelable(true); |
561 |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
562 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
596 |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
597 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
598 |
dialog.dismiss(); |
dialog.dismiss(); |
599 |
|
StationList.this.finish(); |
600 |
} |
} |
601 |
}); |
}); |
602 |
|
|
603 |
builderShowSafe(builder); // builder.show() |
builder.show(); |
604 |
} |
} |
605 |
} |
} |
606 |
} |
} |
618 |
|
|
619 |
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; |
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; |
620 |
selectedPosition = info.position; |
selectedPosition = info.position; |
621 |
int stationID = stations.get(selectedPosition).getId(); |
int stationID = stations.entries.get(selectedPosition).getId(); |
622 |
|
|
623 |
if (!favorites.contains(stationID)) { |
if (!favorites.contains(stationID)) { |
624 |
menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) ); |
menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) ); |
629 |
} |
} |
630 |
|
|
631 |
public void onContextItemSelected(MenuItem item) { |
public void onContextItemSelected(MenuItem item) { |
632 |
StationBean sb = stations.get(selectedPosition); |
StationEntry sb = stations.entries.get(selectedPosition); |
633 |
|
|
634 |
int stationID = sb.getId(); |
int stationID = sb.getId(); |
635 |
if (item.getItemId() == FAVORITES_ADD) { |
if (item.getItemId() == FAVORITES_ADD) { |
642 |
|
|
643 |
|
|
644 |
if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) { |
if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) { |
645 |
stations.remove(selectedPosition); |
stations.entries.remove(selectedPosition); |
646 |
adapter.notifyDataSetChanged(); |
adapter.notifyDataSetChanged(); |
647 |
} |
} |
648 |
} |
} |