/[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 1576 - (hide annotations) (download)
Sun Jul 10 07:18:54 2011 UTC (12 years, 10 months ago) by torben
File size: 14899 byte(s)
The showing of the name search text field is not dependent on savedInstanceState
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 1066 stations = (StationBean) savedInstanceState.getSerializable("stations");
160 torben 260 adapter.setStations(stations);
161 torben 258 }
162 torben 482
163 torben 237 }
164 torben 918
165 torben 1027
166 torben 918
167 torben 1027
168    
169    
170     @Override
171 torben 918 protected void onDestroy() {
172     super.onDestroy();
173    
174 torben 1244 stationsFetched.removeMessages(0);
175 torben 1025
176 torben 1244
177 torben 919 if (locationLookup != null) {
178     locationLookup.stopSearch();
179     }
180 torben 1547
181 torben 918 }
182    
183    
184 torben 482 protected void setTitle() {
185 torben 561 String dialogTitle = getResources().getString(app_name);
186 torben 482 switch (listType) {
187     case ListNearest:
188 torben 561 dialogTitle += " - " + getString(stationlist_nearbystations);
189 torben 482 break;
190     case ListSearch:
191 torben 561 dialogTitle += " - " + getString(stationlist_search);
192 torben 482 break;
193     case ListFavorites:
194 torben 561 dialogTitle += " - " + getString(stationlist_favorites);
195 torben 482 break;
196     default:
197     dialogTitle = "";//not possible
198     }
199    
200     setTitle(dialogTitle);
201 torben 701
202 torben 482 }
203    
204 torben 1547
205     public void enableNamesearchField() {
206     final TextView stationSearch = (TextView) findViewById(R.id.stationsearch);
207     stationSearch.setVisibility( View.VISIBLE );
208     stationSearch.addTextChangedListener( new TextWatcher() {
209    
210     @Override
211     public void afterTextChanged(Editable s) {
212     if (s.length() > 0) {
213     startNameLookup( s.toString() );
214     } else {
215     stations = new StationBean();
216     getListView().invalidateViews();
217     adapter.setStations( stations );
218     }
219     }
220    
221     @Override
222     public void beforeTextChanged(CharSequence s, int start, int count, int after) {
223     }
224    
225     @Override
226     public void onTextChanged(CharSequence s, int start, int before, int count) {
227     }
228    
229     });
230 torben 701
231 torben 1547
232     }
233    
234 torben 371
235 torben 243 @Override
236     public void onSaveInstanceState(Bundle outState)
237     {
238 torben 258 if (dialog != null && dialog.isShowing())
239 torben 243 dialog.dismiss();
240 torben 1066 outState.putSerializable("stations", (StationBean) stations);
241 torben 482
242 torben 243 }
243 torben 237
244 torben 243
245 torben 237
246     @Override
247 torben 368 public boolean onCreateOptionsMenu(Menu menu) {
248 torben 419 MenuItem item;
249 torben 561
250     item = menu.add(0, OPTIONS_MAP, 0, getString(stationlist_stationmap));
251 torben 419 item.setIcon(android.R.drawable.ic_menu_mapmode);
252    
253 torben 561 item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo));
254 torben 1448 item.setIcon(android.R.drawable.ic_menu_info_details);
255 torben 1447 boolean hasLoc = (locationLookup.getLocation() != null);
256     item.setEnabled(hasLoc);
257 torben 481
258 torben 368 return true;
259     }
260 torben 1447
261 torben 368
262 torben 1447
263    
264 torben 368 @Override
265     public boolean onOptionsItemSelected(MenuItem item) {
266 torben 381 boolean retval = true;
267    
268 torben 1453
269 torben 368 switch (item.getItemId()) {
270     case OPTIONS_MAP:
271    
272 torben 1485 if ( stations == null || stations.entries == null || stations.entries.size() == 0 ) {
273 torben 1486 Toast.makeText(this, "No stations to show on map", Toast.LENGTH_SHORT).show(); //TODO: Translate
274 torben 1485 return true;
275     }
276    
277 torben 368 Intent intent = new Intent(this,StationMapView.class);
278    
279     ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();
280 torben 1066 for (StationEntry st : stations.entries ) {
281 torben 369 stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) );
282 torben 368 }
283    
284     intent.putExtra("stations", stationPoints);
285    
286     startActivity(intent);
287     break;
288 torben 481 case OPTIONS_GPSINFO:
289 torben 482 Location loc = locationLookup.getLocation();
290 torben 371 StringBuffer message = new StringBuffer();
291 torben 561 message.append( getString(stationlist_locationinfo) ).append(":\n");
292 torben 1447
293     message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n");
294     message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n");
295     message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n");
296     message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n");
297    
298 torben 556
299 torben 906 MessageBox.showMessage(this, message.toString(), false);
300 torben 371 break;
301 torben 368 default:
302     retval = super.onOptionsItemSelected(item);
303     }
304    
305     return retval;
306     }
307 torben 433
308    
309 torben 368
310     @Override
311 torben 433 public boolean onContextItemSelected(MenuItem item) {
312     contextMenu.onContextItemSelected(item);
313     return true;
314    
315    
316     }
317 torben 556
318     public void showMessageAndClose(String message) {
319     AlertDialog.Builder builder = new AlertDialog.Builder(this);
320     builder.setMessage(message)
321     .setCancelable(false)
322     .setPositiveButton("OK", new DialogInterface.OnClickListener() {
323     public void onClick(DialogInterface dialog, int id) {
324     dialog.dismiss();
325     StationList.this.finish();
326     }
327     })
328     .show();
329     }
330 torben 433
331    
332    
333    
334     @Override
335 torben 237 protected Dialog onCreateDialog(int id) {
336     switch (id) {
337     case DLG_PROGRESS:
338     ProgressDialog dlg = new ProgressDialog(this);
339 torben 561 dlg.setMessage( getString(stationlist_waitforlocation) );
340 torben 237 dlg.setCancelable(false);
341 torben 336 return dlg;
342 torben 381
343 torben 237 default:
344     return super.onCreateDialog(id);
345     }
346    
347     }
348    
349    
350     @Override
351     protected void onPrepareDialog(int id, Dialog dialog) {
352     super.onPrepareDialog(id, dialog);
353     switch (id) {
354     case DLG_PROGRESS:
355     this.dialog = (ProgressDialog) dialog;
356 torben 336 if (!dialogMessage.equals("")) {
357     this.dialog.setMessage(dialogMessage);
358     dialogMessage = "";
359     }
360 torben 237 break;
361     }
362     }
363 torben 381
364     @Override
365     protected void onListItemClick(ListView l, View v, int position, long id) {
366     super.onListItemClick(l, v, position, id);
367    
368 torben 1066 StationEntry station = stations.entries.get(position);
369 torben 381
370 torben 731 if (isLaunchedforShortcut == true) {
371     Intent i = new Intent();
372     i.putExtra("station", station);
373     setResult(Activity.RESULT_OK, i);
374     finish();
375     } else {
376     Intent intent = new Intent(this, DepartureList.class);
377     intent.putExtra("stationbean", station);
378     startActivity(intent);
379     }
380 torben 381 }
381    
382     /////////////////////////////////////////////////////////////
383     //
384    
385 torben 1207 public void startNearestLookup() {
386 torben 561 dialogMessage = getString( stationlist_waitforlocation );
387 torben 1008 showDialog(DLG_PROGRESS);
388 torben 237
389 torben 482 locationLookup.locateStations();
390 torben 1143 stationsFetched.sendEmptyMessageDelayed(0, 500);
391 torben 237 }
392 torben 381
393 torben 1207 void startNameLookup(String name) {
394 torben 381
395 torben 1547 stations = stationProvider.lookupStationsByName(name);
396     getListView().invalidateViews();
397     adapter.setStations( stations );
398 torben 381 }
399 torben 433
400     public void startFavoriteLookup() {
401    
402 torben 440 if (favorites.size() > 0) {
403 torben 1547
404     stations = stationProvider.lookupStationsByIds( favorites.toString() );
405     getListView().invalidateViews();
406     adapter.setStations( stations );
407    
408    
409 torben 433 } else {
410 torben 561 showMessageAndClose( getString( stationlist_nofavorites ) );
411 torben 433 }
412     }
413 torben 381
414 torben 433
415 torben 381
416 torben 1547 void startNearestLookupPhase2()
417     {
418     stations = stationProvider.lookupStationsByLocation( locationLookup.getLocation() );
419     getListView().invalidateViews();
420     adapter.setStations( stations );
421 torben 381 }
422    
423    
424     ////////////////////////////////////////////////////////////////////////////
425     // Inner classes
426    
427     class StationsFetchedHandler extends Handler {
428 torben 237 @Override
429     public void handleMessage(Message msg) {
430 torben 336
431 torben 1143 LocationLookup.LookupStates state = locationLookup.getState();
432    
433    
434 torben 1142 switch (state) {
435 torben 237 case GOTLOCATION:
436 torben 1008 dismissDialog(DLG_PROGRESS);
437 torben 1143
438 torben 1547 startNearestLookupPhase2();
439 torben 319
440 torben 1143 return;
441    
442 torben 237 case NOPROVIDER:
443 torben 1008 dismissDialog(DLG_PROGRESS);
444 torben 906 MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true );
445     //StationList.this.finish();
446 torben 1143 return;
447 torben 1220 case IDLE:
448     Log.e("TrainInfo", "How did this happen ???");
449     dismissDialog(DLG_PROGRESS); // how did we get here ??
450     return;
451    
452 torben 1143 }
453 torben 336
454 torben 1143
455    
456     if (locationLookup.elapsedTime() >= GPS_TIMEOUT_MS) {
457 torben 1245 dismissDialog(DLG_PROGRESS);
458 torben 1143
459 torben 1245
460 torben 1143 locationLookup.stopSearch();
461    
462     if (locationLookup.hasLocation()) {
463 torben 1547 startNearestLookupPhase2();
464 torben 1143 } else {
465     AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);
466     builder.setMessage( getString( stationlist_gpstimeout) );
467     builder.setCancelable(true);
468     builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
469     public void onClick(DialogInterface dialog, int id) {
470     dialog.dismiss();
471 torben 1207 startNearestLookup();
472 torben 1143
473     }
474     });
475     builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() {
476     public void onClick(DialogInterface dialog, int id) {
477     dialog.dismiss();
478 torben 1150 StationList.this.finish(); // Close this Activity
479 torben 1143 }
480     });
481     builder.show();
482    
483 torben 237 }
484 torben 1143 } else {
485     if (locationLookup.hasGps()) {
486     int count = locationLookup.getSatCount();
487     String dialogMessage = getString( stationlist_waitforlocation ) + "\n" + getString( stationlist_satellitecount ) + ": " + count;
488     dialog.setMessage( dialogMessage );
489     }
490     this.sendEmptyMessageDelayed(0, 500);
491 torben 237 }
492 torben 1143
493 torben 237 }
494 torben 1143 }
495 torben 381
496 torben 1143
497    
498 torben 237
499 torben 433 class FavoritesMenu implements OnCreateContextMenuListener {
500     private final static int FAVORITES_ADD = 9001;
501     private final static int FAVORITES_REMOVE = 9002;
502    
503     private int selectedPosition;
504    
505    
506     @Override
507     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
508    
509     AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
510     selectedPosition = info.position;
511 torben 1066 int stationID = stations.entries.get(selectedPosition).getId();
512 torben 433
513 torben 441 if (!favorites.contains(stationID)) {
514 torben 561 menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) );
515 torben 433 } else {
516 torben 563 menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_removefavorite) );
517 torben 433 }
518    
519     }
520    
521     public void onContextItemSelected(MenuItem item) {
522 torben 1066 StationEntry sb = stations.entries.get(selectedPosition);
523 torben 433
524     int stationID = sb.getId();
525     if (item.getItemId() == FAVORITES_ADD) {
526     favorites.add(stationID);
527 torben 561 Toast.makeText(StationList.this, getString(stationlist_stationadded), Toast.LENGTH_SHORT).show();
528 torben 433 } else {
529 torben 473
530 torben 433 favorites.remove(stationID);
531 torben 561 Toast.makeText(StationList.this, getString(stationlist_stationremoved), Toast.LENGTH_SHORT).show();
532 torben 473
533 torben 481
534     if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) {
535 torben 1066 stations.entries.remove(selectedPosition);
536 torben 473 adapter.notifyDataSetChanged();
537     }
538 torben 433 }
539 torben 435 Editor ed = prefs.edit();
540     ed.putString("favorites", favorites.toString());
541     ed.commit();
542 torben 433 }
543     }
544 torben 237 }

  ViewVC Help
Powered by ViewVC 1.1.20