10 |
import android.content.Intent; |
import android.content.Intent; |
11 |
import android.location.Address; |
import android.location.Address; |
12 |
import android.location.Geocoder; |
import android.location.Geocoder; |
13 |
|
import android.location.Location; |
14 |
import android.os.AsyncTask; |
import android.os.AsyncTask; |
15 |
import android.os.Bundle; |
import android.os.Bundle; |
16 |
import android.os.Handler; |
import android.os.Handler; |
18 |
import android.util.Log; |
import android.util.Log; |
19 |
import android.view.View; |
import android.view.View; |
20 |
import android.widget.ListView; |
import android.widget.ListView; |
21 |
|
import dk.thoerup.traininfo.provider.ProviderFactory; |
22 |
|
import dk.thoerup.traininfo.provider.StationProvider; |
23 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
24 |
|
|
25 |
public class TrainInfoList extends ListActivity { |
public class TrainInfoList extends ListActivity { |
33 |
|
|
34 |
/** Called when the activity is first created. */ |
/** Called when the activity is first created. */ |
35 |
ProgressDialog dialog; |
ProgressDialog dialog; |
36 |
StationLocator locator = null; |
LocationLookup locator = null; |
37 |
LocatorTask locatorTask = new LocatorTask(); |
LocatorTask locatorTask = new LocatorTask(); |
38 |
|
|
39 |
boolean isRunning = false; |
boolean isRunning = false; |
40 |
List<StationBean> stations = new ArrayList<StationBean>(); |
List<StationBean> stations = new ArrayList<StationBean>(); |
41 |
|
|
42 |
|
StationProvider stationProvider = ProviderFactory.getStationProvider(); |
43 |
|
|
44 |
|
|
45 |
StationListAdapter adapter = null; |
StationListAdapter adapter = null; |
46 |
@SuppressWarnings("unchecked") |
@SuppressWarnings("unchecked") |
49 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
50 |
setContentView(R.layout.main); |
setContentView(R.layout.main); |
51 |
|
|
52 |
//StationLocator.removeMockLocation(this); |
//LocationLookup.removeMockLocation(this); |
53 |
StationLocator.injectMockLocation(this); |
LocationLookup.injectMockLocation(this); |
54 |
|
|
55 |
adapter = new StationListAdapter(this); |
adapter = new StationListAdapter(this); |
56 |
setListAdapter(adapter); |
setListAdapter(adapter); |
57 |
|
|
58 |
locator = new StationLocator(this, stationsFetched); |
locator = new LocationLookup(this, stationsFetched); |
59 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
60 |
startLookup(); |
startLookup(); |
61 |
} else { |
} else { |
117 |
dialog.setMessage("Finding nearby stations"); |
dialog.setMessage("Finding nearby stations"); |
118 |
locatorTask.execute(); |
locatorTask.execute(); |
119 |
break; |
break; |
120 |
case GOTSTATIONLIST: |
|
|
dialog.dismiss(); |
|
|
if (locator.getStations().size() == 0) |
|
|
MessageBox.showMessage(TrainInfoList.this,"Error loading station list!"); |
|
|
stations = locator.getStations(); |
|
|
adapter.setStations( stations ); |
|
|
break; |
|
121 |
case NOPROVIDER: |
case NOPROVIDER: |
122 |
dialog.dismiss(); |
dialog.dismiss(); |
123 |
MessageBox.showMessage(TrainInfoList.this,"No location provider enabled. Plase enable gps."); |
MessageBox.showMessage(TrainInfoList.this,"No location provider enabled. Plase enable gps."); |
153 |
|
|
154 |
double latitude = station.getLatitude(); |
double latitude = station.getLatitude(); |
155 |
double longitude = station.getLongitude(); |
double longitude = station.getLongitude(); |
|
String addr = station.getAddress(); |
|
156 |
|
|
|
if (addr == null || addr.trim().equals("") ) |
|
|
addr = lookupAddress(latitude, longitude); |
|
157 |
|
|
158 |
|
|
159 |
Intent intent = new Intent(this, DepartureList.class); |
Intent intent = new Intent(this, DepartureList.class); |
160 |
intent.putExtra("name", station.getName()); |
intent.putExtra("name", station.getName()); |
|
intent.putExtra("address", addr); |
|
161 |
intent.putExtra("distance", station.getDistance()); |
intent.putExtra("distance", station.getDistance()); |
162 |
intent.putExtra("latitude", latitude); |
intent.putExtra("latitude", latitude); |
163 |
intent.putExtra("longitude", longitude); |
intent.putExtra("longitude", longitude); |
164 |
intent.putExtra("stationid", station.getId()); |
intent.putExtra("stationid", station.getId()); |
165 |
|
intent.putExtra("address", station.getAddress()); |
166 |
startActivity(intent); |
startActivity(intent); |
167 |
} |
} |
168 |
|
|
192 |
return sb.toString(); |
return sb.toString(); |
193 |
} |
} |
194 |
|
|
|
|
|
195 |
class LocatorTask extends AsyncTask<Void,Void,Void> { |
class LocatorTask extends AsyncTask<Void,Void,Void> { |
196 |
|
boolean success; |
197 |
@Override |
@Override |
198 |
protected void onPreExecute() { |
protected void onPreExecute() { |
199 |
|
|
202 |
|
|
203 |
@Override |
@Override |
204 |
protected Void doInBackground(Void... params) { |
protected Void doInBackground(Void... params) { |
205 |
locator.findNearestStations(); |
Location loc = locator.getLocation(); |
206 |
|
success = stationProvider.lookupStations(loc); |
207 |
|
|
208 |
|
|
209 |
|
List<StationBean> stations = stationProvider.getStations(); |
210 |
|
for (StationBean station : stations) { |
211 |
|
String addr = lookupAddress(station.getLatitude(), station.getLongitude()); |
212 |
|
station.setAddress(addr); |
213 |
|
} |
214 |
|
|
215 |
return null; |
return null; |
216 |
} |
} |
218 |
@Override |
@Override |
219 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
220 |
super.onPostExecute(result); |
super.onPostExecute(result); |
221 |
|
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 |
} |
} |
233 |
} |
} |
234 |
} |
} |