/[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 366 by torben, Wed Sep 30 10:20:05 2009 UTC revision 1257 by torben, Mon Apr 4 15:28:12 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    
 import android.app.Activity;  
12  import android.app.AlertDialog;  import android.app.AlertDialog;
13  import android.app.Dialog;  import android.app.Dialog;
14    import android.app.ListActivity;
15  import android.app.ProgressDialog;  import android.app.ProgressDialog;
16  import android.content.DialogInterface;  import android.content.DialogInterface;
17  import android.content.Intent;  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;
21    import android.view.View;
22  import android.widget.ListView;  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;
31    
32  public class TimetableList extends Activity {  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 Activ Line 48  public class TimetableList extends Activ
48                                    
49                  provider = ProviderFactory.getTimetableProvider();                  provider = ProviderFactory.getTimetableProvider();
50                                    
51                  adapter = new TimetableListAdapter(this);                  adapter = new TimetableListAdapter(this);              
52                                    setListAdapter(adapter);
53                  ListView lv = (ListView) findViewById(R.id.List);  
                 lv.setAdapter(adapter);  
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                    
72                    // tilføj:
73                    // * afgår fra
74                    // * eksterne forhold
75                    // * teknisk fejl på et signal
76    
77                    
78                    
79                    ((TextView)findViewById(R.id.Status)).setText(status);
80                    ((TextView)findViewById(R.id.Note)).setText(note);
81                    
82    
83                    
84                    
85                                                                                                    
86                                    
87                    ProviderFactory.purgeOldEntries(); //cleanup before fetching more data
88                    
89                  if (savedInstanceState == null) {                  if (savedInstanceState == null) {
90                          startTimetableFetcher();                          startTimetableFetcher();
91                  } else {                  } else {
92                          timetables = (List<TimetableBean>) savedInstanceState.getSerializable("timetables");                          timetables = (TimetableBean) savedInstanceState.getSerializable("timetables");
93                          adapter.setTimetable(timetables);                          adapter.setTimetable(timetables);
94                  }                  }
95          }          }
96    
97            @Override
98            protected void onDestroy() {
99                    super.onDestroy();
100                    
101                    if (fetcher != null) {
102                            fetcher.cancel(true);
103                    }
104            }
105                    
106      @Override      @Override
107            protected void onListItemClick(ListView l, View v, int position, long id) {
108                    super.onListItemClick(l, v, position, id);
109    
110                    TimetableEntry tt = timetables.entries.get(position);
111                    
112                    StationEntry station = new StationEntry();
113                    station.setName( tt.getStation() );
114                    station.setId( tt.getStationId() );
115                    station.setIsRegional(true);
116                    
117                    
118                    Intent intent = new Intent(this, DepartureList.class);
119                    intent.putExtra("stationbean", station);
120                    startActivity(intent);
121                    
122            }
123        
124    
125    
126            @Override
127      public void onSaveInstanceState(Bundle outState)      public void onSaveInstanceState(Bundle outState)
128      {                {          
129          dismissDialog(DLG_PROGRESS);          dismissDialog(DLG_PROGRESS);
130          outState.putSerializable("timetables", (ArrayList<TimetableBean>) timetables);          outState.putSerializable("timetables", (TimetableBean) timetables);
131      }      }
132    
 /*              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;  
   
  */  
