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

  ViewVC Help
Powered by ViewVC 1.1.20