1 |
torben |
237 |
package dk.thoerup.traininfo; |
2 |
|
|
|
3 |
torben |
258 |
import java.util.ArrayList; |
4 |
|
|
import java.util.List; |
5 |
torben |
294 |
import java.util.Locale; |
6 |
torben |
258 |
|
7 |
torben |
336 |
import android.app.AlertDialog; |
8 |
torben |
237 |
import android.app.Dialog; |
9 |
|
|
import android.app.ListActivity; |
10 |
|
|
import android.app.ProgressDialog; |
11 |
torben |
336 |
import android.content.DialogInterface; |
12 |
torben |
237 |
import android.content.Intent; |
13 |
torben |
294 |
import android.location.Address; |
14 |
|
|
import android.location.Geocoder; |
15 |
torben |
319 |
import android.location.Location; |
16 |
torben |
241 |
import android.os.AsyncTask; |
17 |
torben |
237 |
import android.os.Bundle; |
18 |
|
|
import android.os.Handler; |
19 |
|
|
import android.os.Message; |
20 |
torben |
294 |
import android.util.Log; |
21 |
torben |
381 |
import android.view.LayoutInflater; |
22 |
torben |
368 |
import android.view.Menu; |
23 |
|
|
import android.view.MenuItem; |
24 |
torben |
237 |
import android.view.View; |
25 |
torben |
381 |
import android.widget.EditText; |
26 |
torben |
237 |
import android.widget.ListView; |
27 |
torben |
319 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
28 |
|
|
import dk.thoerup.traininfo.provider.StationProvider; |
29 |
torben |
368 |
import dk.thoerup.traininfo.stationmap.GeoPair; |
30 |
|
|
import dk.thoerup.traininfo.stationmap.StationMapView; |
31 |
torben |
245 |
import dk.thoerup.traininfo.util.MessageBox; |
32 |
torben |
237 |
|
33 |
torben |
336 |
public class StationList extends ListActivity { |
34 |
|
|
public static final int GOTLOCATION = 1001; |
35 |
|
|
public static final int GOTSTATIONLIST = 1002; |
36 |
|
|
public static final int NOPROVIDER = 1003; |
37 |
|
|
public static final int LOCATIONFIXTIMEOUT = 1004; |
38 |
torben |
368 |
|
39 |
torben |
381 |
public static final int OPTIONS_RESCAN = 2001; |
40 |
|
|
public static final int OPTIONS_NAMESEARCH = 2002; |
41 |
|
|
public static final int OPTIONS_MAP = 2003; |
42 |
|
|
public static final int OPTIONS_ABOUT = 2004; |
43 |
torben |
336 |
|
44 |
torben |
237 |
|
45 |
torben |
381 |
public static final int DLG_PROGRESS = 3001; |
46 |
|
|
public static final int DLG_STATIONNAME = 3002; |
47 |
torben |
237 |
|
48 |
|
|
/** Called when the activity is first created. */ |
49 |
torben |
336 |
String dialogMessage = ""; |
50 |
torben |
237 |
ProgressDialog dialog; |
51 |
torben |
319 |
LocationLookup locator = null; |
52 |
torben |
336 |
LocatorTask locatorTask; |
53 |
torben |
381 |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
54 |
torben |
237 |
|
55 |
torben |
374 |
GeoPair location = new GeoPair(); |
56 |
|
|
|
57 |
torben |
258 |
boolean isRunning = false; |
58 |
|
|
List<StationBean> stations = new ArrayList<StationBean>(); |
59 |
|
|
|
60 |
torben |
319 |
StationProvider stationProvider = ProviderFactory.getStationProvider(); |
61 |
torben |
381 |
|
62 |
|
|
StationListAdapter adapter = null; |
63 |
torben |
258 |
|
64 |
torben |
381 |
static enum LookupMethod { |
65 |
|
|
ByLocation, |
66 |
|
|
ByName, |
67 |
|
|
MethodNone |
68 |
|
|
} |
69 |
torben |
319 |
|
70 |
torben |
381 |
/////////////////////////////////////////////////////////////////////////////////////////// |
71 |
|
|
//Activity call backs |
72 |
|
|
|
73 |
torben |
258 |
@SuppressWarnings("unchecked") |
74 |
torben |
237 |
@Override |
75 |
|
|
public void onCreate(Bundle savedInstanceState) { |
76 |
|
|
super.onCreate(savedInstanceState); |
77 |
|
|
setContentView(R.layout.main); |
78 |
|
|
|
79 |
torben |
241 |
|
80 |
torben |
237 |
adapter = new StationListAdapter(this); |
81 |
|
|
setListAdapter(adapter); |
82 |
|
|
|
83 |
torben |
319 |
locator = new LocationLookup(this, stationsFetched); |
84 |
torben |
258 |
if (savedInstanceState == null) { |
85 |
|
|
startLookup(); |
86 |
|
|
} else { |
87 |
|
|
stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations"); |
88 |
torben |
260 |
adapter.setStations(stations); |
89 |
torben |
374 |
location = (GeoPair) savedInstanceState.getSerializable("location"); |
90 |
torben |
258 |
} |
91 |
torben |
237 |
} |
92 |
torben |
371 |
|
93 |
|
|
|
94 |
torben |
243 |
@Override |
95 |
|
|
public void onSaveInstanceState(Bundle outState) |
96 |
|
|
{ |
97 |
torben |
258 |
if (dialog != null && dialog.isShowing()) |
98 |
torben |
243 |
dialog.dismiss(); |
99 |
torben |
258 |
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
100 |
torben |
374 |
outState.putSerializable("location", location); |
101 |
torben |
243 |
} |
102 |
torben |
237 |
|
103 |
torben |
243 |
|
104 |
torben |
237 |
|
105 |
|
|
@Override |
106 |
torben |
368 |
public boolean onCreateOptionsMenu(Menu menu) { |
107 |
torben |
381 |
menu.add(0, OPTIONS_RESCAN, 0, "Find nearest stations"); |
108 |
|
|
menu.add(0, OPTIONS_NAMESEARCH, 0, "Search for station"); |
109 |
torben |
368 |
menu.add(0, OPTIONS_MAP, 0, "Show station map"); |
110 |
|
|
menu.add(0, OPTIONS_ABOUT, 0, "About"); |
111 |
|
|
return true; |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
@Override |
115 |
|
|
public boolean onOptionsItemSelected(MenuItem item) { |
116 |
torben |
381 |
boolean retval = true; |
117 |
|
|
|
118 |
torben |
368 |
|
119 |
|
|
switch (item.getItemId()) { |
120 |
torben |
381 |
case OPTIONS_RESCAN: |
121 |
|
|
startLookup(); |
122 |
|
|
break; |
123 |
|
|
case OPTIONS_NAMESEARCH: |
124 |
|
|
showDialog(DLG_STATIONNAME); |
125 |
|
|
break; |
126 |
torben |
368 |
case OPTIONS_MAP: |
127 |
|
|
|
128 |
|
|
Intent intent = new Intent(this,StationMapView.class); |
129 |
torben |
374 |
intent.putExtra("userlocation", location ); |
130 |
torben |
368 |
|
131 |
|
|
ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>(); |
132 |
|
|
for (StationBean st : stations ) { |
133 |
torben |
369 |
stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) ); |
134 |
torben |
368 |
} |
135 |
|
|
|
136 |
|
|
intent.putExtra("stations", stationPoints); |
137 |
|
|
|
138 |
|
|
startActivity(intent); |
139 |
|
|
break; |
140 |
torben |
371 |
case OPTIONS_ABOUT: |
141 |
|
|
String ver = this.getResources().getString(R.string.app_version); |
142 |
|
|
|
143 |
|
|
StringBuffer message = new StringBuffer(); |
144 |
|
|
message.append("TrainInfo DK v").append(ver).append("\n"); |
145 |
|
|
message.append("By Torben Nielsen\n"); |
146 |
|
|
|
147 |
|
|
MessageBox.showMessage(this, message.toString()); |
148 |
|
|
break; |
149 |
torben |
368 |
default: |
150 |
|
|
retval = super.onOptionsItemSelected(item); |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
return retval; |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
@Override |
157 |
torben |
237 |
protected Dialog onCreateDialog(int id) { |
158 |
|
|
switch (id) { |
159 |
|
|
case DLG_PROGRESS: |
160 |
|
|
ProgressDialog dlg = new ProgressDialog(this); |
161 |
|
|
dlg.setMessage("Wait for location fix"); |
162 |
|
|
dlg.setCancelable(false); |
163 |
torben |
336 |
return dlg; |
164 |
torben |
381 |
case DLG_STATIONNAME: |
165 |
|
|
LayoutInflater factory = LayoutInflater.from(this); |
166 |
|
|
final View rootView = factory.inflate(R.layout.textinput, null); |
167 |
|
|
|
168 |
|
|
|
169 |
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
170 |
|
|
|
171 |
|
|
builder.setTitle("Station search"); |
172 |
|
|
builder.setView(rootView); |
173 |
|
|
builder.setCancelable(true); |
174 |
|
|
builder.setPositiveButton("Search", new DialogInterface.OnClickListener() { |
175 |
|
|
public void onClick(DialogInterface dialog, int which) { |
176 |
|
|
EditText et = (EditText) rootView.findViewById(R.id.EditText); |
177 |
|
|
dialog.dismiss(); |
178 |
|
|
if (et.getText().toString().length() >= 2) { |
179 |
|
|
startNameSearch(et.getText().toString()); |
180 |
|
|
} else { |
181 |
torben |
382 |
MessageBox.showMessage(StationList.this, "Two characters minimum" ); |
182 |
torben |
381 |
} |
183 |
|
|
} |
184 |
|
|
}); |
185 |
|
|
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
186 |
|
|
public void onClick(DialogInterface dialog, int which) { |
187 |
|
|
dialog.dismiss(); |
188 |
|
|
} |
189 |
|
|
}); |
190 |
|
|
return builder.create(); |
191 |
|
|
|
192 |
torben |
237 |
default: |
193 |
|
|
return super.onCreateDialog(id); |
194 |
|
|
} |
195 |
|
|
|
196 |
|
|
} |
197 |
|
|
|
198 |
|
|
|
199 |
|
|
@Override |
200 |
|
|
protected void onPrepareDialog(int id, Dialog dialog) { |
201 |
|
|
super.onPrepareDialog(id, dialog); |
202 |
|
|
switch (id) { |
203 |
|
|
case DLG_PROGRESS: |
204 |
|
|
this.dialog = (ProgressDialog) dialog; |
205 |
torben |
336 |
if (!dialogMessage.equals("")) { |
206 |
|
|
this.dialog.setMessage(dialogMessage); |
207 |
|
|
dialogMessage = ""; |
208 |
|
|
} |
209 |
torben |
237 |
break; |
210 |
|
|
} |
211 |
|
|
} |
212 |
torben |
381 |
|
213 |
|
|
@Override |
214 |
|
|
protected void onListItemClick(ListView l, View v, int position, long id) { |
215 |
|
|
super.onListItemClick(l, v, position, id); |
216 |
|
|
|
217 |
|
|
StationBean station = stations.get(position); |
218 |
torben |
237 |
|
219 |
torben |
381 |
double latitude = station.getLatitude(); |
220 |
|
|
double longitude = station.getLongitude(); |
221 |
|
|
|
222 |
|
|
|
223 |
|
|
|
224 |
|
|
Intent intent = new Intent(this, DepartureList.class); |
225 |
|
|
intent.putExtra("name", station.getName()); |
226 |
|
|
intent.putExtra("distance", station.getDistance()); |
227 |
|
|
intent.putExtra("latitude", latitude); |
228 |
|
|
intent.putExtra("longitude", longitude); |
229 |
|
|
intent.putExtra("stationid", station.getId()); |
230 |
|
|
intent.putExtra("address", station.getAddress()); |
231 |
|
|
startActivity(intent); |
232 |
|
|
} |
233 |
|
|
|
234 |
|
|
///////////////////////////////////////////////////////////// |
235 |
|
|
// |
236 |
|
|
|
237 |
torben |
237 |
public void startLookup() { |
238 |
|
|
isRunning = true; |
239 |
torben |
381 |
dialogMessage = "Wait for location fix"; |
240 |
torben |
237 |
showDialog(DLG_PROGRESS); |
241 |
|
|
|
242 |
|
|
locator.locateStations(); |
243 |
torben |
336 |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); |
244 |
torben |
237 |
} |
245 |
torben |
381 |
|
246 |
|
|
void startNameSearch(String name) { |
247 |
|
|
dialogMessage = "Finding stations by name"; |
248 |
|
|
showDialog(DLG_PROGRESS); |
249 |
torben |
237 |
|
250 |
torben |
381 |
locatorTask = new LocatorTask(); |
251 |
|
|
locatorTask.searchByName(name, locator.getLocation()); |
252 |
|
|
locatorTask.execute(); |
253 |
|
|
|
254 |
|
|
} |
255 |
torben |
237 |
|
256 |
torben |
381 |
|
257 |
|
|
|
258 |
|
|
void startLocatorTask() |
259 |
|
|
{ |
260 |
|
|
dialogMessage = "Finding nearby stations"; |
261 |
|
|
showDialog(DLG_PROGRESS); |
262 |
|
|
|
263 |
|
|
locatorTask = new LocatorTask(); |
264 |
|
|
locatorTask.searchByLocation( locator.getLocation() ); |
265 |
|
|
locatorTask.execute(); |
266 |
|
|
} |
267 |
|
|
|
268 |
|
|
|
269 |
|
|
String lookupAddress(double latitude, double longitude) { |
270 |
|
|
|
271 |
|
|
Geocoder coder = new Geocoder(this, new Locale("da")); |
272 |
|
|
StringBuilder sb = new StringBuilder(); |
273 |
|
|
Log.i("lookupaddr", "" + latitude + "/" + longitude); |
274 |
|
|
try { |
275 |
|
|
List<Address> addressList = coder.getFromLocation(latitude, longitude, 1); |
276 |
|
|
Address addr = addressList.get(0); |
277 |
|
|
|
278 |
|
|
|
279 |
|
|
int max = addr.getMaxAddressLineIndex(); |
280 |
|
|
for (int i=0; i<max; i++) { |
281 |
|
|
if (i>0) |
282 |
|
|
sb.append(", "); |
283 |
|
|
|
284 |
|
|
sb.append(addr.getAddressLine(i)); |
285 |
|
|
} |
286 |
|
|
|
287 |
|
|
|
288 |
|
|
} catch (Exception e) { |
289 |
|
|
Log.e("DepartureList", "geocoder failed", e); |
290 |
|
|
} |
291 |
|
|
|
292 |
|
|
return sb.toString(); |
293 |
|
|
} |
294 |
|
|
|
295 |
|
|
|
296 |
|
|
//////////////////////////////////////////////////////////////////////////// |
297 |
|
|
// Inner classes |
298 |
|
|
|
299 |
|
|
class StationsFetchedHandler extends Handler { |
300 |
torben |
237 |
@Override |
301 |
|
|
public void handleMessage(Message msg) { |
302 |
torben |
336 |
|
303 |
torben |
237 |
switch (msg.what) { |
304 |
|
|
case GOTLOCATION: |
305 |
torben |
336 |
dismissDialog(DLG_PROGRESS); |
306 |
|
|
|
307 |
|
|
startLocatorTask(); |
308 |
torben |
374 |
location = GeoPair.fromLocation( locator.getLocation() ); |
309 |
torben |
336 |
|
310 |
torben |
237 |
break; |
311 |
torben |
319 |
|
312 |
torben |
237 |
case NOPROVIDER: |
313 |
torben |
336 |
dismissDialog(DLG_PROGRESS); |
314 |
|
|
MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps."); |
315 |
torben |
237 |
break; |
316 |
torben |
336 |
case LOCATIONFIXTIMEOUT: |
317 |
torben |
237 |
if (isRunning) { |
318 |
torben |
336 |
locator.stopSearch(); |
319 |
torben |
285 |
if (locator.hasLocation()) { |
320 |
torben |
336 |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
321 |
|
|
} else { |
322 |
|
|
dismissDialog(DLG_PROGRESS); |
323 |
|
|
|
324 |
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
325 |
|
|
builder.setMessage("GPS fix timed out"); |
326 |
|
|
builder.setCancelable(true); |
327 |
|
|
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
328 |
|
|
public void onClick(DialogInterface dialog, int id) { |
329 |
|
|
dialog.dismiss(); |
330 |
|
|
startLookup(); |
331 |
|
|
|
332 |
|
|
} |
333 |
|
|
}); |
334 |
|
|
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
335 |
|
|
public void onClick(DialogInterface dialog, int id) { |
336 |
|
|
dialog.dismiss(); |
337 |
|
|
} |
338 |
|
|
}); |
339 |
|
|
builder.show(); |
340 |
|
|
|
341 |
torben |
285 |
} |
342 |
torben |
237 |
} |
343 |
|
|
break; |
344 |
|
|
} |
345 |
|
|
isRunning = false; |
346 |
|
|
} |
347 |
|
|
}; |
348 |
torben |
381 |
|
349 |
torben |
237 |
|
350 |
torben |
381 |
class LocatorTask extends AsyncTask<Void,Void,Void> { |
351 |
torben |
336 |
|
352 |
torben |
381 |
LookupMethod method = LookupMethod.MethodNone; |
353 |
|
|
boolean success; |
354 |
|
|
String name; |
355 |
|
|
Location loc; |
356 |
torben |
237 |
|
357 |
torben |
381 |
public void searchByName(String n, Location l) { |
358 |
torben |
317 |
|
359 |
torben |
381 |
method = LookupMethod.ByName; |
360 |
|
|
loc = l; |
361 |
|
|
name = n; |
362 |
torben |
317 |
} |
363 |
|
|
|
364 |
torben |
381 |
public void searchByLocation(Location l) { |
365 |
|
|
method = LookupMethod.ByLocation; |
366 |
|
|
loc = l; |
367 |
|
|
} |
368 |
|
|
|
369 |
torben |
241 |
@Override |
370 |
|
|
protected void onPreExecute() { |
371 |
torben |
258 |
|
372 |
torben |
381 |
if (method.equals(LookupMethod.MethodNone)) |
373 |
|
|
throw new RuntimeException("Method not set"); |
374 |
torben |
241 |
super.onPreExecute(); |
375 |
|
|
} |
376 |
|
|
|
377 |
|
|
@Override |
378 |
|
|
protected Void doInBackground(Void... params) { |
379 |
torben |
381 |
|
380 |
|
|
if (method.equals(LookupMethod.ByLocation)) |
381 |
|
|
success = stationProvider.lookupStations(loc); |
382 |
torben |
258 |
|
383 |
torben |
381 |
if (method.equals(LookupMethod.ByName)) |
384 |
|
|
success = stationProvider.lookupStations(name); |
385 |
torben |
319 |
|
386 |
torben |
381 |
Location dummy = new Location("gps"); |
387 |
torben |
319 |
List<StationBean> stations = stationProvider.getStations(); |
388 |
torben |
381 |
|
389 |
|
|
for (StationBean station : stations) { |
390 |
torben |
317 |
String addr = lookupAddress(station.getLatitude(), station.getLongitude()); |
391 |
|
|
station.setAddress(addr); |
392 |
torben |
381 |
|
393 |
|
|
if (method.equals(LookupMethod.ByName) ) { |
394 |
|
|
dummy.setLatitude(station.getLatitude()); |
395 |
|
|
dummy.setLongitude(station.getLongitude()); |
396 |
|
|
station.setDistance( (int)loc.distanceTo(dummy) ); |
397 |
|
|
} |
398 |
|
|
} |
399 |
torben |
317 |
|
400 |
torben |
241 |
return null; |
401 |
|
|
} |
402 |
|
|
|
403 |
|
|
@Override |
404 |
|
|
protected void onPostExecute(Void result) { |
405 |
|
|
super.onPostExecute(result); |
406 |
torben |
319 |
dialog.dismiss(); |
407 |
|
|
|
408 |
|
|
if (success) { |
409 |
|
|
if (stationProvider.getStations().size() == 0) |
410 |
torben |
336 |
MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! |
411 |
torben |
319 |
stations = stationProvider.getStations(); |
412 |
|
|
adapter.setStations( stations ); |
413 |
|
|
|
414 |
|
|
} else { //communication or parse errors |
415 |
torben |
336 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
416 |
|
|
builder.setMessage("Error on finding nearby stations"); |
417 |
|
|
builder.setCancelable(true); |
418 |
|
|
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
419 |
|
|
public void onClick(DialogInterface dialog, int id) { |
420 |
|
|
dialog.dismiss(); |
421 |
|
|
|
422 |
|
|
stationsFetched.post( new Runnable() { |
423 |
|
|
@Override |
424 |
|
|
public void run() { |
425 |
|
|
startLocatorTask(); |
426 |
|
|
} |
427 |
|
|
}); |
428 |
|
|
} |
429 |
|
|
}); |
430 |
|
|
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
431 |
|
|
public void onClick(DialogInterface dialog, int id) { |
432 |
|
|
dialog.dismiss(); |
433 |
|
|
} |
434 |
|
|
}); |
435 |
|
|
builder.show(); |
436 |
torben |
319 |
} |
437 |
torben |
241 |
} |
438 |
|
|
} |
439 |
torben |
237 |
} |