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 |
torben |
257 |
import java.util.ArrayList;
|
11 |
torben |
237 |
import java.util.List;
|
12 |
|
|
|
13 |
torben |
336 |
import android.app.AlertDialog;
|
14 |
torben |
237 |
import android.app.Dialog;
|
15 |
|
|
import android.app.ListActivity;
|
16 |
|
|
import android.app.ProgressDialog;
|
17 |
torben |
336 |
import android.content.DialogInterface;
|
18 |
torben |
237 |
import android.content.Intent;
|
19 |
torben |
239 |
import android.net.Uri;
|
20 |
torben |
237 |
import android.os.AsyncTask;
|
21 |
|
|
import android.os.Bundle;
|
22 |
torben |
630 |
import android.util.Log;
|
23 |
torben |
237 |
import android.view.View;
|
24 |
torben |
835 |
import android.view.View.OnClickListener;
|
25 |
|
|
import android.widget.Button;
|
26 |
torben |
237 |
import android.widget.ListView;
|
27 |
|
|
import android.widget.TextView;
|
28 |
torben |
255 |
import dk.thoerup.traininfo.provider.DepartureProvider;
|
29 |
torben |
253 |
import dk.thoerup.traininfo.provider.ProviderFactory;
|
30 |
torben |
245 |
import dk.thoerup.traininfo.util.MessageBox;
|
31 |
torben |
237 |
|
32 |
|
|
public class DepartureList extends ListActivity {
|
33 |
|
|
|
34 |
|
|
public static final int DLG_PROGRESS = 1;
|
35 |
|
|
|
36 |
torben |
362 |
|
37 |
torben |
237 |
DepartureListAdapter adapter;
|
38 |
|
|
DepartureProvider provider;
|
39 |
|
|
List<DepartureBean> departures;
|
40 |
|
|
|
41 |
torben |
257 |
int selectedItemId;
|
42 |
|
|
//DepartureBean currentDeparture;
|
43 |
torben |
237 |
|
44 |
|
|
ProgressDialog pgDialog;
|
45 |
torben |
362 |
|
46 |
torben |
238 |
DepartureFetcher fetcher;
|
47 |
torben |
557 |
|
48 |
|
|
StationBean station;
|
49 |
torben |
238 |
|
50 |
torben |
835 |
boolean arrival = false;
|
51 |
|
|
|
52 |
torben |
257 |
@SuppressWarnings("unchecked")
|
53 |
torben |
237 |
@Override
|
54 |
|
|
protected void onCreate(Bundle savedInstanceState) {
|
55 |
|
|
super.onCreate(savedInstanceState);
|
56 |
|
|
setContentView(R.layout.departurelist);
|
57 |
|
|
|
58 |
|
|
adapter = new DepartureListAdapter(this);
|
59 |
|
|
setListAdapter(adapter);
|
60 |
|
|
|
61 |
|
|
Intent launchedBy = getIntent();
|
62 |
torben |
557 |
|
63 |
|
|
station = (StationBean) launchedBy.getSerializableExtra("stationbean");
|
64 |
torben |
239 |
|
65 |
torben |
557 |
((TextView) findViewById(R.id.stationName)).setText( station.getName() );
|
66 |
torben |
317 |
|
67 |
torben |
557 |
|
68 |
|
|
((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() );
|
69 |
torben |
237 |
|
70 |
torben |
835 |
final Button departureBtn = (Button) findViewById(R.id.departurebtn);
|
71 |
|
|
final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn);
|
72 |
torben |
294 |
|
73 |
torben |
835 |
departureBtn.setOnClickListener( new OnClickListener() {
|
74 |
|
|
@Override
|
75 |
|
|
public void onClick(View arg0) {
|
76 |
|
|
arrivalBtn.setBackgroundResource(R.drawable.custom_button);
|
77 |
|
|
departureBtn.setBackgroundResource(R.drawable.custom_button_hilight);
|
78 |
|
|
arrival = false;
|
79 |
|
|
startDepartureFetcher();
|
80 |
|
|
}
|
81 |
|
|
});
|
82 |
|
|
arrivalBtn.setOnClickListener( new OnClickListener() {
|
83 |
|
|
@Override
|
84 |
|
|
public void onClick(View arg0) {
|
85 |
|
|
arrivalBtn.setBackgroundResource(R.drawable.custom_button_hilight);
|
86 |
|
|
departureBtn.setBackgroundResource(R.drawable.custom_button);
|
87 |
|
|
arrival = true;
|
88 |
|
|
startDepartureFetcher();
|
89 |
|
|
}
|
90 |
|
|
});
|
91 |
|
|
|
92 |
|
|
|
93 |
|
|
|
94 |
|
|
|
95 |
torben |
239 |
findViewById(R.id.header).setOnClickListener( mapLauncher );
|
96 |
torben |
237 |
|
97 |
torben |
557 |
int distance = station.getDistance();
|
98 |
torben |
742 |
if (distance != 0) {
|
99 |
|
|
NumberFormat format = NumberFormat.getNumberInstance();
|
100 |
|
|
format.setMaximumFractionDigits(1);
|
101 |
|
|
format.setMinimumFractionDigits(1);
|
102 |
torben |
575 |
|
103 |
torben |
742 |
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." );
|
104 |
|
|
} else {
|
105 |
|
|
((TextView) findViewById(R.id.stationDistance)).setVisibility(View.GONE);
|
106 |
|
|
}
|
107 |
|
|
|
108 |
torben |
552 |
|
109 |
torben |
557 |
if (station.isRegional() == false && station.isSTrain() == false) {
|
110 |
torben |
552 |
getListView().setVisibility( View.GONE );
|
111 |
|
|
findViewById(R.id.metroonly).setVisibility( View.VISIBLE );
|
112 |
torben |
835 |
departureBtn.setVisibility( View.GONE );
|
113 |
|
|
arrivalBtn.setVisibility(View.GONE);
|
114 |
torben |
552 |
|
115 |
torben |
257 |
} else {
|
116 |
torben |
552 |
provider = ProviderFactory.getDepartureProvider();
|
117 |
|
|
|
118 |
|
|
if (savedInstanceState == null) {
|
119 |
|
|
startDepartureFetcher();
|
120 |
|
|
} else {
|
121 |
|
|
departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures");
|
122 |
|
|
adapter.setDepartures(departures);
|
123 |
|
|
selectedItemId = savedInstanceState.getInt("selectedItemId");
|
124 |
|
|
}
|
125 |
torben |
257 |
}
|
126 |
torben |
237 |
}
|
127 |
|
|
|
128 |
torben |
243 |
@Override
|
129 |
|
|
public void onSaveInstanceState(Bundle outState)
|
130 |
|
|
{
|
131 |
torben |
257 |
if (pgDialog != null && pgDialog.isShowing())
|
132 |
|
|
dismissDialog(DLG_PROGRESS);
|
133 |
torben |
362 |
|
134 |
torben |
257 |
outState.putInt("selectedItemId", selectedItemId);
|
135 |
|
|
|
136 |
|
|
outState.putSerializable("departures", (ArrayList<DepartureBean>) departures);
|
137 |
torben |
243 |
}
|
138 |
torben |
918 |
|
139 |
|
|
|
140 |
torben |
243 |
|
141 |
torben |
237 |
@Override
|
142 |
torben |
918 |
protected void onDestroy() {
|
143 |
|
|
super.onDestroy();
|
144 |
|
|
|
145 |
|
|
if (fetcher != null) {
|
146 |
|
|
fetcher.cancel(true);
|
147 |
|
|
}
|
148 |
|
|
}
|
149 |
|
|
|
150 |
|
|
@Override
|
151 |
torben |
237 |
protected void onListItemClick(ListView l, View v, int position, long id) {
|
152 |
|
|
super.onListItemClick(l, v, position, id);
|
153 |
torben |
362 |
|
154 |
|
|
selectedItemId = position;
|
155 |
torben |
364 |
|
156 |
|
|
DepartureBean dep = departures.get(selectedItemId);
|
157 |
torben |
237 |
|
158 |
torben |
362 |
Intent intent = new Intent(this, TimetableList.class);
|
159 |
torben |
364 |
intent.putExtra("departure", dep);
|
160 |
torben |
362 |
|
161 |
|
|
startActivity(intent);
|
162 |
|
|
|
163 |
torben |
237 |
}
|
164 |
|
|
|
165 |
|
|
|
166 |
|
|
@Override
|
167 |
|
|
protected void onPrepareDialog(int id, Dialog dialog) {
|
168 |
|
|
super.onPrepareDialog(id, dialog);
|
169 |
torben |
257 |
|
170 |
torben |
237 |
switch (id) {
|
171 |
|
|
case DLG_PROGRESS:
|
172 |
|
|
pgDialog = (ProgressDialog) dialog;
|
173 |
torben |
835 |
int messageId = arrival == false ? departurelist_fetchdepartures : departurelist_fetcharrivals;
|
174 |
|
|
pgDialog.setMessage( getString(messageId) );
|
175 |
torben |
237 |
break;
|
176 |
|
|
}
|
177 |
|
|
}
|
178 |
|
|
|
179 |
|
|
@Override
|
180 |
|
|
protected Dialog onCreateDialog(int id) {
|
181 |
|
|
switch (id) {
|
182 |
|
|
case DLG_PROGRESS:
|
183 |
torben |
835 |
|
184 |
torben |
237 |
ProgressDialog dlg = new ProgressDialog(this);
|
185 |
|
|
dlg.setCancelable(true);
|
186 |
torben |
362 |
return dlg;
|
187 |
torben |
237 |
default:
|
188 |
|
|
return super.onCreateDialog(id);
|
189 |
|
|
}
|
190 |
|
|
}
|
191 |
torben |
336 |
|
192 |
|
|
void startDepartureFetcher() {
|
193 |
|
|
showDialog(DLG_PROGRESS);
|
194 |
|
|
fetcher = new DepartureFetcher();
|
195 |
torben |
557 |
fetcher.execute(station.getId());
|
196 |
torben |
336 |
}
|
197 |
torben |
316 |
|
198 |
torben |
237 |
class DialogDismisser implements View.OnClickListener {
|
199 |
|
|
|
200 |
|
|
Dialog dlg;
|
201 |
|
|
public DialogDismisser(Dialog d) {
|
202 |
|
|
dlg = d;
|
203 |
|
|
}
|
204 |
|
|
|
205 |
|
|
@Override
|
206 |
|
|
public void onClick(View v) {
|
207 |
|
|
if (dlg.isShowing())
|
208 |
|
|
dlg.dismiss();
|
209 |
torben |
365 |
}
|
210 |
|
|
}
|
211 |
torben |
238 |
|
212 |
torben |
239 |
View.OnClickListener mapLauncher = new View.OnClickListener() {
|
213 |
|
|
@Override
|
214 |
torben |
557 |
public void onClick(View v) {
|
215 |
|
|
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude());
|
216 |
torben |
239 |
startActivity( new Intent(Intent.ACTION_VIEW, uri));
|
217 |
|
|
}
|
218 |
|
|
};
|
219 |
torben |
336 |
|
220 |
|
|
|
221 |
torben |
238 |
|
222 |
torben |
310 |
class DepartureFetcher extends AsyncTask<Integer, Void, Void> {
|
223 |
torben |
238 |
|
224 |
torben |
319 |
boolean success;
|
225 |
torben |
365 |
|
226 |
torben |
238 |
@Override
|
227 |
|
|
protected void onPostExecute(Void result) {
|
228 |
|
|
super.onPostExecute(result);
|
229 |
|
|
|
230 |
torben |
336 |
|
231 |
torben |
238 |
pgDialog.dismiss();
|
232 |
|
|
|
233 |
torben |
319 |
if (success) {
|
234 |
torben |
917 |
DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html
|
235 |
torben |
336 |
adapter.setDepartures(departures);
|
236 |
torben |
917 |
DepartureList.this.getListView().setVisibility(View.VISIBLE);
|
237 |
|
|
|
238 |
torben |
319 |
if (departures.size() == 0) {
|
239 |
torben |
906 |
MessageBox.showMessage(DepartureList.this, "No departures found", true);
|
240 |
torben |
319 |
}
|
241 |
|
|
} else { // communication or parse error
|
242 |
torben |
336 |
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this);
|
243 |
|
|
builder.setMessage("Error finding departures");
|
244 |
|
|
builder.setCancelable(true);
|
245 |
torben |
561 |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
|
246 |
torben |
336 |
public void onClick(DialogInterface dialog, int id) {
|
247 |
|
|
dialog.dismiss();
|
248 |
|
|
startDepartureFetcher();
|
249 |
|
|
|
250 |
|
|
}
|
251 |
|
|
});
|
252 |
torben |
561 |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
|
253 |
torben |
336 |
public void onClick(DialogInterface dialog, int id) {
|
254 |
|
|
dialog.dismiss();
|
255 |
torben |
843 |
DepartureList.this.finish();
|
256 |
torben |
336 |
}
|
257 |
torben |
630 |
});
|
258 |
|
|
|
259 |
|
|
try {
|
260 |
|
|
builder.show();
|
261 |
|
|
} catch (android.view.WindowManager.BadTokenException e) {
|
262 |
|
|
Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running
|
263 |
|
|
}
|
264 |
torben |
319 |
}
|
265 |
torben |
238 |
}
|
266 |
|
|
|
267 |
|
|
@Override
|
268 |
torben |
310 |
protected Void doInBackground(Integer... params) {
|
269 |
torben |
835 |
success = provider.lookupDepartures(params[0], DepartureList.this.arrival);
|
270 |
|
|
departures = provider.getDepartures(params[0], DepartureList.this.arrival);
|
271 |
torben |
238 |
return null;
|
272 |
|
|
}
|
273 |
|
|
|
274 |
|
|
}
|
275 |
torben |
237 |
}
|