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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20