/[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 362 by torben, Tue Sep 29 21:30:16 2009 UTC revision 1373 by torben, Sat Apr 23 08:01:57 2011 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.regex.Pattern;
11    
12    import android.app.AlertDialog;
13    import android.app.Dialog;
14  import android.app.ListActivity;  import android.app.ListActivity;
15    import android.app.ProgressDialog;
16    import android.content.DialogInterface;
17    import android.content.Intent;
18    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;
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;
29    import dk.thoerup.traininfo.provider.TimetableProvider;
30    import dk.thoerup.traininfo.util.MessageBox;
31    
32  public class TimetableList extends ListActivity {  public class TimetableList extends ListActivity {
33            
34            private static final int DLG_PROGRESS = 8000;
35            DepartureEntry departure;
36            TimetableListAdapter adapter;
37            TimetableFetcher fetcher;
38            TimetableBean timetables;
39            int commFailCounter = 0;
40            
41            TimetableProvider provider;
42    
43    
44          @Override          @Override
45          protected void onCreate(Bundle savedInstanceState) {          protected void onCreate(Bundle savedInstanceState) {
46                  super.onCreate(savedInstanceState);                  super.onCreate(savedInstanceState);
47                  setContentView(R.layout.timetablelist);                  setContentView(R.layout.timetablelist);
48                    
49                    provider = ProviderFactory.getTimetableProvider();
50                    
51                    adapter = new TimetableListAdapter(this);              
52                    setListAdapter(adapter);
53    
54    
55                    
56                    Intent launchedBy = getIntent();
57                    departure = (DepartureEntry) launchedBy.getSerializableExtra("departure");
58                    
59                    ((TextView)findViewById(R.id.Train)).setText(departure.getTrainNumber());
60                                    
61                    ((TextView)findViewById(R.id.Location)).setText(departure.getLocation());              
62                    ((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                    note = note.replace("Materielforhold", this.getString(R.string.timetablelist_equipmentrelated) );
76                    note = note.replace("Passagerforhold", this.getString(R.string.timetablelist_passengerrelated) );
77                    note = note.replace("Forventet rettidig afgang", this.getString(R.string.timetablelist_expectedtimelydeparture) );
78                    
79    
80                    
81                    ((TextView)findViewById(R.id.Status)).setText(status);
82                    ((TextView)findViewById(R.id.Note)).setText(note);
83                                                            
84                    
85                    ProviderFactory.purgeOldEntries(); //cleanup before fetching more data
86                    
87                    if (savedInstanceState == null) {
88                            startTimetableFetcher();
89                    } else {
90                            timetables = (TimetableBean) savedInstanceState.getSerializable("timetables");
91                            adapter.setTimetable(timetables);
92                    }
93            }
94    
95            @Override
96            protected void onDestroy() {
97                    super.onDestroy();
98                    
99                    if (fetcher != null) {
100                            fetcher.cancel(true);
101                    }
102            }
103            
104        @Override
105            protected void onListItemClick(ListView l, View v, int position, long id) {
106                    super.onListItemClick(l, v, position, id);
107    
108                    TimetableEntry tt = timetables.entries.get(position);
109                    
110                    StationEntry station = new StationEntry();
111                    station.setName( tt.getStation() );
112                    station.setId( tt.getStationId() );
113                    station.setIsRegional(true);
114                    
115                    
116                    Intent intent = new Intent(this, DepartureList.class);
117                    intent.putExtra("stationbean", station);
118                    startActivity(intent);
119                    
120          }          }
121  /*      
122   *              case DLG_DETAILS:  
123                          DepartureBean currentDeparture = departures.get(selectedItemId);  
124                          ((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime());          @Override
125                          ((TextView)dialog.findViewById(R.id.Train)).setText(currentDeparture.getTrainNumber());      public void onSaveInstanceState(Bundle outState)
126                          ((TextView)dialog.findViewById(R.id.Destination)).setText( currentDeparture.getDestination());      {          
127                          ((TextView)dialog.findViewById(R.id.Origin)).setText(currentDeparture.getOrigin());          dismissDialog(DLG_PROGRESS);
128                          ((TextView)dialog.findViewById(R.id.Location)).setText(currentDeparture.getLocation());          outState.putSerializable("timetables", (TimetableBean) timetables);
129                          ((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString());      }
130                          ((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus());  
131                          ((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote());          
132                          detailsDialog = dialog;          @Override
133            protected void onPrepareDialog(int id, Dialog dialog) {
134                    super.onPrepareDialog(id, dialog);
135                    
136                    switch (id) {
137                    case DLG_PROGRESS:
138                            //pgDialog = (ProgressDialog) dialog;
139                          break;                          break;
140                    }
141            }
142            
143            @Override
144            protected Dialog onCreateDialog(int id) {
145                    switch (id) {
146                    case DLG_PROGRESS:
147                            ProgressDialog dlg = new ProgressDialog(this);                  
148                            dlg.setMessage( getString(timetablelist_fetchdata) );
149                            dlg.setCancelable(true);
150                            return dlg;                    
151                    default:
152                            return super.onCreateDialog(id);                
153                    }
154            }
155    
156            void startTimetableFetcher() {
157                    showDialog(DLG_PROGRESS);
158                    fetcher = new TimetableFetcher();
159                    fetcher.execute(departure.getType(), departure.getTrainNumber());
160            }
161            
162            
163            public String getLastUpdateString(int lastUpdate) {
164                    String minutes = this.getString(R.string.departurebean_minutes);
165                    String unknown = this.getString(R.string.departurebean_unknown);
166                    switch (lastUpdate) {
167                    case 1:
168                            return "<3 " + minutes;
169                    case 2:
170                            return "3-10 " + minutes;
171                    case 3:
172                            return ">3 " + minutes;
173                    case 4:
174                            return unknown;
175                    default:
176                            return "";
177                    }
178            }
179    
180   */          
181            class TimetableFetcher extends AsyncTask<String,Void,Void> {
182                    
183                    
184                    @Override
185                    protected void onPostExecute(Void result) {
186                            super.onPostExecute(result);
187                            dismissDialog(DLG_PROGRESS);
188            
189                            //TODO: differentiate between a communication error and a downed bane.dk site  
190                            if (timetables != null && timetables.errorCode == null) {
191                                    commFailCounter = 0;
192                                    TimetableList.this.getListView().invalidateViews();
193                                    adapter.setTimetable(timetables);
194                                    if (timetables.entries.size() == 0) {
195                                            MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata), true);
196                                    }
197                            } else { // communication or parse error
198                                    commFailCounter++;
199                                    AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this);                                              
200                                    
201                                    
202                                    if (timetables != null && timetables.errorCode != null ) { //got an error xml back
203                                            commFailCounter = 10;                                  
204                                            builder.setMessage( getString(R.string.no_backend) );
205                                    } else {
206                                            builder.setMessage(getString(timetablelist_fetcherror));        
207                                    }
208                                    
209                                    builder.setCancelable(true);
210                                    if (commFailCounter < 3) {
211                                            builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
212                                                    public void onClick(DialogInterface dialog, int id) {
213                                                            dialog.dismiss();
214                                                            startTimetableFetcher();
215                                                            
216                                                    }
217                                            });
218                                    }
219                                    builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
220                                            public void onClick(DialogInterface dialog, int id) {
221                                                    dialog.dismiss();
222                                                    TimetableList.this.finish();
223                                            }                                                      
224                                    });
225                                    
226                                    try {
227                                            builder.show();
228                                    } catch (android.view.WindowManager.BadTokenException e) {                                      
229                                            Log.i("TimetableList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running
230                                    }
231                                                                    
232                            }
233                            
234                    }
235                    
236                    @Override
237                    protected Void doInBackground(String... arg0) {
238                            String type = arg0[0];
239                            String trainID = arg0[1];
240                            timetables = provider.lookupTimetable(type, trainID);
241                            
242                            return null;
243                    }
244                    
245            }
246  }  }

Legend:
Removed from v.362  
changed lines
  Added in v.1373

  ViewVC Help
Powered by ViewVC 1.1.20