/[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 354 - (hide annotations) (download)
Tue Sep 29 18:24:23 2009 UTC (14 years, 8 months ago) by torben
File size: 7390 byte(s)
Remove mock location code
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 237 import android.view.View;
22     import android.widget.ListView;
23 torben 319 import dk.thoerup.traininfo.provider.ProviderFactory;
24     import dk.thoerup.traininfo.provider.StationProvider;
25 torben 245 import dk.thoerup.traininfo.util.MessageBox;
26 torben 237
27 torben 336 public class StationList extends ListActivity {
28     public static final int GOTLOCATION = 1001;
29     public static final int GOTSTATIONLIST = 1002;
30     public static final int NOPROVIDER = 1003;
31     public static final int LOCATIONFIXTIMEOUT = 1004;
32    
33 torben 237
34 torben 336 public static final int DLG_PROGRESS = 1001;
35 torben 237
36     /** Called when the activity is first created. */
37 torben 336 String dialogMessage = "";
38 torben 237 ProgressDialog dialog;
39 torben 319 LocationLookup locator = null;
40 torben 336 LocatorTask locatorTask;
41 torben 237
42 torben 258 boolean isRunning = false;
43     List<StationBean> stations = new ArrayList<StationBean>();
44    
45 torben 319 StationProvider stationProvider = ProviderFactory.getStationProvider();
46 torben 258
47 torben 319
48 torben 237 StationListAdapter adapter = null;
49 torben 258 @SuppressWarnings("unchecked")
50 torben 237 @Override
51     public void onCreate(Bundle savedInstanceState) {
52     super.onCreate(savedInstanceState);
53     setContentView(R.layout.main);
54    
55 torben 241
56 torben 237 adapter = new StationListAdapter(this);
57     setListAdapter(adapter);
58    
59 torben 319 locator = new LocationLookup(this, stationsFetched);
60 torben 258 if (savedInstanceState == null) {
61     startLookup();
62     } else {
63     stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations");
64 torben 260 adapter.setStations(stations);
65 torben 258 }
66 torben 237 }
67    
68 torben 243 @Override
69     public void onSaveInstanceState(Bundle outState)
70     {
71 torben 258 if (dialog != null && dialog.isShowing())
72 torben 243 dialog.dismiss();
73 torben 258 outState.putSerializable("stations", (ArrayList<StationBean>) stations);
74 torben 243 }
75 torben 237
76 torben 243
77 torben 237
78     @Override
79     protected Dialog onCreateDialog(int id) {
80     switch (id) {
81     case DLG_PROGRESS:
82     ProgressDialog dlg = new ProgressDialog(this);
83     dlg.setMessage("Wait for location fix");
84     dlg.setCancelable(false);
85 torben 336 return dlg;
86 torben 237 default:
87     return super.onCreateDialog(id);
88     }
89    
90     }
91    
92    
93    
94     @Override
95     protected void onPrepareDialog(int id, Dialog dialog) {
96     super.onPrepareDialog(id, dialog);
97     switch (id) {
98     case DLG_PROGRESS:
99     this.dialog = (ProgressDialog) dialog;
100 torben 336 if (!dialogMessage.equals("")) {
101     this.dialog.setMessage(dialogMessage);
102     dialogMessage = "";
103     }
104 torben 237 break;
105     }
106     }
107    
108     public void startLookup() {
109     isRunning = true;
110     showDialog(DLG_PROGRESS);
111    
112     locator.locateStations();
113 torben 336 stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);
114 torben 237 }
115    
116    
117     Handler stationsFetched = new Handler() {
118     @Override
119     public void handleMessage(Message msg) {
120 torben 336
121 torben 237 switch (msg.what) {
122     case GOTLOCATION:
123 torben 336 dismissDialog(DLG_PROGRESS);
124    
125     startLocatorTask();
126    
127 torben 237 break;
128 torben 319
129 torben 237 case NOPROVIDER:
130 torben 336 dismissDialog(DLG_PROGRESS);
131     MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps.");
132 torben 237 break;
133 torben 336 case LOCATIONFIXTIMEOUT:
134 torben 237 if (isRunning) {
135 torben 336 locator.stopSearch();
136 torben 285 if (locator.hasLocation()) {
137 torben 336 stationsFetched.sendEmptyMessage( GOTLOCATION );
138     } else {
139     dismissDialog(DLG_PROGRESS);
140    
141     AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);
142     builder.setMessage("GPS fix timed out");
143     builder.setCancelable(true);
144     builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
145     public void onClick(DialogInterface dialog, int id) {
146     dialog.dismiss();
147     startLookup();
148    
149     }
150     });
151     builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
152     public void onClick(DialogInterface dialog, int id) {
153     dialog.dismiss();
154     }
155     });
156     builder.show();
157    
158 torben 285 }
159 torben 237 }
160     break;
161     }
162     isRunning = false;
163     }
164     };
165    
166 torben 336 void startLocatorTask()
167     {
168     dialogMessage = "Finding nearby stations";
169     showDialog(DLG_PROGRESS);
170    
171     locatorTask = new LocatorTask();
172     locatorTask.execute();
173     }
174 torben 237
175     @Override
176     protected void onListItemClick(ListView l, View v, int position, long id) {
177     super.onListItemClick(l, v, position, id);
178 torben 294
179 torben 258 StationBean station = stations.get(position);
180 torben 294
181     double latitude = station.getLatitude();
182     double longitude = station.getLongitude();
183    
184    
185 torben 237
186     Intent intent = new Intent(this, DepartureList.class);
187     intent.putExtra("name", station.getName());
188     intent.putExtra("distance", station.getDistance());
189 torben 294 intent.putExtra("latitude", latitude);
190     intent.putExtra("longitude", longitude);
191 torben 310 intent.putExtra("stationid", station.getId());
192 torben 317 intent.putExtra("address", station.getAddress());
193 torben 237 startActivity(intent);
194     }
195    
196 torben 317 String lookupAddress(double latitude, double longitude) {
197    
198     Geocoder coder = new Geocoder(this, new Locale("da"));
199     StringBuilder sb = new StringBuilder();
200     Log.i("lookupaddr", "" + latitude + "/" + longitude);
201     try {
202     List<Address> addressList = coder.getFromLocation(latitude, longitude, 1);
203     Address addr = addressList.get(0);
204    
205    
206     int max = addr.getMaxAddressLineIndex();
207     for (int i=0; i<max; i++) {
208     if (i>0)
209     sb.append(", ");
210    
211     sb.append(addr.getAddressLine(i));
212     }
213    
214    
215     } catch (Exception e) {
216     Log.e("DepartureList", "geocoder failed", e);
217     }
218    
219     return sb.toString();
220     }
221 torben 241
222     class LocatorTask extends AsyncTask<Void,Void,Void> {
223 torben 319 boolean success;
224 torben 241 @Override
225     protected void onPreExecute() {
226 torben 258
227 torben 241 super.onPreExecute();
228     }
229    
230     @Override
231     protected Void doInBackground(Void... params) {
232 torben 319 Location loc = locator.getLocation();
233     success = stationProvider.lookupStations(loc);
234 torben 258
235 torben 319
236     List<StationBean> stations = stationProvider.getStations();
237 torben 317 for (StationBean station : stations) {
238     String addr = lookupAddress(station.getLatitude(), station.getLongitude());
239     station.setAddress(addr);
240     }
241    
242 torben 241 return null;
243     }
244    
245     @Override
246     protected void onPostExecute(Void result) {
247     super.onPostExecute(result);
248 torben 319 dialog.dismiss();
249    
250     if (success) {
251     if (stationProvider.getStations().size() == 0)
252 torben 336 MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?!
253 torben 319 stations = stationProvider.getStations();
254     adapter.setStations( stations );
255    
256     } else { //communication or parse errors
257 torben 336 AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);
258     builder.setMessage("Error on finding nearby stations");
259     builder.setCancelable(true);
260     builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
261     public void onClick(DialogInterface dialog, int id) {
262     dialog.dismiss();
263    
264     stationsFetched.post( new Runnable() {
265     @Override
266     public void run() {
267     startLocatorTask();
268     }
269     });
270     }
271     });
272     builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
273     public void onClick(DialogInterface dialog, int id) {
274     dialog.dismiss();
275     }
276     });
277     builder.show();
278 torben 319 }
279 torben 241 }
280     }
281 torben 237 }

  ViewVC Help
Powered by ViewVC 1.1.20