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