/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/TimetableList.java
ViewVC logotype

Contents of /android/TrainInfo/src/dk/thoerup/traininfo/TimetableList.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1066 - (show annotations) (download)
Thu Sep 16 15:32:42 2010 UTC (13 years, 8 months ago) by torben
File size: 6112 byte(s)
Experimental #5, update TrainInfo client to use common data-beans
1 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 import android.app.AlertDialog;
10 import android.app.Dialog;
11 import android.app.ListActivity;
12 import android.app.ProgressDialog;
13 import android.content.DialogInterface;
14 import android.content.Intent;
15 import android.os.AsyncTask;
16 import android.os.Bundle;
17 import android.util.Log;
18 import android.view.View;
19 import android.widget.ListView;
20 import android.widget.TextView;
21 import dk.thoerup.android.traininfo.common.DepartureEntry;
22 import dk.thoerup.android.traininfo.common.TimetableBean;
23 import dk.thoerup.android.traininfo.common.TimetableEntry;
24 import dk.thoerup.android.traininfo.common.StationBean.StationEntry;
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 {
30
31 private static final int DLG_PROGRESS = 8000;
32 DepartureEntry departure;
33 TimetableListAdapter adapter;
34 TimetableFetcher fetcher;
35 TimetableBean timetables;
36 int commFailCounter = 0;
37
38 TimetableProvider provider;
39
40 @SuppressWarnings("unchecked")
41 @Override
42 protected void onCreate(Bundle savedInstanceState) {
43 super.onCreate(savedInstanceState);
44 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();
54 departure = (DepartureEntry) launchedBy.getSerializableExtra("departure");
55
56 ((TextView)findViewById(R.id.Train)).setText(departure.getTrainNumber());
57 ((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());
60 ((TextView)findViewById(R.id.Updated)).setText( getLastUpdateString( departure.getUpdated() ));
61
62
63 ProviderFactory.purgeOldEntries(); //cleanup before fetching more data
64
65 if (savedInstanceState == null) {
66 startTimetableFetcher();
67 } else {
68 timetables = (TimetableBean) savedInstanceState.getSerializable("timetables");
69 adapter.setTimetable(timetables);
70 }
71 }
72
73 @Override
74 protected void onDestroy() {
75 super.onDestroy();
76
77 if (fetcher != null) {
78 fetcher.cancel(true);
79 }
80 }
81
82 @Override
83 protected void onListItemClick(ListView l, View v, int position, long id) {
84 super.onListItemClick(l, v, position, id);
85
86 TimetableEntry tt = timetables.entries.get(position);
87
88 StationEntry station = new StationEntry();
89 station.setName( tt.getStation() );
90 station.setId( tt.getStationId() );
91 station.setIsRegional(true);
92
93
94 Intent intent = new Intent(this, DepartureList.class);
95 intent.putExtra("stationbean", station);
96 startActivity(intent);
97
98 }
99
100
101
102 @Override
103 public void onSaveInstanceState(Bundle outState)
104 {
105 dismissDialog(DLG_PROGRESS);
106 outState.putSerializable("timetables", (TimetableBean) timetables);
107 }
108
109
110 @Override
111 protected void onPrepareDialog(int id, Dialog dialog) {
112 super.onPrepareDialog(id, dialog);
113
114 switch (id) {
115 case DLG_PROGRESS:
116 //pgDialog = (ProgressDialog) dialog;
117 break;
118 }
119 }
120
121 @Override
122 protected Dialog onCreateDialog(int id) {
123 switch (id) {
124 case DLG_PROGRESS:
125 ProgressDialog dlg = new ProgressDialog(this);
126 dlg.setMessage( getString(timetablelist_fetchdata) );
127 dlg.setCancelable(true);
128 return dlg;
129 default:
130 return super.onCreateDialog(id);
131 }
132 }
133
134 void startTimetableFetcher() {
135 showDialog(DLG_PROGRESS);
136 fetcher = new TimetableFetcher();
137 fetcher.execute(departure.getType(), departure.getTrainNumber());
138 }
139
140
141 public String getLastUpdateString(int lastUpdate) {
142 String minutes = this.getString(R.string.departurebean_minutes);
143 String unknown = this.getString(R.string.departurebean_unknown);
144 switch (lastUpdate) {
145 case 1:
146 return "<3 " + minutes;
147 case 2:
148 return "3-10 " + minutes;
149 case 3:
150 return ">3 " + minutes;
151 case 4:
152 return unknown;
153 default:
154 return "";
155 }
156 }
157
158
159 class TimetableFetcher extends AsyncTask<String,Void,Void> {
160
161
162 @Override
163 protected void onPostExecute(Void result) {
164 super.onPostExecute(result);
165 dismissDialog(DLG_PROGRESS);
166
167
168 if (timetables != null) {
169 commFailCounter = 0;
170 TimetableList.this.getListView().invalidateViews();
171 adapter.setTimetable(timetables);
172 if (timetables.entries.size() == 0) {
173 MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata), true);
174 }
175 } else { // communication or parse error
176 commFailCounter++;
177 AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this);
178 builder.setMessage(getString(timetablelist_fetcherror));
179 builder.setCancelable(true);
180 if (commFailCounter < 3) {
181 builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
182 public void onClick(DialogInterface dialog, int id) {
183 dialog.dismiss();
184 startTimetableFetcher();
185
186 }
187 });
188 }
189 builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
190 public void onClick(DialogInterface dialog, int id) {
191 dialog.dismiss();
192 TimetableList.this.finish();
193 }
194 });
195
196 try {
197 builder.show();
198 } catch (android.view.WindowManager.BadTokenException e) {
199 Log.i("TimetableList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running
200 }
201
202 }
203
204 }
205
206 @Override
207 protected Void doInBackground(String... arg0) {
208 String type = arg0[0];
209 String trainID = arg0[1];
210 timetables = provider.lookupTimetable(type, trainID);
211
212 return null;
213 }
214
215 }
216 }

  ViewVC Help
Powered by ViewVC 1.1.20