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