1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
3 |
import android.app.AlertDialog; |
import java.util.ArrayList; |
4 |
|
import java.util.List; |
5 |
|
import java.util.Locale; |
6 |
|
|
7 |
import android.app.Dialog; |
import android.app.Dialog; |
8 |
import android.app.ListActivity; |
import android.app.ListActivity; |
9 |
import android.app.ProgressDialog; |
import android.app.ProgressDialog; |
|
import android.content.DialogInterface; |
|
10 |
import android.content.Intent; |
import android.content.Intent; |
11 |
|
import android.location.Address; |
12 |
|
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; |
17 |
import android.os.Message; |
import android.os.Message; |
18 |
|
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; |
24 |
|
|
25 |
public class TrainInfoList extends ListActivity { |
public class TrainInfoList extends ListActivity { |
26 |
public static final int GOTLOCATION = 1; |
public static final int GOTLOCATION = 1; |
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; |
boolean isRunning = false; |
40 |
|
List<StationBean> stations = new ArrayList<StationBean>(); |
41 |
|
|
42 |
|
StationProvider stationProvider = ProviderFactory.getStationProvider(); |
43 |
|
|
44 |
|
|
45 |
StationListAdapter adapter = null; |
StationListAdapter adapter = null; |
46 |
|
@SuppressWarnings("unchecked") |
47 |
@Override |
@Override |
48 |
public void onCreate(Bundle savedInstanceState) { |
public void onCreate(Bundle savedInstanceState) { |
49 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
50 |
setContentView(R.layout.main); |
setContentView(R.layout.main); |
51 |
|
|
52 |
|
LocationLookup.removeMockLocation(this); |
53 |
//StationLocator.injectMockLocation(this); |
//StationLocator.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) { |
60 |
startLookup(); |
startLookup(); |
61 |
|
} else { |
62 |
|
stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations"); |
63 |
|
adapter.setStations(stations); |
64 |
|
} |
65 |
} |
} |
66 |
|
|
67 |
@Override |
@Override |
68 |
public void onSaveInstanceState(Bundle outState) |
public void onSaveInstanceState(Bundle outState) |
69 |
{ |
{ |
70 |
if (dialog.isShowing()) |
if (dialog != null && dialog.isShowing()) |
71 |
dialog.dismiss(); |
dialog.dismiss(); |
72 |
|
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
73 |
} |
} |
74 |
|
|
75 |
|
|
105 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
106 |
|
|
107 |
locator.locateStations(); |
locator.locateStations(); |
108 |
stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000); |
stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000); |
109 |
} |
} |
110 |
|
|
111 |
|
|
112 |
Handler stationsFetched = new Handler() { |
Handler stationsFetched = new Handler() { |
113 |
@Override |
@Override |
114 |
public void handleMessage(Message msg) { |
public void handleMessage(Message msg) { |
|
|
|
115 |
switch (msg.what) { |
switch (msg.what) { |
116 |
case GOTLOCATION: |
case GOTLOCATION: |
117 |
dialog.setMessage("Finding nearby stations"); |
dialog.setMessage("Finding nearby stations"); |
118 |
locatorTask.execute(); |
locatorTask.execute(); |
119 |
break; |
break; |
120 |
case GOTSTATIONLIST: |
|
|
dialog.dismiss(); |
|
|
adapter.setStations( locator.getStations() ); |
|
|
break; |
|
121 |
case NOPROVIDER: |
case NOPROVIDER: |
122 |
dialog.dismiss(); |
dialog.dismiss(); |
123 |
showMessageBox("No location provider enabled. Plase enable gps."); |
MessageBox.showMessage(TrainInfoList.this,"No location provider enabled. Plase enable gps."); |
124 |
break; |
break; |
125 |
case FIXTIMEOUT: |
case FIXTIMEOUT: |
126 |
dialog.dismiss(); |
dialog.dismiss(); |
127 |
if (isRunning) { |
if (isRunning) { |
128 |
locator.abortLocationListener(); |
locator.abortLocationListener(); |
129 |
showMessageBox("GPS fix timed out"); |
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 |
} |
} |
136 |
break; |
break; |
137 |
case LOOKUPSTATIONFAILED: |
case LOOKUPSTATIONFAILED: |
138 |
dialog.dismiss(); |
dialog.dismiss(); |
139 |
showMessageBox("Error on finding nearby stations"); |
MessageBox.showMessage(TrainInfoList.this,"Error on finding nearby stations"); |
140 |
break; |
break; |
141 |
} |
} |
|
|
|
142 |
isRunning = false; |
isRunning = false; |
143 |
} |
} |
144 |
}; |
}; |
148 |
@Override |
@Override |
149 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
150 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
151 |
|
|
152 |
StationBean station = adapter.getStation(position); |
StationBean station = stations.get(position); |
153 |
|
|
154 |
|
double latitude = station.getLatitude(); |
155 |
|
double longitude = station.getLongitude(); |
156 |
|
|
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", station.getAddress()); |
|
161 |
intent.putExtra("distance", station.getDistance()); |
intent.putExtra("distance", station.getDistance()); |
162 |
intent.putExtra("latitude", station.getLatitude()); |
intent.putExtra("latitude", latitude); |
163 |
intent.putExtra("longitude", station.getLongitude()); |
intent.putExtra("longitude", longitude); |
164 |
|
intent.putExtra("stationid", station.getId()); |
165 |
|
intent.putExtra("address", station.getAddress()); |
166 |
startActivity(intent); |
startActivity(intent); |
167 |
} |
} |
168 |
|
|
169 |
public void showMessageBox(String message) { |
String lookupAddress(double latitude, double longitude) { |
170 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
|
171 |
builder.setMessage(message) |
Geocoder coder = new Geocoder(this, new Locale("da")); |
172 |
.setCancelable(false) |
StringBuilder sb = new StringBuilder(); |
173 |
.setPositiveButton("OK", new DialogInterface.OnClickListener() { |
Log.i("lookupaddr", "" + latitude + "/" + longitude); |
174 |
public void onClick(DialogInterface dialog, int id) { |
try { |
175 |
dialog.dismiss(); |
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 |
.show(); |
|
188 |
|
} catch (Exception e) { |
189 |
|
Log.e("DepartureList", "geocoder failed", e); |
190 |
|
} |
191 |
|
|
192 |
|
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 |
|
|
200 |
super.onPreExecute(); |
super.onPreExecute(); |
201 |
} |
} |
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 |
} |
} |
217 |
|
|
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 |
} |
} |