1 |
package dk.thoerup.traininfo; |
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; |
import android.app.AlertDialog; |
8 |
import android.app.Dialog; |
import android.app.Dialog; |
9 |
import android.app.ListActivity; |
import android.app.ListActivity; |
10 |
import android.app.ProgressDialog; |
import android.app.ProgressDialog; |
11 |
import android.content.DialogInterface; |
import android.content.DialogInterface; |
12 |
import android.content.Intent; |
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; |
import android.os.Bundle; |
18 |
import android.os.Handler; |
import android.os.Handler; |
19 |
import android.os.Message; |
import android.os.Message; |
20 |
|
import android.util.Log; |
21 |
import android.view.View; |
import android.view.View; |
22 |
import android.widget.ListView; |
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 |
|
|
|
public class TrainInfoList extends ListActivity { |
|
|
public static final int GOTLOCATION = 1; |
|
|
public static final int GOTSTATIONLIST = 2; |
|
|
public static final int NOPROVIDER = 3; |
|
|
public static final int FIXTIMEOUT = 4; |
|
|
public static final int LOOKUPSTATIONFAILED = 5; |
|
33 |
|
|
34 |
public static final int DLG_PROGRESS = 1; |
public static final int DLG_PROGRESS = 1001; |
35 |
|
|
36 |
/** Called when the activity is first created. */ |
/** Called when the activity is first created. */ |
37 |
|
String dialogMessage = ""; |
38 |
ProgressDialog dialog; |
ProgressDialog dialog; |
39 |
StationLocator locator = null; |
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 |
|
|
|
boolean isRunning; |
|
|
|
|
48 |
StationListAdapter adapter = null; |
StationListAdapter adapter = null; |
49 |
|
@SuppressWarnings("unchecked") |
50 |
@Override |
@Override |
51 |
public void onCreate(Bundle savedInstanceState) { |
public void onCreate(Bundle savedInstanceState) { |
52 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
53 |
setContentView(R.layout.main); |
setContentView(R.layout.main); |
54 |
|
|
55 |
|
|
56 |
adapter = new StationListAdapter(this); |
adapter = new StationListAdapter(this); |
57 |
setListAdapter(adapter); |
setListAdapter(adapter); |
58 |
|
|
59 |
locator = new StationLocator(this, stationsFetched); |
locator = new LocationLookup(this, stationsFetched); |
60 |
|
if (savedInstanceState == null) { |
61 |
startLookup(); |
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 |
@Override |
82 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
83 |
dlg.setMessage("Wait for location fix"); |
dlg.setMessage("Wait for location fix"); |
84 |
dlg.setCancelable(false); |
dlg.setCancelable(false); |
85 |
return dlg; |
return dlg; |
86 |
default: |
default: |
87 |
return super.onCreateDialog(id); |
return super.onCreateDialog(id); |
88 |
} |
} |
91 |
|
|
92 |
|
|
93 |
|
|
|
|
|
94 |
@Override |
@Override |
95 |
protected void onPrepareDialog(int id, Dialog dialog) { |
protected void onPrepareDialog(int id, Dialog dialog) { |
96 |
super.onPrepareDialog(id, dialog); |
super.onPrepareDialog(id, dialog); |
97 |
switch (id) { |
switch (id) { |
98 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
99 |
this.dialog = (ProgressDialog) dialog; |
this.dialog = (ProgressDialog) dialog; |
100 |
|
if (!dialogMessage.equals("")) { |
101 |
|
this.dialog.setMessage(dialogMessage); |
102 |
|
dialogMessage = ""; |
103 |
|
} |
104 |
break; |
break; |
105 |
} |
} |
106 |
} |
} |
107 |
|
|
|
|
|
|
|
|
|
public void progressDialog() { |
|
|
dialog = new ProgressDialog(this); |
|
|
dialog.setMessage("Wait for location fix"); |
|
|
dialog.setCancelable(false); |
|
|
dialog.show(); |
|
|
} |
|
|
|
|
108 |
public void startLookup() { |
public void startLookup() { |
109 |
isRunning = true; |
isRunning = true; |
110 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
|
//progressDialog(); |
|
111 |
|
|
112 |
locator.locateStations(); |
locator.locateStations(); |
113 |
stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000); |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); |
114 |
} |
} |
115 |
|
|
116 |
|
|
117 |
Handler stationsFetched = new Handler() { |
Handler stationsFetched = new Handler() { |
118 |
@Override |
@Override |
119 |
public void handleMessage(Message msg) { |
public void handleMessage(Message msg) { |
120 |
|
|
121 |
switch (msg.what) { |
switch (msg.what) { |
122 |
case GOTLOCATION: |
case GOTLOCATION: |
123 |
dialog.setMessage("Finding nearby stations"); |
dismissDialog(DLG_PROGRESS); |
124 |
break; |
|
125 |
case GOTSTATIONLIST: |
startLocatorTask(); |
126 |
dialog.dismiss(); |
|
|
adapter.setStations( locator.getStations() ); |
|
127 |
break; |
break; |
128 |
|
|
129 |
case NOPROVIDER: |
case NOPROVIDER: |
130 |
dialog.dismiss(); |
dismissDialog(DLG_PROGRESS); |
131 |
showMessageBox("No Location provider enabled. Plase enabled gps."); |
MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps."); |
132 |
break; |
break; |
133 |
case FIXTIMEOUT: |
case LOCATIONFIXTIMEOUT: |
|
dialog.dismiss(); |
|
134 |
if (isRunning) { |
if (isRunning) { |
135 |
locator.abortLocationListener(); |
locator.stopSearch(); |
136 |
showMessageBox("GPS fix timed out"); |
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; |
break; |
|
case LOOKUPSTATIONFAILED: |
|
|
dialog.dismiss(); |
|
|
showMessageBox("Error on finding nearby stations"); |
|
|
break; |
|
161 |
} |
} |
|
|
|
162 |
isRunning = false; |
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 |
@Override |
176 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
177 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
178 |
|
|
179 |
StationBean station = adapter.getStation(position); |
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); |
Intent intent = new Intent(this, DepartureList.class); |
187 |
intent.putExtra("name", station.getName()); |
intent.putExtra("name", station.getName()); |
|
intent.putExtra("address", station.getAddress()); |
|
188 |
intent.putExtra("distance", station.getDistance()); |
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); |
startActivity(intent); |
194 |
} |
} |
195 |
|
|
196 |
public void showMessageBox(String message) { |
String lookupAddress(double latitude, double longitude) { |
197 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
|
198 |
builder.setMessage(message) |
Geocoder coder = new Geocoder(this, new Locale("da")); |
199 |
.setCancelable(false) |
StringBuilder sb = new StringBuilder(); |
200 |
.setPositiveButton("OK", new DialogInterface.OnClickListener() { |
Log.i("lookupaddr", "" + latitude + "/" + longitude); |
201 |
public void onClick(DialogInterface dialog, int id) { |
try { |
202 |
dialog.dismiss(); |
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 |
.show(); |
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 |
} |
} |