/[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 1258 by torben, Mon Apr 4 15:47:06 2011 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfo;  package dk.thoerup.traininfo;
2    
3    
4  import java.util.ArrayList;  import static dk.thoerup.traininfo.R.string.generic_cancel;
5  import java.util.List;  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.regex.Pattern;
11    
12  import android.app.AlertDialog;  import android.app.AlertDialog;
13  import android.app.Dialog;  import android.app.Dialog;
# Line 13  import android.content.Intent; Line 18  import android.content.Intent;
18  import android.os.AsyncTask;  import android.os.AsyncTask;
19  import android.os.Bundle;  import android.os.Bundle;
20  import android.util.Log;  import android.util.Log;
21    import android.view.View;
22    import android.widget.ListView;
23  import android.widget.TextView;  import android.widget.TextView;
24    import dk.thoerup.android.traininfo.common.DepartureEntry;
25    import dk.thoerup.android.traininfo.common.StationBean.StationEntry;
26    import dk.thoerup.android.traininfo.common.TimetableBean;
27    import dk.thoerup.android.traininfo.common.TimetableEntry;
28  import dk.thoerup.traininfo.provider.ProviderFactory;  import dk.thoerup.traininfo.provider.ProviderFactory;
29  import dk.thoerup.traininfo.provider.TimetableProvider;  import dk.thoerup.traininfo.provider.TimetableProvider;
30  import dk.thoerup.traininfo.util.MessageBox;  import dk.thoerup.traininfo.util.MessageBox;
# Line 21  import dk.thoerup.traininfo.util.Message Line 32  import dk.thoerup.traininfo.util.Message
32  public class TimetableList extends ListActivity {  public class TimetableList extends ListActivity {
33                    
34          private static final int DLG_PROGRESS = 8000;          private static final int DLG_PROGRESS = 8000;
35          DepartureBean departure;          DepartureEntry departure;
36          TimetableListAdapter adapter;          TimetableListAdapter adapter;
37          TimetableFetcher fetcher;          TimetableFetcher fetcher;
38          List<TimetableBean> timetables;          TimetableBean timetables;
39            int commFailCounter = 0;
40                    
41          TimetableProvider provider;          TimetableProvider provider;
42    
43          @SuppressWarnings("unchecked")  
44          @Override          @Override
45          protected void onCreate(Bundle savedInstanceState) {          protected void onCreate(Bundle savedInstanceState) {
46                  super.onCreate(savedInstanceState);                  super.onCreate(savedInstanceState);
# Line 36  public class TimetableList extends ListA Line 48  public class TimetableList extends ListA
48                                    
49                  provider = ProviderFactory.getTimetableProvider();                  provider = ProviderFactory.getTimetableProvider();
50                                    
51                  adapter = new TimetableListAdapter(this);                  adapter = new TimetableListAdapter(this);              
52                  setListAdapter(adapter);                  setListAdapter(adapter);
53    
54    
55                                    
56                  Intent launchedBy = getIntent();                  Intent launchedBy = getIntent();
57                  departure = (DepartureBean) launchedBy.getSerializableExtra("departure");                  departure = (DepartureEntry) launchedBy.getSerializableExtra("departure");
58                                    
59                  ((TextView)findViewById(R.id.Train)).setText(departure.getTrainNumber());                  ((TextView)findViewById(R.id.Train)).setText(departure.getTrainNumber());
60                  ((TextView)findViewById(R.id.Status)).setText(departure.getStatus());                                  
61                  ((TextView)findViewById(R.id.Note)).setText(departure.getNote());                  ((TextView)findViewById(R.id.Location)).setText(departure.getLocation());              
62                  ((TextView)findViewById(R.id.Updated)).setText(departure.getLastUpdateString());                  ((TextView)findViewById(R.id.Updated)).setText( getLastUpdateString( departure.getUpdated() ));
63                    
64                    String status = departure.getStatus() != null ? departure.getStatus() : "";            
65                    if ( Pattern.matches("[0-9]+.+min", status) ) {
66                            status += " " + this.getString(R.string.departurebean_delayed);
67                    }
68                    String note = departure.getNote() != null ? departure.getNote() : "";
69                    note = note.replace("Aflyst", this.getString(R.string.timetablelist_cancelled) );
70                    note = note.replace("Kører kun til", this.getString(R.string.timetablelist_goesonlyto) );
71                    note = note.replace("Afgår fra", this.getString(R.string.timetablelist_startsat) );
72                    note = note.replace("Erstattet af", this.getString(R.string.timetablelist_replacedby) );
73                    note = note.replace("Eksterne forhold", this.getString(R.string.timetablelist_externalconditions) );
74                    note = note.replace("Teknisk fejl på et signal", this.getString(R.string.timetablelist_technicalerroronsignal) );
75                    
76                    // tilføj:
77                    // * teknisk fejl på et signal
78    
79                    
80                    
81                    ((TextView)findViewById(R.id.Status)).setText(status);
82                    ((TextView)findViewById(R.id.Note)).setText(note);
83                    
84    
85                                    
86                                    
87                                                    
88                    
89                    ProviderFactory.purgeOldEntries(); //cleanup before fetching more data
90                    
91                  if (savedInstanceState == null) {                  if (savedInstanceState == null) {
92                          startTimetableFetcher();                          startTimetableFetcher();
93                  } else {                  } else {
94                          timetables = (List<TimetableBean>) savedInstanceState.getSerializable("timetables");                          timetables = (TimetableBean) savedInstanceState.getSerializable("timetables");
95                          adapter.setTimetable(timetables);                          adapter.setTimetable(timetables);
96                  }                  }
97          }          }
98    
99            @Override
100            protected void onDestroy() {
101                    super.onDestroy();
102                    
103                    if (fetcher != null) {
104                            fetcher.cancel(true);
105                    }
106            }
107                    
108      @Override      @Override
109            protected void onListItemClick(ListView l, View v, int position, long id) {
110                    super.onListItemClick(l, v, position, id);
111    
112                    TimetableEntry tt = timetables.entries.get(position);
113                    
114                    StationEntry station = new StationEntry();
115                    station.setName( tt.getStation() );
116                    station.setId( tt.getStationId() );
117                    station.setIsRegional(true);
118                    
119                    
120                    Intent intent = new Intent(this, DepartureList.class);
121                    intent.putExtra("stationbean", station);
122                    startActivity(intent);
123                    
124            }
125        
126    
127    
128            @Override
129      public void onSaveInstanceState(Bundle outState)      public void onSaveInstanceState(Bundle outState)
130      {                {          
131          dismissDialog(DLG_PROGRESS);          dismissDialog(DLG_PROGRESS);
132          outState.putSerializable("timetables", (ArrayList<TimetableBean>) timetables);          outState.putSerializable("timetables", (TimetableBean) timetables);
133      }      }
134    
 /*              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;  
   
  */  
135                    
136          @Override          @Override
137          protected void onPrepareDialog(int id, Dialog dialog) {          protected void onPrepareDialog(int id, Dialog dialog) {
# Line 93  public class TimetableList extends ListA Line 148  public class TimetableList extends ListA
148          protected Dialog onCreateDialog(int id) {          protected Dialog onCreateDialog(int id) {
149                  switch (id) {                  switch (id) {
150                  case DLG_PROGRESS:                  case DLG_PROGRESS:
151                          ProgressDialog dlg = new ProgressDialog(this);                          ProgressDialog dlg = new ProgressDialog(this);                  
152                          dlg.setMessage("Fetch timetable data");                          dlg.setMessage( getString(timetablelist_fetchdata) );
153                          dlg.setCancelable(true);                          dlg.setCancelable(true);
154                          return dlg;                                              return dlg;                    
155                  default:                  default:
# Line 105  public class TimetableList extends ListA Line 160  public class TimetableList extends ListA
160          void startTimetableFetcher() {          void startTimetableFetcher() {
161                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
162                  fetcher = new TimetableFetcher();                  fetcher = new TimetableFetcher();
163                  fetcher.execute(departure.getTrainNumber());                  fetcher.execute(departure.getType(), departure.getTrainNumber());
164          }          }
165                    
166            
167            public String getLastUpdateString(int lastUpdate) {
168                    String minutes = this.getString(R.string.departurebean_minutes);
169                    String unknown = this.getString(R.string.departurebean_unknown);
170                    switch (lastUpdate) {
171                    case 1:
172                            return "<3 " + minutes;
173                    case 2:
174                            return "3-10 " + minutes;
175                    case 3:
176                            return ">3 " + minutes;
177                    case 4:
178                            return unknown;
179                    default:
180                            return "";
181                    }
182            }
183    
184            
185          class TimetableFetcher extends AsyncTask<String,Void,Void> {          class TimetableFetcher extends AsyncTask<String,Void,Void> {
186                                    
                 boolean success;  
187                                    
188                  @Override                  @Override
189                  protected void onPostExecute(Void result) {                  protected void onPostExecute(Void result) {
# Line 118  public class TimetableList extends ListA Line 191  public class TimetableList extends ListA
191                          dismissDialog(DLG_PROGRESS);                          dismissDialog(DLG_PROGRESS);
192                    
193                                                    
194                          if (success) {                          if (timetables != null) {
195                                    commFailCounter = 0;
196                                    TimetableList.this.getListView().invalidateViews();
197                                  adapter.setTimetable(timetables);                                  adapter.setTimetable(timetables);
198                                  if (timetables.size() == 0) {                                  if (timetables.entries.size() == 0) {
199                                          MessageBox.showMessage(TimetableList.this, "No timetable found");                                          MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata), true);
200                                  }                                  }
201                          } else { // communication or parse error                          } else { // communication or parse error
202                                    commFailCounter++;
203                                  AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this);                                                                                AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this);                                              
204                                  builder.setMessage("Error finding departures");                                  builder.setMessage(getString(timetablelist_fetcherror));
205                                  builder.setCancelable(true);                                  builder.setCancelable(true);
206                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                  if (commFailCounter < 3) {
207                                          public void onClick(DialogInterface dialog, int id) {                                          builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
208                                                  dialog.dismiss();                                                  public void onClick(DialogInterface dialog, int id) {
209                                                  startTimetableFetcher();                                                          dialog.dismiss();
210                                                                                                            startTimetableFetcher();
211                                          }                                                          
212                                  });                                                  }
213                                  builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                                          });
214                                    }
215                                    builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
216                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
217                                                  dialog.dismiss();                                                  dialog.dismiss();
218                                                    TimetableList.this.finish();
219                                          }                                                                                                }                                                      
220                                  });                                                                                                                              });
221                                  builder.show();                                                          
222                                    try {
223                                            builder.show();
224                                    } catch (android.view.WindowManager.BadTokenException e) {                                      
225                                            Log.i("TimetableList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running
226                                    }
227                                                                    
228                          }                          }
229                                                    
230                  }                  }
231                                    
232                  @Override                  @Override
233                  protected Void doInBackground(String... arg0) {                  protected Void doInBackground(String... arg0) {
234                          String trainID = arg0[0];                          String type = arg0[0];
235                          success = provider.lookupTimetable(trainID);                          String trainID = arg0[1];
236                          timetables = provider.getTimetable();                          timetables = provider.lookupTimetable(type, trainID);
237                                                    
238                          return null;                          return null;
239                  }                  }

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

  ViewVC Help
Powered by ViewVC 1.1.20