/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java
ViewVC logotype

Diff of /android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1262 by torben, Tue Apr 5 05:31:32 2011 UTC revision 1798 by torben, Wed Apr 18 17:38:54 2012 UTC
# Line 11  import android.app.AlertDialog; Line 11  import android.app.AlertDialog;
11  import android.app.Dialog;  import android.app.Dialog;
12  import android.app.ListActivity;  import android.app.ListActivity;
13  import android.app.ProgressDialog;  import android.app.ProgressDialog;
14    import android.content.ActivityNotFoundException;
15  import android.content.DialogInterface;  import android.content.DialogInterface;
16  import android.content.Intent;  import android.content.Intent;
17    import android.content.SharedPreferences;
18  import android.graphics.Typeface;  import android.graphics.Typeface;
19  import android.net.Uri;  import android.net.Uri;
20  import android.os.AsyncTask;  import android.os.AsyncTask;
21  import android.os.Bundle;  import android.os.Bundle;
22    import android.preference.PreferenceManager;
23  import android.util.Log;  import android.util.Log;
24  import android.view.Menu;  import android.view.Menu;
25  import android.view.MenuItem;  import android.view.MenuItem;
# Line 27  import android.widget.ListView; Line 30  import android.widget.ListView;
30  import android.widget.TableLayout;  import android.widget.TableLayout;
31  import android.widget.TableRow;  import android.widget.TableRow;
32  import android.widget.TextView;  import android.widget.TextView;
33    import android.widget.Toast;
34  import dk.thoerup.android.traininfo.common.DepartureBean;  import dk.thoerup.android.traininfo.common.DepartureBean;
35  import dk.thoerup.android.traininfo.common.DepartureEntry;  import dk.thoerup.android.traininfo.common.DepartureEntry;
36  import dk.thoerup.android.traininfo.common.MetroBean;  import dk.thoerup.android.traininfo.common.MetroBean;
37  import dk.thoerup.android.traininfo.common.MetroBean.MetroEntry;  import dk.thoerup.android.traininfo.common.MetroBean.MetroEntry;
38  import dk.thoerup.android.traininfo.common.StationBean.StationEntry;  import dk.thoerup.android.traininfo.common.StationEntry;
39  import dk.thoerup.traininfo.provider.DepartureProvider;  import dk.thoerup.traininfo.provider.DepartureProvider;
40  import dk.thoerup.traininfo.provider.MetroProvider;  import dk.thoerup.traininfo.provider.MetroProvider;
41  import dk.thoerup.traininfo.provider.ProviderFactory;  import dk.thoerup.traininfo.provider.ProviderFactory;
42    
43    import dk.thoerup.traininfo.util.FavoritesHelper;
44  import dk.thoerup.traininfo.util.MessageBox;  import dk.thoerup.traininfo.util.MessageBox;
45    import dk.thoerup.traininfo.util.StationEntryCsv;
46    
47  public class DepartureList extends ListActivity {  public class DepartureList extends ListActivity {
48    
49          public static final int DLG_PROGRESS = 1;          public static final int DLG_PROGRESS = 1;
50          static final int MENU_MAP = 100;          static final int MENU_MAP = 100;
51          static final int MENU_NOTIFICATIONS = 101;          static final int MENU_NOTIFICATIONS = 101;
52          static final int MENU_METROMAP= 102;          static final int MENU_METROMAP = 102;
53            static final int MENU_TOGGLEDETAILS= 103;
54    
55            static final int MENU_FAVORITES_ADD = 104;
56            static final int MENU_FAVORITES_REMOVE = 105;
57                    
58                    
59          DepartureListAdapter adapter;          DepartureListAdapter adapter;
# Line 53  public class DepartureList extends ListA Line 64  public class DepartureList extends ListA
64          MetroProvider metro;          MetroProvider metro;
65                    
66          int selectedItemId;          int selectedItemId;
67    
68            FavoritesHelper favorites;
69    
70          //DepartureBean currentDeparture;          //DepartureBean currentDeparture;
71                    
72          ProgressDialog pgDialog;          ProgressDialog pgDialog;
# Line 64  public class DepartureList extends ListA Line 78  public class DepartureList extends ListA
78                    
79          String trainType = "REGIONAL";          String trainType = "REGIONAL";
80                    
81          boolean arrival = false;          boolean arrival = false;        
82    
83          int commFailCounter = 0;          int commFailCounter = 0;
84    
# Line 72  public class DepartureList extends ListA Line 86  public class DepartureList extends ListA
86          protected void onCreate(Bundle savedInstanceState) {          protected void onCreate(Bundle savedInstanceState) {
87                  super.onCreate(savedInstanceState);                  super.onCreate(savedInstanceState);
88                  setContentView(R.layout.departurelist);                  setContentView(R.layout.departurelist);
89                            
90                    favorites = new FavoritesHelper(this);
91            
92                  adapter = new DepartureListAdapter(this);                  adapter = new DepartureListAdapter(this);
93                  setListAdapter(adapter);                  setListAdapter(adapter);
94                                                    
95                  Intent launchedBy = getIntent();                  Intent launchedBy = getIntent();
96                    
97                  station = (StationEntry) launchedBy.getSerializableExtra("stationbean");                  station = (StationEntry) launchedBy.getSerializableExtra("stationbean");
# Line 190  public class DepartureList extends ListA Line 206  public class DepartureList extends ListA
206                    
207                  ProviderFactory.purgeOldEntries(); //cleanup before fetching more data                  ProviderFactory.purgeOldEntries(); //cleanup before fetching more data
208                                    
209                  Log.e("Station", station.toCSV() );                  Log.e("Station", StationEntryCsv.toCSV(station) );
210                    
211                    
212                                    
213                  if (station.isMetro() == false) {                  if (station.isMetro() == false) {
214                          metroBtn.setVisibility( View.GONE );                          metroBtn.setVisibility( View.GONE );
# Line 220  public class DepartureList extends ListA Line 238  public class DepartureList extends ListA
238                          trainType = "STOG";                          trainType = "STOG";
239                                                    
240                  }                  }
241                    //Both enabled - use preferred from preferences
242                    if (station.isRegional() == true && station.isStrain() == true ) {
243                            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
244                            trainType = prefs.getString("traintype", "REGIONAL"); //default value is gps    
245                            
246                            if (trainType.equals("STOG") ) {
247                                    stogBtn.setBackgroundResource(R.drawable.custom_button_hilight);
248                                    regionalBtn.setBackgroundResource(R.drawable.custom_button);
249                            }
250                    }
251                                                                    
252                                    
253                  if (station.isRegional() == false && station.isStrain() == false) {                  if (station.isRegional() == false && station.isStrain() == false) {
# Line 236  public class DepartureList extends ListA Line 264  public class DepartureList extends ListA
264                                  startMetroFetcher();                                  startMetroFetcher();
265                          } else {                          } else {
266                                  metroBean = (MetroBean) savedInstanceState.getSerializable("metro");                                  metroBean = (MetroBean) savedInstanceState.getSerializable("metro");
267                                  loadMetroData();                                  if (metroBean != null) { // Avoid reported NPE
268                                            loadMetroData();
269                                    } else {
270                                            startMetroFetcher();    
271                                    }
272                          }                          }
273                                                    
274                  } else {                  } else {
# Line 260  public class DepartureList extends ListA Line 292  public class DepartureList extends ListA
292                  }                  }
293          }          }
294                    
295            
296            
297          boolean hasNotifications() {          boolean hasNotifications() {
298                  return (departures != null && departures.notifications.size() > 0);                  return (departures != null && departures.notifications.size() > 0);
299          }          }
# Line 295  public class DepartureList extends ListA Line 329  public class DepartureList extends ListA
329          protected void onListItemClick(ListView l, View v, int position, long id) {          protected void onListItemClick(ListView l, View v, int position, long id) {
330                  super.onListItemClick(l, v, position, id);                  super.onListItemClick(l, v, position, id);
331                                    
332                    //how can this happen ??
333                    if (departures == null || departures.entries == null || departures.entries.size() == 0) {
334                            Toast.makeText(this, "No departures in list ?!?", Toast.LENGTH_LONG).show(); //TODO: translate
335                            return;
336                    }
337                    
338                  selectedItemId = position;                  selectedItemId = position;
339                                    
340                  DepartureEntry dep = departures.entries.get(selectedItemId);                  DepartureEntry dep = departures.entries.get(selectedItemId);
# Line 335  public class DepartureList extends ListA Line 375  public class DepartureList extends ListA
375                    
376                    
377    
378            @Override
379            public boolean onPrepareOptionsMenu(Menu menu) {                
380                    super.onPrepareOptionsMenu(menu);
381    
382                    ///////////////////////
383                    
384                    MenuItem item = menu.findItem( MENU_NOTIFICATIONS );
385                    boolean notifEnabled = hasNotifications();
386                    item.setEnabled(notifEnabled);
387    
388                    //////////////////////////
389    
390    
391                    
392                    
393                    return true;
394            }
395    
396          @Override          @Override
397          public boolean onCreateOptionsMenu(Menu menu) {          public boolean onCreateOptionsMenu(Menu menu) {
398                  MenuItem item;                  MenuItem item;
399                                    
400                    
401                    item = menu.add(0, MENU_TOGGLEDETAILS, 0, getString(R.string.departurelist_toggledetails));
402                    item.setIcon(android.R.drawable.ic_menu_view);
403                    
404                  item = menu.add(0, MENU_MAP, 0, getString(R.string.departurelist_showonmap) );                  item = menu.add(0, MENU_MAP, 0, getString(R.string.departurelist_showonmap) );
405                  item.setIcon(android.R.drawable.ic_menu_mapmode);                  item.setIcon(android.R.drawable.ic_menu_mapmode);
406                                    
# Line 349  public class DepartureList extends ListA Line 409  public class DepartureList extends ListA
409                                    
410                  boolean notifEnabled = hasNotifications();                  boolean notifEnabled = hasNotifications();
411                  item.setEnabled(notifEnabled);                  item.setEnabled(notifEnabled);
412    
413                    ///////////////////////////////////////
414                    if ( ! favorites.hasFavorite( this.station.getId() ) ) {
415                            item = menu.add(0, MENU_FAVORITES_ADD, 0, getString(dk.thoerup.traininfo.R.string.stationlist_addfavorite) );
416                    } else {
417                            item = menu.add(0, MENU_FAVORITES_REMOVE, 0, getString(dk.thoerup.traininfo.R.string.stationlist_removefavorite) );
418                    }
419                    item.setIcon(android.R.drawable.ic_menu_save);                  
420                    /////////////////////////////
421                                    
422                  if (station.isMetro()) {                  if (station.isMetro()) {
423                          item = menu.add(0, MENU_METROMAP, 0, "Metro" ); //TODO:translate!?!                          item = menu.add(0, MENU_METROMAP, 0, "Metro" ); //TODO:translate!?!
424                          item.setIcon(android.R.drawable.ic_menu_mapmode);                                                item.setIcon(android.R.drawable.ic_menu_mapmode);                      
425                  }                  }                                              
                   
426    
427                  return true;                  return true;
428          }          }
429    
430          @Override          @Override
431          public boolean onOptionsItemSelected(MenuItem item) {                    public boolean onOptionsItemSelected(MenuItem item) {          
432                  boolean res;                  boolean res = true;
433                  switch(item.getItemId()) {                  switch(item.getItemId()) {
434                  case MENU_MAP:                  case MENU_MAP:
435                          Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude() + "?z=16");                          Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude() + "?z=16");
436                          startActivity( new Intent(Intent.ACTION_VIEW, uri));                          
437                          res = true;                          try {                          
438                                    startActivity( new Intent(Intent.ACTION_VIEW, uri));
439                            } catch (ActivityNotFoundException anfe) {
440                                     Toast.makeText(this, "Could not launch google maps", Toast.LENGTH_LONG).show();
441                            }
442                            
443                          break;                          break;
444                  case MENU_NOTIFICATIONS:                  case MENU_NOTIFICATIONS:
445                          Intent i = new Intent(this,dk.thoerup.traininfo.NotificationList.class);                          Intent i = new Intent(this,dk.thoerup.traininfo.NotificationList.class);
446                          i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications);                          i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications);
447                          startActivity(i);                          startActivity(i);                      
                         res = true;  
448                          break;                          break;
449                  case MENU_METROMAP:                  case MENU_METROMAP:
450                          Intent metroMap = new Intent(this,dk.thoerup.traininfo.MetroMap.class);                          Intent metroMap = new Intent(this,dk.thoerup.traininfo.MetroMap.class);
451                          startActivity(metroMap);                          startActivity(metroMap);                        
                         res = true;  
452                          break;                                            break;                  
453                    case MENU_TOGGLEDETAILS:
454                            adapter.toggleShowDetails();                                            
455                            break;
456                    case MENU_FAVORITES_ADD:
457                            favorites.addFavorite( this.station.getId() );
458                            Toast.makeText(this, getString(dk.thoerup.traininfo.R.string.stationlist_stationadded), Toast.LENGTH_SHORT).show();
459                            break;
460                    case MENU_FAVORITES_REMOVE:
461                            favorites.removeFavorite( station.getId() );
462                            Toast.makeText(this, getString(dk.thoerup.traininfo.R.string.stationlist_stationremoved), Toast.LENGTH_SHORT).show();
463                            break;
464                  default:                  default:
465                          res = super.onOptionsItemSelected(item);                          res = super.onOptionsItemSelected(item);
466                  }                  }
# Line 429  public class DepartureList extends ListA Line 511  public class DepartureList extends ListA
511                                                    
512                                                    
513                          pgDialog.dismiss();                          pgDialog.dismiss();
514                            
515                          if (departures != null) {                          if (departures != null && departures.errorCode == null) {
516                                  commFailCounter = 0;                                  commFailCounter = 0;
517                                  DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html                                  DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html
518                                  adapter.setDepartures(departures.entries);                                  adapter.setDepartures(departures.entries);
519                                  DepartureList.this.getListView().setVisibility(View.VISIBLE);                                  DepartureList.this.getListView().setVisibility(View.VISIBLE);
520                                                                    
521                                                                    
522                                  // handle notification icon.                                                                                              // handle notification icon.
523                                  if ( hasNotifications() ) {                                  View notifIcon = findViewById(R.id.notifIcon);
524                                          View notifIcon = findViewById(R.id.notifIcon);                                  if ( hasNotifications() ) {                                    
525                                          notifIcon.setVisibility(View.VISIBLE);                                          notifIcon.setVisibility(View.VISIBLE);
526                                          notifIcon.setClickable(true);                                          notifIcon.setClickable(true);
527                                          notifIcon.setOnClickListener( new View.OnClickListener() {                                                                                        notifIcon.setOnClickListener( new View.OnClickListener() {                                              
# Line 450  public class DepartureList extends ListA Line 532  public class DepartureList extends ListA
532                                                          startActivity(i);                                                                                                                startActivity(i);                                                      
533                                                  }                                                  }
534                                          });                                          });
535                                  }                                                                } else {
536                                            notifIcon.setVisibility(View.INVISIBLE);
537                                    }
538                                                                    
539                                  if (departures.entries.size() != 0) {                                  if (departures.entries.size() == 0) {
540                                          int msgId = (arrival==false) ? R.string.departurelist_nodepartures : R.string.departurelist_noarrivals;                                          int msgId = (arrival==false) ? R.string.departurelist_nodepartures : R.string.departurelist_noarrivals;
541                                          MessageBox.showMessage(DepartureList.this, getString(msgId), false);                                          MessageBox.showMessage(DepartureList.this, getString(msgId), false);
542                                  }                                  }
543                          } else { // communication or parse error                          } else { // communication or parse error
544                                  commFailCounter++;                                  commFailCounter++;
545                                  AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this);                                                                                AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this);
546                                  builder.setMessage("Error finding departures");                                  
547                                    if (departures != null && departures.errorCode != null ) { //got an error xml back
548                                            commFailCounter = 10;
549                                            builder.setMessage( getString(R.string.no_backend) );
550                                    } else {                                        
551                                            builder.setMessage( getString(R.string.departurelist_fetcherror) );            
552                                    }
553                                  builder.setCancelable(true);                                  builder.setCancelable(true);
554                                    
555                                  if (commFailCounter < 3) {                                  if (commFailCounter < 3) {
556                                          builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {                                          builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
557                                                  public void onClick(DialogInterface dialog, int id) {                                                  public void onClick(DialogInterface dialog, int id) {
# Line 477  public class DepartureList extends ListA Line 568  public class DepartureList extends ListA
568                                          }                                                                                                }                                                      
569                                  });                                  });
570                                                                    
571                                  try {                                  try { //TODO: is this still necessary after the 0.9.4.1 fix ?
572                                          builder.show();                                          builder.show();
573                                  } catch (android.view.WindowManager.BadTokenException e) {                                                                        } catch (android.view.WindowManager.BadTokenException e) {                                      
574                                          Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running                                          Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running
# Line 583  public class DepartureList extends ListA Line 674  public class DepartureList extends ListA
674                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
675                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
676                                                  dialog.dismiss();                                                  dialog.dismiss();
677                                                  DepartureList.this.finish();                                                  DepartureList.this.finish(); //TODO: should we really close the activity ??
678                                          }                                                                                                }                                                      
679                                  });                                  });
680                                                                    
681                                  try {                                  try { //TODO: is this still necessary after the 0.9.4.1 fix ?
682                                          builder.show();                                          builder.show();
683                                  } catch (android.view.WindowManager.BadTokenException e) {                                                                        } catch (android.view.WindowManager.BadTokenException e) {                                      
684                                          Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running                                          Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running

Legend:
Removed from v.1262  
changed lines
  Added in v.1798

  ViewVC Help
Powered by ViewVC 1.1.20