/[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 1433 by torben, Tue May 3 12:38:04 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                    
49                    
50          DepartureListAdapter adapter;          DepartureListAdapter adapter;
# Line 56  public class DepartureList extends ListA Line 62  public class DepartureList extends ListA
62          DepartureFetcher fetcher;          DepartureFetcher fetcher;
63          MetroFetcher metroFetcher;          MetroFetcher metroFetcher;
64    
65          StationBean station;          StationEntry station;
66            
67            String trainType = "REGIONAL";
68                    
69          boolean arrival = false;          boolean arrival = false;
70    
# Line 72  public class DepartureList extends ListA Line 80  public class DepartureList extends ListA
80                                    
81                  Intent launchedBy = getIntent();                  Intent launchedBy = getIntent();
82                    
83                  station = (StationBean) launchedBy.getSerializableExtra("stationbean");                  station = (StationEntry) launchedBy.getSerializableExtra("stationbean");
84                                    
85                  ((TextView) findViewById(R.id.stationName)).setText( station.getName() );                  ((TextView) findViewById(R.id.stationName)).setText( station.getName() );
86    
# Line 82  public class DepartureList extends ListA Line 90  public class DepartureList extends ListA
90                  final Button departureBtn = (Button) findViewById(R.id.departurebtn);                  final Button departureBtn = (Button) findViewById(R.id.departurebtn);
91                  final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn);                  final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn);
92                  final Button metroBtn = (Button) findViewById(R.id.metrobtn);                  final Button metroBtn = (Button) findViewById(R.id.metrobtn);
93                    final Button regionalBtn = (Button) findViewById(R.id.regionalbtn);
94                    final Button stogBtn = (Button) findViewById(R.id.stogbtn);
95                                    
96                  final View metroView = findViewById(R.id.metroonly);                  final View metroView = findViewById(R.id.metroonly);
97                                    
# Line 112  public class DepartureList extends ListA Line 122  public class DepartureList extends ListA
122                          }                          }
123                  });                  });
124                                    
125                    regionalBtn.setOnClickListener( new OnClickListener() {
126                            @Override
127                            public void onClick(View arg0) {        
128                                    regionalBtn.setBackgroundResource(R.drawable.custom_button_hilight);
129                                    stogBtn.setBackgroundResource(R.drawable.custom_button);                                
130                                    metroBtn.setBackgroundResource(R.drawable.custom_button);
131                                    
132                                    departureBtn.setVisibility( View.VISIBLE );
133                                    arrivalBtn.setVisibility( View.VISIBLE );
134                                    
135                                    getListView().setVisibility( View.VISIBLE );            
136                                    metroView.setVisibility( View.GONE );
137                                    trainType = "REGIONAL";
138                                    startDepartureFetcher();
139                            }
140                    });
141                    stogBtn.setOnClickListener( new OnClickListener() {
142                            @Override
143                            public void onClick(View arg0) {        
144                                    regionalBtn.setBackgroundResource(R.drawable.custom_button);
145                                    stogBtn.setBackgroundResource(R.drawable.custom_button_hilight);                                
146                                    metroBtn.setBackgroundResource(R.drawable.custom_button);
147    
148    
149                                    departureBtn.setVisibility( View.VISIBLE );
150                                    arrivalBtn.setVisibility( View.VISIBLE );
151                                    
152                                    getListView().setVisibility( View.VISIBLE );            
153                                    metroView.setVisibility( View.GONE );
154                                    trainType = "STOG";                    
155                                    startDepartureFetcher();
156                            }
157                    });
158    
159                    
160                    
161                  metroBtn.setOnClickListener( new OnClickListener() {                                      metroBtn.setOnClickListener( new OnClickListener() {                    
162                          @Override                          @Override
163                          public void onClick(View v) {                          public void onClick(View v) {
164                                  arrivalBtn.setBackgroundResource(R.drawable.custom_button);                                  regionalBtn.setBackgroundResource(R.drawable.custom_button);
165                                  departureBtn.setBackgroundResource(R.drawable.custom_button);                                  stogBtn.setBackgroundResource(R.drawable.custom_button);                                
166                                  metroBtn.setBackgroundResource(R.drawable.custom_button_hilight);                                  metroBtn.setBackgroundResource(R.drawable.custom_button_hilight);
167                                                                    
168                                    departureBtn.setVisibility( View.GONE );
169                                    arrivalBtn.setVisibility( View.GONE );
170                                    
171                                  getListView().setVisibility( View.GONE );                                                getListView().setVisibility( View.GONE );              
172                                  metroView.setVisibility( View.VISIBLE );                                  metroView.setVisibility( View.VISIBLE );
173                                  startMetroFetcher();                                  startMetroFetcher();
# Line 130  public class DepartureList extends ListA Line 179  public class DepartureList extends ListA
179                                    
180                  // findViewById(R.id.header).setOnClickListener( mapLauncher );                  // findViewById(R.id.header).setOnClickListener( mapLauncher );
181                                    
182                  int distance = station.getDistance();                  int distance = station.getCalcdist();
183                  if (distance != 0) {                  if (distance != 0) {
184                          NumberFormat format = NumberFormat.getNumberInstance();                          NumberFormat format = NumberFormat.getNumberInstance();
185                          format.setMaximumFractionDigits(1);                          format.setMaximumFractionDigits(1);
# Line 151  public class DepartureList extends ListA Line 200  public class DepartureList extends ListA
200                    
201                  metro = ProviderFactory.getMetroProvider();                  metro = ProviderFactory.getMetroProvider();
202                                    
203                  if (station.isRegional() == false && station.isSTrain() == false) {                  if (station.isRegional() == false ) {
204                            regionalBtn.setVisibility(View.GONE);
205                    }
206                    
207                    if (station.isStrain() == false ) {
208                            stogBtn.setVisibility(View.GONE);
209                    }
210                    
211                    if (station.isRegional() == true && station.isStrain() == false ) {
212                            if ( station.isMetro() == false )
213                                    regionalBtn.setVisibility(View.GONE);
214                            trainType = "REGIONAL";                
215                    }              
216    
217                    if (station.isRegional() == false  && station.isStrain() == true) {
218                            if (station.isMetro() == false)
219                                    stogBtn.setVisibility(View.GONE);
220    
221                            stogBtn.setBackgroundResource(R.drawable.custom_button_hilight);
222                            trainType = "STOG";
223                            
224                    }
225                                    
226                    
227                    if (station.isRegional() == false && station.isStrain() == false) {
228                          getListView().setVisibility( View.GONE );                                        getListView().setVisibility( View.GONE );              
229                          metroView.setVisibility( View.VISIBLE );                          metroView.setVisibility( View.VISIBLE );
230                                                    
231                          departureBtn.setVisibility( View.GONE );                          departureBtn.setVisibility( View.GONE );
232                          arrivalBtn.setVisibility(View.GONE);                          arrivalBtn.setVisibility(View.GONE);
233                          metroBtn.setVisibility( View.GONE );                          metroBtn.setVisibility( View.GONE );            
234                            
235                            
236                                                    
237                          if (savedInstanceState == null) {                          if (savedInstanceState == null) {
238                                  startMetroFetcher();                                  startMetroFetcher();
# Line 272  public class DepartureList extends ListA Line 347  public class DepartureList extends ListA
347                  item.setIcon(android.R.drawable.ic_menu_mapmode);                  item.setIcon(android.R.drawable.ic_menu_mapmode);
348                                    
349                  item = menu.add(0, MENU_NOTIFICATIONS, 0, getString(R.string.departurelist_notifications) );                  item = menu.add(0, MENU_NOTIFICATIONS, 0, getString(R.string.departurelist_notifications) );
350                  item.setIcon(android.R.drawable.ic_menu_info_details);                  item.setIcon(android.R.drawable.ic_menu_info_details);                  
                   
351                                    
352                  boolean notifEnabled = hasNotifications();                  boolean notifEnabled = hasNotifications();
353                  item.setEnabled(notifEnabled);                  item.setEnabled(notifEnabled);
354                                    
355                    if (station.isMetro()) {
356                            item = menu.add(0, MENU_METROMAP, 0, "Metro" ); //TODO:translate!?!
357                            item.setIcon(android.R.drawable.ic_menu_mapmode);                      
358                    }
359                    
360    
361                  return true;                  return true;
362          }          }
# Line 287  public class DepartureList extends ListA Line 366  public class DepartureList extends ListA
366                  boolean res;                  boolean res;
367                  switch(item.getItemId()) {                  switch(item.getItemId()) {
368                  case MENU_MAP:                  case MENU_MAP:
369                          Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude());                          try {
370                          startActivity( new Intent(Intent.ACTION_VIEW, uri));                                  Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude() + "?z=16");
371                                    startActivity( new Intent(Intent.ACTION_VIEW, uri));
372                            } catch (ActivityNotFoundException anfe) {
373                                     Toast.makeText(this, "Could not launch google maps", Toast.LENGTH_LONG).show();
374                            }
375                          res = true;                          res = true;
376                          break;                          break;
377                  case MENU_NOTIFICATIONS:                  case MENU_NOTIFICATIONS:
# Line 297  public class DepartureList extends ListA Line 380  public class DepartureList extends ListA
380                          startActivity(i);                          startActivity(i);
381                          res = true;                          res = true;
382                          break;                          break;
383                    case MENU_METROMAP:
384                            Intent metroMap = new Intent(this,dk.thoerup.traininfo.MetroMap.class);
385                            startActivity(metroMap);
386                            res = true;
387                            break;                  
388                  default:                  default:
389                          res = super.onOptionsItemSelected(item);                          res = super.onOptionsItemSelected(item);
390                  }                  }
# Line 347  public class DepartureList extends ListA Line 435  public class DepartureList extends ListA
435                                                    
436                                                    
437                          pgDialog.dismiss();                          pgDialog.dismiss();
438                            
439                          if (departures != null) {                          if (departures != null && departures.errorCode == null) {
440                                  commFailCounter = 0;                                  commFailCounter = 0;
441                                  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
442                                  adapter.setDepartures(departures.entries);                                  adapter.setDepartures(departures.entries);
443                                  DepartureList.this.getListView().setVisibility(View.VISIBLE);                                  DepartureList.this.getListView().setVisibility(View.VISIBLE);
444                                                                    
445                                                                    
446                                    // handle notification icon.                                                            
447                                  if ( hasNotifications() ) {                                  if ( hasNotifications() ) {
448                                          findViewById(R.id.notifIcon).setVisibility(View.VISIBLE);                                          View notifIcon = findViewById(R.id.notifIcon);
449                                  }                                          notifIcon.setVisibility(View.VISIBLE);
450                                            notifIcon.setClickable(true);
451                                            notifIcon.setOnClickListener( new View.OnClickListener() {                                              
452                                                    @Override
453                                                    public void onClick(View v) {
454                                                            Intent i = new Intent(DepartureList.this, dk.thoerup.traininfo.NotificationList.class);
455                                                            i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications);
456                                                            startActivity(i);                                                      
457                                                    }
458                                            });
459                                    }                              
460                                                                    
461                                  if (departures.entries.size() == 0) {                                  if (departures.entries.size() == 0) {
462                                          MessageBox.showMessage(DepartureList.this, "No departures found", true);                                          int msgId = (arrival==false) ? R.string.departurelist_nodepartures : R.string.departurelist_noarrivals;
463                                            MessageBox.showMessage(DepartureList.this, getString(msgId), false);
464                                  }                                  }
465                          } else { // communication or parse error                          } else { // communication or parse error
466                                  commFailCounter++;                                  commFailCounter++;
467                                  AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this);                                                                                AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this);
468                                  builder.setMessage("Error finding departures");                                  
469                                    if (departures != null && departures.errorCode != null ) { //got an error xml back
470                                            commFailCounter = 10;
471                                            builder.setMessage( getString(R.string.no_backend) );
472                                    } else {                                        
473                                            builder.setMessage( getString(R.string.departurelist_fetcherror) );            
474                                    }
475                                  builder.setCancelable(true);                                  builder.setCancelable(true);
476                                    
477                                  if (commFailCounter < 3) {                                  if (commFailCounter < 3) {
478                                          builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {                                          builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
479                                                  public void onClick(DialogInterface dialog, int id) {                                                  public void onClick(DialogInterface dialog, int id) {
# Line 383  public class DepartureList extends ListA Line 490  public class DepartureList extends ListA
490                                          }                                                                                                }                                                      
491                                  });                                  });
492                                                                    
493                                  try {                                  try { //TODO: is this still necessary after the 0.9.4.1 fix ?
494                                          builder.show();                                          builder.show();
495                                  } catch (android.view.WindowManager.BadTokenException e) {                                                                        } catch (android.view.WindowManager.BadTokenException e) {                                      
496                                          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 500  public class DepartureList extends ListA
500    
501                  @Override                  @Override
502                  protected Void doInBackground(Integer... params) {                  protected Void doInBackground(Integer... params) {
503                          departures = provider.lookupDepartures(params[0], DepartureList.this.arrival);                          departures = provider.lookupDepartures(params[0], DepartureList.this.arrival, trainType);
504                          return null;                          return null;
505                  }                  }
506                                    
# Line 411  public class DepartureList extends ListA Line 518  public class DepartureList extends ListA
518                                    
519                  TextView h1 = new TextView(this);                  TextView h1 = new TextView(this);
520                  h1.setText("Metro");                  h1.setText("Metro");
521                    h1.setTextSize(16);
522                  h1.setTypeface( Typeface.defaultFromStyle(Typeface.BOLD));                  h1.setTypeface( Typeface.defaultFromStyle(Typeface.BOLD));
523                                    
524                                    
# Line 418  public class DepartureList extends ListA Line 526  public class DepartureList extends ListA
526                  params.span = 2;                  params.span = 2;
527                  head.addView(h1, params);                  head.addView(h1, params);
528                                    
529                    
530                    
531                  TextView h2 = new TextView(this);                  TextView h2 = new TextView(this);
532                  h2.setText("Om minutte");                  h2.setTextSize(16);
533                  h2.setTypeface( Typeface.defaultFromStyle(Typeface.BOLD));                  h2.setTypeface( Typeface.defaultFromStyle(Typeface.BOLD));
534                  head.addView(h2);                  h2.setText("Om minutter");
535                    
536                    params = new TableRow.LayoutParams();
537                    params.weight = 2;
538                    head.addView(h2,params);
539    
540                                    
541                                    
# Line 433  public class DepartureList extends ListA Line 547  public class DepartureList extends ListA
547                          Log.e("Test", "" + entry.destination);                          Log.e("Test", "" + entry.destination);
548                                                    
549                          TextView v1 = new TextView(this);                          TextView v1 = new TextView(this);
550                          v1.setText( entry.metro );                          v1.setTextSize(16);
551                            v1.setText( entry.metro );                      
552                          row.addView(v1);                          row.addView(v1);
553                                                    
554                          TextView v2 = new TextView(this);                          TextView v2 = new TextView(this);
555                            v2.setTextSize(16);
556                          v2.setText( entry.destination );                          v2.setText( entry.destination );
557                          row.addView(v2);                          row.addView(v2);
558                                            
559                          TextView v3 = new TextView(this);                          TextView v3 = new TextView(this);
560                            v3.setTextSize(16);
561                          v3.setText( entry.minutes );                          v3.setText( entry.minutes );
562                          row.addView(v3);                          row.addView(v3);
563                                                    
# Line 479  public class DepartureList extends ListA Line 596  public class DepartureList extends ListA
596                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
597                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
598                                                  dialog.dismiss();                                                  dialog.dismiss();
599                                                  DepartureList.this.finish();                                                  DepartureList.this.finish(); //TODO: should we really close the activity ??
600                                          }                                                                                                }                                                      
601                                  });                                  });
602                                                                    
603                                  try {                                  try { //TODO: is this still necessary after the 0.9.4.1 fix ?
604                                          builder.show();                                          builder.show();
605                                  } catch (android.view.WindowManager.BadTokenException e) {                                                                        } catch (android.view.WindowManager.BadTokenException e) {                                      
606                                          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.1433

  ViewVC Help
Powered by ViewVC 1.1.20