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, |
115 |
|
|
116 |
isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false); |
isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false); |
117 |
|
|
118 |
|
ProviderFactory.purgeOldEntries(); //cleanup before fetching more data |
119 |
|
|
120 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
121 |
|
|
122 |
|
|
124 |
case ListNearest: |
case ListNearest: |
125 |
startLookup(); |
startLookup(); |
126 |
break; |
break; |
127 |
case ListSearch: |
case ListSearch: |
128 |
this.showDialogSafe(DLG_STATIONNAME); |
showDialog(DLG_STATIONNAME); |
129 |
break; |
break; |
130 |
case ListFavorites: |
case ListFavorites: |
131 |
startFavoriteLookup(); |
startFavoriteLookup(); |
141 |
} |
} |
142 |
|
|
143 |
} |
} |
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
@Override |
151 |
|
protected void onDestroy() { |
152 |
|
super.onDestroy(); |
153 |
|
|
154 |
|
isRunning = false; |
155 |
|
|
156 |
|
if (locationLookup != null) { |
157 |
|
locationLookup.stopSearch(); |
158 |
|
} |
159 |
|
if (findStationsTask != null) { |
160 |
|
findStationsTask.cancel(true); |
161 |
|
} |
162 |
|
} |
163 |
|
|
164 |
|
|
165 |
protected void setTitle() { |
protected void setTitle() { |
166 |
String dialogTitle = getResources().getString(app_name); |
String dialogTitle = getResources().getString(app_name); |
167 |
switch (listType) { |
switch (listType) { |
182 |
|
|
183 |
} |
} |
184 |
|
|
|
|
|
|
/* 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); |
|
|
} |
|
185 |
|
|
|
} |
|
|
/* EOF rude and ugly dialog hack */ |
|
|
|
|
|
|
|
186 |
|
|
187 |
@Override |
@Override |
188 |
public void onSaveInstanceState(Bundle outState) |
public void onSaveInstanceState(Bundle outState) |
241 |
message.append( getString(stationlist_nolocation) ); |
message.append( getString(stationlist_nolocation) ); |
242 |
} |
} |
243 |
|
|
244 |
MessageBox.showMessage(this, message.toString()); |
MessageBox.showMessage(this, message.toString(), false); |
245 |
break; |
break; |
246 |
default: |
default: |
247 |
retval = super.onOptionsItemSelected(item); |
retval = super.onOptionsItemSelected(item); |
359 |
public void startLookup() { |
public void startLookup() { |
360 |
isRunning = true; |
isRunning = true; |
361 |
dialogMessage = getString( stationlist_waitforlocation ); |
dialogMessage = getString( stationlist_waitforlocation ); |
362 |
showDialogSafe(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
363 |
|
|
364 |
locationLookup.locateStations(); |
locationLookup.locateStations(); |
365 |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS); |
366 |
} |
} |
367 |
|
|
368 |
void startNameSearch(String name) { |
void startNameSearch(String name) { |
369 |
dialogMessage = getString( stationlist_findbyname ); |
dialogMessage = getString( stationlist_findbyname ); |
370 |
showDialogSafe(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
371 |
|
|
372 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
373 |
findStationsTask.searchByName(name); |
findStationsTask.searchByName(name); |
379 |
|
|
380 |
if (favorites.size() > 0) { |
if (favorites.size() > 0) { |
381 |
dialogMessage = getString( stationlist_loadfavorites ); |
dialogMessage = getString( stationlist_loadfavorites ); |
382 |
showDialogSafe(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
383 |
|
|
384 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
385 |
findStationsTask.searchByIds( favorites.toString() ); |
findStationsTask.searchByIds( favorites.toString() ); |
394 |
void startLocatorTask() |
void startLocatorTask() |
395 |
{ |
{ |
396 |
dialogMessage = getString( stationlist_findingnearby ); |
dialogMessage = getString( stationlist_findingnearby ); |
397 |
showDialogSafe(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
398 |
|
|
399 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
400 |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
411 |
|
|
412 |
switch (msg.what) { |
switch (msg.what) { |
413 |
case GOTLOCATION: |
case GOTLOCATION: |
414 |
dismissDialogSafe(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
415 |
|
|
416 |
startLocatorTask(); |
startLocatorTask(); |
417 |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
419 |
break; |
break; |
420 |
|
|
421 |
case NOPROVIDER: |
case NOPROVIDER: |
422 |
dismissDialogSafe(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
423 |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) ); |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); |
424 |
|
//StationList.this.finish(); |
425 |
break; |
break; |
426 |
case LOCATIONFIXTIMEOUT: |
case LOCATIONFIXTIMEOUT: |
427 |
if (isRunning) { |
if (isRunning) { |
429 |
if (locationLookup.hasLocation()) { |
if (locationLookup.hasLocation()) { |
430 |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
431 |
} else { |
} else { |
432 |
dismissDialogSafe(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
433 |
|
|
434 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
435 |
builder.setMessage( getString( stationlist_gpstimeout) ); |
builder.setMessage( getString( stationlist_gpstimeout) ); |
446 |
dialog.dismiss(); |
dialog.dismiss(); |
447 |
} |
} |
448 |
}); |
}); |
449 |
builderShowSafe(builder); // builder.show() |
builder.show(); |
450 |
|
|
451 |
} |
} |
452 |
} |
} |
460 |
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
461 |
|
|
462 |
LookupMethod method = LookupMethod.MethodNone; |
LookupMethod method = LookupMethod.MethodNone; |
|
boolean success; |
|
463 |
String name; |
String name; |
464 |
Location loc; |
Location loc; |
465 |
String ids; |
String ids; |
494 |
|
|
495 |
switch (method) { |
switch (method) { |
496 |
case ByLocation: |
case ByLocation: |
497 |
success = stationProvider.lookupStations(loc); |
stations = stationProvider.lookupStations(loc); |
498 |
break; |
break; |
499 |
case ByName: |
case ByName: |
500 |
success = stationProvider.lookupStationsByName(name); |
stations = stationProvider.lookupStationsByName(name); |
501 |
break; |
break; |
502 |
case ByList: |
case ByList: |
503 |
success = stationProvider.lookupStationsByIds(ids); |
stations = stationProvider.lookupStationsByIds(ids); |
504 |
break; |
break; |
505 |
default: |
default: |
506 |
success = false; // not possible |
stations = null; // not possible |
507 |
} |
} |
508 |
|
|
509 |
|
|
513 |
@Override |
@Override |
514 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
515 |
super.onPostExecute(result); |
super.onPostExecute(result); |
516 |
dismissDialogSafe(dialog); |
dialog.dismiss(); |
517 |
|
|
518 |
|
|
519 |
if (success) { |
if (stations != null) { |
520 |
if (stationProvider.getStations().size() == 0) { |
if (stations.size() == 0) { |
521 |
showMessageAndClose(getString(stationlist_nostations)); |
showMessageAndClose(getString(stationlist_nostations)); |
522 |
} |
} |
523 |
stations = stationProvider.getStations(); |
|
524 |
|
StationList.this.getListView().invalidateViews(); |
525 |
adapter.setStations( stations ); |
adapter.setStations( stations ); |
526 |
|
|
527 |
|
|
528 |
} else { //communication or parse errors |
} else { //communication or parse errors |
529 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
530 |
builder.setMessage(getString(stationlist_fetcherror)); |
builder.setMessage(getString(stationlist_fetcherror)); |
571 |
} |
} |
572 |
}); |
}); |
573 |
|
|
574 |
builderShowSafe(builder); // builder.show() |
builder.show(); |
575 |
} |
} |
576 |
} |
} |
577 |
} |
} |