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; |
|
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; |
30 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
import dk.thoerup.traininfo.provider.ProviderFactory; |
31 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
|
import static dk.thoerup.traininfo.R.string.*; |
|
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 |
|
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 |
|
|
56 |
@Override |
@Override |
57 |
protected void onCreate(Bundle savedInstanceState) { |
protected void onCreate(Bundle savedInstanceState) { |
58 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
70 |
|
|
71 |
((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() ); |
((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 |
|
departureBtn.setOnClickListener( new OnClickListener() { |
77 |
|
@Override |
78 |
|
public void onClick(View arg0) { |
79 |
|
arrivalBtn.setBackgroundResource(R.drawable.custom_button); |
80 |
|
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 |
|
|
95 |
|
|
|
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
|
96 |
|
|
|
NumberFormat format = NumberFormat.getNumberInstance(); |
|
|
format.setMaximumFractionDigits(1); |
|
|
format.setMinimumFractionDigits(1); |
|
97 |
|
|
98 |
|
// findViewById(R.id.header).setOnClickListener( mapLauncher ); |
99 |
|
|
100 |
int distance = station.getDistance(); |
int distance = station.getDistance(); |
101 |
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); |
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) { |
if (station.isRegional() == false && station.isSTrain() == false) { |
113 |
getListView().setVisibility( View.GONE ); |
getListView().setVisibility( View.GONE ); |
114 |
findViewById(R.id.metroonly).setVisibility( View.VISIBLE ); |
findViewById(R.id.metroonly).setVisibility( View.VISIBLE ); |
115 |
|
departureBtn.setVisibility( View.GONE ); |
116 |
|
arrivalBtn.setVisibility(View.GONE); |
117 |
|
|
118 |
} else { |
} else { |
119 |
provider = ProviderFactory.getDepartureProvider(); |
provider = ProviderFactory.getDepartureProvider(); |
121 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
122 |
startDepartureFetcher(); |
startDepartureFetcher(); |
123 |
} else { |
} else { |
124 |
departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures"); |
departures = (DepartureBean) savedInstanceState.getSerializable("departures"); |
125 |
adapter.setDepartures(departures); |
adapter.setDepartures(departures.entries); |
126 |
selectedItemId = savedInstanceState.getInt("selectedItemId"); |
selectedItemId = savedInstanceState.getInt("selectedItemId"); |
127 |
|
|
128 |
|
if ( hasNotifications() ) { |
129 |
|
findViewById(R.id.notifIcon).setVisibility(View.VISIBLE); |
130 |
|
} |
131 |
|
|
132 |
} |
} |
133 |
} |
} |
134 |
} |
} |
135 |
|
|
136 |
|
boolean hasNotifications() { |
137 |
|
return (departures != null && departures.notifications.size() > 0); |
138 |
|
} |
139 |
|
|
140 |
@Override |
@Override |
141 |
public void onSaveInstanceState(Bundle outState) |
public void onSaveInstanceState(Bundle outState) |
142 |
{ |
{ |
145 |
|
|
146 |
outState.putInt("selectedItemId", selectedItemId); |
outState.putInt("selectedItemId", selectedItemId); |
147 |
|
|
148 |
outState.putSerializable("departures", (ArrayList<DepartureBean>) departures); |
outState.putSerializable("departures", departures); |
149 |
} |
} |
150 |
|
|
151 |
|
|
152 |
|
|
153 |
@Override |
@Override |
154 |
|
protected void onDestroy() { |
155 |
|
super.onDestroy(); |
156 |
|
|
157 |
|
if (fetcher != null) { |
158 |
|
fetcher.cancel(true); |
159 |
|
} |
160 |
|
} |
161 |
|
|
162 |
|
@Override |
163 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
164 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
165 |
|
|
166 |
selectedItemId = position; |
selectedItemId = position; |
167 |
|
|
168 |
DepartureBean dep = departures.get(selectedItemId); |
DepartureEntry dep = departures.entries.get(selectedItemId); |
169 |
|
|
170 |
Intent intent = new Intent(this, TimetableList.class); |
Intent intent = new Intent(this, TimetableList.class); |
171 |
intent.putExtra("departure", dep); |
intent.putExtra("departure", dep); |
182 |
switch (id) { |
switch (id) { |
183 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
184 |
pgDialog = (ProgressDialog) dialog; |
pgDialog = (ProgressDialog) dialog; |
185 |
|
int messageId = arrival == false ? departurelist_fetchdepartures : departurelist_fetcharrivals; |
186 |
|
pgDialog.setMessage( getString(messageId) ); |
187 |
break; |
break; |
188 |
} |
} |
189 |
} |
} |
192 |
protected Dialog onCreateDialog(int id) { |
protected Dialog onCreateDialog(int id) { |
193 |
switch (id) { |
switch (id) { |
194 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
195 |
|
|
196 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
|
dlg.setMessage( getString(departurelist_fetchdata) ); |
|
197 |
dlg.setCancelable(true); |
dlg.setCancelable(true); |
198 |
return dlg; |
return dlg; |
199 |
default: |
default: |
200 |
return super.onCreateDialog(id); |
return super.onCreateDialog(id); |
201 |
} |
} |
202 |
} |
} |
203 |
|
|
204 |
|
|
205 |
|
|
206 |
|
|
207 |
|
|
208 |
|
@Override |
209 |
|
public boolean onCreateOptionsMenu(Menu menu) { |
210 |
|
MenuItem item; |
211 |
|
|
212 |
|
item = menu.add(0, MENU_MAP, 0, getString(R.string.departurelist_showonmap) ); |
213 |
|
item.setIcon(android.R.drawable.ic_menu_mapmode); |
214 |
|
|
215 |
|
item = menu.add(0, MENU_NOTIFICATIONS, 0, getString(R.string.departurelist_notifications) ); |
216 |
|
item.setIcon(android.R.drawable.ic_menu_info_details); |
217 |
|
|
218 |
|
|
219 |
|
boolean notifEnabled = hasNotifications(); |
220 |
|
item.setEnabled(notifEnabled); |
221 |
|
|
222 |
|
|
223 |
|
return true; |
224 |
|
} |
225 |
|
|
226 |
|
@Override |
227 |
|
public boolean onOptionsItemSelected(MenuItem item) { |
228 |
|
boolean res; |
229 |
|
switch(item.getItemId()) { |
230 |
|
case MENU_MAP: |
231 |
|
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude()); |
232 |
|
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
233 |
|
res = true; |
234 |
|
break; |
235 |
|
case MENU_NOTIFICATIONS: |
236 |
|
Intent i = new Intent(this,dk.thoerup.traininfo.NotificationList.class); |
237 |
|
i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications); |
238 |
|
startActivity(i); |
239 |
|
res = true; |
240 |
|
break; |
241 |
|
default: |
242 |
|
res = super.onOptionsItemSelected(item); |
243 |
|
} |
244 |
|
return res; |
245 |
|
} |
246 |
|
|
247 |
void startDepartureFetcher() { |
void startDepartureFetcher() { |
248 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
264 |
} |
} |
265 |
} |
} |
266 |
|
|
267 |
View.OnClickListener mapLauncher = new View.OnClickListener() { |
/*View.OnClickListener mapLauncher = new View.OnClickListener() { |
268 |
@Override |
@Override |
269 |
public void onClick(View v) { |
public void onClick(View v) { |
270 |
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude()); |
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude()); |
271 |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
272 |
} |
} |
273 |
}; |
};*/ |
274 |
|
|
275 |
|
|
276 |
|
|
286 |
pgDialog.dismiss(); |
pgDialog.dismiss(); |
287 |
|
|
288 |
if (success) { |
if (success) { |
289 |
adapter.setDepartures(departures); |
DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html |
290 |
if (departures.size() == 0) { |
adapter.setDepartures(departures.entries); |
291 |
MessageBox.showMessage(DepartureList.this, "No departures found"); |
DepartureList.this.getListView().setVisibility(View.VISIBLE); |
292 |
|
|
293 |
|
|
294 |
|
if ( hasNotifications() ) { |
295 |
|
findViewById(R.id.notifIcon).setVisibility(View.VISIBLE); |
296 |
|
} |
297 |
|
|
298 |
|
if (departures.entries.size() == 0) { |
299 |
|
MessageBox.showMessage(DepartureList.this, "No departures found", true); |
300 |
} |
} |
301 |
} else { // communication or parse error |
} else { // communication or parse error |
302 |
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
312 |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
313 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
314 |
dialog.dismiss(); |
dialog.dismiss(); |
315 |
|
DepartureList.this.finish(); |
316 |
} |
} |
317 |
}); |
}); |
318 |
|
|
326 |
|
|
327 |
@Override |
@Override |
328 |
protected Void doInBackground(Integer... params) { |
protected Void doInBackground(Integer... params) { |
329 |
success = provider.lookupDepartures(params[0]); |
success = provider.lookupDepartures(params[0], DepartureList.this.arrival); |
330 |
departures = provider.getDepartures(params[0]); |
departures = provider.getDepartures(params[0], DepartureList.this.arrival); |
331 |
return null; |
return null; |
332 |
} |
} |
333 |
|
|