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>(); |
52 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
53 |
setContentView(R.layout.main); |
setContentView(R.layout.main); |
54 |
|
|
|
LocationLookup.removeMockLocation(this); |
|
|
//LocationLookup.injectMockLocation(this); |
|
55 |
|
|
56 |
adapter = new StationListAdapter(this); |
adapter = new StationListAdapter(this); |
57 |
setListAdapter(adapter); |
setListAdapter(adapter); |
82 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
83 |
dlg.setMessage("Wait for location fix"); |
dlg.setMessage("Wait for location fix"); |
84 |
dlg.setCancelable(false); |
dlg.setCancelable(false); |
85 |
return dlg; |
return dlg; |
86 |
default: |
default: |
87 |
return super.onCreateDialog(id); |
return super.onCreateDialog(id); |
88 |
} |
} |
97 |
switch (id) { |
switch (id) { |
98 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
99 |
this.dialog = (ProgressDialog) dialog; |
this.dialog = (ProgressDialog) dialog; |
100 |
|
if (!dialogMessage.equals("")) { |
101 |
|
this.dialog.setMessage(dialogMessage); |
102 |
|
dialogMessage = ""; |
103 |
|
} |
104 |
break; |
break; |
105 |
} |
} |
106 |
} |
} |
110 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
111 |
|
|
112 |
locator.locateStations(); |
locator.locateStations(); |
113 |
stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000); |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); |
114 |
} |
} |
115 |
|
|
116 |
|
|
117 |
Handler stationsFetched = new Handler() { |
Handler stationsFetched = new Handler() { |
118 |
@Override |
@Override |
119 |
public void handleMessage(Message msg) { |
public void handleMessage(Message msg) { |
120 |
|
|
121 |
switch (msg.what) { |
switch (msg.what) { |
122 |
case GOTLOCATION: |
case GOTLOCATION: |
123 |
dialog.setMessage("Finding nearby stations"); |
dismissDialog(DLG_PROGRESS); |
124 |
locatorTask.execute(); |
|
125 |
|
startLocatorTask(); |
126 |
|
|
127 |
break; |
break; |
128 |
|
|
129 |
case NOPROVIDER: |
case NOPROVIDER: |
130 |
dialog.dismiss(); |
dismissDialog(DLG_PROGRESS); |
131 |
MessageBox.showMessage(TrainInfoList.this,"No location provider enabled. Plase enable gps."); |
MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps."); |
132 |
break; |
break; |
133 |
case FIXTIMEOUT: |
case LOCATIONFIXTIMEOUT: |
|
dialog.dismiss(); |
|
134 |
if (isRunning) { |
if (isRunning) { |
135 |
locator.abortLocationListener(); |
locator.stopSearch(); |
136 |
if (locator.hasLocation()) { |
if (locator.hasLocation()) { |
137 |
msg.what = GOTLOCATION; |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
138 |
handleMessage( msg ); // ToDo: ugly recursive call !!! |
} else { |
139 |
} else { |
dismissDialog(DLG_PROGRESS); |
140 |
MessageBox.showMessage(TrainInfoList.this,"GPS fix timed out"); |
|
141 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
142 |
|
builder.setMessage("GPS fix timed out"); |
143 |
|
builder.setCancelable(true); |
144 |
|
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
145 |
|
public void onClick(DialogInterface dialog, int id) { |
146 |
|
dialog.dismiss(); |
147 |
|
startLookup(); |
148 |
|
|
149 |
|
} |
150 |
|
}); |
151 |
|
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
152 |
|
public void onClick(DialogInterface dialog, int id) { |
153 |
|
dialog.dismiss(); |
154 |
|
} |
155 |
|
}); |
156 |
|
builder.show(); |
157 |
|
|
158 |
} |
} |
159 |
} |
} |
160 |
break; |
break; |
|
case LOOKUPSTATIONFAILED: |
|
|
dialog.dismiss(); |
|
|
MessageBox.showMessage(TrainInfoList.this,"Error on finding nearby stations"); |
|
|
break; |
|
161 |
} |
} |
162 |
isRunning = false; |
isRunning = false; |
163 |
} |
} |
164 |
}; |
}; |
165 |
|
|
166 |
|
void startLocatorTask() |
167 |
|
{ |
168 |
|
dialogMessage = "Finding nearby stations"; |
169 |
|
showDialog(DLG_PROGRESS); |
170 |
|
|
171 |
|
locatorTask = new LocatorTask(); |
172 |
|
locatorTask.execute(); |
173 |
|
} |
174 |
|
|
175 |
@Override |
@Override |
176 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
249 |
|
|
250 |
if (success) { |
if (success) { |
251 |
if (stationProvider.getStations().size() == 0) |
if (stationProvider.getStations().size() == 0) |
252 |
MessageBox.showMessage(TrainInfoList.this, "No stations found!"); // this should not be possible !?! |
MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! |
253 |
stations = stationProvider.getStations(); |
stations = stationProvider.getStations(); |
254 |
adapter.setStations( stations ); |
adapter.setStations( stations ); |
255 |
|
|
256 |
} else { //communication or parse errors |
} else { //communication or parse errors |
257 |
MessageBox.showMessage(TrainInfoList.this, "Error finding stations!"); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
258 |
|
builder.setMessage("Error on finding nearby stations"); |
259 |
|
builder.setCancelable(true); |
260 |
|
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
261 |
|
public void onClick(DialogInterface dialog, int id) { |
262 |
|
dialog.dismiss(); |
263 |
|
|
264 |
|
stationsFetched.post( new Runnable() { |
265 |
|
@Override |
266 |
|
public void run() { |
267 |
|
startLocatorTask(); |
268 |
|
} |
269 |
|
}); |
270 |
|
} |
271 |
|
}); |
272 |
|
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
273 |
|
public void onClick(DialogInterface dialog, int id) { |
274 |
|
dialog.dismiss(); |
275 |
|
} |
276 |
|
}); |
277 |
|
builder.show(); |
278 |
} |
} |
279 |
} |
} |
280 |
} |
} |