/[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 1547 - (hide annotations) (download)
Thu Jul 7 19:07:55 2011 UTC (12 years, 10 months ago) by torben
File size: 14742 byte(s)
First working version with offline stationlist
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 433 import android.content.SharedPreferences;
36 torben 435 import android.content.SharedPreferences.Editor;
37 torben 319 import android.location.Location;
38 torben 237 import android.os.Bundle;
39     import android.os.Handler;
40     import android.os.Message;
41 torben 1547 import android.text.Editable;
42     import android.text.TextWatcher;
43 torben 1219 import android.util.Log;
44 torben 433 import android.view.ContextMenu;
45 torben 1408 import android.view.ContextMenu.ContextMenuInfo;
46 torben 368 import android.view.Menu;
47     import android.view.MenuItem;
48 torben 237 import android.view.View;
49 torben 433 import android.view.View.OnCreateContextMenuListener;
50     import android.widget.AdapterView;
51 torben 237 import android.widget.ListView;
52 torben 1547 import android.widget.TextView;
53 torben 433 import android.widget.Toast;
54 torben 1066 import dk.thoerup.android.traininfo.common.StationBean;
55 torben 1408 import dk.thoerup.android.traininfo.common.StationEntry;
56 torben 319 import dk.thoerup.traininfo.provider.ProviderFactory;
57     import dk.thoerup.traininfo.provider.StationProvider;
58 torben 368 import dk.thoerup.traininfo.stationmap.GeoPair;
59     import dk.thoerup.traininfo.stationmap.StationMapView;
60 torben 433 import dk.thoerup.traininfo.util.IntSet;
61 torben 245 import dk.thoerup.traininfo.util.MessageBox;
62 torben 237
63 torben 336 public class StationList extends ListActivity {
64 torben 1143
65 torben 368
66 torben 1142
67 torben 1448 public static final int OPTIONS_MAP = 103;
68     public static final int OPTIONS_GPSINFO = 104;
69 torben 433
70 torben 381 public static final int DLG_PROGRESS = 3001;
71 torben 237
72 torben 948
73 torben 1019 public static final int GPS_TIMEOUT_MS = 15000; //how long are we willing to wait for gps fix -in milliseconds
74 torben 948
75    
76 torben 440 static enum LookupMethod {
77     ByLocation,
78     ByName,
79     ByList,
80     MethodNone
81     }
82    
83    
84 torben 336 String dialogMessage = "";
85 torben 237 ProgressDialog dialog;
86 torben 482 LocationLookup locationLookup = null;
87 torben 381 StationsFetchedHandler stationsFetched = new StationsFetchedHandler();
88 torben 237
89 torben 1143 //GeoPair location = new GeoPair();
90 torben 731
91     boolean isLaunchedforShortcut;
92 torben 1143
93 torben 1066 StationBean stations = new StationBean();
94 torben 258
95 torben 319 StationProvider stationProvider = ProviderFactory.getStationProvider();
96 torben 381
97     StationListAdapter adapter = null;
98 torben 258
99 torben 433 FavoritesMenu contextMenu = new FavoritesMenu();
100     IntSet favorites = new IntSet();
101 torben 481
102     WelcomeScreen.ListType listType;
103 torben 433 SharedPreferences prefs;
104 torben 1143
105 torben 381 ///////////////////////////////////////////////////////////////////////////////////////////
106     //Activity call backs
107    
108 torben 1078
109 torben 237 @Override
110     public void onCreate(Bundle savedInstanceState) {
111     super.onCreate(savedInstanceState);
112 torben 481 setContentView(R.layout.stationlist);
113 torben 237
114 torben 241
115 torben 237 adapter = new StationListAdapter(this);
116     setListAdapter(adapter);
117    
118 torben 433 ListView lv = getListView();
119     lv.setOnCreateContextMenuListener(contextMenu);
120    
121 torben 1143 locationLookup = new LocationLookup(this);
122 torben 433
123    
124     prefs = getSharedPreferences("TrainStation", 0);
125     String favoriteString = prefs.getString("favorites", "");
126     if (! favoriteString.equals("") ) {
127     favorites.fromString(favoriteString);
128     }
129    
130 torben 482 listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type");
131     setTitle();
132    
133 torben 731 isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false);
134    
135 torben 1028 ProviderFactory.purgeOldEntries(); //cleanup before fetching more data
136    
137 torben 258 if (savedInstanceState == null) {
138 torben 482
139    
140 torben 481 switch (listType) {
141     case ListNearest:
142 torben 1207 startNearestLookup();
143 torben 481 break;
144 torben 983 case ListSearch:
145 torben 1547 enableNamesearchField();
146 torben 481 break;
147     case ListFavorites:
148     startFavoriteLookup();
149     break;
150     default:
151     // Not possible !?!
152     }
153    
154 torben 258 } else {
155 torben 1066 stations = (StationBean) savedInstanceState.getSerializable("stations");
156 torben 260 adapter.setStations(stations);
157 torben 258 }
158 torben 482
159 torben 237 }
160 torben 918
161 torben 1027
162 torben 918
163 torben 1027
164    
165    
166     @Override
167 torben 918 protected void onDestroy() {
168     super.onDestroy();
169    
170 torben 1244 stationsFetched.removeMessages(0);
171 torben 1025
172 torben 1244
173 torben 919 if (locationLookup != null) {
174     locationLookup.stopSearch();
175     }
176 torben 1547
177 torben 918 }
178    
179    
180 torben 482 protected void setTitle() {
181 torben 561 String dialogTitle = getResources().getString(app_name);
182 torben 482 switch (listType) {
183     case ListNearest:
184 torben 561 dialogTitle += " - " + getString(stationlist_nearbystations);
185 torben 482 break;
186     case ListSearch:
187 torben 561 dialogTitle += " - " + getString(stationlist_search);
188 torben 482 break;
189     case ListFavorites:
190 torben 561 dialogTitle += " - " + getString(stationlist_favorites);
191 torben 482 break;
192     default:
193     dialogTitle = "";//not possible
194     }
195    
196     setTitle(dialogTitle);
197 torben 701
198 torben 482 }
199    
200 torben 1547
201     public void enableNamesearchField() {
202     final TextView stationSearch = (TextView) findViewById(R.id.stationsearch);
203     stationSearch.setVisibility( View.VISIBLE );
204     stationSearch.addTextChangedListener( new TextWatcher() {
205    
206     @Override
207     public void afterTextChanged(Editable s) {
208     if (s.length() > 0) {
209     startNameLookup( s.toString() );
210     } else {
211     stations = new StationBean();
212     getListView().invalidateViews();
213     adapter.setStations( stations );
214     }
215     }
216    
217     @Override
218     public void beforeTextChanged(CharSequence s, int start, int count, int after) {
219     }
220    
221     @Override
222     public void onTextChanged(CharSequence s, int start, int before, int count) {
223     }
224    
225     });
226 torben 701
227 torben 1547
228     }
229    
230 torben 371
231 torben 243 @Override
232     public void onSaveInstanceState(Bundle outState)
233     {
234 torben 258 if (dialog != null && dialog.isShowing())
235 torben 243 dialog.dismiss();
236 torben 1066 outState.putSerializable("stations", (StationBean) stations);
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 440 if (favorites.size() > 0) {
399 torben 1547
400     stations = stationProvider.lookupStationsByIds( favorites.toString() );
401     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 441 if (!favorites.contains(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     favorites.add(stationID);
523 torben 561 Toast.makeText(StationList.this, getString(stationlist_stationadded), Toast.LENGTH_SHORT).show();
524 torben 433 } else {
525 torben 473
526 torben 433 favorites.remove(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 torben 435 Editor ed = prefs.edit();
536     ed.putString("favorites", favorites.toString());
537     ed.commit();
538 torben 433 }
539     }
540 torben 237 }

  ViewVC Help
Powered by ViewVC 1.1.20