13 |
import android.net.Uri; |
import android.net.Uri; |
14 |
import android.os.AsyncTask; |
import android.os.AsyncTask; |
15 |
import android.os.Bundle; |
import android.os.Bundle; |
16 |
|
import android.util.Log; |
17 |
import android.view.View; |
import android.view.View; |
18 |
import android.widget.ListView; |
import android.widget.ListView; |
19 |
import android.widget.TextView; |
import android.widget.TextView; |
20 |
import dk.thoerup.traininfo.provider.DepartureProvider; |
import dk.thoerup.traininfo.provider.DepartureProvider; |
21 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
import dk.thoerup.traininfo.provider.ProviderFactory; |
22 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
23 |
|
import static dk.thoerup.traininfo.R.string.*; |
24 |
|
|
25 |
public class DepartureList extends ListActivity { |
public class DepartureList extends ListActivity { |
26 |
|
|
37 |
ProgressDialog pgDialog; |
ProgressDialog pgDialog; |
38 |
|
|
39 |
DepartureFetcher fetcher; |
DepartureFetcher fetcher; |
40 |
int stationId; |
|
41 |
|
StationBean station; |
|
double latitude,longitude; |
|
42 |
|
|
43 |
@SuppressWarnings("unchecked") |
@SuppressWarnings("unchecked") |
44 |
@Override |
@Override |
50 |
setListAdapter(adapter); |
setListAdapter(adapter); |
51 |
|
|
52 |
Intent launchedBy = getIntent(); |
Intent launchedBy = getIntent(); |
53 |
|
|
54 |
|
station = (StationBean) launchedBy.getSerializableExtra("stationbean"); |
55 |
|
|
56 |
latitude = launchedBy.getDoubleExtra("latitude", 0.0); |
((TextView) findViewById(R.id.stationName)).setText( station.getName() ); |
57 |
longitude = launchedBy.getDoubleExtra("longitude", 0.0); |
|
|
|
|
|
String name = launchedBy.getStringExtra("name"); |
|
|
((TextView) findViewById(R.id.stationName)).setText( name ); |
|
58 |
|
|
59 |
String address = launchedBy.getStringExtra("address"); |
((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() ); |
|
((TextView) findViewById(R.id.stationAddr)).setText( address ); |
|
60 |
|
|
|
stationId = launchedBy.getIntExtra("stationid", -1); |
|
61 |
|
|
62 |
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
63 |
|
|
64 |
NumberFormat format = NumberFormat.getNumberInstance(); |
int distance = station.getDistance(); |
65 |
format.setMaximumFractionDigits(1); |
if (distance != 0) { |
66 |
format.setMinimumFractionDigits(1); |
NumberFormat format = NumberFormat.getNumberInstance(); |
67 |
int distance = launchedBy.getIntExtra("distance", 0); |
format.setMaximumFractionDigits(1); |
68 |
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); |
format.setMinimumFractionDigits(1); |
69 |
|
|
70 |
boolean isRegional = launchedBy.getBooleanExtra("isregional", false); |
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); |
71 |
boolean isSTrain = launchedBy.getBooleanExtra("isstrain", false); |
} else { |
72 |
//boolean isMetro = launchedBy.getBooleanExtra("ismetro", false); // not currently used |
((TextView) findViewById(R.id.stationDistance)).setVisibility(View.GONE); |
73 |
|
} |
74 |
|
|
75 |
|
|
76 |
if (isRegional == false && isSTrain == false) { |
if (station.isRegional() == false && station.isSTrain() == false) { |
77 |
getListView().setVisibility( View.GONE ); |
getListView().setVisibility( View.GONE ); |
78 |
findViewById(R.id.metroonly).setVisibility( View.VISIBLE ); |
findViewById(R.id.metroonly).setVisibility( View.VISIBLE ); |
79 |
|
|
133 |
switch (id) { |
switch (id) { |
134 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
135 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
136 |
dlg.setMessage("Fetch departure data"); |
dlg.setMessage( getString(departurelist_fetchdata) ); |
137 |
dlg.setCancelable(true); |
dlg.setCancelable(true); |
138 |
return dlg; |
return dlg; |
139 |
default: |
default: |
144 |
void startDepartureFetcher() { |
void startDepartureFetcher() { |
145 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
146 |
fetcher = new DepartureFetcher(); |
fetcher = new DepartureFetcher(); |
147 |
fetcher.execute(stationId); |
fetcher.execute(station.getId()); |
148 |
} |
} |
149 |
|
|
150 |
class DialogDismisser implements View.OnClickListener { |
class DialogDismisser implements View.OnClickListener { |
163 |
|
|
164 |
View.OnClickListener mapLauncher = new View.OnClickListener() { |
View.OnClickListener mapLauncher = new View.OnClickListener() { |
165 |
@Override |
@Override |
166 |
public void onClick(View v) { |
public void onClick(View v) { |
167 |
Uri uri = Uri.parse("geo:" + latitude + "," + longitude); |
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude()); |
168 |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
169 |
} |
} |
170 |
}; |
}; |
191 |
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
192 |
builder.setMessage("Error finding departures"); |
builder.setMessage("Error finding departures"); |
193 |
builder.setCancelable(true); |
builder.setCancelable(true); |
194 |
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
195 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
196 |
dialog.dismiss(); |
dialog.dismiss(); |
197 |
startDepartureFetcher(); |
startDepartureFetcher(); |
198 |
|
|
199 |
} |
} |
200 |
}); |
}); |
201 |
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
202 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
203 |
dialog.dismiss(); |
dialog.dismiss(); |
204 |
} |
} |
205 |
}); |
}); |
206 |
builder.show(); |
|
207 |
|
try { |
208 |
|
builder.show(); |
209 |
|
} catch (android.view.WindowManager.BadTokenException e) { |
210 |
|
Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |
211 |
|
} |
212 |
} |
} |
213 |
} |
} |
214 |
|
|