18 |
import android.os.Handler; |
import android.os.Handler; |
19 |
import android.os.Message; |
import android.os.Message; |
20 |
|
|
21 |
|
import android.util.Log; |
22 |
import android.view.ContextMenu; |
import android.view.ContextMenu; |
23 |
import android.view.LayoutInflater; |
import android.view.LayoutInflater; |
24 |
import android.view.Menu; |
import android.view.Menu; |
119 |
startLookup(); |
startLookup(); |
120 |
break; |
break; |
121 |
case ListSearch: |
case ListSearch: |
122 |
this.showDialog(DLG_STATIONNAME); |
this.showDialogSafe(DLG_STATIONNAME); |
123 |
break; |
break; |
124 |
case ListFavorites: |
case ListFavorites: |
125 |
startFavoriteLookup(); |
startFavoriteLookup(); |
152 |
} |
} |
153 |
|
|
154 |
setTitle(dialogTitle); |
setTitle(dialogTitle); |
155 |
|
|
156 |
|
} |
157 |
|
|
158 |
|
|
159 |
|
/* these 3 dialogs helper functions are very rude and ugly hack |
160 |
|
* to remove these auto-reported exceptions |
161 |
|
* - android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@436aaef8 is not valid; is your activity running? |
162 |
|
* - java.lang.IllegalArgumentException: View not attached to window manager |
163 |
|
*/ |
164 |
|
|
165 |
|
public void showDialogSafe(int id) { |
166 |
|
try { |
167 |
|
showDialog(id); |
168 |
|
} catch (Exception e) { |
169 |
|
Log.e("StationList", "showDialog failed", e); |
170 |
|
} |
171 |
} |
} |
172 |
|
|
173 |
|
public void dismissDialogSafe(int id) { |
174 |
|
try { |
175 |
|
dismissDialog(id); |
176 |
|
} catch (Exception e) { |
177 |
|
Log.e("StationList", "dismissDialog failed", e); |
178 |
|
} |
179 |
|
} |
180 |
|
|
181 |
|
public void builderShowSafe(AlertDialog.Builder builder) { |
182 |
|
try { |
183 |
|
builder.show(); |
184 |
|
} catch (Exception e) { |
185 |
|
Log.e("StationList", "builder.show() failed", e); |
186 |
|
} |
187 |
|
|
188 |
|
} |
189 |
|
/* EOF rude and ugly dialog hack */ |
190 |
|
|
191 |
|
|
192 |
|
|
357 |
public void startLookup() { |
public void startLookup() { |
358 |
isRunning = true; |
isRunning = true; |
359 |
dialogMessage = getString( stationlist_waitforlocation ); |
dialogMessage = getString( stationlist_waitforlocation ); |
360 |
showDialog(DLG_PROGRESS); |
showDialogSafe(DLG_PROGRESS); |
361 |
|
|
362 |
locationLookup.locateStations(); |
locationLookup.locateStations(); |
363 |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); |
365 |
|
|
366 |
void startNameSearch(String name) { |
void startNameSearch(String name) { |
367 |
dialogMessage = getString( stationlist_findbyname ); |
dialogMessage = getString( stationlist_findbyname ); |
368 |
showDialog(DLG_PROGRESS); |
showDialogSafe(DLG_PROGRESS); |
369 |
|
|
370 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
371 |
findStationsTask.searchByName(name, locationLookup.getLocation()); |
findStationsTask.searchByName(name); |
372 |
findStationsTask.execute(); |
findStationsTask.execute(); |
373 |
|
|
374 |
} |
} |
377 |
|
|
378 |
if (favorites.size() > 0) { |
if (favorites.size() > 0) { |
379 |
dialogMessage = getString( stationlist_loadfavorites ); |
dialogMessage = getString( stationlist_loadfavorites ); |
380 |
showDialog(DLG_PROGRESS); |
showDialogSafe(DLG_PROGRESS); |
381 |
|
|
382 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
383 |
findStationsTask.searchByIds(favorites.toString(), locationLookup.getLocation()); |
findStationsTask.searchByIds( favorites.toString() ); |
384 |
findStationsTask.execute(); |
findStationsTask.execute(); |
385 |
} else { |
} else { |
386 |
showMessageAndClose( getString( stationlist_nofavorites ) ); |
showMessageAndClose( getString( stationlist_nofavorites ) ); |
392 |
void startLocatorTask() |
void startLocatorTask() |
393 |
{ |
{ |
394 |
dialogMessage = getString( stationlist_findingnearby ); |
dialogMessage = getString( stationlist_findingnearby ); |
395 |
showDialog(DLG_PROGRESS); |
showDialogSafe(DLG_PROGRESS); |
396 |
|
|
397 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
398 |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
399 |
findStationsTask.execute(); |
findStationsTask.execute(); |
400 |
} |
} |
|
|
|
|
|
|
|
/* TODO: Remove this no longer needed function |
|
|
String lookupAddress(double latitude, double longitude) { |
|
|
|
|
|
Geocoder coder = new Geocoder(this, new Locale("da")); |
|
|
StringBuilder sb = new StringBuilder(); |
|
|
Log.i("lookupaddr", "" + latitude + "/" + longitude); |
|
|
try { |
|
|
List<Address> addressList = coder.getFromLocation(latitude, longitude, 1); |
|
|
Address addr = addressList.get(0); |
|
|
|
|
|
|
|
|
int max = addr.getMaxAddressLineIndex(); |
|
|
for (int i=0; i<max; i++) { |
|
|
if (i>0) |
|
|
sb.append(", "); |
|
|
|
|
|
sb.append(addr.getAddressLine(i)); |
|
|
} |
|
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
Log.e("DepartureList", "geocoder failed", e); |
|
|
} |
|
401 |
|
|
|
return sb.toString(); |
|
|
}*/ |
|
|
|
|
402 |
|
|
403 |
//////////////////////////////////////////////////////////////////////////// |
//////////////////////////////////////////////////////////////////////////// |
404 |
// Inner classes |
// Inner classes |
409 |
|
|
410 |
switch (msg.what) { |
switch (msg.what) { |
411 |
case GOTLOCATION: |
case GOTLOCATION: |
412 |
dismissDialog(DLG_PROGRESS); |
dismissDialogSafe(DLG_PROGRESS); |
413 |
|
|
414 |
startLocatorTask(); |
startLocatorTask(); |
415 |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
417 |
break; |
break; |
418 |
|
|
419 |
case NOPROVIDER: |
case NOPROVIDER: |
420 |
dismissDialog(DLG_PROGRESS); |
dismissDialogSafe(DLG_PROGRESS); |
421 |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) ); |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) ); |
422 |
break; |
break; |
423 |
case LOCATIONFIXTIMEOUT: |
case LOCATIONFIXTIMEOUT: |
426 |
if (locationLookup.hasLocation()) { |
if (locationLookup.hasLocation()) { |
427 |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
428 |
} else { |
} else { |
429 |
dismissDialog(DLG_PROGRESS); |
dismissDialogSafe(DLG_PROGRESS); |
430 |
|
|
431 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
432 |
builder.setMessage( getString( stationlist_gpstimeout) ); |
builder.setMessage( getString( stationlist_gpstimeout) ); |
442 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
443 |
dialog.dismiss(); |
dialog.dismiss(); |
444 |
} |
} |
445 |
}); |
}); |
446 |
builder.show(); |
builderShowSafe(builder); // builder.show() |
447 |
|
|
448 |
} |
} |
449 |
} |
} |
462 |
Location loc; |
Location loc; |
463 |
String ids; |
String ids; |
464 |
|
|
465 |
public void searchByName(String n, Location l) { |
public void searchByName(String n) { |
466 |
|
|
467 |
method = LookupMethod.ByName; |
method = LookupMethod.ByName; |
|
loc = l; |
|
468 |
name = n; |
name = n; |
469 |
} |
} |
470 |
|
|
473 |
loc = l; |
loc = l; |
474 |
} |
} |
475 |
|
|
476 |
public void searchByIds(String id, Location l) { |
public void searchByIds(String id) { |
477 |
|
|
478 |
method = LookupMethod.ByList; |
method = LookupMethod.ByList; |
|
loc = l; |
|
479 |
ids = id; |
ids = id; |
480 |
} |
} |
481 |
|
|
505 |
} |
} |
506 |
|
|
507 |
|
|
|
Location dummy = new Location("gps"); |
|
|
List<StationBean> stations = stationProvider.getStations(); |
|
|
|
|
|
for (StationBean station : stations) { |
|
|
|
|
|
if (method.equals(LookupMethod.ByName) || method.equals(LookupMethod.ByList)) { |
|
|
if (loc != null) { //only do the distance calc if we have a location |
|
|
dummy.setLatitude(station.getLatitude()); |
|
|
dummy.setLongitude(station.getLongitude()); |
|
|
station.setDistance( (int)loc.distanceTo(dummy) ); |
|
|
} else { |
|
|
station.setDistance(0); |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
508 |
return null; |
return null; |
509 |
} |
} |
510 |
|
|
529 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
530 |
dialog.dismiss(); |
dialog.dismiss(); |
531 |
|
|
532 |
stationsFetched.post( new Runnable() { |
Runnable runner = null; |
533 |
@Override |
switch (method) { |
534 |
public void run() { |
case ByLocation: |
535 |
startLocatorTask(); |
runner = new Runnable() { |
536 |
} |
@Override |
537 |
}); |
public void run() { |
538 |
|
startLocatorTask(); |
539 |
|
} |
540 |
|
}; |
541 |
|
break; |
542 |
|
case ByName: |
543 |
|
runner = new Runnable() { |
544 |
|
@Override |
545 |
|
public void run() { |
546 |
|
startNameSearch( FindStationsTask.this.name ); |
547 |
|
} |
548 |
|
}; |
549 |
|
break; |
550 |
|
case ByList: |
551 |
|
runner = new Runnable() { |
552 |
|
@Override |
553 |
|
public void run() { |
554 |
|
startFavoriteLookup(); |
555 |
|
} |
556 |
|
}; |
557 |
|
break; |
558 |
|
} |
559 |
|
|
560 |
|
stationsFetched.post( runner ); |
561 |
} |
} |
562 |
}); |
}); |
563 |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
564 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
565 |
dialog.dismiss(); |
dialog.dismiss(); |
566 |
} |
} |
567 |
}); |
}); |
568 |
builder.show(); |
|
569 |
|
builderShowSafe(builder); // builder.show() |
570 |
} |
} |
571 |
} |
} |
572 |
} |
} |