/[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 579 - (hide annotations) (download)
Tue Feb 2 08:52:45 2010 UTC (14 years, 3 months ago) by torben
File size: 15118 byte(s)
We only have a location on a searchByLocation() - so this failover wouldn't work anyways
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 336 import android.app.AlertDialog;
8 torben 237 import android.app.Dialog;
9     import android.app.ListActivity;
10     import android.app.ProgressDialog;
11 torben 336 import android.content.DialogInterface;
12 torben 237 import android.content.Intent;
13 torben 433 import android.content.SharedPreferences;
14 torben 435 import android.content.SharedPreferences.Editor;
15 torben 319 import android.location.Location;
16 torben 241 import android.os.AsyncTask;
17 torben 237 import android.os.Bundle;
18     import android.os.Handler;
19     import android.os.Message;
20 torben 493
21 torben 433 import android.view.ContextMenu;
22 torben 381 import android.view.LayoutInflater;
23 torben 368 import android.view.Menu;
24     import android.view.MenuItem;
25 torben 237 import android.view.View;
26 torben 433 import android.view.ContextMenu.ContextMenuInfo;
27     import android.view.View.OnCreateContextMenuListener;
28     import android.widget.AdapterView;
29 torben 381 import android.widget.EditText;
30 torben 237 import android.widget.ListView;
31 torben 433 import android.widget.Toast;
32 torben 319 import dk.thoerup.traininfo.provider.ProviderFactory;
33     import dk.thoerup.traininfo.provider.StationProvider;
34 torben 368 import dk.thoerup.traininfo.stationmap.GeoPair;
35     import dk.thoerup.traininfo.stationmap.StationMapView;
36 torben 433 import dk.thoerup.traininfo.util.IntSet;
37 torben 245 import dk.thoerup.traininfo.util.MessageBox;
38 torben 237
39 torben 561 import static dk.thoerup.traininfo.R.string.*;
40    
41 torben 336 public class StationList extends ListActivity {
42     public static final int GOTLOCATION = 1001;
43     public static final int GOTSTATIONLIST = 1002;
44     public static final int NOPROVIDER = 1003;
45     public static final int LOCATIONFIXTIMEOUT = 1004;
46 torben 368
47 torben 381 public static final int OPTIONS_MAP = 2003;
48 torben 481 public static final int OPTIONS_GPSINFO = 2004;
49    
50 torben 433
51 torben 336
52 torben 237
53 torben 381 public static final int DLG_PROGRESS = 3001;
54     public static final int DLG_STATIONNAME = 3002;
55 torben 237
56 torben 440 static enum LookupMethod {
57     ByLocation,
58     ByName,
59     ByList,
60     MethodNone
61     }
62    
63    
64 torben 336 String dialogMessage = "";
65 torben 237 ProgressDialog dialog;
66 torben 482 LocationLookup locationLookup = null;
67 torben 440 FindStationsTask findStationsTask;
68 torben 381 StationsFetchedHandler stationsFetched = new StationsFetchedHandler();
69 torben 237
70 torben 374 GeoPair location = new GeoPair();
71    
72 torben 258 boolean isRunning = false;
73     List<StationBean> stations = new ArrayList<StationBean>();
74    
75 torben 319 StationProvider stationProvider = ProviderFactory.getStationProvider();
76 torben 381
77     StationListAdapter adapter = null;
78 torben 258
79 torben 433 FavoritesMenu contextMenu = new FavoritesMenu();
80     IntSet favorites = new IntSet();
81 torben 481
82     WelcomeScreen.ListType listType;
83 torben 433 SharedPreferences prefs;
84    
85 torben 381 ///////////////////////////////////////////////////////////////////////////////////////////
86     //Activity call backs
87    
88 torben 258 @SuppressWarnings("unchecked")
89 torben 237 @Override
90     public void onCreate(Bundle savedInstanceState) {
91     super.onCreate(savedInstanceState);
92 torben 481 setContentView(R.layout.stationlist);
93 torben 237
94 torben 241
95 torben 237 adapter = new StationListAdapter(this);
96     setListAdapter(adapter);
97    
98 torben 433 ListView lv = getListView();
99     lv.setOnCreateContextMenuListener(contextMenu);
100    
101 torben 482 locationLookup = new LocationLookup(this, stationsFetched);
102 torben 433
103    
104     prefs = getSharedPreferences("TrainStation", 0);
105     String favoriteString = prefs.getString("favorites", "");
106     if (! favoriteString.equals("") ) {
107     favorites.fromString(favoriteString);
108     }
109    
110 torben 482 listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type");
111     setTitle();
112    
113 torben 258 if (savedInstanceState == null) {
114 torben 482
115    
116 torben 481 switch (listType) {
117     case ListNearest:
118     startLookup();
119     break;
120     case ListSearch:
121     this.showDialog(DLG_STATIONNAME);
122     break;
123     case ListFavorites:
124     startFavoriteLookup();
125     break;
126     default:
127     // Not possible !?!
128     }
129    
130 torben 258 } else {
131     stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations");
132 torben 260 adapter.setStations(stations);
133 torben 374 location = (GeoPair) savedInstanceState.getSerializable("location");
134 torben 258 }
135 torben 482
136 torben 237 }
137 torben 482 protected void setTitle() {
138 torben 561 String dialogTitle = getResources().getString(app_name);
139 torben 482 switch (listType) {
140     case ListNearest:
141 torben 561 dialogTitle += " - " + getString(stationlist_nearbystations);
142 torben 482 break;
143     case ListSearch:
144 torben 561 dialogTitle += " - " + getString(stationlist_search);
145 torben 482 break;
146     case ListFavorites:
147 torben 561 dialogTitle += " - " + getString(stationlist_favorites);
148 torben 482 break;
149     default:
150     dialogTitle = "";//not possible
151     }
152    
153     setTitle(dialogTitle);
154     }
155    
156    
157 torben 371
158    
159 torben 243 @Override
160     public void onSaveInstanceState(Bundle outState)
161     {
162 torben 258 if (dialog != null && dialog.isShowing())
163 torben 243 dialog.dismiss();
164 torben 258 outState.putSerializable("stations", (ArrayList<StationBean>) stations);
165 torben 374 outState.putSerializable("location", location);
166 torben 482
167 torben 243 }
168 torben 237
169 torben 243
170 torben 237
171     @Override
172 torben 368 public boolean onCreateOptionsMenu(Menu menu) {
173 torben 419 MenuItem item;
174 torben 561
175     item = menu.add(0, OPTIONS_MAP, 0, getString(stationlist_stationmap));
176 torben 419 item.setIcon(android.R.drawable.ic_menu_mapmode);
177    
178 torben 561 item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo));
179 torben 481 item.setIcon(android.R.drawable.ic_menu_mapmode);
180    
181 torben 368 return true;
182     }
183    
184     @Override
185     public boolean onOptionsItemSelected(MenuItem item) {
186 torben 381 boolean retval = true;
187    
188 torben 481 //TODO: Cleanup
189 torben 368 switch (item.getItemId()) {
190     case OPTIONS_MAP:
191    
192     Intent intent = new Intent(this,StationMapView.class);
193    
194     ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();
195     for (StationBean st : stations ) {
196 torben 369 stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) );
197 torben 368 }
198    
199     intent.putExtra("stations", stationPoints);
200    
201     startActivity(intent);
202     break;
203 torben 481 case OPTIONS_GPSINFO:
204 torben 482 Location loc = locationLookup.getLocation();
205 torben 371 StringBuffer message = new StringBuffer();
206 torben 561 message.append( getString(stationlist_locationinfo) ).append(":\n");
207 torben 554 if (loc != null) {
208 torben 561 message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n");
209     message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n");
210 torben 564 message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n");
211     message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n");
212 torben 554 } else {
213 torben 561 message.append( getString(stationlist_nolocation) );
214 torben 556 }
215    
216 torben 371 MessageBox.showMessage(this, message.toString());
217     break;
218 torben 368 default:
219     retval = super.onOptionsItemSelected(item);
220     }
221    
222     return retval;
223     }
224 torben 433
225    
226 torben 368
227     @Override
228 torben 433 public boolean onContextItemSelected(MenuItem item) {
229     contextMenu.onContextItemSelected(item);
230     return true;
231    
232    
233     }
234 torben 556
235     public void showMessageAndClose(String message) {
236     AlertDialog.Builder builder = new AlertDialog.Builder(this);
237     builder.setMessage(message)
238     .setCancelable(false)
239     .setPositiveButton("OK", new DialogInterface.OnClickListener() {
240     public void onClick(DialogInterface dialog, int id) {
241     dialog.dismiss();
242     StationList.this.finish();
243     }
244     })
245     .show();
246     }
247 torben 433
248    
249    
250    
251     @Override
252 torben 237 protected Dialog onCreateDialog(int id) {
253     switch (id) {
254     case DLG_PROGRESS:
255     ProgressDialog dlg = new ProgressDialog(this);
256 torben 561 dlg.setMessage( getString(stationlist_waitforlocation) );
257 torben 237 dlg.setCancelable(false);
258 torben 336 return dlg;
259 torben 381 case DLG_STATIONNAME:
260     LayoutInflater factory = LayoutInflater.from(this);
261     final View rootView = factory.inflate(R.layout.textinput, null);
262    
263    
264     AlertDialog.Builder builder = new AlertDialog.Builder(this);
265    
266 torben 561 builder.setTitle( getString(stationlist_stationsearch) );
267 torben 381 builder.setView(rootView);
268     builder.setCancelable(true);
269 torben 561 builder.setPositiveButton( getString(generic_search), new DialogInterface.OnClickListener() {
270 torben 381 public void onClick(DialogInterface dialog, int which) {
271     EditText et = (EditText) rootView.findViewById(R.id.EditText);
272     dialog.dismiss();
273     if (et.getText().toString().length() >= 2) {
274     startNameSearch(et.getText().toString());
275     } else {
276 torben 561 showMessageAndClose( getString(stationlist_twocharmin) );
277 torben 381 }
278     }
279     });
280 torben 561 builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
281 torben 381 public void onClick(DialogInterface dialog, int which) {
282     dialog.dismiss();
283 torben 555 StationList.this.finish(); // Close this Activity
284 torben 381 }
285     });
286     return builder.create();
287    
288 torben 237 default:
289     return super.onCreateDialog(id);
290     }
291    
292     }
293    
294    
295     @Override
296     protected void onPrepareDialog(int id, Dialog dialog) {
297     super.onPrepareDialog(id, dialog);
298     switch (id) {
299     case DLG_PROGRESS:
300     this.dialog = (ProgressDialog) dialog;
301 torben 336 if (!dialogMessage.equals("")) {
302     this.dialog.setMessage(dialogMessage);
303     dialogMessage = "";
304     }
305 torben 237 break;
306     }
307     }
308 torben 381
309     @Override
310     protected void onListItemClick(ListView l, View v, int position, long id) {
311     super.onListItemClick(l, v, position, id);
312    
313     StationBean station = stations.get(position);
314    
315     Intent intent = new Intent(this, DepartureList.class);
316 torben 557 intent.putExtra("stationbean", station);
317 torben 381 startActivity(intent);
318     }
319    
320     /////////////////////////////////////////////////////////////
321     //
322    
323 torben 237 public void startLookup() {
324 torben 561 isRunning = true;
325     dialogMessage = getString( stationlist_waitforlocation );
326 torben 237 showDialog(DLG_PROGRESS);
327    
328 torben 482 locationLookup.locateStations();
329 torben 336 stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);
330 torben 237 }
331 torben 381
332     void startNameSearch(String name) {
333 torben 561 dialogMessage = getString( stationlist_findbyname );
334 torben 381 showDialog(DLG_PROGRESS);
335 torben 237
336 torben 440 findStationsTask = new FindStationsTask();
337 torben 579 findStationsTask.searchByName(name);
338 torben 440 findStationsTask.execute();
339 torben 381
340     }
341 torben 433
342     public void startFavoriteLookup() {
343    
344 torben 440 if (favorites.size() > 0) {
345 torben 561 dialogMessage = getString( stationlist_loadfavorites );
346 torben 433 showDialog(DLG_PROGRESS);
347 torben 237
348 torben 440 findStationsTask = new FindStationsTask();
349 torben 579 findStationsTask.searchByIds( favorites.toString() );
350 torben 440 findStationsTask.execute();
351 torben 433 } else {
352 torben 561 showMessageAndClose( getString( stationlist_nofavorites ) );
353 torben 433 }
354     }
355 torben 381
356 torben 433
357 torben 381
358     void startLocatorTask()
359     {
360 torben 561 dialogMessage = getString( stationlist_findingnearby );
361 torben 381 showDialog(DLG_PROGRESS);
362    
363 torben 440 findStationsTask = new FindStationsTask();
364 torben 482 findStationsTask.searchByLocation( locationLookup.getLocation() );
365 torben 440 findStationsTask.execute();
366 torben 381 }
367    
368    
369     ////////////////////////////////////////////////////////////////////////////
370     // Inner classes
371    
372     class StationsFetchedHandler extends Handler {
373 torben 237 @Override
374     public void handleMessage(Message msg) {
375 torben 336
376 torben 237 switch (msg.what) {
377     case GOTLOCATION:
378 torben 336 dismissDialog(DLG_PROGRESS);
379    
380     startLocatorTask();
381 torben 482 location = GeoPair.fromLocation( locationLookup.getLocation() );
382 torben 336
383 torben 237 break;
384 torben 319
385 torben 237 case NOPROVIDER:
386 torben 336 dismissDialog(DLG_PROGRESS);
387 torben 561 MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) );
388 torben 237 break;
389 torben 336 case LOCATIONFIXTIMEOUT:
390 torben 237 if (isRunning) {
391 torben 482 locationLookup.stopSearch();
392     if (locationLookup.hasLocation()) {
393 torben 336 stationsFetched.sendEmptyMessage( GOTLOCATION );
394     } else {
395     dismissDialog(DLG_PROGRESS);
396    
397     AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);
398 torben 561 builder.setMessage( getString( stationlist_gpstimeout) );
399 torben 336 builder.setCancelable(true);
400 torben 561 builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
401 torben 336 public void onClick(DialogInterface dialog, int id) {
402     dialog.dismiss();
403     startLookup();
404    
405     }
406     });
407 torben 561 builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() {
408 torben 336 public void onClick(DialogInterface dialog, int id) {
409     dialog.dismiss();
410     }
411     });
412     builder.show();
413    
414 torben 285 }
415 torben 237 }
416     break;
417     }
418     isRunning = false;
419     }
420     };
421 torben 381
422 torben 237
423 torben 440 class FindStationsTask extends AsyncTask<Void,Void,Void> {
424 torben 336
425 torben 381 LookupMethod method = LookupMethod.MethodNone;
426     boolean success;
427     String name;
428     Location loc;
429 torben 433 String ids;
430 torben 237
431 torben 579 public void searchByName(String n) {
432 torben 317
433 torben 381 method = LookupMethod.ByName;
434     name = n;
435 torben 317 }
436    
437 torben 381 public void searchByLocation(Location l) {
438     method = LookupMethod.ByLocation;
439     loc = l;
440     }
441    
442 torben 579 public void searchByIds(String id) {
443 torben 433
444     method = LookupMethod.ByList;
445     ids = id;
446     }
447    
448 torben 241 @Override
449     protected void onPreExecute() {
450 torben 258
451 torben 381 if (method.equals(LookupMethod.MethodNone))
452     throw new RuntimeException("Method not set");
453 torben 241 super.onPreExecute();
454     }
455    
456     @Override
457     protected Void doInBackground(Void... params) {
458 torben 473
459     switch (method) {
460     case ByLocation:
461 torben 381 success = stationProvider.lookupStations(loc);
462 torben 473 break;
463     case ByName:
464 torben 433 success = stationProvider.lookupStationsByName(name);
465 torben 473 break;
466     case ByList:
467 torben 433 success = stationProvider.lookupStationsByIds(ids);
468 torben 473 break;
469     default:
470     success = false; // not possible
471     }
472 torben 433
473 torben 473
474 torben 241 return null;
475     }
476    
477     @Override
478     protected void onPostExecute(Void result) {
479     super.onPostExecute(result);
480 torben 319 dialog.dismiss();
481    
482 torben 473
483 torben 319 if (success) {
484 torben 566 if (stationProvider.getStations().size() == 0) {
485     showMessageAndClose(getString(stationlist_nostations));
486     }
487 torben 319 stations = stationProvider.getStations();
488 torben 481 adapter.setStations( stations );
489 torben 319
490     } else { //communication or parse errors
491 torben 336 AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);
492 torben 561 builder.setMessage(getString(stationlist_nearbyerror));
493 torben 336 builder.setCancelable(true);
494 torben 561 builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
495 torben 336 public void onClick(DialogInterface dialog, int id) {
496     dialog.dismiss();
497    
498     stationsFetched.post( new Runnable() {
499     @Override
500     public void run() {
501     startLocatorTask();
502     }
503     });
504     }
505     });
506 torben 561 builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
507 torben 336 public void onClick(DialogInterface dialog, int id) {
508     dialog.dismiss();
509     }
510     });
511     builder.show();
512 torben 319 }
513 torben 241 }
514     }
515 torben 433
516    
517     class FavoritesMenu implements OnCreateContextMenuListener {
518     private final static int FAVORITES_ADD = 9001;
519     private final static int FAVORITES_REMOVE = 9002;
520    
521     private int selectedPosition;
522    
523    
524     @Override
525     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
526    
527     AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
528     selectedPosition = info.position;
529     int stationID = stations.get(selectedPosition).getId();
530    
531 torben 441 if (!favorites.contains(stationID)) {
532 torben 561 menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) );
533 torben 433 } else {
534 torben 563 menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_removefavorite) );
535 torben 433 }
536    
537     }
538    
539     public void onContextItemSelected(MenuItem item) {
540     StationBean sb = stations.get(selectedPosition);
541    
542     int stationID = sb.getId();
543     if (item.getItemId() == FAVORITES_ADD) {
544     favorites.add(stationID);
545 torben 561 Toast.makeText(StationList.this, getString(stationlist_stationadded), Toast.LENGTH_SHORT).show();
546 torben 433 } else {
547 torben 473
548 torben 433 favorites.remove(stationID);
549 torben 561 Toast.makeText(StationList.this, getString(stationlist_stationremoved), Toast.LENGTH_SHORT).show();
550 torben 473
551 torben 481
552     if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) {
553 torben 473 stations.remove(selectedPosition);
554     adapter.notifyDataSetChanged();
555     }
556 torben 433 }
557 torben 435 Editor ed = prefs.edit();
558     ed.putString("favorites", favorites.toString());
559     ed.commit();
560 torben 433 }
561     }
562 torben 237 }

  ViewVC Help
Powered by ViewVC 1.1.20