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

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

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

revision 906 by torben, Fri Jun 25 23:59:58 2010 UTC revision 1066 by torben, Thu Sep 16 15:32:42 2010 UTC
# Line 6  import static dk.thoerup.traininfo.R.str Line 6  import static dk.thoerup.traininfo.R.str
6  import static dk.thoerup.traininfo.R.string.timetablelist_fetchdata;  import static dk.thoerup.traininfo.R.string.timetablelist_fetchdata;
7  import static dk.thoerup.traininfo.R.string.timetablelist_fetcherror;  import static dk.thoerup.traininfo.R.string.timetablelist_fetcherror;
8  import static dk.thoerup.traininfo.R.string.timetablelist_nodata;  import static dk.thoerup.traininfo.R.string.timetablelist_nodata;
   
 import java.util.ArrayList;  
 import java.util.List;  
   
9  import android.app.AlertDialog;  import android.app.AlertDialog;
10  import android.app.Dialog;  import android.app.Dialog;
11  import android.app.ListActivity;  import android.app.ListActivity;
# Line 22  import android.util.Log; Line 18  import android.util.Log;
18  import android.view.View;  import android.view.View;
19  import android.widget.ListView;  import android.widget.ListView;
20  import android.widget.TextView;  import android.widget.TextView;
21  import android.widget.Toast;  import dk.thoerup.android.traininfo.common.DepartureEntry;
22    import dk.thoerup.android.traininfo.common.TimetableBean;
23    import dk.thoerup.android.traininfo.common.TimetableEntry;
24    import dk.thoerup.android.traininfo.common.StationBean.StationEntry;
25  import dk.thoerup.traininfo.provider.ProviderFactory;  import dk.thoerup.traininfo.provider.ProviderFactory;
26  import dk.thoerup.traininfo.provider.TimetableProvider;  import dk.thoerup.traininfo.provider.TimetableProvider;
27  import dk.thoerup.traininfo.util.MessageBox;  import dk.thoerup.traininfo.util.MessageBox;
# Line 30  import dk.thoerup.traininfo.util.Message Line 29  import dk.thoerup.traininfo.util.Message
29  public class TimetableList extends ListActivity {  public class TimetableList extends ListActivity {
30                    
31          private static final int DLG_PROGRESS = 8000;          private static final int DLG_PROGRESS = 8000;
32          DepartureBean departure;          DepartureEntry departure;
33          TimetableListAdapter adapter;          TimetableListAdapter adapter;
34          TimetableFetcher fetcher;          TimetableFetcher fetcher;
35          List<TimetableBean> timetables;          TimetableBean timetables;
36            int commFailCounter = 0;
37                    
38          TimetableProvider provider;          TimetableProvider provider;
39    
# Line 51  public class TimetableList extends ListA Line 51  public class TimetableList extends ListA
51    
52                                    
53                  Intent launchedBy = getIntent();                  Intent launchedBy = getIntent();
54                  departure = (DepartureBean) launchedBy.getSerializableExtra("departure");                  departure = (DepartureEntry) launchedBy.getSerializableExtra("departure");
55                                    
56                  ((TextView)findViewById(R.id.Train)).setText(departure.getTrainNumber());                  ((TextView)findViewById(R.id.Train)).setText(departure.getTrainNumber());
57                  ((TextView)findViewById(R.id.Status)).setText(departure.getStatus());                  ((TextView)findViewById(R.id.Status)).setText(departure.getStatus());
58                  ((TextView)findViewById(R.id.Location)).setText(departure.getLocation());                  ((TextView)findViewById(R.id.Location)).setText(departure.getLocation());
59                  ((TextView)findViewById(R.id.Note)).setText(departure.getNote());                  ((TextView)findViewById(R.id.Note)).setText(departure.getNote());
60                  ((TextView)findViewById(R.id.Updated)).setText(departure.getLastUpdateString(this));                  ((TextView)findViewById(R.id.Updated)).setText( getLastUpdateString( departure.getUpdated() ));
61                                                                                                    
62                                    
63                    ProviderFactory.purgeOldEntries(); //cleanup before fetching more data
64                    
65                  if (savedInstanceState == null) {                  if (savedInstanceState == null) {
66                          startTimetableFetcher();                          startTimetableFetcher();
67                  } else {                  } else {
68                          timetables = (List<TimetableBean>) savedInstanceState.getSerializable("timetables");                          timetables = (TimetableBean) savedInstanceState.getSerializable("timetables");
69                          adapter.setTimetable(timetables);                          adapter.setTimetable(timetables);
70                  }                  }
71          }          }
72            
73            @Override
74            protected void onDestroy() {
75                    super.onDestroy();
76                    
77                    if (fetcher != null) {
78                            fetcher.cancel(true);
79                    }
80            }
81                    
82      @Override      @Override
83          protected void onListItemClick(ListView l, View v, int position, long id) {          protected void onListItemClick(ListView l, View v, int position, long id) {
84                  super.onListItemClick(l, v, position, id);                  super.onListItemClick(l, v, position, id);
85    
86                  TimetableBean tt = timetables.get(position);                  TimetableEntry tt = timetables.entries.get(position);
87                                    
88                  StationBean station = new StationBean();                  StationEntry station = new StationEntry();
89                  station.setName( tt.getStation() );                  station.setName( tt.getStation() );
90                  station.setId( tt.getStationId() );                  station.setId( tt.getStationId() );
91                  station.setRegional(true);                  station.setIsRegional(true);
92                    
93                                    
94                  Intent intent = new Intent(this, DepartureList.class);                  Intent intent = new Intent(this, DepartureList.class);
95                  intent.putExtra("stationbean", station);                  intent.putExtra("stationbean", station);
# Line 92  public class TimetableList extends ListA Line 103  public class TimetableList extends ListA
103      public void onSaveInstanceState(Bundle outState)      public void onSaveInstanceState(Bundle outState)
104      {                {          
105          dismissDialog(DLG_PROGRESS);          dismissDialog(DLG_PROGRESS);
106          outState.putSerializable("timetables", (ArrayList<TimetableBean>) timetables);          outState.putSerializable("timetables", (TimetableBean) timetables);
107      }      }
108    
109                    
# Line 126  public class TimetableList extends ListA Line 137  public class TimetableList extends ListA
137                  fetcher.execute(departure.getType(), departure.getTrainNumber());                  fetcher.execute(departure.getType(), departure.getTrainNumber());
138          }          }
139                    
140            
141            public String getLastUpdateString(int lastUpdate) {
142                    String minutes = this.getString(R.string.departurebean_minutes);
143                    String unknown = this.getString(R.string.departurebean_unknown);
144                    switch (lastUpdate) {
145                    case 1:
146                            return "<3 " + minutes;
147                    case 2:
148                            return "3-10 " + minutes;
149                    case 3:
150                            return ">3 " + minutes;
151                    case 4:
152                            return unknown;
153                    default:
154                            return "";
155                    }
156            }
157    
158            
159          class TimetableFetcher extends AsyncTask<String,Void,Void> {          class TimetableFetcher extends AsyncTask<String,Void,Void> {
160                                    
                 boolean success;  
161                                    
162                  @Override                  @Override
163                  protected void onPostExecute(Void result) {                  protected void onPostExecute(Void result) {
# Line 136  public class TimetableList extends ListA Line 165  public class TimetableList extends ListA
165                          dismissDialog(DLG_PROGRESS);                          dismissDialog(DLG_PROGRESS);
166                    
167                                                    
168                          if (success) {                          if (timetables != null) {
169                                    commFailCounter = 0;
170                                    TimetableList.this.getListView().invalidateViews();
171                                  adapter.setTimetable(timetables);                                  adapter.setTimetable(timetables);
172                                  if (timetables.size() == 0) {                                  if (timetables.entries.size() == 0) {
173                                          MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata), true);                                          MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata), true);
174                                  }                                  }
175                          } else { // communication or parse error                          } else { // communication or parse error
176                                    commFailCounter++;
177                                  AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this);                                                                                AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this);                                              
178                                  builder.setMessage(getString(timetablelist_fetcherror));                                  builder.setMessage(getString(timetablelist_fetcherror));
179                                  builder.setCancelable(true);                                  builder.setCancelable(true);
180                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {                                  if (commFailCounter < 3) {
181                                          public void onClick(DialogInterface dialog, int id) {                                          builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
182                                                  dialog.dismiss();                                                  public void onClick(DialogInterface dialog, int id) {
183                                                  startTimetableFetcher();                                                          dialog.dismiss();
184                                                                                                            startTimetableFetcher();
185                                          }                                                          
186                                  });                                                  }
187                                            });
188                                    }
189                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
190                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
191                                                  dialog.dismiss();                                                  dialog.dismiss();
# Line 173  public class TimetableList extends ListA Line 207  public class TimetableList extends ListA
207                  protected Void doInBackground(String... arg0) {                  protected Void doInBackground(String... arg0) {
208                          String type = arg0[0];                          String type = arg0[0];
209                          String trainID = arg0[1];                          String trainID = arg0[1];
210                          success = provider.lookupTimetable(type, trainID);                          timetables = provider.lookupTimetable(type, trainID);
                         timetables = provider.getTimetable(type, trainID);  
211                                                    
212                          return null;                          return null;
213                  }                  }

Legend:
Removed from v.906  
changed lines
  Added in v.1066

  ViewVC Help
Powered by ViewVC 1.1.20