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