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

  ViewVC Help
Powered by ViewVC 1.1.20