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.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; |
import android.view.View.OnClickListener; |
28 |
import android.widget.Button; |
import android.widget.Button; |
29 |
import android.widget.ListView; |
import android.widget.ListView; |
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 |
|
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; |
54 |
|
|
55 |
boolean arrival = false; |
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); |
97 |
|
|
98 |
|
|
99 |
|
|
100 |
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
// findViewById(R.id.header).setOnClickListener( mapLauncher ); |
101 |
|
|
102 |
int distance = station.getDistance(); |
int distance = station.getDistance(); |
103 |
if (distance != 0) { |
if (distance != 0) { |
123 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
124 |
startDepartureFetcher(); |
startDepartureFetcher(); |
125 |
} else { |
} else { |
126 |
departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures"); |
departures = (DepartureBean) savedInstanceState.getSerializable("departures"); |
127 |
adapter.setDepartures(departures); |
adapter.setDepartures(departures.entries); |
128 |
selectedItemId = savedInstanceState.getInt("selectedItemId"); |
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 |
143 |
public void onSaveInstanceState(Bundle outState) |
public void onSaveInstanceState(Bundle outState) |
144 |
{ |
{ |
147 |
|
|
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; |
selectedItemId = position; |
169 |
|
|
170 |
DepartureBean dep = departures.get(selectedItemId); |
DepartureEntry dep = departures.entries.get(selectedItemId); |
171 |
|
|
172 |
Intent intent = new Intent(this, TimetableList.class); |
Intent intent = new Intent(this, TimetableList.class); |
173 |
intent.putExtra("departure", dep); |
intent.putExtra("departure", dep); |
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); |
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:" + station.getLatitude() + "," + station.getLongitude()); |
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 |
|
|
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); |
313 |
builder.setNegativeButton(getString(generic_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 |
|
|