1 |
torben |
237 |
package dk.thoerup.traininfo; |
2 |
|
|
|
3 |
torben |
1066 |
import static dk.thoerup.traininfo.R.string.app_name; |
4 |
|
|
import static dk.thoerup.traininfo.R.string.generic_cancel; |
5 |
|
|
import static dk.thoerup.traininfo.R.string.generic_retry; |
6 |
|
|
import static dk.thoerup.traininfo.R.string.generic_search; |
7 |
|
|
import static dk.thoerup.traininfo.R.string.stationlist_accuracy; |
8 |
|
|
import static dk.thoerup.traininfo.R.string.stationlist_addfavorite; |
9 |
|
|
import static dk.thoerup.traininfo.R.string.stationlist_favorites; |
10 |
|
|
import static dk.thoerup.traininfo.R.string.stationlist_fetcherror; |
11 |
|
|
import static dk.thoerup.traininfo.R.string.stationlist_findbyname; |
12 |
|
|
import static dk.thoerup.traininfo.R.string.stationlist_findingnearby; |
13 |
|
|
import static dk.thoerup.traininfo.R.string.stationlist_gpsinfo; |
14 |
|
|
import static dk.thoerup.traininfo.R.string.stationlist_gpstimeout; |
15 |
|
|
import static dk.thoerup.traininfo.R.string.stationlist_latitude; |
16 |
|
|
import static dk.thoerup.traininfo.R.string.stationlist_loadfavorites; |
17 |
|
|
import static dk.thoerup.traininfo.R.string.stationlist_locationinfo; |
18 |
|
|
import static dk.thoerup.traininfo.R.string.stationlist_longitude; |
19 |
|
|
import static dk.thoerup.traininfo.R.string.stationlist_nearbystations; |
20 |
|
|
import static dk.thoerup.traininfo.R.string.stationlist_nofavorites; |
21 |
|
|
import static dk.thoerup.traininfo.R.string.stationlist_nolocation; |
22 |
|
|
import static dk.thoerup.traininfo.R.string.stationlist_nolocationprovider; |
23 |
|
|
import static dk.thoerup.traininfo.R.string.stationlist_nostations; |
24 |
|
|
import static dk.thoerup.traininfo.R.string.stationlist_obtainedby; |
25 |
|
|
import static dk.thoerup.traininfo.R.string.stationlist_removefavorite; |
26 |
torben |
1219 |
import static dk.thoerup.traininfo.R.string.stationlist_satellitecount; |
27 |
torben |
1066 |
import static dk.thoerup.traininfo.R.string.stationlist_search; |
28 |
|
|
import static dk.thoerup.traininfo.R.string.stationlist_stationadded; |
29 |
|
|
import static dk.thoerup.traininfo.R.string.stationlist_stationmap; |
30 |
|
|
import static dk.thoerup.traininfo.R.string.stationlist_stationremoved; |
31 |
|
|
import static dk.thoerup.traininfo.R.string.stationlist_stationsearch; |
32 |
|
|
import static dk.thoerup.traininfo.R.string.stationlist_twocharmin; |
33 |
|
|
import static dk.thoerup.traininfo.R.string.stationlist_waitforlocation; |
34 |
|
|
|
35 |
torben |
258 |
import java.util.ArrayList; |
36 |
|
|
|
37 |
torben |
731 |
import android.app.Activity; |
38 |
torben |
336 |
import android.app.AlertDialog; |
39 |
torben |
237 |
import android.app.Dialog; |
40 |
|
|
import android.app.ListActivity; |
41 |
|
|
import android.app.ProgressDialog; |
42 |
torben |
336 |
import android.content.DialogInterface; |
43 |
torben |
237 |
import android.content.Intent; |
44 |
torben |
433 |
import android.content.SharedPreferences; |
45 |
torben |
435 |
import android.content.SharedPreferences.Editor; |
46 |
torben |
319 |
import android.location.Location; |
47 |
torben |
241 |
import android.os.AsyncTask; |
48 |
torben |
237 |
import android.os.Bundle; |
49 |
|
|
import android.os.Handler; |
50 |
|
|
import android.os.Message; |
51 |
torben |
1219 |
import android.util.Log; |
52 |
torben |
433 |
import android.view.ContextMenu; |
53 |
torben |
381 |
import android.view.LayoutInflater; |
54 |
torben |
368 |
import android.view.Menu; |
55 |
|
|
import android.view.MenuItem; |
56 |
torben |
237 |
import android.view.View; |
57 |
torben |
433 |
import android.view.ContextMenu.ContextMenuInfo; |
58 |
|
|
import android.view.View.OnCreateContextMenuListener; |
59 |
|
|
import android.widget.AdapterView; |
60 |
torben |
381 |
import android.widget.EditText; |
61 |
torben |
237 |
import android.widget.ListView; |
62 |
torben |
433 |
import android.widget.Toast; |
63 |
torben |
1066 |
import dk.thoerup.android.traininfo.common.StationBean; |
64 |
|
|
import dk.thoerup.android.traininfo.common.StationBean.StationEntry; |
65 |
torben |
319 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
66 |
|
|
import dk.thoerup.traininfo.provider.StationProvider; |
67 |
torben |
368 |
import dk.thoerup.traininfo.stationmap.GeoPair; |
68 |
|
|
import dk.thoerup.traininfo.stationmap.StationMapView; |
69 |
torben |
433 |
import dk.thoerup.traininfo.util.IntSet; |
70 |
torben |
245 |
import dk.thoerup.traininfo.util.MessageBox; |
71 |
torben |
237 |
|
72 |
torben |
336 |
public class StationList extends ListActivity { |
73 |
torben |
1143 |
|
74 |
torben |
368 |
|
75 |
torben |
1142 |
|
76 |
torben |
381 |
public static final int OPTIONS_MAP = 2003; |
77 |
torben |
481 |
public static final int OPTIONS_GPSINFO = 2004; |
78 |
torben |
433 |
|
79 |
torben |
381 |
public static final int DLG_PROGRESS = 3001; |
80 |
|
|
public static final int DLG_STATIONNAME = 3002; |
81 |
torben |
237 |
|
82 |
torben |
948 |
|
83 |
torben |
1019 |
public static final int GPS_TIMEOUT_MS = 15000; //how long are we willing to wait for gps fix -in milliseconds |
84 |
torben |
948 |
|
85 |
|
|
|
86 |
torben |
440 |
static enum LookupMethod { |
87 |
|
|
ByLocation, |
88 |
|
|
ByName, |
89 |
|
|
ByList, |
90 |
|
|
MethodNone |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
|
94 |
torben |
336 |
String dialogMessage = ""; |
95 |
torben |
237 |
ProgressDialog dialog; |
96 |
torben |
482 |
LocationLookup locationLookup = null; |
97 |
torben |
440 |
FindStationsTask findStationsTask; |
98 |
torben |
381 |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
99 |
torben |
237 |
|
100 |
torben |
1143 |
//GeoPair location = new GeoPair(); |
101 |
torben |
731 |
|
102 |
|
|
boolean isLaunchedforShortcut; |
103 |
torben |
1143 |
|
104 |
torben |
1066 |
StationBean stations = new StationBean(); |
105 |
torben |
258 |
|
106 |
torben |
319 |
StationProvider stationProvider = ProviderFactory.getStationProvider(); |
107 |
torben |
381 |
|
108 |
|
|
StationListAdapter adapter = null; |
109 |
torben |
258 |
|
110 |
torben |
433 |
FavoritesMenu contextMenu = new FavoritesMenu(); |
111 |
|
|
IntSet favorites = new IntSet(); |
112 |
torben |
481 |
|
113 |
|
|
WelcomeScreen.ListType listType; |
114 |
torben |
433 |
SharedPreferences prefs; |
115 |
torben |
1143 |
|
116 |
torben |
381 |
/////////////////////////////////////////////////////////////////////////////////////////// |
117 |
|
|
//Activity call backs |
118 |
|
|
|
119 |
torben |
1078 |
|
120 |
torben |
237 |
@Override |
121 |
|
|
public void onCreate(Bundle savedInstanceState) { |
122 |
|
|
super.onCreate(savedInstanceState); |
123 |
torben |
481 |
setContentView(R.layout.stationlist); |
124 |
torben |
237 |
|
125 |
torben |
241 |
|
126 |
torben |
237 |
adapter = new StationListAdapter(this); |
127 |
|
|
setListAdapter(adapter); |
128 |
|
|
|
129 |
torben |
433 |
ListView lv = getListView(); |
130 |
|
|
lv.setOnCreateContextMenuListener(contextMenu); |
131 |
|
|
|
132 |
torben |
1143 |
locationLookup = new LocationLookup(this); |
133 |
torben |
433 |
|
134 |
|
|
|
135 |
|
|
prefs = getSharedPreferences("TrainStation", 0); |
136 |
|
|
String favoriteString = prefs.getString("favorites", ""); |
137 |
|
|
if (! favoriteString.equals("") ) { |
138 |
|
|
favorites.fromString(favoriteString); |
139 |
|
|
} |
140 |
|
|
|
141 |
torben |
482 |
listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); |
142 |
|
|
setTitle(); |
143 |
|
|
|
144 |
torben |
731 |
isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false); |
145 |
|
|
|
146 |
torben |
1028 |
ProviderFactory.purgeOldEntries(); //cleanup before fetching more data |
147 |
|
|
|
148 |
torben |
258 |
if (savedInstanceState == null) { |
149 |
torben |
482 |
|
150 |
|
|
|
151 |
torben |
481 |
switch (listType) { |
152 |
|
|
case ListNearest: |
153 |
torben |
1207 |
startNearestLookup(); |
154 |
torben |
481 |
break; |
155 |
torben |
983 |
case ListSearch: |
156 |
torben |
1008 |
showDialog(DLG_STATIONNAME); |
157 |
torben |
481 |
break; |
158 |
|
|
case ListFavorites: |
159 |
|
|
startFavoriteLookup(); |
160 |
|
|
break; |
161 |
|
|
default: |
162 |
|
|
// Not possible !?! |
163 |
|
|
} |
164 |
|
|
|
165 |
torben |
258 |
} else { |
166 |
torben |
1066 |
stations = (StationBean) savedInstanceState.getSerializable("stations"); |
167 |
torben |
260 |
adapter.setStations(stations); |
168 |
torben |
258 |
} |
169 |
torben |
482 |
|
170 |
torben |
237 |
} |
171 |
torben |
918 |
|
172 |
torben |
1027 |
|
173 |
torben |
918 |
|
174 |
torben |
1027 |
|
175 |
|
|
|
176 |
|
|
|
177 |
|
|
@Override |
178 |
torben |
918 |
protected void onDestroy() { |
179 |
|
|
super.onDestroy(); |
180 |
|
|
|
181 |
torben |
1244 |
stationsFetched.removeMessages(0); |
182 |
torben |
1025 |
|
183 |
torben |
1244 |
|
184 |
torben |
919 |
if (locationLookup != null) { |
185 |
|
|
locationLookup.stopSearch(); |
186 |
|
|
} |
187 |
torben |
1025 |
if (findStationsTask != null) { |
188 |
|
|
findStationsTask.cancel(true); |
189 |
|
|
} |
190 |
torben |
918 |
} |
191 |
|
|
|
192 |
|
|
|
193 |
torben |
482 |
protected void setTitle() { |
194 |
torben |
561 |
String dialogTitle = getResources().getString(app_name); |
195 |
torben |
482 |
switch (listType) { |
196 |
|
|
case ListNearest: |
197 |
torben |
561 |
dialogTitle += " - " + getString(stationlist_nearbystations); |
198 |
torben |
482 |
break; |
199 |
|
|
case ListSearch: |
200 |
torben |
561 |
dialogTitle += " - " + getString(stationlist_search); |
201 |
torben |
482 |
break; |
202 |
|
|
case ListFavorites: |
203 |
torben |
561 |
dialogTitle += " - " + getString(stationlist_favorites); |
204 |
torben |
482 |
break; |
205 |
|
|
default: |
206 |
|
|
dialogTitle = "";//not possible |
207 |
|
|
} |
208 |
|
|
|
209 |
|
|
setTitle(dialogTitle); |
210 |
torben |
701 |
|
211 |
torben |
482 |
} |
212 |
|
|
|
213 |
torben |
701 |
|
214 |
torben |
371 |
|
215 |
torben |
243 |
@Override |
216 |
|
|
public void onSaveInstanceState(Bundle outState) |
217 |
|
|
{ |
218 |
torben |
258 |
if (dialog != null && dialog.isShowing()) |
219 |
torben |
243 |
dialog.dismiss(); |
220 |
torben |
1066 |
outState.putSerializable("stations", (StationBean) stations); |
221 |
torben |
482 |
|
222 |
torben |
243 |
} |
223 |
torben |
237 |
|
224 |
torben |
243 |
|
225 |
torben |
237 |
|
226 |
|
|
@Override |
227 |
torben |
368 |
public boolean onCreateOptionsMenu(Menu menu) { |
228 |
torben |
419 |
MenuItem item; |
229 |
torben |
561 |
|
230 |
|
|
item = menu.add(0, OPTIONS_MAP, 0, getString(stationlist_stationmap)); |
231 |
torben |
419 |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
232 |
|
|
|
233 |
torben |
561 |
item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo)); |
234 |
torben |
481 |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
235 |
|
|
|
236 |
torben |
368 |
return true; |
237 |
|
|
} |
238 |
|
|
|
239 |
|
|
@Override |
240 |
|
|
public boolean onOptionsItemSelected(MenuItem item) { |
241 |
torben |
381 |
boolean retval = true; |
242 |
|
|
|
243 |
torben |
481 |
//TODO: Cleanup |
244 |
torben |
368 |
switch (item.getItemId()) { |
245 |
|
|
case OPTIONS_MAP: |
246 |
|
|
|
247 |
|
|
Intent intent = new Intent(this,StationMapView.class); |
248 |
|
|
|
249 |
|
|
ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>(); |
250 |
torben |
1066 |
for (StationEntry st : stations.entries ) { |
251 |
torben |
369 |
stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) ); |
252 |
torben |
368 |
} |
253 |
|
|
|
254 |
|
|
intent.putExtra("stations", stationPoints); |
255 |
|
|
|
256 |
|
|
startActivity(intent); |
257 |
|
|
break; |
258 |
torben |
481 |
case OPTIONS_GPSINFO: |
259 |
torben |
482 |
Location loc = locationLookup.getLocation(); |
260 |
torben |
371 |
StringBuffer message = new StringBuffer(); |
261 |
torben |
561 |
message.append( getString(stationlist_locationinfo) ).append(":\n"); |
262 |
torben |
554 |
if (loc != null) { |
263 |
torben |
561 |
message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n"); |
264 |
|
|
message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n"); |
265 |
torben |
564 |
message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n"); |
266 |
|
|
message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n"); |
267 |
torben |
554 |
} else { |
268 |
torben |
561 |
message.append( getString(stationlist_nolocation) ); |
269 |
torben |
556 |
} |
270 |
|
|
|
271 |
torben |
906 |
MessageBox.showMessage(this, message.toString(), false); |
272 |
torben |
371 |
break; |
273 |
torben |
368 |
default: |
274 |
|
|
retval = super.onOptionsItemSelected(item); |
275 |
|
|
} |
276 |
|
|
|
277 |
|
|
return retval; |
278 |
|
|
} |
279 |
torben |
433 |
|
280 |
|
|
|
281 |
torben |
368 |
|
282 |
|
|
@Override |
283 |
torben |
433 |
public boolean onContextItemSelected(MenuItem item) { |
284 |
|
|
contextMenu.onContextItemSelected(item); |
285 |
|
|
return true; |
286 |
|
|
|
287 |
|
|
|
288 |
|
|
} |
289 |
torben |
556 |
|
290 |
|
|
public void showMessageAndClose(String message) { |
291 |
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
292 |
|
|
builder.setMessage(message) |
293 |
|
|
.setCancelable(false) |
294 |
|
|
.setPositiveButton("OK", new DialogInterface.OnClickListener() { |
295 |
|
|
public void onClick(DialogInterface dialog, int id) { |
296 |
|
|
dialog.dismiss(); |
297 |
|
|
StationList.this.finish(); |
298 |
|
|
} |
299 |
|
|
}) |
300 |
|
|
.show(); |
301 |
|
|
} |
302 |
torben |
433 |
|
303 |
|
|
|
304 |
|
|
|
305 |
|
|
|
306 |
|
|
@Override |
307 |
torben |
237 |
protected Dialog onCreateDialog(int id) { |
308 |
|
|
switch (id) { |
309 |
|
|
case DLG_PROGRESS: |
310 |
|
|
ProgressDialog dlg = new ProgressDialog(this); |
311 |
torben |
561 |
dlg.setMessage( getString(stationlist_waitforlocation) ); |
312 |
torben |
237 |
dlg.setCancelable(false); |
313 |
torben |
336 |
return dlg; |
314 |
torben |
381 |
case DLG_STATIONNAME: |
315 |
|
|
LayoutInflater factory = LayoutInflater.from(this); |
316 |
|
|
final View rootView = factory.inflate(R.layout.textinput, null); |
317 |
|
|
|
318 |
|
|
|
319 |
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
320 |
|
|
|
321 |
torben |
561 |
builder.setTitle( getString(stationlist_stationsearch) ); |
322 |
torben |
381 |
builder.setView(rootView); |
323 |
|
|
builder.setCancelable(true); |
324 |
torben |
561 |
builder.setPositiveButton( getString(generic_search), new DialogInterface.OnClickListener() { |
325 |
torben |
381 |
public void onClick(DialogInterface dialog, int which) { |
326 |
|
|
EditText et = (EditText) rootView.findViewById(R.id.EditText); |
327 |
|
|
dialog.dismiss(); |
328 |
torben |
713 |
String search = et.getText().toString().trim(); |
329 |
|
|
if (search.length() >= 2) { |
330 |
torben |
1207 |
startNameLookup(search); |
331 |
torben |
381 |
} else { |
332 |
torben |
561 |
showMessageAndClose( getString(stationlist_twocharmin) ); |
333 |
torben |
381 |
} |
334 |
|
|
} |
335 |
|
|
}); |
336 |
torben |
561 |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
337 |
torben |
381 |
public void onClick(DialogInterface dialog, int which) { |
338 |
|
|
dialog.dismiss(); |
339 |
torben |
555 |
StationList.this.finish(); // Close this Activity |
340 |
torben |
381 |
} |
341 |
|
|
}); |
342 |
|
|
return builder.create(); |
343 |
|
|
|
344 |
torben |
237 |
default: |
345 |
|
|
return super.onCreateDialog(id); |
346 |
|
|
} |
347 |
|
|
|
348 |
|
|
} |
349 |
|
|
|
350 |
|
|
|
351 |
|
|
@Override |
352 |
|
|
protected void onPrepareDialog(int id, Dialog dialog) { |
353 |
|
|
super.onPrepareDialog(id, dialog); |
354 |
|
|
switch (id) { |
355 |
|
|
case DLG_PROGRESS: |
356 |
|
|
this.dialog = (ProgressDialog) dialog; |
357 |
torben |
336 |
if (!dialogMessage.equals("")) { |
358 |
|
|
this.dialog.setMessage(dialogMessage); |
359 |
|
|
dialogMessage = ""; |
360 |
|
|
} |
361 |
torben |
237 |
break; |
362 |
|
|
} |
363 |
|
|
} |
364 |
torben |
381 |
|
365 |
|
|
@Override |
366 |
|
|
protected void onListItemClick(ListView l, View v, int position, long id) { |
367 |
|
|
super.onListItemClick(l, v, position, id); |
368 |
|
|
|
369 |
torben |
1066 |
StationEntry station = stations.entries.get(position); |
370 |
torben |
381 |
|
371 |
torben |
731 |
if (isLaunchedforShortcut == true) { |
372 |
|
|
Intent i = new Intent(); |
373 |
|
|
i.putExtra("station", station); |
374 |
|
|
setResult(Activity.RESULT_OK, i); |
375 |
|
|
finish(); |
376 |
|
|
} else { |
377 |
|
|
Intent intent = new Intent(this, DepartureList.class); |
378 |
|
|
intent.putExtra("stationbean", station); |
379 |
|
|
startActivity(intent); |
380 |
|
|
} |
381 |
torben |
381 |
} |
382 |
|
|
|
383 |
|
|
///////////////////////////////////////////////////////////// |
384 |
|
|
// |
385 |
|
|
|
386 |
torben |
1207 |
public void startNearestLookup() { |
387 |
torben |
561 |
dialogMessage = getString( stationlist_waitforlocation ); |
388 |
torben |
1008 |
showDialog(DLG_PROGRESS); |
389 |
torben |
237 |
|
390 |
torben |
482 |
locationLookup.locateStations(); |
391 |
torben |
1143 |
stationsFetched.sendEmptyMessageDelayed(0, 500); |
392 |
torben |
237 |
} |
393 |
torben |
381 |
|
394 |
torben |
1207 |
void startNameLookup(String name) { |
395 |
torben |
561 |
dialogMessage = getString( stationlist_findbyname ); |
396 |
torben |
1008 |
showDialog(DLG_PROGRESS); |
397 |
torben |
237 |
|
398 |
torben |
440 |
findStationsTask = new FindStationsTask(); |
399 |
torben |
579 |
findStationsTask.searchByName(name); |
400 |
torben |
440 |
findStationsTask.execute(); |
401 |
torben |
381 |
|
402 |
|
|
} |
403 |
torben |
433 |
|
404 |
|
|
public void startFavoriteLookup() { |
405 |
|
|
|
406 |
torben |
440 |
if (favorites.size() > 0) { |
407 |
torben |
561 |
dialogMessage = getString( stationlist_loadfavorites ); |
408 |
torben |
1008 |
showDialog(DLG_PROGRESS); |
409 |
torben |
237 |
|
410 |
torben |
440 |
findStationsTask = new FindStationsTask(); |
411 |
torben |
579 |
findStationsTask.searchByIds( favorites.toString() ); |
412 |
torben |
440 |
findStationsTask.execute(); |
413 |
torben |
433 |
} else { |
414 |
torben |
561 |
showMessageAndClose( getString( stationlist_nofavorites ) ); |
415 |
torben |
433 |
} |
416 |
|
|
} |
417 |
torben |
381 |
|
418 |
torben |
433 |
|
419 |
torben |
381 |
|
420 |
|
|
void startLocatorTask() |
421 |
|
|
{ |
422 |
torben |
561 |
dialogMessage = getString( stationlist_findingnearby ); |
423 |
torben |
1143 |
showDialog(DLG_PROGRESS); |
424 |
torben |
381 |
|
425 |
torben |
440 |
findStationsTask = new FindStationsTask(); |
426 |
torben |
482 |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
427 |
torben |
440 |
findStationsTask.execute(); |
428 |
torben |
381 |
} |
429 |
|
|
|
430 |
|
|
|
431 |
|
|
//////////////////////////////////////////////////////////////////////////// |
432 |
|
|
// Inner classes |
433 |
|
|
|
434 |
|
|
class StationsFetchedHandler extends Handler { |
435 |
torben |
237 |
@Override |
436 |
|
|
public void handleMessage(Message msg) { |
437 |
torben |
336 |
|
438 |
torben |
1143 |
LocationLookup.LookupStates state = locationLookup.getState(); |
439 |
|
|
|
440 |
|
|
|
441 |
torben |
1142 |
switch (state) { |
442 |
torben |
237 |
case GOTLOCATION: |
443 |
torben |
1008 |
dismissDialog(DLG_PROGRESS); |
444 |
torben |
1143 |
|
445 |
torben |
336 |
startLocatorTask(); |
446 |
torben |
319 |
|
447 |
torben |
1143 |
return; |
448 |
|
|
|
449 |
torben |
237 |
case NOPROVIDER: |
450 |
torben |
1008 |
dismissDialog(DLG_PROGRESS); |
451 |
torben |
906 |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); |
452 |
|
|
//StationList.this.finish(); |
453 |
torben |
1143 |
return; |
454 |
torben |
1220 |
case IDLE: |
455 |
|
|
Log.e("TrainInfo", "How did this happen ???"); |
456 |
|
|
dismissDialog(DLG_PROGRESS); // how did we get here ?? |
457 |
|
|
return; |
458 |
|
|
|
459 |
torben |
1143 |
} |
460 |
torben |
336 |
|
461 |
torben |
1143 |
|
462 |
|
|
|
463 |
|
|
if (locationLookup.elapsedTime() >= GPS_TIMEOUT_MS) { |
464 |
torben |
1245 |
dismissDialog(DLG_PROGRESS); |
465 |
torben |
1143 |
|
466 |
torben |
1245 |
|
467 |
torben |
1143 |
locationLookup.stopSearch(); |
468 |
|
|
|
469 |
|
|
if (locationLookup.hasLocation()) { |
470 |
|
|
startLocatorTask(); |
471 |
|
|
} else { |
472 |
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
473 |
|
|
builder.setMessage( getString( stationlist_gpstimeout) ); |
474 |
|
|
builder.setCancelable(true); |
475 |
|
|
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
476 |
|
|
public void onClick(DialogInterface dialog, int id) { |
477 |
|
|
dialog.dismiss(); |
478 |
torben |
1207 |
startNearestLookup(); |
479 |
torben |
1143 |
|
480 |
|
|
} |
481 |
|
|
}); |
482 |
|
|
builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() { |
483 |
|
|
public void onClick(DialogInterface dialog, int id) { |
484 |
|
|
dialog.dismiss(); |
485 |
torben |
1150 |
StationList.this.finish(); // Close this Activity |
486 |
torben |
1143 |
} |
487 |
|
|
}); |
488 |
|
|
builder.show(); |
489 |
|
|
|
490 |
torben |
237 |
} |
491 |
torben |
1143 |
} else { |
492 |
|
|
if (locationLookup.hasGps()) { |
493 |
|
|
int count = locationLookup.getSatCount(); |
494 |
|
|
String dialogMessage = getString( stationlist_waitforlocation ) + "\n" + getString( stationlist_satellitecount ) + ": " + count; |
495 |
|
|
dialog.setMessage( dialogMessage ); |
496 |
|
|
} |
497 |
|
|
this.sendEmptyMessageDelayed(0, 500); |
498 |
torben |
237 |
} |
499 |
torben |
1143 |
|
500 |
torben |
237 |
} |
501 |
torben |
1143 |
} |
502 |
torben |
381 |
|
503 |
torben |
1143 |
|
504 |
|
|
|
505 |
torben |
237 |
|
506 |
torben |
440 |
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
507 |
torben |
336 |
|
508 |
torben |
381 |
LookupMethod method = LookupMethod.MethodNone; |
509 |
|
|
String name; |
510 |
|
|
Location loc; |
511 |
torben |
433 |
String ids; |
512 |
torben |
237 |
|
513 |
torben |
579 |
public void searchByName(String n) { |
514 |
torben |
317 |
|
515 |
torben |
381 |
method = LookupMethod.ByName; |
516 |
|
|
name = n; |
517 |
torben |
317 |
} |
518 |
|
|
|
519 |
torben |
381 |
public void searchByLocation(Location l) { |
520 |
|
|
method = LookupMethod.ByLocation; |
521 |
|
|
loc = l; |
522 |
|
|
} |
523 |
|
|
|
524 |
torben |
579 |
public void searchByIds(String id) { |
525 |
torben |
433 |
|
526 |
|
|
method = LookupMethod.ByList; |
527 |
|
|
ids = id; |
528 |
|
|
} |
529 |
|
|
|
530 |
torben |
241 |
@Override |
531 |
|
|
protected void onPreExecute() { |
532 |
torben |
258 |
|
533 |
torben |
381 |
if (method.equals(LookupMethod.MethodNone)) |
534 |
|
|
throw new RuntimeException("Method not set"); |
535 |
torben |
241 |
super.onPreExecute(); |
536 |
|
|
} |
537 |
|
|
|
538 |
|
|
@Override |
539 |
|
|
protected Void doInBackground(Void... params) { |
540 |
torben |
473 |
|
541 |
|
|
switch (method) { |
542 |
|
|
case ByLocation: |
543 |
torben |
1164 |
stations = stationProvider.lookupStationsByLocation(loc); |
544 |
torben |
473 |
break; |
545 |
|
|
case ByName: |
546 |
torben |
1006 |
stations = stationProvider.lookupStationsByName(name); |
547 |
torben |
473 |
break; |
548 |
|
|
case ByList: |
549 |
torben |
1006 |
stations = stationProvider.lookupStationsByIds(ids); |
550 |
torben |
473 |
break; |
551 |
|
|
default: |
552 |
torben |
1006 |
stations = null; // not possible |
553 |
torben |
473 |
} |
554 |
torben |
433 |
|
555 |
torben |
473 |
|
556 |
torben |
241 |
return null; |
557 |
|
|
} |
558 |
|
|
|
559 |
|
|
@Override |
560 |
|
|
protected void onPostExecute(Void result) { |
561 |
|
|
super.onPostExecute(result); |
562 |
torben |
1008 |
dialog.dismiss(); |
563 |
torben |
319 |
|
564 |
torben |
473 |
|
565 |
torben |
1006 |
if (stations != null) { |
566 |
torben |
1066 |
if (stations.entries.size() == 0) { |
567 |
torben |
566 |
showMessageAndClose(getString(stationlist_nostations)); |
568 |
|
|
} |
569 |
torben |
917 |
|
570 |
|
|
StationList.this.getListView().invalidateViews(); |
571 |
torben |
481 |
adapter.setStations( stations ); |
572 |
torben |
319 |
|
573 |
torben |
917 |
|
574 |
torben |
319 |
} else { //communication or parse errors |
575 |
torben |
336 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
576 |
torben |
844 |
builder.setMessage(getString(stationlist_fetcherror)); |
577 |
torben |
336 |
builder.setCancelable(true); |
578 |
torben |
561 |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
579 |
torben |
336 |
public void onClick(DialogInterface dialog, int id) { |
580 |
|
|
dialog.dismiss(); |
581 |
|
|
|
582 |
torben |
652 |
Runnable runner = null; |
583 |
|
|
switch (method) { |
584 |
|
|
case ByLocation: |
585 |
|
|
runner = new Runnable() { |
586 |
|
|
@Override |
587 |
|
|
public void run() { |
588 |
|
|
startLocatorTask(); |
589 |
|
|
} |
590 |
|
|
}; |
591 |
|
|
break; |
592 |
|
|
case ByName: |
593 |
|
|
runner = new Runnable() { |
594 |
|
|
@Override |
595 |
|
|
public void run() { |
596 |
torben |
1207 |
startNameLookup( FindStationsTask.this.name ); |
597 |
torben |
652 |
} |
598 |
|
|
}; |
599 |
|
|
break; |
600 |
|
|
case ByList: |
601 |
|
|
runner = new Runnable() { |
602 |
|
|
@Override |
603 |
|
|
public void run() { |
604 |
|
|
startFavoriteLookup(); |
605 |
|
|
} |
606 |
|
|
}; |
607 |
|
|
break; |
608 |
|
|
} |
609 |
|
|
|
610 |
|
|
stationsFetched.post( runner ); |
611 |
torben |
336 |
} |
612 |
|
|
}); |
613 |
torben |
561 |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
614 |
torben |
336 |
public void onClick(DialogInterface dialog, int id) { |
615 |
|
|
dialog.dismiss(); |
616 |
torben |
843 |
StationList.this.finish(); |
617 |
torben |
336 |
} |
618 |
torben |
701 |
}); |
619 |
|
|
|
620 |
torben |
1008 |
builder.show(); |
621 |
torben |
319 |
} |
622 |
torben |
241 |
} |
623 |
|
|
} |
624 |
torben |
433 |
|
625 |
|
|
|
626 |
|
|
class FavoritesMenu implements OnCreateContextMenuListener { |
627 |
|
|
private final static int FAVORITES_ADD = 9001; |
628 |
|
|
private final static int FAVORITES_REMOVE = 9002; |
629 |
|
|
|
630 |
|
|
private int selectedPosition; |
631 |
|
|
|
632 |
|
|
|
633 |
|
|
@Override |
634 |
|
|
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { |
635 |
|
|
|
636 |
|
|
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; |
637 |
|
|
selectedPosition = info.position; |
638 |
torben |
1066 |
int stationID = stations.entries.get(selectedPosition).getId(); |
639 |
torben |
433 |
|
640 |
torben |
441 |
if (!favorites.contains(stationID)) { |
641 |
torben |
561 |
menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) ); |
642 |
torben |
433 |
} else { |
643 |
torben |
563 |
menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_removefavorite) ); |
644 |
torben |
433 |
} |
645 |
|
|
|
646 |
|
|
} |
647 |
|
|
|
648 |
|
|
public void onContextItemSelected(MenuItem item) { |
649 |
torben |
1066 |
StationEntry sb = stations.entries.get(selectedPosition); |
650 |
torben |
433 |
|
651 |
|
|
int stationID = sb.getId(); |
652 |
|
|
if (item.getItemId() == FAVORITES_ADD) { |
653 |
|
|
favorites.add(stationID); |
654 |
torben |
561 |
Toast.makeText(StationList.this, getString(stationlist_stationadded), Toast.LENGTH_SHORT).show(); |
655 |
torben |
433 |
} else { |
656 |
torben |
473 |
|
657 |
torben |
433 |
favorites.remove(stationID); |
658 |
torben |
561 |
Toast.makeText(StationList.this, getString(stationlist_stationremoved), Toast.LENGTH_SHORT).show(); |
659 |
torben |
473 |
|
660 |
torben |
481 |
|
661 |
|
|
if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) { |
662 |
torben |
1066 |
stations.entries.remove(selectedPosition); |
663 |
torben |
473 |
adapter.notifyDataSetChanged(); |
664 |
|
|
} |
665 |
torben |
433 |
} |
666 |
torben |
435 |
Editor ed = prefs.edit(); |
667 |
|
|
ed.putString("favorites", favorites.toString()); |
668 |
|
|
ed.commit(); |
669 |
torben |
433 |
} |
670 |
|
|
} |
671 |
torben |
237 |
} |