/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/StationList.java
ViewVC logotype

Annotation of /android/TrainInfo/src/dk/thoerup/traininfo/StationList.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 917 - (hide annotations) (download)
Sat Jun 26 10:50:34 2010 UTC (13 years, 11 months ago) by torben
File size: 17203 byte(s)
Attempt to silence this exception

java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(16908298, class android.widget.ListView) with Adapter(class dk.thoerup.traininfo.DepartureListAdapter)]

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

  ViewVC Help
Powered by ViewVC 1.1.20