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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20