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