1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
3 |
|
import java.util.ArrayList; |
4 |
|
import java.util.List; |
5 |
|
import java.util.Locale; |
6 |
|
|
7 |
import android.app.AlertDialog; |
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; |
import android.content.DialogInterface; |
12 |
import android.content.Intent; |
import android.content.Intent; |
13 |
|
import android.location.Address; |
14 |
|
import android.location.Geocoder; |
15 |
|
import android.location.Location; |
16 |
|
import android.os.AsyncTask; |
17 |
import android.os.Bundle; |
import android.os.Bundle; |
18 |
import android.os.Handler; |
import android.os.Handler; |
19 |
import android.os.Message; |
import android.os.Message; |
20 |
|
import android.util.Log; |
21 |
|
import android.view.Menu; |
22 |
|
import android.view.MenuItem; |
23 |
import android.view.View; |
import android.view.View; |
24 |
import android.widget.ListView; |
import android.widget.ListView; |
25 |
|
|
26 |
public class TrainInfoList extends ListActivity { |
|
27 |
public static final int GOTLOCATION = 1; |
import dk.thoerup.traininfo.provider.ProviderFactory; |
28 |
public static final int GOTSTATIONLIST = 2; |
import dk.thoerup.traininfo.provider.StationProvider; |
29 |
public static final int NOPROVIDER = 3; |
import dk.thoerup.traininfo.stationmap.GeoPair; |
30 |
public static final int FIXTIMEOUT = 4; |
import dk.thoerup.traininfo.stationmap.StationMapView; |
31 |
public static final int LOOKUPSTATIONFAILED = 5; |
import dk.thoerup.traininfo.util.MessageBox; |
32 |
|
|
33 |
|
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 |
|
|
39 |
|
public static final int OPTIONS_MAP = 2001; |
40 |
|
public static final int OPTIONS_ABOUT = 2002; |
41 |
|
|
42 |
|
|
43 |
public static final int DLG_PROGRESS = 1; |
public static final int DLG_PROGRESS = 1001; |
44 |
|
|
45 |
/** Called when the activity is first created. */ |
/** Called when the activity is first created. */ |
46 |
|
String dialogMessage = ""; |
47 |
ProgressDialog dialog; |
ProgressDialog dialog; |
48 |
StationLocator locator = null; |
LocationLookup locator = null; |
49 |
|
LocatorTask locatorTask; |
50 |
|
|
51 |
|
boolean isRunning = false; |
52 |
|
List<StationBean> stations = new ArrayList<StationBean>(); |
53 |
|
|
54 |
|
StationProvider stationProvider = ProviderFactory.getStationProvider(); |
55 |
|
|
56 |
|
|
|
boolean isRunning; |
|
|
|
|
57 |
StationListAdapter adapter = null; |
StationListAdapter adapter = null; |
58 |
|
@SuppressWarnings("unchecked") |
59 |
@Override |
@Override |
60 |
public void onCreate(Bundle savedInstanceState) { |
public void onCreate(Bundle savedInstanceState) { |
61 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
62 |
setContentView(R.layout.main); |
setContentView(R.layout.main); |
63 |
|
|
64 |
|
|
65 |
adapter = new StationListAdapter(this); |
adapter = new StationListAdapter(this); |
66 |
setListAdapter(adapter); |
setListAdapter(adapter); |
67 |
|
|
68 |
locator = new StationLocator(this, stationsFetched); |
locator = new LocationLookup(this, stationsFetched); |
69 |
|
if (savedInstanceState == null) { |
70 |
startLookup(); |
startLookup(); |
71 |
|
} else { |
72 |
|
stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations"); |
73 |
|
adapter.setStations(stations); |
74 |
|
} |
75 |
} |
} |
76 |
|
|
77 |
|
@Override |
78 |
|
public void onSaveInstanceState(Bundle outState) |
79 |
|
{ |
80 |
|
if (dialog != null && dialog.isShowing()) |
81 |
|
dialog.dismiss(); |
82 |
|
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
83 |
|
} |
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
@Override |
88 |
|
public boolean onCreateOptionsMenu(Menu menu) { |
89 |
|
menu.add(0, OPTIONS_MAP, 0, "Show station map"); |
90 |
|
menu.add(0, OPTIONS_ABOUT, 0, "About"); |
91 |
|
|
92 |
|
return true; |
93 |
|
} |
94 |
|
|
95 |
|
@Override |
96 |
|
public boolean onOptionsItemSelected(MenuItem item) { |
97 |
|
boolean retval; |
98 |
|
|
99 |
|
switch (item.getItemId()) { |
100 |
|
case OPTIONS_MAP: |
101 |
|
|
102 |
|
Intent intent = new Intent(this,StationMapView.class); |
103 |
|
intent.putExtra("userlocation", GeoPair.fromLocation(locator.getLocation()) ); |
104 |
|
|
105 |
|
ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>(); |
106 |
|
for (StationBean st : stations ) { |
107 |
|
stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude()) ); |
108 |
|
} |
109 |
|
|
110 |
|
intent.putExtra("stations", stationPoints); |
111 |
|
|
112 |
|
startActivity(intent); |
113 |
|
retval = true; |
114 |
|
break; |
115 |
|
default: |
116 |
|
retval = super.onOptionsItemSelected(item); |
117 |
|
} |
118 |
|
|
119 |
|
return retval; |
120 |
|
} |
121 |
|
|
122 |
@Override |
@Override |
123 |
protected Dialog onCreateDialog(int id) { |
protected Dialog onCreateDialog(int id) { |
126 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
127 |
dlg.setMessage("Wait for location fix"); |
dlg.setMessage("Wait for location fix"); |
128 |
dlg.setCancelable(false); |
dlg.setCancelable(false); |
129 |
return dlg; |
return dlg; |
130 |
default: |
default: |
131 |
return super.onCreateDialog(id); |
return super.onCreateDialog(id); |
132 |
} |
} |
141 |
switch (id) { |
switch (id) { |
142 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
143 |
this.dialog = (ProgressDialog) dialog; |
this.dialog = (ProgressDialog) dialog; |
144 |
|
if (!dialogMessage.equals("")) { |
145 |
|
this.dialog.setMessage(dialogMessage); |
146 |
|
dialogMessage = ""; |
147 |
|
} |
148 |
break; |
break; |
149 |
} |
} |
150 |
} |
} |
154 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
155 |
|
|
156 |
locator.locateStations(); |
locator.locateStations(); |
157 |
stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000); |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); |
158 |
} |
} |
159 |
|
|
160 |
|
|
161 |
Handler stationsFetched = new Handler() { |
Handler stationsFetched = new Handler() { |
162 |
@Override |
@Override |
163 |
public void handleMessage(Message msg) { |
public void handleMessage(Message msg) { |
164 |
|
|
165 |
switch (msg.what) { |
switch (msg.what) { |
166 |
case GOTLOCATION: |
case GOTLOCATION: |
167 |
dialog.setMessage("Finding nearby stations"); |
dismissDialog(DLG_PROGRESS); |
168 |
break; |
|
169 |
case GOTSTATIONLIST: |
startLocatorTask(); |
170 |
dialog.dismiss(); |
|
|
adapter.setStations( locator.getStations() ); |
|
171 |
break; |
break; |
172 |
|
|
173 |
case NOPROVIDER: |
case NOPROVIDER: |
174 |
dialog.dismiss(); |
dismissDialog(DLG_PROGRESS); |
175 |
showMessageBox("No Location provider enabled. Plase enabled gps."); |
MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps."); |
176 |
break; |
break; |
177 |
case FIXTIMEOUT: |
case LOCATIONFIXTIMEOUT: |
|
dialog.dismiss(); |
|
178 |
if (isRunning) { |
if (isRunning) { |
179 |
locator.abortLocationListener(); |
locator.stopSearch(); |
180 |
showMessageBox("GPS fix timed out"); |
if (locator.hasLocation()) { |
181 |
|
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
182 |
|
} else { |
183 |
|
dismissDialog(DLG_PROGRESS); |
184 |
|
|
185 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
186 |
|
builder.setMessage("GPS fix timed out"); |
187 |
|
builder.setCancelable(true); |
188 |
|
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
189 |
|
public void onClick(DialogInterface dialog, int id) { |
190 |
|
dialog.dismiss(); |
191 |
|
startLookup(); |
192 |
|
|
193 |
|
} |
194 |
|
}); |
195 |
|
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
196 |
|
public void onClick(DialogInterface dialog, int id) { |
197 |
|
dialog.dismiss(); |
198 |
|
} |
199 |
|
}); |
200 |
|
builder.show(); |
201 |
|
|
202 |
|
} |
203 |
} |
} |
204 |
break; |
break; |
|
case LOOKUPSTATIONFAILED: |
|
|
dialog.dismiss(); |
|
|
showMessageBox("Error on finding nearby stations"); |
|
|
break; |
|
205 |
} |
} |
|
|
|
206 |
isRunning = false; |
isRunning = false; |
207 |
} |
} |
208 |
}; |
}; |
209 |
|
|
210 |
|
void startLocatorTask() |
211 |
|
{ |
212 |
|
dialogMessage = "Finding nearby stations"; |
213 |
|
showDialog(DLG_PROGRESS); |
214 |
|
|
215 |
|
locatorTask = new LocatorTask(); |
216 |
|
locatorTask.execute(); |
217 |
|
} |
218 |
|
|
219 |
@Override |
@Override |
220 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
221 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
222 |
|
|
223 |
StationBean station = adapter.getStation(position); |
StationBean station = stations.get(position); |
224 |
|
|
225 |
|
double latitude = station.getLatitude(); |
226 |
|
double longitude = station.getLongitude(); |
227 |
|
|
228 |
|
|
229 |
|
|
230 |
Intent intent = new Intent(this, DepartureList.class); |
Intent intent = new Intent(this, DepartureList.class); |
231 |
intent.putExtra("name", station.getName()); |
intent.putExtra("name", station.getName()); |
|
intent.putExtra("address", station.getAddress()); |
|
232 |
intent.putExtra("distance", station.getDistance()); |
intent.putExtra("distance", station.getDistance()); |
233 |
intent.putExtra("latitude", station.getLatitude()); |
intent.putExtra("latitude", latitude); |
234 |
intent.putExtra("longitude", station.getLongitude()); |
intent.putExtra("longitude", longitude); |
235 |
|
intent.putExtra("stationid", station.getId()); |
236 |
|
intent.putExtra("address", station.getAddress()); |
237 |
startActivity(intent); |
startActivity(intent); |
238 |
} |
} |
239 |
|
|
240 |
public void showMessageBox(String message) { |
String lookupAddress(double latitude, double longitude) { |
241 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
|
242 |
builder.setMessage(message) |
Geocoder coder = new Geocoder(this, new Locale("da")); |
243 |
.setCancelable(false) |
StringBuilder sb = new StringBuilder(); |
244 |
.setPositiveButton("OK", new DialogInterface.OnClickListener() { |
Log.i("lookupaddr", "" + latitude + "/" + longitude); |
245 |
public void onClick(DialogInterface dialog, int id) { |
try { |
246 |
dialog.dismiss(); |
List<Address> addressList = coder.getFromLocation(latitude, longitude, 1); |
247 |
|
Address addr = addressList.get(0); |
248 |
|
|
249 |
|
|
250 |
|
int max = addr.getMaxAddressLineIndex(); |
251 |
|
for (int i=0; i<max; i++) { |
252 |
|
if (i>0) |
253 |
|
sb.append(", "); |
254 |
|
|
255 |
|
sb.append(addr.getAddressLine(i)); |
256 |
|
} |
257 |
|
|
258 |
|
|
259 |
|
} catch (Exception e) { |
260 |
|
Log.e("DepartureList", "geocoder failed", e); |
261 |
|
} |
262 |
|
|
263 |
|
return sb.toString(); |
264 |
|
} |
265 |
|
|
266 |
|
class LocatorTask extends AsyncTask<Void,Void,Void> { |
267 |
|
boolean success; |
268 |
|
@Override |
269 |
|
protected void onPreExecute() { |
270 |
|
|
271 |
|
super.onPreExecute(); |
272 |
|
} |
273 |
|
|
274 |
|
@Override |
275 |
|
protected Void doInBackground(Void... params) { |
276 |
|
Location loc = locator.getLocation(); |
277 |
|
success = stationProvider.lookupStations(loc); |
278 |
|
|
279 |
|
|
280 |
|
List<StationBean> stations = stationProvider.getStations(); |
281 |
|
for (StationBean station : stations) { |
282 |
|
String addr = lookupAddress(station.getLatitude(), station.getLongitude()); |
283 |
|
station.setAddress(addr); |
284 |
} |
} |
285 |
}) |
|
286 |
.show(); |
return null; |
287 |
|
} |
288 |
|
|
289 |
|
@Override |
290 |
|
protected void onPostExecute(Void result) { |
291 |
|
super.onPostExecute(result); |
292 |
|
dialog.dismiss(); |
293 |
|
|
294 |
|
if (success) { |
295 |
|
if (stationProvider.getStations().size() == 0) |
296 |
|
MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! |
297 |
|
stations = stationProvider.getStations(); |
298 |
|
adapter.setStations( stations ); |
299 |
|
|
300 |
|
} else { //communication or parse errors |
301 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
302 |
|
builder.setMessage("Error on finding nearby stations"); |
303 |
|
builder.setCancelable(true); |
304 |
|
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
305 |
|
public void onClick(DialogInterface dialog, int id) { |
306 |
|
dialog.dismiss(); |
307 |
|
|
308 |
|
stationsFetched.post( new Runnable() { |
309 |
|
@Override |
310 |
|
public void run() { |
311 |
|
startLocatorTask(); |
312 |
|
} |
313 |
|
}); |
314 |
|
} |
315 |
|
}); |
316 |
|
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
317 |
|
public void onClick(DialogInterface dialog, int id) { |
318 |
|
dialog.dismiss(); |
319 |
|
} |
320 |
|
}); |
321 |
|
builder.show(); |
322 |
|
} |
323 |
|
} |
324 |
} |
} |
325 |
} |
} |