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; |
import android.view.View.OnClickListener; |
26 |
import android.widget.Button; |
import android.widget.Button; |
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; |
52 |
|
|
53 |
boolean arrival = false; |
boolean arrival = false; |
54 |
|
|
55 |
@SuppressWarnings("unchecked") |
|
56 |
@Override |
@Override |
57 |
protected void onCreate(Bundle savedInstanceState) { |
protected void onCreate(Bundle savedInstanceState) { |
58 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
95 |
|
|
96 |
|
|
97 |
|
|
98 |
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
// findViewById(R.id.header).setOnClickListener( mapLauncher ); |
99 |
|
|
100 |
int distance = station.getDistance(); |
int distance = station.getDistance(); |
101 |
if (distance != 0) { |
if (distance != 0) { |
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); |
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 |
|
item = menu.add(0, MENU_MAP, 0, "Show on map"); |
212 |
|
item.setIcon(android.R.drawable.ic_menu_mapmode); |
213 |
|
|
214 |
|
item = menu.add(0, MENU_NOTIFICATIONS, 0, "Notifications"); |
215 |
|
item.setIcon(android.R.drawable.ic_menu_info_details); |
216 |
|
|
217 |
|
|
218 |
|
boolean notifEnabled = hasNotifications(); |
219 |
|
item.setEnabled(notifEnabled); |
220 |
|
|
221 |
|
|
222 |
|
return true; |
223 |
|
} |
224 |
|
|
225 |
|
@Override |
226 |
|
public boolean onOptionsItemSelected(MenuItem item) { |
227 |
|
boolean res; |
228 |
|
switch(item.getItemId()) { |
229 |
|
case MENU_MAP: |
230 |
|
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude()); |
231 |
|
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
232 |
|
res = true; |
233 |
|
break; |
234 |
|
case MENU_NOTIFICATIONS: |
235 |
|
Intent i = new Intent(this,dk.thoerup.traininfo.NotificationList.class); |
236 |
|
i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications); |
237 |
|
startActivity(i); |
238 |
|
res = true; |
239 |
|
break; |
240 |
|
default: |
241 |
|
res = super.onOptionsItemSelected(item); |
242 |
|
} |
243 |
|
return res; |
244 |
|
} |
245 |
|
|
246 |
void startDepartureFetcher() { |
void startDepartureFetcher() { |
247 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
263 |
} |
} |
264 |
} |
} |
265 |
|
|
266 |
View.OnClickListener mapLauncher = new View.OnClickListener() { |
/*View.OnClickListener mapLauncher = new View.OnClickListener() { |
267 |
@Override |
@Override |
268 |
public void onClick(View v) { |
public void onClick(View v) { |
269 |
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude()); |
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude()); |
270 |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
271 |
} |
} |
272 |
}; |
};*/ |
273 |
|
|
274 |
|
|
275 |
|
|
285 |
pgDialog.dismiss(); |
pgDialog.dismiss(); |
286 |
|
|
287 |
if (success) { |
if (success) { |
288 |
adapter.setDepartures(departures); |
DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html |
289 |
if (departures.size() == 0) { |
adapter.setDepartures(departures.entries); |
290 |
MessageBox.showMessage(DepartureList.this, "No departures found"); |
DepartureList.this.getListView().setVisibility(View.VISIBLE); |
291 |
|
|
292 |
|
|
293 |
|
if ( hasNotifications() ) { |
294 |
|
findViewById(R.id.notifIcon).setVisibility(View.VISIBLE); |
295 |
|
} |
296 |
|
|
297 |
|
if (departures.entries.size() == 0) { |
298 |
|
MessageBox.showMessage(DepartureList.this, "No departures found", true); |
299 |
} |
} |
300 |
} else { // communication or parse error |
} else { // communication or parse error |
301 |
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
311 |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
312 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
313 |
dialog.dismiss(); |
dialog.dismiss(); |
314 |
|
DepartureList.this.finish(); |
315 |
} |
} |
316 |
}); |
}); |
317 |
|
|