2 |
|
|
3 |
import java.util.ArrayList; |
import java.util.ArrayList; |
4 |
import java.util.List; |
import java.util.List; |
|
import java.util.Locale; |
|
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; |
13 |
import android.content.Intent; |
import android.content.Intent; |
14 |
import android.content.SharedPreferences; |
import android.content.SharedPreferences; |
15 |
import android.content.SharedPreferences.Editor; |
import android.content.SharedPreferences.Editor; |
|
import android.location.Address; |
|
|
import android.location.Geocoder; |
|
16 |
import android.location.Location; |
import android.location.Location; |
17 |
import android.os.AsyncTask; |
import android.os.AsyncTask; |
18 |
import android.os.Bundle; |
import android.os.Bundle; |
19 |
import android.os.Handler; |
import android.os.Handler; |
20 |
import android.os.Message; |
import android.os.Message; |
21 |
|
|
22 |
import android.util.Log; |
import android.util.Log; |
23 |
import android.view.ContextMenu; |
import android.view.ContextMenu; |
24 |
import android.view.LayoutInflater; |
import android.view.LayoutInflater; |
38 |
import dk.thoerup.traininfo.util.IntSet; |
import dk.thoerup.traininfo.util.IntSet; |
39 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
40 |
|
|
41 |
|
import static dk.thoerup.traininfo.R.string.*; |
42 |
|
|
43 |
public class StationList extends ListActivity { |
public class StationList extends ListActivity { |
44 |
public static final int GOTLOCATION = 1001; |
public static final int GOTLOCATION = 1001; |
45 |
public static final int GOTSTATIONLIST = 1002; |
public static final int GOTSTATIONLIST = 1002; |
48 |
|
|
49 |
public static final int OPTIONS_MAP = 2003; |
public static final int OPTIONS_MAP = 2003; |
50 |
public static final int OPTIONS_GPSINFO = 2004; |
public static final int OPTIONS_GPSINFO = 2004; |
|
|
|
|
|
|
|
|
|
51 |
|
|
52 |
public static final int DLG_PROGRESS = 3001; |
public static final int DLG_PROGRESS = 3001; |
53 |
public static final int DLG_STATIONNAME = 3002; |
public static final int DLG_STATIONNAME = 3002; |
54 |
|
|
55 |
|
|
56 |
|
public static final int GPS_TIMEOUT_MS = 17500; //how long are we willing to wait for gps fix -in milliseconds |
57 |
|
|
58 |
|
|
59 |
static enum LookupMethod { |
static enum LookupMethod { |
60 |
ByLocation, |
ByLocation, |
61 |
ByName, |
ByName, |
71 |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
72 |
|
|
73 |
GeoPair location = new GeoPair(); |
GeoPair location = new GeoPair(); |
74 |
|
|
75 |
|
boolean isLaunchedforShortcut; |
76 |
boolean isRunning = false; |
boolean isRunning = false; |
77 |
List<StationBean> stations = new ArrayList<StationBean>(); |
List<StationBean> stations = new ArrayList<StationBean>(); |
78 |
|
|
114 |
listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); |
listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); |
115 |
setTitle(); |
setTitle(); |
116 |
|
|
117 |
|
isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false); |
118 |
|
|
119 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
120 |
|
|
121 |
|
|
123 |
case ListNearest: |
case ListNearest: |
124 |
startLookup(); |
startLookup(); |
125 |
break; |
break; |
126 |
case ListSearch: |
case ListSearch: |
127 |
this.showDialog(DLG_STATIONNAME); |
showDialog(DLG_STATIONNAME); //TODO: this.showDialogSafe(DLG_STATIONNAME); |
128 |
break; |
break; |
129 |
case ListFavorites: |
case ListFavorites: |
130 |
startFavoriteLookup(); |
startFavoriteLookup(); |
140 |
} |
} |
141 |
|
|
142 |
} |
} |
143 |
|
|
144 |
|
|
145 |
|
@Override |
146 |
|
protected void onDestroy() { |
147 |
|
super.onDestroy(); |
148 |
|
|
149 |
|
if (findStationsTask != null) { |
150 |
|
findStationsTask.cancel(true); |
151 |
|
} |
152 |
|
if (locationLookup != null) { |
153 |
|
locationLookup.stopSearch(); |
154 |
|
} |
155 |
|
isRunning = false; |
156 |
|
} |
157 |
|
|
158 |
|
|
159 |
protected void setTitle() { |
protected void setTitle() { |
160 |
String dialogTitle = getResources().getString(R.string.app_name); |
String dialogTitle = getResources().getString(app_name); |
161 |
switch (listType) { |
switch (listType) { |
162 |
case ListNearest: |
case ListNearest: |
163 |
dialogTitle += " - Nearby stations"; |
dialogTitle += " - " + getString(stationlist_nearbystations); |
164 |
break; |
break; |
165 |
case ListSearch: |
case ListSearch: |
166 |
dialogTitle += " - Search"; |
dialogTitle += " - " + getString(stationlist_search); |
167 |
break; |
break; |
168 |
case ListFavorites: |
case ListFavorites: |
169 |
dialogTitle += " - Favorites"; |
dialogTitle += " - " + getString(stationlist_favorites); |
170 |
break; |
break; |
171 |
default: |
default: |
172 |
dialogTitle = "";//not possible |
dialogTitle = "";//not possible |
173 |
} |
} |
174 |
|
|
175 |
setTitle(dialogTitle); |
setTitle(dialogTitle); |
176 |
|
|
177 |
} |
} |
178 |
|
|
179 |
|
|
180 |
|
/* these 3 dialogs helper functions are very rude and ugly hack |
181 |
|
* to remove these auto-reported exceptions |
182 |
|
* - android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@436aaef8 is not valid; is your activity running? |
183 |
|
* - java.lang.IllegalArgumentException: View not attached to window manager |
184 |
|
*/ |
185 |
|
|
186 |
|
/* |
187 |
|
public void showDialogSafe(int id) { |
188 |
|
try { |
189 |
|
showDialog(id); |
190 |
|
} catch (Exception e) { |
191 |
|
Log.e("StationList", "showDialog failed", e); |
192 |
|
} |
193 |
|
} |
194 |
|
|
195 |
|
public void dismissDialogSafe(int id) { |
196 |
|
try { |
197 |
|
dismissDialog(id); |
198 |
|
} catch (Exception e) { |
199 |
|
Log.e("StationList", "dismissDialog failed", e); |
200 |
|
} |
201 |
|
} |
202 |
|
public void dismissDialogSafe(Dialog dlg) { |
203 |
|
try { |
204 |
|
dlg.dismiss(); |
205 |
|
} catch (Exception e) { |
206 |
|
Log.e("StationList", "dismissDialog failed", e); |
207 |
|
} |
208 |
|
} |
209 |
|
|
210 |
|
public void builderShowSafe(AlertDialog.Builder builder) { |
211 |
|
try { |
212 |
|
builder.show(); |
213 |
|
} catch (Exception e) { |
214 |
|
Log.e("StationList", "builder.show() failed", e); |
215 |
|
} |
216 |
|
|
217 |
|
}*/ |
218 |
|
|
219 |
|
/* EOF rude and ugly dialog hack */ |
220 |
|
|
221 |
|
|
222 |
|
|
223 |
@Override |
@Override |
235 |
@Override |
@Override |
236 |
public boolean onCreateOptionsMenu(Menu menu) { |
public boolean onCreateOptionsMenu(Menu menu) { |
237 |
MenuItem item; |
MenuItem item; |
238 |
|
|
239 |
item = menu.add(0, OPTIONS_MAP, 0, "Station map"); |
item = menu.add(0, OPTIONS_MAP, 0, getString(stationlist_stationmap)); |
240 |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
241 |
|
|
242 |
item = menu.add(0, OPTIONS_GPSINFO, 0, "GPS Info"); |
item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo)); |
243 |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
244 |
|
|
245 |
return true; |
return true; |
267 |
case OPTIONS_GPSINFO: |
case OPTIONS_GPSINFO: |
268 |
Location loc = locationLookup.getLocation(); |
Location loc = locationLookup.getLocation(); |
269 |
StringBuffer message = new StringBuffer(); |
StringBuffer message = new StringBuffer(); |
270 |
message.append("Location info:\n"); |
message.append( getString(stationlist_locationinfo) ).append(":\n"); |
271 |
message.append("-Obtained by: ").append(loc != null ? loc.getProvider() : "-").append("\n"); |
if (loc != null) { |
272 |
message.append("-Accuracy: ").append(loc != null ? (int)loc.getAccuracy() : "-").append("m\n"); |
message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n"); |
273 |
|
message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n"); |
274 |
MessageBox.showMessage(this, message.toString()); |
message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n"); |
275 |
|
message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n"); |
276 |
|
} else { |
277 |
|
message.append( getString(stationlist_nolocation) ); |
278 |
|
} |
279 |
|
|
280 |
|
MessageBox.showMessage(this, message.toString(), false); |
281 |
break; |
break; |
282 |
default: |
default: |
283 |
retval = super.onOptionsItemSelected(item); |
retval = super.onOptionsItemSelected(item); |
295 |
|
|
296 |
|
|
297 |
} |
} |
298 |
|
|
299 |
|
public void showMessageAndClose(String message) { |
300 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
301 |
|
builder.setMessage(message) |
302 |
|
.setCancelable(false) |
303 |
|
.setPositiveButton("OK", new DialogInterface.OnClickListener() { |
304 |
|
public void onClick(DialogInterface dialog, int id) { |
305 |
|
dialog.dismiss(); |
306 |
|
StationList.this.finish(); |
307 |
|
} |
308 |
|
}) |
309 |
|
.show(); |
310 |
|
} |
311 |
|
|
312 |
|
|
313 |
|
|
317 |
switch (id) { |
switch (id) { |
318 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
319 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
320 |
dlg.setMessage("Wait for location fix"); |
dlg.setMessage( getString(stationlist_waitforlocation) ); |
321 |
dlg.setCancelable(false); |
dlg.setCancelable(false); |
322 |
return dlg; |
return dlg; |
323 |
case DLG_STATIONNAME: |
case DLG_STATIONNAME: |
327 |
|
|
328 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
329 |
|
|
330 |
builder.setTitle("Station search"); |
builder.setTitle( getString(stationlist_stationsearch) ); |
331 |
builder.setView(rootView); |
builder.setView(rootView); |
332 |
builder.setCancelable(true); |
builder.setCancelable(true); |
333 |
builder.setPositiveButton("Search", new DialogInterface.OnClickListener() { |
builder.setPositiveButton( getString(generic_search), new DialogInterface.OnClickListener() { |
334 |
public void onClick(DialogInterface dialog, int which) { |
public void onClick(DialogInterface dialog, int which) { |
335 |
EditText et = (EditText) rootView.findViewById(R.id.EditText); |
EditText et = (EditText) rootView.findViewById(R.id.EditText); |
336 |
dialog.dismiss(); |
dialog.dismiss(); |
337 |
if (et.getText().toString().length() >= 2) { |
String search = et.getText().toString().trim(); |
338 |
startNameSearch(et.getText().toString()); |
if (search.length() >= 2) { |
339 |
|
startNameSearch(search); |
340 |
} else { |
} else { |
341 |
MessageBox.showMessage(StationList.this, "Two characters minimum" ); |
showMessageAndClose( getString(stationlist_twocharmin) ); |
342 |
} |
} |
343 |
} |
} |
344 |
}); |
}); |
345 |
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
346 |
public void onClick(DialogInterface dialog, int which) { |
public void onClick(DialogInterface dialog, int which) { |
347 |
dialog.dismiss(); |
dialog.dismiss(); |
348 |
|
StationList.this.finish(); // Close this Activity |
349 |
} |
} |
350 |
}); |
}); |
351 |
return builder.create(); |
return builder.create(); |
376 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
377 |
|
|
378 |
StationBean station = stations.get(position); |
StationBean station = stations.get(position); |
|
|
|
|
double latitude = station.getLatitude(); |
|
|
double longitude = station.getLongitude(); |
|
|
|
|
|
|
|
379 |
|
|
380 |
Intent intent = new Intent(this, DepartureList.class); |
if (isLaunchedforShortcut == true) { |
381 |
intent.putExtra("name", station.getName()); |
Intent i = new Intent(); |
382 |
intent.putExtra("distance", station.getDistance()); |
i.putExtra("station", station); |
383 |
intent.putExtra("latitude", latitude); |
setResult(Activity.RESULT_OK, i); |
384 |
intent.putExtra("longitude", longitude); |
finish(); |
385 |
intent.putExtra("stationid", station.getId()); |
} else { |
386 |
intent.putExtra("address", station.getAddress()); |
Intent intent = new Intent(this, DepartureList.class); |
387 |
startActivity(intent); |
intent.putExtra("stationbean", station); |
388 |
|
startActivity(intent); |
389 |
|
} |
390 |
} |
} |
391 |
|
|
392 |
///////////////////////////////////////////////////////////// |
///////////////////////////////////////////////////////////// |
393 |
// |
// |
394 |
|
|
395 |
public void startLookup() { |
public void startLookup() { |
396 |
isRunning = true; |
isRunning = true; |
397 |
dialogMessage = "Wait for location fix"; |
dialogMessage = getString( stationlist_waitforlocation ); |
398 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS);//TODO:showDialogSafe(DLG_PROGRESS); |
399 |
|
|
400 |
locationLookup.locateStations(); |
locationLookup.locateStations(); |
401 |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS); |
402 |
} |
} |
403 |
|
|
404 |
void startNameSearch(String name) { |
void startNameSearch(String name) { |
405 |
dialogMessage = "Finding stations by name"; |
dialogMessage = getString( stationlist_findbyname ); |
406 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS);//TODO:showDialogSafe(DLG_PROGRESS); |
407 |
|
|
408 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
409 |
findStationsTask.searchByName(name, locationLookup.getLocation()); |
findStationsTask.searchByName(name); |
410 |
findStationsTask.execute(); |
findStationsTask.execute(); |
411 |
|
|
412 |
} |
} |
414 |
public void startFavoriteLookup() { |
public void startFavoriteLookup() { |
415 |
|
|
416 |
if (favorites.size() > 0) { |
if (favorites.size() > 0) { |
417 |
dialogMessage = "Loading favorites"; |
dialogMessage = getString( stationlist_loadfavorites ); |
418 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS);//TODO:showDialogSafe(DLG_PROGRESS); |
419 |
|
|
420 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
421 |
findStationsTask.searchByIds(favorites.toString(), locationLookup.getLocation()); |
findStationsTask.searchByIds( favorites.toString() ); |
422 |
findStationsTask.execute(); |
findStationsTask.execute(); |
423 |
} else { |
} else { |
424 |
MessageBox.showMessage(this, "Favorite list is empty"); |
showMessageAndClose( getString( stationlist_nofavorites ) ); |
425 |
} |
} |
426 |
} |
} |
427 |
|
|
429 |
|
|
430 |
void startLocatorTask() |
void startLocatorTask() |
431 |
{ |
{ |
432 |
dialogMessage = "Finding nearby stations"; |
dialogMessage = getString( stationlist_findingnearby ); |
433 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS);//TODO:showDialogSafe(DLG_PROGRESS); |
434 |
|
|
435 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
436 |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
437 |
findStationsTask.execute(); |
findStationsTask.execute(); |
438 |
} |
} |
|
|
|
|
|
|
|
String lookupAddress(double latitude, double longitude) { |
|
|
|
|
|
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); |
|
|
} |
|
439 |
|
|
|
return sb.toString(); |
|
|
} |
|
|
|
|
440 |
|
|
441 |
//////////////////////////////////////////////////////////////////////////// |
//////////////////////////////////////////////////////////////////////////// |
442 |
// Inner classes |
// Inner classes |
447 |
|
|
448 |
switch (msg.what) { |
switch (msg.what) { |
449 |
case GOTLOCATION: |
case GOTLOCATION: |
450 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS);//TODO:dismissDialogSafe(DLG_PROGRESS); |
451 |
|
|
452 |
startLocatorTask(); |
startLocatorTask(); |
453 |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
455 |
break; |
break; |
456 |
|
|
457 |
case NOPROVIDER: |
case NOPROVIDER: |
458 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS);//TODO:dismissDialogSafe(DLG_PROGRESS); |
459 |
MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps."); |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); |
460 |
|
//StationList.this.finish(); |
461 |
break; |
break; |
462 |
case LOCATIONFIXTIMEOUT: |
case LOCATIONFIXTIMEOUT: |
463 |
if (isRunning) { |
if (isRunning) { |
465 |
if (locationLookup.hasLocation()) { |
if (locationLookup.hasLocation()) { |
466 |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
467 |
} else { |
} else { |
468 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS);//TODO:dismissDialogSafe(DLG_PROGRESS); |
469 |
|
|
470 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
471 |
builder.setMessage("Location fix timed out"); |
builder.setMessage( getString( stationlist_gpstimeout) ); |
472 |
builder.setCancelable(true); |
builder.setCancelable(true); |
473 |
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
474 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
475 |
dialog.dismiss(); |
dialog.dismiss(); |
476 |
startLookup(); |
startLookup(); |
477 |
|
|
478 |
} |
} |
479 |
}); |
}); |
480 |
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() { |
481 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
482 |
dialog.dismiss(); |
dialog.dismiss(); |
483 |
} |
} |
484 |
}); |
}); |
485 |
builder.show(); |
builder.show();//TODO:builderShowSafe(builder); |
486 |
|
|
487 |
} |
} |
488 |
} |
} |
501 |
Location loc; |
Location loc; |
502 |
String ids; |
String ids; |
503 |
|
|
504 |
public void searchByName(String n, Location l) { |
public void searchByName(String n) { |
505 |
|
|
506 |
method = LookupMethod.ByName; |
method = LookupMethod.ByName; |
|
loc = l; |
|
507 |
name = n; |
name = n; |
508 |
} |
} |
509 |
|
|
512 |
loc = l; |
loc = l; |
513 |
} |
} |
514 |
|
|
515 |
public void searchByIds(String id, Location l) { |
public void searchByIds(String id) { |
516 |
|
|
517 |
method = LookupMethod.ByList; |
method = LookupMethod.ByList; |
|
loc = l; |
|
518 |
ids = id; |
ids = id; |
519 |
} |
} |
520 |
|
|
544 |
} |
} |
545 |
|
|
546 |
|
|
|
Location dummy = new Location("gps"); |
|
|
List<StationBean> stations = stationProvider.getStations(); |
|
|
|
|
|
for (StationBean station : stations) { |
|
|
String addr = lookupAddress(station.getLatitude(), station.getLongitude()); |
|
|
station.setAddress(addr); |
|
|
|
|
|
|
|
|
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); |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
547 |
return null; |
return null; |
548 |
} |
} |
549 |
|
|
550 |
@Override |
@Override |
551 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
552 |
super.onPostExecute(result); |
super.onPostExecute(result); |
553 |
dialog.dismiss(); |
dialog.dismiss();//TODO:dismissDialogSafe(dialog); |
554 |
|
|
555 |
|
|
556 |
if (success) { |
if (success) { |
557 |
if (stationProvider.getStations().size() == 0) |
if (stationProvider.getStations().size() == 0) { |
558 |
MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! |
showMessageAndClose(getString(stationlist_nostations)); |
559 |
|
} |
560 |
stations = stationProvider.getStations(); |
stations = stationProvider.getStations(); |
561 |
|
|
562 |
|
StationList.this.getListView().invalidateViews(); |
563 |
adapter.setStations( stations ); |
adapter.setStations( stations ); |
564 |
|
|
565 |
|
|
566 |
} else { //communication or parse errors |
} else { //communication or parse errors |
567 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
568 |
builder.setMessage("Error on finding nearby stations"); |
builder.setMessage(getString(stationlist_fetcherror)); |
569 |
builder.setCancelable(true); |
builder.setCancelable(true); |
570 |
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
571 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
572 |
dialog.dismiss(); |
dialog.dismiss(); |
573 |
|
|
574 |
stationsFetched.post( new Runnable() { |
Runnable runner = null; |
575 |
@Override |
switch (method) { |
576 |
public void run() { |
case ByLocation: |
577 |
startLocatorTask(); |
runner = new Runnable() { |
578 |
} |
@Override |
579 |
}); |
public void run() { |
580 |
|
startLocatorTask(); |
581 |
|
} |
582 |
|
}; |
583 |
|
break; |
584 |
|
case ByName: |
585 |
|
runner = new Runnable() { |
586 |
|
@Override |
587 |
|
public void run() { |
588 |
|
startNameSearch( FindStationsTask.this.name ); |
589 |
|
} |
590 |
|
}; |
591 |
|
break; |
592 |
|
case ByList: |
593 |
|
runner = new Runnable() { |
594 |
|
@Override |
595 |
|
public void run() { |
596 |
|
startFavoriteLookup(); |
597 |
|
} |
598 |
|
}; |
599 |
|
break; |
600 |
|
} |
601 |
|
|
602 |
|
stationsFetched.post( runner ); |
603 |
} |
} |
604 |
}); |
}); |
605 |
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
606 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
607 |
dialog.dismiss(); |
dialog.dismiss(); |
608 |
|
StationList.this.finish(); |
609 |
} |
} |
610 |
}); |
}); |
611 |
builder.show(); |
|
612 |
|
builder.show();//TODO:builderShowSafe(builder); |
613 |
} |
} |
614 |
} |
} |
615 |
} |
} |
630 |
int stationID = stations.get(selectedPosition).getId(); |
int stationID = stations.get(selectedPosition).getId(); |
631 |
|
|
632 |
if (!favorites.contains(stationID)) { |
if (!favorites.contains(stationID)) { |
633 |
menu.add(0, FAVORITES_ADD, 0, "Add to favorites"); |
menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) ); |
634 |
} else { |
} else { |
635 |
menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites"); |
menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_removefavorite) ); |
636 |
} |
} |
637 |
|
|
638 |
} |
} |
643 |
int stationID = sb.getId(); |
int stationID = sb.getId(); |
644 |
if (item.getItemId() == FAVORITES_ADD) { |
if (item.getItemId() == FAVORITES_ADD) { |
645 |
favorites.add(stationID); |
favorites.add(stationID); |
646 |
Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show(); |
Toast.makeText(StationList.this, getString(stationlist_stationadded), Toast.LENGTH_SHORT).show(); |
647 |
} else { |
} else { |
648 |
|
|
649 |
favorites.remove(stationID); |
favorites.remove(stationID); |
650 |
Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show(); |
Toast.makeText(StationList.this, getString(stationlist_stationremoved), Toast.LENGTH_SHORT).show(); |
651 |
|
|
652 |
|
|
653 |
if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) { |
if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) { |