2 |
|
|
3 |
import java.util.ArrayList; |
import java.util.ArrayList; |
4 |
import java.util.List; |
import java.util.List; |
5 |
import java.util.Locale; |
|
6 |
|
|
7 |
import android.app.AlertDialog; |
import android.app.AlertDialog; |
8 |
import android.app.Dialog; |
import android.app.Dialog; |
10 |
import android.app.ProgressDialog; |
import android.app.ProgressDialog; |
11 |
import android.content.DialogInterface; |
import android.content.DialogInterface; |
12 |
import android.content.Intent; |
import android.content.Intent; |
13 |
import android.location.Address; |
import android.content.SharedPreferences; |
14 |
import android.location.Geocoder; |
import android.content.SharedPreferences.Editor; |
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; |
23 |
|
import android.view.LayoutInflater; |
24 |
import android.view.Menu; |
import android.view.Menu; |
25 |
import android.view.MenuItem; |
import android.view.MenuItem; |
26 |
import android.view.View; |
import android.view.View; |
27 |
|
import android.view.ContextMenu.ContextMenuInfo; |
28 |
|
import android.view.View.OnCreateContextMenuListener; |
29 |
|
import android.widget.AdapterView; |
30 |
|
import android.widget.EditText; |
31 |
import android.widget.ListView; |
import android.widget.ListView; |
32 |
|
import android.widget.Toast; |
|
|
|
33 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
import dk.thoerup.traininfo.provider.ProviderFactory; |
34 |
import dk.thoerup.traininfo.provider.StationProvider; |
import dk.thoerup.traininfo.provider.StationProvider; |
35 |
import dk.thoerup.traininfo.stationmap.GeoPair; |
import dk.thoerup.traininfo.stationmap.GeoPair; |
36 |
import dk.thoerup.traininfo.stationmap.StationMapView; |
import dk.thoerup.traininfo.stationmap.StationMapView; |
37 |
|
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 |
|
|
48 |
public static final int OPTIONS_MAP = 2001; |
public static final int OPTIONS_MAP = 2003; |
49 |
public static final int OPTIONS_ABOUT = 2002; |
public static final int OPTIONS_GPSINFO = 2004; |
50 |
|
|
51 |
|
|
52 |
public static final int DLG_PROGRESS = 1001; |
|
53 |
|
|
54 |
|
public static final int DLG_PROGRESS = 3001; |
55 |
|
public static final int DLG_STATIONNAME = 3002; |
56 |
|
|
57 |
|
static enum LookupMethod { |
58 |
|
ByLocation, |
59 |
|
ByName, |
60 |
|
ByList, |
61 |
|
MethodNone |
62 |
|
} |
63 |
|
|
64 |
|
|
|
/** Called when the activity is first created. */ |
|
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(); |
70 |
|
|
71 |
|
GeoPair location = new GeoPair(); |
72 |
|
|
73 |
boolean isRunning = false; |
boolean isRunning = false; |
74 |
List<StationBean> stations = new ArrayList<StationBean>(); |
List<StationBean> stations = new ArrayList<StationBean>(); |
75 |
|
|
76 |
StationProvider stationProvider = ProviderFactory.getStationProvider(); |
StationProvider stationProvider = ProviderFactory.getStationProvider(); |
77 |
|
|
78 |
|
StationListAdapter adapter = null; |
79 |
|
|
80 |
|
FavoritesMenu contextMenu = new FavoritesMenu(); |
81 |
|
IntSet favorites = new IntSet(); |
82 |
|
|
83 |
|
WelcomeScreen.ListType listType; |
84 |
|
SharedPreferences prefs; |
85 |
|
|
86 |
|
/////////////////////////////////////////////////////////////////////////////////////////// |
87 |
|
//Activity call backs |
88 |
|
|
|
StationListAdapter adapter = null; |
|
89 |
@SuppressWarnings("unchecked") |
@SuppressWarnings("unchecked") |
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); |
97 |
setListAdapter(adapter); |
setListAdapter(adapter); |
98 |
|
|
99 |
locator = new LocationLookup(this, stationsFetched); |
ListView lv = getListView(); |
100 |
|
lv.setOnCreateContextMenuListener(contextMenu); |
101 |
|
|
102 |
|
locationLookup = new LocationLookup(this, stationsFetched); |
103 |
|
|
104 |
|
|
105 |
|
prefs = getSharedPreferences("TrainStation", 0); |
106 |
|
String favoriteString = prefs.getString("favorites", ""); |
107 |
|
if (! favoriteString.equals("") ) { |
108 |
|
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.showDialog(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"); |
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 |
|
|
160 |
@Override |
@Override |
161 |
public void onSaveInstanceState(Bundle outState) |
public void onSaveInstanceState(Bundle outState) |
162 |
{ |
{ |
163 |
if (dialog != null && dialog.isShowing()) |
if (dialog != null && dialog.isShowing()) |
164 |
dialog.dismiss(); |
dialog.dismiss(); |
165 |
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
166 |
|
outState.putSerializable("location", location); |
167 |
|
|
168 |
} |
} |
169 |
|
|
170 |
|
|
171 |
|
|
172 |
@Override |
@Override |
173 |
public boolean onCreateOptionsMenu(Menu menu) { |
public boolean onCreateOptionsMenu(Menu menu) { |
174 |
menu.add(0, OPTIONS_MAP, 0, "Show station map"); |
MenuItem item; |
175 |
menu.add(0, OPTIONS_ABOUT, 0, "About"); |
|
176 |
|
item = menu.add(0, OPTIONS_MAP, 0, getString(stationlist_stationmap)); |
177 |
|
item.setIcon(android.R.drawable.ic_menu_mapmode); |
178 |
|
|
179 |
|
item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo)); |
180 |
|
item.setIcon(android.R.drawable.ic_menu_mapmode); |
181 |
|
|
182 |
return true; |
return true; |
183 |
} |
} |
184 |
|
|
185 |
@Override |
@Override |
186 |
public boolean onOptionsItemSelected(MenuItem item) { |
public boolean onOptionsItemSelected(MenuItem item) { |
187 |
boolean retval; |
boolean retval = true; |
188 |
|
|
189 |
|
//TODO: Cleanup |
190 |
switch (item.getItemId()) { |
switch (item.getItemId()) { |
191 |
case OPTIONS_MAP: |
case OPTIONS_MAP: |
192 |
|
|
193 |
Intent intent = new Intent(this,StationMapView.class); |
Intent intent = new Intent(this,StationMapView.class); |
|
intent.putExtra("userlocation", GeoPair.fromLocation(locator.getLocation()) ); |
|
194 |
|
|
195 |
ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>(); |
ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>(); |
196 |
for (StationBean st : stations ) { |
for (StationBean st : stations ) { |
200 |
intent.putExtra("stations", stationPoints); |
intent.putExtra("stations", stationPoints); |
201 |
|
|
202 |
startActivity(intent); |
startActivity(intent); |
203 |
retval = true; |
break; |
204 |
|
case OPTIONS_GPSINFO: |
205 |
|
Location loc = locationLookup.getLocation(); |
206 |
|
StringBuffer message = new StringBuffer(); |
207 |
|
message.append( getString(stationlist_locationinfo) ).append(":\n"); |
208 |
|
if (loc != null) { |
209 |
|
message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n"); |
210 |
|
message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n"); |
211 |
|
message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n"); |
212 |
|
message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n"); |
213 |
|
} else { |
214 |
|
message.append( getString(stationlist_nolocation) ); |
215 |
|
} |
216 |
|
|
217 |
|
MessageBox.showMessage(this, message.toString()); |
218 |
break; |
break; |
219 |
default: |
default: |
220 |
retval = super.onOptionsItemSelected(item); |
retval = super.onOptionsItemSelected(item); |
222 |
|
|
223 |
return retval; |
return retval; |
224 |
} |
} |
225 |
|
|
226 |
|
|
227 |
|
|
228 |
|
@Override |
229 |
|
public boolean onContextItemSelected(MenuItem item) { |
230 |
|
contextMenu.onContextItemSelected(item); |
231 |
|
return true; |
232 |
|
|
233 |
|
|
234 |
|
} |
235 |
|
|
236 |
|
public void showMessageAndClose(String message) { |
237 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
238 |
|
builder.setMessage(message) |
239 |
|
.setCancelable(false) |
240 |
|
.setPositiveButton("OK", new DialogInterface.OnClickListener() { |
241 |
|
public void onClick(DialogInterface dialog, int id) { |
242 |
|
dialog.dismiss(); |
243 |
|
StationList.this.finish(); |
244 |
|
} |
245 |
|
}) |
246 |
|
.show(); |
247 |
|
} |
248 |
|
|
249 |
|
|
250 |
|
|
251 |
|
|
252 |
@Override |
@Override |
253 |
protected Dialog onCreateDialog(int id) { |
protected Dialog onCreateDialog(int id) { |
254 |
switch (id) { |
switch (id) { |
255 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
256 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
257 |
dlg.setMessage("Wait for location fix"); |
dlg.setMessage( getString(stationlist_waitforlocation) ); |
258 |
dlg.setCancelable(false); |
dlg.setCancelable(false); |
259 |
return dlg; |
return dlg; |
260 |
|
case DLG_STATIONNAME: |
261 |
|
LayoutInflater factory = LayoutInflater.from(this); |
262 |
|
final View rootView = factory.inflate(R.layout.textinput, null); |
263 |
|
|
264 |
|
|
265 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
266 |
|
|
267 |
|
builder.setTitle( getString(stationlist_stationsearch) ); |
268 |
|
builder.setView(rootView); |
269 |
|
builder.setCancelable(true); |
270 |
|
builder.setPositiveButton( getString(generic_search), new DialogInterface.OnClickListener() { |
271 |
|
public void onClick(DialogInterface dialog, int which) { |
272 |
|
EditText et = (EditText) rootView.findViewById(R.id.EditText); |
273 |
|
dialog.dismiss(); |
274 |
|
if (et.getText().toString().length() >= 2) { |
275 |
|
startNameSearch(et.getText().toString()); |
276 |
|
} else { |
277 |
|
showMessageAndClose( getString(stationlist_twocharmin) ); |
278 |
|
} |
279 |
|
} |
280 |
|
}); |
281 |
|
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
282 |
|
public void onClick(DialogInterface dialog, int which) { |
283 |
|
dialog.dismiss(); |
284 |
|
StationList.this.finish(); // Close this Activity |
285 |
|
} |
286 |
|
}); |
287 |
|
return builder.create(); |
288 |
|
|
289 |
default: |
default: |
290 |
return super.onCreateDialog(id); |
return super.onCreateDialog(id); |
291 |
} |
} |
293 |
} |
} |
294 |
|
|
295 |
|
|
|
|
|
296 |
@Override |
@Override |
297 |
protected void onPrepareDialog(int id, Dialog dialog) { |
protected void onPrepareDialog(int id, Dialog dialog) { |
298 |
super.onPrepareDialog(id, dialog); |
super.onPrepareDialog(id, dialog); |
306 |
break; |
break; |
307 |
} |
} |
308 |
} |
} |
309 |
|
|
310 |
|
@Override |
311 |
|
protected void onListItemClick(ListView l, View v, int position, long id) { |
312 |
|
super.onListItemClick(l, v, position, id); |
313 |
|
|
314 |
|
StationBean station = stations.get(position); |
315 |
|
|
316 |
|
Intent intent = new Intent(this, DepartureList.class); |
317 |
|
intent.putExtra("stationbean", station); |
318 |
|
startActivity(intent); |
319 |
|
} |
320 |
|
|
321 |
|
///////////////////////////////////////////////////////////// |
322 |
|
// |
323 |
|
|
324 |
public void startLookup() { |
public void startLookup() { |
325 |
isRunning = true; |
isRunning = true; |
326 |
|
dialogMessage = getString( stationlist_waitforlocation ); |
327 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
328 |
|
|
329 |
locator.locateStations(); |
locationLookup.locateStations(); |
330 |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); |
331 |
} |
} |
332 |
|
|
333 |
|
void startNameSearch(String name) { |
334 |
|
dialogMessage = getString( stationlist_findbyname ); |
335 |
|
showDialog(DLG_PROGRESS); |
336 |
|
|
337 |
|
findStationsTask = new FindStationsTask(); |
338 |
|
findStationsTask.searchByName(name); |
339 |
|
findStationsTask.execute(); |
340 |
|
|
341 |
|
} |
342 |
|
|
343 |
|
public void startFavoriteLookup() { |
344 |
|
|
345 |
|
if (favorites.size() > 0) { |
346 |
|
dialogMessage = getString( stationlist_loadfavorites ); |
347 |
|
showDialog(DLG_PROGRESS); |
348 |
|
|
349 |
|
findStationsTask = new FindStationsTask(); |
350 |
|
findStationsTask.searchByIds( favorites.toString() ); |
351 |
|
findStationsTask.execute(); |
352 |
|
} else { |
353 |
|
showMessageAndClose( getString( stationlist_nofavorites ) ); |
354 |
|
} |
355 |
|
} |
356 |
|
|
357 |
|
|
358 |
|
|
359 |
|
void startLocatorTask() |
360 |
|
{ |
361 |
|
dialogMessage = getString( stationlist_findingnearby ); |
362 |
|
showDialog(DLG_PROGRESS); |
363 |
|
|
364 |
|
findStationsTask = new FindStationsTask(); |
365 |
|
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
366 |
|
findStationsTask.execute(); |
367 |
|
} |
368 |
|
|
369 |
|
|
370 |
|
//////////////////////////////////////////////////////////////////////////// |
371 |
|
// Inner classes |
372 |
|
|
373 |
Handler stationsFetched = new Handler() { |
class StationsFetchedHandler extends Handler { |
374 |
@Override |
@Override |
375 |
public void handleMessage(Message msg) { |
public void handleMessage(Message msg) { |
376 |
|
|
379 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
380 |
|
|
381 |
startLocatorTask(); |
startLocatorTask(); |
382 |
|
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
383 |
|
|
384 |
break; |
break; |
385 |
|
|
386 |
case NOPROVIDER: |
case NOPROVIDER: |
387 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
388 |
MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps."); |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) ); |
389 |
break; |
break; |
390 |
case LOCATIONFIXTIMEOUT: |
case LOCATIONFIXTIMEOUT: |
391 |
if (isRunning) { |
if (isRunning) { |
392 |
locator.stopSearch(); |
locationLookup.stopSearch(); |
393 |
if (locator.hasLocation()) { |
if (locationLookup.hasLocation()) { |
394 |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
395 |
} else { |
} else { |
396 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
397 |
|
|
398 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
399 |
builder.setMessage("GPS fix timed out"); |
builder.setMessage( getString( stationlist_gpstimeout) ); |
400 |
builder.setCancelable(true); |
builder.setCancelable(true); |
401 |
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
402 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
403 |
dialog.dismiss(); |
dialog.dismiss(); |
404 |
startLookup(); |
startLookup(); |
405 |
|
|
406 |
} |
} |
407 |
}); |
}); |
408 |
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() { |
409 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
410 |
dialog.dismiss(); |
dialog.dismiss(); |
411 |
} |
} |
419 |
isRunning = false; |
isRunning = false; |
420 |
} |
} |
421 |
}; |
}; |
|
|
|
|
void startLocatorTask() |
|
|
{ |
|
|
dialogMessage = "Finding nearby stations"; |
|
|
showDialog(DLG_PROGRESS); |
|
|
|
|
|
locatorTask = new LocatorTask(); |
|
|
locatorTask.execute(); |
|
|
} |
|
|
|
|
|
@Override |
|
|
protected void onListItemClick(ListView l, View v, int position, long id) { |
|
|
super.onListItemClick(l, v, position, id); |
|
|
|
|
|
StationBean station = stations.get(position); |
|
|
|
|
|
double latitude = station.getLatitude(); |
|
|
double longitude = station.getLongitude(); |
|
|
|
|
422 |
|
|
423 |
|
|
424 |
|
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
425 |
|
|
426 |
Intent intent = new Intent(this, DepartureList.class); |
LookupMethod method = LookupMethod.MethodNone; |
427 |
intent.putExtra("name", station.getName()); |
boolean success; |
428 |
intent.putExtra("distance", station.getDistance()); |
String name; |
429 |
intent.putExtra("latitude", latitude); |
Location loc; |
430 |
intent.putExtra("longitude", longitude); |
String ids; |
|
intent.putExtra("stationid", station.getId()); |
|
|
intent.putExtra("address", station.getAddress()); |
|
|
startActivity(intent); |
|
|
} |
|
|
|
|
|
String lookupAddress(double latitude, double longitude) { |
|
431 |
|
|
432 |
Geocoder coder = new Geocoder(this, new Locale("da")); |
public void searchByName(String n) { |
|
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)); |
|
|
} |
|
433 |
|
|
434 |
|
method = LookupMethod.ByName; |
435 |
|
name = n; |
436 |
|
} |
437 |
|
|
438 |
|
public void searchByLocation(Location l) { |
439 |
|
method = LookupMethod.ByLocation; |
440 |
|
loc = l; |
441 |
|
} |
442 |
|
|
443 |
|
public void searchByIds(String id) { |
444 |
|
|
445 |
} catch (Exception e) { |
method = LookupMethod.ByList; |
446 |
Log.e("DepartureList", "geocoder failed", e); |
ids = id; |
447 |
} |
} |
448 |
|
|
|
return sb.toString(); |
|
|
} |
|
|
|
|
|
class LocatorTask extends AsyncTask<Void,Void,Void> { |
|
|
boolean success; |
|
449 |
@Override |
@Override |
450 |
protected void onPreExecute() { |
protected void onPreExecute() { |
451 |
|
|
452 |
|
if (method.equals(LookupMethod.MethodNone)) |
453 |
|
throw new RuntimeException("Method not set"); |
454 |
super.onPreExecute(); |
super.onPreExecute(); |
455 |
} |
} |
456 |
|
|
457 |
@Override |
@Override |
458 |
protected Void doInBackground(Void... params) { |
protected Void doInBackground(Void... params) { |
459 |
Location loc = locator.getLocation(); |
|
460 |
success = stationProvider.lookupStations(loc); |
switch (method) { |
461 |
|
case ByLocation: |
462 |
|
success = stationProvider.lookupStations(loc); |
463 |
List<StationBean> stations = stationProvider.getStations(); |
break; |
464 |
for (StationBean station : stations) { |
case ByName: |
465 |
String addr = lookupAddress(station.getLatitude(), station.getLongitude()); |
success = stationProvider.lookupStationsByName(name); |
466 |
station.setAddress(addr); |
break; |
467 |
|
case ByList: |
468 |
|
success = stationProvider.lookupStationsByIds(ids); |
469 |
|
break; |
470 |
|
default: |
471 |
|
success = false; // not possible |
472 |
} |
} |
473 |
|
|
474 |
|
|
475 |
return null; |
return null; |
476 |
} |
} |
477 |
|
|
480 |
super.onPostExecute(result); |
super.onPostExecute(result); |
481 |
dialog.dismiss(); |
dialog.dismiss(); |
482 |
|
|
483 |
|
|
484 |
if (success) { |
if (success) { |
485 |
if (stationProvider.getStations().size() == 0) |
if (stationProvider.getStations().size() == 0) { |
486 |
MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! |
showMessageAndClose(getString(stationlist_nostations)); |
487 |
|
} |
488 |
stations = stationProvider.getStations(); |
stations = stationProvider.getStations(); |
489 |
adapter.setStations( stations ); |
adapter.setStations( stations ); |
490 |
|
|
491 |
} else { //communication or parse errors |
} else { //communication or parse errors |
492 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
493 |
builder.setMessage("Error on finding nearby stations"); |
builder.setMessage(getString(stationlist_nearbyerror)); |
494 |
builder.setCancelable(true); |
builder.setCancelable(true); |
495 |
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
496 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
497 |
dialog.dismiss(); |
dialog.dismiss(); |
498 |
|
|
504 |
}); |
}); |
505 |
} |
} |
506 |
}); |
}); |
507 |
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
508 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
509 |
dialog.dismiss(); |
dialog.dismiss(); |
510 |
} |
} |
511 |
}); |
}); |
512 |
builder.show(); |
try { |
513 |
|
builder.show(); |
514 |
|
} catch (android.view.WindowManager.BadTokenException e) { |
515 |
|
Log.i("StationList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |
516 |
|
} |
517 |
|
} |
518 |
|
} |
519 |
|
} |
520 |
|
|
521 |
|
|
522 |
|
class FavoritesMenu implements OnCreateContextMenuListener { |
523 |
|
private final static int FAVORITES_ADD = 9001; |
524 |
|
private final static int FAVORITES_REMOVE = 9002; |
525 |
|
|
526 |
|
private int selectedPosition; |
527 |
|
|
528 |
|
|
529 |
|
@Override |
530 |
|
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { |
531 |
|
|
532 |
|
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; |
533 |
|
selectedPosition = info.position; |
534 |
|
int stationID = stations.get(selectedPosition).getId(); |
535 |
|
|
536 |
|
if (!favorites.contains(stationID)) { |
537 |
|
menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) ); |
538 |
|
} else { |
539 |
|
menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_removefavorite) ); |
540 |
|
} |
541 |
|
|
542 |
|
} |
543 |
|
|
544 |
|
public void onContextItemSelected(MenuItem item) { |
545 |
|
StationBean sb = stations.get(selectedPosition); |
546 |
|
|
547 |
|
int stationID = sb.getId(); |
548 |
|
if (item.getItemId() == FAVORITES_ADD) { |
549 |
|
favorites.add(stationID); |
550 |
|
Toast.makeText(StationList.this, getString(stationlist_stationadded), Toast.LENGTH_SHORT).show(); |
551 |
|
} else { |
552 |
|
|
553 |
|
favorites.remove(stationID); |
554 |
|
Toast.makeText(StationList.this, getString(stationlist_stationremoved), Toast.LENGTH_SHORT).show(); |
555 |
|
|
556 |
|
|
557 |
|
if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) { |
558 |
|
stations.remove(selectedPosition); |
559 |
|
adapter.notifyDataSetChanged(); |
560 |
|
} |
561 |
} |
} |
562 |
|
Editor ed = prefs.edit(); |
563 |
|
ed.putString("favorites", favorites.toString()); |
564 |
|
ed.commit(); |
565 |
} |
} |
566 |
} |
} |
567 |
} |
} |