/[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 1053 by torben, Tue Sep 14 16:00:57 2010 UTC revision 1441 by torben, Tue May 3 20:30:12 2011 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.graphics.Typeface;  import android.graphics.Typeface;
# Line 27  import android.widget.ListView; Line 28  import android.widget.ListView;
28  import android.widget.TableLayout;  import android.widget.TableLayout;
29  import android.widget.TableRow;  import android.widget.TableRow;
30  import android.widget.TextView;  import android.widget.TextView;
31    import android.widget.Toast;
32    import dk.thoerup.android.traininfo.common.DepartureBean;
33    import dk.thoerup.android.traininfo.common.DepartureEntry;
34    import dk.thoerup.android.traininfo.common.MetroBean;
35    import dk.thoerup.android.traininfo.common.MetroBean.MetroEntry;
36    import dk.thoerup.android.traininfo.common.StationEntry;
37  import dk.thoerup.traininfo.provider.DepartureProvider;  import dk.thoerup.traininfo.provider.DepartureProvider;
38  import dk.thoerup.traininfo.provider.MetroProvider;  import dk.thoerup.traininfo.provider.MetroProvider;
39  import dk.thoerup.traininfo.provider.ProviderFactory;  import dk.thoerup.traininfo.provider.ProviderFactory;
 import dk.thoerup.traininfo.provider.MetroProvider.MetroBean;  
 import dk.thoerup.traininfo.provider.MetroProvider.MetroEntry;  
40  import dk.thoerup.traininfo.util.MessageBox;  import dk.thoerup.traininfo.util.MessageBox;
41    
42  public class DepartureList extends ListActivity {  public class DepartureList extends ListActivity {
# Line 39  public class DepartureList extends ListA Line 44  public class DepartureList extends ListA
44          public static final int DLG_PROGRESS = 1;          public static final int DLG_PROGRESS = 1;
45          static final int MENU_MAP = 100;          static final int MENU_MAP = 100;
46          static final int MENU_NOTIFICATIONS = 101;          static final int MENU_NOTIFICATIONS = 101;
47            static final int MENU_METROMAP = 102;
48            static final int MENU_TOGGLEDETAILS= 103;
49                    
50                    
51          DepartureListAdapter adapter;          DepartureListAdapter adapter;
# Line 56  public class DepartureList extends ListA Line 63  public class DepartureList extends ListA
63          DepartureFetcher fetcher;          DepartureFetcher fetcher;
64          MetroFetcher metroFetcher;          MetroFetcher metroFetcher;
65    
66          StationBean station;          StationEntry station;
67                    
68          boolean arrival = false;          String trainType = "REGIONAL";
69            
70            boolean arrival = false;        
71    
72          int commFailCounter = 0;          int commFailCounter = 0;
73    
# Line 68  public class DepartureList extends ListA Line 77  public class DepartureList extends ListA
77                  setContentView(R.layout.departurelist);                  setContentView(R.layout.departurelist);
78                                    
79                  adapter = new DepartureListAdapter(this);                  adapter = new DepartureListAdapter(this);
80                  setListAdapter(adapter);                  setListAdapter(adapter);                                
81                                    
82                  Intent launchedBy = getIntent();                  Intent launchedBy = getIntent();
83                    
84                  station = (StationBean) launchedBy.getSerializableExtra("stationbean");                  station = (StationEntry) launchedBy.getSerializableExtra("stationbean");
85                                    
86                  ((TextView) findViewById(R.id.stationName)).setText( station.getName() );                  ((TextView) findViewById(R.id.stationName)).setText( station.getName() );
87    
# Line 82  public class DepartureList extends ListA Line 91  public class DepartureList extends ListA
91                  final Button departureBtn = (Button) findViewById(R.id.departurebtn);                  final Button departureBtn = (Button) findViewById(R.id.departurebtn);
92                  final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn);                  final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn);
93                  final Button metroBtn = (Button) findViewById(R.id.metrobtn);                  final Button metroBtn = (Button) findViewById(R.id.metrobtn);
94                    final Button regionalBtn = (Button) findViewById(R.id.regionalbtn);
95                    final Button stogBtn = (Button) findViewById(R.id.stogbtn);
96                                    
97                  final View metroView = findViewById(R.id.metroonly);                  final View metroView = findViewById(R.id.metroonly);
98                                    
# Line 112  public class DepartureList extends ListA Line 123  public class DepartureList extends ListA
123                          }                          }
124                  });                  });
125                                    
126                    regionalBtn.setOnClickListener( new OnClickListener() {
127                            @Override
128                            public void onClick(View arg0) {        
129                                    regionalBtn.setBackgroundResource(R.drawable.custom_button_hilight);
130                                    stogBtn.setBackgroundResource(R.drawable.custom_button);                                
131                                    metroBtn.setBackgroundResource(R.drawable.custom_button);
132                                    
133                                    departureBtn.setVisibility( View.VISIBLE );
134                                    arrivalBtn.setVisibility( View.VISIBLE );
135                                    
136                                    getListView().setVisibility( View.VISIBLE );            
137                                    metroView.setVisibility( View.GONE );
138                                    trainType = "REGIONAL";
139                                    startDepartureFetcher();
140                            }
141                    });
142                    stogBtn.setOnClickListener( new OnClickListener() {
143                            @Override
144                            public void onClick(View arg0) {        
145                                    regionalBtn.setBackgroundResource(R.drawable.custom_button);
146                                    stogBtn.setBackgroundResource(R.drawable.custom_button_hilight);                                
147                                    metroBtn.setBackgroundResource(R.drawable.custom_button);
148    
149    
150                                    departureBtn.setVisibility( View.VISIBLE );
151                                    arrivalBtn.setVisibility( View.VISIBLE );
152                                    
153                                    getListView().setVisibility( View.VISIBLE );            
154                                    metroView.setVisibility( View.GONE );
155                                    trainType = "STOG";                    
156                                    startDepartureFetcher();
157                            }
158                    });
159    
160                    
161                    
162                  metroBtn.setOnClickListener( new OnClickListener() {                                      metroBtn.setOnClickListener( new OnClickListener() {                    
163                          @Override                          @Override
164                          public void onClick(View v) {                          public void onClick(View v) {
165                                  arrivalBtn.setBackgroundResource(R.drawable.custom_button);                                  regionalBtn.setBackgroundResource(R.drawable.custom_button);
166                                  departureBtn.setBackgroundResource(R.drawable.custom_button);                                  stogBtn.setBackgroundResource(R.drawable.custom_button);                                
167                                  metroBtn.setBackgroundResource(R.drawable.custom_button_hilight);                                  metroBtn.setBackgroundResource(R.drawable.custom_button_hilight);
168                                                                    
169                                    departureBtn.setVisibility( View.GONE );
170                                    arrivalBtn.setVisibility( View.GONE );
171                                    
172                                  getListView().setVisibility( View.GONE );                                                getListView().setVisibility( View.GONE );              
173                                  metroView.setVisibility( View.VISIBLE );                                  metroView.setVisibility( View.VISIBLE );
174                                  startMetroFetcher();                                  startMetroFetcher();
# Line 130  public class DepartureList extends ListA Line 180  public class DepartureList extends ListA
180                                    
181                  // findViewById(R.id.header).setOnClickListener( mapLauncher );                  // findViewById(R.id.header).setOnClickListener( mapLauncher );
182                                    
183                  int distance = station.getDistance();                  int distance = station.getCalcdist();
184                  if (distance != 0) {                  if (distance != 0) {
185                          NumberFormat format = NumberFormat.getNumberInstance();                          NumberFormat format = NumberFormat.getNumberInstance();
186                          format.setMaximumFractionDigits(1);                          format.setMaximumFractionDigits(1);
# Line 151  public class DepartureList extends ListA Line 201  public class DepartureList extends ListA
201                    
202                  metro = ProviderFactory.getMetroProvider();                  metro = ProviderFactory.getMetroProvider();
203                                    
204                  if (station.isRegional() == false && station.isSTrain() == false) {                  if (station.isRegional() == false ) {
205                            regionalBtn.setVisibility(View.GONE);
206                    }
207                    
208                    if (station.isStrain() == false ) {
209                            stogBtn.setVisibility(View.GONE);
210                    }
211                    
212                    if (station.isRegional() == true && station.isStrain() == false ) {
213                            if ( station.isMetro() == false )
214                                    regionalBtn.setVisibility(View.GONE);
215                            trainType = "REGIONAL";                
216                    }              
217    
218                    if (station.isRegional() == false  && station.isStrain() == true) {
219                            if (station.isMetro() == false)
220                                    stogBtn.setVisibility(View.GONE);
221    
222                            stogBtn.setBackgroundResource(R.drawable.custom_button_hilight);
223                            trainType = "STOG";
224                            
225                    }
226                                    
227                    
228                    if (station.isRegional() == false && station.isStrain() == false) {
229                          getListView().setVisibility( View.GONE );                                        getListView().setVisibility( View.GONE );              
230                          metroView.setVisibility( View.VISIBLE );                          metroView.setVisibility( View.VISIBLE );
231                                                    
232                          departureBtn.setVisibility( View.GONE );                          departureBtn.setVisibility( View.GONE );
233                          arrivalBtn.setVisibility(View.GONE);                          arrivalBtn.setVisibility(View.GONE);
234                          metroBtn.setVisibility( View.GONE );                          metroBtn.setVisibility( View.GONE );            
235                            
236                            
237                                                    
238                          if (savedInstanceState == null) {                          if (savedInstanceState == null) {
239                                  startMetroFetcher();                                  startMetroFetcher();
# Line 262  public class DepartureList extends ListA Line 338  public class DepartureList extends ListA
338                    
339                    
340    
   
   
341          @Override          @Override
342          public boolean onCreateOptionsMenu(Menu menu) {          public boolean onCreateOptionsMenu(Menu menu) {
343                  MenuItem item;                  MenuItem item;
344                                    
345                    
346                    item = menu.add(0, MENU_TOGGLEDETAILS, 0, getString(R.string.departurelist_toggledetails));
347                    item.setIcon(android.R.drawable.ic_menu_view);
348                    
349                  item = menu.add(0, MENU_MAP, 0, getString(R.string.departurelist_showonmap) );                  item = menu.add(0, MENU_MAP, 0, getString(R.string.departurelist_showonmap) );
350                  item.setIcon(android.R.drawable.ic_menu_mapmode);                  item.setIcon(android.R.drawable.ic_menu_mapmode);
351                                    
352                  item = menu.add(0, MENU_NOTIFICATIONS, 0, getString(R.string.departurelist_notifications) );                  item = menu.add(0, MENU_NOTIFICATIONS, 0, getString(R.string.departurelist_notifications) );
353                  item.setIcon(android.R.drawable.ic_menu_info_details);                  item.setIcon(android.R.drawable.ic_menu_info_details);                  
                   
354                                    
355                  boolean notifEnabled = hasNotifications();                  boolean notifEnabled = hasNotifications();
356                  item.setEnabled(notifEnabled);                  item.setEnabled(notifEnabled);
357                                    
358                    if (station.isMetro()) {
359                            item = menu.add(0, MENU_METROMAP, 0, "Metro" ); //TODO:translate!?!
360                            item.setIcon(android.R.drawable.ic_menu_mapmode);                      
361                    }                                              
362    
363                  return true;                  return true;
364          }          }
365    
366          @Override          @Override
367          public boolean onOptionsItemSelected(MenuItem item) {                    public boolean onOptionsItemSelected(MenuItem item) {          
368                  boolean res;                  boolean res = true;
369                  switch(item.getItemId()) {                  switch(item.getItemId()) {
370                  case MENU_MAP:                  case MENU_MAP:
371                          Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude());                          Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude() + "?z=16");
372                          startActivity( new Intent(Intent.ACTION_VIEW, uri));                          
373                          res = true;                          try {                          
374                                    startActivity( new Intent(Intent.ACTION_VIEW, uri));
375                            } catch (ActivityNotFoundException anfe) {
376                                     Toast.makeText(this, "Could not launch google maps", Toast.LENGTH_LONG).show();
377                            }
378                            
379                          break;                          break;
380                  case MENU_NOTIFICATIONS:                  case MENU_NOTIFICATIONS:
381                          Intent i = new Intent(this,dk.thoerup.traininfo.NotificationList.class);                          Intent i = new Intent(this,dk.thoerup.traininfo.NotificationList.class);
382                          i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications);                          i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications);
383                          startActivity(i);                          startActivity(i);                      
384                          res = true;                          break;
385                    case MENU_METROMAP:
386                            Intent metroMap = new Intent(this,dk.thoerup.traininfo.MetroMap.class);
387                            startActivity(metroMap);                        
388                            break;                  
389                    case MENU_TOGGLEDETAILS:
390                            adapter.toggleShowDetails();                                            
391                          break;                          break;
392                  default:                  default:
393                          res = super.onOptionsItemSelected(item);                          res = super.onOptionsItemSelected(item);
# Line 347  public class DepartureList extends ListA Line 439  public class DepartureList extends ListA
439                                                    
440                                                    
441                          pgDialog.dismiss();                          pgDialog.dismiss();
442                            
443                          if (departures != null) {                          if (departures != null && departures.errorCode == null) {
444                                  commFailCounter = 0;                                  commFailCounter = 0;
445                                  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
446                                  adapter.setDepartures(departures.entries);                                  adapter.setDepartures(departures.entries);
447                                  DepartureList.this.getListView().setVisibility(View.VISIBLE);                                  DepartureList.this.getListView().setVisibility(View.VISIBLE);
448                                                                    
449                                                                    
450                                  if ( hasNotifications() ) {                                  // handle notification icon.
451                                          findViewById(R.id.notifIcon).setVisibility(View.VISIBLE);                                  View notifIcon = findViewById(R.id.notifIcon);
452                                    if ( hasNotifications() ) {                                    
453                                            notifIcon.setVisibility(View.VISIBLE);
454                                            notifIcon.setClickable(true);
455                                            notifIcon.setOnClickListener( new View.OnClickListener() {                                              
456                                                    @Override
457                                                    public void onClick(View v) {
458                                                            Intent i = new Intent(DepartureList.this, dk.thoerup.traininfo.NotificationList.class);
459                                                            i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications);
460                                                            startActivity(i);                                                      
461                                                    }
462                                            });
463                                    } else {
464                                            notifIcon.setVisibility(View.INVISIBLE);
465                                  }                                  }
466                                                                    
467                                  if (departures.entries.size() == 0) {                                  if (departures.entries.size() == 0) {
468                                          MessageBox.showMessage(DepartureList.this, "No departures found", true);                                          int msgId = (arrival==false) ? R.string.departurelist_nodepartures : R.string.departurelist_noarrivals;
469                                            MessageBox.showMessage(DepartureList.this, getString(msgId), false);
470                                  }                                  }
471                          } else { // communication or parse error                          } else { // communication or parse error
472                                  commFailCounter++;                                  commFailCounter++;
473                                  AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this);                                                                                AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this);
474                                  builder.setMessage("Error finding departures");                                  
475                                    if (departures != null && departures.errorCode != null ) { //got an error xml back
476                                            commFailCounter = 10;
477                                            builder.setMessage( getString(R.string.no_backend) );
478                                    } else {                                        
479                                            builder.setMessage( getString(R.string.departurelist_fetcherror) );            
480                                    }
481                                  builder.setCancelable(true);                                  builder.setCancelable(true);
482                                    
483                                  if (commFailCounter < 3) {                                  if (commFailCounter < 3) {
484                                          builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {                                          builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
485                                                  public void onClick(DialogInterface dialog, int id) {                                                  public void onClick(DialogInterface dialog, int id) {
# Line 383  public class DepartureList extends ListA Line 496  public class DepartureList extends ListA
496                                          }                                                                                                }                                                      
497                                  });                                  });
498                                                                    
499                                  try {                                  try { //TODO: is this still necessary after the 0.9.4.1 fix ?
500                                          builder.show();                                          builder.show();
501                                  } catch (android.view.WindowManager.BadTokenException e) {                                                                        } catch (android.view.WindowManager.BadTokenException e) {                                      
502                                          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 393  public class DepartureList extends ListA Line 506  public class DepartureList extends ListA
506    
507                  @Override                  @Override
508                  protected Void doInBackground(Integer... params) {                  protected Void doInBackground(Integer... params) {
509                          departures = provider.lookupDepartures(params[0], DepartureList.this.arrival);                          departures = provider.lookupDepartures(params[0], DepartureList.this.arrival, trainType);
510                          return null;                          return null;
511                  }                  }
512                                    
# Line 411  public class DepartureList extends ListA Line 524  public class DepartureList extends ListA
524                                    
525                  TextView h1 = new TextView(this);                  TextView h1 = new TextView(this);
526                  h1.setText("Metro");                  h1.setText("Metro");
527                    h1.setTextSize(16);
528                  h1.setTypeface( Typeface.defaultFromStyle(Typeface.BOLD));                  h1.setTypeface( Typeface.defaultFromStyle(Typeface.BOLD));
529                                    
530                                    
# Line 418  public class DepartureList extends ListA Line 532  public class DepartureList extends ListA
532                  params.span = 2;                  params.span = 2;
533                  head.addView(h1, params);                  head.addView(h1, params);
534                                    
535                    
536                    
537                  TextView h2 = new TextView(this);                  TextView h2 = new TextView(this);
538                  h2.setText("Om minutte");                  h2.setTextSize(16);
539                  h2.setTypeface( Typeface.defaultFromStyle(Typeface.BOLD));                  h2.setTypeface( Typeface.defaultFromStyle(Typeface.BOLD));
540                  head.addView(h2);                  h2.setText("Om minutter");
541                    
542                    params = new TableRow.LayoutParams();
543                    params.weight = 2;
544                    head.addView(h2,params);
545    
546                                    
547                                    
# Line 433  public class DepartureList extends ListA Line 553  public class DepartureList extends ListA
553                          Log.e("Test", "" + entry.destination);                          Log.e("Test", "" + entry.destination);
554                                                    
555                          TextView v1 = new TextView(this);                          TextView v1 = new TextView(this);
556                          v1.setText( entry.metro );                          v1.setTextSize(16);
557                            v1.setText( entry.metro );                      
558                          row.addView(v1);                          row.addView(v1);
559                                                    
560                          TextView v2 = new TextView(this);                          TextView v2 = new TextView(this);
561                            v2.setTextSize(16);
562                          v2.setText( entry.destination );                          v2.setText( entry.destination );
563                          row.addView(v2);                          row.addView(v2);
564                                            
565                          TextView v3 = new TextView(this);                          TextView v3 = new TextView(this);
566                            v3.setTextSize(16);
567                          v3.setText( entry.minutes );                          v3.setText( entry.minutes );
568                          row.addView(v3);                          row.addView(v3);
569                                                    
# Line 479  public class DepartureList extends ListA Line 602  public class DepartureList extends ListA
602                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
603                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
604                                                  dialog.dismiss();                                                  dialog.dismiss();
605                                                  DepartureList.this.finish();                                                  DepartureList.this.finish(); //TODO: should we really close the activity ??
606                                          }                                                                                                }                                                      
607                                  });                                  });
608                                                                    
609                                  try {                                  try { //TODO: is this still necessary after the 0.9.4.1 fix ?
610                                          builder.show();                                          builder.show();
611                                  } catch (android.view.WindowManager.BadTokenException e) {                                                                        } catch (android.view.WindowManager.BadTokenException e) {                                      
612                                          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.1053  
changed lines
  Added in v.1441

  ViewVC Help
Powered by ViewVC 1.1.20