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

  ViewVC Help
Powered by ViewVC 1.1.20