133                    
134          @Override          @Override
135          protected void onPrepareDialog(int id, Dialog dialog) {          protected void onPrepareDialog(int id, Dialog dialog) {
# Line 96  public class TimetableList extends Activ Line 146  public class TimetableList extends Activ
146          protected Dialog onCreateDialog(int id) {          protected Dialog onCreateDialog(int id) {
147                  switch (id) {                  switch (id) {
148                  case DLG_PROGRESS:                  case DLG_PROGRESS:
149                          ProgressDialog dlg = new ProgressDialog(this);                          ProgressDialog dlg = new ProgressDialog(this);                  
150                          dlg.setMessage("Fetch timetable data");                          dlg.setMessage( getString(timetablelist_fetchdata) );
151                          dlg.setCancelable(true);                          dlg.setCancelable(true);
152                          return dlg;                                              return dlg;                    
153                  default:                  default:
# Line 108  public class TimetableList extends Activ Line 158  public class TimetableList extends Activ
158          void startTimetableFetcher() {          void startTimetableFetcher() {
159                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
160                  fetcher = new TimetableFetcher();                  fetcher = new TimetableFetcher();
161                  fetcher.execute(departure.getTrainNumber());                  fetcher.execute(departure.getType(), departure.getTrainNumber());
162          }          }
163                    
164            
165            public String getLastUpdateString(int lastUpdate) {
166                    String minutes = this.getString(R.string.departurebean_minutes);
167                    String unknown = this.getString(R.string.departurebean_unknown);
168                    switch (lastUpdate) {
169                    case 1:
170                            return "<3 " + minutes;
171                    case 2:
172                            return "3-10 " + minutes;
173                    case 3:
174                            return ">3 " + minutes;
175                    case 4:
176                            return unknown;
177                    default:
178                            return "";
179                    }
180            }
181    
182            
183          class TimetableFetcher extends AsyncTask<String,Void,Void> {          class TimetableFetcher extends AsyncTask<String,Void,Void> {
184                                    
                 boolean success;  
185                                    
186                  @Override                  @Override
187                  protected void onPostExecute(Void result) {                  protected void onPostExecute(Void result) {
# Line 121  public class TimetableList extends Activ Line 189  public class TimetableList extends Activ
189                          dismissDialog(DLG_PROGRESS);                          dismissDialog(DLG_PROGRESS);
190                    
191                                                    
192                          if (success) {                          if (timetables != null) {
193                                    commFailCounter = 0;
194                                    TimetableList.this.getListView().invalidateViews();
195                                  adapter.setTimetable(timetables);                                  adapter.setTimetable(timetables);
196                                  if (timetables.size() == 0) {                                  if (timetables.entries.size() == 0) {
197                                          MessageBox.showMessage(TimetableList.this, "No timetable found");                                          MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata), true);
198                                  }                                  }
199                          } else { // communication or parse error                          } else { // communication or parse error
200                                    commFailCounter++;
201                                  AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this);                                                                                AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this);                                              
202                                  builder.setMessage("Error finding departures");                                  builder.setMessage(getString(timetablelist_fetcherror));
203                                  builder.setCancelable(true);                                  builder.setCancelable(true);
204                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                  if (commFailCounter < 3) {
205                                          public void onClick(DialogInterface dialog, int id) {                                          builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
206                                                  dialog.dismiss();                                                  public void onClick(DialogInterface dialog, int id) {
207                                                  startTimetableFetcher();                                                          dialog.dismiss();
208                                                                                                            startTimetableFetcher();
209                                          }                                                          
210                                  });                                                  }
211                                  builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                                          });
212                                    }
213                                    builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
214                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
215                                                  dialog.dismiss();                                                  dialog.dismiss();
216                                                    TimetableList.this.finish();
217                                          }                                                                                                }                                                      
218                                  });                                                                                                                              });
219                                  builder.show();                                                          
220                                    try {
221                                            builder.show();
222                                    } catch (android.view.WindowManager.BadTokenException e) {                                      
223                                            Log.i("TimetableList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running
224                                    }
225                                                                    
226                          }                          }
227                                                    
228                  }                  }
229                                    
230                  @Override                  @Override
231                  protected Void doInBackground(String... arg0) {                  protected Void doInBackground(String... arg0) {
232                          String trainID = arg0[0];                          String type = arg0[0];
233                          success = provider.lookupTimetable(trainID);                          String trainID = arg0[1];
234                          timetables = provider.getTimetable();                          timetables = provider.lookupTimetable(type, trainID);
235                                                    
236                          return null;                          return null;
237                  }                  }

Legend:
Removed from v.366  
changed lines
  Added in v.1257

  ViewVC Help
Powered by ViewVC 1.1.20