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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 285 - (hide annotations) (download)
Fri Aug 28 06:34:42 2009 UTC (14 years, 9 months ago) by torben
File size: 4561 byte(s)
Safer fail over if there actually was obtained a usable location
1 torben 237 package dk.thoerup.traininfo;
2    
3 torben 258 import java.util.ArrayList;
4     import java.util.List;
5    
6 torben 237 import android.app.Dialog;
7     import android.app.ListActivity;
8     import android.app.ProgressDialog;
9     import android.content.Intent;
10 torben 241 import android.os.AsyncTask;
11 torben 237 import android.os.Bundle;
12     import android.os.Handler;
13     import android.os.Message;
14 torben 258
15 torben 237 import android.view.View;
16     import android.widget.ListView;
17 torben 245 import dk.thoerup.traininfo.util.MessageBox;
18 torben 237
19     public class TrainInfoList extends ListActivity {
20     public static final int GOTLOCATION = 1;
21     public static final int GOTSTATIONLIST = 2;
22     public static final int NOPROVIDER = 3;
23     public static final int FIXTIMEOUT = 4;
24     public static final int LOOKUPSTATIONFAILED = 5;
25    
26     public static final int DLG_PROGRESS = 1;
27    
28     /** Called when the activity is first created. */
29     ProgressDialog dialog;
30     StationLocator locator = null;
31 torben 241 LocatorTask locatorTask = new LocatorTask();
32 torben 237
33 torben 258 boolean isRunning = false;
34     List<StationBean> stations = new ArrayList<StationBean>();
35    
36    
37 torben 237 StationListAdapter adapter = null;
38 torben 258 @SuppressWarnings("unchecked")
39 torben 237 @Override
40     public void onCreate(Bundle savedInstanceState) {
41     super.onCreate(savedInstanceState);
42     setContentView(R.layout.main);
43    
44 torben 252 StationLocator.removeMockLocation(this);
45 torben 241 //StationLocator.injectMockLocation(this);
46    
47 torben 237 adapter = new StationListAdapter(this);
48     setListAdapter(adapter);
49    
50     locator = new StationLocator(this, stationsFetched);
51 torben 258 if (savedInstanceState == null) {
52     startLookup();
53     } else {
54     stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations");
55 torben 260 adapter.setStations(stations);
56 torben 258 }
57 torben 237 }
58    
59 torben 243 @Override
60     public void onSaveInstanceState(Bundle outState)
61     {
62 torben 258 if (dialog != null && dialog.isShowing())
63 torben 243 dialog.dismiss();
64 torben 258 outState.putSerializable("stations", (ArrayList<StationBean>) stations);
65 torben 243 }
66 torben 237
67 torben 243
68 torben 237
69     @Override
70     protected Dialog onCreateDialog(int id) {
71     switch (id) {
72     case DLG_PROGRESS:
73     ProgressDialog dlg = new ProgressDialog(this);
74     dlg.setMessage("Wait for location fix");
75     dlg.setCancelable(false);
76     return dlg;
77     default:
78     return super.onCreateDialog(id);
79     }
80    
81     }
82    
83    
84    
85     @Override
86     protected void onPrepareDialog(int id, Dialog dialog) {
87     super.onPrepareDialog(id, dialog);
88     switch (id) {
89     case DLG_PROGRESS:
90     this.dialog = (ProgressDialog) dialog;
91     break;
92     }
93     }
94    
95     public void startLookup() {
96     isRunning = true;
97     showDialog(DLG_PROGRESS);
98    
99     locator.locateStations();
100 torben 258 stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000);
101 torben 237 }
102    
103    
104     Handler stationsFetched = new Handler() {
105     @Override
106     public void handleMessage(Message msg) {
107     switch (msg.what) {
108     case GOTLOCATION:
109     dialog.setMessage("Finding nearby stations");
110 torben 241 locatorTask.execute();
111 torben 237 break;
112     case GOTSTATIONLIST:
113     dialog.dismiss();
114 torben 245 if (locator.getStations().size() == 0)
115     MessageBox.showMessage(TrainInfoList.this,"Error loading station list!");
116 torben 258 stations = locator.getStations();
117     adapter.setStations( stations );
118 torben 237 break;
119     case NOPROVIDER:
120     dialog.dismiss();
121 torben 245 MessageBox.showMessage(TrainInfoList.this,"No location provider enabled. Plase enable gps.");
122 torben 237 break;
123     case FIXTIMEOUT:
124     dialog.dismiss();
125     if (isRunning) {
126     locator.abortLocationListener();
127 torben 285 if (locator.hasLocation()) {
128     msg.what = GOTLOCATION;
129     handleMessage( msg ); // ToDo: ugly recursive call !!!
130     } else {
131     MessageBox.showMessage(TrainInfoList.this,"GPS fix timed out");
132     }
133 torben 237 }
134     break;
135     case LOOKUPSTATIONFAILED:
136     dialog.dismiss();
137 torben 245 MessageBox.showMessage(TrainInfoList.this,"Error on finding nearby stations");
138 torben 237 break;
139     }
140     isRunning = false;
141     }
142     };
143    
144    
145    
146     @Override
147     protected void onListItemClick(ListView l, View v, int position, long id) {
148     super.onListItemClick(l, v, position, id);
149    
150    
151 torben 258 StationBean station = stations.get(position);
152 torben 237
153 torben 258
154 torben 237 Intent intent = new Intent(this, DepartureList.class);
155     intent.putExtra("name", station.getName());
156     intent.putExtra("address", station.getAddress());
157     intent.putExtra("distance", station.getDistance());
158 torben 239 intent.putExtra("latitude", station.getLatitude());
159     intent.putExtra("longitude", station.getLongitude());
160 torben 237 startActivity(intent);
161     }
162    
163 torben 241
164     class LocatorTask extends AsyncTask<Void,Void,Void> {
165     @Override
166     protected void onPreExecute() {
167 torben 258
168 torben 241 super.onPreExecute();
169     }
170    
171     @Override
172     protected Void doInBackground(Void... params) {
173     locator.findNearestStations();
174 torben 258
175 torben 241 return null;
176     }
177    
178     @Override
179     protected void onPostExecute(Void result) {
180     super.onPostExecute(result);
181 torben 258
182 torben 241 }
183     }
184 torben 237 }

  ViewVC Help
Powered by ViewVC 1.1.20