/[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 652 - (hide annotations) (download)
Tue Apr 20 14:17:34 2010 UTC (14 years, 1 month ago) by torben
File size: 15849 byte(s)
Bug fix: If we retry a station lookup we should retry with the method used in the previous attempt

maybe this resolves all the reported null pointer exceptions from name lookup (I should verify this tonight!)


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

  ViewVC Help
Powered by ViewVC 1.1.20