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

  ViewVC Help
Powered by ViewVC 1.1.20