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