2 |
|
|
3 |
import java.util.ArrayList; |
import java.util.ArrayList; |
4 |
import java.util.List; |
import java.util.List; |
5 |
import java.util.Locale; |
|
|
import java.util.prefs.Preferences; |
|
6 |
|
|
7 |
import android.app.AlertDialog; |
import android.app.AlertDialog; |
8 |
import android.app.Dialog; |
import android.app.Dialog; |
11 |
import android.content.DialogInterface; |
import android.content.DialogInterface; |
12 |
import android.content.Intent; |
import android.content.Intent; |
13 |
import android.content.SharedPreferences; |
import android.content.SharedPreferences; |
14 |
import android.location.Address; |
import android.content.SharedPreferences.Editor; |
|
import android.location.Geocoder; |
|
15 |
import android.location.Location; |
import android.location.Location; |
16 |
import android.os.AsyncTask; |
import android.os.AsyncTask; |
17 |
import android.os.Bundle; |
import android.os.Bundle; |
18 |
import android.os.Handler; |
import android.os.Handler; |
19 |
import android.os.Message; |
import android.os.Message; |
20 |
|
|
21 |
import android.util.Log; |
import android.util.Log; |
22 |
import android.view.ContextMenu; |
import android.view.ContextMenu; |
23 |
import android.view.LayoutInflater; |
import android.view.LayoutInflater; |
37 |
import dk.thoerup.traininfo.util.IntSet; |
import dk.thoerup.traininfo.util.IntSet; |
38 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
39 |
|
|
40 |
|
import static dk.thoerup.traininfo.R.string.*; |
41 |
|
|
42 |
public class StationList extends ListActivity { |
public class StationList extends ListActivity { |
43 |
public static final int GOTLOCATION = 1001; |
public static final int GOTLOCATION = 1001; |
44 |
public static final int GOTSTATIONLIST = 1002; |
public static final int GOTSTATIONLIST = 1002; |
45 |
public static final int NOPROVIDER = 1003; |
public static final int NOPROVIDER = 1003; |
46 |
public static final int LOCATIONFIXTIMEOUT = 1004; |
public static final int LOCATIONFIXTIMEOUT = 1004; |
47 |
|
|
|
public static final int OPTIONS_RESCAN = 2001; |
|
|
public static final int OPTIONS_NAMESEARCH = 2002; |
|
48 |
public static final int OPTIONS_MAP = 2003; |
public static final int OPTIONS_MAP = 2003; |
49 |
public static final int OPTIONS_ABOUT = 2004; |
public static final int OPTIONS_GPSINFO = 2004; |
50 |
public static final int OPTIONS_FAVORITES = 2005; |
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
public static final int DLG_PROGRESS = 3001; |
public static final int DLG_PROGRESS = 3001; |
55 |
public static final int DLG_STATIONNAME = 3002; |
public static final int DLG_STATIONNAME = 3002; |
56 |
|
|
57 |
/** Called when the activity is first created. */ |
static enum LookupMethod { |
58 |
|
ByLocation, |
59 |
|
ByName, |
60 |
|
ByList, |
61 |
|
MethodNone |
62 |
|
} |
63 |
|
|
64 |
|
|
65 |
String dialogMessage = ""; |
String dialogMessage = ""; |
66 |
ProgressDialog dialog; |
ProgressDialog dialog; |
67 |
LocationLookup locator = null; |
LocationLookup locationLookup = null; |
68 |
LocatorTask locatorTask; |
FindStationsTask findStationsTask; |
69 |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
70 |
|
|
71 |
GeoPair location = new GeoPair(); |
GeoPair location = new GeoPair(); |
79 |
|
|
80 |
FavoritesMenu contextMenu = new FavoritesMenu(); |
FavoritesMenu contextMenu = new FavoritesMenu(); |
81 |
IntSet favorites = new IntSet(); |
IntSet favorites = new IntSet(); |
82 |
|
|
83 |
static enum LookupMethod { |
WelcomeScreen.ListType listType; |
|
ByLocation, |
|
|
ByName, |
|
|
ByList, |
|
|
MethodNone |
|
|
} |
|
|
|
|
84 |
SharedPreferences prefs; |
SharedPreferences prefs; |
85 |
|
|
86 |
/////////////////////////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////////////////////////// |
90 |
@Override |
@Override |
91 |
public void onCreate(Bundle savedInstanceState) { |
public void onCreate(Bundle savedInstanceState) { |
92 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
93 |
setContentView(R.layout.main); |
setContentView(R.layout.stationlist); |
94 |
|
|
95 |
|
|
96 |
adapter = new StationListAdapter(this); |
adapter = new StationListAdapter(this); |
99 |
ListView lv = getListView(); |
ListView lv = getListView(); |
100 |
lv.setOnCreateContextMenuListener(contextMenu); |
lv.setOnCreateContextMenuListener(contextMenu); |
101 |
|
|
102 |
locator = new LocationLookup(this, stationsFetched); |
locationLookup = new LocationLookup(this, stationsFetched); |
103 |
|
|
104 |
|
|
105 |
prefs = getSharedPreferences("TrainStation", 0); |
prefs = getSharedPreferences("TrainStation", 0); |
108 |
favorites.fromString(favoriteString); |
favorites.fromString(favoriteString); |
109 |
} |
} |
110 |
|
|
111 |
|
listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); |
112 |
|
setTitle(); |
113 |
|
|
114 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
115 |
startLookup(); |
|
116 |
|
|
117 |
|
switch (listType) { |
118 |
|
case ListNearest: |
119 |
|
startLookup(); |
120 |
|
break; |
121 |
|
case ListSearch: |
122 |
|
this.showDialogSafe(DLG_STATIONNAME); |
123 |
|
break; |
124 |
|
case ListFavorites: |
125 |
|
startFavoriteLookup(); |
126 |
|
break; |
127 |
|
default: |
128 |
|
// Not possible !?! |
129 |
|
} |
130 |
|
|
131 |
} else { |
} else { |
132 |
stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations"); |
stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations"); |
133 |
adapter.setStations(stations); |
adapter.setStations(stations); |
134 |
location = (GeoPair) savedInstanceState.getSerializable("location"); |
location = (GeoPair) savedInstanceState.getSerializable("location"); |
135 |
} |
} |
136 |
|
|
137 |
} |
} |
138 |
|
protected void setTitle() { |
139 |
|
String dialogTitle = getResources().getString(app_name); |
140 |
|
switch (listType) { |
141 |
|
case ListNearest: |
142 |
|
dialogTitle += " - " + getString(stationlist_nearbystations); |
143 |
|
break; |
144 |
|
case ListSearch: |
145 |
|
dialogTitle += " - " + getString(stationlist_search); |
146 |
|
break; |
147 |
|
case ListFavorites: |
148 |
|
dialogTitle += " - " + getString(stationlist_favorites); |
149 |
|
break; |
150 |
|
default: |
151 |
|
dialogTitle = "";//not possible |
152 |
|
} |
153 |
|
|
154 |
|
setTitle(dialogTitle); |
155 |
|
|
156 |
|
} |
157 |
|
|
158 |
|
|
159 |
|
/* these 3 dialogs helper functions are very rude and ugly hack |
160 |
|
* to remove these auto-reported exceptions |
161 |
|
* - android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@436aaef8 is not valid; is your activity running? |
162 |
|
* - java.lang.IllegalArgumentException: View not attached to window manager |
163 |
|
*/ |
164 |
|
|
165 |
|
public void showDialogSafe(int id) { |
166 |
|
try { |
167 |
|
showDialog(id); |
168 |
|
} catch (Exception e) { |
169 |
|
Log.e("StationList", "showDialog failed", e); |
170 |
|
} |
171 |
|
} |
172 |
|
|
173 |
|
public void dismissDialogSafe(int id) { |
174 |
|
try { |
175 |
|
dismissDialog(id); |
176 |
|
} catch (Exception e) { |
177 |
|
Log.e("StationList", "dismissDialog failed", e); |
178 |
|
} |
179 |
|
} |
180 |
|
|
181 |
|
public void builderShowSafe(AlertDialog.Builder builder) { |
182 |
|
try { |
183 |
|
builder.show(); |
184 |
|
} catch (Exception e) { |
185 |
|
Log.e("StationList", "builder.show() failed", e); |
186 |
|
} |
187 |
|
|
188 |
|
} |
189 |
|
/* EOF rude and ugly dialog hack */ |
190 |
|
|
191 |
|
|
192 |
|
|
193 |
@Override |
@Override |
197 |
dialog.dismiss(); |
dialog.dismiss(); |
198 |
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
199 |
outState.putSerializable("location", location); |
outState.putSerializable("location", location); |
200 |
|
|
201 |
} |
} |
202 |
|
|
203 |
|
|
206 |
public boolean onCreateOptionsMenu(Menu menu) { |
public boolean onCreateOptionsMenu(Menu menu) { |
207 |
MenuItem item; |
MenuItem item; |
208 |
|
|
209 |
item = menu.add(0, OPTIONS_RESCAN, 0, "Find nearest stations"); |
item = menu.add(0, OPTIONS_MAP, 0, getString(stationlist_stationmap)); |
|
item.setIcon(android.R.drawable.ic_menu_mylocation); |
|
|
|
|
|
item = menu.add(0, OPTIONS_NAMESEARCH, 0, "Search for station"); |
|
|
item.setIcon(android.R.drawable.ic_menu_search); |
|
|
|
|
|
item = menu.add(0, OPTIONS_FAVORITES, 0, "Favorites"); |
|
|
item.setIcon(android.R.drawable.ic_menu_agenda); |
|
|
|
|
|
item = menu.add(0, OPTIONS_MAP, 0, "Show station map"); |
|
210 |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
211 |
|
|
212 |
item = menu.add(0, OPTIONS_ABOUT, 0, "About"); |
item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo)); |
213 |
item.setIcon(android.R.drawable.ic_menu_info_details); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
214 |
|
|
215 |
return true; |
return true; |
216 |
} |
} |
217 |
|
|
219 |
public boolean onOptionsItemSelected(MenuItem item) { |
public boolean onOptionsItemSelected(MenuItem item) { |
220 |
boolean retval = true; |
boolean retval = true; |
221 |
|
|
222 |
|
//TODO: Cleanup |
223 |
switch (item.getItemId()) { |
switch (item.getItemId()) { |
|
case OPTIONS_RESCAN: |
|
|
startLookup(); |
|
|
break; |
|
|
case OPTIONS_NAMESEARCH: |
|
|
showDialog(DLG_STATIONNAME); |
|
|
break; |
|
|
case OPTIONS_FAVORITES: |
|
|
startFavoriteLookup(); |
|
|
break; |
|
224 |
case OPTIONS_MAP: |
case OPTIONS_MAP: |
225 |
|
|
226 |
Intent intent = new Intent(this,StationMapView.class); |
Intent intent = new Intent(this,StationMapView.class); |
|
intent.putExtra("userlocation", location ); |
|
227 |
|
|
228 |
ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>(); |
ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>(); |
229 |
for (StationBean st : stations ) { |
for (StationBean st : stations ) { |
234 |
|
|
235 |
startActivity(intent); |
startActivity(intent); |
236 |
break; |
break; |
237 |
case OPTIONS_ABOUT: |
case OPTIONS_GPSINFO: |
238 |
String ver = this.getResources().getString(R.string.app_version); |
Location loc = locationLookup.getLocation(); |
|
|
|
|
Location loc = locator.getLocation(); |
|
239 |
StringBuffer message = new StringBuffer(); |
StringBuffer message = new StringBuffer(); |
240 |
message.append("TrainInfo DK v").append(ver).append("\n"); |
message.append( getString(stationlist_locationinfo) ).append(":\n"); |
241 |
message.append("By Torben Nielsen\n"); |
if (loc != null) { |
242 |
message.append("\n"); |
message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n"); |
243 |
message.append("Location info:\n"); |
message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n"); |
244 |
message.append("-Obtained by: ").append(loc != null ? loc.getProvider() : "-").append("\n"); |
message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n"); |
245 |
message.append("-Accuracy: ").append(loc != null ? (int)loc.getAccuracy() : "-").append("m\n"); |
message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n"); |
246 |
|
} else { |
247 |
|
message.append( getString(stationlist_nolocation) ); |
248 |
|
} |
249 |
|
|
250 |
MessageBox.showMessage(this, message.toString()); |
MessageBox.showMessage(this, message.toString()); |
251 |
break; |
break; |
252 |
default: |
default: |
265 |
|
|
266 |
|
|
267 |
} |
} |
268 |
|
|
269 |
|
public void showMessageAndClose(String message) { |
270 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
271 |
|
builder.setMessage(message) |
272 |
|
.setCancelable(false) |
273 |
|
.setPositiveButton("OK", new DialogInterface.OnClickListener() { |
274 |
|
public void onClick(DialogInterface dialog, int id) { |
275 |
|
dialog.dismiss(); |
276 |
|
StationList.this.finish(); |
277 |
|
} |
278 |
|
}) |
279 |
|
.show(); |
280 |
|
} |
281 |
|
|
282 |
|
|
283 |
|
|
287 |
switch (id) { |
switch (id) { |
288 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
289 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
290 |
dlg.setMessage("Wait for location fix"); |
dlg.setMessage( getString(stationlist_waitforlocation) ); |
291 |
dlg.setCancelable(false); |
dlg.setCancelable(false); |
292 |
return dlg; |
return dlg; |
293 |
case DLG_STATIONNAME: |
case DLG_STATIONNAME: |
297 |
|
|
298 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
299 |
|
|
300 |
builder.setTitle("Station search"); |
builder.setTitle( getString(stationlist_stationsearch) ); |
301 |
builder.setView(rootView); |
builder.setView(rootView); |
302 |
builder.setCancelable(true); |
builder.setCancelable(true); |
303 |
builder.setPositiveButton("Search", new DialogInterface.OnClickListener() { |
builder.setPositiveButton( getString(generic_search), new DialogInterface.OnClickListener() { |
304 |
public void onClick(DialogInterface dialog, int which) { |
public void onClick(DialogInterface dialog, int which) { |
305 |
EditText et = (EditText) rootView.findViewById(R.id.EditText); |
EditText et = (EditText) rootView.findViewById(R.id.EditText); |
306 |
dialog.dismiss(); |
dialog.dismiss(); |
307 |
if (et.getText().toString().length() >= 2) { |
String search = et.getText().toString().trim(); |
308 |
startNameSearch(et.getText().toString()); |
if (search.length() >= 2) { |
309 |
|
startNameSearch(search); |
310 |
} else { |
} else { |
311 |
MessageBox.showMessage(StationList.this, "Two characters minimum" ); |
showMessageAndClose( getString(stationlist_twocharmin) ); |
312 |
} |
} |
313 |
} |
} |
314 |
}); |
}); |
315 |
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
316 |
public void onClick(DialogInterface dialog, int which) { |
public void onClick(DialogInterface dialog, int which) { |
317 |
dialog.dismiss(); |
dialog.dismiss(); |
318 |
|
StationList.this.finish(); // Close this Activity |
319 |
} |
} |
320 |
}); |
}); |
321 |
return builder.create(); |
return builder.create(); |
346 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
347 |
|
|
348 |
StationBean station = stations.get(position); |
StationBean station = stations.get(position); |
|
|
|
|
double latitude = station.getLatitude(); |
|
|
double longitude = station.getLongitude(); |
|
|
|
|
|
|
|
349 |
|
|
350 |
Intent intent = new Intent(this, DepartureList.class); |
Intent intent = new Intent(this, DepartureList.class); |
351 |
intent.putExtra("name", station.getName()); |
intent.putExtra("stationbean", station); |
|
intent.putExtra("distance", station.getDistance()); |
|
|
intent.putExtra("latitude", latitude); |
|
|
intent.putExtra("longitude", longitude); |
|
|
intent.putExtra("stationid", station.getId()); |
|
|
intent.putExtra("address", station.getAddress()); |
|
352 |
startActivity(intent); |
startActivity(intent); |
353 |
} |
} |
354 |
|
|
356 |
// |
// |
357 |
|
|
358 |
public void startLookup() { |
public void startLookup() { |
359 |
isRunning = true; |
isRunning = true; |
360 |
dialogMessage = "Wait for location fix"; |
dialogMessage = getString( stationlist_waitforlocation ); |
361 |
showDialog(DLG_PROGRESS); |
showDialogSafe(DLG_PROGRESS); |
362 |
|
|
363 |
locator.locateStations(); |
locationLookup.locateStations(); |
364 |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); |
365 |
} |
} |
366 |
|
|
367 |
void startNameSearch(String name) { |
void startNameSearch(String name) { |
368 |
dialogMessage = "Finding stations by name"; |
dialogMessage = getString( stationlist_findbyname ); |
369 |
showDialog(DLG_PROGRESS); |
showDialogSafe(DLG_PROGRESS); |
370 |
|
|
371 |
locatorTask = new LocatorTask(); |
findStationsTask = new FindStationsTask(); |
372 |
locatorTask.searchByName(name, locator.getLocation()); |
findStationsTask.searchByName(name); |
373 |
locatorTask.execute(); |
findStationsTask.execute(); |
374 |
|
|
375 |
} |
} |
376 |
|
|
377 |
public void startFavoriteLookup() { |
public void startFavoriteLookup() { |
378 |
|
|
379 |
if (favorites.toString().length() > 0) { |
if (favorites.size() > 0) { |
380 |
dialogMessage = "Loading favorites"; |
dialogMessage = getString( stationlist_loadfavorites ); |
381 |
showDialog(DLG_PROGRESS); |
showDialogSafe(DLG_PROGRESS); |
382 |
|
|
383 |
locatorTask = new LocatorTask(); |
findStationsTask = new FindStationsTask(); |
384 |
locatorTask.searchByIds(favorites.toString(), locator.getLocation()); |
findStationsTask.searchByIds( favorites.toString() ); |
385 |
locatorTask.execute(); |
findStationsTask.execute(); |
386 |
} else { |
} else { |
387 |
MessageBox.showMessage(this, "Favorite list is empty"); |
showMessageAndClose( getString( stationlist_nofavorites ) ); |
388 |
} |
} |
389 |
} |
} |
390 |
|
|
392 |
|
|
393 |
void startLocatorTask() |
void startLocatorTask() |
394 |
{ |
{ |
395 |
dialogMessage = "Finding nearby stations"; |
dialogMessage = getString( stationlist_findingnearby ); |
396 |
showDialog(DLG_PROGRESS); |
showDialogSafe(DLG_PROGRESS); |
397 |
|
|
398 |
locatorTask = new LocatorTask(); |
findStationsTask = new FindStationsTask(); |
399 |
locatorTask.searchByLocation( locator.getLocation() ); |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
400 |
locatorTask.execute(); |
findStationsTask.execute(); |
401 |
} |
} |
|
|
|
|
|
|
|
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); |
|
|
} |
|
402 |
|
|
|
return sb.toString(); |
|
|
} |
|
|
|
|
403 |
|
|
404 |
//////////////////////////////////////////////////////////////////////////// |
//////////////////////////////////////////////////////////////////////////// |
405 |
// Inner classes |
// Inner classes |
410 |
|
|
411 |
switch (msg.what) { |
switch (msg.what) { |
412 |
case GOTLOCATION: |
case GOTLOCATION: |
413 |
dismissDialog(DLG_PROGRESS); |
dismissDialogSafe(DLG_PROGRESS); |
414 |
|
|
415 |
startLocatorTask(); |
startLocatorTask(); |
416 |
location = GeoPair.fromLocation( locator.getLocation() ); |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
417 |
|
|
418 |
break; |
break; |
419 |
|
|
420 |
case NOPROVIDER: |
case NOPROVIDER: |
421 |
dismissDialog(DLG_PROGRESS); |
dismissDialogSafe(DLG_PROGRESS); |
422 |
MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps."); |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) ); |
423 |
break; |
break; |
424 |
case LOCATIONFIXTIMEOUT: |
case LOCATIONFIXTIMEOUT: |
425 |
if (isRunning) { |
if (isRunning) { |
426 |
locator.stopSearch(); |
locationLookup.stopSearch(); |
427 |
if (locator.hasLocation()) { |
if (locationLookup.hasLocation()) { |
428 |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
429 |
} else { |
} else { |
430 |
dismissDialog(DLG_PROGRESS); |
dismissDialogSafe(DLG_PROGRESS); |
431 |
|
|
432 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
433 |
builder.setMessage("Location fix timed out"); |
builder.setMessage( getString( stationlist_gpstimeout) ); |
434 |
builder.setCancelable(true); |
builder.setCancelable(true); |
435 |
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
436 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
437 |
dialog.dismiss(); |
dialog.dismiss(); |
438 |
startLookup(); |
startLookup(); |
439 |
|
|
440 |
} |
} |
441 |
}); |
}); |
442 |
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() { |
443 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
444 |
dialog.dismiss(); |
dialog.dismiss(); |
445 |
} |
} |
446 |
}); |
}); |
447 |
builder.show(); |
builderShowSafe(builder); // builder.show() |
448 |
|
|
449 |
} |
} |
450 |
} |
} |
455 |
}; |
}; |
456 |
|
|
457 |
|
|
458 |
class LocatorTask extends AsyncTask<Void,Void,Void> { |
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
459 |
|
|
460 |
LookupMethod method = LookupMethod.MethodNone; |
LookupMethod method = LookupMethod.MethodNone; |
461 |
boolean success; |
boolean success; |
463 |
Location loc; |
Location loc; |
464 |
String ids; |
String ids; |
465 |
|
|
466 |
public void searchByName(String n, Location l) { |
public void searchByName(String n) { |
467 |
|
|
468 |
method = LookupMethod.ByName; |
method = LookupMethod.ByName; |
|
loc = l; |
|
469 |
name = n; |
name = n; |
470 |
} |
} |
471 |
|
|
474 |
loc = l; |
loc = l; |
475 |
} |
} |
476 |
|
|
477 |
public void searchByIds(String id, Location l) { |
public void searchByIds(String id) { |
478 |
|
|
479 |
method = LookupMethod.ByList; |
method = LookupMethod.ByList; |
|
loc = l; |
|
480 |
ids = id; |
ids = id; |
481 |
} |
} |
482 |
|
|
490 |
|
|
491 |
@Override |
@Override |
492 |
protected Void doInBackground(Void... params) { |
protected Void doInBackground(Void... params) { |
493 |
|
|
494 |
if (method.equals(LookupMethod.ByLocation)) |
switch (method) { |
495 |
|
case ByLocation: |
496 |
success = stationProvider.lookupStations(loc); |
success = stationProvider.lookupStations(loc); |
497 |
|
break; |
498 |
if (method.equals(LookupMethod.ByName)) |
case ByName: |
499 |
success = stationProvider.lookupStationsByName(name); |
success = stationProvider.lookupStationsByName(name); |
500 |
|
break; |
501 |
if (method.equals(LookupMethod.ByList)) |
case ByList: |
502 |
success = stationProvider.lookupStationsByIds(ids); |
success = stationProvider.lookupStationsByIds(ids); |
503 |
|
break; |
504 |
|
default: |
505 |
|
success = false; // not possible |
506 |
|
} |
507 |
|
|
|
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)) { |
|
|
dummy.setLatitude(station.getLatitude()); |
|
|
dummy.setLongitude(station.getLongitude()); |
|
|
station.setDistance( (int)loc.distanceTo(dummy) ); |
|
|
} |
|
|
} |
|
508 |
|
|
509 |
return null; |
return null; |
510 |
} |
} |
514 |
super.onPostExecute(result); |
super.onPostExecute(result); |
515 |
dialog.dismiss(); |
dialog.dismiss(); |
516 |
|
|
517 |
|
|
518 |
if (success) { |
if (success) { |
519 |
if (stationProvider.getStations().size() == 0) |
if (stationProvider.getStations().size() == 0) { |
520 |
MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! |
showMessageAndClose(getString(stationlist_nostations)); |
521 |
|
} |
522 |
stations = stationProvider.getStations(); |
stations = stationProvider.getStations(); |
523 |
adapter.setStations( stations ); |
adapter.setStations( stations ); |
524 |
|
|
525 |
} else { //communication or parse errors |
} else { //communication or parse errors |
526 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
527 |
builder.setMessage("Error on finding nearby stations"); |
builder.setMessage(getString(stationlist_nearbyerror)); |
528 |
builder.setCancelable(true); |
builder.setCancelable(true); |
529 |
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
530 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
531 |
dialog.dismiss(); |
dialog.dismiss(); |
532 |
|
|
533 |
stationsFetched.post( new Runnable() { |
Runnable runner = null; |
534 |
@Override |
switch (method) { |
535 |
public void run() { |
case ByLocation: |
536 |
startLocatorTask(); |
runner = new Runnable() { |
537 |
} |
@Override |
538 |
}); |
public void run() { |
539 |
|
startLocatorTask(); |
540 |
|
} |
541 |
|
}; |
542 |
|
break; |
543 |
|
case ByName: |
544 |
|
runner = new Runnable() { |
545 |
|
@Override |
546 |
|
public void run() { |
547 |
|
startNameSearch( FindStationsTask.this.name ); |
548 |
|
} |
549 |
|
}; |
550 |
|
break; |
551 |
|
case ByList: |
552 |
|
runner = new Runnable() { |
553 |
|
@Override |
554 |
|
public void run() { |
555 |
|
startFavoriteLookup(); |
556 |
|
} |
557 |
|
}; |
558 |
|
break; |
559 |
|
} |
560 |
|
|
561 |
|
stationsFetched.post( runner ); |
562 |
} |
} |
563 |
}); |
}); |
564 |
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
565 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
566 |
dialog.dismiss(); |
dialog.dismiss(); |
567 |
} |
} |
568 |
}); |
}); |
569 |
builder.show(); |
|
570 |
|
builderShowSafe(builder); // builder.show() |
571 |
} |
} |
572 |
} |
} |
573 |
} |
} |
587 |
selectedPosition = info.position; |
selectedPosition = info.position; |
588 |
int stationID = stations.get(selectedPosition).getId(); |
int stationID = stations.get(selectedPosition).getId(); |
589 |
|
|
590 |
if (!favorites.hasInt(stationID)) { |
if (!favorites.contains(stationID)) { |
591 |
menu.add(0, FAVORITES_ADD, 0, "Add to favorites"); |
menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) ); |
592 |
} else { |
} else { |
593 |
menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites"); |
menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_removefavorite) ); |
594 |
} |
} |
595 |
|
|
596 |
} |
} |
601 |
int stationID = sb.getId(); |
int stationID = sb.getId(); |
602 |
if (item.getItemId() == FAVORITES_ADD) { |
if (item.getItemId() == FAVORITES_ADD) { |
603 |
favorites.add(stationID); |
favorites.add(stationID); |
604 |
Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show(); |
Toast.makeText(StationList.this, getString(stationlist_stationadded), Toast.LENGTH_SHORT).show(); |
605 |
} else { |
} else { |
606 |
|
|
607 |
favorites.remove(stationID); |
favorites.remove(stationID); |
608 |
Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show(); |
Toast.makeText(StationList.this, getString(stationlist_stationremoved), Toast.LENGTH_SHORT).show(); |
609 |
|
|
610 |
|
|
611 |
|
if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) { |
612 |
|
stations.remove(selectedPosition); |
613 |
|
adapter.notifyDataSetChanged(); |
614 |
|
} |
615 |
} |
} |
616 |
|
Editor ed = prefs.edit(); |
617 |
prefs.edit().putString("favorites", favorites.toString()); |
ed.putString("favorites", favorites.toString()); |
618 |
|
ed.commit(); |
619 |
} |
} |
620 |
} |
} |
621 |
} |
} |