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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 333 - (show annotations) (download)
Wed Sep 23 05:39:04 2009 UTC (14 years, 7 months ago) by torben
File size: 6099 byte(s)
New icons

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.Dialog;
8 import android.app.ListActivity;
9 import android.app.ProgressDialog;
10 import android.content.Intent;
11 import android.location.Address;
12 import android.location.Geocoder;
13 import android.location.Location;
14 import android.os.AsyncTask;
15 import android.os.Bundle;
16 import android.os.Handler;
17 import android.os.Message;
18 import android.util.Log;
19 import android.view.View;
20 import android.widget.ListView;
21 import dk.thoerup.traininfo.provider.ProviderFactory;
22 import dk.thoerup.traininfo.provider.StationProvider;
23 import dk.thoerup.traininfo.util.MessageBox;
24
25 public class TrainInfoList extends ListActivity {
26 public static final int GOTLOCATION = 1;
27 public static final int GOTSTATIONLIST = 2;
28 public static final int NOPROVIDER = 3;
29 public static final int FIXTIMEOUT = 4;
30 public static final int LOOKUPSTATIONFAILED = 5;
31
32 public static final int DLG_PROGRESS = 1;
33
34 /** Called when the activity is first created. */
35 ProgressDialog dialog;
36 LocationLookup locator = null;
37 LocatorTask locatorTask = new LocatorTask();
38
39 boolean isRunning = false;
40 List<StationBean> stations = new ArrayList<StationBean>();
41
42 StationProvider stationProvider = ProviderFactory.getStationProvider();
43
44
45 StationListAdapter adapter = null;
46 @SuppressWarnings("unchecked")
47 @Override
48 public void onCreate(Bundle savedInstanceState) {
49 super.onCreate(savedInstanceState);
50 setContentView(R.layout.main);
51
52 LocationLookup.removeMockLocation(this);
53 //LocationLookup.injectMockLocation(this);
54
55 adapter = new StationListAdapter(this);
56 setListAdapter(adapter);
57
58 locator = new LocationLookup(this, stationsFetched);
59 if (savedInstanceState == null) {
60 startLookup();
61 } else {
62 stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations");
63 adapter.setStations(stations);
64 }
65 }
66
67 @Override
68 public void onSaveInstanceState(Bundle outState)
69 {
70 if (dialog != null && dialog.isShowing())
71 dialog.dismiss();
72 outState.putSerializable("stations", (ArrayList<StationBean>) stations);
73 }
74
75
76
77 @Override
78 protected Dialog onCreateDialog(int id) {
79 switch (id) {
80 case DLG_PROGRESS:
81 ProgressDialog dlg = new ProgressDialog(this);
82 dlg.setMessage("Wait for location fix");
83 dlg.setCancelable(false);
84 return dlg;
85 default:
86 return super.onCreateDialog(id);
87 }
88
89 }
90
91
92
93 @Override
94 protected void onPrepareDialog(int id, Dialog dialog) {
95 super.onPrepareDialog(id, dialog);
96 switch (id) {
97 case DLG_PROGRESS:
98 this.dialog = (ProgressDialog) dialog;
99 break;
100 }
101 }
102
103 public void startLookup() {
104 isRunning = true;
105 showDialog(DLG_PROGRESS);
106
107 locator.locateStations();
108 stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000);
109 }
110
111
112 Handler stationsFetched = new Handler() {
113 @Override
114 public void handleMessage(Message msg) {
115 switch (msg.what) {
116 case GOTLOCATION:
117 dialog.setMessage("Finding nearby stations");
118 locatorTask.execute();
119 break;
120
121 case NOPROVIDER:
122 dialog.dismiss();
123 MessageBox.showMessage(TrainInfoList.this,"No location provider enabled. Plase enable gps.");
124 break;
125 case FIXTIMEOUT:
126 dialog.dismiss();
127 if (isRunning) {
128 locator.abortLocationListener();
129 if (locator.hasLocation()) {
130 msg.what = GOTLOCATION;
131 handleMessage( msg ); // ToDo: ugly recursive call !!!
132 } else {
133 MessageBox.showMessage(TrainInfoList.this,"GPS fix timed out");
134 }
135 }
136 break;
137 case LOOKUPSTATIONFAILED:
138 dialog.dismiss();
139 MessageBox.showMessage(TrainInfoList.this,"Error on finding nearby stations");
140 break;
141 }
142 isRunning = false;
143 }
144 };
145
146
147
148 @Override
149 protected void onListItemClick(ListView l, View v, int position, long id) {
150 super.onListItemClick(l, v, position, id);
151
152 StationBean station = stations.get(position);
153
154 double latitude = station.getLatitude();
155 double longitude = station.getLongitude();
156
157
158
159 Intent intent = new Intent(this, DepartureList.class);
160 intent.putExtra("name", station.getName());
161 intent.putExtra("distance", station.getDistance());
162 intent.putExtra("latitude", latitude);
163 intent.putExtra("longitude", longitude);
164 intent.putExtra("stationid", station.getId());
165 intent.putExtra("address", station.getAddress());
166 startActivity(intent);
167 }
168
169 String lookupAddress(double latitude, double longitude) {
170
171 Geocoder coder = new Geocoder(this, new Locale("da"));
172 StringBuilder sb = new StringBuilder();
173 Log.i("lookupaddr", "" + latitude + "/" + longitude);
174 try {
175 List<Address> addressList = coder.getFromLocation(latitude, longitude, 1);
176 Address addr = addressList.get(0);
177
178
179 int max = addr.getMaxAddressLineIndex();
180 for (int i=0; i<max; i++) {
181 if (i>0)
182 sb.append(", ");
183
184 sb.append(addr.getAddressLine(i));
185 }
186
187
188 } catch (Exception e) {
189 Log.e("DepartureList", "geocoder failed", e);
190 }
191
192 return sb.toString();
193 }
194
195 class LocatorTask extends AsyncTask<Void,Void,Void> {
196 boolean success;
197 @Override
198 protected void onPreExecute() {
199
200 super.onPreExecute();
201 }
202
203 @Override
204 protected Void doInBackground(Void... params) {
205 Location loc = locator.getLocation();
206 success = stationProvider.lookupStations(loc);
207
208
209 List<StationBean> stations = stationProvider.getStations();
210 for (StationBean station : stations) {
211 String addr = lookupAddress(station.getLatitude(), station.getLongitude());
212 station.setAddress(addr);
213 }
214
215 return null;
216 }
217
218 @Override
219 protected void onPostExecute(Void result) {
220 super.onPostExecute(result);
221 dialog.dismiss();
222
223 if (success) {
224 if (stationProvider.getStations().size() == 0)
225 MessageBox.showMessage(TrainInfoList.this, "No stations found!"); // this should not be possible !?!
226 stations = stationProvider.getStations();
227 adapter.setStations( stations );
228
229 } else { //communication or parse errors
230 MessageBox.showMessage(TrainInfoList.this, "Error finding stations!");
231 }
232 }
233 }
234 }

  ViewVC Help
Powered by ViewVC 1.1.20