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.Dialog;
|
9 |
import android.app.ListActivity;
|
10 |
import android.app.ProgressDialog;
|
11 |
import android.content.Intent;
|
12 |
import android.location.Address;
|
13 |
import android.location.Geocoder;
|
14 |
import android.net.Uri;
|
15 |
import android.os.AsyncTask;
|
16 |
import android.os.Bundle;
|
17 |
import android.util.Log;
|
18 |
import android.view.View;
|
19 |
import android.widget.ListView;
|
20 |
import android.widget.TextView;
|
21 |
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 {
|
26 |
|
27 |
public static final int DLG_PROGRESS = 1;
|
28 |
public static final int DLG_DETAILS = 2;
|
29 |
|
30 |
DepartureListAdapter adapter;
|
31 |
DepartureProvider provider;
|
32 |
List<DepartureBean> departures;
|
33 |
|
34 |
int selectedItemId;
|
35 |
//DepartureBean currentDeparture;
|
36 |
|
37 |
ProgressDialog pgDialog;
|
38 |
Dialog detailsDialog;
|
39 |
DepartureFetcher fetcher;
|
40 |
|
41 |
double latitude,longitude;
|
42 |
|
43 |
@SuppressWarnings("unchecked")
|
44 |
@Override
|
45 |
protected void onCreate(Bundle savedInstanceState) {
|
46 |
super.onCreate(savedInstanceState);
|
47 |
setContentView(R.layout.departurelist);
|
48 |
|
49 |
adapter = new DepartureListAdapter(this);
|
50 |
setListAdapter(adapter);
|
51 |
|
52 |
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");
|
58 |
((TextView) findViewById(R.id.stationName)).setText( name );
|
59 |
|
60 |
String address = launchedBy.getStringExtra("address");
|
61 |
((TextView) findViewById(R.id.stationAddr)).setText( address );
|
62 |
|
63 |
int stationId = launchedBy.getIntExtra("stationid", -1);
|
64 |
|
65 |
findViewById(R.id.header).setOnClickListener( mapLauncher );
|
66 |
|
67 |
NumberFormat format = NumberFormat.getNumberInstance();
|
68 |
format.setMaximumFractionDigits(1);
|
69 |
format.setMinimumFractionDigits(1);
|
70 |
int distance = launchedBy.getIntExtra("distance", 0);
|
71 |
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." );
|
72 |
|
73 |
|
74 |
provider = ProviderFactory.getDepartureProvider();
|
75 |
|
76 |
fetcher = new DepartureFetcher();
|
77 |
if (savedInstanceState == null) {
|
78 |
showDialog(DLG_PROGRESS);
|
79 |
fetcher.execute(stationId);
|
80 |
} else {
|
81 |
departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures");
|
82 |
adapter.setDepartures(departures);
|
83 |
selectedItemId = savedInstanceState.getInt("selectedItemId");
|
84 |
boolean detailsShowing = savedInstanceState.getBoolean("detailsShowing");
|
85 |
if (detailsShowing)
|
86 |
showDialog(DLG_DETAILS);
|
87 |
}
|
88 |
}
|
89 |
|
90 |
@Override
|
91 |
public void onSaveInstanceState(Bundle outState)
|
92 |
{
|
93 |
if (pgDialog != null && pgDialog.isShowing())
|
94 |
dismissDialog(DLG_PROGRESS);
|
95 |
boolean detailsShowing = (detailsDialog != null && detailsDialog.isShowing());
|
96 |
if (detailsShowing) {
|
97 |
dismissDialog(DLG_DETAILS);
|
98 |
}
|
99 |
outState.putBoolean("detailsShowing", detailsShowing);
|
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 |
showDialog(DLG_DETAILS);
|
111 |
}
|
112 |
|
113 |
|
114 |
@Override
|
115 |
protected void onPrepareDialog(int id, Dialog dialog) {
|
116 |
super.onPrepareDialog(id, dialog);
|
117 |
|
118 |
switch (id) {
|
119 |
case DLG_DETAILS:
|
120 |
DepartureBean currentDeparture = departures.get(selectedItemId);
|
121 |
((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime());
|
122 |
((TextView)dialog.findViewById(R.id.Train)).setText(currentDeparture.getTrainNumber());
|
123 |
((TextView)dialog.findViewById(R.id.Destination)).setText( currentDeparture.getDestination());
|
124 |
((TextView)dialog.findViewById(R.id.Origin)).setText(currentDeparture.getOrigin());
|
125 |
((TextView)dialog.findViewById(R.id.Location)).setText(currentDeparture.getLocation());
|
126 |
((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString());
|
127 |
((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus());
|
128 |
((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote());
|
129 |
detailsDialog = dialog;
|
130 |
break;
|
131 |
case DLG_PROGRESS:
|
132 |
pgDialog = (ProgressDialog) dialog;
|
133 |
break;
|
134 |
}
|
135 |
}
|
136 |
|
137 |
@Override
|
138 |
protected Dialog onCreateDialog(int id) {
|
139 |
switch (id) {
|
140 |
case DLG_PROGRESS:
|
141 |
ProgressDialog dlg = new ProgressDialog(this);
|
142 |
dlg.setMessage("Fetch departure data");
|
143 |
dlg.setCancelable(true);
|
144 |
return dlg;
|
145 |
case DLG_DETAILS:
|
146 |
//Context mContext = getApplicationContext();
|
147 |
Dialog dialog = new Dialog(this);
|
148 |
dialog.setCancelable(true);
|
149 |
|
150 |
dialog.setContentView(R.layout.departuredetails);
|
151 |
dialog.setTitle("Departure details");
|
152 |
|
153 |
View root = dialog.findViewById(R.id.layout_root);
|
154 |
root.setOnClickListener( new DialogDismisser(dialog) );
|
155 |
return dialog;
|
156 |
default:
|
157 |
return super.onCreateDialog(id);
|
158 |
}
|
159 |
}
|
160 |
|
161 |
class DialogDismisser implements View.OnClickListener {
|
162 |
|
163 |
Dialog dlg;
|
164 |
public DialogDismisser(Dialog d) {
|
165 |
dlg = d;
|
166 |
}
|
167 |
|
168 |
@Override
|
169 |
public void onClick(View v) {
|
170 |
if (dlg.isShowing())
|
171 |
dlg.dismiss();
|
172 |
}
|
173 |
}
|
174 |
|
175 |
View.OnClickListener mapLauncher = new View.OnClickListener() {
|
176 |
@Override
|
177 |
public void onClick(View v) {
|
178 |
Uri uri = Uri.parse("geo:" + latitude + "," + longitude);
|
179 |
startActivity( new Intent(Intent.ACTION_VIEW, uri));
|
180 |
}
|
181 |
};
|
182 |
|
183 |
|
184 |
class DepartureFetcher extends AsyncTask<Integer, Void, Void> {
|
185 |
|
186 |
String addr;
|
187 |
@Override
|
188 |
protected void onPostExecute(Void result) {
|
189 |
super.onPostExecute(result);
|
190 |
|
191 |
adapter.setDepartures(departures);
|
192 |
pgDialog.dismiss();
|
193 |
|
194 |
if (departures.size() == 0)
|
195 |
MessageBox.showMessage(DepartureList.this, "No departures found");
|
196 |
}
|
197 |
|
198 |
@Override
|
199 |
protected Void doInBackground(Integer... params) {
|
200 |
provider.lookupDepartures(params[0]);
|
201 |
departures = provider.getDepartures();
|
202 |
return null;
|
203 |
}
|
204 |
|
205 |
}
|
206 |
}
|