1 |
torben |
237 |
package dk.thoerup.traininfo;
|
2 |
|
|
|
3 |
|
|
import java.text.NumberFormat;
|
4 |
torben |
257 |
import java.util.ArrayList;
|
5 |
torben |
237 |
import java.util.List;
|
6 |
|
|
|
7 |
torben |
336 |
import android.app.AlertDialog;
|
8 |
torben |
237 |
import android.app.Dialog;
|
9 |
|
|
import android.app.ListActivity;
|
10 |
|
|
import android.app.ProgressDialog;
|
11 |
torben |
336 |
import android.content.DialogInterface;
|
12 |
torben |
237 |
import android.content.Intent;
|
13 |
torben |
239 |
import android.net.Uri;
|
14 |
torben |
237 |
import android.os.AsyncTask;
|
15 |
|
|
import android.os.Bundle;
|
16 |
|
|
import android.view.View;
|
17 |
|
|
import android.widget.ListView;
|
18 |
|
|
import android.widget.TextView;
|
19 |
torben |
255 |
import dk.thoerup.traininfo.provider.DepartureProvider;
|
20 |
torben |
253 |
import dk.thoerup.traininfo.provider.ProviderFactory;
|
21 |
torben |
245 |
import dk.thoerup.traininfo.util.MessageBox;
|
22 |
torben |
561 |
import static dk.thoerup.traininfo.R.string.*;
|
23 |
torben |
237 |
|
24 |
|
|
public class DepartureList extends ListActivity {
|
25 |
|
|
|
26 |
|
|
public static final int DLG_PROGRESS = 1;
|
27 |
|
|
|
28 |
torben |
362 |
|
29 |
torben |
237 |
DepartureListAdapter adapter;
|
30 |
|
|
DepartureProvider provider;
|
31 |
|
|
List<DepartureBean> departures;
|
32 |
|
|
|
33 |
torben |
257 |
int selectedItemId;
|
34 |
|
|
//DepartureBean currentDeparture;
|
35 |
torben |
237 |
|
36 |
|
|
ProgressDialog pgDialog;
|
37 |
torben |
362 |
|
38 |
torben |
238 |
DepartureFetcher fetcher;
|
39 |
torben |
557 |
|
40 |
|
|
StationBean station;
|
41 |
torben |
238 |
|
42 |
torben |
257 |
@SuppressWarnings("unchecked")
|
43 |
torben |
237 |
@Override
|
44 |
|
|
protected void onCreate(Bundle savedInstanceState) {
|
45 |
|
|
super.onCreate(savedInstanceState);
|
46 |
|
|
setContentView(R.layout.departurelist);
|
47 |
|
|
|
48 |
|
|
adapter = new DepartureListAdapter(this);
|
49 |
|
|
setListAdapter(adapter);
|
50 |
|
|
|
51 |
|
|
Intent launchedBy = getIntent();
|
52 |
torben |
557 |
|
53 |
|
|
station = (StationBean) launchedBy.getSerializableExtra("stationbean");
|
54 |
torben |
239 |
|
55 |
torben |
557 |
((TextView) findViewById(R.id.stationName)).setText( station.getName() );
|
56 |
torben |
317 |
|
57 |
torben |
557 |
|
58 |
|
|
((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() );
|
59 |
torben |
237 |
|
60 |
torben |
294 |
|
61 |
torben |
239 |
findViewById(R.id.header).setOnClickListener( mapLauncher );
|
62 |
torben |
237 |
|
63 |
|
|
NumberFormat format = NumberFormat.getNumberInstance();
|
64 |
|
|
format.setMaximumFractionDigits(1);
|
65 |
|
|
format.setMinimumFractionDigits(1);
|
66 |
torben |
557 |
|
67 |
|
|
|
68 |
|
|
int distance = station.getDistance();
|
69 |
torben |
237 |
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." );
|
70 |
torben |
238 |
|
71 |
torben |
552 |
|
72 |
torben |
557 |
if (station.isRegional() == false && station.isSTrain() == false) {
|
73 |
torben |
552 |
getListView().setVisibility( View.GONE );
|
74 |
|
|
findViewById(R.id.metroonly).setVisibility( View.VISIBLE );
|
75 |
|
|
|
76 |
torben |
257 |
} else {
|
77 |
torben |
552 |
provider = ProviderFactory.getDepartureProvider();
|
78 |
|
|
|
79 |
|
|
if (savedInstanceState == null) {
|
80 |
|
|
startDepartureFetcher();
|
81 |
|
|
} else {
|
82 |
|
|
departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures");
|
83 |
|
|
adapter.setDepartures(departures);
|
84 |
|
|
selectedItemId = savedInstanceState.getInt("selectedItemId");
|
85 |
|
|
}
|
86 |
torben |
257 |
}
|
87 |
torben |
237 |
}
|
88 |
|
|
|
89 |
torben |
243 |
@Override
|
90 |
|
|
public void onSaveInstanceState(Bundle outState)
|
91 |
|
|
{
|
92 |
torben |
257 |
if (pgDialog != null && pgDialog.isShowing())
|
93 |
|
|
dismissDialog(DLG_PROGRESS);
|
94 |
torben |
362 |
|
95 |
torben |
257 |
outState.putInt("selectedItemId", selectedItemId);
|
96 |
|
|
|
97 |
|
|
outState.putSerializable("departures", (ArrayList<DepartureBean>) departures);
|
98 |
torben |
243 |
}
|
99 |
|
|
|
100 |
torben |
237 |
@Override
|
101 |
|
|
protected void onListItemClick(ListView l, View v, int position, long id) {
|
102 |
|
|
super.onListItemClick(l, v, position, id);
|
103 |
torben |
362 |
|
104 |
|
|
selectedItemId = position;
|
105 |
torben |
364 |
|
106 |
|
|
DepartureBean dep = departures.get(selectedItemId);
|
107 |
torben |
237 |
|
108 |
torben |
362 |
Intent intent = new Intent(this, TimetableList.class);
|
109 |
torben |
364 |
intent.putExtra("departure", dep);
|
110 |
torben |
362 |
|
111 |
|
|
startActivity(intent);
|
112 |
|
|
|
113 |
torben |
237 |
}
|
114 |
|
|
|
115 |
|
|
|
116 |
|
|
@Override
|
117 |
|
|
protected void onPrepareDialog(int id, Dialog dialog) {
|
118 |
|
|
super.onPrepareDialog(id, dialog);
|
119 |
torben |
257 |
|
120 |
torben |
237 |
switch (id) {
|
121 |
|
|
case DLG_PROGRESS:
|
122 |
|
|
pgDialog = (ProgressDialog) dialog;
|
123 |
|
|
break;
|
124 |
|
|
}
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
@Override
|
128 |
|
|
protected Dialog onCreateDialog(int id) {
|
129 |
|
|
switch (id) {
|
130 |
|
|
case DLG_PROGRESS:
|
131 |
|
|
ProgressDialog dlg = new ProgressDialog(this);
|
132 |
torben |
561 |
dlg.setMessage( getString(departurelist_fetchdata) );
|
133 |
torben |
237 |
dlg.setCancelable(true);
|
134 |
torben |
362 |
return dlg;
|
135 |
torben |
237 |
default:
|
136 |
|
|
return super.onCreateDialog(id);
|
137 |
|
|
}
|
138 |
|
|
}
|
139 |
torben |
336 |
|
140 |
|
|
void startDepartureFetcher() {
|
141 |
|
|
showDialog(DLG_PROGRESS);
|
142 |
|
|
fetcher = new DepartureFetcher();
|
143 |
torben |
557 |
fetcher.execute(station.getId());
|
144 |
torben |
336 |
}
|
145 |
torben |
316 |
|
146 |
torben |
237 |
class DialogDismisser implements View.OnClickListener {
|
147 |
|
|
|
148 |
|
|
Dialog dlg;
|
149 |
|
|
public DialogDismisser(Dialog d) {
|
150 |
|
|
dlg = d;
|
151 |
|
|
}
|
152 |
|
|
|
153 |
|
|
@Override
|
154 |
|
|
public void onClick(View v) {
|
155 |
|
|
if (dlg.isShowing())
|
156 |
|
|
dlg.dismiss();
|
157 |
torben |
365 |
}
|
158 |
|
|
}
|
159 |
torben |
238 |
|
160 |
torben |
239 |
View.OnClickListener mapLauncher = new View.OnClickListener() {
|
161 |
|
|
@Override
|
162 |
torben |
557 |
public void onClick(View v) {
|
163 |
|
|
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude());
|
164 |
torben |
239 |
startActivity( new Intent(Intent.ACTION_VIEW, uri));
|
165 |
|
|
}
|
166 |
|
|
};
|
167 |
torben |
336 |
|
168 |
|
|
|
169 |
torben |
238 |
|
170 |
torben |
310 |
class DepartureFetcher extends AsyncTask<Integer, Void, Void> {
|
171 |
torben |
238 |
|
172 |
torben |
319 |
boolean success;
|
173 |
torben |
365 |
|
174 |
torben |
238 |
@Override
|
175 |
|
|
protected void onPostExecute(Void result) {
|
176 |
|
|
super.onPostExecute(result);
|
177 |
|
|
|
178 |
torben |
336 |
|
179 |
torben |
238 |
pgDialog.dismiss();
|
180 |
|
|
|
181 |
torben |
319 |
if (success) {
|
182 |
torben |
336 |
adapter.setDepartures(departures);
|
183 |
torben |
319 |
if (departures.size() == 0) {
|
184 |
|
|
MessageBox.showMessage(DepartureList.this, "No departures found");
|
185 |
|
|
}
|
186 |
|
|
} else { // communication or parse error
|
187 |
torben |
336 |
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this);
|
188 |
|
|
builder.setMessage("Error finding departures");
|
189 |
|
|
builder.setCancelable(true);
|
190 |
torben |
561 |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
|
191 |
torben |
336 |
public void onClick(DialogInterface dialog, int id) {
|
192 |
|
|
dialog.dismiss();
|
193 |
|
|
startDepartureFetcher();
|
194 |
|
|
|
195 |
|
|
}
|
196 |
|
|
});
|
197 |
torben |
561 |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
|
198 |
torben |
336 |
public void onClick(DialogInterface dialog, int id) {
|
199 |
|
|
dialog.dismiss();
|
200 |
|
|
}
|
201 |
|
|
});
|
202 |
|
|
builder.show();
|
203 |
torben |
319 |
}
|
204 |
torben |
238 |
}
|
205 |
|
|
|
206 |
|
|
@Override
|
207 |
torben |
310 |
protected Void doInBackground(Integer... params) {
|
208 |
torben |
319 |
success = provider.lookupDepartures(params[0]);
|
209 |
|
|
departures = provider.getDepartures(params[0]);
|
210 |
torben |
238 |
return null;
|
211 |
|
|
}
|
212 |
|
|
|
213 |
|
|
}
|
214 |
torben |
237 |
}
|