19 |
import android.os.Handler; |
import android.os.Handler; |
20 |
import android.os.Message; |
import android.os.Message; |
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; |
47 |
|
|
48 |
public static final int OPTIONS_MAP = 2003; |
public static final int OPTIONS_MAP = 2003; |
49 |
public static final int OPTIONS_GPSINFO = 2004; |
public static final int OPTIONS_GPSINFO = 2004; |
|
|
|
|
|
|
|
|
|
50 |
|
|
51 |
public static final int DLG_PROGRESS = 3001; |
public static final int DLG_PROGRESS = 3001; |
52 |
public static final int DLG_STATIONNAME = 3002; |
public static final int DLG_STATIONNAME = 3002; |
53 |
|
|
54 |
|
|
55 |
|
public static final int GPS_TIMEOUT_MS = 15000; //how long are we willing to wait for gps fix -in milliseconds |
56 |
|
|
57 |
|
|
58 |
static enum LookupMethod { |
static enum LookupMethod { |
59 |
ByLocation, |
ByLocation, |
60 |
ByName, |
ByName, |
122 |
case ListNearest: |
case ListNearest: |
123 |
startLookup(); |
startLookup(); |
124 |
break; |
break; |
125 |
case ListSearch: |
case ListSearch: |
126 |
this.showDialogSafe(DLG_STATIONNAME); |
showDialog(DLG_STATIONNAME); |
127 |
break; |
break; |
128 |
case ListFavorites: |
case ListFavorites: |
129 |
startFavoriteLookup(); |
startFavoriteLookup(); |
139 |
} |
} |
140 |
|
|
141 |
} |
} |
142 |
|
|
143 |
|
|
144 |
|
@Override |
145 |
|
protected void onDestroy() { |
146 |
|
super.onDestroy(); |
147 |
|
|
148 |
|
isRunning = false; |
149 |
|
|
150 |
|
if (locationLookup != null) { |
151 |
|
locationLookup.stopSearch(); |
152 |
|
} |
153 |
|
if (findStationsTask != null) { |
154 |
|
findStationsTask.cancel(true); |
155 |
|
} |
156 |
|
} |
157 |
|
|
158 |
|
|
159 |
protected void setTitle() { |
protected void setTitle() { |
160 |
String dialogTitle = getResources().getString(app_name); |
String dialogTitle = getResources().getString(app_name); |
161 |
switch (listType) { |
switch (listType) { |
176 |
|
|
177 |
} |
} |
178 |
|
|
|
|
|
|
/* 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 dismissDialogSafe(Dialog dlg) { |
|
|
try { |
|
|
dlg.dismiss(); |
|
|
} 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); |
|
|
} |
|
179 |
|
|
|
} |
|
|
/* EOF rude and ugly dialog hack */ |
|
|
|
|
|
|
|
180 |
|
|
181 |
@Override |
@Override |
182 |
public void onSaveInstanceState(Bundle outState) |
public void onSaveInstanceState(Bundle outState) |
353 |
public void startLookup() { |
public void startLookup() { |
354 |
isRunning = true; |
isRunning = true; |
355 |
dialogMessage = getString( stationlist_waitforlocation ); |
dialogMessage = getString( stationlist_waitforlocation ); |
356 |
showDialogSafe(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
357 |
|
|
358 |
locationLookup.locateStations(); |
locationLookup.locateStations(); |
359 |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS); |
360 |
} |
} |
361 |
|
|
362 |
void startNameSearch(String name) { |
void startNameSearch(String name) { |
363 |
dialogMessage = getString( stationlist_findbyname ); |
dialogMessage = getString( stationlist_findbyname ); |
364 |
showDialogSafe(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
365 |
|
|
366 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
367 |
findStationsTask.searchByName(name); |
findStationsTask.searchByName(name); |
373 |
|
|
374 |
if (favorites.size() > 0) { |
if (favorites.size() > 0) { |
375 |
dialogMessage = getString( stationlist_loadfavorites ); |
dialogMessage = getString( stationlist_loadfavorites ); |
376 |
showDialogSafe(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
377 |
|
|
378 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
379 |
findStationsTask.searchByIds( favorites.toString() ); |
findStationsTask.searchByIds( favorites.toString() ); |
388 |
void startLocatorTask() |
void startLocatorTask() |
389 |
{ |
{ |
390 |
dialogMessage = getString( stationlist_findingnearby ); |
dialogMessage = getString( stationlist_findingnearby ); |
391 |
showDialogSafe(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
392 |
|
|
393 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
394 |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
405 |
|
|
406 |
switch (msg.what) { |
switch (msg.what) { |
407 |
case GOTLOCATION: |
case GOTLOCATION: |
408 |
dismissDialogSafe(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
409 |
|
|
410 |
startLocatorTask(); |
startLocatorTask(); |
411 |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
413 |
break; |
break; |
414 |
|
|
415 |
case NOPROVIDER: |
case NOPROVIDER: |
416 |
dismissDialogSafe(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
417 |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); |
418 |
//StationList.this.finish(); |
//StationList.this.finish(); |
419 |
break; |
break; |
423 |
if (locationLookup.hasLocation()) { |
if (locationLookup.hasLocation()) { |
424 |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
425 |
} else { |
} else { |
426 |
dismissDialogSafe(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
427 |
|
|
428 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
429 |
builder.setMessage( getString( stationlist_gpstimeout) ); |
builder.setMessage( getString( stationlist_gpstimeout) ); |
440 |
dialog.dismiss(); |
dialog.dismiss(); |
441 |
} |
} |
442 |
}); |
}); |
443 |
builderShowSafe(builder); // builder.show() |
builder.show(); |
444 |
|
|
445 |
} |
} |
446 |
} |
} |
454 |
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
455 |
|
|
456 |
LookupMethod method = LookupMethod.MethodNone; |
LookupMethod method = LookupMethod.MethodNone; |
|
boolean success; |
|
457 |
String name; |
String name; |
458 |
Location loc; |
Location loc; |
459 |
String ids; |
String ids; |
488 |
|
|
489 |
switch (method) { |
switch (method) { |
490 |
case ByLocation: |
case ByLocation: |
491 |
success = stationProvider.lookupStations(loc); |
stations = stationProvider.lookupStations(loc); |
492 |
break; |
break; |
493 |
case ByName: |
case ByName: |
494 |
success = stationProvider.lookupStationsByName(name); |
stations = stationProvider.lookupStationsByName(name); |
495 |
break; |
break; |
496 |
case ByList: |
case ByList: |
497 |
success = stationProvider.lookupStationsByIds(ids); |
stations = stationProvider.lookupStationsByIds(ids); |
498 |
break; |
break; |
499 |
default: |
default: |
500 |
success = false; // not possible |
stations = null; // not possible |
501 |
} |
} |
502 |
|
|
503 |
|
|
507 |
@Override |
@Override |
508 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
509 |
super.onPostExecute(result); |
super.onPostExecute(result); |
510 |
dismissDialogSafe(dialog); |
dialog.dismiss(); |
511 |
|
|
512 |
|
|
513 |
if (success) { |
if (stations != null) { |
514 |
if (stationProvider.getStations().size() == 0) { |
if (stations.size() == 0) { |
515 |
showMessageAndClose(getString(stationlist_nostations)); |
showMessageAndClose(getString(stationlist_nostations)); |
516 |
} |
} |
|
stations = stationProvider.getStations(); |
|
517 |
|
|
518 |
StationList.this.getListView().invalidateViews(); |
StationList.this.getListView().invalidateViews(); |
519 |
adapter.setStations( stations ); |
adapter.setStations( stations ); |
565 |
} |
} |
566 |
}); |
}); |
567 |
|
|
568 |
builderShowSafe(builder); // builder.show() |
builder.show(); |
569 |
} |
} |
570 |
} |
} |
571 |
} |
} |