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

  ViewVC Help
Powered by ViewVC 1.1.20