/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java
ViewVC logotype

Contents of /android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 316 - (show annotations) (download)
Fri Sep 11 08:48:18 2009 UTC (14 years, 8 months ago) by torben
File size: 7041 byte(s)
Lookup of the stations address is now done in background
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 ((TextView) findViewById(R.id.stationAddr)).setText( "searching..." );
61
62 int 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
73 provider = ProviderFactory.getDepartureProvider();
74
75 fetcher = new DepartureFetcher();
76 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
105 protected void onListItemClick(ListView l, View v, int position, long id) {
106 super.onListItemClick(l, v, position, id);
107
108 selectedItemId = position;
109 showDialog(DLG_DETAILS);
110 }
111
112
113 @Override
114 protected void onPrepareDialog(int id, Dialog dialog) {
115 super.onPrepareDialog(id, dialog);
116
117 switch (id) {
118 case DLG_DETAILS:
119 DepartureBean currentDeparture = departures.get(selectedItemId);
120 ((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());
123 ((TextView)dialog.findViewById(R.id.Origin)).setText(currentDeparture.getOrigin());
124 ((TextView)dialog.findViewById(R.id.Location)).setText(currentDeparture.getLocation());
125 ((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString());
126 ((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus());
127 ((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote());
128 detailsDialog = dialog;
129 break;
130 case DLG_PROGRESS:
131 pgDialog = (ProgressDialog) dialog;
132 break;
133 }
134 }
135
136 @Override
137 protected Dialog onCreateDialog(int id) {
138 switch (id) {
139 case DLG_PROGRESS:
140 ProgressDialog dlg = new ProgressDialog(this);
141 dlg.setMessage("Fetch departure data");
142 dlg.setCancelable(true);
143 return dlg;
144 case DLG_DETAILS:
145 //Context mContext = getApplicationContext();
146 Dialog dialog = new Dialog(this);
147 dialog.setCancelable(true);
148
149 dialog.setContentView(R.layout.departuredetails);
150 dialog.setTitle("Departure details");
151
152 View root = dialog.findViewById(R.id.layout_root);
153 root.setOnClickListener( new DialogDismisser(dialog) );
154 return dialog;
155 default:
156 return super.onCreateDialog(id);
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 {
187
188 Dialog dlg;
189 public DialogDismisser(Dialog d) {
190 dlg = d;
191 }
192
193 @Override
194 public void onClick(View v) {
195 if (dlg.isShowing())
196 dlg.dismiss();
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
209 class DepartureFetcher extends AsyncTask<Integer, Void, Void> {
210
211 String addr;
212 @Override
213 protected void onPostExecute(Void result) {
214 super.onPostExecute(result);
215
216 adapter.setDepartures(departures);
217 pgDialog.dismiss();
218
219
220 ((TextView) findViewById(R.id.stationAddr)).setText( addr );
221
222 if (departures.size() == 0)
223 MessageBox.showMessage(DepartureList.this, "No departures found");
224 }
225
226 @Override
227 protected Void doInBackground(Integer... params) {
228 addr = lookupAddress( latitude , longitude);
229 provider.lookupDepartures(params[0]);
230 departures = provider.getDepartures();
231 return null;
232 }
233
234 }
235 }

  ViewVC Help
Powered by ViewVC 1.1.20