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

  ViewVC Help
Powered by ViewVC 1.1.20