4 |
import java.util.List; |
import java.util.List; |
5 |
import java.util.Locale; |
import java.util.Locale; |
6 |
|
|
7 |
|
import android.app.AlertDialog; |
8 |
import android.app.Dialog; |
import android.app.Dialog; |
9 |
import android.app.ListActivity; |
import android.app.ListActivity; |
10 |
import android.app.ProgressDialog; |
import android.app.ProgressDialog; |
11 |
|
import android.content.DialogInterface; |
12 |
import android.content.Intent; |
import android.content.Intent; |
13 |
import android.location.Address; |
import android.location.Address; |
14 |
import android.location.Geocoder; |
import android.location.Geocoder; |
18 |
import android.os.Handler; |
import android.os.Handler; |
19 |
import android.os.Message; |
import android.os.Message; |
20 |
import android.util.Log; |
import android.util.Log; |
21 |
|
import android.view.LayoutInflater; |
22 |
|
import android.view.Menu; |
23 |
|
import android.view.MenuItem; |
24 |
import android.view.View; |
import android.view.View; |
25 |
|
import android.widget.EditText; |
26 |
import android.widget.ListView; |
import android.widget.ListView; |
27 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
import dk.thoerup.traininfo.provider.ProviderFactory; |
28 |
import dk.thoerup.traininfo.provider.StationProvider; |
import dk.thoerup.traininfo.provider.StationProvider; |
29 |
|
import dk.thoerup.traininfo.stationmap.GeoPair; |
30 |
|
import dk.thoerup.traininfo.stationmap.StationMapView; |
31 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
32 |
|
|
33 |
public class TrainInfoList extends ListActivity { |
public class StationList extends ListActivity { |
34 |
public static final int GOTLOCATION = 1; |
public static final int GOTLOCATION = 1001; |
35 |
public static final int GOTSTATIONLIST = 2; |
public static final int GOTSTATIONLIST = 1002; |
36 |
public static final int NOPROVIDER = 3; |
public static final int NOPROVIDER = 1003; |
37 |
public static final int FIXTIMEOUT = 4; |
public static final int LOCATIONFIXTIMEOUT = 1004; |
38 |
public static final int LOOKUPSTATIONFAILED = 5; |
|
39 |
|
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 |
|
|
44 |
|
|
45 |
public static final int DLG_PROGRESS = 1; |
public static final int DLG_PROGRESS = 3001; |
46 |
|
public static final int DLG_STATIONNAME = 3002; |
47 |
|
|
48 |
/** Called when the activity is first created. */ |
/** Called when the activity is first created. */ |
49 |
|
String dialogMessage = ""; |
50 |
ProgressDialog dialog; |
ProgressDialog dialog; |
51 |
LocationLookup locator = null; |
LocationLookup locator = null; |
52 |
LocatorTask locatorTask = new LocatorTask(); |
LocatorTask locatorTask; |
53 |
|
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
54 |
|
|
55 |
|
GeoPair location = new GeoPair(); |
56 |
|
|
57 |
boolean isRunning = false; |
boolean isRunning = false; |
58 |
List<StationBean> stations = new ArrayList<StationBean>(); |
List<StationBean> stations = new ArrayList<StationBean>(); |
59 |
|
|
60 |
StationProvider stationProvider = ProviderFactory.getStationProvider(); |
StationProvider stationProvider = ProviderFactory.getStationProvider(); |
61 |
|
|
62 |
|
StationListAdapter adapter = null; |
63 |
|
|
64 |
|
static enum LookupMethod { |
65 |
|
ByLocation, |
66 |
|
ByName, |
67 |
|
MethodNone |
68 |
|
} |
69 |
|
|
70 |
|
/////////////////////////////////////////////////////////////////////////////////////////// |
71 |
|
//Activity call backs |
72 |
|
|
|
StationListAdapter adapter = null; |
|
73 |
@SuppressWarnings("unchecked") |
@SuppressWarnings("unchecked") |
74 |
@Override |
@Override |
75 |
public void onCreate(Bundle savedInstanceState) { |
public void onCreate(Bundle savedInstanceState) { |
76 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
77 |
setContentView(R.layout.main); |
setContentView(R.layout.main); |
78 |
|
|
|
//LocationLookup.removeMockLocation(this); |
|
|
LocationLookup.injectMockLocation(this); |
|
79 |
|
|
80 |
adapter = new StationListAdapter(this); |
adapter = new StationListAdapter(this); |
81 |
setListAdapter(adapter); |
setListAdapter(adapter); |
86 |
} else { |
} else { |
87 |
stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations"); |
stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations"); |
88 |
adapter.setStations(stations); |
adapter.setStations(stations); |
89 |
|
location = (GeoPair) savedInstanceState.getSerializable("location"); |
90 |
} |
} |
91 |
} |
} |
92 |
|
|
93 |
|
|
94 |
@Override |
@Override |
95 |
public void onSaveInstanceState(Bundle outState) |
public void onSaveInstanceState(Bundle outState) |
96 |
{ |
{ |
97 |
if (dialog != null && dialog.isShowing()) |
if (dialog != null && dialog.isShowing()) |
98 |
dialog.dismiss(); |
dialog.dismiss(); |
99 |
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
100 |
|
outState.putSerializable("location", location); |
101 |
} |
} |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
@Override |
@Override |
106 |
|
public boolean onCreateOptionsMenu(Menu menu) { |
107 |
|
menu.add(0, OPTIONS_RESCAN, 0, "Find nearest stations"); |
108 |
|
menu.add(0, OPTIONS_NAMESEARCH, 0, "Search for station"); |
109 |
|
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 |
|
boolean retval = true; |
117 |
|
|
118 |
|
|
119 |
|
switch (item.getItemId()) { |
120 |
|
case OPTIONS_RESCAN: |
121 |
|
startLookup(); |
122 |
|
break; |
123 |
|
case OPTIONS_NAMESEARCH: |
124 |
|
showDialog(DLG_STATIONNAME); |
125 |
|
break; |
126 |
|
case OPTIONS_MAP: |
127 |
|
|
128 |
|
Intent intent = new Intent(this,StationMapView.class); |
129 |
|
intent.putExtra("userlocation", location ); |
130 |
|
|
131 |
|
ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>(); |
132 |
|
for (StationBean st : stations ) { |
133 |
|
stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) ); |
134 |
|
} |
135 |
|
|
136 |
|
intent.putExtra("stations", stationPoints); |
137 |
|
|
138 |
|
startActivity(intent); |
139 |
|
break; |
140 |
|
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 |
|
default: |
150 |
|
retval = super.onOptionsItemSelected(item); |
151 |
|
} |
152 |
|
|
153 |
|
return retval; |
154 |
|
} |
155 |
|
|
156 |
|
@Override |
157 |
protected Dialog onCreateDialog(int id) { |
protected Dialog onCreateDialog(int id) { |
158 |
switch (id) { |
switch (id) { |
159 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
160 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
161 |
dlg.setMessage("Wait for location fix"); |
dlg.setMessage("Wait for location fix"); |
162 |
dlg.setCancelable(false); |
dlg.setCancelable(false); |
163 |
return dlg; |
return dlg; |
164 |
|
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 |
|
MessageBox.showMessage(StationList.this, "Two characters minimum" ); |
182 |
|
} |
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 |
default: |
default: |
193 |
return super.onCreateDialog(id); |
return super.onCreateDialog(id); |
194 |
} |
} |
196 |
} |
} |
197 |
|
|
198 |
|
|
|
|
|
199 |
@Override |
@Override |
200 |
protected void onPrepareDialog(int id, Dialog dialog) { |
protected void onPrepareDialog(int id, Dialog dialog) { |
201 |
super.onPrepareDialog(id, dialog); |
super.onPrepareDialog(id, dialog); |
202 |
switch (id) { |
switch (id) { |
203 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
204 |
this.dialog = (ProgressDialog) dialog; |
this.dialog = (ProgressDialog) dialog; |
205 |
|
if (!dialogMessage.equals("")) { |
206 |
|
this.dialog.setMessage(dialogMessage); |
207 |
|
dialogMessage = ""; |
208 |
|
} |
209 |
break; |
break; |
210 |
} |
} |
211 |
} |
} |
|
|
|
|
public void startLookup() { |
|
|
isRunning = true; |
|
|
showDialog(DLG_PROGRESS); |
|
|
|
|
|
locator.locateStations(); |
|
|
stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000); |
|
|
} |
|
|
|
|
|
|
|
|
Handler stationsFetched = new Handler() { |
|
|
@Override |
|
|
public void handleMessage(Message msg) { |
|
|
switch (msg.what) { |
|
|
case GOTLOCATION: |
|
|
dialog.setMessage("Finding nearby stations"); |
|
|
locatorTask.execute(); |
|
|
break; |
|
|
|
|
|
case NOPROVIDER: |
|
|
dialog.dismiss(); |
|
|
MessageBox.showMessage(TrainInfoList.this,"No location provider enabled. Plase enable gps."); |
|
|
break; |
|
|
case FIXTIMEOUT: |
|
|
dialog.dismiss(); |
|
|
if (isRunning) { |
|
|
locator.abortLocationListener(); |
|
|
if (locator.hasLocation()) { |
|
|
msg.what = GOTLOCATION; |
|
|
handleMessage( msg ); // ToDo: ugly recursive call !!! |
|
|
} else { |
|
|
MessageBox.showMessage(TrainInfoList.this,"GPS fix timed out"); |
|
|
} |
|
|
} |
|
|
break; |
|
|
case LOOKUPSTATIONFAILED: |
|
|
dialog.dismiss(); |
|
|
MessageBox.showMessage(TrainInfoList.this,"Error on finding nearby stations"); |
|
|
break; |
|
|
} |
|
|
isRunning = false; |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
212 |
|
|
213 |
@Override |
@Override |
214 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
231 |
startActivity(intent); |
startActivity(intent); |
232 |
} |
} |
233 |
|
|
234 |
|
///////////////////////////////////////////////////////////// |
235 |
|
// |
236 |
|
|
237 |
|
public void startLookup() { |
238 |
|
isRunning = true; |
239 |
|
dialogMessage = "Wait for location fix"; |
240 |
|
showDialog(DLG_PROGRESS); |
241 |
|
|
242 |
|
locator.locateStations(); |
243 |
|
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); |
244 |
|
} |
245 |
|
|
246 |
|
void startNameSearch(String name) { |
247 |
|
dialogMessage = "Finding stations by name"; |
248 |
|
showDialog(DLG_PROGRESS); |
249 |
|
|
250 |
|
locatorTask = new LocatorTask(); |
251 |
|
locatorTask.searchByName(name, locator.getLocation()); |
252 |
|
locatorTask.execute(); |
253 |
|
|
254 |
|
} |
255 |
|
|
256 |
|
|
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) { |
String lookupAddress(double latitude, double longitude) { |
270 |
|
|
271 |
Geocoder coder = new Geocoder(this, new Locale("da")); |
Geocoder coder = new Geocoder(this, new Locale("da")); |
292 |
return sb.toString(); |
return sb.toString(); |
293 |
} |
} |
294 |
|
|
295 |
|
|
296 |
|
//////////////////////////////////////////////////////////////////////////// |
297 |
|
// Inner classes |
298 |
|
|
299 |
|
class StationsFetchedHandler extends Handler { |
300 |
|
@Override |
301 |
|
public void handleMessage(Message msg) { |
302 |
|
|
303 |
|
switch (msg.what) { |
304 |
|
case GOTLOCATION: |
305 |
|
dismissDialog(DLG_PROGRESS); |
306 |
|
|
307 |
|
startLocatorTask(); |
308 |
|
location = GeoPair.fromLocation( locator.getLocation() ); |
309 |
|
|
310 |
|
break; |
311 |
|
|
312 |
|
case NOPROVIDER: |
313 |
|
dismissDialog(DLG_PROGRESS); |
314 |
|
MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps."); |
315 |
|
break; |
316 |
|
case LOCATIONFIXTIMEOUT: |
317 |
|
if (isRunning) { |
318 |
|
locator.stopSearch(); |
319 |
|
if (locator.hasLocation()) { |
320 |
|
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 |
|
} |
342 |
|
} |
343 |
|
break; |
344 |
|
} |
345 |
|
isRunning = false; |
346 |
|
} |
347 |
|
}; |
348 |
|
|
349 |
|
|
350 |
class LocatorTask extends AsyncTask<Void,Void,Void> { |
class LocatorTask extends AsyncTask<Void,Void,Void> { |
351 |
|
|
352 |
|
LookupMethod method = LookupMethod.MethodNone; |
353 |
boolean success; |
boolean success; |
354 |
|
String name; |
355 |
|
Location loc; |
356 |
|
|
357 |
|
public void searchByName(String n, Location l) { |
358 |
|
|
359 |
|
method = LookupMethod.ByName; |
360 |
|
loc = l; |
361 |
|
name = n; |
362 |
|
} |
363 |
|
|
364 |
|
public void searchByLocation(Location l) { |
365 |
|
method = LookupMethod.ByLocation; |
366 |
|
loc = l; |
367 |
|
} |
368 |
|
|
369 |
@Override |
@Override |
370 |
protected void onPreExecute() { |
protected void onPreExecute() { |
371 |
|
|
372 |
|
if (method.equals(LookupMethod.MethodNone)) |
373 |
|
throw new RuntimeException("Method not set"); |
374 |
super.onPreExecute(); |
super.onPreExecute(); |
375 |
} |
} |
376 |
|
|
377 |
@Override |
@Override |
378 |
protected Void doInBackground(Void... params) { |
protected Void doInBackground(Void... params) { |
379 |
Location loc = locator.getLocation(); |
|
380 |
success = stationProvider.lookupStations(loc); |
if (method.equals(LookupMethod.ByLocation)) |
381 |
|
success = stationProvider.lookupStations(loc); |
382 |
|
|
383 |
|
if (method.equals(LookupMethod.ByName)) |
384 |
|
success = stationProvider.lookupStations(name); |
385 |
|
|
386 |
|
Location dummy = new Location("gps"); |
387 |
List<StationBean> stations = stationProvider.getStations(); |
List<StationBean> stations = stationProvider.getStations(); |
388 |
for (StationBean station : stations) { |
|
389 |
|
for (StationBean station : stations) { |
390 |
String addr = lookupAddress(station.getLatitude(), station.getLongitude()); |
String addr = lookupAddress(station.getLatitude(), station.getLongitude()); |
391 |
station.setAddress(addr); |
station.setAddress(addr); |
392 |
} |
|
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 |
|
|
400 |
return null; |
return null; |
401 |
} |
} |
407 |
|
|
408 |
if (success) { |
if (success) { |
409 |
if (stationProvider.getStations().size() == 0) |
if (stationProvider.getStations().size() == 0) |
410 |
MessageBox.showMessage(TrainInfoList.this, "No stations found!"); // this should not be possible !?! |
MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! |
411 |
stations = stationProvider.getStations(); |
stations = stationProvider.getStations(); |
412 |
adapter.setStations( stations ); |
adapter.setStations( stations ); |
413 |
|
|
414 |
} else { //communication or parse errors |
} else { //communication or parse errors |
415 |
MessageBox.showMessage(TrainInfoList.this, "Error finding stations!"); |
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 |
} |
} |
437 |
} |
} |
438 |
} |
} |