4 |
import java.util.List; |
import java.util.List; |
5 |
import java.util.Locale; |
import java.util.Locale; |
6 |
|
|
7 |
|
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; |
12 |
import android.content.Intent; |
import android.content.Intent; |
13 |
import android.location.Address; |
import android.location.Address; |
14 |
import android.location.Geocoder; |
import android.location.Geocoder; |
15 |
|
import android.location.Location; |
16 |
import android.os.AsyncTask; |
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; |
import android.util.Log; |
21 |
|
import android.view.Menu; |
22 |
|
import android.view.MenuItem; |
23 |
import android.view.View; |
import android.view.View; |
24 |
import android.widget.ListView; |
import android.widget.ListView; |
25 |
|
|
26 |
|
|
27 |
|
import dk.thoerup.traininfo.provider.ProviderFactory; |
28 |
|
import dk.thoerup.traininfo.provider.StationProvider; |
29 |
|
import dk.thoerup.traininfo.stationmap.GeoPair; |
30 |
|
import dk.thoerup.traininfo.stationmap.StationMapView; |
31 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
32 |
|
|
33 |
public class TrainInfoList extends ListActivity { |
public class StationList extends ListActivity { |
34 |
public static final int GOTLOCATION = 1; |
public static final int GOTLOCATION = 1001; |
35 |
public static final int GOTSTATIONLIST = 2; |
public static final int GOTSTATIONLIST = 1002; |
36 |
public static final int NOPROVIDER = 3; |
public static final int NOPROVIDER = 1003; |
37 |
public static final int FIXTIMEOUT = 4; |
public static final int LOCATIONFIXTIMEOUT = 1004; |
38 |
public static final int LOOKUPSTATIONFAILED = 5; |
|
39 |
|
public static final int OPTIONS_MAP = 2001; |
40 |
|
public static final int OPTIONS_ABOUT = 2002; |
41 |
|
|
42 |
|
|
43 |
public static final int DLG_PROGRESS = 1; |
public static final int DLG_PROGRESS = 1001; |
44 |
|
|
45 |
/** Called when the activity is first created. */ |
/** Called when the activity is first created. */ |
46 |
|
String dialogMessage = ""; |
47 |
ProgressDialog dialog; |
ProgressDialog dialog; |
48 |
StationLocator locator = null; |
LocationLookup locator = null; |
49 |
LocatorTask locatorTask = new LocatorTask(); |
LocatorTask locatorTask; |
50 |
|
|
51 |
boolean isRunning = false; |
boolean isRunning = false; |
52 |
List<StationBean> stations = new ArrayList<StationBean>(); |
List<StationBean> stations = new ArrayList<StationBean>(); |
53 |
|
|
54 |
|
StationProvider stationProvider = ProviderFactory.getStationProvider(); |
55 |
|
|
56 |
|
|
57 |
StationListAdapter adapter = null; |
StationListAdapter adapter = null; |
58 |
@SuppressWarnings("unchecked") |
@SuppressWarnings("unchecked") |
61 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
62 |
setContentView(R.layout.main); |
setContentView(R.layout.main); |
63 |
|
|
|
StationLocator.removeMockLocation(this); |
|
|
//StationLocator.injectMockLocation(this); |
|
64 |
|
|
65 |
adapter = new StationListAdapter(this); |
adapter = new StationListAdapter(this); |
66 |
setListAdapter(adapter); |
setListAdapter(adapter); |
67 |
|
|
68 |
locator = new StationLocator(this, stationsFetched); |
locator = new LocationLookup(this, stationsFetched); |
69 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
70 |
startLookup(); |
startLookup(); |
71 |
} else { |
} else { |
73 |
adapter.setStations(stations); |
adapter.setStations(stations); |
74 |
} |
} |
75 |
} |
} |
76 |
|
|
77 |
|
|
78 |
@Override |
@Override |
79 |
public void onSaveInstanceState(Bundle outState) |
public void onSaveInstanceState(Bundle outState) |
80 |
{ |
{ |
86 |
|
|
87 |
|
|
88 |
@Override |
@Override |
89 |
|
public boolean onCreateOptionsMenu(Menu menu) { |
90 |
|
menu.add(0, OPTIONS_MAP, 0, "Show station map"); |
91 |
|
menu.add(0, OPTIONS_ABOUT, 0, "About"); |
92 |
|
|
93 |
|
return true; |
94 |
|
} |
95 |
|
|
96 |
|
@Override |
97 |
|
public boolean onOptionsItemSelected(MenuItem item) { |
98 |
|
boolean retval; |
99 |
|
|
100 |
|
switch (item.getItemId()) { |
101 |
|
case OPTIONS_MAP: |
102 |
|
|
103 |
|
Intent intent = new Intent(this,StationMapView.class); |
104 |
|
intent.putExtra("userlocation", GeoPair.fromLocation(locator.getLocation()) ); |
105 |
|
|
106 |
|
ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>(); |
107 |
|
for (StationBean st : stations ) { |
108 |
|
stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) ); |
109 |
|
} |
110 |
|
|
111 |
|
intent.putExtra("stations", stationPoints); |
112 |
|
|
113 |
|
startActivity(intent); |
114 |
|
retval = true; |
115 |
|
break; |
116 |
|
case OPTIONS_ABOUT: |
117 |
|
String ver = this.getResources().getString(R.string.app_version); |
118 |
|
|
119 |
|
StringBuffer message = new StringBuffer(); |
120 |
|
message.append("TrainInfo DK v").append(ver).append("\n"); |
121 |
|
message.append("By Torben Nielsen\n"); |
122 |
|
|
123 |
|
MessageBox.showMessage(this, message.toString()); |
124 |
|
retval = true; |
125 |
|
break; |
126 |
|
default: |
127 |
|
retval = super.onOptionsItemSelected(item); |
128 |
|
} |
129 |
|
|
130 |
|
return retval; |
131 |
|
} |
132 |
|
|
133 |
|
@Override |
134 |
protected Dialog onCreateDialog(int id) { |
protected Dialog onCreateDialog(int id) { |
135 |
switch (id) { |
switch (id) { |
136 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
137 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
138 |
dlg.setMessage("Wait for location fix"); |
dlg.setMessage("Wait for location fix"); |
139 |
dlg.setCancelable(false); |
dlg.setCancelable(false); |
140 |
return dlg; |
return dlg; |
141 |
default: |
default: |
142 |
return super.onCreateDialog(id); |
return super.onCreateDialog(id); |
143 |
} |
} |
152 |
switch (id) { |
switch (id) { |
153 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
154 |
this.dialog = (ProgressDialog) dialog; |
this.dialog = (ProgressDialog) dialog; |
155 |
|
if (!dialogMessage.equals("")) { |
156 |
|
this.dialog.setMessage(dialogMessage); |
157 |
|
dialogMessage = ""; |
158 |
|
} |
159 |
break; |
break; |
160 |
} |
} |
161 |
} |
} |
165 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
166 |
|
|
167 |
locator.locateStations(); |
locator.locateStations(); |
168 |
stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000); |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); |
169 |
} |
} |
170 |
|
|
171 |
|
|
172 |
Handler stationsFetched = new Handler() { |
Handler stationsFetched = new Handler() { |
173 |
@Override |
@Override |
174 |
public void handleMessage(Message msg) { |
public void handleMessage(Message msg) { |
175 |
|
|
176 |
switch (msg.what) { |
switch (msg.what) { |
177 |
case GOTLOCATION: |
case GOTLOCATION: |
178 |
dialog.setMessage("Finding nearby stations"); |
dismissDialog(DLG_PROGRESS); |
179 |
locatorTask.execute(); |
|
180 |
break; |
startLocatorTask(); |
181 |
case GOTSTATIONLIST: |
|
|
dialog.dismiss(); |
|
|
if (locator.getStations().size() == 0) |
|
|
MessageBox.showMessage(TrainInfoList.this,"Error loading station list!"); |
|
|
stations = locator.getStations(); |
|
|
adapter.setStations( stations ); |
|
182 |
break; |
break; |
183 |
|
|
184 |
case NOPROVIDER: |
case NOPROVIDER: |
185 |
dialog.dismiss(); |
dismissDialog(DLG_PROGRESS); |
186 |
MessageBox.showMessage(TrainInfoList.this,"No location provider enabled. Plase enable gps."); |
MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps."); |
187 |
break; |
break; |
188 |
case FIXTIMEOUT: |
case LOCATIONFIXTIMEOUT: |
|
dialog.dismiss(); |
|
189 |
if (isRunning) { |
if (isRunning) { |
190 |
locator.abortLocationListener(); |
locator.stopSearch(); |
191 |
if (locator.hasLocation()) { |
if (locator.hasLocation()) { |
192 |
msg.what = GOTLOCATION; |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
193 |
handleMessage( msg ); // ToDo: ugly recursive call !!! |
} else { |
194 |
} else { |
dismissDialog(DLG_PROGRESS); |
195 |
MessageBox.showMessage(TrainInfoList.this,"GPS fix timed out"); |
|
196 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
197 |
|
builder.setMessage("GPS fix timed out"); |
198 |
|
builder.setCancelable(true); |
199 |
|
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
200 |
|
public void onClick(DialogInterface dialog, int id) { |
201 |
|
dialog.dismiss(); |
202 |
|
startLookup(); |
203 |
|
|
204 |
|
} |
205 |
|
}); |
206 |
|
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
207 |
|
public void onClick(DialogInterface dialog, int id) { |
208 |
|
dialog.dismiss(); |
209 |
|
} |
210 |
|
}); |
211 |
|
builder.show(); |
212 |
|
|
213 |
} |
} |
214 |
} |
} |
215 |
break; |
break; |
|
case LOOKUPSTATIONFAILED: |
|
|
dialog.dismiss(); |
|
|
MessageBox.showMessage(TrainInfoList.this,"Error on finding nearby stations"); |
|
|
break; |
|
216 |
} |
} |
217 |
isRunning = false; |
isRunning = false; |
218 |
} |
} |
219 |
}; |
}; |
220 |
|
|
221 |
|
void startLocatorTask() |
222 |
|
{ |
223 |
|
dialogMessage = "Finding nearby stations"; |
224 |
|
showDialog(DLG_PROGRESS); |
225 |
|
|
226 |
|
locatorTask = new LocatorTask(); |
227 |
|
locatorTask.execute(); |
228 |
|
} |
229 |
|
|
230 |
@Override |
@Override |
231 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
275 |
} |
} |
276 |
|
|
277 |
class LocatorTask extends AsyncTask<Void,Void,Void> { |
class LocatorTask extends AsyncTask<Void,Void,Void> { |
278 |
|
boolean success; |
279 |
@Override |
@Override |
280 |
protected void onPreExecute() { |
protected void onPreExecute() { |
281 |
|
|
284 |
|
|
285 |
@Override |
@Override |
286 |
protected Void doInBackground(Void... params) { |
protected Void doInBackground(Void... params) { |
287 |
locator.findNearestStations(); |
Location loc = locator.getLocation(); |
288 |
|
success = stationProvider.lookupStations(loc); |
289 |
|
|
290 |
|
|
291 |
List<StationBean> stations = locator.getStations(); |
List<StationBean> stations = stationProvider.getStations(); |
292 |
for (StationBean station : stations) { |
for (StationBean station : stations) { |
293 |
String addr = lookupAddress(station.getLatitude(), station.getLongitude()); |
String addr = lookupAddress(station.getLatitude(), station.getLongitude()); |
294 |
station.setAddress(addr); |
station.setAddress(addr); |
300 |
@Override |
@Override |
301 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
302 |
super.onPostExecute(result); |
super.onPostExecute(result); |
303 |
|
dialog.dismiss(); |
304 |
|
|
305 |
|
if (success) { |
306 |
|
if (stationProvider.getStations().size() == 0) |
307 |
|
MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! |
308 |
|
stations = stationProvider.getStations(); |
309 |
|
adapter.setStations( stations ); |
310 |
|
|
311 |
|
} else { //communication or parse errors |
312 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
313 |
|
builder.setMessage("Error on finding nearby stations"); |
314 |
|
builder.setCancelable(true); |
315 |
|
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
316 |
|
public void onClick(DialogInterface dialog, int id) { |
317 |
|
dialog.dismiss(); |
318 |
|
|
319 |
|
stationsFetched.post( new Runnable() { |
320 |
|
@Override |
321 |
|
public void run() { |
322 |
|
startLocatorTask(); |
323 |
|
} |
324 |
|
}); |
325 |
|
} |
326 |
|
}); |
327 |
|
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
328 |
|
public void onClick(DialogInterface dialog, int id) { |
329 |
|
dialog.dismiss(); |
330 |
|
} |
331 |
|
}); |
332 |
|
builder.show(); |
333 |
|
} |
334 |
} |
} |
335 |
} |
} |
336 |
} |
} |