/[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 365 by torben, Wed Sep 30 09:14:27 2009 UTC revision 1028 by torben, Wed Sep 8 06:25:13 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfo;  package dk.thoerup.traininfo;
2    
3    
4    import static dk.thoerup.traininfo.R.string.generic_cancel;
5    import static dk.thoerup.traininfo.R.string.generic_retry;
6    import static dk.thoerup.traininfo.R.string.timetablelist_fetchdata;
7    import static dk.thoerup.traininfo.R.string.timetablelist_fetcherror;
8    import static dk.thoerup.traininfo.R.string.timetablelist_nodata;
9    
10  import java.util.ArrayList;  import java.util.ArrayList;
11  import java.util.List;  import java.util.List;
12    
# Line 13  import android.content.Intent; Line 19  import android.content.Intent;
19  import android.os.AsyncTask;  import android.os.AsyncTask;
20  import android.os.Bundle;  import android.os.Bundle;
21  import android.util.Log;  import android.util.Log;
22    import android.view.View;
23    import android.widget.ListView;
24  import android.widget.TextView;  import android.widget.TextView;
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;
# Line 21  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;          List<TimetableBean> timetables;
36            int commFailCounter = 0;
37                    
38          TimetableProvider provider;          TimetableProvider provider;
39    
# Line 36  public class TimetableList extends ListA Line 45  public class TimetableList extends ListA
45                                    
46                  provider = ProviderFactory.getTimetableProvider();                  provider = ProviderFactory.getTimetableProvider();
47                                    
48                  adapter = new TimetableListAdapter(this);                  adapter = new TimetableListAdapter(this);              
49                  setListAdapter(adapter);                  setListAdapter(adapter);
50    
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());
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());                  ((TextView)findViewById(R.id.Updated)).setText(departure.getLastUpdateString(this));
61                                                                    
62                    ProviderFactory.purgeOldEntries(); //cleanup before fetching more data
63                                    
64                  if (savedInstanceState == null) {                  if (savedInstanceState == null) {
65                          startTimetableFetcher();                          startTimetableFetcher();
# Line 55  public class TimetableList extends ListA Line 68  public class TimetableList extends ListA
68                          adapter.setTimetable(timetables);                          adapter.setTimetable(timetables);
69                  }                  }
70          }          }
71    
72            @Override
73            protected void onDestroy() {
74                    super.onDestroy();
75                    
76                    if (fetcher != null) {
77                            fetcher.cancel(true);
78                    }
79            }
80                    
81      @Override      @Override
82            protected void onListItemClick(ListView l, View v, int position, long id) {
83                    super.onListItemClick(l, v, position, id);
84    
85                    TimetableBean tt = timetables.get(position);
86                    
87                    StationBean station = new StationBean();
88                    station.setName( tt.getStation() );
89                    station.setId( tt.getStationId() );
90                    station.setRegional(true);
91                    
92                    Intent intent = new Intent(this, DepartureList.class);
93                    intent.putExtra("stationbean", station);
94                    startActivity(intent);
95                    
96            }
97        
98    
99    
100            @Override
101      public void onSaveInstanceState(Bundle outState)      public void onSaveInstanceState(Bundle outState)
102      {                {          
103          dismissDialog(DLG_PROGRESS);          dismissDialog(DLG_PROGRESS);
104          outState.putSerializable("timetables", (ArrayList<TimetableBean>) timetables);          outState.putSerializable("timetables", (ArrayList<TimetableBean>) timetables);
105      }      }
106    
 /*              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;  
   
  */  
107                    
108          @Override          @Override
109          protected void onPrepareDialog(int id, Dialog dialog) {          protected void onPrepareDialog(int id, Dialog dialog) {
# Line 93  public class TimetableList extends ListA Line 120  public class TimetableList extends ListA
120          protected Dialog onCreateDialog(int id) {          protected Dialog onCreateDialog(int id) {
121                  switch (id) {                  switch (id) {
122                  case DLG_PROGRESS:                  case DLG_PROGRESS:
123                          ProgressDialog dlg = new ProgressDialog(this);                          ProgressDialog dlg = new ProgressDialog(this);                  
124                          dlg.setMessage("Fetch timetable data");                          dlg.setMessage( getString(timetablelist_fetchdata) );
125                          dlg.setCancelable(true);                          dlg.setCancelable(true);
126                          return dlg;                                              return dlg;                    
127                  default:                  default:
# Line 105  public class TimetableList extends ListA Line 132  public class TimetableList extends ListA
132          void startTimetableFetcher() {          void startTimetableFetcher() {
133                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
134                  fetcher = new TimetableFetcher();                  fetcher = new TimetableFetcher();
135                  fetcher.execute(departure.getTrainNumber());                  fetcher.execute(departure.getType(), departure.getTrainNumber());
136          }          }
137                    
138          class TimetableFetcher extends AsyncTask<String,Void,Void> {          class TimetableFetcher extends AsyncTask<String,Void,Void> {
139                                    
                 boolean success;  
140                                    
141                  @Override                  @Override
142                  protected void onPostExecute(Void result) {                  protected void onPostExecute(Void result) {
# Line 118  public class TimetableList extends ListA Line 144  public class TimetableList extends ListA
144                          dismissDialog(DLG_PROGRESS);                          dismissDialog(DLG_PROGRESS);
145                    
146                                                    
147                          if (success) {                          if (timetables != null) {
148                                    commFailCounter = 0;
149                                    TimetableList.this.getListView().invalidateViews();
150                                  adapter.setTimetable(timetables);                                  adapter.setTimetable(timetables);
151                                  if (timetables.size() == 0) {                                  if (timetables.size() == 0) {
152                                          MessageBox.showMessage(TimetableList.this, "No timetable found");                                          MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata), true);
153                                  }                                  }
154                          } else { // communication or parse error                          } else { // communication or parse error
155                                    commFailCounter++;
156                                  AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this);                                                                                AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this);                                              
157                                  builder.setMessage("Error finding departures");                                  builder.setMessage(getString(timetablelist_fetcherror));
158                                  builder.setCancelable(true);                                  builder.setCancelable(true);
159                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                  if (commFailCounter < 3) {
160                                          public void onClick(DialogInterface dialog, int id) {                                          builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
161                                                  dialog.dismiss();                                                  public void onClick(DialogInterface dialog, int id) {
162                                                  startTimetableFetcher();                                                          dialog.dismiss();
163                                                                                                            startTimetableFetcher();
164                                          }                                                          
165                                  });                                                  }
166                                  builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                                          });
167                                    }
168                                    builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
169                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
170                                                  dialog.dismiss();                                                  dialog.dismiss();
171                                                    TimetableList.this.finish();
172                                          }                                                                                                }                                                      
173                                  });                                                                                                                              });
174                                  builder.show();                                                          
175                                    try {
176                                            builder.show();
177                                    } catch (android.view.WindowManager.BadTokenException e) {                                      
178                                            Log.i("TimetableList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running
179                                    }
180                                                                    
181                          }                          }
182                                                    
183                  }                  }
184                                    
185                  @Override                  @Override
186                  protected Void doInBackground(String... arg0) {                  protected Void doInBackground(String... arg0) {
187                          String trainID = arg0[0];                          String type = arg0[0];
188                          success = provider.lookupTimetable(trainID);                          String trainID = arg0[1];
189                          timetables = provider.getTimetable();                          timetables = provider.lookupTimetable(type, trainID);
190                                                    
191                          return null;                          return null;
192                  }                  }

Legend:
Removed from v.365  
changed lines
  Added in v.1028

  ViewVC Help
Powered by ViewVC 1.1.20