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 |
237 |
import android.app.Dialog; |
8 |
|
|
import android.app.ListActivity; |
9 |
|
|
import android.app.ProgressDialog; |
10 |
|
|
import android.content.Intent; |
11 |
torben |
294 |
import android.location.Address; |
12 |
|
|
import android.location.Geocoder; |
13 |
torben |
319 |
import android.location.Location; |
14 |
torben |
241 |
import android.os.AsyncTask; |
15 |
torben |
237 |
import android.os.Bundle; |
16 |
|
|
import android.os.Handler; |
17 |
|
|
import android.os.Message; |
18 |
torben |
294 |
import android.util.Log; |
19 |
torben |
237 |
import android.view.View; |
20 |
|
|
import android.widget.ListView; |
21 |
torben |
319 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
22 |
|
|
import dk.thoerup.traininfo.provider.StationProvider; |
23 |
torben |
245 |
import dk.thoerup.traininfo.util.MessageBox; |
24 |
torben |
237 |
|
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 |
torben |
319 |
LocationLookup locator = null; |
37 |
torben |
241 |
LocatorTask locatorTask = new LocatorTask(); |
38 |
torben |
237 |
|
39 |
torben |
258 |
boolean isRunning = false; |
40 |
|
|
List<StationBean> stations = new ArrayList<StationBean>(); |
41 |
|
|
|
42 |
torben |
319 |
StationProvider stationProvider = ProviderFactory.getStationProvider(); |
43 |
torben |
258 |
|
44 |
torben |
319 |
|
45 |
torben |
237 |
StationListAdapter adapter = null; |
46 |
torben |
258 |
@SuppressWarnings("unchecked") |
47 |
torben |
237 |
@Override |
48 |
|
|
public void onCreate(Bundle savedInstanceState) { |
49 |
|
|
super.onCreate(savedInstanceState); |
50 |
|
|
setContentView(R.layout.main); |
51 |
|
|
|
52 |
torben |
333 |
LocationLookup.removeMockLocation(this); |
53 |
|
|
//LocationLookup.injectMockLocation(this); |
54 |
torben |
241 |
|
55 |
torben |
237 |
adapter = new StationListAdapter(this); |
56 |
|
|
setListAdapter(adapter); |
57 |
|
|
|
58 |
torben |
319 |
locator = new LocationLookup(this, stationsFetched); |
59 |
torben |
258 |
if (savedInstanceState == null) { |
60 |
|
|
startLookup(); |
61 |
|
|
} else { |
62 |
|
|
stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations"); |
63 |
torben |
260 |
adapter.setStations(stations); |
64 |
torben |
258 |
} |
65 |
torben |
237 |
} |
66 |
|
|
|
67 |
torben |
243 |
@Override |
68 |
|
|
public void onSaveInstanceState(Bundle outState) |
69 |
|
|
{ |
70 |
torben |
258 |
if (dialog != null && dialog.isShowing()) |
71 |
torben |
243 |
dialog.dismiss(); |
72 |
torben |
258 |
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
73 |
torben |
243 |
} |
74 |
torben |
237 |
|
75 |
torben |
243 |
|
76 |
torben |
237 |
|
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 |
torben |
258 |
stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000); |
109 |
torben |
237 |
} |
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 |
torben |
241 |
locatorTask.execute(); |
119 |
torben |
237 |
break; |
120 |
torben |
319 |
|
121 |
torben |
237 |
case NOPROVIDER: |
122 |
|
|
dialog.dismiss(); |
123 |
torben |
245 |
MessageBox.showMessage(TrainInfoList.this,"No location provider enabled. Plase enable gps."); |
124 |
torben |
237 |
break; |
125 |
|
|
case FIXTIMEOUT: |
126 |
|
|
dialog.dismiss(); |
127 |
|
|
if (isRunning) { |
128 |
|
|
locator.abortLocationListener(); |
129 |
torben |
285 |
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 |
torben |
237 |
} |
136 |
|
|
break; |
137 |
|
|
case LOOKUPSTATIONFAILED: |
138 |
|
|
dialog.dismiss(); |
139 |
torben |
245 |
MessageBox.showMessage(TrainInfoList.this,"Error on finding nearby stations"); |
140 |
torben |
237 |
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 |
torben |
294 |
|
152 |
torben |
258 |
StationBean station = stations.get(position); |
153 |
torben |
294 |
|
154 |
|
|
double latitude = station.getLatitude(); |
155 |
|
|
double longitude = station.getLongitude(); |
156 |
|
|
|
157 |
|
|
|
158 |
torben |
237 |
|
159 |
|
|
Intent intent = new Intent(this, DepartureList.class); |
160 |
|
|
intent.putExtra("name", station.getName()); |
161 |
|
|
intent.putExtra("distance", station.getDistance()); |
162 |
torben |
294 |
intent.putExtra("latitude", latitude); |
163 |
|
|
intent.putExtra("longitude", longitude); |
164 |
torben |
310 |
intent.putExtra("stationid", station.getId()); |
165 |
torben |
317 |
intent.putExtra("address", station.getAddress()); |
166 |
torben |
237 |
startActivity(intent); |
167 |
|
|
} |
168 |
|
|
|
169 |
torben |
317 |
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 |
torben |
241 |
|
195 |
|
|
class LocatorTask extends AsyncTask<Void,Void,Void> { |
196 |
torben |
319 |
boolean success; |
197 |
torben |
241 |
@Override |
198 |
|
|
protected void onPreExecute() { |
199 |
torben |
258 |
|
200 |
torben |
241 |
super.onPreExecute(); |
201 |
|
|
} |
202 |
|
|
|
203 |
|
|
@Override |
204 |
|
|
protected Void doInBackground(Void... params) { |
205 |
torben |
319 |
Location loc = locator.getLocation(); |
206 |
|
|
success = stationProvider.lookupStations(loc); |
207 |
torben |
258 |
|
208 |
torben |
319 |
|
209 |
|
|
List<StationBean> stations = stationProvider.getStations(); |
210 |
torben |
317 |
for (StationBean station : stations) { |
211 |
|
|
String addr = lookupAddress(station.getLatitude(), station.getLongitude()); |
212 |
|
|
station.setAddress(addr); |
213 |
|
|
} |
214 |
|
|
|
215 |
torben |
241 |
return null; |
216 |
|
|
} |
217 |
|
|
|
218 |
|
|
@Override |
219 |
|
|
protected void onPostExecute(Void result) { |
220 |
|
|
super.onPostExecute(result); |
221 |
torben |
319 |
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 |
torben |
241 |
} |
233 |
|
|
} |
234 |
torben |
237 |
} |