/[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 371 - (hide annotations) (download)
Wed Sep 30 19:09:59 2009 UTC (14 years, 8 months ago) by torben
File size: 8823 byte(s)
Added about box
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 258 boolean isRunning = false;
52     List<StationBean> stations = new ArrayList<StationBean>();
53    
54 torben 319 StationProvider stationProvider = ProviderFactory.getStationProvider();
55 torben 258
56 torben 319
57 torben 237 StationListAdapter adapter = null;
58 torben 258 @SuppressWarnings("unchecked")
59 torben 237 @Override
60     public void onCreate(Bundle savedInstanceState) {
61     super.onCreate(savedInstanceState);
62     setContentView(R.layout.main);
63    
64 torben 241
65 torben 237 adapter = new StationListAdapter(this);
66     setListAdapter(adapter);
67    
68 torben 319 locator = new LocationLookup(this, stationsFetched);
69 torben 258 if (savedInstanceState == null) {
70     startLookup();
71     } else {
72     stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations");
73 torben 260 adapter.setStations(stations);
74 torben 258 }
75 torben 237 }
76 torben 371
77    
78 torben 243 @Override
79     public void onSaveInstanceState(Bundle outState)
80     {
81 torben 258 if (dialog != null && dialog.isShowing())
82 torben 243 dialog.dismiss();
83 torben 258 outState.putSerializable("stations", (ArrayList<StationBean>) stations);
84 torben 243 }
85 torben 237
86 torben 243
87 torben 237
88     @Override
89 torben 368 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 torben 369 stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) );
109 torben 368 }
110    
111     intent.putExtra("stations", stationPoints);
112    
113     startActivity(intent);
114     retval = true;
115     break;
116 torben 371 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 torben 368 default:
127     retval = super.onOptionsItemSelected(item);
128     }
129    
130     return retval;
131     }
132    
133     @Override
134 torben 237 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 torben 336 return dlg;
141 torben 237 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 torben 336 if (!dialogMessage.equals("")) {
156     this.dialog.setMessage(dialogMessage);
157     dialogMessage = "";
158     }
159 torben 237 break;
160     }
161     }
162    
163     public void startLookup() {
164     isRunning = true;
165     showDialog(DLG_PROGRESS);
166    
167     locator.locateStations();
168 torben 336 stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);
169 torben 237 }
170    
171    
172     Handler stationsFetched = new Handler() {
173     @Override
174     public void handleMessage(Message msg) {
175 torben 336
176 torben 237 switch (msg.what) {
177     case GOTLOCATION:
178 torben 336 dismissDialog(DLG_PROGRESS);
179    
180     startLocatorTask();
181    
182 torben 237 break;
183 torben 319
184 torben 237 case NOPROVIDER:
185 torben 336 dismissDialog(DLG_PROGRESS);
186     MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps.");
187 torben 237 break;
188 torben 336 case LOCATIONFIXTIMEOUT:
189 torben 237 if (isRunning) {
190 torben 336 locator.stopSearch();
191 torben 285 if (locator.hasLocation()) {
192 torben 336 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 torben 285 }
214 torben 237 }
215     break;
216     }
217     isRunning = false;
218     }
219     };
220    
221 torben 336 void startLocatorTask()
222     {
223     dialogMessage = "Finding nearby stations";
224     showDialog(DLG_PROGRESS);
225    
226     locatorTask = new LocatorTask();
227     locatorTask.execute();
228     }
229 torben 237
230     @Override
231     protected void onListItemClick(ListView l, View v, int position, long id) {
232     super.onListItemClick(l, v, position, id);
233 torben 294
234 torben 258 StationBean station = stations.get(position);
235 torben 294
236     double latitude = station.getLatitude();
237     double longitude = station.getLongitude();
238    
239    
240 torben 237
241     Intent intent = new Intent(this, DepartureList.class);
242     intent.putExtra("name", station.getName());
243     intent.putExtra("distance", station.getDistance());
244 torben 294 intent.putExtra("latitude", latitude);
245     intent.putExtra("longitude", longitude);
246 torben 310 intent.putExtra("stationid", station.getId());
247 torben 317 intent.putExtra("address", station.getAddress());
248 torben 237 startActivity(intent);
249     }
250    
251 torben 317 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 torben 241
277     class LocatorTask extends AsyncTask<Void,Void,Void> {
278 torben 319 boolean success;
279 torben 241 @Override
280     protected void onPreExecute() {
281 torben 258
282 torben 241 super.onPreExecute();
283     }
284    
285     @Override
286     protected Void doInBackground(Void... params) {
287 torben 319 Location loc = locator.getLocation();
288     success = stationProvider.lookupStations(loc);
289 torben 258
290 torben 319
291     List<StationBean> stations = stationProvider.getStations();
292 torben 317 for (StationBean station : stations) {
293     String addr = lookupAddress(station.getLatitude(), station.getLongitude());
294     station.setAddress(addr);
295     }
296    
297 torben 241 return null;
298     }
299    
300     @Override
301     protected void onPostExecute(Void result) {
302     super.onPostExecute(result);
303 torben 319 dialog.dismiss();
304    
305     if (success) {
306     if (stationProvider.getStations().size() == 0)
307 torben 336 MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?!
308 torben 319 stations = stationProvider.getStations();
309     adapter.setStations( stations );
310    
311     } else { //communication or parse errors
312 torben 336 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 torben 319 }
334 torben 241 }
335     }
336 torben 237 }

  ViewVC Help
Powered by ViewVC 1.1.20