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.ArrayList; |
11 |
import java.util.List; |
import java.util.List; |
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.util.Log; |
23 |
import android.view.View; |
import android.view.View; |
24 |
|
import android.view.View.OnClickListener; |
25 |
|
import android.widget.Button; |
26 |
import android.widget.ListView; |
import android.widget.ListView; |
27 |
import android.widget.TextView; |
import android.widget.TextView; |
28 |
import dk.thoerup.traininfo.provider.DepartureProvider; |
import dk.thoerup.traininfo.provider.DepartureProvider; |
29 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
import dk.thoerup.traininfo.provider.ProviderFactory; |
30 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
|
import static dk.thoerup.traininfo.R.string.*; |
|
31 |
|
|
32 |
public class DepartureList extends ListActivity { |
public class DepartureList extends ListActivity { |
33 |
|
|
47 |
|
|
48 |
StationBean station; |
StationBean station; |
49 |
|
|
50 |
|
boolean arrival = false; |
51 |
|
|
52 |
@SuppressWarnings("unchecked") |
@SuppressWarnings("unchecked") |
53 |
@Override |
@Override |
54 |
protected void onCreate(Bundle savedInstanceState) { |
protected void onCreate(Bundle savedInstanceState) { |
67 |
|
|
68 |
((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() ); |
((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() ); |
69 |
|
|
70 |
|
final Button departureBtn = (Button) findViewById(R.id.departurebtn); |
71 |
|
final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn); |
72 |
|
|
73 |
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
departureBtn.setOnClickListener( new OnClickListener() { |
74 |
|
@Override |
75 |
|
public void onClick(View arg0) { |
76 |
|
arrivalBtn.setBackgroundResource(R.drawable.custom_button); |
77 |
|
departureBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
78 |
|
arrival = false; |
79 |
|
startDepartureFetcher(); |
80 |
|
} |
81 |
|
}); |
82 |
|
arrivalBtn.setOnClickListener( new OnClickListener() { |
83 |
|
@Override |
84 |
|
public void onClick(View arg0) { |
85 |
|
arrivalBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
86 |
|
departureBtn.setBackgroundResource(R.drawable.custom_button); |
87 |
|
arrival = true; |
88 |
|
startDepartureFetcher(); |
89 |
|
} |
90 |
|
}); |
91 |
|
|
|
NumberFormat format = NumberFormat.getNumberInstance(); |
|
|
format.setMaximumFractionDigits(1); |
|
|
format.setMinimumFractionDigits(1); |
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
96 |
|
|
97 |
int distance = station.getDistance(); |
int distance = station.getDistance(); |
98 |
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); |
if (distance != 0) { |
99 |
|
NumberFormat format = NumberFormat.getNumberInstance(); |
100 |
|
format.setMaximumFractionDigits(1); |
101 |
|
format.setMinimumFractionDigits(1); |
102 |
|
|
103 |
|
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); |
104 |
|
} else { |
105 |
|
((TextView) findViewById(R.id.stationDistance)).setVisibility(View.GONE); |
106 |
|
} |
107 |
|
|
108 |
|
|
109 |
if (station.isRegional() == false && station.isSTrain() == false) { |
if (station.isRegional() == false && station.isSTrain() == false) { |
110 |
getListView().setVisibility( View.GONE ); |
getListView().setVisibility( View.GONE ); |
111 |
findViewById(R.id.metroonly).setVisibility( View.VISIBLE ); |
findViewById(R.id.metroonly).setVisibility( View.VISIBLE ); |
112 |
|
departureBtn.setVisibility( View.GONE ); |
113 |
|
arrivalBtn.setVisibility(View.GONE); |
114 |
|
|
115 |
} else { |
} else { |
116 |
provider = ProviderFactory.getDepartureProvider(); |
provider = ProviderFactory.getDepartureProvider(); |
159 |
switch (id) { |
switch (id) { |
160 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
161 |
pgDialog = (ProgressDialog) dialog; |
pgDialog = (ProgressDialog) dialog; |
162 |
|
int messageId = arrival == false ? departurelist_fetchdepartures : departurelist_fetcharrivals; |
163 |
|
pgDialog.setMessage( getString(messageId) ); |
164 |
break; |
break; |
165 |
} |
} |
166 |
} |
} |
169 |
protected Dialog onCreateDialog(int id) { |
protected Dialog onCreateDialog(int id) { |
170 |
switch (id) { |
switch (id) { |
171 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
172 |
|
|
173 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
|
dlg.setMessage( getString(departurelist_fetchdata) ); |
|
174 |
dlg.setCancelable(true); |
dlg.setCancelable(true); |
175 |
return dlg; |
return dlg; |
176 |
default: |
default: |
238 |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
239 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
240 |
dialog.dismiss(); |
dialog.dismiss(); |
241 |
|
DepartureList.this.finish(); |
242 |
} |
} |
243 |
}); |
}); |
244 |
builder.show(); |
|
245 |
|
try { |
246 |
|
builder.show(); |
247 |
|
} catch (android.view.WindowManager.BadTokenException e) { |
248 |
|
Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |
249 |
|
} |
250 |
} |
} |
251 |
} |
} |
252 |
|
|
253 |
@Override |
@Override |
254 |
protected Void doInBackground(Integer... params) { |
protected Void doInBackground(Integer... params) { |
255 |
success = provider.lookupDepartures(params[0]); |
success = provider.lookupDepartures(params[0], DepartureList.this.arrival); |
256 |
departures = provider.getDepartures(params[0]); |
departures = provider.getDepartures(params[0], DepartureList.this.arrival); |
257 |
return null; |
return null; |
258 |
} |
} |
259 |
|
|