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