11 |
import android.app.Dialog; |
import android.app.Dialog; |
12 |
import android.app.ListActivity; |
import android.app.ListActivity; |
13 |
import android.app.ProgressDialog; |
import android.app.ProgressDialog; |
14 |
|
import android.content.ActivityNotFoundException; |
15 |
import android.content.DialogInterface; |
import android.content.DialogInterface; |
16 |
import android.content.Intent; |
import android.content.Intent; |
17 |
|
import android.content.SharedPreferences; |
18 |
import android.graphics.Typeface; |
import android.graphics.Typeface; |
19 |
import android.net.Uri; |
import android.net.Uri; |
20 |
import android.os.AsyncTask; |
import android.os.AsyncTask; |
21 |
import android.os.Bundle; |
import android.os.Bundle; |
22 |
|
import android.preference.PreferenceManager; |
23 |
import android.util.Log; |
import android.util.Log; |
24 |
import android.view.Menu; |
import android.view.Menu; |
25 |
import android.view.MenuItem; |
import android.view.MenuItem; |
30 |
import android.widget.TableLayout; |
import android.widget.TableLayout; |
31 |
import android.widget.TableRow; |
import android.widget.TableRow; |
32 |
import android.widget.TextView; |
import android.widget.TextView; |
33 |
|
import android.widget.Toast; |
34 |
import dk.thoerup.android.traininfo.common.DepartureBean; |
import dk.thoerup.android.traininfo.common.DepartureBean; |
35 |
import dk.thoerup.android.traininfo.common.DepartureEntry; |
import dk.thoerup.android.traininfo.common.DepartureEntry; |
36 |
import dk.thoerup.android.traininfo.common.MetroBean; |
import dk.thoerup.android.traininfo.common.MetroBean; |
37 |
import dk.thoerup.android.traininfo.common.MetroBean.MetroEntry; |
import dk.thoerup.android.traininfo.common.MetroBean.MetroEntry; |
38 |
import dk.thoerup.android.traininfo.common.StationBean.StationEntry; |
import dk.thoerup.android.traininfo.common.StationEntry; |
39 |
import dk.thoerup.traininfo.provider.DepartureProvider; |
import dk.thoerup.traininfo.provider.DepartureProvider; |
40 |
import dk.thoerup.traininfo.provider.MetroProvider; |
import dk.thoerup.traininfo.provider.MetroProvider; |
41 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
import dk.thoerup.traininfo.provider.ProviderFactory; |
46 |
public static final int DLG_PROGRESS = 1; |
public static final int DLG_PROGRESS = 1; |
47 |
static final int MENU_MAP = 100; |
static final int MENU_MAP = 100; |
48 |
static final int MENU_NOTIFICATIONS = 101; |
static final int MENU_NOTIFICATIONS = 101; |
49 |
static final int MENU_METROMAP= 102; |
static final int MENU_METROMAP = 102; |
50 |
|
static final int MENU_TOGGLEDETAILS= 103; |
51 |
|
|
52 |
|
|
53 |
DepartureListAdapter adapter; |
DepartureListAdapter adapter; |
67 |
|
|
68 |
StationEntry station; |
StationEntry station; |
69 |
|
|
70 |
boolean arrival = false; |
String trainType = "REGIONAL"; |
71 |
|
|
72 |
|
boolean arrival = false; |
73 |
|
|
74 |
int commFailCounter = 0; |
int commFailCounter = 0; |
75 |
|
|
80 |
|
|
81 |
adapter = new DepartureListAdapter(this); |
adapter = new DepartureListAdapter(this); |
82 |
setListAdapter(adapter); |
setListAdapter(adapter); |
83 |
|
|
84 |
Intent launchedBy = getIntent(); |
Intent launchedBy = getIntent(); |
85 |
|
|
86 |
station = (StationEntry) launchedBy.getSerializableExtra("stationbean"); |
station = (StationEntry) launchedBy.getSerializableExtra("stationbean"); |
93 |
final Button departureBtn = (Button) findViewById(R.id.departurebtn); |
final Button departureBtn = (Button) findViewById(R.id.departurebtn); |
94 |
final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn); |
final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn); |
95 |
final Button metroBtn = (Button) findViewById(R.id.metrobtn); |
final Button metroBtn = (Button) findViewById(R.id.metrobtn); |
96 |
|
final Button regionalBtn = (Button) findViewById(R.id.regionalbtn); |
97 |
|
final Button stogBtn = (Button) findViewById(R.id.stogbtn); |
98 |
|
|
99 |
final View metroView = findViewById(R.id.metroonly); |
final View metroView = findViewById(R.id.metroonly); |
100 |
|
|
125 |
} |
} |
126 |
}); |
}); |
127 |
|
|
128 |
|
regionalBtn.setOnClickListener( new OnClickListener() { |
129 |
|
@Override |
130 |
|
public void onClick(View arg0) { |
131 |
|
regionalBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
132 |
|
stogBtn.setBackgroundResource(R.drawable.custom_button); |
133 |
|
metroBtn.setBackgroundResource(R.drawable.custom_button); |
134 |
|
|
135 |
|
departureBtn.setVisibility( View.VISIBLE ); |
136 |
|
arrivalBtn.setVisibility( View.VISIBLE ); |
137 |
|
|
138 |
|
getListView().setVisibility( View.VISIBLE ); |
139 |
|
metroView.setVisibility( View.GONE ); |
140 |
|
trainType = "REGIONAL"; |
141 |
|
startDepartureFetcher(); |
142 |
|
} |
143 |
|
}); |
144 |
|
stogBtn.setOnClickListener( new OnClickListener() { |
145 |
|
@Override |
146 |
|
public void onClick(View arg0) { |
147 |
|
regionalBtn.setBackgroundResource(R.drawable.custom_button); |
148 |
|
stogBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
149 |
|
metroBtn.setBackgroundResource(R.drawable.custom_button); |
150 |
|
|
151 |
|
|
152 |
|
departureBtn.setVisibility( View.VISIBLE ); |
153 |
|
arrivalBtn.setVisibility( View.VISIBLE ); |
154 |
|
|
155 |
|
getListView().setVisibility( View.VISIBLE ); |
156 |
|
metroView.setVisibility( View.GONE ); |
157 |
|
trainType = "STOG"; |
158 |
|
startDepartureFetcher(); |
159 |
|
} |
160 |
|
}); |
161 |
|
|
162 |
|
|
163 |
|
|
164 |
metroBtn.setOnClickListener( new OnClickListener() { |
metroBtn.setOnClickListener( new OnClickListener() { |
165 |
@Override |
@Override |
166 |
public void onClick(View v) { |
public void onClick(View v) { |
167 |
arrivalBtn.setBackgroundResource(R.drawable.custom_button); |
regionalBtn.setBackgroundResource(R.drawable.custom_button); |
168 |
departureBtn.setBackgroundResource(R.drawable.custom_button); |
stogBtn.setBackgroundResource(R.drawable.custom_button); |
169 |
metroBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
metroBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
170 |
|
|
171 |
|
departureBtn.setVisibility( View.GONE ); |
172 |
|
arrivalBtn.setVisibility( View.GONE ); |
173 |
|
|
174 |
getListView().setVisibility( View.GONE ); |
getListView().setVisibility( View.GONE ); |
175 |
metroView.setVisibility( View.VISIBLE ); |
metroView.setVisibility( View.VISIBLE ); |
176 |
startMetroFetcher(); |
startMetroFetcher(); |
197 |
|
|
198 |
Log.e("Station", station.toCSV() ); |
Log.e("Station", station.toCSV() ); |
199 |
|
|
200 |
|
|
201 |
|
|
202 |
if (station.isMetro() == false) { |
if (station.isMetro() == false) { |
203 |
metroBtn.setVisibility( View.GONE ); |
metroBtn.setVisibility( View.GONE ); |
204 |
} |
} |
205 |
|
|
206 |
metro = ProviderFactory.getMetroProvider(); |
metro = ProviderFactory.getMetroProvider(); |
207 |
|
|
208 |
|
if (station.isRegional() == false ) { |
209 |
|
regionalBtn.setVisibility(View.GONE); |
210 |
|
} |
211 |
|
|
212 |
|
if (station.isStrain() == false ) { |
213 |
|
stogBtn.setVisibility(View.GONE); |
214 |
|
} |
215 |
|
|
216 |
|
if (station.isRegional() == true && station.isStrain() == false ) { |
217 |
|
if ( station.isMetro() == false ) |
218 |
|
regionalBtn.setVisibility(View.GONE); |
219 |
|
trainType = "REGIONAL"; |
220 |
|
} |
221 |
|
|
222 |
|
if (station.isRegional() == false && station.isStrain() == true) { |
223 |
|
if (station.isMetro() == false) |
224 |
|
stogBtn.setVisibility(View.GONE); |
225 |
|
|
226 |
|
stogBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
227 |
|
trainType = "STOG"; |
228 |
|
|
229 |
|
} |
230 |
|
//Both enabled - use preferred from preferences |
231 |
|
if (station.isRegional() == true && station.isStrain() == true ) { |
232 |
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); |
233 |
|
trainType = prefs.getString("traintype", "REGIONAL"); //default value is gps |
234 |
|
|
235 |
|
if (trainType.equals("STOG") ) { |
236 |
|
stogBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
237 |
|
regionalBtn.setBackgroundResource(R.drawable.custom_button); |
238 |
|
} |
239 |
|
} |
240 |
|
|
241 |
|
|
242 |
if (station.isRegional() == false && station.isStrain() == false) { |
if (station.isRegional() == false && station.isStrain() == false) { |
243 |
getListView().setVisibility( View.GONE ); |
getListView().setVisibility( View.GONE ); |
244 |
metroView.setVisibility( View.VISIBLE ); |
metroView.setVisibility( View.VISIBLE ); |
245 |
|
|
246 |
departureBtn.setVisibility( View.GONE ); |
departureBtn.setVisibility( View.GONE ); |
247 |
arrivalBtn.setVisibility(View.GONE); |
arrivalBtn.setVisibility(View.GONE); |
248 |
metroBtn.setVisibility( View.GONE ); |
metroBtn.setVisibility( View.GONE ); |
249 |
|
|
250 |
|
|
251 |
|
|
252 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
253 |
startMetroFetcher(); |
startMetroFetcher(); |
277 |
} |
} |
278 |
} |
} |
279 |
|
|
280 |
|
|
281 |
|
|
282 |
boolean hasNotifications() { |
boolean hasNotifications() { |
283 |
return (departures != null && departures.notifications.size() > 0); |
return (departures != null && departures.notifications.size() > 0); |
284 |
} |
} |
354 |
|
|
355 |
|
|
356 |
|
|
357 |
|
@Override |
358 |
|
public boolean onPrepareOptionsMenu(Menu menu) { |
359 |
|
super.onPrepareOptionsMenu(menu); |
360 |
|
|
361 |
|
MenuItem item = menu.findItem( MENU_NOTIFICATIONS ); |
362 |
|
boolean notifEnabled = hasNotifications(); |
363 |
|
item.setEnabled(notifEnabled); |
364 |
|
|
365 |
|
return true; |
366 |
|
} |
367 |
|
|
368 |
@Override |
@Override |
369 |
public boolean onCreateOptionsMenu(Menu menu) { |
public boolean onCreateOptionsMenu(Menu menu) { |
370 |
MenuItem item; |
MenuItem item; |
371 |
|
|
372 |
|
|
373 |
|
item = menu.add(0, MENU_TOGGLEDETAILS, 0, getString(R.string.departurelist_toggledetails)); |
374 |
|
item.setIcon(android.R.drawable.ic_menu_view); |
375 |
|
|
376 |
item = menu.add(0, MENU_MAP, 0, getString(R.string.departurelist_showonmap) ); |
item = menu.add(0, MENU_MAP, 0, getString(R.string.departurelist_showonmap) ); |
377 |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
378 |
|
|
385 |
if (station.isMetro()) { |
if (station.isMetro()) { |
386 |
item = menu.add(0, MENU_METROMAP, 0, "Metro" ); //TODO:translate!?! |
item = menu.add(0, MENU_METROMAP, 0, "Metro" ); //TODO:translate!?! |
387 |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
388 |
} |
} |
|
|
|
389 |
|
|
390 |
return true; |
return true; |
391 |
} |
} |
392 |
|
|
393 |
@Override |
@Override |
394 |
public boolean onOptionsItemSelected(MenuItem item) { |
public boolean onOptionsItemSelected(MenuItem item) { |
395 |
boolean res; |
boolean res = true; |
396 |
switch(item.getItemId()) { |
switch(item.getItemId()) { |
397 |
case MENU_MAP: |
case MENU_MAP: |
398 |
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude() + "?z=16"); |
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude() + "?z=16"); |
399 |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
|
400 |
res = true; |
try { |
401 |
|
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
402 |
|
} catch (ActivityNotFoundException anfe) { |
403 |
|
Toast.makeText(this, "Could not launch google maps", Toast.LENGTH_LONG).show(); |
404 |
|
} |
405 |
|
|
406 |
break; |
break; |
407 |
case MENU_NOTIFICATIONS: |
case MENU_NOTIFICATIONS: |
408 |
Intent i = new Intent(this,dk.thoerup.traininfo.NotificationList.class); |
Intent i = new Intent(this,dk.thoerup.traininfo.NotificationList.class); |
409 |
i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications); |
i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications); |
410 |
startActivity(i); |
startActivity(i); |
|
res = true; |
|
411 |
break; |
break; |
412 |
case MENU_METROMAP: |
case MENU_METROMAP: |
413 |
Intent metroMap = new Intent(this,dk.thoerup.traininfo.MetroMap.class); |
Intent metroMap = new Intent(this,dk.thoerup.traininfo.MetroMap.class); |
414 |
startActivity(metroMap); |
startActivity(metroMap); |
|
res = true; |
|
415 |
break; |
break; |
416 |
|
case MENU_TOGGLEDETAILS: |
417 |
|
adapter.toggleShowDetails(); |
418 |
|
break; |
419 |
default: |
default: |
420 |
res = super.onOptionsItemSelected(item); |
res = super.onOptionsItemSelected(item); |
421 |
} |
} |
466 |
|
|
467 |
|
|
468 |
pgDialog.dismiss(); |
pgDialog.dismiss(); |
469 |
|
|
470 |
if (departures != null) { |
if (departures != null && departures.errorCode == null) { |
471 |
commFailCounter = 0; |
commFailCounter = 0; |
472 |
DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html |
DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html |
473 |
adapter.setDepartures(departures.entries); |
adapter.setDepartures(departures.entries); |
474 |
DepartureList.this.getListView().setVisibility(View.VISIBLE); |
DepartureList.this.getListView().setVisibility(View.VISIBLE); |
475 |
|
|
476 |
|
|
477 |
if ( hasNotifications() ) { |
// handle notification icon. |
478 |
findViewById(R.id.notifIcon).setVisibility(View.VISIBLE); |
View notifIcon = findViewById(R.id.notifIcon); |
479 |
|
if ( hasNotifications() ) { |
480 |
|
notifIcon.setVisibility(View.VISIBLE); |
481 |
|
notifIcon.setClickable(true); |
482 |
|
notifIcon.setOnClickListener( new View.OnClickListener() { |
483 |
|
@Override |
484 |
|
public void onClick(View v) { |
485 |
|
Intent i = new Intent(DepartureList.this, dk.thoerup.traininfo.NotificationList.class); |
486 |
|
i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications); |
487 |
|
startActivity(i); |
488 |
|
} |
489 |
|
}); |
490 |
|
} else { |
491 |
|
notifIcon.setVisibility(View.INVISIBLE); |
492 |
} |
} |
493 |
|
|
494 |
if (departures.entries.size() == 0) { |
if (departures.entries.size() == 0) { |
495 |
MessageBox.showMessage(DepartureList.this, "No departures found", true); |
int msgId = (arrival==false) ? R.string.departurelist_nodepartures : R.string.departurelist_noarrivals; |
496 |
|
MessageBox.showMessage(DepartureList.this, getString(msgId), false); |
497 |
} |
} |
498 |
} else { // communication or parse error |
} else { // communication or parse error |
499 |
commFailCounter++; |
commFailCounter++; |
500 |
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
501 |
builder.setMessage("Error finding departures"); |
|
502 |
|
if (departures != null && departures.errorCode != null ) { //got an error xml back |
503 |
|
commFailCounter = 10; |
504 |
|
builder.setMessage( getString(R.string.no_backend) ); |
505 |
|
} else { |
506 |
|
builder.setMessage( getString(R.string.departurelist_fetcherror) ); |
507 |
|
} |
508 |
builder.setCancelable(true); |
builder.setCancelable(true); |
509 |
|
|
510 |
if (commFailCounter < 3) { |
if (commFailCounter < 3) { |
511 |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
512 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
523 |
} |
} |
524 |
}); |
}); |
525 |
|
|
526 |
try { |
try { //TODO: is this still necessary after the 0.9.4.1 fix ? |
527 |
builder.show(); |
builder.show(); |
528 |
} catch (android.view.WindowManager.BadTokenException e) { |
} catch (android.view.WindowManager.BadTokenException e) { |
529 |
Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |
Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |
533 |
|
|
534 |
@Override |
@Override |
535 |
protected Void doInBackground(Integer... params) { |
protected Void doInBackground(Integer... params) { |
536 |
departures = provider.lookupDepartures(params[0], DepartureList.this.arrival, "BOTH"); |
departures = provider.lookupDepartures(params[0], DepartureList.this.arrival, trainType); |
537 |
return null; |
return null; |
538 |
} |
} |
539 |
|
|
629 |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
630 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
631 |
dialog.dismiss(); |
dialog.dismiss(); |
632 |
DepartureList.this.finish(); |
DepartureList.this.finish(); //TODO: should we really close the activity ?? |
633 |
} |
} |
634 |
}); |
}); |
635 |
|
|
636 |
try { |
try { //TODO: is this still necessary after the 0.9.4.1 fix ? |
637 |
builder.show(); |
builder.show(); |
638 |
} catch (android.view.WindowManager.BadTokenException e) { |
} catch (android.view.WindowManager.BadTokenException e) { |
639 |
Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |
Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |