1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
3 |
|
import static dk.thoerup.traininfo.R.string.departurelist_fetchdata; |
4 |
|
import static dk.thoerup.traininfo.R.string.generic_cancel; |
5 |
|
import static dk.thoerup.traininfo.R.string.generic_retry; |
6 |
|
|
7 |
import java.text.NumberFormat; |
import java.text.NumberFormat; |
8 |
import java.util.ArrayList; |
import java.util.ArrayList; |
9 |
import java.util.List; |
import java.util.List; |
|
import java.util.Locale; |
|
10 |
|
|
11 |
import android.app.AlertDialog; |
import android.app.AlertDialog; |
12 |
import android.app.Dialog; |
import android.app.Dialog; |
14 |
import android.app.ProgressDialog; |
import android.app.ProgressDialog; |
15 |
import android.content.DialogInterface; |
import android.content.DialogInterface; |
16 |
import android.content.Intent; |
import android.content.Intent; |
|
import android.location.Address; |
|
|
import android.location.Geocoder; |
|
17 |
import android.net.Uri; |
import android.net.Uri; |
18 |
import android.os.AsyncTask; |
import android.os.AsyncTask; |
19 |
import android.os.Bundle; |
import android.os.Bundle; |
40 |
ProgressDialog pgDialog; |
ProgressDialog pgDialog; |
41 |
|
|
42 |
DepartureFetcher fetcher; |
DepartureFetcher fetcher; |
43 |
int stationId; |
|
44 |
|
StationBean station; |
|
double latitude,longitude; |
|
45 |
|
|
46 |
@SuppressWarnings("unchecked") |
@SuppressWarnings("unchecked") |
47 |
@Override |
@Override |
53 |
setListAdapter(adapter); |
setListAdapter(adapter); |
54 |
|
|
55 |
Intent launchedBy = getIntent(); |
Intent launchedBy = getIntent(); |
56 |
|
|
57 |
|
station = (StationBean) launchedBy.getSerializableExtra("stationbean"); |
58 |
|
|
59 |
latitude = launchedBy.getDoubleExtra("latitude", 0.0); |
((TextView) findViewById(R.id.stationName)).setText( station.getName() ); |
|
longitude = launchedBy.getDoubleExtra("longitude", 0.0); |
|
|
|
|
|
String name = launchedBy.getStringExtra("name"); |
|
|
((TextView) findViewById(R.id.stationName)).setText( name ); |
|
60 |
|
|
61 |
String address = launchedBy.getStringExtra("address"); |
|
62 |
((TextView) findViewById(R.id.stationAddr)).setText( address ); |
((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() ); |
63 |
|
|
|
stationId = launchedBy.getIntExtra("stationid", -1); |
|
64 |
|
|
65 |
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
66 |
|
|
67 |
NumberFormat format = NumberFormat.getNumberInstance(); |
NumberFormat format = NumberFormat.getNumberInstance(); |
68 |
format.setMaximumFractionDigits(1); |
format.setMaximumFractionDigits(1); |
69 |
format.setMinimumFractionDigits(1); |
format.setMinimumFractionDigits(1); |
70 |
int distance = launchedBy.getIntExtra("distance", 0); |
|
71 |
|
|
72 |
|
int distance = station.getDistance(); |
73 |
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); |
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); |
74 |
|
station.setRegional(true); |
75 |
|
Log.e("ASD", "" + station.isRegional() + "/" + station.isSTrain() + "/" + station.isMetro()); |
76 |
|
|
77 |
|
if (station.isRegional() == false && station.isSTrain() == false) { |
78 |
provider = ProviderFactory.getDepartureProvider(); |
getListView().setVisibility( View.GONE ); |
79 |
|
findViewById(R.id.metroonly).setVisibility( View.VISIBLE ); |
80 |
if (savedInstanceState == null) { |
|
|
startDepartureFetcher(); |
|
81 |
} else { |
} else { |
82 |
departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures"); |
provider = ProviderFactory.getDepartureProvider(); |
83 |
adapter.setDepartures(departures); |
|
84 |
selectedItemId = savedInstanceState.getInt("selectedItemId"); |
if (savedInstanceState == null) { |
85 |
|
startDepartureFetcher(); |
86 |
|
} else { |
87 |
|
departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures"); |
88 |
|
adapter.setDepartures(departures); |
89 |
|
selectedItemId = savedInstanceState.getInt("selectedItemId"); |
90 |
|
} |
91 |
} |
} |
92 |
} |
} |
93 |
|
|
134 |
switch (id) { |
switch (id) { |
135 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
136 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
137 |
dlg.setMessage("Fetch departure data"); |
dlg.setMessage( getString(departurelist_fetchdata) ); |
138 |
dlg.setCancelable(true); |
dlg.setCancelable(true); |
139 |
return dlg; |
return dlg; |
140 |
default: |
default: |
145 |
void startDepartureFetcher() { |
void startDepartureFetcher() { |
146 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
147 |
fetcher = new DepartureFetcher(); |
fetcher = new DepartureFetcher(); |
148 |
fetcher.execute(stationId); |
fetcher.execute(station.getId()); |
149 |
} |
} |
150 |
|
|
151 |
class DialogDismisser implements View.OnClickListener { |
class DialogDismisser implements View.OnClickListener { |
159 |
public void onClick(View v) { |
public void onClick(View v) { |
160 |
if (dlg.isShowing()) |
if (dlg.isShowing()) |
161 |
dlg.dismiss(); |
dlg.dismiss(); |
162 |
} |
} |
163 |
|
} |
|
|
|
|
} |
|
164 |
|
|
165 |
View.OnClickListener mapLauncher = new View.OnClickListener() { |
View.OnClickListener mapLauncher = new View.OnClickListener() { |
166 |
@Override |
@Override |
167 |
public void onClick(View v) { |
public void onClick(View v) { |
168 |
Uri uri = Uri.parse("geo:" + latitude + "," + longitude); |
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude()); |
169 |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
170 |
} |
} |
171 |
}; |
}; |
175 |
class DepartureFetcher extends AsyncTask<Integer, Void, Void> { |
class DepartureFetcher extends AsyncTask<Integer, Void, Void> { |
176 |
|
|
177 |
boolean success; |
boolean success; |
178 |
String addr; |
|
179 |
@Override |
@Override |
180 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
181 |
super.onPostExecute(result); |
super.onPostExecute(result); |
192 |
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
193 |
builder.setMessage("Error finding departures"); |
builder.setMessage("Error finding departures"); |
194 |
builder.setCancelable(true); |
builder.setCancelable(true); |
195 |
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
196 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
197 |
dialog.dismiss(); |
dialog.dismiss(); |
198 |
startDepartureFetcher(); |
startDepartureFetcher(); |
199 |
|
|
200 |
} |
} |
201 |
}); |
}); |
202 |
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
203 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
204 |
dialog.dismiss(); |
dialog.dismiss(); |
205 |
} |
} |