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.os.AsyncTask; |
import android.os.AsyncTask; |
14 |
import android.os.Bundle; |
import android.os.Bundle; |
15 |
import android.os.Handler; |
import android.os.Handler; |
16 |
import android.os.Message; |
import android.os.Message; |
17 |
|
import android.util.Log; |
18 |
import android.view.View; |
import android.view.View; |
19 |
import android.widget.ListView; |
import android.widget.ListView; |
20 |
|
import dk.thoerup.traininfo.util.MessageBox; |
21 |
|
|
22 |
public class TrainInfoList extends ListActivity { |
public class TrainInfoList extends ListActivity { |
23 |
public static final int GOTLOCATION = 1; |
public static final int GOTLOCATION = 1; |
33 |
StationLocator locator = null; |
StationLocator locator = null; |
34 |
LocatorTask locatorTask = new LocatorTask(); |
LocatorTask locatorTask = new LocatorTask(); |
35 |
|
|
36 |
boolean isRunning; |
boolean isRunning = false; |
37 |
|
List<StationBean> stations = new ArrayList<StationBean>(); |
38 |
|
|
39 |
|
|
40 |
StationListAdapter adapter = null; |
StationListAdapter adapter = null; |
41 |
|
@SuppressWarnings("unchecked") |
42 |
@Override |
@Override |
43 |
public void onCreate(Bundle savedInstanceState) { |
public void onCreate(Bundle savedInstanceState) { |
44 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
45 |
setContentView(R.layout.main); |
setContentView(R.layout.main); |
46 |
|
|
47 |
|
StationLocator.removeMockLocation(this); |
48 |
//StationLocator.injectMockLocation(this); |
//StationLocator.injectMockLocation(this); |
49 |
|
|
50 |
adapter = new StationListAdapter(this); |
adapter = new StationListAdapter(this); |
51 |
setListAdapter(adapter); |
setListAdapter(adapter); |
52 |
|
|
53 |
locator = new StationLocator(this, stationsFetched); |
locator = new StationLocator(this, stationsFetched); |
54 |
|
if (savedInstanceState == null) { |
55 |
startLookup(); |
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 |
@Override |
100 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
101 |
|
|
102 |
locator.locateStations(); |
locator.locateStations(); |
103 |
stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000); |
stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000); |
104 |
} |
} |
105 |
|
|
106 |
|
|
107 |
Handler stationsFetched = new Handler() { |
Handler stationsFetched = new Handler() { |
108 |
@Override |
@Override |
109 |
public void handleMessage(Message msg) { |
public void handleMessage(Message msg) { |
|
|
|
110 |
switch (msg.what) { |
switch (msg.what) { |
111 |
case GOTLOCATION: |
case GOTLOCATION: |
112 |
dialog.setMessage("Finding nearby stations"); |
dialog.setMessage("Finding nearby stations"); |
114 |
break; |
break; |
115 |
case GOTSTATIONLIST: |
case GOTSTATIONLIST: |
116 |
dialog.dismiss(); |
dialog.dismiss(); |
117 |
adapter.setStations( locator.getStations() ); |
if (locator.getStations().size() == 0) |
118 |
|
MessageBox.showMessage(TrainInfoList.this,"Error loading station list!"); |
119 |
|
stations = locator.getStations(); |
120 |
|
adapter.setStations( stations ); |
121 |
break; |
break; |
122 |
case NOPROVIDER: |
case NOPROVIDER: |
123 |
dialog.dismiss(); |
dialog.dismiss(); |
124 |
showMessageBox("No location provider enabled. Plase enable gps."); |
MessageBox.showMessage(TrainInfoList.this,"No location provider enabled. Plase enable gps."); |
125 |
break; |
break; |
126 |
case FIXTIMEOUT: |
case FIXTIMEOUT: |
127 |
dialog.dismiss(); |
dialog.dismiss(); |
128 |
if (isRunning) { |
if (isRunning) { |
129 |
locator.abortLocationListener(); |
locator.abortLocationListener(); |
130 |
showMessageBox("GPS fix timed out"); |
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; |
break; |
138 |
case LOOKUPSTATIONFAILED: |
case LOOKUPSTATIONFAILED: |
139 |
dialog.dismiss(); |
dialog.dismiss(); |
140 |
showMessageBox("Error on finding nearby stations"); |
MessageBox.showMessage(TrainInfoList.this,"Error on finding nearby stations"); |
141 |
break; |
break; |
142 |
} |
} |
|
|
|
143 |
isRunning = false; |
isRunning = false; |
144 |
} |
} |
145 |
}; |
}; |
149 |
@Override |
@Override |
150 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
151 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
152 |
|
|
153 |
StationBean station = adapter.getStation(position); |
StationBean station = stations.get(position); |
154 |
|
|
155 |
|
double latitude = station.getLatitude(); |
156 |
|
double longitude = station.getLongitude(); |
157 |
|
String addr = station.getAddress(); |
158 |
|
|
159 |
|
if (addr == null || addr.trim().equals("") ) |
160 |
|
addr = lookupAddress(latitude, longitude); |
161 |
|
|
162 |
|
|
163 |
Intent intent = new Intent(this, DepartureList.class); |
Intent intent = new Intent(this, DepartureList.class); |
164 |
intent.putExtra("name", station.getName()); |
intent.putExtra("name", station.getName()); |
165 |
intent.putExtra("address", station.getAddress()); |
intent.putExtra("address", addr); |
166 |
intent.putExtra("distance", station.getDistance()); |
intent.putExtra("distance", station.getDistance()); |
167 |
intent.putExtra("latitude", station.getLatitude()); |
intent.putExtra("latitude", latitude); |
168 |
intent.putExtra("longitude", station.getLongitude()); |
intent.putExtra("longitude", longitude); |
169 |
|
intent.putExtra("code", station.getCode()); |
170 |
startActivity(intent); |
startActivity(intent); |
171 |
} |
} |
172 |
|
|
173 |
public void showMessageBox(String message) { |
String lookupAddress(double latitude, double longitude) { |
174 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
|
175 |
builder.setMessage(message) |
Geocoder coder = new Geocoder(this, new Locale("da")); |
176 |
.setCancelable(false) |
StringBuilder sb = new StringBuilder(); |
177 |
.setPositiveButton("OK", new DialogInterface.OnClickListener() { |
Log.i("lookupaddr", "" + latitude + "/" + longitude); |
178 |
public void onClick(DialogInterface dialog, int id) { |
try { |
179 |
dialog.dismiss(); |
List<Address> addressList = coder.getFromLocation(latitude, longitude, 1); |
180 |
|
Address addr = addressList.get(0); |
181 |
|
|
182 |
|
|
183 |
|
int max = addr.getMaxAddressLineIndex(); |
184 |
|
for (int i=0; i<max; i++) { |
185 |
|
if (i>0) |
186 |
|
sb.append(", "); |
187 |
|
|
188 |
|
sb.append(addr.getAddressLine(i)); |
189 |
} |
} |
190 |
}) |
|
191 |
.show(); |
|
192 |
|
} catch (Exception e) { |
193 |
|
Log.e("DepartureList", "geocoder failed", e); |
194 |
|
} |
195 |
|
|
196 |
|
return sb.toString(); |
197 |
} |
} |
198 |
|
|
199 |
|
|
200 |
class LocatorTask extends AsyncTask<Void,Void,Void> { |
class LocatorTask extends AsyncTask<Void,Void,Void> { |
201 |
@Override |
@Override |
202 |
protected void onPreExecute() { |
protected void onPreExecute() { |
203 |
|
|
204 |
super.onPreExecute(); |
super.onPreExecute(); |
205 |
} |
} |
206 |
|
|
207 |
@Override |
@Override |
208 |
protected Void doInBackground(Void... params) { |
protected Void doInBackground(Void... params) { |
209 |
locator.findNearestStations(); |
locator.findNearestStations(); |
210 |
|
|
211 |
return null; |
return null; |
212 |
} |
} |
213 |
|
|
214 |
@Override |
@Override |
215 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
216 |
super.onPostExecute(result); |
super.onPostExecute(result); |
217 |
|
|
218 |
} |
} |
219 |
} |
} |
220 |
} |
} |