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.content.SharedPreferences; |
14 |
|
import android.content.SharedPreferences.Editor; |
15 |
import android.location.Address; |
import android.location.Address; |
16 |
import android.location.Geocoder; |
import android.location.Geocoder; |
17 |
import android.location.Location; |
import android.location.Location; |
20 |
import android.os.Handler; |
import android.os.Handler; |
21 |
import android.os.Message; |
import android.os.Message; |
22 |
import android.util.Log; |
import android.util.Log; |
23 |
|
import android.view.ContextMenu; |
24 |
import android.view.LayoutInflater; |
import android.view.LayoutInflater; |
25 |
import android.view.Menu; |
import android.view.Menu; |
26 |
import android.view.MenuItem; |
import android.view.MenuItem; |
27 |
import android.view.View; |
import android.view.View; |
28 |
|
import android.view.ContextMenu.ContextMenuInfo; |
29 |
|
import android.view.View.OnCreateContextMenuListener; |
30 |
|
import android.widget.AdapterView; |
31 |
import android.widget.EditText; |
import android.widget.EditText; |
32 |
import android.widget.ListView; |
import android.widget.ListView; |
33 |
|
import android.widget.Toast; |
34 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
import dk.thoerup.traininfo.provider.ProviderFactory; |
35 |
import dk.thoerup.traininfo.provider.StationProvider; |
import dk.thoerup.traininfo.provider.StationProvider; |
36 |
import dk.thoerup.traininfo.stationmap.GeoPair; |
import dk.thoerup.traininfo.stationmap.GeoPair; |
37 |
import dk.thoerup.traininfo.stationmap.StationMapView; |
import dk.thoerup.traininfo.stationmap.StationMapView; |
38 |
|
import dk.thoerup.traininfo.util.IntSet; |
39 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
40 |
|
|
41 |
public class StationList extends ListActivity { |
public class StationList extends ListActivity { |
44 |
public static final int NOPROVIDER = 1003; |
public static final int NOPROVIDER = 1003; |
45 |
public static final int LOCATIONFIXTIMEOUT = 1004; |
public static final int LOCATIONFIXTIMEOUT = 1004; |
46 |
|
|
|
public static final int OPTIONS_RESCAN = 2001; |
|
|
public static final int OPTIONS_NAMESEARCH = 2002; |
|
47 |
public static final int OPTIONS_MAP = 2003; |
public static final int OPTIONS_MAP = 2003; |
48 |
public static final int OPTIONS_ABOUT = 2004; |
public static final int OPTIONS_GPSINFO = 2004; |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
public static final int DLG_PROGRESS = 3001; |
public static final int DLG_PROGRESS = 3001; |
54 |
public static final int DLG_STATIONNAME = 3002; |
public static final int DLG_STATIONNAME = 3002; |
55 |
|
|
56 |
/** Called when the activity is first created. */ |
static enum LookupMethod { |
57 |
|
ByLocation, |
58 |
|
ByName, |
59 |
|
ByList, |
60 |
|
MethodNone |
61 |
|
} |
62 |
|
|
63 |
|
|
64 |
String dialogMessage = ""; |
String dialogMessage = ""; |
65 |
ProgressDialog dialog; |
ProgressDialog dialog; |
66 |
LocationLookup locator = null; |
LocationLookup locator = null; |
67 |
LocatorTask locatorTask; |
FindStationsTask findStationsTask; |
68 |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
69 |
|
|
70 |
GeoPair location = new GeoPair(); |
GeoPair location = new GeoPair(); |
76 |
|
|
77 |
StationListAdapter adapter = null; |
StationListAdapter adapter = null; |
78 |
|
|
79 |
static enum LookupMethod { |
FavoritesMenu contextMenu = new FavoritesMenu(); |
80 |
ByLocation, |
IntSet favorites = new IntSet(); |
81 |
ByName, |
|
82 |
MethodNone |
WelcomeScreen.ListType listType; |
83 |
} |
String dialogTitle; |
84 |
|
SharedPreferences prefs; |
85 |
|
|
86 |
/////////////////////////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////////////////////////// |
87 |
//Activity call backs |
//Activity call backs |
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 |
|
ListView lv = getListView(); |
100 |
|
lv.setOnCreateContextMenuListener(contextMenu); |
101 |
|
|
102 |
locator = new LocationLookup(this, stationsFetched); |
locator = 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 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
112 |
startLookup(); |
listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); |
113 |
|
switch (listType) { |
114 |
|
case ListNearest: |
115 |
|
startLookup(); |
116 |
|
break; |
117 |
|
case ListSearch: |
118 |
|
this.showDialog(DLG_STATIONNAME); |
119 |
|
break; |
120 |
|
case ListFavorites: |
121 |
|
startFavoriteLookup(); |
122 |
|
break; |
123 |
|
default: |
124 |
|
// Not possible !?! |
125 |
|
} |
126 |
|
|
127 |
} else { |
} else { |
128 |
stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations"); |
stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations"); |
129 |
adapter.setStations(stations); |
adapter.setStations(stations); |
130 |
location = (GeoPair) savedInstanceState.getSerializable("location"); |
location = (GeoPair) savedInstanceState.getSerializable("location"); |
131 |
|
dialogTitle = savedInstanceState.getString("title"); |
132 |
|
setTitle(dialogTitle); |
133 |
} |
} |
134 |
} |
} |
135 |
|
|
141 |
dialog.dismiss(); |
dialog.dismiss(); |
142 |
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
143 |
outState.putSerializable("location", location); |
outState.putSerializable("location", location); |
144 |
|
outState.putString("title", dialogTitle); |
145 |
} |
} |
146 |
|
|
147 |
|
|
148 |
|
|
149 |
@Override |
@Override |
150 |
public boolean onCreateOptionsMenu(Menu menu) { |
public boolean onCreateOptionsMenu(Menu menu) { |
151 |
menu.add(0, OPTIONS_RESCAN, 0, "Find nearest stations"); |
MenuItem item; |
152 |
menu.add(0, OPTIONS_NAMESEARCH, 0, "Search for station"); |
|
153 |
menu.add(0, OPTIONS_MAP, 0, "Show station map"); |
item = menu.add(0, OPTIONS_MAP, 0, "Station map"); |
154 |
menu.add(0, OPTIONS_ABOUT, 0, "About"); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
155 |
|
|
156 |
|
item = menu.add(0, OPTIONS_GPSINFO, 0, "GPS Info"); |
157 |
|
item.setIcon(android.R.drawable.ic_menu_mapmode); |
158 |
|
|
159 |
return true; |
return true; |
160 |
} |
} |
161 |
|
|
163 |
public boolean onOptionsItemSelected(MenuItem item) { |
public boolean onOptionsItemSelected(MenuItem item) { |
164 |
boolean retval = true; |
boolean retval = true; |
165 |
|
|
166 |
|
//TODO: Cleanup |
167 |
switch (item.getItemId()) { |
switch (item.getItemId()) { |
|
case OPTIONS_RESCAN: |
|
|
startLookup(); |
|
|
break; |
|
|
case OPTIONS_NAMESEARCH: |
|
|
showDialog(DLG_STATIONNAME); |
|
|
break; |
|
168 |
case OPTIONS_MAP: |
case OPTIONS_MAP: |
169 |
|
|
170 |
Intent intent = new Intent(this,StationMapView.class); |
Intent intent = new Intent(this,StationMapView.class); |
179 |
|
|
180 |
startActivity(intent); |
startActivity(intent); |
181 |
break; |
break; |
182 |
case OPTIONS_ABOUT: |
case OPTIONS_GPSINFO: |
183 |
String ver = this.getResources().getString(R.string.app_version); |
Location loc = locator.getLocation(); |
|
|
|
184 |
StringBuffer message = new StringBuffer(); |
StringBuffer message = new StringBuffer(); |
185 |
message.append("TrainInfo DK v").append(ver).append("\n"); |
message.append("Location info:\n"); |
186 |
message.append("By Torben Nielsen\n"); |
message.append("-Obtained by: ").append(loc != null ? loc.getProvider() : "-").append("\n"); |
187 |
|
message.append("-Accuracy: ").append(loc != null ? (int)loc.getAccuracy() : "-").append("m\n"); |
188 |
|
|
189 |
MessageBox.showMessage(this, message.toString()); |
MessageBox.showMessage(this, message.toString()); |
190 |
break; |
break; |
194 |
|
|
195 |
return retval; |
return retval; |
196 |
} |
} |
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
@Override |
201 |
|
public boolean onContextItemSelected(MenuItem item) { |
202 |
|
contextMenu.onContextItemSelected(item); |
203 |
|
return true; |
204 |
|
|
205 |
|
|
206 |
|
} |
207 |
|
|
208 |
|
|
209 |
|
|
210 |
|
|
211 |
@Override |
@Override |
212 |
protected Dialog onCreateDialog(int id) { |
protected Dialog onCreateDialog(int id) { |
233 |
if (et.getText().toString().length() >= 2) { |
if (et.getText().toString().length() >= 2) { |
234 |
startNameSearch(et.getText().toString()); |
startNameSearch(et.getText().toString()); |
235 |
} else { |
} else { |
236 |
MessageBox.showMessage(StationList.this, "To characters, minimum" ); |
MessageBox.showMessage(StationList.this, "Two characters minimum" ); |
237 |
} |
} |
238 |
} |
} |
239 |
}); |
}); |
302 |
dialogMessage = "Finding stations by name"; |
dialogMessage = "Finding stations by name"; |
303 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
304 |
|
|
305 |
locatorTask = new LocatorTask(); |
findStationsTask = new FindStationsTask(); |
306 |
locatorTask.searchByName(name, locator.getLocation()); |
findStationsTask.searchByName(name, locator.getLocation()); |
307 |
locatorTask.execute(); |
findStationsTask.execute(); |
308 |
|
|
309 |
} |
} |
310 |
|
|
311 |
|
public void startFavoriteLookup() { |
312 |
|
|
313 |
|
if (favorites.size() > 0) { |
314 |
|
dialogMessage = "Loading favorites"; |
315 |
|
showDialog(DLG_PROGRESS); |
316 |
|
|
317 |
|
findStationsTask = new FindStationsTask(); |
318 |
|
findStationsTask.searchByIds(favorites.toString(), locator.getLocation()); |
319 |
|
findStationsTask.execute(); |
320 |
|
} else { |
321 |
|
MessageBox.showMessage(this, "Favorite list is empty"); |
322 |
|
} |
323 |
|
} |
324 |
|
|
325 |
|
|
326 |
|
|
329 |
dialogMessage = "Finding nearby stations"; |
dialogMessage = "Finding nearby stations"; |
330 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
331 |
|
|
332 |
locatorTask = new LocatorTask(); |
findStationsTask = new FindStationsTask(); |
333 |
locatorTask.searchByLocation( locator.getLocation() ); |
findStationsTask.searchByLocation( locator.getLocation() ); |
334 |
locatorTask.execute(); |
findStationsTask.execute(); |
335 |
} |
} |
336 |
|
|
337 |
|
|
391 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
392 |
|
|
393 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
394 |
builder.setMessage("GPS fix timed out"); |
builder.setMessage("Location fix timed out"); |
395 |
builder.setCancelable(true); |
builder.setCancelable(true); |
396 |
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
397 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
416 |
}; |
}; |
417 |
|
|
418 |
|
|
419 |
class LocatorTask extends AsyncTask<Void,Void,Void> { |
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
420 |
|
|
421 |
LookupMethod method = LookupMethod.MethodNone; |
LookupMethod method = LookupMethod.MethodNone; |
422 |
boolean success; |
boolean success; |
423 |
String name; |
String name; |
424 |
Location loc; |
Location loc; |
425 |
|
String ids; |
426 |
|
|
427 |
public void searchByName(String n, Location l) { |
public void searchByName(String n, Location l) { |
428 |
|
|
436 |
loc = l; |
loc = l; |
437 |
} |
} |
438 |
|
|
439 |
|
public void searchByIds(String id, Location l) { |
440 |
|
|
441 |
|
method = LookupMethod.ByList; |
442 |
|
loc = l; |
443 |
|
ids = id; |
444 |
|
} |
445 |
|
|
446 |
@Override |
@Override |
447 |
protected void onPreExecute() { |
protected void onPreExecute() { |
448 |
|
|
453 |
|
|
454 |
@Override |
@Override |
455 |
protected Void doInBackground(Void... params) { |
protected Void doInBackground(Void... params) { |
456 |
|
|
457 |
if (method.equals(LookupMethod.ByLocation)) |
switch (method) { |
458 |
|
case ByLocation: |
459 |
success = stationProvider.lookupStations(loc); |
success = stationProvider.lookupStations(loc); |
460 |
|
break; |
461 |
|
case ByName: |
462 |
|
success = stationProvider.lookupStationsByName(name); |
463 |
|
break; |
464 |
|
case ByList: |
465 |
|
success = stationProvider.lookupStationsByIds(ids); |
466 |
|
break; |
467 |
|
default: |
468 |
|
success = false; // not possible |
469 |
|
} |
470 |
|
|
|
if (method.equals(LookupMethod.ByName)) |
|
|
success = stationProvider.lookupStations(name); |
|
471 |
|
|
472 |
Location dummy = new Location("gps"); |
Location dummy = new Location("gps"); |
473 |
List<StationBean> stations = stationProvider.getStations(); |
List<StationBean> stations = stationProvider.getStations(); |
476 |
String addr = lookupAddress(station.getLatitude(), station.getLongitude()); |
String addr = lookupAddress(station.getLatitude(), station.getLongitude()); |
477 |
station.setAddress(addr); |
station.setAddress(addr); |
478 |
|
|
479 |
if (method.equals(LookupMethod.ByName) ) { |
|
480 |
dummy.setLatitude(station.getLatitude()); |
if (method.equals(LookupMethod.ByName) || method.equals(LookupMethod.ByList)) { |
481 |
dummy.setLongitude(station.getLongitude()); |
if (loc != null) { //only do the distance calc if we have a location |
482 |
station.setDistance( (int)loc.distanceTo(dummy) ); |
dummy.setLatitude(station.getLatitude()); |
483 |
|
dummy.setLongitude(station.getLongitude()); |
484 |
|
station.setDistance( (int)loc.distanceTo(dummy) ); |
485 |
|
} else { |
486 |
|
station.setDistance(0); |
487 |
|
} |
488 |
} |
} |
489 |
|
|
490 |
} |
} |
491 |
|
|
492 |
return null; |
return null; |
497 |
super.onPostExecute(result); |
super.onPostExecute(result); |
498 |
dialog.dismiss(); |
dialog.dismiss(); |
499 |
|
|
500 |
|
//set title |
501 |
|
switch (method) { |
502 |
|
case ByLocation: |
503 |
|
dialogTitle = "Traininfo DK - Nearby stations"; |
504 |
|
break; |
505 |
|
case ByName: |
506 |
|
dialogTitle = "Traininfo DK - Search"; |
507 |
|
break; |
508 |
|
case ByList: |
509 |
|
dialogTitle = "Traininfo DK - Favorites"; |
510 |
|
break; |
511 |
|
default: |
512 |
|
dialogTitle = "";//not possible |
513 |
|
} |
514 |
|
|
515 |
|
StationList.this.setTitle(dialogTitle); |
516 |
|
//set title end |
517 |
|
|
518 |
if (success) { |
if (success) { |
519 |
if (stationProvider.getStations().size() == 0) |
if (stationProvider.getStations().size() == 0) |
520 |
MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! |
MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! |
546 |
} |
} |
547 |
} |
} |
548 |
} |
} |
549 |
|
|
550 |
|
|
551 |
|
class FavoritesMenu implements OnCreateContextMenuListener { |
552 |
|
private final static int FAVORITES_ADD = 9001; |
553 |
|
private final static int FAVORITES_REMOVE = 9002; |
554 |
|
|
555 |
|
private int selectedPosition; |
556 |
|
|
557 |
|
|
558 |
|
@Override |
559 |
|
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { |
560 |
|
|
561 |
|
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; |
562 |
|
selectedPosition = info.position; |
563 |
|
int stationID = stations.get(selectedPosition).getId(); |
564 |
|
|
565 |
|
if (!favorites.contains(stationID)) { |
566 |
|
menu.add(0, FAVORITES_ADD, 0, "Add to favorites"); |
567 |
|
} else { |
568 |
|
menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites"); |
569 |
|
} |
570 |
|
|
571 |
|
} |
572 |
|
|
573 |
|
public void onContextItemSelected(MenuItem item) { |
574 |
|
StationBean sb = stations.get(selectedPosition); |
575 |
|
|
576 |
|
int stationID = sb.getId(); |
577 |
|
if (item.getItemId() == FAVORITES_ADD) { |
578 |
|
favorites.add(stationID); |
579 |
|
Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show(); |
580 |
|
} else { |
581 |
|
|
582 |
|
favorites.remove(stationID); |
583 |
|
Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show(); |
584 |
|
|
585 |
|
|
586 |
|
if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) { |
587 |
|
stations.remove(selectedPosition); |
588 |
|
adapter.notifyDataSetChanged(); |
589 |
|
} |
590 |
|
} |
591 |
|
Editor ed = prefs.edit(); |
592 |
|
ed.putString("favorites", favorites.toString()); |
593 |
|
ed.commit(); |
594 |
|
} |
595 |
|
} |
596 |
} |
} |