/[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 369 - (hide annotations) (download)
Wed Sep 30 16:40:48 2009 UTC (14 years, 8 months ago) by torben
File size: 8496 byte(s)
Draw user location on stationmap,
When a station icon is pressed, show the station name in a toaster 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    
77 torben 243 @Override
78     public void onSaveInstanceState(Bundle outState)
79     {
80 torben 258 if (dialog != null && dialog.isShowing())
81 torben 243 dialog.dismiss();
82 torben 258 outState.putSerializable("stations", (ArrayList<StationBean>) stations);
83 torben 243 }
84 torben 237
85 torben 243
86 torben 237
87     @Override
88 torben 368 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 torben 369 stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) );
108 torben 368 }
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 torben 237 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 torben 336 return dlg;
130 torben 237 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 torben 336 if (!dialogMessage.equals("")) {
145     this.dialog.setMessage(dialogMessage);
146     dialogMessage = "";
147     }
148 torben 237 break;
149     }
150     }
151    
152     public void startLookup() {
153     isRunning = true;
154     showDialog(DLG_PROGRESS);
155    
156     locator.locateStations();
157 torben 336 stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);
158 torben 237 }
159    
160    
161     Handler stationsFetched = new Handler() {
162     @Override
163     public void handleMessage(Message msg) {
164 torben 336
165 torben 237 switch (msg.what) {
166     case GOTLOCATION:
167 torben 336 dismissDialog(DLG_PROGRESS);
168    
169     startLocatorTask();
170    
171 torben 237 break;
172 torben 319
173 torben 237 case NOPROVIDER:
174 torben 336 dismissDialog(DLG_PROGRESS);
175     MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps.");
176 torben 237 break;
177 torben 336 case LOCATIONFIXTIMEOUT:
178 torben 237 if (isRunning) {
179 torben 336 locator.stopSearch();
180 torben 285 if (locator.hasLocation()) {
181 torben 336 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 torben 285 }
203 torben 237 }
204     break;
205     }
206     isRunning = false;
207     }
208     };
209    
210 torben 336 void startLocatorTask()
211     {
212     dialogMessage = "Finding nearby stations";
213     showDialog(DLG_PROGRESS);
214    
215     locatorTask = new LocatorTask();
216     locatorTask.execute();
217     }
218 torben 237
219     @Override
220     protected void onListItemClick(ListView l, View v, int position, long id) {
221     super.onListItemClick(l, v, position, id);
222 torben 294
223 torben 258 StationBean station = stations.get(position);
224 torben 294
225     double latitude = station.getLatitude();
226     double longitude = station.getLongitude();
227    
228    
229 torben 237
230     Intent intent = new Intent(this, DepartureList.class);
231     intent.putExtra("name", station.getName());
232     intent.putExtra("distance", station.getDistance());
233 torben 294 intent.putExtra("latitude", latitude);
234     intent.putExtra("longitude", longitude);
235 torben 310 intent.putExtra("stationid", station.getId());
236 torben 317 intent.putExtra("address", station.getAddress());
237 torben 237 startActivity(intent);
238     }
239    
240 torben 317 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 torben 241
266     class LocatorTask extends AsyncTask<Void,Void,Void> {
267 torben 319 boolean success;
268 torben 241 @Override
269     protected void onPreExecute() {
270 torben 258
271 torben 241 super.onPreExecute();
272     }
273    
274     @Override
275     protected Void doInBackground(Void... params) {
276 torben 319 Location loc = locator.getLocation();
277     success = stationProvider.lookupStations(loc);
278 torben 258
279 torben 319
280     List<StationBean> stations = stationProvider.getStations();
281 torben 317 for (StationBean station : stations) {
282     String addr = lookupAddress(station.getLatitude(), station.getLongitude());
283     station.setAddress(addr);
284     }
285    
286 torben 241 return null;
287     }
288    
289     @Override
290     protected void onPostExecute(Void result) {
291     super.onPostExecute(result);
292 torben 319 dialog.dismiss();
293    
294     if (success) {
295     if (stationProvider.getStations().size() == 0)
296 torben 336 MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?!
297 torben 319 stations = stationProvider.getStations();
298     adapter.setStations( stations );
299    
300     } else { //communication or parse errors
301 torben 336 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 torben 319 }
323 torben 241 }
324     }
325 torben 237 }

  ViewVC Help
Powered by ViewVC 1.1.20