2 |
|
|
3 |
import java.util.ArrayList; |
import java.util.ArrayList; |
4 |
import java.util.List; |
import java.util.List; |
|
import java.util.Locale; |
|
|
import java.util.prefs.Preferences; |
|
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; |
12 |
import android.content.DialogInterface; |
import android.content.DialogInterface; |
13 |
import android.content.Intent; |
import android.content.Intent; |
14 |
import android.content.SharedPreferences; |
import android.content.SharedPreferences; |
15 |
import android.location.Address; |
import android.content.SharedPreferences.Editor; |
|
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 |
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; |
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; |
|
public static final int OPTIONS_FAVORITES = 2005; |
|
|
|
|
|
|
|
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 |
/** Called when the activity is first created. */ |
|
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 { |
59 |
|
ByLocation, |
60 |
|
ByName, |
61 |
|
ByList, |
62 |
|
MethodNone |
63 |
|
} |
64 |
|
|
65 |
|
|
66 |
String dialogMessage = ""; |
String dialogMessage = ""; |
67 |
ProgressDialog dialog; |
ProgressDialog dialog; |
68 |
LocationLookup locator = null; |
LocationLookup locationLookup = null; |
69 |
LocatorTask locatorTask; |
FindStationsTask findStationsTask; |
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 |
|
|
81 |
|
|
82 |
FavoritesMenu contextMenu = new FavoritesMenu(); |
FavoritesMenu contextMenu = new FavoritesMenu(); |
83 |
IntSet favorites = new IntSet(); |
IntSet favorites = new IntSet(); |
84 |
|
|
85 |
static enum LookupMethod { |
WelcomeScreen.ListType listType; |
|
ByLocation, |
|
|
ByName, |
|
|
ByList, |
|
|
MethodNone |
|
|
} |
|
|
|
|
86 |
SharedPreferences prefs; |
SharedPreferences prefs; |
87 |
|
|
88 |
/////////////////////////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////////////////////////// |
92 |
@Override |
@Override |
93 |
public void onCreate(Bundle savedInstanceState) { |
public void onCreate(Bundle savedInstanceState) { |
94 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
95 |
setContentView(R.layout.main); |
setContentView(R.layout.stationlist); |
96 |
|
|
97 |
|
|
98 |
adapter = new StationListAdapter(this); |
adapter = new StationListAdapter(this); |
101 |
ListView lv = getListView(); |
ListView lv = getListView(); |
102 |
lv.setOnCreateContextMenuListener(contextMenu); |
lv.setOnCreateContextMenuListener(contextMenu); |
103 |
|
|
104 |
locator = new LocationLookup(this, stationsFetched); |
locationLookup = new LocationLookup(this, stationsFetched); |
105 |
|
|
106 |
|
|
107 |
prefs = getSharedPreferences("TrainStation", 0); |
prefs = getSharedPreferences("TrainStation", 0); |
110 |
favorites.fromString(favoriteString); |
favorites.fromString(favoriteString); |
111 |
} |
} |
112 |
|
|
113 |
|
listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); |
114 |
|
setTitle(); |
115 |
|
|
116 |
|
isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false); |
117 |
|
|
118 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
119 |
startLookup(); |
|
120 |
|
|
121 |
|
switch (listType) { |
122 |
|
case ListNearest: |
123 |
|
startLookup(); |
124 |
|
break; |
125 |
|
case ListSearch: |
126 |
|
showDialog(DLG_STATIONNAME); |
127 |
|
break; |
128 |
|
case ListFavorites: |
129 |
|
startFavoriteLookup(); |
130 |
|
break; |
131 |
|
default: |
132 |
|
// Not possible !?! |
133 |
|
} |
134 |
|
|
135 |
} else { |
} else { |
136 |
stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations"); |
stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations"); |
137 |
adapter.setStations(stations); |
adapter.setStations(stations); |
138 |
location = (GeoPair) savedInstanceState.getSerializable("location"); |
location = (GeoPair) savedInstanceState.getSerializable("location"); |
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() { |
160 |
|
String dialogTitle = getResources().getString(app_name); |
161 |
|
switch (listType) { |
162 |
|
case ListNearest: |
163 |
|
dialogTitle += " - " + getString(stationlist_nearbystations); |
164 |
|
break; |
165 |
|
case ListSearch: |
166 |
|
dialogTitle += " - " + getString(stationlist_search); |
167 |
|
break; |
168 |
|
case ListFavorites: |
169 |
|
dialogTitle += " - " + getString(stationlist_favorites); |
170 |
|
break; |
171 |
|
default: |
172 |
|
dialogTitle = "";//not possible |
173 |
|
} |
174 |
|
|
175 |
|
setTitle(dialogTitle); |
176 |
|
|
177 |
|
} |
178 |
|
|
179 |
|
|
180 |
|
|
181 |
@Override |
@Override |
182 |
public void onSaveInstanceState(Bundle outState) |
public void onSaveInstanceState(Bundle outState) |
185 |
dialog.dismiss(); |
dialog.dismiss(); |
186 |
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
187 |
outState.putSerializable("location", location); |
outState.putSerializable("location", location); |
188 |
|
|
189 |
} |
} |
190 |
|
|
191 |
|
|
194 |
public boolean onCreateOptionsMenu(Menu menu) { |
public boolean onCreateOptionsMenu(Menu menu) { |
195 |
MenuItem item; |
MenuItem item; |
196 |
|
|
197 |
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"); |
|
198 |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
199 |
|
|
200 |
item = menu.add(0, OPTIONS_ABOUT, 0, "About"); |
item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo)); |
201 |
item.setIcon(android.R.drawable.ic_menu_info_details); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
202 |
|
|
203 |
return true; |
return true; |
204 |
} |
} |
205 |
|
|
207 |
public boolean onOptionsItemSelected(MenuItem item) { |
public boolean onOptionsItemSelected(MenuItem item) { |
208 |
boolean retval = true; |
boolean retval = true; |
209 |
|
|
210 |
|
//TODO: Cleanup |
211 |
switch (item.getItemId()) { |
switch (item.getItemId()) { |
|
case OPTIONS_RESCAN: |
|
|
startLookup(); |
|
|
break; |
|
|
case OPTIONS_NAMESEARCH: |
|
|
showDialog(DLG_STATIONNAME); |
|
|
break; |
|
|
case OPTIONS_FAVORITES: |
|
|
startFavoriteLookup(); |
|
|
break; |
|
212 |
case OPTIONS_MAP: |
case OPTIONS_MAP: |
213 |
|
|
214 |
Intent intent = new Intent(this,StationMapView.class); |
Intent intent = new Intent(this,StationMapView.class); |
|
intent.putExtra("userlocation", location ); |
|
215 |
|
|
216 |
ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>(); |
ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>(); |
217 |
for (StationBean st : stations ) { |
for (StationBean st : stations ) { |
222 |
|
|
223 |
startActivity(intent); |
startActivity(intent); |
224 |
break; |
break; |
225 |
case OPTIONS_ABOUT: |
case OPTIONS_GPSINFO: |
226 |
String ver = this.getResources().getString(R.string.app_version); |
Location loc = locationLookup.getLocation(); |
|
|
|
|
Location loc = locator.getLocation(); |
|
227 |
StringBuffer message = new StringBuffer(); |
StringBuffer message = new StringBuffer(); |
228 |
message.append("TrainInfo DK v").append(ver).append("\n"); |
message.append( getString(stationlist_locationinfo) ).append(":\n"); |
229 |
message.append("By Torben Nielsen\n"); |
if (loc != null) { |
230 |
message.append("\n"); |
message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n"); |
231 |
message.append("Location info:\n"); |
message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n"); |
232 |
message.append("-Obtained by: ").append(loc != null ? loc.getProvider() : "-").append("\n"); |
message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n"); |
233 |
message.append("-Accuracy: ").append(loc != null ? (int)loc.getAccuracy() : "-").append("m\n"); |
message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n"); |
234 |
|
} else { |
235 |
MessageBox.showMessage(this, message.toString()); |
message.append( getString(stationlist_nolocation) ); |
236 |
|
} |
237 |
|
|
238 |
|
MessageBox.showMessage(this, message.toString(), false); |
239 |
break; |
break; |
240 |
default: |
default: |
241 |
retval = super.onOptionsItemSelected(item); |
retval = super.onOptionsItemSelected(item); |
253 |
|
|
254 |
|
|
255 |
} |
} |
256 |
|
|
257 |
|
public void showMessageAndClose(String message) { |
258 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
259 |
|
builder.setMessage(message) |
260 |
|
.setCancelable(false) |
261 |
|
.setPositiveButton("OK", new DialogInterface.OnClickListener() { |
262 |
|
public void onClick(DialogInterface dialog, int id) { |
263 |
|
dialog.dismiss(); |
264 |
|
StationList.this.finish(); |
265 |
|
} |
266 |
|
}) |
267 |
|
.show(); |
268 |
|
} |
269 |
|
|
270 |
|
|
271 |
|
|
275 |
switch (id) { |
switch (id) { |
276 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
277 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
278 |
dlg.setMessage("Wait for location fix"); |
dlg.setMessage( getString(stationlist_waitforlocation) ); |
279 |
dlg.setCancelable(false); |
dlg.setCancelable(false); |
280 |
return dlg; |
return dlg; |
281 |
case DLG_STATIONNAME: |
case DLG_STATIONNAME: |
285 |
|
|
286 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
287 |
|
|
288 |
builder.setTitle("Station search"); |
builder.setTitle( getString(stationlist_stationsearch) ); |
289 |
builder.setView(rootView); |
builder.setView(rootView); |
290 |
builder.setCancelable(true); |
builder.setCancelable(true); |
291 |
builder.setPositiveButton("Search", new DialogInterface.OnClickListener() { |
builder.setPositiveButton( getString(generic_search), new DialogInterface.OnClickListener() { |
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 |
MessageBox.showMessage(StationList.this, "Two characters minimum" ); |
showMessageAndClose( getString(stationlist_twocharmin) ); |
300 |
} |
} |
301 |
} |
} |
302 |
}); |
}); |
303 |
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
304 |
public void onClick(DialogInterface dialog, int which) { |
public void onClick(DialogInterface dialog, int which) { |
305 |
dialog.dismiss(); |
dialog.dismiss(); |
306 |
|
StationList.this.finish(); // Close this Activity |
307 |
} |
} |
308 |
}); |
}); |
309 |
return builder.create(); |
return builder.create(); |
334 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
335 |
|
|
336 |
StationBean station = stations.get(position); |
StationBean station = stations.get(position); |
|
|
|
|
double latitude = station.getLatitude(); |
|
|
double longitude = station.getLongitude(); |
|
|
|
|
|
|
|
337 |
|
|
338 |
Intent intent = new Intent(this, DepartureList.class); |
if (isLaunchedforShortcut == true) { |
339 |
intent.putExtra("name", station.getName()); |
Intent i = new Intent(); |
340 |
intent.putExtra("distance", station.getDistance()); |
i.putExtra("station", station); |
341 |
intent.putExtra("latitude", latitude); |
setResult(Activity.RESULT_OK, i); |
342 |
intent.putExtra("longitude", longitude); |
finish(); |
343 |
intent.putExtra("stationid", station.getId()); |
} else { |
344 |
intent.putExtra("address", station.getAddress()); |
Intent intent = new Intent(this, DepartureList.class); |
345 |
startActivity(intent); |
intent.putExtra("stationbean", station); |
346 |
|
startActivity(intent); |
347 |
|
} |
348 |
} |
} |
349 |
|
|
350 |
///////////////////////////////////////////////////////////// |
///////////////////////////////////////////////////////////// |
351 |
// |
// |
352 |
|
|
353 |
public void startLookup() { |
public void startLookup() { |
354 |
isRunning = true; |
isRunning = true; |
355 |
dialogMessage = "Wait for location fix"; |
dialogMessage = getString( stationlist_waitforlocation ); |
356 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
357 |
|
|
358 |
locator.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) { |
363 |
dialogMessage = "Finding stations by name"; |
dialogMessage = getString( stationlist_findbyname ); |
364 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
365 |
|
|
366 |
locatorTask = new LocatorTask(); |
findStationsTask = new FindStationsTask(); |
367 |
locatorTask.searchByName(name, locator.getLocation()); |
findStationsTask.searchByName(name); |
368 |
locatorTask.execute(); |
findStationsTask.execute(); |
369 |
|
|
370 |
} |
} |
371 |
|
|
372 |
public void startFavoriteLookup() { |
public void startFavoriteLookup() { |
373 |
|
|
374 |
if (favorites.toString().length() > 0) { |
if (favorites.size() > 0) { |
375 |
dialogMessage = "Loading favorites"; |
dialogMessage = getString( stationlist_loadfavorites ); |
376 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
377 |
|
|
378 |
locatorTask = new LocatorTask(); |
findStationsTask = new FindStationsTask(); |
379 |
locatorTask.searchByIds(favorites.toString(), locator.getLocation()); |
findStationsTask.searchByIds( favorites.toString() ); |
380 |
locatorTask.execute(); |
findStationsTask.execute(); |
381 |
} else { |
} else { |
382 |
MessageBox.showMessage(this, "Favorite list is empty"); |
showMessageAndClose( getString( stationlist_nofavorites ) ); |
383 |
} |
} |
384 |
} |
} |
385 |
|
|
387 |
|
|
388 |
void startLocatorTask() |
void startLocatorTask() |
389 |
{ |
{ |
390 |
dialogMessage = "Finding nearby stations"; |
dialogMessage = getString( stationlist_findingnearby ); |
391 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
392 |
|
|
393 |
locatorTask = new LocatorTask(); |
findStationsTask = new FindStationsTask(); |
394 |
locatorTask.searchByLocation( locator.getLocation() ); |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
395 |
locatorTask.execute(); |
findStationsTask.execute(); |
396 |
} |
} |
|
|
|
|
|
|
|
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); |
|
|
} |
|
397 |
|
|
|
return sb.toString(); |
|
|
} |
|
|
|
|
398 |
|
|
399 |
//////////////////////////////////////////////////////////////////////////// |
//////////////////////////////////////////////////////////////////////////// |
400 |
// Inner classes |
// Inner classes |
408 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
409 |
|
|
410 |
startLocatorTask(); |
startLocatorTask(); |
411 |
location = GeoPair.fromLocation( locator.getLocation() ); |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
412 |
|
|
413 |
break; |
break; |
414 |
|
|
415 |
case NOPROVIDER: |
case NOPROVIDER: |
416 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
417 |
MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps."); |
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) { |
422 |
locator.stopSearch(); |
locationLookup.stopSearch(); |
423 |
if (locator.hasLocation()) { |
if (locationLookup.hasLocation()) { |
424 |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
425 |
} else { |
} else { |
426 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
427 |
|
|
428 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
429 |
builder.setMessage("Location fix timed out"); |
builder.setMessage( getString( stationlist_gpstimeout) ); |
430 |
builder.setCancelable(true); |
builder.setCancelable(true); |
431 |
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
432 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
433 |
dialog.dismiss(); |
dialog.dismiss(); |
434 |
startLookup(); |
startLookup(); |
435 |
|
|
436 |
} |
} |
437 |
}); |
}); |
438 |
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() { |
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 |
} |
} |
451 |
}; |
}; |
452 |
|
|
453 |
|
|
454 |
class LocatorTask 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 |
|
|
485 |
|
|
486 |
@Override |
@Override |
487 |
protected Void doInBackground(Void... params) { |
protected Void doInBackground(Void... params) { |
488 |
|
|
489 |
if (method.equals(LookupMethod.ByLocation)) |
switch (method) { |
490 |
success = stationProvider.lookupStations(loc); |
case ByLocation: |
491 |
|
stations = stationProvider.lookupStations(loc); |
492 |
if (method.equals(LookupMethod.ByName)) |
break; |
493 |
success = stationProvider.lookupStationsByName(name); |
case ByName: |
494 |
|
stations = stationProvider.lookupStationsByName(name); |
495 |
if (method.equals(LookupMethod.ByList)) |
break; |
496 |
success = stationProvider.lookupStationsByIds(ids); |
case ByList: |
497 |
|
stations = stationProvider.lookupStationsByIds(ids); |
498 |
Location dummy = new Location("gps"); |
break; |
499 |
List<StationBean> stations = stationProvider.getStations(); |
default: |
500 |
|
stations = null; // not possible |
501 |
|
} |
502 |
|
|
|
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) ); |
|
|
} |
|
|
} |
|
503 |
|
|
504 |
return null; |
return null; |
505 |
} |
} |
509 |
super.onPostExecute(result); |
super.onPostExecute(result); |
510 |
dialog.dismiss(); |
dialog.dismiss(); |
511 |
|
|
512 |
if (success) { |
|
513 |
if (stationProvider.getStations().size() == 0) |
if (stations != null) { |
514 |
MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! |
if (stations.size() == 0) { |
515 |
stations = stationProvider.getStations(); |
showMessageAndClose(getString(stationlist_nostations)); |
516 |
|
} |
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("Error on finding nearby stations"); |
builder.setMessage(getString(stationlist_fetcherror)); |
525 |
builder.setCancelable(true); |
builder.setCancelable(true); |
526 |
builder.setPositiveButton("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("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 |
} |
} |
585 |
selectedPosition = info.position; |
selectedPosition = info.position; |
586 |
int stationID = stations.get(selectedPosition).getId(); |
int stationID = stations.get(selectedPosition).getId(); |
587 |
|
|
588 |
if (!favorites.hasInt(stationID)) { |
if (!favorites.contains(stationID)) { |
589 |
menu.add(0, FAVORITES_ADD, 0, "Add to favorites"); |
menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) ); |
590 |
} else { |
} else { |
591 |
menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites"); |
menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_removefavorite) ); |
592 |
} |
} |
593 |
|
|
594 |
} |
} |
599 |
int stationID = sb.getId(); |
int stationID = sb.getId(); |
600 |
if (item.getItemId() == FAVORITES_ADD) { |
if (item.getItemId() == FAVORITES_ADD) { |
601 |
favorites.add(stationID); |
favorites.add(stationID); |
602 |
Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show(); |
Toast.makeText(StationList.this, getString(stationlist_stationadded), Toast.LENGTH_SHORT).show(); |
603 |
} else { |
} else { |
604 |
|
|
605 |
favorites.remove(stationID); |
favorites.remove(stationID); |
606 |
Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show(); |
Toast.makeText(StationList.this, getString(stationlist_stationremoved), Toast.LENGTH_SHORT).show(); |
607 |
|
|
608 |
|
|
609 |
|
if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) { |
610 |
|
stations.remove(selectedPosition); |
611 |
|
adapter.notifyDataSetChanged(); |
612 |
|
} |
613 |
} |
} |
614 |
|
Editor ed = prefs.edit(); |
615 |
prefs.edit().putString("favorites", favorites.toString()); |
ed.putString("favorites", favorites.toString()); |
616 |
|
ed.commit(); |
617 |
} |
} |
618 |
} |
} |
619 |
} |
} |