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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 316 - (hide 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 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     import android.app.Dialog;
9     import android.app.ListActivity;
10     import android.app.ProgressDialog;
11     import android.content.Intent;
12 torben 316 import android.location.Address;
13     import android.location.Geocoder;
14 torben 239 import android.net.Uri;
15 torben 237 import android.os.AsyncTask;
16     import android.os.Bundle;
17 torben 316 import android.util.Log;
18 torben 237 import android.view.View;
19     import android.widget.ListView;
20     import android.widget.TextView;
21 torben 255 import dk.thoerup.traininfo.provider.DepartureProvider;
22 torben 253 import dk.thoerup.traininfo.provider.ProviderFactory;
23 torben 245 import dk.thoerup.traininfo.util.MessageBox;
24 torben 237
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 torben 257 int selectedItemId;
35     //DepartureBean currentDeparture;
36 torben 237
37     ProgressDialog pgDialog;
38 torben 257 Dialog detailsDialog;
39 torben 238 DepartureFetcher fetcher;
40    
41 torben 239 double latitude,longitude;
42    
43 torben 257 @SuppressWarnings("unchecked")
44 torben 237 @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 torben 239
54     latitude = launchedBy.getDoubleExtra("latitude", 0.0);
55     longitude = launchedBy.getDoubleExtra("longitude", 0.0);
56    
57 torben 237 String name = launchedBy.getStringExtra("name");
58     ((TextView) findViewById(R.id.stationName)).setText( name );
59    
60 torben 316 ((TextView) findViewById(R.id.stationAddr)).setText( "searching..." );
61 torben 237
62 torben 310 int stationId = launchedBy.getIntExtra("stationid", -1);
63 torben 294
64 torben 239 findViewById(R.id.header).setOnClickListener( mapLauncher );
65 torben 237
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 torben 238
73 torben 253 provider = ProviderFactory.getDepartureProvider();
74 torben 238
75     fetcher = new DepartureFetcher();
76 torben 257 if (savedInstanceState == null) {
77     showDialog(DLG_PROGRESS);
78 torben 310 fetcher.execute(stationId);
79 torben 257 } 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 torben 237 }
88    
89 torben 243 @Override
90     public void onSaveInstanceState(Bundle outState)
91     {
92 torben 257 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 torben 243 }
103    
104 torben 237 @Override
105     protected void onListItemClick(ListView l, View v, int position, long id) {
106     super.onListItemClick(l, v, position, id);
107    
108 torben 257 selectedItemId = position;
109 torben 237 showDialog(DLG_DETAILS);
110     }
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_DETAILS:
119 torben 257 DepartureBean currentDeparture = departures.get(selectedItemId);
120 torben 237 ((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime());
121 torben 249 ((TextView)dialog.findViewById(R.id.Train)).setText(currentDeparture.getTrainNumber());
122 torben 237 ((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 torben 257 detailsDialog = dialog;
129 torben 237 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 torben 316 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 torben 237 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 torben 238
200 torben 239 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 torben 238
208 torben 239
209 torben 310 class DepartureFetcher extends AsyncTask<Integer, Void, Void> {
210 torben 238
211 torben 316 String addr;
212 torben 238 @Override
213     protected void onPostExecute(Void result) {
214     super.onPostExecute(result);
215    
216     adapter.setDepartures(departures);
217     pgDialog.dismiss();
218    
219 torben 316
220     ((TextView) findViewById(R.id.stationAddr)).setText( addr );
221    
222 torben 238 if (departures.size() == 0)
223 torben 245 MessageBox.showMessage(DepartureList.this, "No departures found");
224 torben 238 }
225    
226     @Override
227 torben 310 protected Void doInBackground(Integer... params) {
228 torben 316 addr = lookupAddress( latitude , longitude);
229 torben 238 provider.lookupDepartures(params[0]);
230     departures = provider.getDepartures();
231     return null;
232     }
233    
234     }
235 torben 237 }

  ViewVC Help
Powered by ViewVC 1.1.20