/[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 364 by torben, Wed Sep 30 06:02:03 2009 UTC revision 1017 by torben, Tue Aug 24 19:36:27 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;
11    import java.util.List;
12    
13    import android.app.AlertDialog;
14    import android.app.Dialog;
15  import android.app.ListActivity;  import android.app.ListActivity;
16    import android.app.ProgressDialog;
17    import android.content.DialogInterface;
18  import android.content.Intent;  import android.content.Intent;
19    import android.os.AsyncTask;
20  import android.os.Bundle;  import android.os.Bundle;
21    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;
26    import dk.thoerup.traininfo.provider.TimetableProvider;
27    import dk.thoerup.traininfo.util.MessageBox;
28    
29  public class TimetableList extends ListActivity {  public class TimetableList extends ListActivity {
30                    
31          DepartureBean departure;          private static final int DLG_PROGRESS = 8000;
32            DepartureEntry departure;
33            TimetableListAdapter adapter;
34            TimetableFetcher fetcher;
35            List<TimetableBean> timetables;
36            int commFailCounter = 0;
37                    
38            TimetableProvider provider;
39    
40            @SuppressWarnings("unchecked")
41          @Override          @Override
42          protected void onCreate(Bundle savedInstanceState) {          protected void onCreate(Bundle savedInstanceState) {
43                  super.onCreate(savedInstanceState);                  super.onCreate(savedInstanceState);
44                  setContentView(R.layout.timetablelist);                  setContentView(R.layout.timetablelist);
45                                    
46                    provider = ProviderFactory.getTimetableProvider();
47                    
48                    adapter = new TimetableListAdapter(this);              
49                    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                    
63                    if (savedInstanceState == null) {
64                            startTimetableFetcher();
65                    } else {
66                            timetables = (List<TimetableBean>) savedInstanceState.getSerializable("timetables");
67                            adapter.setTimetable(timetables);
68                    }
69            }
70    
71            @Override
72            protected void onDestroy() {
73                    super.onDestroy();
74                    
75                    if (fetcher != null) {
76                            fetcher.cancel(true);
77                    }
78            }
79            
80        @Override
81            protected void onListItemClick(ListView l, View v, int position, long id) {
82                    super.onListItemClick(l, v, position, id);
83    
84                    TimetableBean tt = timetables.get(position);
85                    
86                    StationBean station = new StationBean();
87                    station.setName( tt.getStation() );
88                    station.setId( tt.getStationId() );
89                    station.setRegional(true);
90                    
91                    Intent intent = new Intent(this, DepartureList.class);
92                    intent.putExtra("stationbean", station);
93                    startActivity(intent);
94                    
95          }          }
96        
97    
98    
99            @Override
100        public void onSaveInstanceState(Bundle outState)
101        {          
102            dismissDialog(DLG_PROGRESS);
103            outState.putSerializable("timetables", (ArrayList<TimetableBean>) timetables);
104        }
105    
106  /*              case DLG_DETAILS:          
107                          DepartureBean currentDeparture = departures.get(selectedItemId);          @Override
108                          ((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime());          protected void onPrepareDialog(int id, Dialog dialog) {
109                          ((TextView)dialog.findViewById(R.id.Train)).setText(currentDeparture.getTrainNumber());                  super.onPrepareDialog(id, dialog);
110                          ((TextView)dialog.findViewById(R.id.Destination)).setText( currentDeparture.getDestination());                  
111                          ((TextView)dialog.findViewById(R.id.Origin)).setText(currentDeparture.getOrigin());                  switch (id) {
112                          ((TextView)dialog.findViewById(R.id.Location)).setText(currentDeparture.getLocation());                  case DLG_PROGRESS:
113                          ((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString());                          //pgDialog = (ProgressDialog) dialog;
                         ((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus());  
                         ((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote());  
                         detailsDialog = dialog;  
114                          break;                          break;
115                    }
116            }
117            
118            @Override
119            protected Dialog onCreateDialog(int id) {
120                    switch (id) {
121                    case DLG_PROGRESS:
122                            ProgressDialog dlg = new ProgressDialog(this);                  
123                            dlg.setMessage( getString(timetablelist_fetchdata) );
124                            dlg.setCancelable(true);
125                            return dlg;                    
126                    default:
127                            return super.onCreateDialog(id);                
128                    }
129            }
130    
131   */          void startTimetableFetcher() {
132                    showDialog(DLG_PROGRESS);
133                    fetcher = new TimetableFetcher();
134                    fetcher.execute(departure.getType(), departure.getTrainNumber());
135            }
136            
137            class TimetableFetcher extends AsyncTask<String,Void,Void> {
138                    
139                    
140                    @Override
141                    protected void onPostExecute(Void result) {
142                            super.onPostExecute(result);
143                            dismissDialog(DLG_PROGRESS);
144            
145                            
146                            if (timetables != null) {
147                                    commFailCounter = 0;
148                                    TimetableList.this.getListView().invalidateViews();
149                                    adapter.setTimetable(timetables);
150                                    if (timetables.size() == 0) {
151                                            MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata), true);
152                                    }
153                            } else { // communication or parse error
154                                    commFailCounter++;
155                                    AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this);                                              
156                                    builder.setMessage(getString(timetablelist_fetcherror));
157                                    builder.setCancelable(true);
158                                    if (commFailCounter < 3) {
159                                            builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
160                                                    public void onClick(DialogInterface dialog, int id) {
161                                                            dialog.dismiss();
162                                                            startTimetableFetcher();
163                                                            
164                                                    }
165                                            });
166                                    }
167                                    builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
168                                            public void onClick(DialogInterface dialog, int id) {
169                                                    dialog.dismiss();
170                                                    TimetableList.this.finish();
171                                            }                                                      
172                                    });
173                                    
174                                    try {
175                                            builder.show();
176                                    } catch (android.view.WindowManager.BadTokenException e) {                                      
177                                            Log.i("TimetableList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running
178                                    }
179                                                                    
180                            }
181                            
182                    }
183                    
184                    @Override
185                    protected Void doInBackground(String... arg0) {
186                            String type = arg0[0];
187                            String trainID = arg0[1];
188                            timetables = provider.lookupTimetable(type, trainID);
189                            
190                            return null;
191                    }
192                    
193            }
194  }  }

Legend:
Removed from v.364  
changed lines
  Added in v.1017

  ViewVC Help
Powered by ViewVC 1.1.20