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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 354 - (show annotations) (download)
Tue Sep 29 18:24:23 2009 UTC (14 years, 7 months ago) by torben
File size: 7390 byte(s)
Remove mock location code
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.AlertDialog;
8 import android.app.Dialog;
9 import android.app.ListActivity;
10 import android.app.ProgressDialog;
11 import android.content.DialogInterface;
12 import android.content.Intent;
13 import android.location.Address;
14 import android.location.Geocoder;
15 import android.location.Location;
16 import android.os.AsyncTask;
17 import android.os.Bundle;
18 import android.os.Handler;
19 import android.os.Message;
20 import android.util.Log;
21 import android.view.View;
22 import android.widget.ListView;
23 import dk.thoerup.traininfo.provider.ProviderFactory;
24 import dk.thoerup.traininfo.provider.StationProvider;
25 import dk.thoerup.traininfo.util.MessageBox;
26
27 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
34 public static final int DLG_PROGRESS = 1001;
35
36 /** Called when the activity is first created. */
37 String dialogMessage = "";
38 ProgressDialog dialog;
39 LocationLookup locator = null;
40 LocatorTask locatorTask;
41
42 boolean isRunning = false;
43 List<StationBean> stations = new ArrayList<StationBean>();
44
45 StationProvider stationProvider = ProviderFactory.getStationProvider();
46
47
48 StationListAdapter adapter = null;
49 @SuppressWarnings("unchecked")
50 @Override
51 public void onCreate(Bundle savedInstanceState) {
52 super.onCreate(savedInstanceState);
53 setContentView(R.layout.main);
54
55
56 adapter = new StationListAdapter(this);
57 setListAdapter(adapter);
58
59 locator = new LocationLookup(this, stationsFetched);
60 if (savedInstanceState == null) {
61 startLookup();
62 } else {
63 stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations");
64 adapter.setStations(stations);
65 }
66 }
67
68 @Override
69 public void onSaveInstanceState(Bundle outState)
70 {
71 if (dialog != null && dialog.isShowing())
72 dialog.dismiss();
73 outState.putSerializable("stations", (ArrayList<StationBean>) stations);
74 }
75
76
77
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 return dlg;
86 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 if (!dialogMessage.equals("")) {
101 this.dialog.setMessage(dialogMessage);
102 dialogMessage = "";
103 }
104 break;
105 }
106 }
107
108 public void startLookup() {
109 isRunning = true;
110 showDialog(DLG_PROGRESS);
111
112 locator.locateStations();
113 stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);
114 }
115
116
117 Handler stationsFetched = new Handler() {
118 @Override
119 public void handleMessage(Message msg) {
120
121 switch (msg.what) {
122 case GOTLOCATION:
123 dismissDialog(DLG_PROGRESS);
124
125 startLocatorTask();
126
127 break;
128
129 case NOPROVIDER:
130 dismissDialog(DLG_PROGRESS);
131 MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps.");
132 break;
133 case LOCATIONFIXTIMEOUT:
134 if (isRunning) {
135 locator.stopSearch();
136 if (locator.hasLocation()) {
137 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 }
159 }
160 break;
161 }
162 isRunning = false;
163 }
164 };
165
166 void startLocatorTask()
167 {
168 dialogMessage = "Finding nearby stations";
169 showDialog(DLG_PROGRESS);
170
171 locatorTask = new LocatorTask();
172 locatorTask.execute();
173 }
174
175 @Override
176 protected void onListItemClick(ListView l, View v, int position, long id) {
177 super.onListItemClick(l, v, position, id);
178
179 StationBean station = stations.get(position);
180
181 double latitude = station.getLatitude();
182 double longitude = station.getLongitude();
183
184
185
186 Intent intent = new Intent(this, DepartureList.class);
187 intent.putExtra("name", station.getName());
188 intent.putExtra("distance", station.getDistance());
189 intent.putExtra("latitude", latitude);
190 intent.putExtra("longitude", longitude);
191 intent.putExtra("stationid", station.getId());
192 intent.putExtra("address", station.getAddress());
193 startActivity(intent);
194 }
195
196 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
222 class LocatorTask extends AsyncTask<Void,Void,Void> {
223 boolean success;
224 @Override
225 protected void onPreExecute() {
226
227 super.onPreExecute();
228 }
229
230 @Override
231 protected Void doInBackground(Void... params) {
232 Location loc = locator.getLocation();
233 success = stationProvider.lookupStations(loc);
234
235
236 List<StationBean> stations = stationProvider.getStations();
237 for (StationBean station : stations) {
238 String addr = lookupAddress(station.getLatitude(), station.getLongitude());
239 station.setAddress(addr);
240 }
241
242 return null;
243 }
244
245 @Override
246 protected void onPostExecute(Void result) {
247 super.onPostExecute(result);
248 dialog.dismiss();
249
250 if (success) {
251 if (stationProvider.getStations().size() == 0)
252 MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?!
253 stations = stationProvider.getStations();
254 adapter.setStations( stations );
255
256 } else { //communication or parse errors
257 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 }
279 }
280 }
281 }

  ViewVC Help
Powered by ViewVC 1.1.20