/[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 1260 - (show annotations) (download)
Mon Apr 4 18:33:27 2011 UTC (13 years, 1 month ago) by torben
File size: 7058 byte(s)
Another translation
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
10 import java.util.regex.Pattern;
11
12 import android.app.AlertDialog;
13 import android.app.Dialog;
14 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;
20 import android.util.Log;
21 import android.view.View;
22 import android.widget.ListView;
23 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 {
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
45 protected void onCreate(Bundle savedInstanceState) {
46 super.onCreate(savedInstanceState);
47 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_equipmentcircumstances) );
76
77
78 ((TextView)findViewById(R.id.Status)).setText(status);
79 ((TextView)findViewById(R.id.Note)).setText(note);
80
81
82 ProviderFactory.purgeOldEntries(); //cleanup before fetching more data
83
84 if (savedInstanceState == null) {
85 startTimetableFetcher();
86 } else {
87 timetables = (TimetableBean) savedInstanceState.getSerializable("timetables");
88 adapter.setTimetable(timetables);
89 }
90 }
91
92 @Override
93 protected void onDestroy() {
94 super.onDestroy();
95
96 if (fetcher != null) {
97 fetcher.cancel(true);
98 }
99 }
100
101 @Override
102 protected void onListItemClick(ListView l, View v, int position, long id) {
103 super.onListItemClick(l, v, position, id);
104
105 TimetableEntry tt = timetables.entries.get(position);
106
107 StationEntry station = new StationEntry();
108 station.setName( tt.getStation() );
109 station.setId( tt.getStationId() );
110 station.setIsRegional(true);
111
112
113 Intent intent = new Intent(this, DepartureList.class);
114 intent.putExtra("stationbean", station);
115 startActivity(intent);
116
117 }
118
119
120
121 @Override
122 public void onSaveInstanceState(Bundle outState)
123 {
124 dismissDialog(DLG_PROGRESS);
125 outState.putSerializable("timetables", (TimetableBean) timetables);
126 }
127
128
129 @Override
130 protected void onPrepareDialog(int id, Dialog dialog) {
131 super.onPrepareDialog(id, dialog);
132
133 switch (id) {
134 case DLG_PROGRESS:
135 //pgDialog = (ProgressDialog) dialog;
136 break;
137 }
138 }
139
140 @Override
141 protected Dialog onCreateDialog(int id) {
142 switch (id) {
143 case DLG_PROGRESS:
144 ProgressDialog dlg = new ProgressDialog(this);
145 dlg.setMessage( getString(timetablelist_fetchdata) );
146 dlg.setCancelable(true);
147 return dlg;
148 default:
149 return super.onCreateDialog(id);
150 }
151 }
152
153 void startTimetableFetcher() {
154 showDialog(DLG_PROGRESS);
155 fetcher = new TimetableFetcher();
156 fetcher.execute(departure.getType(), departure.getTrainNumber());
157 }
158
159
160 public String getLastUpdateString(int lastUpdate) {
161 String minutes = this.getString(R.string.departurebean_minutes);
162 String unknown = this.getString(R.string.departurebean_unknown);
163 switch (lastUpdate) {
164 case 1:
165 return "<3 " + minutes;
166 case 2:
167 return "3-10 " + minutes;
168 case 3:
169 return ">3 " + minutes;
170 case 4:
171 return unknown;
172 default:
173 return "";
174 }
175 }
176
177
178 class TimetableFetcher extends AsyncTask<String,Void,Void> {
179
180
181 @Override
182 protected void onPostExecute(Void result) {
183 super.onPostExecute(result);
184 dismissDialog(DLG_PROGRESS);
185
186
187 if (timetables != null) {
188 commFailCounter = 0;
189 TimetableList.this.getListView().invalidateViews();
190 adapter.setTimetable(timetables);
191 if (timetables.entries.size() == 0) {
192 MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata), true);
193 }
194 } else { // communication or parse error
195 commFailCounter++;
196 AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this);
197 builder.setMessage(getString(timetablelist_fetcherror));
198 builder.setCancelable(true);
199 if (commFailCounter < 3) {
200 builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
201 public void onClick(DialogInterface dialog, int id) {
202 dialog.dismiss();
203 startTimetableFetcher();
204
205 }
206 });
207 }
208 builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
209 public void onClick(DialogInterface dialog, int id) {
210 dialog.dismiss();
211 TimetableList.this.finish();
212 }
213 });
214
215 try {
216 builder.show();
217 } catch (android.view.WindowManager.BadTokenException e) {
218 Log.i("TimetableList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running
219 }
220
221 }
222
223 }
224
225 @Override
226 protected Void doInBackground(String... arg0) {
227 String type = arg0[0];
228 String trainID = arg0[1];
229 timetables = provider.lookupTimetable(type, trainID);
230
231 return null;
232 }
233
234 }
235 }

  ViewVC Help
Powered by ViewVC 1.1.20