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

  ViewVC Help
Powered by ViewVC 1.1.20