/[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 1709 - (hide annotations) (download)
Tue Mar 6 11:02:04 2012 UTC (12 years, 3 months ago) by torben
File size: 14767 byte(s)
refactor favorites handling to a seperate class and add favorite adder/remover to departurelist
1 torben 237 package dk.thoerup.traininfo;
2    
3 torben 1066 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.stationlist_accuracy;
7     import static dk.thoerup.traininfo.R.string.stationlist_addfavorite;
8     import static dk.thoerup.traininfo.R.string.stationlist_favorites;
9     import static dk.thoerup.traininfo.R.string.stationlist_gpsinfo;
10     import static dk.thoerup.traininfo.R.string.stationlist_gpstimeout;
11     import static dk.thoerup.traininfo.R.string.stationlist_latitude;
12     import static dk.thoerup.traininfo.R.string.stationlist_locationinfo;
13     import static dk.thoerup.traininfo.R.string.stationlist_longitude;
14     import static dk.thoerup.traininfo.R.string.stationlist_nearbystations;
15     import static dk.thoerup.traininfo.R.string.stationlist_nofavorites;
16     import static dk.thoerup.traininfo.R.string.stationlist_nolocationprovider;
17     import static dk.thoerup.traininfo.R.string.stationlist_obtainedby;
18     import static dk.thoerup.traininfo.R.string.stationlist_removefavorite;
19 torben 1219 import static dk.thoerup.traininfo.R.string.stationlist_satellitecount;
20 torben 1066 import static dk.thoerup.traininfo.R.string.stationlist_search;
21     import static dk.thoerup.traininfo.R.string.stationlist_stationadded;
22     import static dk.thoerup.traininfo.R.string.stationlist_stationmap;
23     import static dk.thoerup.traininfo.R.string.stationlist_stationremoved;
24     import static dk.thoerup.traininfo.R.string.stationlist_waitforlocation;
25    
26 torben 258 import java.util.ArrayList;
27    
28 torben 731 import android.app.Activity;
29 torben 336 import android.app.AlertDialog;
30 torben 237 import android.app.Dialog;
31     import android.app.ListActivity;
32     import android.app.ProgressDialog;
33 torben 336 import android.content.DialogInterface;
34 torben 237 import android.content.Intent;
35 torben 319 import android.location.Location;
36 torben 237 import android.os.Bundle;
37     import android.os.Handler;
38     import android.os.Message;
39 torben 1547 import android.text.Editable;
40     import android.text.TextWatcher;
41 torben 1219 import android.util.Log;
42 torben 433 import android.view.ContextMenu;
43 torben 1408 import android.view.ContextMenu.ContextMenuInfo;
44 torben 368 import android.view.Menu;
45     import android.view.MenuItem;
46 torben 237 import android.view.View;
47 torben 433 import android.view.View.OnCreateContextMenuListener;
48     import android.widget.AdapterView;
49 torben 237 import android.widget.ListView;
50 torben 1547 import android.widget.TextView;
51 torben 433 import android.widget.Toast;
52 torben 1066 import dk.thoerup.android.traininfo.common.StationBean;
53 torben 1408 import dk.thoerup.android.traininfo.common.StationEntry;
54 torben 1558 import dk.thoerup.traininfo.WelcomeScreen.ListType;
55 torben 319 import dk.thoerup.traininfo.provider.ProviderFactory;
56     import dk.thoerup.traininfo.provider.StationProvider;
57 torben 368 import dk.thoerup.traininfo.stationmap.GeoPair;
58     import dk.thoerup.traininfo.stationmap.StationMapView;
59 torben 1709 import dk.thoerup.traininfo.util.FavoritesHelper;
60 torben 245 import dk.thoerup.traininfo.util.MessageBox;
61 torben 237
62 torben 336 public class StationList extends ListActivity {
63 torben 1143
64 torben 368
65 torben 1142
66 torben 1448 public static final int OPTIONS_MAP = 103;
67     public static final int OPTIONS_GPSINFO = 104;
68 torben 433
69 torben 381 public static final int DLG_PROGRESS = 3001;
70 torben 237
71 torben 948
72 torben 1019 public static final int GPS_TIMEOUT_MS = 15000; //how long are we willing to wait for gps fix -in milliseconds
73 torben 948
74    
75 torben 440 static enum LookupMethod {
76     ByLocation,
77     ByName,
78     ByList,
79     MethodNone
80     }
81    
82    
83 torben 336 String dialogMessage = "";
84 torben 237 ProgressDialog dialog;
85 torben 482 LocationLookup locationLookup = null;
86 torben 381 StationsFetchedHandler stationsFetched = new StationsFetchedHandler();
87 torben 237
88 torben 1143 //GeoPair location = new GeoPair();
89 torben 731
90     boolean isLaunchedforShortcut;
91 torben 1143
92 torben 1066 StationBean stations = new StationBean();
93 torben 258
94 torben 319 StationProvider stationProvider = ProviderFactory.getStationProvider();
95 torben 381
96     StationListAdapter adapter = null;
97 torben 258
98 torben 433 FavoritesMenu contextMenu = new FavoritesMenu();
99 torben 1709 FavoritesHelper favorites;
100 torben 481
101     WelcomeScreen.ListType listType;
102 torben 1143
103 torben 381 ///////////////////////////////////////////////////////////////////////////////////////////
104     //Activity call backs
105    
106 torben 1078
107 torben 237 @Override
108     public void onCreate(Bundle savedInstanceState) {
109     super.onCreate(savedInstanceState);
110 torben 481 setContentView(R.layout.stationlist);
111 torben 1709
112     favorites = new FavoritesHelper(this);
113 torben 237
114 torben 1558 listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type");
115 torben 241
116 torben 1558 adapter = new StationListAdapter(this, (listType == ListType.ListNearest) );
117 torben 237 setListAdapter(adapter);
118    
119 torben 433 ListView lv = getListView();
120     lv.setOnCreateContextMenuListener(contextMenu);
121    
122 torben 1143 locationLookup = new LocationLookup(this);
123 torben 433
124    
125 torben 482 setTitle();
126    
127 torben 731 isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false);
128    
129 torben 1028 ProviderFactory.purgeOldEntries(); //cleanup before fetching more data
130    
131 torben 1576 if (listType == ListType.ListSearch) {
132     enableNamesearchField();
133     }
134    
135 torben 258 if (savedInstanceState == null) {
136 torben 1576
137 torben 481 switch (listType) {
138     case ListNearest:
139 torben 1207 startNearestLookup();
140 torben 481 break;
141 torben 983 case ListSearch:
142 torben 1576 // do nothing here
143 torben 481 break;
144     case ListFavorites:
145     startFavoriteLookup();
146     break;
147     default:
148     // Not possible !?!
149     }
150    
151 torben 258 } else {
152 torben 1577 ((TextView)findViewById(R.id.stationsearch)).setText( savedInstanceState.getString( "search") );
153    
154 torben 1066 stations = (StationBean) savedInstanceState.getSerializable("stations");
155 torben 260 adapter.setStations(stations);
156 torben 258 }
157 torben 482
158 torben 237 }
159 torben 918
160 torben 1027
161 torben 918
162 torben 1027
163    
164    
165     @Override
166 torben 918 protected void onDestroy() {
167     super.onDestroy();
168    
169 torben 1244 stationsFetched.removeMessages(0);
170 torben 1025
171 torben 1244
172 torben 919 if (locationLookup != null) {
173     locationLookup.stopSearch();
174     }
175 torben 1547
176 torben 918 }
177    
178    
179 torben 482 protected void setTitle() {
180 torben 561 String dialogTitle = getResources().getString(app_name);
181 torben 482 switch (listType) {
182     case ListNearest:
183 torben 561 dialogTitle += " - " + getString(stationlist_nearbystations);
184 torben 482 break;
185     case ListSearch:
186 torben 561 dialogTitle += " - " + getString(stationlist_search);
187 torben 482 break;
188     case ListFavorites:
189 torben 561 dialogTitle += " - " + getString(stationlist_favorites);
190 torben 482 break;
191     default:
192     dialogTitle = "";//not possible
193     }
194    
195     setTitle(dialogTitle);
196 torben 701
197 torben 482 }
198    
199 torben 1547
200     public void enableNamesearchField() {
201     final TextView stationSearch = (TextView) findViewById(R.id.stationsearch);
202     stationSearch.setVisibility( View.VISIBLE );
203     stationSearch.addTextChangedListener( new TextWatcher() {
204    
205     @Override
206     public void afterTextChanged(Editable s) {
207     if (s.length() > 0) {
208     startNameLookup( s.toString() );
209     } else {
210     stations = new StationBean();
211     getListView().invalidateViews();
212     adapter.setStations( stations );
213     }
214     }
215    
216     @Override
217     public void beforeTextChanged(CharSequence s, int start, int count, int after) {
218     }
219    
220     @Override
221     public void onTextChanged(CharSequence s, int start, int before, int count) {
222     }
223    
224     });
225 torben 701
226 torben 1547
227     }
228    
229 torben 371
230 torben 243 @Override
231     public void onSaveInstanceState(Bundle outState)
232     {
233 torben 258 if (dialog != null && dialog.isShowing())
234 torben 243 dialog.dismiss();
235 torben 1066 outState.putSerializable("stations", (StationBean) stations);
236 torben 1577 outState.putString("search", ((TextView)findViewById(R.id.stationsearch)).getText().toString() );
237 torben 482
238 torben 243 }
239 torben 237
240 torben 243
241 torben 237
242     @Override
243 torben 368 public boolean onCreateOptionsMenu(Menu menu) {
244 torben 419 MenuItem item;
245 torben 561
246     item = menu.add(0, OPTIONS_MAP, 0, getString(stationlist_stationmap));
247 torben 419 item.setIcon(android.R.drawable.ic_menu_mapmode);
248    
249 torben 561 item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo));
250 torben 1448 item.setIcon(android.R.drawable.ic_menu_info_details);
251 torben 1447 boolean hasLoc = (locationLookup.getLocation() != null);
252     item.setEnabled(hasLoc);
253 torben 481
254 torben 368 return true;
255     }
256 torben 1447
257 torben 368
258 torben 1447
259    
260 torben 368 @Override
261     public boolean onOptionsItemSelected(MenuItem item) {
262 torben 381 boolean retval = true;
263    
264 torben 1453
265 torben 368 switch (item.getItemId()) {
266     case OPTIONS_MAP:
267    
268 torben 1485 if ( stations == null || stations.entries == null || stations.entries.size() == 0 ) {
269 torben 1486 Toast.makeText(this, "No stations to show on map", Toast.LENGTH_SHORT).show(); //TODO: Translate
270 torben 1485 return true;
271     }
272    
273 torben 368 Intent intent = new Intent(this,StationMapView.class);
274    
275     ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();
276 torben 1066 for (StationEntry st : stations.entries ) {
277 torben 369 stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) );
278 torben 368 }
279    
280     intent.putExtra("stations", stationPoints);
281    
282     startActivity(intent);
283     break;
284 torben 481 case OPTIONS_GPSINFO:
285 torben 482 Location loc = locationLookup.getLocation();
286 torben 371 StringBuffer message = new StringBuffer();
287 torben 561 message.append( getString(stationlist_locationinfo) ).append(":\n");
288 torben 1447
289     message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n");
290     message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n");
291     message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n");
292     message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n");
293    
294 torben 556
295 torben 906 MessageBox.showMessage(this, message.toString(), false);
296 torben 371 break;
297 torben 368 default:
298     retval = super.onOptionsItemSelected(item);
299     }
300    
301     return retval;
302     }
303 torben 433
304    
305 torben 368
306     @Override
307 torben 433 public boolean onContextItemSelected(MenuItem item) {
308     contextMenu.onContextItemSelected(item);
309     return true;
310    
311    
312     }
313 torben 556
314     public void showMessageAndClose(String message) {
315     AlertDialog.Builder builder = new AlertDialog.Builder(this);
316     builder.setMessage(message)
317     .setCancelable(false)
318     .setPositiveButton("OK", new DialogInterface.OnClickListener() {
319     public void onClick(DialogInterface dialog, int id) {
320     dialog.dismiss();
321     StationList.this.finish();
322     }
323     })
324     .show();
325     }
326 torben 433
327    
328    
329    
330     @Override
331 torben 237 protected Dialog onCreateDialog(int id) {
332     switch (id) {
333     case DLG_PROGRESS:
334     ProgressDialog dlg = new ProgressDialog(this);
335 torben 561 dlg.setMessage( getString(stationlist_waitforlocation) );
336 torben 237 dlg.setCancelable(false);
337 torben 336 return dlg;
338 torben 381
339 torben 237 default:
340     return super.onCreateDialog(id);
341     }
342    
343     }
344    
345    
346     @Override
347     protected void onPrepareDialog(int id, Dialog dialog) {
348     super.onPrepareDialog(id, dialog);
349     switch (id) {
350     case DLG_PROGRESS:
351     this.dialog = (ProgressDialog) dialog;
352 torben 336 if (!dialogMessage.equals("")) {
353     this.dialog.setMessage(dialogMessage);
354     dialogMessage = "";
355     }
356 torben 237 break;
357     }
358     }
359 torben 381
360     @Override
361     protected void onListItemClick(ListView l, View v, int position, long id) {
362     super.onListItemClick(l, v, position, id);
363    
364 torben 1066 StationEntry station = stations.entries.get(position);
365 torben 381
366 torben 731 if (isLaunchedforShortcut == true) {
367     Intent i = new Intent();
368     i.putExtra("station", station);
369     setResult(Activity.RESULT_OK, i);
370     finish();
371     } else {
372     Intent intent = new Intent(this, DepartureList.class);
373     intent.putExtra("stationbean", station);
374     startActivity(intent);
375     }
376 torben 381 }
377    
378     /////////////////////////////////////////////////////////////
379     //
380    
381 torben 1207 public void startNearestLookup() {
382 torben 561 dialogMessage = getString( stationlist_waitforlocation );
383 torben 1008 showDialog(DLG_PROGRESS);
384 torben 237
385 torben 482 locationLookup.locateStations();
386 torben 1143 stationsFetched.sendEmptyMessageDelayed(0, 500);
387 torben 237 }
388 torben 381
389 torben 1207 void startNameLookup(String name) {
390 torben 381
391 torben 1547 stations = stationProvider.lookupStationsByName(name);
392     getListView().invalidateViews();
393     adapter.setStations( stations );
394 torben 381 }
395 torben 433
396     public void startFavoriteLookup() {
397    
398 torben 1709 if (favorites.getSize() > 0) {
399 torben 1547
400 torben 1709 stations = stationProvider.lookupStationsByIds( favorites.getString() );
401 torben 1547 getListView().invalidateViews();
402     adapter.setStations( stations );
403    
404    
405 torben 433 } else {
406 torben 561 showMessageAndClose( getString( stationlist_nofavorites ) );
407 torben 433 }
408     }
409 torben 381
410 torben 433
411 torben 381
412 torben 1547 void startNearestLookupPhase2()
413     {
414     stations = stationProvider.lookupStationsByLocation( locationLookup.getLocation() );
415     getListView().invalidateViews();
416     adapter.setStations( stations );
417 torben 381 }
418    
419    
420     ////////////////////////////////////////////////////////////////////////////
421     // Inner classes
422    
423     class StationsFetchedHandler extends Handler {
424 torben 237 @Override
425     public void handleMessage(Message msg) {
426 torben 336
427 torben 1143 LocationLookup.LookupStates state = locationLookup.getState();
428    
429    
430 torben 1142 switch (state) {
431 torben 237 case GOTLOCATION:
432 torben 1008 dismissDialog(DLG_PROGRESS);
433 torben 1143
434 torben 1547 startNearestLookupPhase2();
435 torben 319
436 torben 1143 return;
437    
438 torben 237 case NOPROVIDER:
439 torben 1008 dismissDialog(DLG_PROGRESS);
440 torben 906 MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true );
441     //StationList.this.finish();
442 torben 1143 return;
443 torben 1220 case IDLE:
444     Log.e("TrainInfo", "How did this happen ???");
445     dismissDialog(DLG_PROGRESS); // how did we get here ??
446     return;
447    
448 torben 1143 }
449 torben 336
450 torben 1143
451    
452     if (locationLookup.elapsedTime() >= GPS_TIMEOUT_MS) {
453 torben 1245 dismissDialog(DLG_PROGRESS);
454 torben 1143
455 torben 1245
456 torben 1143 locationLookup.stopSearch();
457    
458     if (locationLookup.hasLocation()) {
459 torben 1547 startNearestLookupPhase2();
460 torben 1143 } else {
461     AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);
462     builder.setMessage( getString( stationlist_gpstimeout) );
463     builder.setCancelable(true);
464     builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
465     public void onClick(DialogInterface dialog, int id) {
466     dialog.dismiss();
467 torben 1207 startNearestLookup();
468 torben 1143
469     }
470     });
471     builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() {
472     public void onClick(DialogInterface dialog, int id) {
473     dialog.dismiss();
474 torben 1150 StationList.this.finish(); // Close this Activity
475 torben 1143 }
476     });
477     builder.show();
478    
479 torben 237 }
480 torben 1143 } else {
481     if (locationLookup.hasGps()) {
482     int count = locationLookup.getSatCount();
483     String dialogMessage = getString( stationlist_waitforlocation ) + "\n" + getString( stationlist_satellitecount ) + ": " + count;
484     dialog.setMessage( dialogMessage );
485     }
486     this.sendEmptyMessageDelayed(0, 500);
487 torben 237 }
488 torben 1143
489 torben 237 }
490 torben 1143 }
491 torben 381
492 torben 1143
493    
494 torben 237
495 torben 433 class FavoritesMenu implements OnCreateContextMenuListener {
496     private final static int FAVORITES_ADD = 9001;
497     private final static int FAVORITES_REMOVE = 9002;
498    
499     private int selectedPosition;
500    
501    
502     @Override
503     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
504    
505     AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
506     selectedPosition = info.position;
507 torben 1066 int stationID = stations.entries.get(selectedPosition).getId();
508 torben 433
509 torben 1709 if (!favorites.hasFavorite(stationID)) {
510 torben 561 menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) );
511 torben 433 } else {
512 torben 563 menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_removefavorite) );
513 torben 433 }
514    
515     }
516    
517     public void onContextItemSelected(MenuItem item) {
518 torben 1066 StationEntry sb = stations.entries.get(selectedPosition);
519 torben 433
520     int stationID = sb.getId();
521     if (item.getItemId() == FAVORITES_ADD) {
522 torben 1709 favorites.addFavorite(stationID);
523 torben 561 Toast.makeText(StationList.this, getString(stationlist_stationadded), Toast.LENGTH_SHORT).show();
524 torben 433 } else {
525 torben 473
526 torben 1709 favorites.removeFavorite(stationID);
527 torben 561 Toast.makeText(StationList.this, getString(stationlist_stationremoved), Toast.LENGTH_SHORT).show();
528 torben 473
529 torben 481
530     if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) {
531 torben 1066 stations.entries.remove(selectedPosition);
532 torben 473 adapter.notifyDataSetChanged();
533     }
534 torben 433 }
535     }
536     }
537 torben 1709 }

  ViewVC Help
Powered by ViewVC 1.1.20