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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20