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 = 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.showDialog(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) { |
173 |
} |
} |
174 |
|
|
175 |
setTitle(dialogTitle); |
setTitle(dialogTitle); |
176 |
|
|
177 |
} |
} |
178 |
|
|
179 |
|
|
|
|
|
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); |
292 |
public void onClick(DialogInterface dialog, int which) { |
public void onClick(DialogInterface dialog, int which) { |
293 |
EditText et = (EditText) rootView.findViewById(R.id.EditText); |
EditText et = (EditText) rootView.findViewById(R.id.EditText); |
294 |
dialog.dismiss(); |
dialog.dismiss(); |
295 |
if (et.getText().toString().length() >= 2) { |
String search = et.getText().toString().trim(); |
296 |
startNameSearch(et.getText().toString()); |
if (search.length() >= 2) { |
297 |
|
startNameSearch(search); |
298 |
} else { |
} else { |
299 |
showMessageAndClose( getString(stationlist_twocharmin) ); |
showMessageAndClose( getString(stationlist_twocharmin) ); |
300 |
} |
} |
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 |
///////////////////////////////////////////////////////////// |
///////////////////////////////////////////////////////////// |
356 |
showDialog(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) { |
364 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
365 |
|
|
366 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
367 |
findStationsTask.searchByName(name, locationLookup.getLocation()); |
findStationsTask.searchByName(name); |
368 |
findStationsTask.execute(); |
findStationsTask.execute(); |
369 |
|
|
370 |
} |
} |
376 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
377 |
|
|
378 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
379 |
findStationsTask.searchByIds(favorites.toString(), locationLookup.getLocation()); |
findStationsTask.searchByIds( favorites.toString() ); |
380 |
findStationsTask.execute(); |
findStationsTask.execute(); |
381 |
} else { |
} else { |
382 |
showMessageAndClose( getString( stationlist_nofavorites ) ); |
showMessageAndClose( getString( stationlist_nofavorites ) ); |
394 |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
395 |
findStationsTask.execute(); |
findStationsTask.execute(); |
396 |
} |
} |
|
|
|
|
|
|
|
/* TODO: Remove this no longer needed function |
|
|
String lookupAddress(double latitude, double longitude) { |
|
397 |
|
|
|
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(); |
|
|
}*/ |
|
|
|
|
398 |
|
|
399 |
//////////////////////////////////////////////////////////////////////////// |
//////////////////////////////////////////////////////////////////////////// |
400 |
// Inner classes |
// Inner classes |
414 |
|
|
415 |
case NOPROVIDER: |
case NOPROVIDER: |
416 |
dismissDialog(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) { |
439 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
440 |
dialog.dismiss(); |
dialog.dismiss(); |
441 |
} |
} |
442 |
}); |
}); |
443 |
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; |
460 |
|
|
461 |
public void searchByName(String n, Location l) { |
public void searchByName(String n) { |
462 |
|
|
463 |
method = LookupMethod.ByName; |
method = LookupMethod.ByName; |
|
loc = l; |
|
464 |
name = n; |
name = n; |
465 |
} |
} |
466 |
|
|
469 |
loc = l; |
loc = l; |
470 |
} |
} |
471 |
|
|
472 |
public void searchByIds(String id, Location l) { |
public void searchByIds(String id) { |
473 |
|
|
474 |
method = LookupMethod.ByList; |
method = LookupMethod.ByList; |
|
loc = l; |
|
475 |
ids = id; |
ids = id; |
476 |
} |
} |
477 |
|
|
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 |
|
|
|
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); |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
504 |
return null; |
return null; |
505 |
} |
} |
506 |
|
|
510 |
dialog.dismiss(); |
dialog.dismiss(); |
511 |
|
|
512 |
|
|
513 |
if (success) { |
if (stations != null) { |
514 |
if (stationProvider.getStations().size() == 0) |
if (stations.size() == 0) { |
515 |
MessageBox.showMessage(StationList.this, getString(stationlist_nostations) ); // this should not be possible !?! |
showMessageAndClose(getString(stationlist_nostations)); |
516 |
stations = stationProvider.getStations(); |
} |
517 |
|
|
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) { |
528 |
dialog.dismiss(); |
dialog.dismiss(); |
529 |
|
|
530 |
stationsFetched.post( new Runnable() { |
Runnable runner = null; |
531 |
@Override |
switch (method) { |
532 |
public void run() { |
case ByLocation: |
533 |
startLocatorTask(); |
runner = new Runnable() { |
534 |
} |
@Override |
535 |
}); |
public void run() { |
536 |
|
startLocatorTask(); |
537 |
|
} |
538 |
|
}; |
539 |
|
break; |
540 |
|
case ByName: |
541 |
|
runner = new Runnable() { |
542 |
|
@Override |
543 |
|
public void run() { |
544 |
|
startNameSearch( FindStationsTask.this.name ); |
545 |
|
} |
546 |
|
}; |
547 |
|
break; |
548 |
|
case ByList: |
549 |
|
runner = new Runnable() { |
550 |
|
@Override |
551 |
|
public void run() { |
552 |
|
startFavoriteLookup(); |
553 |
|
} |
554 |
|
}; |
555 |
|
break; |
556 |
|
} |
557 |
|
|
558 |
|
stationsFetched.post( runner ); |
559 |
} |
} |
560 |
}); |
}); |
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 |
builder.show(); |
|
568 |
|
builder.show(); |
569 |
} |
} |
570 |
} |
} |
571 |
} |
} |