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; |
9 |
import android.app.Dialog; |
import android.app.Dialog; |
10 |
import android.app.ListActivity; |
import android.app.ListActivity; |
11 |
import android.app.ProgressDialog; |
import android.app.ProgressDialog; |
12 |
|
import android.content.DialogInterface; |
13 |
import android.content.Intent; |
import android.content.Intent; |
14 |
import android.location.Address; |
import android.content.SharedPreferences; |
15 |
import android.location.Geocoder; |
import android.content.SharedPreferences.Editor; |
16 |
|
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; |
23 |
|
import android.view.LayoutInflater; |
24 |
|
import android.view.Menu; |
25 |
|
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; |
34 |
|
import dk.thoerup.traininfo.provider.StationProvider; |
35 |
|
import dk.thoerup.traininfo.stationmap.GeoPair; |
36 |
|
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 |
public class TrainInfoList extends ListActivity { |
import static dk.thoerup.traininfo.R.string.*; |
41 |
public static final int GOTLOCATION = 1; |
|
42 |
public static final int GOTSTATIONLIST = 2; |
public class StationList extends ListActivity { |
43 |
public static final int NOPROVIDER = 3; |
public static final int GOTLOCATION = 1001; |
44 |
public static final int FIXTIMEOUT = 4; |
public static final int GOTSTATIONLIST = 1002; |
45 |
public static final int LOOKUPSTATIONFAILED = 5; |
public static final int NOPROVIDER = 1003; |
46 |
|
public static final int LOCATIONFIXTIMEOUT = 1004; |
47 |
|
|
48 |
|
public static final int OPTIONS_MAP = 2003; |
49 |
|
public static final int OPTIONS_GPSINFO = 2004; |
50 |
|
|
51 |
|
public static final int DLG_PROGRESS = 3001; |
52 |
|
public static final int DLG_STATIONNAME = 3002; |
53 |
|
|
54 |
|
|
55 |
|
public static final int GPS_TIMEOUT_MS = 15000; //how long are we willing to wait for gps fix -in milliseconds |
56 |
|
|
|
public static final int DLG_PROGRESS = 1; |
|
57 |
|
|
58 |
/** Called when the activity is first created. */ |
static enum LookupMethod { |
59 |
|
ByLocation, |
60 |
|
ByName, |
61 |
|
ByList, |
62 |
|
MethodNone |
63 |
|
} |
64 |
|
|
65 |
|
|
66 |
|
String dialogMessage = ""; |
67 |
ProgressDialog dialog; |
ProgressDialog dialog; |
68 |
StationLocator locator = null; |
LocationLookup locationLookup = null; |
69 |
LocatorTask locatorTask = new LocatorTask(); |
FindStationsTask findStationsTask; |
70 |
|
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
71 |
|
|
72 |
|
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 |
|
|
78 |
|
StationProvider stationProvider = ProviderFactory.getStationProvider(); |
79 |
|
|
80 |
StationListAdapter adapter = null; |
StationListAdapter adapter = null; |
81 |
|
|
82 |
|
FavoritesMenu contextMenu = new FavoritesMenu(); |
83 |
|
IntSet favorites = new IntSet(); |
84 |
|
|
85 |
|
WelcomeScreen.ListType listType; |
86 |
|
SharedPreferences prefs; |
87 |
|
|
88 |
|
/////////////////////////////////////////////////////////////////////////////////////////// |
89 |
|
//Activity call backs |
90 |
|
|
91 |
@SuppressWarnings("unchecked") |
@SuppressWarnings("unchecked") |
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 |
|
|
|
StationLocator.removeMockLocation(this); |
|
|
//StationLocator.injectMockLocation(this); |
|
97 |
|
|
98 |
adapter = new StationListAdapter(this); |
adapter = new StationListAdapter(this); |
99 |
setListAdapter(adapter); |
setListAdapter(adapter); |
100 |
|
|
101 |
locator = new StationLocator(this, stationsFetched); |
ListView lv = getListView(); |
102 |
|
lv.setOnCreateContextMenuListener(contextMenu); |
103 |
|
|
104 |
|
locationLookup = new LocationLookup(this, stationsFetched); |
105 |
|
|
106 |
|
|
107 |
|
prefs = getSharedPreferences("TrainStation", 0); |
108 |
|
String favoriteString = prefs.getString("favorites", ""); |
109 |
|
if (! favoriteString.equals("") ) { |
110 |
|
favorites.fromString(favoriteString); |
111 |
|
} |
112 |
|
|
113 |
|
listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); |
114 |
|
setTitle(); |
115 |
|
|
116 |
|
isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false); |
117 |
|
|
118 |
|
ProviderFactory.purgeOldEntries(); //cleanup before fetching more data |
119 |
|
|
120 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
121 |
startLookup(); |
|
122 |
|
|
123 |
|
switch (listType) { |
124 |
|
case ListNearest: |
125 |
|
startLookup(); |
126 |
|
break; |
127 |
|
case ListSearch: |
128 |
|
showDialog(DLG_STATIONNAME); |
129 |
|
break; |
130 |
|
case ListFavorites: |
131 |
|
startFavoriteLookup(); |
132 |
|
break; |
133 |
|
default: |
134 |
|
// Not possible !?! |
135 |
|
} |
136 |
|
|
137 |
} else { |
} else { |
138 |
stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations"); |
stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations"); |
139 |
adapter.setStations(stations); |
adapter.setStations(stations); |
140 |
|
location = (GeoPair) savedInstanceState.getSerializable("location"); |
141 |
|
} |
142 |
|
|
143 |
|
} |
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
@Override |
151 |
|
protected void onDestroy() { |
152 |
|
super.onDestroy(); |
153 |
|
|
154 |
|
isRunning = false; |
155 |
|
|
156 |
|
if (locationLookup != null) { |
157 |
|
locationLookup.stopSearch(); |
158 |
|
} |
159 |
|
if (findStationsTask != null) { |
160 |
|
findStationsTask.cancel(true); |
161 |
|
} |
162 |
|
} |
163 |
|
|
164 |
|
|
165 |
|
protected void setTitle() { |
166 |
|
String dialogTitle = getResources().getString(app_name); |
167 |
|
switch (listType) { |
168 |
|
case ListNearest: |
169 |
|
dialogTitle += " - " + getString(stationlist_nearbystations); |
170 |
|
break; |
171 |
|
case ListSearch: |
172 |
|
dialogTitle += " - " + getString(stationlist_search); |
173 |
|
break; |
174 |
|
case ListFavorites: |
175 |
|
dialogTitle += " - " + getString(stationlist_favorites); |
176 |
|
break; |
177 |
|
default: |
178 |
|
dialogTitle = "";//not possible |
179 |
} |
} |
180 |
|
|
181 |
|
setTitle(dialogTitle); |
182 |
|
|
183 |
} |
} |
184 |
|
|
185 |
|
|
186 |
|
|
187 |
@Override |
@Override |
188 |
public void onSaveInstanceState(Bundle outState) |
public void onSaveInstanceState(Bundle outState) |
189 |
{ |
{ |
190 |
if (dialog != null && dialog.isShowing()) |
if (dialog != null && dialog.isShowing()) |
191 |
dialog.dismiss(); |
dialog.dismiss(); |
192 |
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
193 |
|
outState.putSerializable("location", location); |
194 |
|
|
195 |
} |
} |
196 |
|
|
197 |
|
|
198 |
|
|
199 |
@Override |
@Override |
200 |
|
public boolean onCreateOptionsMenu(Menu menu) { |
201 |
|
MenuItem item; |
202 |
|
|
203 |
|
item = menu.add(0, OPTIONS_MAP, 0, getString(stationlist_stationmap)); |
204 |
|
item.setIcon(android.R.drawable.ic_menu_mapmode); |
205 |
|
|
206 |
|
item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo)); |
207 |
|
item.setIcon(android.R.drawable.ic_menu_mapmode); |
208 |
|
|
209 |
|
return true; |
210 |
|
} |
211 |
|
|
212 |
|
@Override |
213 |
|
public boolean onOptionsItemSelected(MenuItem item) { |
214 |
|
boolean retval = true; |
215 |
|
|
216 |
|
//TODO: Cleanup |
217 |
|
switch (item.getItemId()) { |
218 |
|
case OPTIONS_MAP: |
219 |
|
|
220 |
|
Intent intent = new Intent(this,StationMapView.class); |
221 |
|
|
222 |
|
ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>(); |
223 |
|
for (StationBean st : stations ) { |
224 |
|
stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) ); |
225 |
|
} |
226 |
|
|
227 |
|
intent.putExtra("stations", stationPoints); |
228 |
|
|
229 |
|
startActivity(intent); |
230 |
|
break; |
231 |
|
case OPTIONS_GPSINFO: |
232 |
|
Location loc = locationLookup.getLocation(); |
233 |
|
StringBuffer message = new StringBuffer(); |
234 |
|
message.append( getString(stationlist_locationinfo) ).append(":\n"); |
235 |
|
if (loc != null) { |
236 |
|
message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n"); |
237 |
|
message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n"); |
238 |
|
message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n"); |
239 |
|
message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n"); |
240 |
|
} else { |
241 |
|
message.append( getString(stationlist_nolocation) ); |
242 |
|
} |
243 |
|
|
244 |
|
MessageBox.showMessage(this, message.toString(), false); |
245 |
|
break; |
246 |
|
default: |
247 |
|
retval = super.onOptionsItemSelected(item); |
248 |
|
} |
249 |
|
|
250 |
|
return retval; |
251 |
|
} |
252 |
|
|
253 |
|
|
254 |
|
|
255 |
|
@Override |
256 |
|
public boolean onContextItemSelected(MenuItem item) { |
257 |
|
contextMenu.onContextItemSelected(item); |
258 |
|
return true; |
259 |
|
|
260 |
|
|
261 |
|
} |
262 |
|
|
263 |
|
public void showMessageAndClose(String message) { |
264 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
265 |
|
builder.setMessage(message) |
266 |
|
.setCancelable(false) |
267 |
|
.setPositiveButton("OK", new DialogInterface.OnClickListener() { |
268 |
|
public void onClick(DialogInterface dialog, int id) { |
269 |
|
dialog.dismiss(); |
270 |
|
StationList.this.finish(); |
271 |
|
} |
272 |
|
}) |
273 |
|
.show(); |
274 |
|
} |
275 |
|
|
276 |
|
|
277 |
|
|
278 |
|
|
279 |
|
@Override |
280 |
protected Dialog onCreateDialog(int id) { |
protected Dialog onCreateDialog(int id) { |
281 |
switch (id) { |
switch (id) { |
282 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
283 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
284 |
dlg.setMessage("Wait for location fix"); |
dlg.setMessage( getString(stationlist_waitforlocation) ); |
285 |
dlg.setCancelable(false); |
dlg.setCancelable(false); |
286 |
return dlg; |
return dlg; |
287 |
|
case DLG_STATIONNAME: |
288 |
|
LayoutInflater factory = LayoutInflater.from(this); |
289 |
|
final View rootView = factory.inflate(R.layout.textinput, null); |
290 |
|
|
291 |
|
|
292 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
293 |
|
|
294 |
|
builder.setTitle( getString(stationlist_stationsearch) ); |
295 |
|
builder.setView(rootView); |
296 |
|
builder.setCancelable(true); |
297 |
|
builder.setPositiveButton( getString(generic_search), new DialogInterface.OnClickListener() { |
298 |
|
public void onClick(DialogInterface dialog, int which) { |
299 |
|
EditText et = (EditText) rootView.findViewById(R.id.EditText); |
300 |
|
dialog.dismiss(); |
301 |
|
String search = et.getText().toString().trim(); |
302 |
|
if (search.length() >= 2) { |
303 |
|
startNameSearch(search); |
304 |
|
} else { |
305 |
|
showMessageAndClose( getString(stationlist_twocharmin) ); |
306 |
|
} |
307 |
|
} |
308 |
|
}); |
309 |
|
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
310 |
|
public void onClick(DialogInterface dialog, int which) { |
311 |
|
dialog.dismiss(); |
312 |
|
StationList.this.finish(); // Close this Activity |
313 |
|
} |
314 |
|
}); |
315 |
|
return builder.create(); |
316 |
|
|
317 |
default: |
default: |
318 |
return super.onCreateDialog(id); |
return super.onCreateDialog(id); |
319 |
} |
} |
321 |
} |
} |
322 |
|
|
323 |
|
|
|
|
|
324 |
@Override |
@Override |
325 |
protected void onPrepareDialog(int id, Dialog dialog) { |
protected void onPrepareDialog(int id, Dialog dialog) { |
326 |
super.onPrepareDialog(id, dialog); |
super.onPrepareDialog(id, dialog); |
327 |
switch (id) { |
switch (id) { |
328 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
329 |
this.dialog = (ProgressDialog) dialog; |
this.dialog = (ProgressDialog) dialog; |
330 |
|
if (!dialogMessage.equals("")) { |
331 |
|
this.dialog.setMessage(dialogMessage); |
332 |
|
dialogMessage = ""; |
333 |
|
} |
334 |
break; |
break; |
335 |
} |
} |
336 |
} |
} |
337 |
|
|
338 |
|
@Override |
339 |
|
protected void onListItemClick(ListView l, View v, int position, long id) { |
340 |
|
super.onListItemClick(l, v, position, id); |
341 |
|
|
342 |
|
StationBean station = stations.get(position); |
343 |
|
|
344 |
|
if (isLaunchedforShortcut == true) { |
345 |
|
Intent i = new Intent(); |
346 |
|
i.putExtra("station", station); |
347 |
|
setResult(Activity.RESULT_OK, i); |
348 |
|
finish(); |
349 |
|
} else { |
350 |
|
Intent intent = new Intent(this, DepartureList.class); |
351 |
|
intent.putExtra("stationbean", station); |
352 |
|
startActivity(intent); |
353 |
|
} |
354 |
|
} |
355 |
|
|
356 |
|
///////////////////////////////////////////////////////////// |
357 |
|
// |
358 |
|
|
359 |
public void startLookup() { |
public void startLookup() { |
360 |
isRunning = true; |
isRunning = true; |
361 |
|
dialogMessage = getString( stationlist_waitforlocation ); |
362 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
363 |
|
|
364 |
locator.locateStations(); |
locationLookup.locateStations(); |
365 |
stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000); |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS); |
366 |
|
} |
367 |
|
|
368 |
|
void startNameSearch(String name) { |
369 |
|
dialogMessage = getString( stationlist_findbyname ); |
370 |
|
showDialog(DLG_PROGRESS); |
371 |
|
|
372 |
|
findStationsTask = new FindStationsTask(); |
373 |
|
findStationsTask.searchByName(name); |
374 |
|
findStationsTask.execute(); |
375 |
|
|
376 |
|
} |
377 |
|
|
378 |
|
public void startFavoriteLookup() { |
379 |
|
|
380 |
|
if (favorites.size() > 0) { |
381 |
|
dialogMessage = getString( stationlist_loadfavorites ); |
382 |
|
showDialog(DLG_PROGRESS); |
383 |
|
|
384 |
|
findStationsTask = new FindStationsTask(); |
385 |
|
findStationsTask.searchByIds( favorites.toString() ); |
386 |
|
findStationsTask.execute(); |
387 |
|
} else { |
388 |
|
showMessageAndClose( getString( stationlist_nofavorites ) ); |
389 |
|
} |
390 |
} |
} |
391 |
|
|
392 |
|
|
393 |
Handler stationsFetched = new Handler() { |
|
394 |
|
void startLocatorTask() |
395 |
|
{ |
396 |
|
dialogMessage = getString( stationlist_findingnearby ); |
397 |
|
showDialog(DLG_PROGRESS); |
398 |
|
|
399 |
|
findStationsTask = new FindStationsTask(); |
400 |
|
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
401 |
|
findStationsTask.execute(); |
402 |
|
} |
403 |
|
|
404 |
|
|
405 |
|
//////////////////////////////////////////////////////////////////////////// |
406 |
|
// Inner classes |
407 |
|
|
408 |
|
class StationsFetchedHandler extends Handler { |
409 |
@Override |
@Override |
410 |
public void handleMessage(Message msg) { |
public void handleMessage(Message msg) { |
411 |
|
|
412 |
switch (msg.what) { |
switch (msg.what) { |
413 |
case GOTLOCATION: |
case GOTLOCATION: |
414 |
dialog.setMessage("Finding nearby stations"); |
dismissDialog(DLG_PROGRESS); |
415 |
locatorTask.execute(); |
|
416 |
break; |
startLocatorTask(); |
417 |
case GOTSTATIONLIST: |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
418 |
dialog.dismiss(); |
|
|
if (locator.getStations().size() == 0) |
|
|
MessageBox.showMessage(TrainInfoList.this,"Error loading station list!"); |
|
|
stations = locator.getStations(); |
|
|
adapter.setStations( stations ); |
|
419 |
break; |
break; |
420 |
|
|
421 |
case NOPROVIDER: |
case NOPROVIDER: |
422 |
dialog.dismiss(); |
dismissDialog(DLG_PROGRESS); |
423 |
MessageBox.showMessage(TrainInfoList.this,"No location provider enabled. Plase enable gps."); |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); |
424 |
|
//StationList.this.finish(); |
425 |
break; |
break; |
426 |
case FIXTIMEOUT: |
case LOCATIONFIXTIMEOUT: |
|
dialog.dismiss(); |
|
427 |
if (isRunning) { |
if (isRunning) { |
428 |
locator.abortLocationListener(); |
locationLookup.stopSearch(); |
429 |
if (locator.hasLocation()) { |
if (locationLookup.hasLocation()) { |
430 |
msg.what = GOTLOCATION; |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
431 |
handleMessage( msg ); // ToDo: ugly recursive call !!! |
} else { |
432 |
} else { |
dismissDialog(DLG_PROGRESS); |
433 |
MessageBox.showMessage(TrainInfoList.this,"GPS fix timed out"); |
|
434 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
435 |
|
builder.setMessage( getString( stationlist_gpstimeout) ); |
436 |
|
builder.setCancelable(true); |
437 |
|
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
438 |
|
public void onClick(DialogInterface dialog, int id) { |
439 |
|
dialog.dismiss(); |
440 |
|
startLookup(); |
441 |
|
|
442 |
|
} |
443 |
|
}); |
444 |
|
builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() { |
445 |
|
public void onClick(DialogInterface dialog, int id) { |
446 |
|
dialog.dismiss(); |
447 |
|
} |
448 |
|
}); |
449 |
|
builder.show(); |
450 |
|
|
451 |
} |
} |
452 |
} |
} |
453 |
break; |
break; |
|
case LOOKUPSTATIONFAILED: |
|
|
dialog.dismiss(); |
|
|
MessageBox.showMessage(TrainInfoList.this,"Error on finding nearby stations"); |
|
|
break; |
|
454 |
} |
} |
455 |
isRunning = false; |
isRunning = false; |
456 |
} |
} |
457 |
}; |
}; |
|
|
|
|
|
|
|
|
|
|
@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(); |
|
|
String addr = station.getAddress(); |
|
|
|
|
|
if (addr == null || addr.trim().equals("") ) |
|
|
addr = lookupAddress(latitude, longitude); |
|
458 |
|
|
459 |
|
|
460 |
|
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
461 |
|
|
462 |
Intent intent = new Intent(this, DepartureList.class); |
LookupMethod method = LookupMethod.MethodNone; |
463 |
intent.putExtra("name", station.getName()); |
String name; |
464 |
intent.putExtra("address", addr); |
Location loc; |
465 |
intent.putExtra("distance", station.getDistance()); |
String ids; |
|
intent.putExtra("latitude", latitude); |
|
|
intent.putExtra("longitude", longitude); |
|
|
intent.putExtra("code", station.getCode()); |
|
|
startActivity(intent); |
|
|
} |
|
|
|
|
|
String lookupAddress(double latitude, double longitude) { |
|
466 |
|
|
467 |
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)); |
|
|
} |
|
468 |
|
|
469 |
|
method = LookupMethod.ByName; |
470 |
|
name = n; |
471 |
|
} |
472 |
|
|
473 |
|
public void searchByLocation(Location l) { |
474 |
|
method = LookupMethod.ByLocation; |
475 |
|
loc = l; |
476 |
|
} |
477 |
|
|
478 |
|
public void searchByIds(String id) { |
479 |
|
|
480 |
} catch (Exception e) { |
method = LookupMethod.ByList; |
481 |
Log.e("DepartureList", "geocoder failed", e); |
ids = id; |
482 |
} |
} |
483 |
|
|
|
return sb.toString(); |
|
|
} |
|
|
|
|
|
|
|
|
class LocatorTask extends AsyncTask<Void,Void,Void> { |
|
484 |
@Override |
@Override |
485 |
protected void onPreExecute() { |
protected void onPreExecute() { |
486 |
|
|
487 |
|
if (method.equals(LookupMethod.MethodNone)) |
488 |
|
throw new RuntimeException("Method not set"); |
489 |
super.onPreExecute(); |
super.onPreExecute(); |
490 |
} |
} |
491 |
|
|
492 |
@Override |
@Override |
493 |
protected Void doInBackground(Void... params) { |
protected Void doInBackground(Void... params) { |
494 |
locator.findNearestStations(); |
|
495 |
|
switch (method) { |
496 |
|
case ByLocation: |
497 |
|
stations = stationProvider.lookupStations(loc); |
498 |
|
break; |
499 |
|
case ByName: |
500 |
|
stations = stationProvider.lookupStationsByName(name); |
501 |
|
break; |
502 |
|
case ByList: |
503 |
|
stations = stationProvider.lookupStationsByIds(ids); |
504 |
|
break; |
505 |
|
default: |
506 |
|
stations = null; // not possible |
507 |
|
} |
508 |
|
|
509 |
|
|
510 |
return null; |
return null; |
511 |
} |
} |
513 |
@Override |
@Override |
514 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
515 |
super.onPostExecute(result); |
super.onPostExecute(result); |
516 |
|
dialog.dismiss(); |
517 |
|
|
518 |
|
|
519 |
|
if (stations != null) { |
520 |
|
if (stations.size() == 0) { |
521 |
|
showMessageAndClose(getString(stationlist_nostations)); |
522 |
|
} |
523 |
|
|
524 |
|
StationList.this.getListView().invalidateViews(); |
525 |
|
adapter.setStations( stations ); |
526 |
|
|
527 |
|
|
528 |
|
} else { //communication or parse errors |
529 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
530 |
|
builder.setMessage(getString(stationlist_fetcherror)); |
531 |
|
builder.setCancelable(true); |
532 |
|
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
533 |
|
public void onClick(DialogInterface dialog, int id) { |
534 |
|
dialog.dismiss(); |
535 |
|
|
536 |
|
Runnable runner = null; |
537 |
|
switch (method) { |
538 |
|
case ByLocation: |
539 |
|
runner = new Runnable() { |
540 |
|
@Override |
541 |
|
public void run() { |
542 |
|
startLocatorTask(); |
543 |
|
} |
544 |
|
}; |
545 |
|
break; |
546 |
|
case ByName: |
547 |
|
runner = new Runnable() { |
548 |
|
@Override |
549 |
|
public void run() { |
550 |
|
startNameSearch( FindStationsTask.this.name ); |
551 |
|
} |
552 |
|
}; |
553 |
|
break; |
554 |
|
case ByList: |
555 |
|
runner = new Runnable() { |
556 |
|
@Override |
557 |
|
public void run() { |
558 |
|
startFavoriteLookup(); |
559 |
|
} |
560 |
|
}; |
561 |
|
break; |
562 |
|
} |
563 |
|
|
564 |
|
stationsFetched.post( runner ); |
565 |
|
} |
566 |
|
}); |
567 |
|
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
568 |
|
public void onClick(DialogInterface dialog, int id) { |
569 |
|
dialog.dismiss(); |
570 |
|
StationList.this.finish(); |
571 |
|
} |
572 |
|
}); |
573 |
|
|
574 |
|
builder.show(); |
575 |
|
} |
576 |
|
} |
577 |
|
} |
578 |
|
|
579 |
|
|
580 |
|
class FavoritesMenu implements OnCreateContextMenuListener { |
581 |
|
private final static int FAVORITES_ADD = 9001; |
582 |
|
private final static int FAVORITES_REMOVE = 9002; |
583 |
|
|
584 |
|
private int selectedPosition; |
585 |
|
|
586 |
|
|
587 |
|
@Override |
588 |
|
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { |
589 |
|
|
590 |
|
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; |
591 |
|
selectedPosition = info.position; |
592 |
|
int stationID = stations.get(selectedPosition).getId(); |
593 |
|
|
594 |
|
if (!favorites.contains(stationID)) { |
595 |
|
menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) ); |
596 |
|
} else { |
597 |
|
menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_removefavorite) ); |
598 |
|
} |
599 |
|
|
600 |
|
} |
601 |
|
|
602 |
|
public void onContextItemSelected(MenuItem item) { |
603 |
|
StationBean sb = stations.get(selectedPosition); |
604 |
|
|
605 |
|
int stationID = sb.getId(); |
606 |
|
if (item.getItemId() == FAVORITES_ADD) { |
607 |
|
favorites.add(stationID); |
608 |
|
Toast.makeText(StationList.this, getString(stationlist_stationadded), Toast.LENGTH_SHORT).show(); |
609 |
|
} else { |
610 |
|
|
611 |
|
favorites.remove(stationID); |
612 |
|
Toast.makeText(StationList.this, getString(stationlist_stationremoved), Toast.LENGTH_SHORT).show(); |
613 |
|
|
614 |
|
|
615 |
|
if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) { |
616 |
|
stations.remove(selectedPosition); |
617 |
|
adapter.notifyDataSetChanged(); |
618 |
|
} |
619 |
|
} |
620 |
|
Editor ed = prefs.edit(); |
621 |
|
ed.putString("favorites", favorites.toString()); |
622 |
|
ed.commit(); |
623 |
} |
} |
624 |
} |
} |
625 |
} |
} |