1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
3 |
import java.text.NumberFormat; |
import java.text.NumberFormat; |
4 |
|
import java.util.ArrayList; |
5 |
import java.util.List; |
import java.util.List; |
6 |
|
import java.util.Locale; |
7 |
|
|
8 |
import android.app.Dialog; |
import android.app.Dialog; |
9 |
import android.app.ListActivity; |
import android.app.ListActivity; |
10 |
import android.app.ProgressDialog; |
import android.app.ProgressDialog; |
11 |
import android.content.Intent; |
import android.content.Intent; |
12 |
|
import android.location.Address; |
13 |
|
import android.location.Geocoder; |
14 |
|
import android.net.Uri; |
15 |
import android.os.AsyncTask; |
import android.os.AsyncTask; |
16 |
import android.os.Bundle; |
import android.os.Bundle; |
17 |
|
import android.util.Log; |
18 |
import android.view.View; |
import android.view.View; |
19 |
import android.widget.ListView; |
import android.widget.ListView; |
20 |
import android.widget.TextView; |
import android.widget.TextView; |
|
import android.widget.Toast; |
|
|
import dk.thoerup.traininfo.provider.DepartureFactory; |
|
21 |
import dk.thoerup.traininfo.provider.DepartureProvider; |
import dk.thoerup.traininfo.provider.DepartureProvider; |
22 |
|
import dk.thoerup.traininfo.provider.ProviderFactory; |
23 |
|
import dk.thoerup.traininfo.util.MessageBox; |
24 |
|
|
25 |
public class DepartureList extends ListActivity { |
public class DepartureList extends ListActivity { |
26 |
|
|
31 |
DepartureProvider provider; |
DepartureProvider provider; |
32 |
List<DepartureBean> departures; |
List<DepartureBean> departures; |
33 |
|
|
34 |
static int itemId; |
int selectedItemId; |
35 |
static DepartureBean currentDeparture; |
//DepartureBean currentDeparture; |
36 |
|
|
37 |
ProgressDialog pgDialog; |
ProgressDialog pgDialog; |
38 |
|
Dialog detailsDialog; |
39 |
DepartureFetcher fetcher; |
DepartureFetcher fetcher; |
40 |
|
|
41 |
|
double latitude,longitude; |
42 |
|
|
43 |
|
@SuppressWarnings("unchecked") |
44 |
@Override |
@Override |
45 |
protected void onCreate(Bundle savedInstanceState) { |
protected void onCreate(Bundle savedInstanceState) { |
46 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
50 |
setListAdapter(adapter); |
setListAdapter(adapter); |
51 |
|
|
52 |
Intent launchedBy = getIntent(); |
Intent launchedBy = getIntent(); |
53 |
|
|
54 |
|
latitude = launchedBy.getDoubleExtra("latitude", 0.0); |
55 |
|
longitude = launchedBy.getDoubleExtra("longitude", 0.0); |
56 |
|
|
57 |
String name = launchedBy.getStringExtra("name"); |
String name = launchedBy.getStringExtra("name"); |
58 |
((TextView) findViewById(R.id.stationName)).setText( name ); |
((TextView) findViewById(R.id.stationName)).setText( name ); |
59 |
|
|
60 |
String addr = launchedBy.getStringExtra("address"); |
((TextView) findViewById(R.id.stationAddr)).setText( "searching..." ); |
|
((TextView) findViewById(R.id.stationAddr)).setText( addr ); |
|
61 |
|
|
62 |
|
int stationId = launchedBy.getIntExtra("stationid", -1); |
63 |
|
|
64 |
|
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
65 |
|
|
66 |
NumberFormat format = NumberFormat.getNumberInstance(); |
NumberFormat format = NumberFormat.getNumberInstance(); |
67 |
format.setMaximumFractionDigits(1); |
format.setMaximumFractionDigits(1); |
70 |
((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." ); |
71 |
|
|
72 |
|
|
73 |
showDialog(DLG_PROGRESS); |
provider = ProviderFactory.getDepartureProvider(); |
|
provider = DepartureFactory.getProvider(); |
|
74 |
|
|
75 |
fetcher = new DepartureFetcher(); |
fetcher = new DepartureFetcher(); |
76 |
fetcher.execute(name); |
if (savedInstanceState == null) { |
77 |
|
showDialog(DLG_PROGRESS); |
78 |
|
fetcher.execute(stationId); |
79 |
|
} else { |
80 |
|
departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures"); |
81 |
|
adapter.setDepartures(departures); |
82 |
|
selectedItemId = savedInstanceState.getInt("selectedItemId"); |
83 |
|
boolean detailsShowing = savedInstanceState.getBoolean("detailsShowing"); |
84 |
|
if (detailsShowing) |
85 |
|
showDialog(DLG_DETAILS); |
86 |
|
} |
87 |
} |
} |
88 |
|
|
89 |
|
@Override |
90 |
|
public void onSaveInstanceState(Bundle outState) |
91 |
|
{ |
92 |
|
if (pgDialog != null && pgDialog.isShowing()) |
93 |
|
dismissDialog(DLG_PROGRESS); |
94 |
|
boolean detailsShowing = (detailsDialog != null && detailsDialog.isShowing()); |
95 |
|
if (detailsShowing) { |
96 |
|
dismissDialog(DLG_DETAILS); |
97 |
|
} |
98 |
|
outState.putBoolean("detailsShowing", detailsShowing); |
99 |
|
outState.putInt("selectedItemId", selectedItemId); |
100 |
|
|
101 |
|
outState.putSerializable("departures", (ArrayList<DepartureBean>) departures); |
102 |
|
} |
103 |
|
|
104 |
@Override |
@Override |
105 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
106 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
107 |
|
|
108 |
currentDeparture = departures.get(position); |
selectedItemId = position; |
109 |
showDialog(DLG_DETAILS); |
showDialog(DLG_DETAILS); |
110 |
} |
} |
111 |
|
|
113 |
@Override |
@Override |
114 |
protected void onPrepareDialog(int id, Dialog dialog) { |
protected void onPrepareDialog(int id, Dialog dialog) { |
115 |
super.onPrepareDialog(id, dialog); |
super.onPrepareDialog(id, dialog); |
116 |
|
|
117 |
switch (id) { |
switch (id) { |
118 |
case DLG_DETAILS: |
case DLG_DETAILS: |
119 |
|
DepartureBean currentDeparture = departures.get(selectedItemId); |
120 |
((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime()); |
((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime()); |
121 |
|
((TextView)dialog.findViewById(R.id.Train)).setText(currentDeparture.getTrainNumber()); |
122 |
((TextView)dialog.findViewById(R.id.Destination)).setText( currentDeparture.getDestination()); |
((TextView)dialog.findViewById(R.id.Destination)).setText( currentDeparture.getDestination()); |
123 |
((TextView)dialog.findViewById(R.id.Origin)).setText(currentDeparture.getOrigin()); |
((TextView)dialog.findViewById(R.id.Origin)).setText(currentDeparture.getOrigin()); |
124 |
((TextView)dialog.findViewById(R.id.Location)).setText(currentDeparture.getLocation()); |
((TextView)dialog.findViewById(R.id.Location)).setText(currentDeparture.getLocation()); |
125 |
((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString()); |
((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString()); |
126 |
((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus()); |
((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus()); |
127 |
((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote()); |
((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote()); |
128 |
|
detailsDialog = dialog; |
129 |
break; |
break; |
130 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
131 |
pgDialog = (ProgressDialog) dialog; |
pgDialog = (ProgressDialog) dialog; |
157 |
} |
} |
158 |
} |
} |
159 |
|
|
160 |
|
String lookupAddress(double latitude, double longitude) { |
161 |
|
|
162 |
|
Geocoder coder = new Geocoder(this, new Locale("da")); |
163 |
|
StringBuilder sb = new StringBuilder(); |
164 |
|
Log.i("lookupaddr", "" + latitude + "/" + longitude); |
165 |
|
try { |
166 |
|
List<Address> addressList = coder.getFromLocation(latitude, longitude, 1); |
167 |
|
Address addr = addressList.get(0); |
168 |
|
|
169 |
|
|
170 |
|
int max = addr.getMaxAddressLineIndex(); |
171 |
|
for (int i=0; i<max; i++) { |
172 |
|
if (i>0) |
173 |
|
sb.append(", "); |
174 |
|
|
175 |
|
sb.append(addr.getAddressLine(i)); |
176 |
|
} |
177 |
|
|
178 |
|
|
179 |
|
} catch (Exception e) { |
180 |
|
Log.e("DepartureList", "geocoder failed", e); |
181 |
|
} |
182 |
|
|
183 |
|
return sb.toString(); |
184 |
|
} |
185 |
|
|
186 |
class DialogDismisser implements View.OnClickListener { |
class DialogDismisser implements View.OnClickListener { |
187 |
|
|
188 |
Dialog dlg; |
Dialog dlg; |
197 |
} |
} |
198 |
} |
} |
199 |
|
|
200 |
|
View.OnClickListener mapLauncher = new View.OnClickListener() { |
201 |
|
@Override |
202 |
|
public void onClick(View v) { |
203 |
|
Uri uri = Uri.parse("geo:" + latitude + "," + longitude); |
204 |
|
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
205 |
|
} |
206 |
|
}; |
207 |
|
|
208 |
class DepartureFetcher extends AsyncTask<String, Void, Void> { |
|
209 |
|
class DepartureFetcher extends AsyncTask<Integer, Void, Void> { |
210 |
|
|
211 |
|
String addr; |
212 |
@Override |
@Override |
213 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
214 |
super.onPostExecute(result); |
super.onPostExecute(result); |
216 |
adapter.setDepartures(departures); |
adapter.setDepartures(departures); |
217 |
pgDialog.dismiss(); |
pgDialog.dismiss(); |
218 |
|
|
219 |
|
|
220 |
|
((TextView) findViewById(R.id.stationAddr)).setText( addr ); |
221 |
|
|
222 |
if (departures.size() == 0) |
if (departures.size() == 0) |
223 |
Toast.makeText(DepartureList.this, "No departures found", Toast.LENGTH_LONG); |
MessageBox.showMessage(DepartureList.this, "No departures found"); |
224 |
} |
} |
225 |
|
|
226 |
@Override |
@Override |
227 |
protected Void doInBackground(String... params) { |
protected Void doInBackground(Integer... params) { |
228 |
|
addr = lookupAddress( latitude , longitude); |
229 |
provider.lookupDepartures(params[0]); |
provider.lookupDepartures(params[0]); |
230 |
departures = provider.getDepartures(); |
departures = provider.getDepartures(); |
231 |
return null; |
return null; |