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 |
|
|
145 |
|
|
146 |
|
@Override |
147 |
|
protected void onStart() { |
148 |
|
super.onStart(); |
149 |
|
ProviderFactory.purgeOldEntries(); |
150 |
|
} |
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
@Override |
156 |
|
protected void onDestroy() { |
157 |
|
super.onDestroy(); |
158 |
|
|
159 |
|
isRunning = false; |
160 |
|
|
161 |
|
if (locationLookup != null) { |
162 |
|
locationLookup.stopSearch(); |
163 |
|
} |
164 |
|
if (findStationsTask != null) { |
165 |
|
findStationsTask.cancel(true); |
166 |
|
} |
167 |
|
} |
168 |
|
|
169 |
|
|
170 |
protected void setTitle() { |
protected void setTitle() { |
171 |
String dialogTitle = getResources().getString(app_name); |
String dialogTitle = getResources().getString(app_name); |
172 |
switch (listType) { |
switch (listType) { |
187 |
|
|
188 |
} |
} |
189 |
|
|
|
|
|
|
/* 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); |
|
|
} |
|
190 |
|
|
|
} |
|
|
/* EOF rude and ugly dialog hack */ |
|
|
|
|
|
|
|
191 |
|
|
192 |
@Override |
@Override |
193 |
public void onSaveInstanceState(Bundle outState) |
public void onSaveInstanceState(Bundle outState) |
246 |
message.append( getString(stationlist_nolocation) ); |
message.append( getString(stationlist_nolocation) ); |
247 |
} |
} |
248 |
|
|
249 |
MessageBox.showMessage(this, message.toString()); |
MessageBox.showMessage(this, message.toString(), false); |
250 |
break; |
break; |
251 |
default: |
default: |
252 |
retval = super.onOptionsItemSelected(item); |
retval = super.onOptionsItemSelected(item); |
346 |
|
|
347 |
StationBean station = stations.get(position); |
StationBean station = stations.get(position); |
348 |
|
|
349 |
Intent intent = new Intent(this, DepartureList.class); |
if (isLaunchedforShortcut == true) { |
350 |
intent.putExtra("stationbean", station); |
Intent i = new Intent(); |
351 |
startActivity(intent); |
i.putExtra("station", station); |
352 |
|
setResult(Activity.RESULT_OK, i); |
353 |
|
finish(); |
354 |
|
} else { |
355 |
|
Intent intent = new Intent(this, DepartureList.class); |
356 |
|
intent.putExtra("stationbean", station); |
357 |
|
startActivity(intent); |
358 |
|
} |
359 |
} |
} |
360 |
|
|
361 |
///////////////////////////////////////////////////////////// |
///////////////////////////////////////////////////////////// |
364 |
public void startLookup() { |
public void startLookup() { |
365 |
isRunning = true; |
isRunning = true; |
366 |
dialogMessage = getString( stationlist_waitforlocation ); |
dialogMessage = getString( stationlist_waitforlocation ); |
367 |
showDialogSafe(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
368 |
|
|
369 |
locationLookup.locateStations(); |
locationLookup.locateStations(); |
370 |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS); |
371 |
} |
} |
372 |
|
|
373 |
void startNameSearch(String name) { |
void startNameSearch(String name) { |
374 |
dialogMessage = getString( stationlist_findbyname ); |
dialogMessage = getString( stationlist_findbyname ); |
375 |
showDialogSafe(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
376 |
|
|
377 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
378 |
findStationsTask.searchByName(name); |
findStationsTask.searchByName(name); |
384 |
|
|
385 |
if (favorites.size() > 0) { |
if (favorites.size() > 0) { |
386 |
dialogMessage = getString( stationlist_loadfavorites ); |
dialogMessage = getString( stationlist_loadfavorites ); |
387 |
showDialogSafe(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
388 |
|
|
389 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
390 |
findStationsTask.searchByIds( favorites.toString() ); |
findStationsTask.searchByIds( favorites.toString() ); |
399 |
void startLocatorTask() |
void startLocatorTask() |
400 |
{ |
{ |
401 |
dialogMessage = getString( stationlist_findingnearby ); |
dialogMessage = getString( stationlist_findingnearby ); |
402 |
showDialogSafe(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
403 |
|
|
404 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
405 |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
416 |
|
|
417 |
switch (msg.what) { |
switch (msg.what) { |
418 |
case GOTLOCATION: |
case GOTLOCATION: |
419 |
dismissDialogSafe(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
420 |
|
|
421 |
startLocatorTask(); |
startLocatorTask(); |
422 |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
424 |
break; |
break; |
425 |
|
|
426 |
case NOPROVIDER: |
case NOPROVIDER: |
427 |
dismissDialogSafe(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
428 |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) ); |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); |
429 |
|
//StationList.this.finish(); |
430 |
break; |
break; |
431 |
case LOCATIONFIXTIMEOUT: |
case LOCATIONFIXTIMEOUT: |
432 |
if (isRunning) { |
if (isRunning) { |
434 |
if (locationLookup.hasLocation()) { |
if (locationLookup.hasLocation()) { |
435 |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
436 |
} else { |
} else { |
437 |
dismissDialogSafe(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
438 |
|
|
439 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
440 |
builder.setMessage( getString( stationlist_gpstimeout) ); |
builder.setMessage( getString( stationlist_gpstimeout) ); |
451 |
dialog.dismiss(); |
dialog.dismiss(); |
452 |
} |
} |
453 |
}); |
}); |
454 |
builderShowSafe(builder); // builder.show() |
builder.show(); |
455 |
|
|
456 |
} |
} |
457 |
} |
} |
465 |
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
466 |
|
|
467 |
LookupMethod method = LookupMethod.MethodNone; |
LookupMethod method = LookupMethod.MethodNone; |
|
boolean success; |
|
468 |
String name; |
String name; |
469 |
Location loc; |
Location loc; |
470 |
String ids; |
String ids; |
499 |
|
|
500 |
switch (method) { |
switch (method) { |
501 |
case ByLocation: |
case ByLocation: |
502 |
success = stationProvider.lookupStations(loc); |
stations = stationProvider.lookupStations(loc); |
503 |
break; |
break; |
504 |
case ByName: |
case ByName: |
505 |
success = stationProvider.lookupStationsByName(name); |
stations = stationProvider.lookupStationsByName(name); |
506 |
break; |
break; |
507 |
case ByList: |
case ByList: |
508 |
success = stationProvider.lookupStationsByIds(ids); |
stations = stationProvider.lookupStationsByIds(ids); |
509 |
break; |
break; |
510 |
default: |
default: |
511 |
success = false; // not possible |
stations = null; // not possible |
512 |
} |
} |
513 |
|
|
514 |
|
|
521 |
dialog.dismiss(); |
dialog.dismiss(); |
522 |
|
|
523 |
|
|
524 |
if (success) { |
if (stations != null) { |
525 |
if (stationProvider.getStations().size() == 0) { |
if (stations.size() == 0) { |
526 |
showMessageAndClose(getString(stationlist_nostations)); |
showMessageAndClose(getString(stationlist_nostations)); |
527 |
} |
} |
528 |
stations = stationProvider.getStations(); |
|
529 |
|
StationList.this.getListView().invalidateViews(); |
530 |
adapter.setStations( stations ); |
adapter.setStations( stations ); |
531 |
|
|
532 |
|
|
533 |
} else { //communication or parse errors |
} else { //communication or parse errors |
534 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
535 |
builder.setMessage(getString(stationlist_nearbyerror)); |
builder.setMessage(getString(stationlist_fetcherror)); |
536 |
builder.setCancelable(true); |
builder.setCancelable(true); |
537 |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
538 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
572 |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
573 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
574 |
dialog.dismiss(); |
dialog.dismiss(); |
575 |
|
StationList.this.finish(); |
576 |
} |
} |
577 |
}); |
}); |
578 |
|
|
579 |
builderShowSafe(builder); // builder.show() |
builder.show(); |
580 |
} |
} |
581 |
} |
} |
582 |
} |
} |