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

Annotation of /android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1017 - (hide annotations) (download)
Tue Aug 24 19:36:27 2010 UTC (13 years, 9 months ago) by torben
File size: 9525 byte(s)
Only try 3 times and then show the cancel button to abort
1 torben 237 package dk.thoerup.traininfo;
2    
3 torben 835 import static dk.thoerup.traininfo.R.string.departurelist_fetchdepartures;
4     import static dk.thoerup.traininfo.R.string.departurelist_fetcharrivals;
5     import static dk.thoerup.traininfo.R.string.generic_cancel;
6     import static dk.thoerup.traininfo.R.string.generic_retry;
7    
8    
9 torben 237 import java.text.NumberFormat;
10    
11 torben 981
12 torben 336 import android.app.AlertDialog;
13 torben 237 import android.app.Dialog;
14     import android.app.ListActivity;
15     import android.app.ProgressDialog;
16 torben 336 import android.content.DialogInterface;
17 torben 237 import android.content.Intent;
18 torben 239 import android.net.Uri;
19 torben 237 import android.os.AsyncTask;
20     import android.os.Bundle;
21 torben 630 import android.util.Log;
22 torben 982 import android.view.Menu;
23     import android.view.MenuItem;
24 torben 237 import android.view.View;
25 torben 835 import android.view.View.OnClickListener;
26     import android.widget.Button;
27 torben 237 import android.widget.ListView;
28     import android.widget.TextView;
29 torben 255 import dk.thoerup.traininfo.provider.DepartureProvider;
30 torben 253 import dk.thoerup.traininfo.provider.ProviderFactory;
31 torben 245 import dk.thoerup.traininfo.util.MessageBox;
32 torben 237
33     public class DepartureList extends ListActivity {
34    
35     public static final int DLG_PROGRESS = 1;
36 torben 982 static final int MENU_MAP = 100;
37     static final int MENU_NOTIFICATIONS = 101;
38 torben 237
39 torben 362
40 torben 237 DepartureListAdapter adapter;
41     DepartureProvider provider;
42 torben 981 DepartureBean departures;
43 torben 237
44 torben 257 int selectedItemId;
45     //DepartureBean currentDeparture;
46 torben 237
47     ProgressDialog pgDialog;
48 torben 362
49 torben 238 DepartureFetcher fetcher;
50 torben 557
51     StationBean station;
52 torben 238
53 torben 835 boolean arrival = false;
54 torben 981
55 torben 1017 int commFailCounter = 0;
56    
57 torben 237 @Override
58     protected void onCreate(Bundle savedInstanceState) {
59     super.onCreate(savedInstanceState);
60     setContentView(R.layout.departurelist);
61    
62     adapter = new DepartureListAdapter(this);
63     setListAdapter(adapter);
64    
65     Intent launchedBy = getIntent();
66 torben 557
67     station = (StationBean) launchedBy.getSerializableExtra("stationbean");
68 torben 239
69 torben 557 ((TextView) findViewById(R.id.stationName)).setText( station.getName() );
70 torben 317
71 torben 557
72     ((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() );
73 torben 237
74 torben 835 final Button departureBtn = (Button) findViewById(R.id.departurebtn);
75     final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn);
76 torben 294
77 torben 835 departureBtn.setOnClickListener( new OnClickListener() {
78     @Override
79     public void onClick(View arg0) {
80     arrivalBtn.setBackgroundResource(R.drawable.custom_button);
81     departureBtn.setBackgroundResource(R.drawable.custom_button_hilight);
82     arrival = false;
83     startDepartureFetcher();
84     }
85     });
86     arrivalBtn.setOnClickListener( new OnClickListener() {
87     @Override
88     public void onClick(View arg0) {
89     arrivalBtn.setBackgroundResource(R.drawable.custom_button_hilight);
90     departureBtn.setBackgroundResource(R.drawable.custom_button);
91     arrival = true;
92     startDepartureFetcher();
93     }
94     });
95    
96    
97    
98    
99 torben 982 // findViewById(R.id.header).setOnClickListener( mapLauncher );
100 torben 237
101 torben 557 int distance = station.getDistance();
102 torben 742 if (distance != 0) {
103     NumberFormat format = NumberFormat.getNumberInstance();
104     format.setMaximumFractionDigits(1);
105     format.setMinimumFractionDigits(1);
106 torben 575
107 torben 742 ((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." );
108     } else {
109     ((TextView) findViewById(R.id.stationDistance)).setVisibility(View.GONE);
110     }
111    
112 torben 552
113 torben 557 if (station.isRegional() == false && station.isSTrain() == false) {
114 torben 552 getListView().setVisibility( View.GONE );
115     findViewById(R.id.metroonly).setVisibility( View.VISIBLE );
116 torben 835 departureBtn.setVisibility( View.GONE );
117     arrivalBtn.setVisibility(View.GONE);
118 torben 552
119 torben 257 } else {
120 torben 552 provider = ProviderFactory.getDepartureProvider();
121    
122     if (savedInstanceState == null) {
123     startDepartureFetcher();
124     } else {
125 torben 981 departures = (DepartureBean) savedInstanceState.getSerializable("departures");
126 torben 990
127     if ( (departures != null) && (departures.entries != null) ) {
128     adapter.setDepartures(departures.entries);
129     }
130 torben 982 selectedItemId = savedInstanceState.getInt("selectedItemId");
131    
132     if ( hasNotifications() ) {
133     findViewById(R.id.notifIcon).setVisibility(View.VISIBLE);
134     }
135    
136 torben 552 }
137 torben 257 }
138 torben 237 }
139    
140 torben 982 boolean hasNotifications() {
141     return (departures != null && departures.notifications.size() > 0);
142     }
143    
144 torben 243 @Override
145     public void onSaveInstanceState(Bundle outState)
146     {
147 torben 257 if (pgDialog != null && pgDialog.isShowing())
148     dismissDialog(DLG_PROGRESS);
149 torben 362
150 torben 257 outState.putInt("selectedItemId", selectedItemId);
151    
152 torben 981 outState.putSerializable("departures", departures);
153 torben 243 }
154 torben 918
155    
156 torben 243
157 torben 237 @Override
158 torben 918 protected void onDestroy() {
159     super.onDestroy();
160    
161     if (fetcher != null) {
162     fetcher.cancel(true);
163     }
164     }
165    
166     @Override
167 torben 237 protected void onListItemClick(ListView l, View v, int position, long id) {
168     super.onListItemClick(l, v, position, id);
169 torben 362
170     selectedItemId = position;
171 torben 364
172 torben 981 DepartureEntry dep = departures.entries.get(selectedItemId);
173 torben 237
174 torben 362 Intent intent = new Intent(this, TimetableList.class);
175 torben 364 intent.putExtra("departure", dep);
176 torben 362
177     startActivity(intent);
178    
179 torben 237 }
180    
181    
182     @Override
183     protected void onPrepareDialog(int id, Dialog dialog) {
184     super.onPrepareDialog(id, dialog);
185 torben 257
186 torben 237 switch (id) {
187     case DLG_PROGRESS:
188     pgDialog = (ProgressDialog) dialog;
189 torben 835 int messageId = arrival == false ? departurelist_fetchdepartures : departurelist_fetcharrivals;
190     pgDialog.setMessage( getString(messageId) );
191 torben 237 break;
192     }
193     }
194    
195     @Override
196     protected Dialog onCreateDialog(int id) {
197     switch (id) {
198     case DLG_PROGRESS:
199 torben 835
200 torben 237 ProgressDialog dlg = new ProgressDialog(this);
201     dlg.setCancelable(true);
202 torben 362 return dlg;
203 torben 237 default:
204     return super.onCreateDialog(id);
205     }
206     }
207 torben 982
208    
209 torben 336
210 torben 982
211    
212     @Override
213     public boolean onCreateOptionsMenu(Menu menu) {
214     MenuItem item;
215 torben 985
216     item = menu.add(0, MENU_MAP, 0, getString(R.string.departurelist_showonmap) );
217 torben 982 item.setIcon(android.R.drawable.ic_menu_mapmode);
218    
219 torben 985 item = menu.add(0, MENU_NOTIFICATIONS, 0, getString(R.string.departurelist_notifications) );
220 torben 982 item.setIcon(android.R.drawable.ic_menu_info_details);
221    
222    
223     boolean notifEnabled = hasNotifications();
224     item.setEnabled(notifEnabled);
225    
226    
227     return true;
228     }
229    
230     @Override
231     public boolean onOptionsItemSelected(MenuItem item) {
232     boolean res;
233     switch(item.getItemId()) {
234     case MENU_MAP:
235     Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude());
236     startActivity( new Intent(Intent.ACTION_VIEW, uri));
237     res = true;
238     break;
239     case MENU_NOTIFICATIONS:
240     Intent i = new Intent(this,dk.thoerup.traininfo.NotificationList.class);
241     i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications);
242     startActivity(i);
243     res = true;
244     break;
245     default:
246     res = super.onOptionsItemSelected(item);
247     }
248     return res;
249     }
250    
251 torben 336 void startDepartureFetcher() {
252     showDialog(DLG_PROGRESS);
253     fetcher = new DepartureFetcher();
254 torben 557 fetcher.execute(station.getId());
255 torben 336 }
256 torben 316
257 torben 237 class DialogDismisser implements View.OnClickListener {
258    
259     Dialog dlg;
260     public DialogDismisser(Dialog d) {
261     dlg = d;
262     }
263    
264     @Override
265     public void onClick(View v) {
266     if (dlg.isShowing())
267     dlg.dismiss();
268 torben 365 }
269     }
270 torben 238
271 torben 982 /*View.OnClickListener mapLauncher = new View.OnClickListener() {
272 torben 239 @Override
273 torben 557 public void onClick(View v) {
274     Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude());
275 torben 239 startActivity( new Intent(Intent.ACTION_VIEW, uri));
276     }
277 torben 982 };*/
278 torben 336
279    
280 torben 238
281 torben 310 class DepartureFetcher extends AsyncTask<Integer, Void, Void> {
282 torben 238
283 torben 365
284 torben 238 @Override
285     protected void onPostExecute(Void result) {
286     super.onPostExecute(result);
287    
288 torben 336
289 torben 238 pgDialog.dismiss();
290    
291 torben 1007 if (departures != null) {
292 torben 1017 commFailCounter = 0;
293 torben 917 DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html
294 torben 981 adapter.setDepartures(departures.entries);
295 torben 917 DepartureList.this.getListView().setVisibility(View.VISIBLE);
296    
297 torben 982
298     if ( hasNotifications() ) {
299     findViewById(R.id.notifIcon).setVisibility(View.VISIBLE);
300     }
301    
302 torben 981 if (departures.entries.size() == 0) {
303 torben 906 MessageBox.showMessage(DepartureList.this, "No departures found", true);
304 torben 319 }
305     } else { // communication or parse error
306 torben 1017 commFailCounter++;
307 torben 336 AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this);
308     builder.setMessage("Error finding departures");
309     builder.setCancelable(true);
310 torben 1017 if (commFailCounter < 3) {
311     builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
312     public void onClick(DialogInterface dialog, int id) {
313     dialog.dismiss();
314     startDepartureFetcher();
315    
316     }
317     });
318     }
319 torben 561 builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
320 torben 336 public void onClick(DialogInterface dialog, int id) {
321     dialog.dismiss();
322 torben 843 DepartureList.this.finish();
323 torben 336 }
324 torben 630 });
325    
326     try {
327     builder.show();
328     } catch (android.view.WindowManager.BadTokenException e) {
329     Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running
330     }
331 torben 319 }
332 torben 238 }
333    
334     @Override
335 torben 310 protected Void doInBackground(Integer... params) {
336 torben 1007 departures = provider.lookupDepartures(params[0], DepartureList.this.arrival);
337 torben 238 return null;
338     }
339    
340     }
341 torben 237 }

  ViewVC Help
Powered by ViewVC 1.1.20