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; |
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 = 17500; //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.showDialog(DLG_STATIONNAME); |
showDialog(DLG_STATIONNAME); //TODO: this.showDialogSafe(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 |
|
if (findStationsTask != null) { |
149 |
|
findStationsTask.cancel(true); |
150 |
|
} |
151 |
|
if (locationLookup != null) { |
152 |
|
locationLookup.stopSearch(); |
153 |
|
} |
154 |
|
isRunning = false; |
155 |
|
} |
156 |
|
|
157 |
|
|
158 |
protected void setTitle() { |
protected void setTitle() { |
159 |
String dialogTitle = getResources().getString(app_name); |
String dialogTitle = getResources().getString(app_name); |
160 |
switch (listType) { |
switch (listType) { |
172 |
} |
} |
173 |
|
|
174 |
setTitle(dialogTitle); |
setTitle(dialogTitle); |
175 |
|
|
176 |
} |
} |
177 |
|
|
178 |
|
|
179 |
|
/* these 3 dialogs helper functions are very rude and ugly hack |
180 |
|
* to remove these auto-reported exceptions |
181 |
|
* - android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@436aaef8 is not valid; is your activity running? |
182 |
|
* - java.lang.IllegalArgumentException: View not attached to window manager |
183 |
|
*/ |
184 |
|
|
185 |
|
/* |
186 |
|
public void showDialogSafe(int id) { |
187 |
|
try { |
188 |
|
showDialog(id); |
189 |
|
} catch (Exception e) { |
190 |
|
Log.e("StationList", "showDialog failed", e); |
191 |
|
} |
192 |
|
} |
193 |
|
|
194 |
|
public void dismissDialogSafe(int id) { |
195 |
|
try { |
196 |
|
dismissDialog(id); |
197 |
|
} catch (Exception e) { |
198 |
|
Log.e("StationList", "dismissDialog failed", e); |
199 |
|
} |
200 |
|
} |
201 |
|
public void dismissDialogSafe(Dialog dlg) { |
202 |
|
try { |
203 |
|
dlg.dismiss(); |
204 |
|
} catch (Exception e) { |
205 |
|
Log.e("StationList", "dismissDialog failed", e); |
206 |
|
} |
207 |
|
} |
208 |
|
|
209 |
|
public void builderShowSafe(AlertDialog.Builder builder) { |
210 |
|
try { |
211 |
|
builder.show(); |
212 |
|
} catch (Exception e) { |
213 |
|
Log.e("StationList", "builder.show() failed", e); |
214 |
|
} |
215 |
|
|
216 |
|
}*/ |
217 |
|
|
218 |
|
/* EOF rude and ugly dialog hack */ |
219 |
|
|
220 |
|
|
221 |
|
|
222 |
@Override |
@Override |
276 |
message.append( getString(stationlist_nolocation) ); |
message.append( getString(stationlist_nolocation) ); |
277 |
} |
} |
278 |
|
|
279 |
MessageBox.showMessage(this, message.toString()); |
MessageBox.showMessage(this, message.toString(), false); |
280 |
break; |
break; |
281 |
default: |
default: |
282 |
retval = super.onOptionsItemSelected(item); |
retval = super.onOptionsItemSelected(item); |
333 |
public void onClick(DialogInterface dialog, int which) { |
public void onClick(DialogInterface dialog, int which) { |
334 |
EditText et = (EditText) rootView.findViewById(R.id.EditText); |
EditText et = (EditText) rootView.findViewById(R.id.EditText); |
335 |
dialog.dismiss(); |
dialog.dismiss(); |
336 |
if (et.getText().toString().length() >= 2) { |
String search = et.getText().toString().trim(); |
337 |
startNameSearch(et.getText().toString()); |
if (search.length() >= 2) { |
338 |
|
startNameSearch(search); |
339 |
} else { |
} else { |
340 |
showMessageAndClose( getString(stationlist_twocharmin) ); |
showMessageAndClose( getString(stationlist_twocharmin) ); |
341 |
} |
} |
376 |
|
|
377 |
StationBean station = stations.get(position); |
StationBean station = stations.get(position); |
378 |
|
|
379 |
Intent intent = new Intent(this, DepartureList.class); |
if (isLaunchedforShortcut == true) { |
380 |
intent.putExtra("stationbean", station); |
Intent i = new Intent(); |
381 |
startActivity(intent); |
i.putExtra("station", station); |
382 |
|
setResult(Activity.RESULT_OK, i); |
383 |
|
finish(); |
384 |
|
} else { |
385 |
|
Intent intent = new Intent(this, DepartureList.class); |
386 |
|
intent.putExtra("stationbean", station); |
387 |
|
startActivity(intent); |
388 |
|
} |
389 |
} |
} |
390 |
|
|
391 |
///////////////////////////////////////////////////////////// |
///////////////////////////////////////////////////////////// |
394 |
public void startLookup() { |
public void startLookup() { |
395 |
isRunning = true; |
isRunning = true; |
396 |
dialogMessage = getString( stationlist_waitforlocation ); |
dialogMessage = getString( stationlist_waitforlocation ); |
397 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS);//TODO:showDialogSafe(DLG_PROGRESS); |
398 |
|
|
399 |
locationLookup.locateStations(); |
locationLookup.locateStations(); |
400 |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS); |
401 |
} |
} |
402 |
|
|
403 |
void startNameSearch(String name) { |
void startNameSearch(String name) { |
404 |
dialogMessage = getString( stationlist_findbyname ); |
dialogMessage = getString( stationlist_findbyname ); |
405 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS);//TODO:showDialogSafe(DLG_PROGRESS); |
406 |
|
|
407 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
408 |
findStationsTask.searchByName(name, locationLookup.getLocation()); |
findStationsTask.searchByName(name); |
409 |
findStationsTask.execute(); |
findStationsTask.execute(); |
410 |
|
|
411 |
} |
} |
414 |
|
|
415 |
if (favorites.size() > 0) { |
if (favorites.size() > 0) { |
416 |
dialogMessage = getString( stationlist_loadfavorites ); |
dialogMessage = getString( stationlist_loadfavorites ); |
417 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS);//TODO:showDialogSafe(DLG_PROGRESS); |
418 |
|
|
419 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
420 |
findStationsTask.searchByIds(favorites.toString(), locationLookup.getLocation()); |
findStationsTask.searchByIds( favorites.toString() ); |
421 |
findStationsTask.execute(); |
findStationsTask.execute(); |
422 |
} else { |
} else { |
423 |
showMessageAndClose( getString( stationlist_nofavorites ) ); |
showMessageAndClose( getString( stationlist_nofavorites ) ); |
429 |
void startLocatorTask() |
void startLocatorTask() |
430 |
{ |
{ |
431 |
dialogMessage = getString( stationlist_findingnearby ); |
dialogMessage = getString( stationlist_findingnearby ); |
432 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS);//TODO:showDialogSafe(DLG_PROGRESS); |
433 |
|
|
434 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
435 |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
436 |
findStationsTask.execute(); |
findStationsTask.execute(); |
437 |
} |
} |
|
|
|
|
|
|
|
/* TODO: Remove this no longer needed function |
|
|
String lookupAddress(double latitude, double longitude) { |
|
438 |
|
|
|
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); |
|
|
} |
|
|
|
|
|
return sb.toString(); |
|
|
}*/ |
|
|
|
|
439 |
|
|
440 |
//////////////////////////////////////////////////////////////////////////// |
//////////////////////////////////////////////////////////////////////////// |
441 |
// Inner classes |
// Inner classes |
446 |
|
|
447 |
switch (msg.what) { |
switch (msg.what) { |
448 |
case GOTLOCATION: |
case GOTLOCATION: |
449 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS);//TODO:dismissDialogSafe(DLG_PROGRESS); |
450 |
|
|
451 |
startLocatorTask(); |
startLocatorTask(); |
452 |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
454 |
break; |
break; |
455 |
|
|
456 |
case NOPROVIDER: |
case NOPROVIDER: |
457 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS);//TODO:dismissDialogSafe(DLG_PROGRESS); |
458 |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) ); |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); |
459 |
|
//StationList.this.finish(); |
460 |
break; |
break; |
461 |
case LOCATIONFIXTIMEOUT: |
case LOCATIONFIXTIMEOUT: |
462 |
if (isRunning) { |
if (isRunning) { |
464 |
if (locationLookup.hasLocation()) { |
if (locationLookup.hasLocation()) { |
465 |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
466 |
} else { |
} else { |
467 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS);//TODO:dismissDialogSafe(DLG_PROGRESS); |
468 |
|
|
469 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
470 |
builder.setMessage( getString( stationlist_gpstimeout) ); |
builder.setMessage( getString( stationlist_gpstimeout) ); |
480 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
481 |
dialog.dismiss(); |
dialog.dismiss(); |
482 |
} |
} |
483 |
}); |
}); |
484 |
builder.show(); |
builder.show();//TODO:builderShowSafe(builder); |
485 |
|
|
486 |
} |
} |
487 |
} |
} |
500 |
Location loc; |
Location loc; |
501 |
String ids; |
String ids; |
502 |
|
|
503 |
public void searchByName(String n, Location l) { |
public void searchByName(String n) { |
504 |
|
|
505 |
method = LookupMethod.ByName; |
method = LookupMethod.ByName; |
|
loc = l; |
|
506 |
name = n; |
name = n; |
507 |
} |
} |
508 |
|
|
511 |
loc = l; |
loc = l; |
512 |
} |
} |
513 |
|
|
514 |
public void searchByIds(String id, Location l) { |
public void searchByIds(String id) { |
515 |
|
|
516 |
method = LookupMethod.ByList; |
method = LookupMethod.ByList; |
|
loc = l; |
|
517 |
ids = id; |
ids = id; |
518 |
} |
} |
519 |
|
|
543 |
} |
} |
544 |
|
|
545 |
|
|
|
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); |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
546 |
return null; |
return null; |
547 |
} |
} |
548 |
|
|
549 |
@Override |
@Override |
550 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
551 |
super.onPostExecute(result); |
super.onPostExecute(result); |
552 |
dialog.dismiss(); |
dialog.dismiss();//TODO:dismissDialogSafe(dialog); |
553 |
|
|
554 |
|
|
555 |
if (success) { |
if (success) { |
556 |
if (stationProvider.getStations().size() == 0) |
if (stationProvider.getStations().size() == 0) { |
557 |
MessageBox.showMessage(StationList.this, getString(stationlist_nostations) ); // this should not be possible !?! |
showMessageAndClose(getString(stationlist_nostations)); |
558 |
|
} |
559 |
stations = stationProvider.getStations(); |
stations = stationProvider.getStations(); |
560 |
|
|
561 |
|
StationList.this.getListView().invalidateViews(); |
562 |
adapter.setStations( stations ); |
adapter.setStations( stations ); |
563 |
|
|
564 |
|
|
565 |
} else { //communication or parse errors |
} else { //communication or parse errors |
566 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
567 |
builder.setMessage(getString(stationlist_nearbyerror)); |
builder.setMessage(getString(stationlist_fetcherror)); |
568 |
builder.setCancelable(true); |
builder.setCancelable(true); |
569 |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
570 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
571 |
dialog.dismiss(); |
dialog.dismiss(); |
572 |
|
|
573 |
stationsFetched.post( new Runnable() { |
Runnable runner = null; |
574 |
@Override |
switch (method) { |
575 |
public void run() { |
case ByLocation: |
576 |
startLocatorTask(); |
runner = new Runnable() { |
577 |
} |
@Override |
578 |
}); |
public void run() { |
579 |
|
startLocatorTask(); |
580 |
|
} |
581 |
|
}; |
582 |
|
break; |
583 |
|
case ByName: |
584 |
|
runner = new Runnable() { |
585 |
|
@Override |
586 |
|
public void run() { |
587 |
|
startNameSearch( FindStationsTask.this.name ); |
588 |
|
} |
589 |
|
}; |
590 |
|
break; |
591 |
|
case ByList: |
592 |
|
runner = new Runnable() { |
593 |
|
@Override |
594 |
|
public void run() { |
595 |
|
startFavoriteLookup(); |
596 |
|
} |
597 |
|
}; |
598 |
|
break; |
599 |
|
} |
600 |
|
|
601 |
|
stationsFetched.post( runner ); |
602 |
} |
} |
603 |
}); |
}); |
604 |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
605 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
606 |
dialog.dismiss(); |
dialog.dismiss(); |
607 |
|
StationList.this.finish(); |
608 |
} |
} |
609 |
}); |
}); |
610 |
builder.show(); |
|
611 |
|
builder.show();//TODO:builderShowSafe(builder); |
612 |
} |
} |
613 |
} |
} |
614 |
} |
} |