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 |
|
158 |
|
159 |
|
160 |
Intent intent = new Intent(this, DepartureList.class); |
161 |
intent.putExtra("name", station.getName()); |
162 |
intent.putExtra("distance", station.getDistance()); |
163 |
intent.putExtra("latitude", latitude); |
164 |
intent.putExtra("longitude", longitude); |
165 |
intent.putExtra("stationid", station.getId()); |
166 |
intent.putExtra("address", station.getAddress()); |
167 |
startActivity(intent); |
168 |
} |
169 |
|
170 |
String lookupAddress(double latitude, double longitude) { |
171 |
|
172 |
Geocoder coder = new Geocoder(this, new Locale("da")); |
173 |
StringBuilder sb = new StringBuilder(); |
174 |
Log.i("lookupaddr", "" + latitude + "/" + longitude); |
175 |
try { |
176 |
List<Address> addressList = coder.getFromLocation(latitude, longitude, 1); |
177 |
Address addr = addressList.get(0); |
178 |
|
179 |
|
180 |
int max = addr.getMaxAddressLineIndex(); |
181 |
for (int i=0; i<max; i++) { |
182 |
if (i>0) |
183 |
sb.append(", "); |
184 |
|
185 |
sb.append(addr.getAddressLine(i)); |
186 |
} |
187 |
|
188 |
|
189 |
} catch (Exception e) { |
190 |
Log.e("DepartureList", "geocoder failed", e); |
191 |
} |
192 |
|
193 |
return sb.toString(); |
194 |
} |
195 |
|
196 |
class LocatorTask extends AsyncTask<Void,Void,Void> { |
197 |
@Override |
198 |
protected void onPreExecute() { |
199 |
|
200 |
super.onPreExecute(); |
201 |
} |
202 |
|
203 |
@Override |
204 |
protected Void doInBackground(Void... params) { |
205 |
locator.findNearestStations(); |
206 |
|
207 |
List<StationBean> stations = locator.getStations(); |
208 |
for (StationBean station : stations) { |
209 |
String addr = lookupAddress(station.getLatitude(), station.getLongitude()); |
210 |
station.setAddress(addr); |
211 |
} |
212 |
|
213 |
return null; |
214 |
} |
215 |
|
216 |
@Override |
217 |
protected void onPostExecute(Void result) { |
218 |
super.onPostExecute(result); |
219 |
|
220 |
} |
221 |
} |
222 |
} |