/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/StationList.java
ViewVC logotype

Contents of /android/TrainInfo/src/dk/thoerup/traininfo/StationList.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 371 - (show annotations) (download)
Wed Sep 30 19:09:59 2009 UTC (14 years, 7 months ago) by torben
File size: 8823 byte(s)
Added about box
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
78 @Override
79 public void onSaveInstanceState(Bundle outState)
80 {
81 if (dialog != null && dialog.isShowing())
82 dialog.dismiss();
83 outState.putSerializable("stations", (ArrayList<StationBean>) stations);
84 }
85
86
87
88 @Override
89 public boolean onCreateOptionsMenu(Menu menu) {
90 menu.add(0, OPTIONS_MAP, 0, "Show station map");
91 menu.add(0, OPTIONS_ABOUT, 0, "About");
92
93 return true;
94 }
95
96 @Override
97 public boolean onOptionsItemSelected(MenuItem item) {
98 boolean retval;
99
100 switch (item.getItemId()) {
101 case OPTIONS_MAP:
102
103 Intent intent = new Intent(this,StationMapView.class);
104 intent.putExtra("userlocation", GeoPair.fromLocation(locator.getLocation()) );
105
106 ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();
107 for (StationBean st : stations ) {
108 stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) );
109 }
110
111 intent.putExtra("stations", stationPoints);
112
113 startActivity(intent);
114 retval = true;
115 break;
116 case OPTIONS_ABOUT:
117 String ver = this.getResources().getString(R.string.app_version);
118
119 StringBuffer message = new StringBuffer();
120 message.append("TrainInfo DK v").append(ver).append("\n");
121 message.append("By Torben Nielsen\n");
122
123 MessageBox.showMessage(this, message.toString());
124 retval = true;
125 break;
126 default:
127 retval = super.onOptionsItemSelected(item);
128 }
129
130 return retval;
131 }
132
133 @Override
134 protected Dialog onCreateDialog(int id) {
135 switch (id) {
136 case DLG_PROGRESS:
137 ProgressDialog dlg = new ProgressDialog(this);
138 dlg.setMessage("Wait for location fix");
139 dlg.setCancelable(false);
140 return dlg;
141 default:
142 return super.onCreateDialog(id);
143 }
144
145 }
146
147
148
149 @Override
150 protected void onPrepareDialog(int id, Dialog dialog) {
151 super.onPrepareDialog(id, dialog);
152 switch (id) {
153 case DLG_PROGRESS:
154 this.dialog = (ProgressDialog) dialog;
155 if (!dialogMessage.equals("")) {
156 this.dialog.setMessage(dialogMessage);
157 dialogMessage = "";
158 }
159 break;
160 }
161 }
162
163 public void startLookup() {
164 isRunning = true;
165 showDialog(DLG_PROGRESS);
166
167 locator.locateStations();
168 stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);
169 }
170
171
172 Handler stationsFetched = new Handler() {
173 @Override
174 public void handleMessage(Message msg) {
175
176 switch (msg.what) {
177 case GOTLOCATION:
178 dismissDialog(DLG_PROGRESS);
179
180 startLocatorTask();
181
182 break;
183
184 case NOPROVIDER:
185 dismissDialog(DLG_PROGRESS);
186 MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps.");
187 break;
188 case LOCATIONFIXTIMEOUT:
189 if (isRunning) {
190 locator.stopSearch();
191 if (locator.hasLocation()) {
192 stationsFetched.sendEmptyMessage( GOTLOCATION );
193 } else {
194 dismissDialog(DLG_PROGRESS);
195
196 AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);
197 builder.setMessage("GPS fix timed out");
198 builder.setCancelable(true);
199 builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
200 public void onClick(DialogInterface dialog, int id) {
201 dialog.dismiss();
202 startLookup();
203
204 }
205 });
206 builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
207 public void onClick(DialogInterface dialog, int id) {
208 dialog.dismiss();
209 }
210 });
211 builder.show();
212
213 }
214 }
215 break;
216 }
217 isRunning = false;
218 }
219 };
220
221 void startLocatorTask()
222 {
223 dialogMessage = "Finding nearby stations";
224 showDialog(DLG_PROGRESS);
225
226 locatorTask = new LocatorTask();
227 locatorTask.execute();
228 }
229
230 @Override
231 protected void onListItemClick(ListView l, View v, int position, long id) {
232 super.onListItemClick(l, v, position, id);
233
234 StationBean station = stations.get(position);
235
236 double latitude = station.getLatitude();
237 double longitude = station.getLongitude();
238
239
240
241 Intent intent = new Intent(this, DepartureList.class);
242 intent.putExtra("name", station.getName());
243 intent.putExtra("distance", station.getDistance());
244 intent.putExtra("latitude", latitude);
245 intent.putExtra("longitude", longitude);
246 intent.putExtra("stationid", station.getId());
247 intent.putExtra("address", station.getAddress());
248 startActivity(intent);
249 }
250
251 String lookupAddress(double latitude, double longitude) {
252
253 Geocoder coder = new Geocoder(this, new Locale("da"));
254 StringBuilder sb = new StringBuilder();
255 Log.i("lookupaddr", "" + latitude + "/" + longitude);
256 try {
257 List<Address> addressList = coder.getFromLocation(latitude, longitude, 1);
258 Address addr = addressList.get(0);
259
260
261 int max = addr.getMaxAddressLineIndex();
262 for (int i=0; i<max; i++) {
263 if (i>0)
264 sb.append(", ");
265
266 sb.append(addr.getAddressLine(i));
267 }
268
269
270 } catch (Exception e) {
271 Log.e("DepartureList", "geocoder failed", e);
272 }
273
274 return sb.toString();
275 }
276
277 class LocatorTask extends AsyncTask<Void,Void,Void> {
278 boolean success;
279 @Override
280 protected void onPreExecute() {
281
282 super.onPreExecute();
283 }
284
285 @Override
286 protected Void doInBackground(Void... params) {
287 Location loc = locator.getLocation();
288 success = stationProvider.lookupStations(loc);
289
290
291 List<StationBean> stations = stationProvider.getStations();
292 for (StationBean station : stations) {
293 String addr = lookupAddress(station.getLatitude(), station.getLongitude());
294 station.setAddress(addr);
295 }
296
297 return null;
298 }
299
300 @Override
301 protected void onPostExecute(Void result) {
302 super.onPostExecute(result);
303 dialog.dismiss();
304
305 if (success) {
306 if (stationProvider.getStations().size() == 0)
307 MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?!
308 stations = stationProvider.getStations();
309 adapter.setStations( stations );
310
311 } else { //communication or parse errors
312 AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);
313 builder.setMessage("Error on finding nearby stations");
314 builder.setCancelable(true);
315 builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
316 public void onClick(DialogInterface dialog, int id) {
317 dialog.dismiss();
318
319 stationsFetched.post( new Runnable() {
320 @Override
321 public void run() {
322 startLocatorTask();
323 }
324 });
325 }
326 });
327 builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
328 public void onClick(DialogInterface dialog, int id) {
329 dialog.dismiss();
330 }
331 });
332 builder.show();
333 }
334 }
335 }
336 }

  ViewVC Help
Powered by ViewVC 1.1.20