3 |
import java.util.ArrayList; |
import java.util.ArrayList; |
4 |
import java.util.List; |
import java.util.List; |
5 |
import java.util.Locale; |
import java.util.Locale; |
|
import java.util.prefs.Preferences; |
|
6 |
|
|
7 |
import android.app.AlertDialog; |
import android.app.AlertDialog; |
8 |
import android.app.Dialog; |
import android.app.Dialog; |
11 |
import android.content.DialogInterface; |
import android.content.DialogInterface; |
12 |
import android.content.Intent; |
import android.content.Intent; |
13 |
import android.content.SharedPreferences; |
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; |
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 |
public static final int OPTIONS_FAVORITES = 2005; |
|
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 locationLookup = 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(); |
78 |
|
|
79 |
FavoritesMenu contextMenu = new FavoritesMenu(); |
FavoritesMenu contextMenu = new FavoritesMenu(); |
80 |
IntSet favorites = new IntSet(); |
IntSet favorites = new IntSet(); |
81 |
|
|
82 |
static enum LookupMethod { |
WelcomeScreen.ListType listType; |
|
ByLocation, |
|
|
ByName, |
|
|
ByList, |
|
|
MethodNone |
|
|
} |
|
|
|
|
83 |
SharedPreferences prefs; |
SharedPreferences prefs; |
84 |
|
|
85 |
/////////////////////////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////////////////////////// |
89 |
@Override |
@Override |
90 |
public void onCreate(Bundle savedInstanceState) { |
public void onCreate(Bundle savedInstanceState) { |
91 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
92 |
setContentView(R.layout.main); |
setContentView(R.layout.stationlist); |
93 |
|
|
94 |
|
|
95 |
adapter = new StationListAdapter(this); |
adapter = new StationListAdapter(this); |
98 |
ListView lv = getListView(); |
ListView lv = getListView(); |
99 |
lv.setOnCreateContextMenuListener(contextMenu); |
lv.setOnCreateContextMenuListener(contextMenu); |
100 |
|
|
101 |
locator = new LocationLookup(this, stationsFetched); |
locationLookup = new LocationLookup(this, stationsFetched); |
102 |
|
|
103 |
|
|
104 |
prefs = getSharedPreferences("TrainStation", 0); |
prefs = getSharedPreferences("TrainStation", 0); |
107 |
favorites.fromString(favoriteString); |
favorites.fromString(favoriteString); |
108 |
} |
} |
109 |
|
|
110 |
|
listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); |
111 |
|
setTitle(); |
112 |
|
|
113 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
114 |
startLookup(); |
|
115 |
|
|
116 |
|
switch (listType) { |
117 |
|
case ListNearest: |
118 |
|
startLookup(); |
119 |
|
break; |
120 |
|
case ListSearch: |
121 |
|
this.showDialog(DLG_STATIONNAME); |
122 |
|
break; |
123 |
|
case ListFavorites: |
124 |
|
startFavoriteLookup(); |
125 |
|
break; |
126 |
|
default: |
127 |
|
// Not possible !?! |
128 |
|
} |
129 |
|
|
130 |
} else { |
} else { |
131 |
stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations"); |
stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations"); |
132 |
adapter.setStations(stations); |
adapter.setStations(stations); |
133 |
location = (GeoPair) savedInstanceState.getSerializable("location"); |
location = (GeoPair) savedInstanceState.getSerializable("location"); |
134 |
} |
} |
135 |
|
|
136 |
|
} |
137 |
|
protected void setTitle() { |
138 |
|
String dialogTitle = getResources().getString(R.string.app_name); |
139 |
|
switch (listType) { |
140 |
|
case ListNearest: |
141 |
|
dialogTitle += " - Nearby stations"; |
142 |
|
break; |
143 |
|
case ListSearch: |
144 |
|
dialogTitle += " - Search"; |
145 |
|
break; |
146 |
|
case ListFavorites: |
147 |
|
dialogTitle += " - Favorites"; |
148 |
|
break; |
149 |
|
default: |
150 |
|
dialogTitle = "";//not possible |
151 |
|
} |
152 |
|
|
153 |
|
setTitle(dialogTitle); |
154 |
} |
} |
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
|
159 |
@Override |
@Override |
163 |
dialog.dismiss(); |
dialog.dismiss(); |
164 |
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
165 |
outState.putSerializable("location", location); |
outState.putSerializable("location", location); |
166 |
|
|
167 |
} |
} |
168 |
|
|
169 |
|
|
171 |
@Override |
@Override |
172 |
public boolean onCreateOptionsMenu(Menu menu) { |
public boolean onCreateOptionsMenu(Menu menu) { |
173 |
MenuItem item; |
MenuItem item; |
174 |
|
|
175 |
item = menu.add(0, OPTIONS_RESCAN, 0, "Find nearest stations"); |
item = menu.add(0, OPTIONS_MAP, 0, "Station map"); |
|
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"); |
|
176 |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
177 |
|
|
178 |
item = menu.add(0, OPTIONS_ABOUT, 0, "About"); |
item = menu.add(0, OPTIONS_GPSINFO, 0, "GPS Info"); |
179 |
item.setIcon(android.R.drawable.ic_menu_info_details); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
180 |
|
|
181 |
return true; |
return true; |
182 |
} |
} |
183 |
|
|
185 |
public boolean onOptionsItemSelected(MenuItem item) { |
public boolean onOptionsItemSelected(MenuItem item) { |
186 |
boolean retval = true; |
boolean retval = true; |
187 |
|
|
188 |
|
//TODO: Cleanup |
189 |
switch (item.getItemId()) { |
switch (item.getItemId()) { |
|
case OPTIONS_RESCAN: |
|
|
startLookup(); |
|
|
break; |
|
|
case OPTIONS_NAMESEARCH: |
|
|
showDialog(DLG_STATIONNAME); |
|
|
break; |
|
|
case OPTIONS_FAVORITES: |
|
|
startFavoriteLookup(); |
|
|
break; |
|
190 |
case OPTIONS_MAP: |
case OPTIONS_MAP: |
191 |
|
|
192 |
Intent intent = new Intent(this,StationMapView.class); |
Intent intent = new Intent(this,StationMapView.class); |
|
intent.putExtra("userlocation", location ); |
|
193 |
|
|
194 |
ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>(); |
ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>(); |
195 |
for (StationBean st : stations ) { |
for (StationBean st : stations ) { |
200 |
|
|
201 |
startActivity(intent); |
startActivity(intent); |
202 |
break; |
break; |
203 |
case OPTIONS_ABOUT: |
case OPTIONS_GPSINFO: |
204 |
String ver = this.getResources().getString(R.string.app_version); |
Location loc = locationLookup.getLocation(); |
|
|
|
|
Location loc = locator.getLocation(); |
|
205 |
StringBuffer message = new StringBuffer(); |
StringBuffer message = new StringBuffer(); |
|
message.append("TrainInfo DK v").append(ver).append("\n"); |
|
|
message.append("By Torben Nielsen\n"); |
|
|
message.append("\n"); |
|
206 |
message.append("Location info:\n"); |
message.append("Location info:\n"); |
207 |
message.append("-Obtained by: ").append(loc != null ? loc.getProvider() : "-").append("\n"); |
message.append("-Obtained by: ").append(loc != null ? loc.getProvider() : "-").append("\n"); |
208 |
message.append("-Accuracy: ").append(loc != null ? (int)loc.getAccuracy() : "-").append("m\n"); |
message.append("-Accuracy: ").append(loc != null ? (int)loc.getAccuracy() : "-").append("m\n"); |
315 |
dialogMessage = "Wait for location fix"; |
dialogMessage = "Wait for location fix"; |
316 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
317 |
|
|
318 |
locator.locateStations(); |
locationLookup.locateStations(); |
319 |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); |
320 |
} |
} |
321 |
|
|
323 |
dialogMessage = "Finding stations by name"; |
dialogMessage = "Finding stations by name"; |
324 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
325 |
|
|
326 |
locatorTask = new LocatorTask(); |
findStationsTask = new FindStationsTask(); |
327 |
locatorTask.searchByName(name, locator.getLocation()); |
findStationsTask.searchByName(name, locationLookup.getLocation()); |
328 |
locatorTask.execute(); |
findStationsTask.execute(); |
329 |
|
|
330 |
} |
} |
331 |
|
|
332 |
public void startFavoriteLookup() { |
public void startFavoriteLookup() { |
333 |
|
|
334 |
if (favorites.toString().length() > 0) { |
if (favorites.size() > 0) { |
335 |
dialogMessage = "Loading favorites"; |
dialogMessage = "Loading favorites"; |
336 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
337 |
|
|
338 |
locatorTask = new LocatorTask(); |
findStationsTask = new FindStationsTask(); |
339 |
locatorTask.searchByIds(favorites.toString(), locator.getLocation()); |
findStationsTask.searchByIds(favorites.toString(), locationLookup.getLocation()); |
340 |
locatorTask.execute(); |
findStationsTask.execute(); |
341 |
} else { |
} else { |
342 |
MessageBox.showMessage(this, "Favorite list is empty"); |
MessageBox.showMessage(this, "Favorite list is empty"); |
343 |
} |
} |
350 |
dialogMessage = "Finding nearby stations"; |
dialogMessage = "Finding nearby stations"; |
351 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
352 |
|
|
353 |
locatorTask = new LocatorTask(); |
findStationsTask = new FindStationsTask(); |
354 |
locatorTask.searchByLocation( locator.getLocation() ); |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
355 |
locatorTask.execute(); |
findStationsTask.execute(); |
356 |
} |
} |
357 |
|
|
358 |
|
|
395 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
396 |
|
|
397 |
startLocatorTask(); |
startLocatorTask(); |
398 |
location = GeoPair.fromLocation( locator.getLocation() ); |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
399 |
|
|
400 |
break; |
break; |
401 |
|
|
405 |
break; |
break; |
406 |
case LOCATIONFIXTIMEOUT: |
case LOCATIONFIXTIMEOUT: |
407 |
if (isRunning) { |
if (isRunning) { |
408 |
locator.stopSearch(); |
locationLookup.stopSearch(); |
409 |
if (locator.hasLocation()) { |
if (locationLookup.hasLocation()) { |
410 |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
411 |
} else { |
} else { |
412 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
437 |
}; |
}; |
438 |
|
|
439 |
|
|
440 |
class LocatorTask extends AsyncTask<Void,Void,Void> { |
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
441 |
|
|
442 |
LookupMethod method = LookupMethod.MethodNone; |
LookupMethod method = LookupMethod.MethodNone; |
443 |
boolean success; |
boolean success; |
474 |
|
|
475 |
@Override |
@Override |
476 |
protected Void doInBackground(Void... params) { |
protected Void doInBackground(Void... params) { |
477 |
|
|
478 |
if (method.equals(LookupMethod.ByLocation)) |
switch (method) { |
479 |
|
case ByLocation: |
480 |
success = stationProvider.lookupStations(loc); |
success = stationProvider.lookupStations(loc); |
481 |
|
break; |
482 |
if (method.equals(LookupMethod.ByName)) |
case ByName: |
483 |
success = stationProvider.lookupStationsByName(name); |
success = stationProvider.lookupStationsByName(name); |
484 |
|
break; |
485 |
if (method.equals(LookupMethod.ByList)) |
case ByList: |
486 |
success = stationProvider.lookupStationsByIds(ids); |
success = stationProvider.lookupStationsByIds(ids); |
487 |
|
break; |
488 |
|
default: |
489 |
|
success = false; // not possible |
490 |
|
} |
491 |
|
|
492 |
|
|
493 |
Location dummy = new Location("gps"); |
Location dummy = new Location("gps"); |
494 |
List<StationBean> stations = stationProvider.getStations(); |
List<StationBean> stations = stationProvider.getStations(); |
497 |
String addr = lookupAddress(station.getLatitude(), station.getLongitude()); |
String addr = lookupAddress(station.getLatitude(), station.getLongitude()); |
498 |
station.setAddress(addr); |
station.setAddress(addr); |
499 |
|
|
500 |
|
|
501 |
if (method.equals(LookupMethod.ByName) || method.equals(LookupMethod.ByList)) { |
if (method.equals(LookupMethod.ByName) || method.equals(LookupMethod.ByList)) { |
502 |
dummy.setLatitude(station.getLatitude()); |
if (loc != null) { //only do the distance calc if we have a location |
503 |
dummy.setLongitude(station.getLongitude()); |
dummy.setLatitude(station.getLatitude()); |
504 |
station.setDistance( (int)loc.distanceTo(dummy) ); |
dummy.setLongitude(station.getLongitude()); |
505 |
|
station.setDistance( (int)loc.distanceTo(dummy) ); |
506 |
|
} else { |
507 |
|
station.setDistance(0); |
508 |
|
} |
509 |
} |
} |
510 |
|
|
511 |
} |
} |
512 |
|
|
513 |
return null; |
return null; |
518 |
super.onPostExecute(result); |
super.onPostExecute(result); |
519 |
dialog.dismiss(); |
dialog.dismiss(); |
520 |
|
|
521 |
|
|
522 |
if (success) { |
if (success) { |
523 |
if (stationProvider.getStations().size() == 0) |
if (stationProvider.getStations().size() == 0) |
524 |
MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! |
MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! |
566 |
selectedPosition = info.position; |
selectedPosition = info.position; |
567 |
int stationID = stations.get(selectedPosition).getId(); |
int stationID = stations.get(selectedPosition).getId(); |
568 |
|
|
569 |
if (!favorites.hasInt(stationID)) { |
if (!favorites.contains(stationID)) { |
570 |
menu.add(0, FAVORITES_ADD, 0, "Add to favorites"); |
menu.add(0, FAVORITES_ADD, 0, "Add to favorites"); |
571 |
} else { |
} else { |
572 |
menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites"); |
menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites"); |
582 |
favorites.add(stationID); |
favorites.add(stationID); |
583 |
Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show(); |
Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show(); |
584 |
} else { |
} else { |
585 |
|
|
586 |
favorites.remove(stationID); |
favorites.remove(stationID); |
587 |
Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show(); |
Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show(); |
588 |
|
|
589 |
|
|
590 |
|
if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) { |
591 |
|
stations.remove(selectedPosition); |
592 |
|
adapter.notifyDataSetChanged(); |
593 |
|
} |
594 |
} |
} |
595 |
|
Editor ed = prefs.edit(); |
596 |
prefs.edit().putString("favorites", favorites.toString()); |
ed.putString("favorites", favorites.toString()); |
597 |
|
ed.commit(); |
598 |
} |
} |
599 |
} |
} |
600 |
} |
} |