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; |
44 |
ProgressDialog pgDialog; |
ProgressDialog pgDialog; |
45 |
|
|
46 |
DepartureFetcher fetcher; |
DepartureFetcher fetcher; |
47 |
int stationId; |
|
48 |
|
StationBean station; |
49 |
|
|
50 |
double latitude,longitude; |
boolean arrival = false; |
51 |
|
|
52 |
@SuppressWarnings("unchecked") |
@SuppressWarnings("unchecked") |
53 |
@Override |
@Override |
59 |
setListAdapter(adapter); |
setListAdapter(adapter); |
60 |
|
|
61 |
Intent launchedBy = getIntent(); |
Intent launchedBy = getIntent(); |
62 |
|
|
63 |
|
station = (StationBean) launchedBy.getSerializableExtra("stationbean"); |
64 |
|
|
65 |
latitude = launchedBy.getDoubleExtra("latitude", 0.0); |
((TextView) findViewById(R.id.stationName)).setText( station.getName() ); |
66 |
longitude = launchedBy.getDoubleExtra("longitude", 0.0); |
|
|
|
|
|
String name = launchedBy.getStringExtra("name"); |
|
|
((TextView) findViewById(R.id.stationName)).setText( name ); |
|
67 |
|
|
68 |
String address = launchedBy.getStringExtra("address"); |
((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() ); |
69 |
((TextView) findViewById(R.id.stationAddr)).setText( address ); |
|
70 |
|
final Button departureBtn = (Button) findViewById(R.id.departurebtn); |
71 |
|
final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn); |
72 |
|
|
73 |
|
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 |
|
|
92 |
|
|
93 |
|
|
|
stationId = launchedBy.getIntExtra("stationid", -1); |
|
94 |
|
|
95 |
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
96 |
|
|
97 |
NumberFormat format = NumberFormat.getNumberInstance(); |
int distance = station.getDistance(); |
98 |
format.setMaximumFractionDigits(1); |
if (distance != 0) { |
99 |
format.setMinimumFractionDigits(1); |
NumberFormat format = NumberFormat.getNumberInstance(); |
100 |
int distance = launchedBy.getIntExtra("distance", 0); |
format.setMaximumFractionDigits(1); |
101 |
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); |
format.setMinimumFractionDigits(1); |
102 |
|
|
103 |
boolean isRegional = launchedBy.getBooleanExtra("isregional", false); |
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); |
104 |
boolean isSTrain = launchedBy.getBooleanExtra("isstrain", false); |
} else { |
105 |
//boolean isMetro = launchedBy.getBooleanExtra("ismetro", false); // not currently used |
((TextView) findViewById(R.id.stationDistance)).setVisibility(View.GONE); |
106 |
|
} |
107 |
|
|
108 |
|
|
109 |
if (isRegional == false && 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("Fetch departure data"); |
|
174 |
dlg.setCancelable(true); |
dlg.setCancelable(true); |
175 |
return dlg; |
return dlg; |
176 |
default: |
default: |
181 |
void startDepartureFetcher() { |
void startDepartureFetcher() { |
182 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
183 |
fetcher = new DepartureFetcher(); |
fetcher = new DepartureFetcher(); |
184 |
fetcher.execute(stationId); |
fetcher.execute(station.getId()); |
185 |
} |
} |
186 |
|
|
187 |
class DialogDismisser implements View.OnClickListener { |
class DialogDismisser implements View.OnClickListener { |
200 |
|
|
201 |
View.OnClickListener mapLauncher = new View.OnClickListener() { |
View.OnClickListener mapLauncher = new View.OnClickListener() { |
202 |
@Override |
@Override |
203 |
public void onClick(View v) { |
public void onClick(View v) { |
204 |
Uri uri = Uri.parse("geo:" + latitude + "," + longitude); |
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude()); |
205 |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
206 |
} |
} |
207 |
}; |
}; |
220 |
pgDialog.dismiss(); |
pgDialog.dismiss(); |
221 |
|
|
222 |
if (success) { |
if (success) { |
223 |
|
DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html |
224 |
adapter.setDepartures(departures); |
adapter.setDepartures(departures); |
225 |
|
DepartureList.this.getListView().setVisibility(View.VISIBLE); |
226 |
|
|
227 |
if (departures.size() == 0) { |
if (departures.size() == 0) { |
228 |
MessageBox.showMessage(DepartureList.this, "No departures found"); |
MessageBox.showMessage(DepartureList.this, "No departures found", true); |
229 |
} |
} |
230 |
} else { // communication or parse error |
} else { // communication or parse error |
231 |
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
232 |
builder.setMessage("Error finding departures"); |
builder.setMessage("Error finding departures"); |
233 |
builder.setCancelable(true); |
builder.setCancelable(true); |
234 |
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
235 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
236 |
dialog.dismiss(); |
dialog.dismiss(); |
237 |
startDepartureFetcher(); |
startDepartureFetcher(); |
238 |
|
|
239 |
} |
} |
240 |
}); |
}); |
241 |
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
242 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
243 |
dialog.dismiss(); |
dialog.dismiss(); |
244 |
|
DepartureList.this.finish(); |
245 |
} |
} |
246 |
}); |
}); |
247 |
builder.show(); |
|
248 |
|
try { |
249 |
|
builder.show(); |
250 |
|
} catch (android.view.WindowManager.BadTokenException e) { |
251 |
|
Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |
252 |
|
} |
253 |
} |
} |
254 |
} |
} |
255 |
|
|
256 |
@Override |
@Override |
257 |
protected Void doInBackground(Integer... params) { |
protected Void doInBackground(Integer... params) { |
258 |
success = provider.lookupDepartures(params[0]); |
success = provider.lookupDepartures(params[0], DepartureList.this.arrival); |
259 |
departures = provider.getDepartures(params[0]); |
departures = provider.getDepartures(params[0], DepartureList.this.arrival); |
260 |
return null; |
return null; |
261 |
} |
} |
262 |
|
|