1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
3 |
import static dk.thoerup.traininfo.R.string.departurelist_fetchdata; |
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; |
import static dk.thoerup.traininfo.R.string.generic_cancel; |
6 |
import static dk.thoerup.traininfo.R.string.generic_retry; |
import static dk.thoerup.traininfo.R.string.generic_retry; |
7 |
|
|
8 |
|
|
9 |
import java.text.NumberFormat; |
import java.text.NumberFormat; |
10 |
import java.util.ArrayList; |
|
|
import java.util.List; |
|
11 |
|
|
12 |
import android.app.AlertDialog; |
import android.app.AlertDialog; |
13 |
import android.app.Dialog; |
import android.app.Dialog; |
19 |
import android.os.AsyncTask; |
import android.os.AsyncTask; |
20 |
import android.os.Bundle; |
import android.os.Bundle; |
21 |
import android.util.Log; |
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; |
29 |
import dk.thoerup.traininfo.provider.DepartureProvider; |
import dk.thoerup.traininfo.provider.DepartureProvider; |
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 |
|
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 |
int selectedItemId; |
int selectedItemId; |
45 |
//DepartureBean currentDeparture; |
//DepartureBean currentDeparture; |
50 |
|
|
51 |
StationBean station; |
StationBean station; |
52 |
|
|
53 |
@SuppressWarnings("unchecked") |
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); |
71 |
|
|
72 |
((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() ); |
((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() ); |
73 |
|
|
74 |
|
final Button departureBtn = (Button) findViewById(R.id.departurebtn); |
75 |
|
final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn); |
76 |
|
|
77 |
|
departureBtn.setOnClickListener( new OnClickListener() { |
78 |
|
@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 |
|
|
96 |
|
|
|
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
|
97 |
|
|
|
NumberFormat format = NumberFormat.getNumberInstance(); |
|
|
format.setMaximumFractionDigits(1); |
|
|
format.setMinimumFractionDigits(1); |
|
98 |
|
|
99 |
|
// findViewById(R.id.header).setOnClickListener( mapLauncher ); |
100 |
|
|
101 |
int distance = station.getDistance(); |
int distance = station.getDistance(); |
102 |
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); |
if (distance != 0) { |
103 |
station.setRegional(true); |
NumberFormat format = NumberFormat.getNumberInstance(); |
104 |
Log.e("ASD", "" + station.isRegional() + "/" + station.isSTrain() + "/" + station.isMetro()); |
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) { |
if (station.isRegional() == false && station.isSTrain() == false) { |
114 |
getListView().setVisibility( View.GONE ); |
getListView().setVisibility( View.GONE ); |
115 |
findViewById(R.id.metroonly).setVisibility( View.VISIBLE ); |
findViewById(R.id.metroonly).setVisibility( View.VISIBLE ); |
116 |
|
departureBtn.setVisibility( View.GONE ); |
117 |
|
arrivalBtn.setVisibility(View.GONE); |
118 |
|
|
119 |
} else { |
} else { |
120 |
provider = ProviderFactory.getDepartureProvider(); |
provider = ProviderFactory.getDepartureProvider(); |
122 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
123 |
startDepartureFetcher(); |
startDepartureFetcher(); |
124 |
} else { |
} else { |
125 |
departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures"); |
departures = (DepartureBean) savedInstanceState.getSerializable("departures"); |
126 |
adapter.setDepartures(departures); |
|
127 |
selectedItemId = savedInstanceState.getInt("selectedItemId"); |
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 |
|
@Override |
141 |
|
protected void onStart() { |
142 |
|
super.onStart(); |
143 |
|
ProviderFactory.purgeOldEntries(); |
144 |
|
} |
145 |
|
|
146 |
|
boolean hasNotifications() { |
147 |
|
return (departures != null && departures.notifications.size() > 0); |
148 |
|
} |
149 |
|
|
150 |
@Override |
@Override |
151 |
public void onSaveInstanceState(Bundle outState) |
public void onSaveInstanceState(Bundle outState) |
152 |
{ |
{ |
155 |
|
|
156 |
outState.putInt("selectedItemId", selectedItemId); |
outState.putInt("selectedItemId", selectedItemId); |
157 |
|
|
158 |
outState.putSerializable("departures", (ArrayList<DepartureBean>) departures); |
outState.putSerializable("departures", departures); |
159 |
} |
} |
160 |
|
|
161 |
|
|
162 |
|
|
163 |
@Override |
@Override |
164 |
|
protected void onDestroy() { |
165 |
|
super.onDestroy(); |
166 |
|
|
167 |
|
if (fetcher != null) { |
168 |
|
fetcher.cancel(true); |
169 |
|
} |
170 |
|
} |
171 |
|
|
172 |
|
@Override |
173 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
174 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
175 |
|
|
176 |
selectedItemId = position; |
selectedItemId = position; |
177 |
|
|
178 |
DepartureBean dep = departures.get(selectedItemId); |
DepartureEntry dep = departures.entries.get(selectedItemId); |
179 |
|
|
180 |
Intent intent = new Intent(this, TimetableList.class); |
Intent intent = new Intent(this, TimetableList.class); |
181 |
intent.putExtra("departure", dep); |
intent.putExtra("departure", dep); |
192 |
switch (id) { |
switch (id) { |
193 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
194 |
pgDialog = (ProgressDialog) dialog; |
pgDialog = (ProgressDialog) dialog; |
195 |
|
int messageId = arrival == false ? departurelist_fetchdepartures : departurelist_fetcharrivals; |
196 |
|
pgDialog.setMessage( getString(messageId) ); |
197 |
break; |
break; |
198 |
} |
} |
199 |
} |
} |
202 |
protected Dialog onCreateDialog(int id) { |
protected Dialog onCreateDialog(int id) { |
203 |
switch (id) { |
switch (id) { |
204 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
205 |
|
|
206 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
|
dlg.setMessage( getString(departurelist_fetchdata) ); |
|
207 |
dlg.setCancelable(true); |
dlg.setCancelable(true); |
208 |
return dlg; |
return dlg; |
209 |
default: |
default: |
210 |
return super.onCreateDialog(id); |
return super.onCreateDialog(id); |
211 |
} |
} |
212 |
} |
} |
213 |
|
|
214 |
|
|
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
@Override |
219 |
|
public boolean onCreateOptionsMenu(Menu menu) { |
220 |
|
MenuItem item; |
221 |
|
|
222 |
|
item = menu.add(0, MENU_MAP, 0, getString(R.string.departurelist_showonmap) ); |
223 |
|
item.setIcon(android.R.drawable.ic_menu_mapmode); |
224 |
|
|
225 |
|
item = menu.add(0, MENU_NOTIFICATIONS, 0, getString(R.string.departurelist_notifications) ); |
226 |
|
item.setIcon(android.R.drawable.ic_menu_info_details); |
227 |
|
|
228 |
|
|
229 |
|
boolean notifEnabled = hasNotifications(); |
230 |
|
item.setEnabled(notifEnabled); |
231 |
|
|
232 |
|
|
233 |
|
return true; |
234 |
|
} |
235 |
|
|
236 |
|
@Override |
237 |
|
public boolean onOptionsItemSelected(MenuItem item) { |
238 |
|
boolean res; |
239 |
|
switch(item.getItemId()) { |
240 |
|
case MENU_MAP: |
241 |
|
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude()); |
242 |
|
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
243 |
|
res = true; |
244 |
|
break; |
245 |
|
case MENU_NOTIFICATIONS: |
246 |
|
Intent i = new Intent(this,dk.thoerup.traininfo.NotificationList.class); |
247 |
|
i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications); |
248 |
|
startActivity(i); |
249 |
|
res = true; |
250 |
|
break; |
251 |
|
default: |
252 |
|
res = super.onOptionsItemSelected(item); |
253 |
|
} |
254 |
|
return res; |
255 |
|
} |
256 |
|
|
257 |
void startDepartureFetcher() { |
void startDepartureFetcher() { |
258 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
274 |
} |
} |
275 |
} |
} |
276 |
|
|
277 |
View.OnClickListener mapLauncher = new View.OnClickListener() { |
/*View.OnClickListener mapLauncher = new View.OnClickListener() { |
278 |
@Override |
@Override |
279 |
public void onClick(View v) { |
public void onClick(View v) { |
280 |
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude()); |
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude()); |
281 |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
282 |
} |
} |
283 |
}; |
};*/ |
284 |
|
|
285 |
|
|
286 |
|
|
287 |
class DepartureFetcher extends AsyncTask<Integer, Void, Void> { |
class DepartureFetcher extends AsyncTask<Integer, Void, Void> { |
288 |
|
|
|
boolean success; |
|
289 |
|
|
290 |
@Override |
@Override |
291 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
294 |
|
|
295 |
pgDialog.dismiss(); |
pgDialog.dismiss(); |
296 |
|
|
297 |
if (success) { |
if (departures != null) { |
298 |
adapter.setDepartures(departures); |
commFailCounter = 0; |
299 |
if (departures.size() == 0) { |
DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html |
300 |
MessageBox.showMessage(DepartureList.this, "No departures found"); |
adapter.setDepartures(departures.entries); |
301 |
|
DepartureList.this.getListView().setVisibility(View.VISIBLE); |
302 |
|
|
303 |
|
|
304 |
|
if ( hasNotifications() ) { |
305 |
|
findViewById(R.id.notifIcon).setVisibility(View.VISIBLE); |
306 |
|
} |
307 |
|
|
308 |
|
if (departures.entries.size() == 0) { |
309 |
|
MessageBox.showMessage(DepartureList.this, "No departures found", true); |
310 |
} |
} |
311 |
} else { // communication or parse error |
} else { // communication or parse error |
312 |
|
commFailCounter++; |
313 |
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
314 |
builder.setMessage("Error finding departures"); |
builder.setMessage("Error finding departures"); |
315 |
builder.setCancelable(true); |
builder.setCancelable(true); |
316 |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
if (commFailCounter < 3) { |
317 |
public void onClick(DialogInterface dialog, int id) { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
318 |
dialog.dismiss(); |
public void onClick(DialogInterface dialog, int id) { |
319 |
startDepartureFetcher(); |
dialog.dismiss(); |
320 |
|
startDepartureFetcher(); |
321 |
} |
|
322 |
}); |
} |
323 |
|
}); |
324 |
|
} |
325 |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
326 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
327 |
dialog.dismiss(); |
dialog.dismiss(); |
328 |
|
DepartureList.this.finish(); |
329 |
} |
} |
330 |
}); |
}); |
331 |
builder.show(); |
|
332 |
|
try { |
333 |
|
builder.show(); |
334 |
|
} catch (android.view.WindowManager.BadTokenException e) { |
335 |
|
Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |
336 |
|
} |
337 |
} |
} |
338 |
} |
} |
339 |
|
|
340 |
@Override |
@Override |
341 |
protected Void doInBackground(Integer... params) { |
protected Void doInBackground(Integer... params) { |
342 |
success = provider.lookupDepartures(params[0]); |
departures = provider.lookupDepartures(params[0], DepartureList.this.arrival); |
|
departures = provider.getDepartures(params[0]); |
|
343 |
return null; |
return null; |
344 |
} |
} |
345 |
|
|