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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 336 - (hide annotations) (download)
Wed Sep 23 12:51:49 2009 UTC (14 years, 8 months ago) by torben
File size: 7478 byte(s)
* Enable retry on errors,
* rename TrainInfoList to StationList
* add AdMob advertising
1 torben 237 package dk.thoerup.traininfo;
2    
3 torben 258 import java.util.ArrayList;
4     import java.util.List;
5 torben 294 import java.util.Locale;
6 torben 258
7 torben 336 import android.app.AlertDialog;
8 torben 237 import android.app.Dialog;
9     import android.app.ListActivity;
10     import android.app.ProgressDialog;
11 torben 336 import android.content.DialogInterface;
12 torben 237 import android.content.Intent;
13 torben 294 import android.location.Address;
14     import android.location.Geocoder;
15 torben 319 import android.location.Location;
16 torben 241 import android.os.AsyncTask;
17 torben 237 import android.os.Bundle;
18     import android.os.Handler;
19     import android.os.Message;
20 torben 294 import android.util.Log;
21 torben 237 import android.view.View;
22     import android.widget.ListView;
23 torben 319 import dk.thoerup.traininfo.provider.ProviderFactory;
24     import dk.thoerup.traininfo.provider.StationProvider;
25 torben 245 import dk.thoerup.traininfo.util.MessageBox;
26 torben 237
27 torben 336 public class StationList extends ListActivity {
28     public static final int GOTLOCATION = 1001;
29     public static final int GOTSTATIONLIST = 1002;
30     public static final int NOPROVIDER = 1003;
31     public static final int LOCATIONFIXTIMEOUT = 1004;
32    
33 torben 237
34 torben 336 public static final int DLG_PROGRESS = 1001;
35 torben 237
36     /** Called when the activity is first created. */
37 torben 336 String dialogMessage = "";
38 torben 237 ProgressDialog dialog;
39 torben 319 LocationLookup locator = null;
40 torben 336 LocatorTask locatorTask;
41 torben 237
42 torben 258 boolean isRunning = false;
43     List<StationBean> stations = new ArrayList<StationBean>();
44    
45 torben 319 StationProvider stationProvider = ProviderFactory.getStationProvider();
46 torben 258
47 torben 319
48 torben 237 StationListAdapter adapter = null;
49 torben 258 @SuppressWarnings("unchecked")
50 torben 237 @Override
51     public void onCreate(Bundle savedInstanceState) {
52     super.onCreate(savedInstanceState);
53     setContentView(R.layout.main);
54    
55 torben 333 LocationLookup.removeMockLocation(this);
56     //LocationLookup.injectMockLocation(this);
57 torben 241
58 torben 237 adapter = new StationListAdapter(this);
59     setListAdapter(adapter);
60    
61 torben 319 locator = new LocationLookup(this, stationsFetched);
62 torben 258 if (savedInstanceState == null) {
63     startLookup();
64     } else {
65     stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations");
66 torben 260 adapter.setStations(stations);
67 torben 258 }
68 torben 237 }
69    
70 torben 243 @Override
71     public void onSaveInstanceState(Bundle outState)
72     {
73 torben 258 if (dialog != null && dialog.isShowing())
74 torben 243 dialog.dismiss();
75 torben 258 outState.putSerializable("stations", (ArrayList<StationBean>) stations);
76 torben 243 }
77 torben 237
78 torben 243
79 torben 237
80     @Override
81     protected Dialog onCreateDialog(int id) {
82     switch (id) {
83     case DLG_PROGRESS:
84     ProgressDialog dlg = new ProgressDialog(this);
85     dlg.setMessage("Wait for location fix");
86     dlg.setCancelable(false);
87 torben 336 return dlg;
88 torben 237 default:
89     return super.onCreateDialog(id);
90     }
91    
92     }
93    
94    
95    
96     @Override
97     protected void onPrepareDialog(int id, Dialog dialog) {
98     super.onPrepareDialog(id, dialog);
99     switch (id) {
100     case DLG_PROGRESS:
101     this.dialog = (ProgressDialog) dialog;
102 torben 336 if (!dialogMessage.equals("")) {
103     this.dialog.setMessage(dialogMessage);
104     dialogMessage = "";
105     }
106 torben 237 break;
107     }
108     }
109    
110     public void startLookup() {
111     isRunning = true;
112     showDialog(DLG_PROGRESS);
113    
114     locator.locateStations();
115 torben 336 stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);
116 torben 237 }
117    
118    
119     Handler stationsFetched = new Handler() {
120     @Override
121     public void handleMessage(Message msg) {
122 torben 336
123 torben 237 switch (msg.what) {
124     case GOTLOCATION:
125 torben 336 dismissDialog(DLG_PROGRESS);
126    
127     startLocatorTask();
128    
129 torben 237 break;
130 torben 319
131 torben 237 case NOPROVIDER:
132 torben 336 dismissDialog(DLG_PROGRESS);
133     MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps.");
134 torben 237 break;
135 torben 336 case LOCATIONFIXTIMEOUT:
136 torben 237 if (isRunning) {
137 torben 336 locator.stopSearch();
138 torben 285 if (locator.hasLocation()) {
139 torben 336 stationsFetched.sendEmptyMessage( GOTLOCATION );
140     } else {
141     dismissDialog(DLG_PROGRESS);
142    
143     AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);
144     builder.setMessage("GPS fix timed out");
145     builder.setCancelable(true);
146     builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
147     public void onClick(DialogInterface dialog, int id) {
148     dialog.dismiss();
149     startLookup();
150    
151     }
152     });
153     builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
154     public void onClick(DialogInterface dialog, int id) {
155     dialog.dismiss();
156     }
157     });
158     builder.show();
159    
160 torben 285 }
161 torben 237 }
162     break;
163     }
164     isRunning = false;
165     }
166     };
167    
168 torben 336 void startLocatorTask()
169     {
170     dialogMessage = "Finding nearby stations";
171     showDialog(DLG_PROGRESS);
172    
173     locatorTask = new LocatorTask();
174     locatorTask.execute();
175     }
176 torben 237
177     @Override
178     protected void onListItemClick(ListView l, View v, int position, long id) {
179     super.onListItemClick(l, v, position, id);
180 torben 294
181 torben 258 StationBean station = stations.get(position);
182 torben 294
183     double latitude = station.getLatitude();
184     double longitude = station.getLongitude();
185    
186    
187 torben 237
188     Intent intent = new Intent(this, DepartureList.class);
189     intent.putExtra("name", station.getName());
190     intent.putExtra("distance", station.getDistance());
191 torben 294 intent.putExtra("latitude", latitude);
192     intent.putExtra("longitude", longitude);
193 torben 310 intent.putExtra("stationid", station.getId());
194 torben 317 intent.putExtra("address", station.getAddress());
195 torben 237 startActivity(intent);
196     }
197    
198 torben 317 String lookupAddress(double latitude, double longitude) {
199    
200     Geocoder coder = new Geocoder(this, new Locale("da"));
201     StringBuilder sb = new StringBuilder();
202     Log.i("lookupaddr", "" + latitude + "/" + longitude);
203     try {
204     List<Address> addressList = coder.getFromLocation(latitude, longitude, 1);
205     Address addr = addressList.get(0);
206    
207    
208     int max = addr.getMaxAddressLineIndex();
209     for (int i=0; i<max; i++) {
210     if (i>0)
211     sb.append(", ");
212    
213     sb.append(addr.getAddressLine(i));
214     }
215    
216    
217     } catch (Exception e) {
218     Log.e("DepartureList", "geocoder failed", e);
219     }
220    
221     return sb.toString();
222     }
223 torben 241
224     class LocatorTask extends AsyncTask<Void,Void,Void> {
225 torben 319 boolean success;
226 torben 241 @Override
227     protected void onPreExecute() {
228 torben 258
229 torben 241 super.onPreExecute();
230     }
231    
232     @Override
233     protected Void doInBackground(Void... params) {
234 torben 319 Location loc = locator.getLocation();
235     success = stationProvider.lookupStations(loc);
236 torben 258
237 torben 319
238     List<StationBean> stations = stationProvider.getStations();
239 torben 317 for (StationBean station : stations) {
240     String addr = lookupAddress(station.getLatitude(), station.getLongitude());
241     station.setAddress(addr);
242     }
243    
244 torben 241 return null;
245     }
246    
247     @Override
248     protected void onPostExecute(Void result) {
249     super.onPostExecute(result);
250 torben 319 dialog.dismiss();
251    
252     if (success) {
253     if (stationProvider.getStations().size() == 0)
254 torben 336 MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?!
255 torben 319 stations = stationProvider.getStations();
256     adapter.setStations( stations );
257    
258     } else { //communication or parse errors
259 torben 336 AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);
260     builder.setMessage("Error on finding nearby stations");
261     builder.setCancelable(true);
262     builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
263     public void onClick(DialogInterface dialog, int id) {
264     dialog.dismiss();
265    
266     stationsFetched.post( new Runnable() {
267     @Override
268     public void run() {
269     startLocatorTask();
270     }
271     });
272     }
273     });
274     builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
275     public void onClick(DialogInterface dialog, int id) {
276     dialog.dismiss();
277     }
278     });
279     builder.show();
280 torben 319 }
281 torben 241 }
282     }
283 torben 237 }

  ViewVC Help
Powered by ViewVC 1.1.20