/[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 725 - (hide annotations) (download)
Tue May 11 05:38:18 2010 UTC (14 years ago) by torben
File size: 16756 byte(s)
Another dialog exception-prevention hack
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 torben 701 this.showDialogSafe(DLG_STATIONNAME);
123 torben 481 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 torben 701
156 torben 482 }
157    
158    
159 torben 701 /* these 3 dialogs helper functions are very rude and ugly hack
160     * to remove these auto-reported exceptions
161     * - android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@436aaef8 is not valid; is your activity running?
162     * - java.lang.IllegalArgumentException: View not attached to window manager
163     */
164    
165     public void showDialogSafe(int id) {
166     try {
167     showDialog(id);
168     } catch (Exception e) {
169     Log.e("StationList", "showDialog failed", e);
170     }
171     }
172    
173     public void dismissDialogSafe(int id) {
174     try {
175     dismissDialog(id);
176     } catch (Exception e) {
177     Log.e("StationList", "dismissDialog failed", e);
178     }
179     }
180 torben 725 public void dismissDialogSafe(Dialog dlg) {
181     try {
182     dlg.dismiss();
183     } catch (Exception e) {
184     Log.e("StationList", "dismissDialog failed", e);
185     }
186     }
187 torben 701
188     public void builderShowSafe(AlertDialog.Builder builder) {
189     try {
190     builder.show();
191     } catch (Exception e) {
192     Log.e("StationList", "builder.show() failed", e);
193     }
194    
195     }
196     /* EOF rude and ugly dialog hack */
197    
198 torben 371
199    
200 torben 243 @Override
201     public void onSaveInstanceState(Bundle outState)
202     {
203 torben 258 if (dialog != null && dialog.isShowing())
204 torben 243 dialog.dismiss();
205 torben 258 outState.putSerializable("stations", (ArrayList<StationBean>) stations);
206 torben 374 outState.putSerializable("location", location);
207 torben 482
208 torben 243 }
209 torben 237
210 torben 243
211 torben 237
212     @Override
213 torben 368 public boolean onCreateOptionsMenu(Menu menu) {
214 torben 419 MenuItem item;
215 torben 561
216     item = menu.add(0, OPTIONS_MAP, 0, getString(stationlist_stationmap));
217 torben 419 item.setIcon(android.R.drawable.ic_menu_mapmode);
218    
219 torben 561 item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo));
220 torben 481 item.setIcon(android.R.drawable.ic_menu_mapmode);
221    
222 torben 368 return true;
223     }
224    
225     @Override
226     public boolean onOptionsItemSelected(MenuItem item) {
227 torben 381 boolean retval = true;
228    
229 torben 481 //TODO: Cleanup
230 torben 368 switch (item.getItemId()) {
231     case OPTIONS_MAP:
232    
233     Intent intent = new Intent(this,StationMapView.class);
234    
235     ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();
236     for (StationBean st : stations ) {
237 torben 369 stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) );
238 torben 368 }
239    
240     intent.putExtra("stations", stationPoints);
241    
242     startActivity(intent);
243     break;
244 torben 481 case OPTIONS_GPSINFO:
245 torben 482 Location loc = locationLookup.getLocation();
246 torben 371 StringBuffer message = new StringBuffer();
247 torben 561 message.append( getString(stationlist_locationinfo) ).append(":\n");
248 torben 554 if (loc != null) {
249 torben 561 message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n");
250     message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n");
251 torben 564 message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n");
252     message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n");
253 torben 554 } else {
254 torben 561 message.append( getString(stationlist_nolocation) );
255 torben 556 }
256    
257 torben 371 MessageBox.showMessage(this, message.toString());
258     break;
259 torben 368 default:
260     retval = super.onOptionsItemSelected(item);
261     }
262    
263     return retval;
264     }
265 torben 433
266    
267 torben 368
268     @Override
269 torben 433 public boolean onContextItemSelected(MenuItem item) {
270     contextMenu.onContextItemSelected(item);
271     return true;
272    
273    
274     }
275 torben 556
276     public void showMessageAndClose(String message) {
277     AlertDialog.Builder builder = new AlertDialog.Builder(this);
278     builder.setMessage(message)
279     .setCancelable(false)
280     .setPositiveButton("OK", new DialogInterface.OnClickListener() {
281     public void onClick(DialogInterface dialog, int id) {
282     dialog.dismiss();
283     StationList.this.finish();
284     }
285     })
286     .show();
287     }
288 torben 433
289    
290    
291    
292     @Override
293 torben 237 protected Dialog onCreateDialog(int id) {
294     switch (id) {
295     case DLG_PROGRESS:
296     ProgressDialog dlg = new ProgressDialog(this);
297 torben 561 dlg.setMessage( getString(stationlist_waitforlocation) );
298 torben 237 dlg.setCancelable(false);
299 torben 336 return dlg;
300 torben 381 case DLG_STATIONNAME:
301     LayoutInflater factory = LayoutInflater.from(this);
302     final View rootView = factory.inflate(R.layout.textinput, null);
303    
304    
305     AlertDialog.Builder builder = new AlertDialog.Builder(this);
306    
307 torben 561 builder.setTitle( getString(stationlist_stationsearch) );
308 torben 381 builder.setView(rootView);
309     builder.setCancelable(true);
310 torben 561 builder.setPositiveButton( getString(generic_search), new DialogInterface.OnClickListener() {
311 torben 381 public void onClick(DialogInterface dialog, int which) {
312     EditText et = (EditText) rootView.findViewById(R.id.EditText);
313     dialog.dismiss();
314 torben 713 String search = et.getText().toString().trim();
315     if (search.length() >= 2) {
316     startNameSearch(search);
317 torben 381 } else {
318 torben 561 showMessageAndClose( getString(stationlist_twocharmin) );
319 torben 381 }
320     }
321     });
322 torben 561 builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
323 torben 381 public void onClick(DialogInterface dialog, int which) {
324     dialog.dismiss();
325 torben 555 StationList.this.finish(); // Close this Activity
326 torben 381 }
327     });
328     return builder.create();
329    
330 torben 237 default:
331     return super.onCreateDialog(id);
332     }
333    
334     }
335    
336    
337     @Override
338     protected void onPrepareDialog(int id, Dialog dialog) {
339     super.onPrepareDialog(id, dialog);
340     switch (id) {
341     case DLG_PROGRESS:
342     this.dialog = (ProgressDialog) dialog;
343 torben 336 if (!dialogMessage.equals("")) {
344     this.dialog.setMessage(dialogMessage);
345     dialogMessage = "";
346     }
347 torben 237 break;
348     }
349     }
350 torben 381
351     @Override
352     protected void onListItemClick(ListView l, View v, int position, long id) {
353     super.onListItemClick(l, v, position, id);
354    
355     StationBean station = stations.get(position);
356    
357     Intent intent = new Intent(this, DepartureList.class);
358 torben 557 intent.putExtra("stationbean", station);
359 torben 381 startActivity(intent);
360     }
361    
362     /////////////////////////////////////////////////////////////
363     //
364    
365 torben 237 public void startLookup() {
366 torben 561 isRunning = true;
367     dialogMessage = getString( stationlist_waitforlocation );
368 torben 701 showDialogSafe(DLG_PROGRESS);
369 torben 237
370 torben 482 locationLookup.locateStations();
371 torben 336 stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);
372 torben 237 }
373 torben 381
374     void startNameSearch(String name) {
375 torben 561 dialogMessage = getString( stationlist_findbyname );
376 torben 701 showDialogSafe(DLG_PROGRESS);
377 torben 237
378 torben 440 findStationsTask = new FindStationsTask();
379 torben 579 findStationsTask.searchByName(name);
380 torben 440 findStationsTask.execute();
381 torben 381
382     }
383 torben 433
384     public void startFavoriteLookup() {
385    
386 torben 440 if (favorites.size() > 0) {
387 torben 561 dialogMessage = getString( stationlist_loadfavorites );
388 torben 701 showDialogSafe(DLG_PROGRESS);
389 torben 237
390 torben 440 findStationsTask = new FindStationsTask();
391 torben 579 findStationsTask.searchByIds( favorites.toString() );
392 torben 440 findStationsTask.execute();
393 torben 433 } else {
394 torben 561 showMessageAndClose( getString( stationlist_nofavorites ) );
395 torben 433 }
396     }
397 torben 381
398 torben 433
399 torben 381
400     void startLocatorTask()
401     {
402 torben 561 dialogMessage = getString( stationlist_findingnearby );
403 torben 701 showDialogSafe(DLG_PROGRESS);
404 torben 381
405 torben 440 findStationsTask = new FindStationsTask();
406 torben 482 findStationsTask.searchByLocation( locationLookup.getLocation() );
407 torben 440 findStationsTask.execute();
408 torben 381 }
409    
410    
411     ////////////////////////////////////////////////////////////////////////////
412     // Inner classes
413    
414     class StationsFetchedHandler extends Handler {
415 torben 237 @Override
416     public void handleMessage(Message msg) {
417 torben 336
418 torben 237 switch (msg.what) {
419     case GOTLOCATION:
420 torben 701 dismissDialogSafe(DLG_PROGRESS);
421 torben 336
422     startLocatorTask();
423 torben 482 location = GeoPair.fromLocation( locationLookup.getLocation() );
424 torben 336
425 torben 237 break;
426 torben 319
427 torben 237 case NOPROVIDER:
428 torben 701 dismissDialogSafe(DLG_PROGRESS);
429 torben 561 MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) );
430 torben 237 break;
431 torben 336 case LOCATIONFIXTIMEOUT:
432 torben 237 if (isRunning) {
433 torben 482 locationLookup.stopSearch();
434     if (locationLookup.hasLocation()) {
435 torben 336 stationsFetched.sendEmptyMessage( GOTLOCATION );
436     } else {
437 torben 701 dismissDialogSafe(DLG_PROGRESS);
438 torben 336
439     AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);
440 torben 561 builder.setMessage( getString( stationlist_gpstimeout) );
441 torben 336 builder.setCancelable(true);
442 torben 561 builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
443 torben 336 public void onClick(DialogInterface dialog, int id) {
444     dialog.dismiss();
445     startLookup();
446    
447     }
448     });
449 torben 561 builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() {
450 torben 336 public void onClick(DialogInterface dialog, int id) {
451     dialog.dismiss();
452     }
453 torben 701 });
454     builderShowSafe(builder); // builder.show()
455 torben 336
456 torben 285 }
457 torben 237 }
458     break;
459     }
460     isRunning = false;
461     }
462     };
463 torben 381
464 torben 237
465 torben 440 class FindStationsTask extends AsyncTask<Void,Void,Void> {
466 torben 336
467 torben 381 LookupMethod method = LookupMethod.MethodNone;
468     boolean success;
469     String name;
470     Location loc;
471 torben 433 String ids;
472 torben 237
473 torben 579 public void searchByName(String n) {
474 torben 317
475 torben 381 method = LookupMethod.ByName;
476     name = n;
477 torben 317 }
478    
479 torben 381 public void searchByLocation(Location l) {
480     method = LookupMethod.ByLocation;
481     loc = l;
482     }
483    
484 torben 579 public void searchByIds(String id) {
485 torben 433
486     method = LookupMethod.ByList;
487     ids = id;
488     }
489    
490 torben 241 @Override
491     protected void onPreExecute() {
492 torben 258
493 torben 381 if (method.equals(LookupMethod.MethodNone))
494     throw new RuntimeException("Method not set");
495 torben 241 super.onPreExecute();
496     }
497    
498     @Override
499     protected Void doInBackground(Void... params) {
500 torben 473
501     switch (method) {
502     case ByLocation:
503 torben 381 success = stationProvider.lookupStations(loc);
504 torben 473 break;
505     case ByName:
506 torben 433 success = stationProvider.lookupStationsByName(name);
507 torben 473 break;
508     case ByList:
509 torben 433 success = stationProvider.lookupStationsByIds(ids);
510 torben 473 break;
511     default:
512     success = false; // not possible
513     }
514 torben 433
515 torben 473
516 torben 241 return null;
517     }
518    
519     @Override
520     protected void onPostExecute(Void result) {
521     super.onPostExecute(result);
522 torben 725 dismissDialogSafe(dialog);
523 torben 319
524 torben 473
525 torben 319 if (success) {
526 torben 566 if (stationProvider.getStations().size() == 0) {
527     showMessageAndClose(getString(stationlist_nostations));
528     }
529 torben 319 stations = stationProvider.getStations();
530 torben 481 adapter.setStations( stations );
531 torben 319
532     } else { //communication or parse errors
533 torben 336 AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);
534 torben 561 builder.setMessage(getString(stationlist_nearbyerror));
535 torben 336 builder.setCancelable(true);
536 torben 561 builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
537 torben 336 public void onClick(DialogInterface dialog, int id) {
538     dialog.dismiss();
539    
540 torben 652 Runnable runner = null;
541     switch (method) {
542     case ByLocation:
543     runner = new Runnable() {
544     @Override
545     public void run() {
546     startLocatorTask();
547     }
548     };
549     break;
550     case ByName:
551     runner = new Runnable() {
552     @Override
553     public void run() {
554     startNameSearch( FindStationsTask.this.name );
555     }
556     };
557     break;
558     case ByList:
559     runner = new Runnable() {
560     @Override
561     public void run() {
562     startFavoriteLookup();
563     }
564     };
565     break;
566     }
567    
568     stationsFetched.post( runner );
569 torben 336 }
570     });
571 torben 561 builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
572 torben 336 public void onClick(DialogInterface dialog, int id) {
573     dialog.dismiss();
574     }
575 torben 701 });
576    
577     builderShowSafe(builder); // builder.show()
578 torben 319 }
579 torben 241 }
580     }
581 torben 433
582    
583     class FavoritesMenu implements OnCreateContextMenuListener {
584     private final static int FAVORITES_ADD = 9001;
585     private final static int FAVORITES_REMOVE = 9002;
586    
587     private int selectedPosition;
588    
589    
590     @Override
591     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
592    
593     AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
594     selectedPosition = info.position;
595     int stationID = stations.get(selectedPosition).getId();
596    
597 torben 441 if (!favorites.contains(stationID)) {
598 torben 561 menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) );
599 torben 433 } else {
600 torben 563 menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_removefavorite) );
601 torben 433 }
602    
603     }
604    
605     public void onContextItemSelected(MenuItem item) {
606     StationBean sb = stations.get(selectedPosition);
607    
608     int stationID = sb.getId();
609     if (item.getItemId() == FAVORITES_ADD) {
610     favorites.add(stationID);
611 torben 561 Toast.makeText(StationList.this, getString(stationlist_stationadded), Toast.LENGTH_SHORT).show();
612 torben 433 } else {
613 torben 473
614 torben 433 favorites.remove(stationID);
615 torben 561 Toast.makeText(StationList.this, getString(stationlist_stationremoved), Toast.LENGTH_SHORT).show();
616 torben 473
617 torben 481
618     if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) {
619 torben 473 stations.remove(selectedPosition);
620     adapter.notifyDataSetChanged();
621     }
622 torben 433 }
623 torben 435 Editor ed = prefs.edit();
624     ed.putString("favorites", favorites.toString());
625     ed.commit();
626 torben 433 }
627     }
628 torben 237 }

  ViewVC Help
Powered by ViewVC 1.1.20