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; |
24 |
import dk.thoerup.traininfo.provider.StationProvider; |
import dk.thoerup.traininfo.provider.StationProvider; |
25 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
26 |
|
|
27 |
public class TrainInfoList extends ListActivity { |
public class StationList extends ListActivity { |
28 |
public static final int GOTLOCATION = 1; |
public static final int GOTLOCATION = 1001; |
29 |
public static final int GOTSTATIONLIST = 2; |
public static final int GOTSTATIONLIST = 1002; |
30 |
public static final int NOPROVIDER = 3; |
public static final int NOPROVIDER = 1003; |
31 |
public static final int FIXTIMEOUT = 4; |
public static final int LOCATIONFIXTIMEOUT = 1004; |
32 |
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 |
LocationLookup locator = null; |
LocationLookup locator = null; |
40 |
LocatorTask locatorTask = new LocatorTask(); |
LocatorTask locatorTask; |
41 |
|
|
42 |
boolean isRunning = false; |
boolean isRunning = false; |
43 |
List<StationBean> stations = new ArrayList<StationBean>(); |
List<StationBean> stations = new ArrayList<StationBean>(); |
84 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
85 |
dlg.setMessage("Wait for location fix"); |
dlg.setMessage("Wait for location fix"); |
86 |
dlg.setCancelable(false); |
dlg.setCancelable(false); |
87 |
return dlg; |
return dlg; |
88 |
default: |
default: |
89 |
return super.onCreateDialog(id); |
return super.onCreateDialog(id); |
90 |
} |
} |
99 |
switch (id) { |
switch (id) { |
100 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
101 |
this.dialog = (ProgressDialog) dialog; |
this.dialog = (ProgressDialog) dialog; |
102 |
|
if (!dialogMessage.equals("")) { |
103 |
|
this.dialog.setMessage(dialogMessage); |
104 |
|
dialogMessage = ""; |
105 |
|
} |
106 |
break; |
break; |
107 |
} |
} |
108 |
} |
} |
112 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
113 |
|
|
114 |
locator.locateStations(); |
locator.locateStations(); |
115 |
stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000); |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); |
116 |
} |
} |
117 |
|
|
118 |
|
|
119 |
Handler stationsFetched = new Handler() { |
Handler stationsFetched = new Handler() { |
120 |
@Override |
@Override |
121 |
public void handleMessage(Message msg) { |
public void handleMessage(Message msg) { |
122 |
|
|
123 |
switch (msg.what) { |
switch (msg.what) { |
124 |
case GOTLOCATION: |
case GOTLOCATION: |
125 |
dialog.setMessage("Finding nearby stations"); |
dismissDialog(DLG_PROGRESS); |
126 |
locatorTask.execute(); |
|
127 |
|
startLocatorTask(); |
128 |
|
|
129 |
break; |
break; |
130 |
|
|
131 |
case NOPROVIDER: |
case NOPROVIDER: |
132 |
dialog.dismiss(); |
dismissDialog(DLG_PROGRESS); |
133 |
MessageBox.showMessage(TrainInfoList.this,"No location provider enabled. Plase enable gps."); |
MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps."); |
134 |
break; |
break; |
135 |
case FIXTIMEOUT: |
case LOCATIONFIXTIMEOUT: |
|
dialog.dismiss(); |
|
136 |
if (isRunning) { |
if (isRunning) { |
137 |
locator.abortLocationListener(); |
locator.stopSearch(); |
138 |
if (locator.hasLocation()) { |
if (locator.hasLocation()) { |
139 |
msg.what = GOTLOCATION; |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
140 |
handleMessage( msg ); // ToDo: ugly recursive call !!! |
} else { |
141 |
} else { |
dismissDialog(DLG_PROGRESS); |
142 |
MessageBox.showMessage(TrainInfoList.this,"GPS fix timed out"); |
|
143 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
144 |
|
builder.setMessage("GPS fix timed out"); |
145 |
|
builder.setCancelable(true); |
146 |
|
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
147 |
|
public void onClick(DialogInterface dialog, int id) { |
148 |
|
dialog.dismiss(); |
149 |
|
startLookup(); |
150 |
|
|
151 |
|
} |
152 |
|
}); |
153 |
|
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
154 |
|
public void onClick(DialogInterface dialog, int id) { |
155 |
|
dialog.dismiss(); |
156 |
|
} |
157 |
|
}); |
158 |
|
builder.show(); |
159 |
|
|
160 |
} |
} |
161 |
} |
} |
162 |
break; |
break; |
|
case LOOKUPSTATIONFAILED: |
|
|
dialog.dismiss(); |
|
|
MessageBox.showMessage(TrainInfoList.this,"Error on finding nearby stations"); |
|
|
break; |
|
163 |
} |
} |
164 |
isRunning = false; |
isRunning = false; |
165 |
} |
} |
166 |
}; |
}; |
167 |
|
|
168 |
|
void startLocatorTask() |
169 |
|
{ |
170 |
|
dialogMessage = "Finding nearby stations"; |
171 |
|
showDialog(DLG_PROGRESS); |
172 |
|
|
173 |
|
locatorTask = new LocatorTask(); |
174 |
|
locatorTask.execute(); |
175 |
|
} |
176 |
|
|
177 |
@Override |
@Override |
178 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
251 |
|
|
252 |
if (success) { |
if (success) { |
253 |
if (stationProvider.getStations().size() == 0) |
if (stationProvider.getStations().size() == 0) |
254 |
MessageBox.showMessage(TrainInfoList.this, "No stations found!"); // this should not be possible !?! |
MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! |
255 |
stations = stationProvider.getStations(); |
stations = stationProvider.getStations(); |
256 |
adapter.setStations( stations ); |
adapter.setStations( stations ); |
257 |
|
|
258 |
} else { //communication or parse errors |
} else { //communication or parse errors |
259 |
MessageBox.showMessage(TrainInfoList.this, "Error finding stations!"); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
260 |
|
builder.setMessage("Error on finding nearby stations"); |
261 |
|
builder.setCancelable(true); |
262 |
|
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
263 |
|
public void onClick(DialogInterface dialog, int id) { |
264 |
|
dialog.dismiss(); |
265 |
|
|
266 |
|
stationsFetched.post( new Runnable() { |
267 |
|
@Override |
268 |
|
public void run() { |
269 |
|
startLocatorTask(); |
270 |
|
} |
271 |
|
}); |
272 |
|
} |
273 |
|
}); |
274 |
|
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
275 |
|
public void onClick(DialogInterface dialog, int id) { |
276 |
|
dialog.dismiss(); |
277 |
|
} |
278 |
|
}); |
279 |
|
builder.show(); |
280 |
} |
} |
281 |
} |
} |
282 |
} |
} |