/[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 1044 - (hide 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 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 1044 final Button metroBtn = (Button) findViewById(R.id.metrobtn);
77 torben 294
78 torben 835 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 torben 1044 metroBtn.setBackgroundResource(R.drawable.custom_button);
84 torben 835 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 torben 1044 metroBtn.setBackgroundResource(R.drawable.custom_button);
94 torben 835 arrival = true;
95     startDepartureFetcher();
96     }
97     });
98    
99 torben 1044 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 torben 835
108    
109    
110 torben 1044
111 torben 982 // findViewById(R.id.header).setOnClickListener( mapLauncher );
112 torben 237
113 torben 557 int distance = station.getDistance();
114 torben 742 if (distance != 0) {
115     NumberFormat format = NumberFormat.getNumberInstance();
116     format.setMaximumFractionDigits(1);
117     format.setMinimumFractionDigits(1);
118 torben 575
119 torben 742 ((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 torben 552
124 torben 1028 ProviderFactory.purgeOldEntries(); //cleanup before fetching more data
125 torben 1044
126     Log.e("Station", station.toCSV() );
127    
128     if (station.isMetro() == false) {
129     metroBtn.setVisibility( View.GONE );
130     }
131 torben 1028
132 torben 557 if (station.isRegional() == false && station.isSTrain() == false) {
133 torben 552 getListView().setVisibility( View.GONE );
134     findViewById(R.id.metroonly).setVisibility( View.VISIBLE );
135 torben 835 departureBtn.setVisibility( View.GONE );
136     arrivalBtn.setVisibility(View.GONE);
137 torben 1044 metroBtn.setVisibility( View.GONE );
138 torben 552
139 torben 257 } else {
140 torben 552 provider = ProviderFactory.getDepartureProvider();
141    
142     if (savedInstanceState == null) {
143     startDepartureFetcher();
144     } else {
145 torben 981 departures = (DepartureBean) savedInstanceState.getSerializable("departures");
146 torben 990
147     if ( (departures != null) && (departures.entries != null) ) {
148     adapter.setDepartures(departures.entries);
149     }
150 torben 982 selectedItemId = savedInstanceState.getInt("selectedItemId");
151    
152     if ( hasNotifications() ) {
153     findViewById(R.id.notifIcon).setVisibility(View.VISIBLE);
154     }
155    
156 torben 552 }
157 torben 257 }
158 torben 237 }
159    
160 torben 982 boolean hasNotifications() {
161     return (departures != null && departures.notifications.size() > 0);
162     }
163    
164 torben 243 @Override
165     public void onSaveInstanceState(Bundle outState)
166     {
167 torben 257 if (pgDialog != null && pgDialog.isShowing())
168     dismissDialog(DLG_PROGRESS);
169 torben 362
170 torben 257 outState.putInt("selectedItemId", selectedItemId);
171    
172 torben 981 outState.putSerializable("departures", departures);
173 torben 243 }
174 torben 918
175    
176 torben 243
177 torben 237 @Override
178 torben 918 protected void onDestroy() {
179     super.onDestroy();
180    
181     if (fetcher != null) {
182     fetcher.cancel(true);
183     }
184     }
185    
186     @Override
187 torben 237 protected void onListItemClick(ListView l, View v, int position, long id) {
188     super.onListItemClick(l, v, position, id);
189 torben 362
190     selectedItemId = position;
191 torben 364
192 torben 981 DepartureEntry dep = departures.entries.get(selectedItemId);
193 torben 237
194 torben 362 Intent intent = new Intent(this, TimetableList.class);
195 torben 364 intent.putExtra("departure", dep);
196 torben 362
197     startActivity(intent);
198    
199 torben 237 }
200    
201    
202     @Override
203     protected void onPrepareDialog(int id, Dialog dialog) {
204     super.onPrepareDialog(id, dialog);
205 torben 257
206 torben 237 switch (id) {
207     case DLG_PROGRESS:
208     pgDialog = (ProgressDialog) dialog;
209 torben 835 int messageId = arrival == false ? departurelist_fetchdepartures : departurelist_fetcharrivals;
210     pgDialog.setMessage( getString(messageId) );
211 torben 237 break;
212     }
213     }
214    
215     @Override
216     protected Dialog onCreateDialog(int id) {
217     switch (id) {
218     case DLG_PROGRESS:
219 torben 835
220 torben 237 ProgressDialog dlg = new ProgressDialog(this);
221     dlg.setCancelable(true);
222 torben 362 return dlg;
223 torben 237 default:
224     return super.onCreateDialog(id);
225     }
226     }
227 torben 982
228    
229 torben 336
230 torben 982
231    
232     @Override
233     public boolean onCreateOptionsMenu(Menu menu) {
234     MenuItem item;
235 torben 985
236     item = menu.add(0, MENU_MAP, 0, getString(R.string.departurelist_showonmap) );
237 torben 982 item.setIcon(android.R.drawable.ic_menu_mapmode);
238    
239 torben 985 item = menu.add(0, MENU_NOTIFICATIONS, 0, getString(R.string.departurelist_notifications) );
240 torben 982 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 torben 336 void startDepartureFetcher() {
272     showDialog(DLG_PROGRESS);
273     fetcher = new DepartureFetcher();
274 torben 557 fetcher.execute(station.getId());
275 torben 336 }
276 torben 316
277 torben 237 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 torben 365 }
289     }
290 torben 238
291 torben 982 /*View.OnClickListener mapLauncher = new View.OnClickListener() {
292 torben 239 @Override
293 torben 557 public void onClick(View v) {
294     Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude());
295 torben 239 startActivity( new Intent(Intent.ACTION_VIEW, uri));
296     }
297 torben 982 };*/
298 torben 336
299    
300 torben 238
301 torben 310 class DepartureFetcher extends AsyncTask<Integer, Void, Void> {
302 torben 238
303 torben 365
304 torben 238 @Override
305     protected void onPostExecute(Void result) {
306     super.onPostExecute(result);
307    
308 torben 336
309 torben 238 pgDialog.dismiss();
310    
311 torben 1007 if (departures != null) {
312 torben 1017 commFailCounter = 0;
313 torben 917 DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html
314 torben 981 adapter.setDepartures(departures.entries);
315 torben 917 DepartureList.this.getListView().setVisibility(View.VISIBLE);
316    
317 torben 982
318     if ( hasNotifications() ) {
319     findViewById(R.id.notifIcon).setVisibility(View.VISIBLE);
320     }
321    
322 torben 981 if (departures.entries.size() == 0) {
323 torben 906 MessageBox.showMessage(DepartureList.this, "No departures found", true);
324 torben 319 }
325     } else { // communication or parse error
326 torben 1017 commFailCounter++;
327 torben 336 AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this);
328     builder.setMessage("Error finding departures");
329     builder.setCancelable(true);
330 torben 1017 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 torben 561 builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
340 torben 336 public void onClick(DialogInterface dialog, int id) {
341     dialog.dismiss();
342 torben 843 DepartureList.this.finish();
343 torben 336 }
344 torben 630 });
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 torben 319 }
352 torben 238 }
353    
354     @Override
355 torben 310 protected Void doInBackground(Integer... params) {
356 torben 1007 departures = provider.lookupDepartures(params[0], DepartureList.this.arrival);
357 torben 238 return null;
358     }
359    
360     }
361 torben 237 }

  ViewVC Help
Powered by ViewVC 1.1.20