4 |
import java.util.List; |
import java.util.List; |
5 |
|
|
6 |
|
|
7 |
|
import android.app.Activity; |
8 |
import android.app.AlertDialog; |
import android.app.AlertDialog; |
9 |
import android.app.Dialog; |
import android.app.Dialog; |
10 |
import android.app.ListActivity; |
import android.app.ListActivity; |
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, |
70 |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
71 |
|
|
72 |
GeoPair location = new GeoPair(); |
GeoPair location = new GeoPair(); |
73 |
|
|
74 |
|
boolean isLaunchedforShortcut; |
75 |
boolean isRunning = false; |
boolean isRunning = false; |
76 |
List<StationBean> stations = new ArrayList<StationBean>(); |
List<StationBean> stations = new ArrayList<StationBean>(); |
77 |
|
|
113 |
listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); |
listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); |
114 |
setTitle(); |
setTitle(); |
115 |
|
|
116 |
|
isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false); |
117 |
|
|
118 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
119 |
|
|
120 |
|
|
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) |
235 |
message.append( getString(stationlist_nolocation) ); |
message.append( getString(stationlist_nolocation) ); |
236 |
} |
} |
237 |
|
|
238 |
MessageBox.showMessage(this, message.toString()); |
MessageBox.showMessage(this, message.toString(), false); |
239 |
break; |
break; |
240 |
default: |
default: |
241 |
retval = super.onOptionsItemSelected(item); |
retval = super.onOptionsItemSelected(item); |
335 |
|
|
336 |
StationBean station = stations.get(position); |
StationBean station = stations.get(position); |
337 |
|
|
338 |
Intent intent = new Intent(this, DepartureList.class); |
if (isLaunchedforShortcut == true) { |
339 |
intent.putExtra("stationbean", station); |
Intent i = new Intent(); |
340 |
startActivity(intent); |
i.putExtra("station", station); |
341 |
|
setResult(Activity.RESULT_OK, i); |
342 |
|
finish(); |
343 |
|
} else { |
344 |
|
Intent intent = new Intent(this, DepartureList.class); |
345 |
|
intent.putExtra("stationbean", station); |
346 |
|
startActivity(intent); |
347 |
|
} |
348 |
} |
} |
349 |
|
|
350 |
///////////////////////////////////////////////////////////// |
///////////////////////////////////////////////////////////// |
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) ); |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); |
418 |
|
//StationList.this.finish(); |
419 |
break; |
break; |
420 |
case LOCATIONFIXTIMEOUT: |
case LOCATIONFIXTIMEOUT: |
421 |
if (isRunning) { |
if (isRunning) { |
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 |
} |
} |
517 |
stations = stationProvider.getStations(); |
|
518 |
|
StationList.this.getListView().invalidateViews(); |
519 |
adapter.setStations( stations ); |
adapter.setStations( stations ); |
520 |
|
|
521 |
|
|
522 |
} else { //communication or parse errors |
} else { //communication or parse errors |
523 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
524 |
builder.setMessage(getString(stationlist_nearbyerror)); |
builder.setMessage(getString(stationlist_fetcherror)); |
525 |
builder.setCancelable(true); |
builder.setCancelable(true); |
526 |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
527 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
561 |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
562 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
563 |
dialog.dismiss(); |
dialog.dismiss(); |
564 |
|
StationList.this.finish(); |
565 |
} |
} |
566 |
}); |
}); |
567 |
|
|
568 |
builderShowSafe(builder); // builder.show() |
builder.show(); |
569 |
} |
} |
570 |
} |
} |
571 |
} |
} |