/[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 294 by torben, Tue Sep 1 20:28:55 2009 UTC revision 1709 by torben, Tue Mar 6 11:02:04 2012 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfo;  package dk.thoerup.traininfo;
2    
3    import static dk.thoerup.traininfo.R.string.departurelist_fetcharrivals;
4    import static dk.thoerup.traininfo.R.string.departurelist_fetchdepartures;
5    import static dk.thoerup.traininfo.R.string.generic_cancel;
6    import static dk.thoerup.traininfo.R.string.generic_retry;
7    
8  import java.text.NumberFormat;  import java.text.NumberFormat;
 import java.util.ArrayList;  
 import java.util.List;  
9    
10    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;
16  import android.content.Intent;  import android.content.Intent;
17    import android.content.SharedPreferences;
18    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;
24    import android.view.Menu;
25    import android.view.MenuItem;
26  import android.view.View;  import android.view.View;
27    import android.view.View.OnClickListener;
28    import android.widget.Button;
29  import android.widget.ListView;  import android.widget.ListView;
30    import android.widget.TableLayout;
31    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;
35    import dk.thoerup.android.traininfo.common.DepartureEntry;
36    import dk.thoerup.android.traininfo.common.MetroBean;
37    import dk.thoerup.android.traininfo.common.MetroBean.MetroEntry;
38    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;
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          public static final int DLG_DETAILS = 2;          static final int MENU_MAP = 100;
51            static final int MENU_NOTIFICATIONS = 101;
52            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;
60          DepartureProvider provider;          DepartureProvider provider;
61          List<DepartureBean> departures;          DepartureBean departures;
62            
63            MetroBean metroBean;
64            MetroProvider metro;
65                    
66          int selectedItemId;          int selectedItemId;
67    
68            FavoritesHelper favorites;
69    
70          //DepartureBean currentDeparture;          //DepartureBean currentDeparture;
71                    
72          ProgressDialog pgDialog;          ProgressDialog pgDialog;
73          Dialog detailsDialog;          
74          DepartureFetcher fetcher;          DepartureFetcher fetcher;
75            MetroFetcher metroFetcher;
76    
77            StationEntry station;
78                    
79          double latitude,longitude;          String trainType = "REGIONAL";
80                    
81          @SuppressWarnings("unchecked")          boolean arrival = false;        
82    
83            int commFailCounter = 0;
84    
85          @Override          @Override
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");
98                    
99                    ((TextView) findViewById(R.id.stationName)).setText( station.getName() );
100    
101    
102                    ((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() );
103                                    
104                  latitude = launchedBy.getDoubleExtra("latitude", 0.0);                  final Button departureBtn = (Button) findViewById(R.id.departurebtn);
105                  longitude = launchedBy.getDoubleExtra("longitude", 0.0);                  final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn);
106                    final Button metroBtn = (Button) findViewById(R.id.metrobtn);
107                    final Button regionalBtn = (Button) findViewById(R.id.regionalbtn);
108                    final Button stogBtn = (Button) findViewById(R.id.stogbtn);
109                    
110                    final View metroView = findViewById(R.id.metroonly);
111                    
112                    departureBtn.setOnClickListener( new OnClickListener() {
113                            @Override
114                            public void onClick(View arg0) {        
115                                    arrivalBtn.setBackgroundResource(R.drawable.custom_button);
116                                    departureBtn.setBackgroundResource(R.drawable.custom_button_hilight);
117                                    metroBtn.setBackgroundResource(R.drawable.custom_button);
118                                    
119                                    getListView().setVisibility( View.VISIBLE );            
120                                    metroView.setVisibility( View.GONE );
121                                    arrival = false;
122                                    startDepartureFetcher();
123                            }
124                    });
125                    arrivalBtn.setOnClickListener( new OnClickListener() {
126                            @Override
127                            public void onClick(View arg0) {        
128                                    arrivalBtn.setBackgroundResource(R.drawable.custom_button_hilight);
129                                    departureBtn.setBackgroundResource(R.drawable.custom_button);
130                                    metroBtn.setBackgroundResource(R.drawable.custom_button);
131                                    
132                                    getListView().setVisibility( View.VISIBLE );            
133                                    metroView.setVisibility( View.GONE );
134                                    arrival = true;                
135                                    startDepartureFetcher();
136                            }
137                    });
138                    
139                    regionalBtn.setOnClickListener( new OnClickListener() {
140                            @Override
141                            public void onClick(View arg0) {        
142                                    regionalBtn.setBackgroundResource(R.drawable.custom_button_hilight);
143                                    stogBtn.setBackgroundResource(R.drawable.custom_button);                                
144                                    metroBtn.setBackgroundResource(R.drawable.custom_button);
145                                    
146                                    departureBtn.setVisibility( View.VISIBLE );
147                                    arrivalBtn.setVisibility( View.VISIBLE );
148                                    
149                                    getListView().setVisibility( View.VISIBLE );            
150                                    metroView.setVisibility( View.GONE );
151                                    trainType = "REGIONAL";
152                                    startDepartureFetcher();
153                            }
154                    });
155                    stogBtn.setOnClickListener( new OnClickListener() {
156                            @Override
157                            public void onClick(View arg0) {        
158                                    regionalBtn.setBackgroundResource(R.drawable.custom_button);
159                                    stogBtn.setBackgroundResource(R.drawable.custom_button_hilight);                                
160                                    metroBtn.setBackgroundResource(R.drawable.custom_button);
161    
162    
163                                    departureBtn.setVisibility( View.VISIBLE );
164                                    arrivalBtn.setVisibility( View.VISIBLE );
165                                    
166                                    getListView().setVisibility( View.VISIBLE );            
167                                    metroView.setVisibility( View.GONE );
168                                    trainType = "STOG";                    
169                                    startDepartureFetcher();
170                            }
171                    });
172    
173                    
174                    
175                    metroBtn.setOnClickListener( new OnClickListener() {                    
176                            @Override
177                            public void onClick(View v) {
178                                    regionalBtn.setBackgroundResource(R.drawable.custom_button);
179                                    stogBtn.setBackgroundResource(R.drawable.custom_button);                                
180                                    metroBtn.setBackgroundResource(R.drawable.custom_button_hilight);
181                                    
182                                    departureBtn.setVisibility( View.GONE );
183                                    arrivalBtn.setVisibility( View.GONE );
184                                    
185                                    getListView().setVisibility( View.GONE );              
186                                    metroView.setVisibility( View.VISIBLE );
187                                    startMetroFetcher();
188                            }
189                    });
190                    
191                    
192                    
193                    
194                    // findViewById(R.id.header).setOnClickListener( mapLauncher );
195                    
196                    int distance = station.getCalcdist();
197                    if (distance != 0) {
198                            NumberFormat format = NumberFormat.getNumberInstance();
199                            format.setMaximumFractionDigits(1);
200                            format.setMinimumFractionDigits(1);
201                                    
202                  String name = launchedBy.getStringExtra("name");                          ((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." );
203                  ((TextView) findViewById(R.id.stationName)).setText( name );                  } else {
204                            ((TextView) findViewById(R.id.stationDistance)).setVisibility(View.GONE);
205                    }
206            
207                    ProviderFactory.purgeOldEntries(); //cleanup before fetching more data
208                                    
209                  String addr = launchedBy.getStringExtra("address");                  Log.e("Station", StationEntryCsv.toCSV(station) );
                 ((TextView) findViewById(R.id.stationAddr)).setText( addr );  
210                                    
                 String code = launchedBy.getStringExtra("code");  
211                                    
                 findViewById(R.id.header).setOnClickListener( mapLauncher );  
212                                    
213                  NumberFormat format = NumberFormat.getNumberInstance();                  if (station.isMetro() == false) {
214                  format.setMaximumFractionDigits(1);                          metroBtn.setVisibility( View.GONE );
215                  format.setMinimumFractionDigits(1);                  }
                 int distance = launchedBy.getIntExtra("distance", 0);  
                 ((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." );  
216                    
217                    metro = ProviderFactory.getMetroProvider();
218                    
219                    if (station.isRegional() == false ) {
220                            regionalBtn.setVisibility(View.GONE);
221                    }
222                    
223                    if (station.isStrain() == false ) {
224                            stogBtn.setVisibility(View.GONE);
225                    }
226                                    
227                  provider = ProviderFactory.getDepartureProvider();                  if (station.isRegional() == true && station.isStrain() == false ) {
228                            if ( station.isMetro() == false )
229                                    regionalBtn.setVisibility(View.GONE);
230                            trainType = "REGIONAL";                
231                    }              
232    
233                    if (station.isRegional() == false  && station.isStrain() == true) {
234                            if (station.isMetro() == false)
235                                    stogBtn.setVisibility(View.GONE);
236    
237                            stogBtn.setBackgroundResource(R.drawable.custom_button_hilight);
238                            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                  fetcher = new DepartureFetcher();                  if (station.isRegional() == false && station.isStrain() == false) {
254                  if (savedInstanceState == null) {                          getListView().setVisibility( View.GONE );              
255                          showDialog(DLG_PROGRESS);                          metroView.setVisibility( View.VISIBLE );
256                          fetcher.execute(code);                          
257                            departureBtn.setVisibility( View.GONE );
258                            arrivalBtn.setVisibility(View.GONE);
259                            metroBtn.setVisibility( View.GONE );            
260                            
261                            
262                            
263                            if (savedInstanceState == null) {
264                                    startMetroFetcher();
265                            } else {
266                                    metroBean = (MetroBean) savedInstanceState.getSerializable("metro");
267                                    loadMetroData();
268                            }
269                            
270                  } else {                  } else {
271                          departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures");                          provider = ProviderFactory.getDepartureProvider();
272                          adapter.setDepartures(departures);                          
273                          selectedItemId = savedInstanceState.getInt("selectedItemId");                            if (savedInstanceState == null) {
274                          boolean detailsShowing = savedInstanceState.getBoolean("detailsShowing");                                  startDepartureFetcher();
275                          if (detailsShowing)                          } else {
276                                  showDialog(DLG_DETAILS);                                  departures = (DepartureBean) savedInstanceState.getSerializable("departures");
277                                    
278                                    if ( (departures != null) && (departures.entries != null) ) {
279                                            adapter.setDepartures(departures.entries);
280                                    }
281                                    selectedItemId = savedInstanceState.getInt("selectedItemId");
282                                    
283                                    if ( hasNotifications() ) {
284                                            findViewById(R.id.notifIcon).setVisibility(View.VISIBLE);
285                                    }
286                                    
287                            }
288                  }                  }
289          }          }
290                    
291            
292            
293            boolean hasNotifications() {
294                    return (departures != null && departures.notifications.size() > 0);
295            }
296            
297      @Override      @Override
298      public void onSaveInstanceState(Bundle outState)      public void onSaveInstanceState(Bundle outState)
299      {      {
300          if (pgDialog != null && pgDialog.isShowing())          if (pgDialog != null && pgDialog.isShowing())
301                  dismissDialog(DLG_PROGRESS);                  dismissDialog(DLG_PROGRESS);
302          boolean detailsShowing = (detailsDialog != null && detailsDialog.isShowing());  
         if (detailsShowing) {  
                 dismissDialog(DLG_DETAILS);  
         }  
         outState.putBoolean("detailsShowing", detailsShowing);  
303          outState.putInt("selectedItemId", selectedItemId);          outState.putInt("selectedItemId", selectedItemId);
304                    
305          outState.putSerializable("departures", (ArrayList<DepartureBean>) departures);          outState.putSerializable("departures",  departures);
306            outState.putSerializable("metro", metroBean);
307      }      }
308        
309        
310                    
311          @Override          @Override
312            protected void onDestroy() {
313                    super.onDestroy();
314                    
315                    if (fetcher != null) {
316                            fetcher.cancel(true);
317                    }
318                    
319                    if (metroFetcher != null) {
320                            metroFetcher.cancel(true);
321                    }
322            }
323    
324            @Override
325          protected void onListItemClick(ListView l, View v, int position, long id) {          protected void onListItemClick(ListView l, View v, int position, long id) {
326                  super.onListItemClick(l, v, position, id);                  super.onListItemClick(l, v, position, id);
327                    
328                    //how can this happen ??
329                    if (departures == null || departures.entries == null || departures.entries.size() == 0) {
330                            Toast.makeText(this, "No departures in list ?!?", Toast.LENGTH_LONG).show(); //TODO: translate
331                            return;
332                    }
333                    
334                    selectedItemId = position;
335                    
336                    DepartureEntry dep = departures.entries.get(selectedItemId);
337                    
338                  selectedItemId = position;                                Intent intent = new Intent(this, TimetableList.class);
339                  showDialog(DLG_DETAILS);                                  intent.putExtra("departure", dep);
340                    
341                    startActivity(intent);
342                                    
343          }          }
344                    
345    
# Line 112  public class DepartureList extends ListA Line 348  public class DepartureList extends ListA
348                  super.onPrepareDialog(id, dialog);                  super.onPrepareDialog(id, dialog);
349                                    
350                  switch (id) {                  switch (id) {
                 case DLG_DETAILS:  
                         DepartureBean currentDeparture = departures.get(selectedItemId);  
                         ((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime());  
                         ((TextView)dialog.findViewById(R.id.Train)).setText(currentDeparture.getTrainNumber());  
                         ((TextView)dialog.findViewById(R.id.Destination)).setText( currentDeparture.getDestination());  
                         ((TextView)dialog.findViewById(R.id.Origin)).setText(currentDeparture.getOrigin());  
                         ((TextView)dialog.findViewById(R.id.Location)).setText(currentDeparture.getLocation());  
                         ((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString());  
                         ((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus());  
                         ((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote());  
                         detailsDialog = dialog;  
                         break;  
351                  case DLG_PROGRESS:                  case DLG_PROGRESS:
352                          pgDialog = (ProgressDialog) dialog;                          pgDialog = (ProgressDialog) dialog;
353                            int messageId = arrival == false ? departurelist_fetchdepartures : departurelist_fetcharrivals;
354                            pgDialog.setMessage( getString(messageId) );
355                          break;                          break;
356                  }                  }
357          }          }
# Line 134  public class DepartureList extends ListA Line 360  public class DepartureList extends ListA
360          protected Dialog onCreateDialog(int id) {          protected Dialog onCreateDialog(int id) {
361                  switch (id) {                  switch (id) {
362                  case DLG_PROGRESS:                  case DLG_PROGRESS:
363    
364                          ProgressDialog dlg = new ProgressDialog(this);                          ProgressDialog dlg = new ProgressDialog(this);
                         dlg.setMessage("Fetch departure data");  
365                          dlg.setCancelable(true);                          dlg.setCancelable(true);
366                          return dlg;                          return dlg;                    
                 case DLG_DETAILS:  
                         //Context mContext = getApplicationContext();  
                         Dialog dialog = new Dialog(this);  
                         dialog.setCancelable(true);  
   
                         dialog.setContentView(R.layout.departuredetails);  
                         dialog.setTitle("Departure details");  
   
                         View root = dialog.findViewById(R.id.layout_root);  
                         root.setOnClickListener( new DialogDismisser(dialog) );  
                         return dialog;                    
367                  default:                  default:
368                          return super.onCreateDialog(id);                                          return super.onCreateDialog(id);                
369                  }                  }
370          }          }
371            
372            
373    
374            @Override
375            public boolean onPrepareOptionsMenu(Menu menu) {                
376                    super.onPrepareOptionsMenu(menu);
377    
378                    ///////////////////////
379                    
380                    MenuItem item = menu.findItem( MENU_NOTIFICATIONS );
381                    boolean notifEnabled = hasNotifications();
382                    item.setEnabled(notifEnabled);
383    
384                    //////////////////////////
385    
386    
387                    
388                    
389                    return true;
390            }
391    
392            @Override
393            public boolean onCreateOptionsMenu(Menu menu) {
394                    MenuItem item;
395                    
396                    
397                    item = menu.add(0, MENU_TOGGLEDETAILS, 0, getString(R.string.departurelist_toggledetails));
398                    item.setIcon(android.R.drawable.ic_menu_view);
399                    
400                    item = menu.add(0, MENU_MAP, 0, getString(R.string.departurelist_showonmap) );
401                    item.setIcon(android.R.drawable.ic_menu_mapmode);
402                    
403                    item = menu.add(0, MENU_NOTIFICATIONS, 0, getString(R.string.departurelist_notifications) );
404                    item.setIcon(android.R.drawable.ic_menu_info_details);                  
405                    
406                    boolean notifEnabled = hasNotifications();
407                    item.setEnabled(notifEnabled);
408    
409                    ///////////////////////////////////////
410                    if ( ! favorites.hasFavorite( this.station.getId() ) ) {
411                            item = menu.add(0, MENU_FAVORITES_ADD, 0, getString(dk.thoerup.traininfo.R.string.stationlist_addfavorite) );
412                    } else {
413                            item = menu.add(0, MENU_FAVORITES_REMOVE, 0, getString(dk.thoerup.traininfo.R.string.stationlist_removefavorite) );
414                    }
415                    /////////////////////////////
416                    
417                    if (station.isMetro()) {
418                            item = menu.add(0, MENU_METROMAP, 0, "Metro" ); //TODO:translate!?!
419                            item.setIcon(android.R.drawable.ic_menu_mapmode);                      
420                    }                                              
421    
422                    return true;
423            }
424    
425            @Override
426            public boolean onOptionsItemSelected(MenuItem item) {          
427                    boolean res = true;
428                    switch(item.getItemId()) {
429                    case MENU_MAP:
430                            Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude() + "?z=16");
431                            
432                            try {                          
433                                    startActivity( new Intent(Intent.ACTION_VIEW, uri));
434                            } catch (ActivityNotFoundException anfe) {
435                                     Toast.makeText(this, "Could not launch google maps", Toast.LENGTH_LONG).show();
436                            }
437                            
438                            break;
439                    case MENU_NOTIFICATIONS:
440                            Intent i = new Intent(this,dk.thoerup.traininfo.NotificationList.class);
441                            i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications);
442                            startActivity(i);                      
443                            break;
444                    case MENU_METROMAP:
445                            Intent metroMap = new Intent(this,dk.thoerup.traininfo.MetroMap.class);
446                            startActivity(metroMap);                        
447                            break;                  
448                    case MENU_TOGGLEDETAILS:
449                            adapter.toggleShowDetails();                                            
450                            break;
451                    case MENU_FAVORITES_ADD:
452                            favorites.addFavorite( this.station.getId() );
453                            Toast.makeText(this, getString(dk.thoerup.traininfo.R.string.stationlist_stationadded), Toast.LENGTH_SHORT).show();
454                            break;
455                    case MENU_FAVORITES_REMOVE:
456                            favorites.removeFavorite( station.getId() );
457                            Toast.makeText(this, getString(dk.thoerup.traininfo.R.string.stationlist_stationremoved), Toast.LENGTH_SHORT).show();
458                            break;
459                    default:
460                            res = super.onOptionsItemSelected(item);
461                    }
462                    return res;
463            }
464    
465            void startDepartureFetcher() {
466                    showDialog(DLG_PROGRESS);
467                    fetcher = new DepartureFetcher();
468                    fetcher.execute(station.getId());
469            }
470            
471            void startMetroFetcher() {
472                    showDialog(DLG_PROGRESS);
473                    metroFetcher = new MetroFetcher();
474                    metroFetcher.execute(station.getId());          
475            }
476            
477          class DialogDismisser implements View.OnClickListener {          class DialogDismisser implements View.OnClickListener {
478    
479                  Dialog dlg;                  Dialog dlg;
# Line 165  public class DepartureList extends ListA Line 485  public class DepartureList extends ListA
485                  public void onClick(View v) {                  public void onClick(View v) {
486                          if (dlg.isShowing())                          if (dlg.isShowing())
487                                  dlg.dismiss();                                  dlg.dismiss();
488                  }                  }      
489          }          }
490                    
491          View.OnClickListener mapLauncher = new View.OnClickListener() {          /*View.OnClickListener mapLauncher = new View.OnClickListener() {
492                  @Override                  @Override
493                  public void onClick(View v) {                  public void onClick(View v) {                  
494                          Uri uri = Uri.parse("geo:" + latitude + "," + longitude);                          Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude());
495                          startActivity( new Intent(Intent.ACTION_VIEW, uri));                          startActivity( new Intent(Intent.ACTION_VIEW, uri));
496                  }                  }
497          };          };*/
498    
499    
500                    
501            class DepartureFetcher extends AsyncTask<Integer, Void, Void> {
502    
503                    @Override
504                    protected void onPostExecute(Void result) {
505                            super.onPostExecute(result);
506                            
507                            
508                            pgDialog.dismiss();
509    
510                            if (departures != null && departures.errorCode == null) {
511                                    commFailCounter = 0;
512                                    DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html
513                                    adapter.setDepartures(departures.entries);
514                                    DepartureList.this.getListView().setVisibility(View.VISIBLE);
515                                    
516                                    
517                                    // handle notification icon.
518                                    View notifIcon = findViewById(R.id.notifIcon);
519                                    if ( hasNotifications() ) {                                    
520                                            notifIcon.setVisibility(View.VISIBLE);
521                                            notifIcon.setClickable(true);
522                                            notifIcon.setOnClickListener( new View.OnClickListener() {                                              
523                                                    @Override
524                                                    public void onClick(View v) {
525                                                            Intent i = new Intent(DepartureList.this, dk.thoerup.traininfo.NotificationList.class);
526                                                            i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications);
527                                                            startActivity(i);                                                      
528                                                    }
529                                            });
530                                    } else {
531                                            notifIcon.setVisibility(View.INVISIBLE);
532                                    }
533                                    
534                                    if (departures.entries.size() == 0) {
535                                            int msgId = (arrival==false) ? R.string.departurelist_nodepartures : R.string.departurelist_noarrivals;
536                                            MessageBox.showMessage(DepartureList.this, getString(msgId), false);
537                                    }
538                            } else { // communication or parse error
539                                    commFailCounter++;
540                                    AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this);
541                                    
542                                    if (departures != null && departures.errorCode != null ) { //got an error xml back
543                                            commFailCounter = 10;
544                                            builder.setMessage( getString(R.string.no_backend) );
545                                    } else {                                        
546                                            builder.setMessage( getString(R.string.departurelist_fetcherror) );            
547                                    }
548                                    builder.setCancelable(true);
549                                    
550                                    if (commFailCounter < 3) {
551                                            builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
552                                                    public void onClick(DialogInterface dialog, int id) {
553                                                            dialog.dismiss();
554                                                            startDepartureFetcher();
555                                                            
556                                                    }
557                                            });
558                                    }
559                                    builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
560                                            public void onClick(DialogInterface dialog, int id) {
561                                                    dialog.dismiss();
562                                                    DepartureList.this.finish();
563                                            }                                                      
564                                    });
565                                    
566                                    try { //TODO: is this still necessary after the 0.9.4.1 fix ?
567                                            builder.show();
568                                    } catch (android.view.WindowManager.BadTokenException e) {                                      
569                                            Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running
570                                    }                              
571                            }
572                    }
573    
574                    @Override
575                    protected Void doInBackground(Integer... params) {
576                            departures = provider.lookupDepartures(params[0], DepartureList.this.arrival, trainType);
577                            return null;
578                    }
579                    
580            }
581                    
582          class DepartureFetcher extends AsyncTask<String, Void, Void> {          public void loadMetroData() {
583                    ((TextView) findViewById(R.id.operations)).setText( metroBean.operationInfo );
584                    ((TextView) findViewById(R.id.plan)).setText( metroBean.plan );
585                    
586                    
587                    TableLayout table = (TableLayout) findViewById(R.id.metrotable);
588                    table.removeAllViews();
589                    
590                    TableRow head = new TableRow(this);
591                    
592                    TextView h1 = new TextView(this);
593                    h1.setText("Metro");
594                    h1.setTextSize(16);
595                    h1.setTypeface( Typeface.defaultFromStyle(Typeface.BOLD));
596                    
597                    
598                    TableRow.LayoutParams params = new TableRow.LayoutParams();
599                    params.span = 2;
600                    head.addView(h1, params);
601                    
602                    
603                    
604                    TextView h2 = new TextView(this);
605                    h2.setTextSize(16);
606                    h2.setTypeface( Typeface.defaultFromStyle(Typeface.BOLD));
607                    h2.setText("Om minutter");
608                    
609                    params = new TableRow.LayoutParams();
610                    params.weight = 2;
611                    head.addView(h2,params);
612    
613                    
614                    
615                    table.addView(head);
616    
617                    for (MetroEntry entry : metroBean.entries) {
618                            TableRow row = new TableRow(this);
619                            
620                            Log.e("Test", "" + entry.destination);
621                            
622                            TextView v1 = new TextView(this);
623                            v1.setTextSize(16);
624                            v1.setText( entry.metro );                      
625                            row.addView(v1);
626                            
627                            TextView v2 = new TextView(this);
628                            v2.setTextSize(16);
629                            v2.setText( entry.destination );
630                            row.addView(v2);
631                    
632                            TextView v3 = new TextView(this);
633                            v3.setTextSize(16);
634                            v3.setText( entry.minutes );
635                            row.addView(v3);
636                            
637                            table.addView(row);
638                            
639                    }
640                    findViewById(R.id.rootView).requestLayout();
641            }
642            
643            class MetroFetcher extends AsyncTask<Integer, Void, Void> {
644                    
645                  @Override                  @Override
646                  protected void onPostExecute(Void result) {                  protected void onPostExecute(Void result) {
647                          super.onPostExecute(result);                          super.onPostExecute(result);
648                                                    
649                          adapter.setDepartures(departures);                          
650                            
651                          pgDialog.dismiss();                          pgDialog.dismiss();
652                                                    
653                          if (departures.size() == 0)                          if (metroBean != null) {
654                                  MessageBox.showMessage(DepartureList.this, "No departures found");                                  loadMetroData();
655                            } else { // communication or parse error
656                                    commFailCounter++;
657                                    AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this);                                              
658                                    builder.setMessage("Error finding metro data");
659                                    builder.setCancelable(true);
660                                    if (commFailCounter < 3) {
661                                            builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
662                                                    public void onClick(DialogInterface dialog, int id) {
663                                                            dialog.dismiss();
664                                                            startMetroFetcher();
665                                                            
666                                                    }
667                                            });
668                                    }
669                                    builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
670                                            public void onClick(DialogInterface dialog, int id) {
671                                                    dialog.dismiss();
672                                                    DepartureList.this.finish(); //TODO: should we really close the activity ??
673                                            }                                                      
674                                    });
675                                    
676                                    try { //TODO: is this still necessary after the 0.9.4.1 fix ?
677                                            builder.show();
678                                    } catch (android.view.WindowManager.BadTokenException e) {                                      
679                                            Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running
680                                    }                              
681                            }
682                  }                  }
683    
684                  @Override                  @Override
685                  protected Void doInBackground(String... params) {                  protected Void doInBackground(Integer... params) {                      
686                          provider.lookupDepartures(params[0]);                          metroBean = metro.lookupMetroInfo(params[0]);
                         departures = provider.getDepartures();  
687                          return null;                          return null;
688                  }                  }
689                                    
690          }          }
691    
692  }  }

Legend:
Removed from v.294  
changed lines
  Added in v.1709

  ViewVC Help
Powered by ViewVC 1.1.20