/[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 258 - (hide annotations) (download)
Mon Aug 10 19:37:18 2009 UTC (14 years, 9 months ago) by torben
File size: 4442 byte(s)
Also make the station list work correctly when device is flipped
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     adapter.setStations(new ArrayList<StationBean>(stations));
56     }
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 245 MessageBox.showMessage(TrainInfoList.this,"GPS fix timed out");
128 torben 237 }
129     break;
130     case LOOKUPSTATIONFAILED:
131     dialog.dismiss();
132 torben 245 MessageBox.showMessage(TrainInfoList.this,"Error on finding nearby stations");
133 torben 237 break;
134     }
135     isRunning = false;
136     }
137     };
138    
139    
140    
141     @Override
142     protected void onListItemClick(ListView l, View v, int position, long id) {
143     super.onListItemClick(l, v, position, id);
144    
145    
146 torben 258 StationBean station = stations.get(position);
147 torben 237
148 torben 258
149 torben 237 Intent intent = new Intent(this, DepartureList.class);
150     intent.putExtra("name", station.getName());
151     intent.putExtra("address", station.getAddress());
152     intent.putExtra("distance", station.getDistance());
153 torben 239 intent.putExtra("latitude", station.getLatitude());
154     intent.putExtra("longitude", station.getLongitude());
155 torben 237 startActivity(intent);
156     }
157    
158 torben 241
159     class LocatorTask extends AsyncTask<Void,Void,Void> {
160     @Override
161     protected void onPreExecute() {
162 torben 258
163 torben 241 super.onPreExecute();
164     }
165    
166     @Override
167     protected Void doInBackground(Void... params) {
168     locator.findNearestStations();
169 torben 258
170 torben 241 return null;
171     }
172    
173     @Override
174     protected void onPostExecute(Void result) {
175     super.onPostExecute(result);
176 torben 258
177 torben 241 }
178     }
179 torben 237 }

  ViewVC Help
Powered by ViewVC 1.1.20