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