1 |
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; |
8 |
import android.app.Dialog; |
9 |
import android.app.ListActivity; |
10 |
import android.app.ProgressDialog; |
11 |
import android.content.DialogInterface; |
12 |
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; |
18 |
import android.os.Handler; |
19 |
import android.os.Message; |
20 |
import android.util.Log; |
21 |
import android.view.Menu; |
22 |
import android.view.MenuItem; |
23 |
import android.view.View; |
24 |
import android.widget.ListView; |
25 |
|
26 |
|
27 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
28 |
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; |
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 = 1001; |
44 |
|
45 |
/** Called when the activity is first created. */ |
46 |
String dialogMessage = ""; |
47 |
ProgressDialog dialog; |
48 |
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 |
|
57 |
StationListAdapter adapter = null; |
58 |
@SuppressWarnings("unchecked") |
59 |
@Override |
60 |
public void onCreate(Bundle savedInstanceState) { |
61 |
super.onCreate(savedInstanceState); |
62 |
setContentView(R.layout.main); |
63 |
|
64 |
|
65 |
adapter = new StationListAdapter(this); |
66 |
setListAdapter(adapter); |
67 |
|
68 |
locator = new LocationLookup(this, stationsFetched); |
69 |
if (savedInstanceState == null) { |
70 |
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(), st.getName()) ); |
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 |
123 |
protected Dialog onCreateDialog(int id) { |
124 |
switch (id) { |
125 |
case DLG_PROGRESS: |
126 |
ProgressDialog dlg = new ProgressDialog(this); |
127 |
dlg.setMessage("Wait for location fix"); |
128 |
dlg.setCancelable(false); |
129 |
return dlg; |
130 |
default: |
131 |
return super.onCreateDialog(id); |
132 |
} |
133 |
|
134 |
} |
135 |
|
136 |
|
137 |
|
138 |
@Override |
139 |
protected void onPrepareDialog(int id, Dialog dialog) { |
140 |
super.onPrepareDialog(id, dialog); |
141 |
switch (id) { |
142 |
case DLG_PROGRESS: |
143 |
this.dialog = (ProgressDialog) dialog; |
144 |
if (!dialogMessage.equals("")) { |
145 |
this.dialog.setMessage(dialogMessage); |
146 |
dialogMessage = ""; |
147 |
} |
148 |
break; |
149 |
} |
150 |
} |
151 |
|
152 |
public void startLookup() { |
153 |
isRunning = true; |
154 |
showDialog(DLG_PROGRESS); |
155 |
|
156 |
locator.locateStations(); |
157 |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); |
158 |
} |
159 |
|
160 |
|
161 |
Handler stationsFetched = new Handler() { |
162 |
@Override |
163 |
public void handleMessage(Message msg) { |
164 |
|
165 |
switch (msg.what) { |
166 |
case GOTLOCATION: |
167 |
dismissDialog(DLG_PROGRESS); |
168 |
|
169 |
startLocatorTask(); |
170 |
|
171 |
break; |
172 |
|
173 |
case NOPROVIDER: |
174 |
dismissDialog(DLG_PROGRESS); |
175 |
MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps."); |
176 |
break; |
177 |
case LOCATIONFIXTIMEOUT: |
178 |
if (isRunning) { |
179 |
locator.stopSearch(); |
180 |
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; |
205 |
} |
206 |
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 |
220 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
221 |
super.onListItemClick(l, v, position, id); |
222 |
|
223 |
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); |
231 |
intent.putExtra("name", station.getName()); |
232 |
intent.putExtra("distance", station.getDistance()); |
233 |
intent.putExtra("latitude", latitude); |
234 |
intent.putExtra("longitude", longitude); |
235 |
intent.putExtra("stationid", station.getId()); |
236 |
intent.putExtra("address", station.getAddress()); |
237 |
startActivity(intent); |
238 |
} |
239 |
|
240 |
String lookupAddress(double latitude, double longitude) { |
241 |
|
242 |
Geocoder coder = new Geocoder(this, new Locale("da")); |
243 |
StringBuilder sb = new StringBuilder(); |
244 |
Log.i("lookupaddr", "" + latitude + "/" + longitude); |
245 |
try { |
246 |
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 |
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 |
} |