1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
3 |
|
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.stationlist_accuracy; |
7 |
|
import static dk.thoerup.traininfo.R.string.stationlist_addfavorite; |
8 |
|
import static dk.thoerup.traininfo.R.string.stationlist_favorites; |
9 |
|
import static dk.thoerup.traininfo.R.string.stationlist_gpsinfo; |
10 |
|
import static dk.thoerup.traininfo.R.string.stationlist_gpstimeout; |
11 |
|
import static dk.thoerup.traininfo.R.string.stationlist_latitude; |
12 |
|
import static dk.thoerup.traininfo.R.string.stationlist_locationinfo; |
13 |
|
import static dk.thoerup.traininfo.R.string.stationlist_longitude; |
14 |
|
import static dk.thoerup.traininfo.R.string.stationlist_nearbystations; |
15 |
|
import static dk.thoerup.traininfo.R.string.stationlist_nofavorites; |
16 |
|
import static dk.thoerup.traininfo.R.string.stationlist_nolocationprovider; |
17 |
|
import static dk.thoerup.traininfo.R.string.stationlist_obtainedby; |
18 |
|
import static dk.thoerup.traininfo.R.string.stationlist_removefavorite; |
19 |
|
import static dk.thoerup.traininfo.R.string.stationlist_satellitecount; |
20 |
|
import static dk.thoerup.traininfo.R.string.stationlist_search; |
21 |
|
import static dk.thoerup.traininfo.R.string.stationlist_stationadded; |
22 |
|
import static dk.thoerup.traininfo.R.string.stationlist_stationmap; |
23 |
|
import static dk.thoerup.traininfo.R.string.stationlist_stationremoved; |
24 |
|
import static dk.thoerup.traininfo.R.string.stationlist_waitforlocation; |
25 |
|
|
26 |
import java.util.ArrayList; |
import java.util.ArrayList; |
|
import java.util.List; |
|
27 |
|
|
28 |
|
import android.app.Activity; |
29 |
|
import android.app.AlertDialog; |
30 |
import android.app.Dialog; |
import android.app.Dialog; |
31 |
import android.app.ListActivity; |
import android.app.ListActivity; |
32 |
import android.app.ProgressDialog; |
import android.app.ProgressDialog; |
33 |
|
import android.content.DialogInterface; |
34 |
import android.content.Intent; |
import android.content.Intent; |
35 |
import android.os.AsyncTask; |
import android.content.SharedPreferences; |
36 |
|
import android.content.SharedPreferences.Editor; |
37 |
|
import android.location.Location; |
38 |
import android.os.Bundle; |
import android.os.Bundle; |
39 |
import android.os.Handler; |
import android.os.Handler; |
40 |
import android.os.Message; |
import android.os.Message; |
41 |
|
import android.text.Editable; |
42 |
|
import android.text.TextWatcher; |
43 |
|
import android.util.Log; |
44 |
|
import android.view.ContextMenu; |
45 |
|
import android.view.ContextMenu.ContextMenuInfo; |
46 |
|
import android.view.Menu; |
47 |
|
import android.view.MenuItem; |
48 |
import android.view.View; |
import android.view.View; |
49 |
|
import android.view.View.OnCreateContextMenuListener; |
50 |
|
import android.widget.AdapterView; |
51 |
import android.widget.ListView; |
import android.widget.ListView; |
52 |
|
import android.widget.TextView; |
53 |
|
import android.widget.Toast; |
54 |
|
import dk.thoerup.android.traininfo.common.StationBean; |
55 |
|
import dk.thoerup.android.traininfo.common.StationEntry; |
56 |
|
import dk.thoerup.traininfo.WelcomeScreen.ListType; |
57 |
|
import dk.thoerup.traininfo.provider.ProviderFactory; |
58 |
|
import dk.thoerup.traininfo.provider.StationProvider; |
59 |
|
import dk.thoerup.traininfo.stationmap.GeoPair; |
60 |
|
import dk.thoerup.traininfo.stationmap.StationMapView; |
61 |
|
import dk.thoerup.traininfo.util.IntSet; |
62 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
63 |
|
|
64 |
public class TrainInfoList extends ListActivity { |
public class StationList extends ListActivity { |
65 |
public static final int GOTLOCATION = 1; |
|
|
public static final int GOTSTATIONLIST = 2; |
|
|
public static final int NOPROVIDER = 3; |
|
|
public static final int FIXTIMEOUT = 4; |
|
|
public static final int LOOKUPSTATIONFAILED = 5; |
|
66 |
|
|
|
public static final int DLG_PROGRESS = 1; |
|
67 |
|
|
68 |
/** Called when the activity is first created. */ |
public static final int OPTIONS_MAP = 103; |
69 |
ProgressDialog dialog; |
public static final int OPTIONS_GPSINFO = 104; |
70 |
StationLocator locator = null; |
|
71 |
LocatorTask locatorTask = new LocatorTask(); |
public static final int DLG_PROGRESS = 3001; |
72 |
|
|
73 |
|
|
74 |
boolean isRunning = false; |
public static final int GPS_TIMEOUT_MS = 15000; //how long are we willing to wait for gps fix -in milliseconds |
75 |
List<StationBean> stations = new ArrayList<StationBean>(); |
|
76 |
|
|
77 |
|
static enum LookupMethod { |
78 |
|
ByLocation, |
79 |
|
ByName, |
80 |
|
ByList, |
81 |
|
MethodNone |
82 |
|
} |
83 |
|
|
84 |
|
|
85 |
|
String dialogMessage = ""; |
86 |
|
ProgressDialog dialog; |
87 |
|
LocationLookup locationLookup = null; |
88 |
|
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
89 |
|
|
90 |
|
//GeoPair location = new GeoPair(); |
91 |
|
|
92 |
|
boolean isLaunchedforShortcut; |
93 |
|
|
94 |
|
StationBean stations = new StationBean(); |
95 |
|
|
96 |
|
StationProvider stationProvider = ProviderFactory.getStationProvider(); |
97 |
|
|
98 |
StationListAdapter adapter = null; |
StationListAdapter adapter = null; |
99 |
@SuppressWarnings("unchecked") |
|
100 |
|
FavoritesMenu contextMenu = new FavoritesMenu(); |
101 |
|
IntSet favorites = new IntSet(); |
102 |
|
|
103 |
|
WelcomeScreen.ListType listType; |
104 |
|
SharedPreferences prefs; |
105 |
|
|
106 |
|
/////////////////////////////////////////////////////////////////////////////////////////// |
107 |
|
//Activity call backs |
108 |
|
|
109 |
|
|
110 |
@Override |
@Override |
111 |
public void onCreate(Bundle savedInstanceState) { |
public void onCreate(Bundle savedInstanceState) { |
112 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
113 |
setContentView(R.layout.main); |
setContentView(R.layout.stationlist); |
114 |
|
|
115 |
StationLocator.removeMockLocation(this); |
listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); |
|
//StationLocator.injectMockLocation(this); |
|
116 |
|
|
117 |
adapter = new StationListAdapter(this); |
adapter = new StationListAdapter(this, (listType == ListType.ListNearest) ); |
118 |
setListAdapter(adapter); |
setListAdapter(adapter); |
119 |
|
|
120 |
locator = new StationLocator(this, stationsFetched); |
ListView lv = getListView(); |
121 |
|
lv.setOnCreateContextMenuListener(contextMenu); |
122 |
|
|
123 |
|
locationLookup = new LocationLookup(this); |
124 |
|
|
125 |
|
|
126 |
|
prefs = getSharedPreferences("TrainStation", 0); |
127 |
|
String favoriteString = prefs.getString("favorites", ""); |
128 |
|
if (! favoriteString.equals("") ) { |
129 |
|
favorites.fromString(favoriteString); |
130 |
|
} |
131 |
|
|
132 |
|
setTitle(); |
133 |
|
|
134 |
|
isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false); |
135 |
|
|
136 |
|
ProviderFactory.purgeOldEntries(); //cleanup before fetching more data |
137 |
|
|
138 |
|
if (listType == ListType.ListSearch) { |
139 |
|
enableNamesearchField(); |
140 |
|
} |
141 |
|
|
142 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
143 |
startLookup(); |
|
144 |
|
switch (listType) { |
145 |
|
case ListNearest: |
146 |
|
startNearestLookup(); |
147 |
|
break; |
148 |
|
case ListSearch: |
149 |
|
// do nothing here |
150 |
|
break; |
151 |
|
case ListFavorites: |
152 |
|
startFavoriteLookup(); |
153 |
|
break; |
154 |
|
default: |
155 |
|
// Not possible !?! |
156 |
|
} |
157 |
|
|
158 |
} else { |
} else { |
159 |
stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations"); |
((TextView)findViewById(R.id.stationsearch)).setText( savedInstanceState.getString( "search") ); |
160 |
|
|
161 |
|
stations = (StationBean) savedInstanceState.getSerializable("stations"); |
162 |
adapter.setStations(stations); |
adapter.setStations(stations); |
163 |
} |
} |
164 |
|
|
165 |
} |
} |
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
|
172 |
|
@Override |
173 |
|
protected void onDestroy() { |
174 |
|
super.onDestroy(); |
175 |
|
|
176 |
|
stationsFetched.removeMessages(0); |
177 |
|
|
178 |
|
|
179 |
|
if (locationLookup != null) { |
180 |
|
locationLookup.stopSearch(); |
181 |
|
} |
182 |
|
|
183 |
|
} |
184 |
|
|
185 |
|
|
186 |
|
protected void setTitle() { |
187 |
|
String dialogTitle = getResources().getString(app_name); |
188 |
|
switch (listType) { |
189 |
|
case ListNearest: |
190 |
|
dialogTitle += " - " + getString(stationlist_nearbystations); |
191 |
|
break; |
192 |
|
case ListSearch: |
193 |
|
dialogTitle += " - " + getString(stationlist_search); |
194 |
|
break; |
195 |
|
case ListFavorites: |
196 |
|
dialogTitle += " - " + getString(stationlist_favorites); |
197 |
|
break; |
198 |
|
default: |
199 |
|
dialogTitle = "";//not possible |
200 |
|
} |
201 |
|
|
202 |
|
setTitle(dialogTitle); |
203 |
|
|
204 |
|
} |
205 |
|
|
206 |
|
|
207 |
|
public void enableNamesearchField() { |
208 |
|
final TextView stationSearch = (TextView) findViewById(R.id.stationsearch); |
209 |
|
stationSearch.setVisibility( View.VISIBLE ); |
210 |
|
stationSearch.addTextChangedListener( new TextWatcher() { |
211 |
|
|
212 |
|
@Override |
213 |
|
public void afterTextChanged(Editable s) { |
214 |
|
if (s.length() > 0) { |
215 |
|
startNameLookup( s.toString() ); |
216 |
|
} else { |
217 |
|
stations = new StationBean(); |
218 |
|
getListView().invalidateViews(); |
219 |
|
adapter.setStations( stations ); |
220 |
|
} |
221 |
|
} |
222 |
|
|
223 |
|
@Override |
224 |
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
225 |
|
} |
226 |
|
|
227 |
|
@Override |
228 |
|
public void onTextChanged(CharSequence s, int start, int before, int count) { |
229 |
|
} |
230 |
|
|
231 |
|
}); |
232 |
|
|
233 |
|
|
234 |
|
} |
235 |
|
|
236 |
|
|
237 |
@Override |
@Override |
238 |
public void onSaveInstanceState(Bundle outState) |
public void onSaveInstanceState(Bundle outState) |
239 |
{ |
{ |
240 |
if (dialog != null && dialog.isShowing()) |
if (dialog != null && dialog.isShowing()) |
241 |
dialog.dismiss(); |
dialog.dismiss(); |
242 |
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
outState.putSerializable("stations", (StationBean) stations); |
243 |
|
outState.putString("search", ((TextView)findViewById(R.id.stationsearch)).getText().toString() ); |
244 |
|
|
245 |
} |
} |
246 |
|
|
247 |
|
|
248 |
|
|
249 |
@Override |
@Override |
250 |
|
public boolean onCreateOptionsMenu(Menu menu) { |
251 |
|
MenuItem item; |
252 |
|
|
253 |
|
item = menu.add(0, OPTIONS_MAP, 0, getString(stationlist_stationmap)); |
254 |
|
item.setIcon(android.R.drawable.ic_menu_mapmode); |
255 |
|
|
256 |
|
item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo)); |
257 |
|
item.setIcon(android.R.drawable.ic_menu_info_details); |
258 |
|
boolean hasLoc = (locationLookup.getLocation() != null); |
259 |
|
item.setEnabled(hasLoc); |
260 |
|
|
261 |
|
return true; |
262 |
|
} |
263 |
|
|
264 |
|
|
265 |
|
|
266 |
|
|
267 |
|
@Override |
268 |
|
public boolean onOptionsItemSelected(MenuItem item) { |
269 |
|
boolean retval = true; |
270 |
|
|
271 |
|
|
272 |
|
switch (item.getItemId()) { |
273 |
|
case OPTIONS_MAP: |
274 |
|
|
275 |
|
if ( stations == null || stations.entries == null || stations.entries.size() == 0 ) { |
276 |
|
Toast.makeText(this, "No stations to show on map", Toast.LENGTH_SHORT).show(); //TODO: Translate |
277 |
|
return true; |
278 |
|
} |
279 |
|
|
280 |
|
Intent intent = new Intent(this,StationMapView.class); |
281 |
|
|
282 |
|
ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>(); |
283 |
|
for (StationEntry st : stations.entries ) { |
284 |
|
stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) ); |
285 |
|
} |
286 |
|
|
287 |
|
intent.putExtra("stations", stationPoints); |
288 |
|
|
289 |
|
startActivity(intent); |
290 |
|
break; |
291 |
|
case OPTIONS_GPSINFO: |
292 |
|
Location loc = locationLookup.getLocation(); |
293 |
|
StringBuffer message = new StringBuffer(); |
294 |
|
message.append( getString(stationlist_locationinfo) ).append(":\n"); |
295 |
|
|
296 |
|
message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n"); |
297 |
|
message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n"); |
298 |
|
message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n"); |
299 |
|
message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n"); |
300 |
|
|
301 |
|
|
302 |
|
MessageBox.showMessage(this, message.toString(), false); |
303 |
|
break; |
304 |
|
default: |
305 |
|
retval = super.onOptionsItemSelected(item); |
306 |
|
} |
307 |
|
|
308 |
|
return retval; |
309 |
|
} |
310 |
|
|
311 |
|
|
312 |
|
|
313 |
|
@Override |
314 |
|
public boolean onContextItemSelected(MenuItem item) { |
315 |
|
contextMenu.onContextItemSelected(item); |
316 |
|
return true; |
317 |
|
|
318 |
|
|
319 |
|
} |
320 |
|
|
321 |
|
public void showMessageAndClose(String message) { |
322 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
323 |
|
builder.setMessage(message) |
324 |
|
.setCancelable(false) |
325 |
|
.setPositiveButton("OK", new DialogInterface.OnClickListener() { |
326 |
|
public void onClick(DialogInterface dialog, int id) { |
327 |
|
dialog.dismiss(); |
328 |
|
StationList.this.finish(); |
329 |
|
} |
330 |
|
}) |
331 |
|
.show(); |
332 |
|
} |
333 |
|
|
334 |
|
|
335 |
|
|
336 |
|
|
337 |
|
@Override |
338 |
protected Dialog onCreateDialog(int id) { |
protected Dialog onCreateDialog(int id) { |
339 |
switch (id) { |
switch (id) { |
340 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
341 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
342 |
dlg.setMessage("Wait for location fix"); |
dlg.setMessage( getString(stationlist_waitforlocation) ); |
343 |
dlg.setCancelable(false); |
dlg.setCancelable(false); |
344 |
return dlg; |
return dlg; |
345 |
|
|
346 |
default: |
default: |
347 |
return super.onCreateDialog(id); |
return super.onCreateDialog(id); |
348 |
} |
} |
350 |
} |
} |
351 |
|
|
352 |
|
|
|
|
|
353 |
@Override |
@Override |
354 |
protected void onPrepareDialog(int id, Dialog dialog) { |
protected void onPrepareDialog(int id, Dialog dialog) { |
355 |
super.onPrepareDialog(id, dialog); |
super.onPrepareDialog(id, dialog); |
356 |
switch (id) { |
switch (id) { |
357 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
358 |
this.dialog = (ProgressDialog) dialog; |
this.dialog = (ProgressDialog) dialog; |
359 |
|
if (!dialogMessage.equals("")) { |
360 |
|
this.dialog.setMessage(dialogMessage); |
361 |
|
dialogMessage = ""; |
362 |
|
} |
363 |
break; |
break; |
364 |
} |
} |
365 |
} |
} |
366 |
|
|
367 |
|
@Override |
368 |
|
protected void onListItemClick(ListView l, View v, int position, long id) { |
369 |
|
super.onListItemClick(l, v, position, id); |
370 |
|
|
371 |
|
StationEntry station = stations.entries.get(position); |
372 |
|
|
373 |
|
if (isLaunchedforShortcut == true) { |
374 |
|
Intent i = new Intent(); |
375 |
|
i.putExtra("station", station); |
376 |
|
setResult(Activity.RESULT_OK, i); |
377 |
|
finish(); |
378 |
|
} else { |
379 |
|
Intent intent = new Intent(this, DepartureList.class); |
380 |
|
intent.putExtra("stationbean", station); |
381 |
|
startActivity(intent); |
382 |
|
} |
383 |
|
} |
384 |
|
|
385 |
public void startLookup() { |
///////////////////////////////////////////////////////////// |
386 |
isRunning = true; |
// |
387 |
|
|
388 |
|
public void startNearestLookup() { |
389 |
|
dialogMessage = getString( stationlist_waitforlocation ); |
390 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
391 |
|
|
392 |
locator.locateStations(); |
locationLookup.locateStations(); |
393 |
stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000); |
stationsFetched.sendEmptyMessageDelayed(0, 500); |
394 |
|
} |
395 |
|
|
396 |
|
void startNameLookup(String name) { |
397 |
|
|
398 |
|
stations = stationProvider.lookupStationsByName(name); |
399 |
|
getListView().invalidateViews(); |
400 |
|
adapter.setStations( stations ); |
401 |
|
} |
402 |
|
|
403 |
|
public void startFavoriteLookup() { |
404 |
|
|
405 |
|
if (favorites.size() > 0) { |
406 |
|
|
407 |
|
stations = stationProvider.lookupStationsByIds( favorites.toString() ); |
408 |
|
getListView().invalidateViews(); |
409 |
|
adapter.setStations( stations ); |
410 |
|
|
411 |
|
|
412 |
|
} else { |
413 |
|
showMessageAndClose( getString( stationlist_nofavorites ) ); |
414 |
|
} |
415 |
} |
} |
416 |
|
|
417 |
|
|
418 |
Handler stationsFetched = new Handler() { |
|
419 |
|
void startNearestLookupPhase2() |
420 |
|
{ |
421 |
|
stations = stationProvider.lookupStationsByLocation( locationLookup.getLocation() ); |
422 |
|
getListView().invalidateViews(); |
423 |
|
adapter.setStations( stations ); |
424 |
|
} |
425 |
|
|
426 |
|
|
427 |
|
//////////////////////////////////////////////////////////////////////////// |
428 |
|
// Inner classes |
429 |
|
|
430 |
|
class StationsFetchedHandler extends Handler { |
431 |
@Override |
@Override |
432 |
public void handleMessage(Message msg) { |
public void handleMessage(Message msg) { |
433 |
switch (msg.what) { |
|
434 |
|
LocationLookup.LookupStates state = locationLookup.getState(); |
435 |
|
|
436 |
|
|
437 |
|
switch (state) { |
438 |
case GOTLOCATION: |
case GOTLOCATION: |
439 |
dialog.setMessage("Finding nearby stations"); |
dismissDialog(DLG_PROGRESS); |
440 |
locatorTask.execute(); |
|
441 |
break; |
startNearestLookupPhase2(); |
442 |
case GOTSTATIONLIST: |
|
443 |
dialog.dismiss(); |
return; |
444 |
if (locator.getStations().size() == 0) |
|
|
MessageBox.showMessage(TrainInfoList.this,"Error loading station list!"); |
|
|
stations = locator.getStations(); |
|
|
adapter.setStations( stations ); |
|
|
break; |
|
445 |
case NOPROVIDER: |
case NOPROVIDER: |
446 |
dialog.dismiss(); |
dismissDialog(DLG_PROGRESS); |
447 |
MessageBox.showMessage(TrainInfoList.this,"No location provider enabled. Plase enable gps."); |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); |
448 |
break; |
//StationList.this.finish(); |
449 |
case FIXTIMEOUT: |
return; |
450 |
dialog.dismiss(); |
case IDLE: |
451 |
if (isRunning) { |
Log.e("TrainInfo", "How did this happen ???"); |
452 |
locator.abortLocationListener(); |
dismissDialog(DLG_PROGRESS); // how did we get here ?? |
453 |
if (locator.hasLocation()) { |
return; |
454 |
msg.what = GOTLOCATION; |
|
455 |
handleMessage( msg ); // ToDo: ugly recursive call !!! |
} |
456 |
} else { |
|
457 |
MessageBox.showMessage(TrainInfoList.this,"GPS fix timed out"); |
|
458 |
} |
|
459 |
|
if (locationLookup.elapsedTime() >= GPS_TIMEOUT_MS) { |
460 |
|
dismissDialog(DLG_PROGRESS); |
461 |
|
|
462 |
|
|
463 |
|
locationLookup.stopSearch(); |
464 |
|
|
465 |
|
if (locationLookup.hasLocation()) { |
466 |
|
startNearestLookupPhase2(); |
467 |
|
} else { |
468 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
469 |
|
builder.setMessage( getString( stationlist_gpstimeout) ); |
470 |
|
builder.setCancelable(true); |
471 |
|
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
472 |
|
public void onClick(DialogInterface dialog, int id) { |
473 |
|
dialog.dismiss(); |
474 |
|
startNearestLookup(); |
475 |
|
|
476 |
|
} |
477 |
|
}); |
478 |
|
builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() { |
479 |
|
public void onClick(DialogInterface dialog, int id) { |
480 |
|
dialog.dismiss(); |
481 |
|
StationList.this.finish(); // Close this Activity |
482 |
|
} |
483 |
|
}); |
484 |
|
builder.show(); |
485 |
|
|
486 |
} |
} |
487 |
break; |
} else { |
488 |
case LOOKUPSTATIONFAILED: |
if (locationLookup.hasGps()) { |
489 |
dialog.dismiss(); |
int count = locationLookup.getSatCount(); |
490 |
MessageBox.showMessage(TrainInfoList.this,"Error on finding nearby stations"); |
String dialogMessage = getString( stationlist_waitforlocation ) + "\n" + getString( stationlist_satellitecount ) + ": " + count; |
491 |
break; |
dialog.setMessage( dialogMessage ); |
492 |
|
} |
493 |
|
this.sendEmptyMessageDelayed(0, 500); |
494 |
} |
} |
495 |
isRunning = false; |
|
496 |
} |
} |
497 |
}; |
} |
498 |
|
|
499 |
|
|
500 |
|
|
501 |
|
|
502 |
@Override |
class FavoritesMenu implements OnCreateContextMenuListener { |
503 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
private final static int FAVORITES_ADD = 9001; |
504 |
super.onListItemClick(l, v, position, id); |
private final static int FAVORITES_REMOVE = 9002; |
505 |
|
|
506 |
|
private int selectedPosition; |
507 |
|
|
|
StationBean station = stations.get(position); |
|
508 |
|
|
|
|
|
|
Intent intent = new Intent(this, DepartureList.class); |
|
|
intent.putExtra("name", station.getName()); |
|
|
intent.putExtra("address", station.getAddress()); |
|
|
intent.putExtra("distance", station.getDistance()); |
|
|
intent.putExtra("latitude", station.getLatitude()); |
|
|
intent.putExtra("longitude", station.getLongitude()); |
|
|
startActivity(intent); |
|
|
} |
|
|
|
|
|
|
|
|
class LocatorTask extends AsyncTask<Void,Void,Void> { |
|
509 |
@Override |
@Override |
510 |
protected void onPreExecute() { |
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { |
511 |
|
|
512 |
super.onPreExecute(); |
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; |
513 |
|
selectedPosition = info.position; |
514 |
|
int stationID = stations.entries.get(selectedPosition).getId(); |
515 |
|
|
516 |
|
if (!favorites.contains(stationID)) { |
517 |
|
menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) ); |
518 |
|
} else { |
519 |
|
menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_removefavorite) ); |
520 |
|
} |
521 |
|
|
522 |
} |
} |
523 |
|
|
524 |
@Override |
public void onContextItemSelected(MenuItem item) { |
525 |
protected Void doInBackground(Void... params) { |
StationEntry sb = stations.entries.get(selectedPosition); |
|
locator.findNearestStations(); |
|
526 |
|
|
527 |
return null; |
int stationID = sb.getId(); |
528 |
} |
if (item.getItemId() == FAVORITES_ADD) { |
529 |
|
favorites.add(stationID); |
530 |
@Override |
Toast.makeText(StationList.this, getString(stationlist_stationadded), Toast.LENGTH_SHORT).show(); |
531 |
protected void onPostExecute(Void result) { |
} else { |
532 |
super.onPostExecute(result); |
|
533 |
|
favorites.remove(stationID); |
534 |
|
Toast.makeText(StationList.this, getString(stationlist_stationremoved), Toast.LENGTH_SHORT).show(); |
535 |
|
|
536 |
|
|
537 |
|
if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) { |
538 |
|
stations.entries.remove(selectedPosition); |
539 |
|
adapter.notifyDataSetChanged(); |
540 |
|
} |
541 |
|
} |
542 |
|
Editor ed = prefs.edit(); |
543 |
|
ed.putString("favorites", favorites.toString()); |
544 |
|
ed.commit(); |
545 |
} |
} |
546 |
} |
} |
547 |
} |
} |