1 |
package dk.thoerup.traininfo;
|
2 |
|
3 |
import java.text.NumberFormat;
|
4 |
import java.util.ArrayList;
|
5 |
import java.util.List;
|
6 |
import java.util.Locale;
|
7 |
|
8 |
import android.app.AlertDialog;
|
9 |
import android.app.Dialog;
|
10 |
import android.app.ListActivity;
|
11 |
import android.app.ProgressDialog;
|
12 |
import android.content.DialogInterface;
|
13 |
import android.content.Intent;
|
14 |
import android.location.Address;
|
15 |
import android.location.Geocoder;
|
16 |
import android.net.Uri;
|
17 |
import android.os.AsyncTask;
|
18 |
import android.os.Bundle;
|
19 |
import android.util.Log;
|
20 |
import android.view.View;
|
21 |
import android.widget.ListView;
|
22 |
import android.widget.TextView;
|
23 |
import dk.thoerup.traininfo.provider.DepartureProvider;
|
24 |
import dk.thoerup.traininfo.provider.ProviderFactory;
|
25 |
import dk.thoerup.traininfo.util.MessageBox;
|
26 |
|
27 |
public class DepartureList extends ListActivity {
|
28 |
|
29 |
public static final int DLG_PROGRESS = 1;
|
30 |
|
31 |
|
32 |
DepartureListAdapter adapter;
|
33 |
DepartureProvider provider;
|
34 |
List<DepartureBean> departures;
|
35 |
|
36 |
int selectedItemId;
|
37 |
//DepartureBean currentDeparture;
|
38 |
|
39 |
ProgressDialog pgDialog;
|
40 |
|
41 |
DepartureFetcher fetcher;
|
42 |
int stationId;
|
43 |
|
44 |
double latitude,longitude;
|
45 |
|
46 |
@SuppressWarnings("unchecked")
|
47 |
@Override
|
48 |
protected void onCreate(Bundle savedInstanceState) {
|
49 |
super.onCreate(savedInstanceState);
|
50 |
setContentView(R.layout.departurelist);
|
51 |
|
52 |
adapter = new DepartureListAdapter(this);
|
53 |
setListAdapter(adapter);
|
54 |
|
55 |
Intent launchedBy = getIntent();
|
56 |
|
57 |
latitude = launchedBy.getDoubleExtra("latitude", 0.0);
|
58 |
longitude = launchedBy.getDoubleExtra("longitude", 0.0);
|
59 |
|
60 |
String name = launchedBy.getStringExtra("name");
|
61 |
((TextView) findViewById(R.id.stationName)).setText( name );
|
62 |
|
63 |
String address = launchedBy.getStringExtra("address");
|
64 |
((TextView) findViewById(R.id.stationAddr)).setText( address );
|
65 |
|
66 |
stationId = launchedBy.getIntExtra("stationid", -1);
|
67 |
|
68 |
findViewById(R.id.header).setOnClickListener( mapLauncher );
|
69 |
|
70 |
NumberFormat format = NumberFormat.getNumberInstance();
|
71 |
format.setMaximumFractionDigits(1);
|
72 |
format.setMinimumFractionDigits(1);
|
73 |
int distance = launchedBy.getIntExtra("distance", 0);
|
74 |
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." );
|
75 |
|
76 |
|
77 |
provider = ProviderFactory.getDepartureProvider();
|
78 |
|
79 |
if (savedInstanceState == null) {
|
80 |
startDepartureFetcher();
|
81 |
} else {
|
82 |
departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures");
|
83 |
adapter.setDepartures(departures);
|
84 |
selectedItemId = savedInstanceState.getInt("selectedItemId");
|
85 |
}
|
86 |
}
|
87 |
|
88 |
@Override
|
89 |
public void onSaveInstanceState(Bundle outState)
|
90 |
{
|
91 |
if (pgDialog != null && pgDialog.isShowing())
|
92 |
dismissDialog(DLG_PROGRESS);
|
93 |
|
94 |
outState.putInt("selectedItemId", selectedItemId);
|
95 |
|
96 |
outState.putSerializable("departures", (ArrayList<DepartureBean>) departures);
|
97 |
}
|
98 |
|
99 |
@Override
|
100 |
protected void onListItemClick(ListView l, View v, int position, long id) {
|
101 |
super.onListItemClick(l, v, position, id);
|
102 |
|
103 |
selectedItemId = position;
|
104 |
|
105 |
DepartureBean dep = departures.get(selectedItemId);
|
106 |
|
107 |
Intent intent = new Intent(this, TimetableList.class);
|
108 |
intent.putExtra("departure", dep);
|
109 |
|
110 |
startActivity(intent);
|
111 |
|
112 |
}
|
113 |
|
114 |
|
115 |
@Override
|
116 |
protected void onPrepareDialog(int id, Dialog dialog) {
|
117 |
super.onPrepareDialog(id, dialog);
|
118 |
|
119 |
switch (id) {
|
120 |
case DLG_PROGRESS:
|
121 |
pgDialog = (ProgressDialog) dialog;
|
122 |
break;
|
123 |
}
|
124 |
}
|
125 |
|
126 |
@Override
|
127 |
protected Dialog onCreateDialog(int id) {
|
128 |
switch (id) {
|
129 |
case DLG_PROGRESS:
|
130 |
ProgressDialog dlg = new ProgressDialog(this);
|
131 |
dlg.setMessage("Fetch departure data");
|
132 |
dlg.setCancelable(true);
|
133 |
return dlg;
|
134 |
default:
|
135 |
return super.onCreateDialog(id);
|
136 |
}
|
137 |
}
|
138 |
|
139 |
void startDepartureFetcher() {
|
140 |
showDialog(DLG_PROGRESS);
|
141 |
fetcher = new DepartureFetcher();
|
142 |
fetcher.execute(stationId);
|
143 |
}
|
144 |
|
145 |
class DialogDismisser implements View.OnClickListener {
|
146 |
|
147 |
Dialog dlg;
|
148 |
public DialogDismisser(Dialog d) {
|
149 |
dlg = d;
|
150 |
}
|
151 |
|
152 |
@Override
|
153 |
public void onClick(View v) {
|
154 |
if (dlg.isShowing())
|
155 |
dlg.dismiss();
|
156 |
}
|
157 |
|
158 |
|
159 |
}
|
160 |
|
161 |
View.OnClickListener mapLauncher = new View.OnClickListener() {
|
162 |
@Override
|
163 |
public void onClick(View v) {
|
164 |
Uri uri = Uri.parse("geo:" + latitude + "," + longitude);
|
165 |
startActivity( new Intent(Intent.ACTION_VIEW, uri));
|
166 |
}
|
167 |
};
|
168 |
|
169 |
|
170 |
|
171 |
class DepartureFetcher extends AsyncTask<Integer, Void, Void> {
|
172 |
|
173 |
boolean success;
|
174 |
String addr;
|
175 |
@Override
|
176 |
protected void onPostExecute(Void result) {
|
177 |
super.onPostExecute(result);
|
178 |
|
179 |
|
180 |
pgDialog.dismiss();
|
181 |
|
182 |
if (success) {
|
183 |
adapter.setDepartures(departures);
|
184 |
if (departures.size() == 0) {
|
185 |
MessageBox.showMessage(DepartureList.this, "No departures found");
|
186 |
}
|
187 |
} else { // communication or parse error
|
188 |
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this);
|
189 |
builder.setMessage("Error finding departures");
|
190 |
builder.setCancelable(true);
|
191 |
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
|
192 |
public void onClick(DialogInterface dialog, int id) {
|
193 |
dialog.dismiss();
|
194 |
startDepartureFetcher();
|
195 |
|
196 |
}
|
197 |
});
|
198 |
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
|
199 |
public void onClick(DialogInterface dialog, int id) {
|
200 |
dialog.dismiss();
|
201 |
}
|
202 |
});
|
203 |
builder.show();
|
204 |
}
|
205 |
}
|
206 |
|
207 |
@Override
|
208 |
protected Void doInBackground(Integer... params) {
|
209 |
success = provider.lookupDepartures(params[0]);
|
210 |
departures = provider.getDepartures(params[0]);
|
211 |
return null;
|
212 |
}
|
213 |
|
214 |
}
|
215 |
}
|