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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 258 - (show 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 package dk.thoerup.traininfo;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import android.app.Dialog;
7 import android.app.ListActivity;
8 import android.app.ProgressDialog;
9 import android.content.Intent;
10 import android.os.AsyncTask;
11 import android.os.Bundle;
12 import android.os.Handler;
13 import android.os.Message;
14
15 import android.view.View;
16 import android.widget.ListView;
17 import dk.thoerup.traininfo.util.MessageBox;
18
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 LocatorTask locatorTask = new LocatorTask();
32
33 boolean isRunning = false;
34 List<StationBean> stations = new ArrayList<StationBean>();
35
36
37 StationListAdapter adapter = null;
38 @SuppressWarnings("unchecked")
39 @Override
40 public void onCreate(Bundle savedInstanceState) {
41 super.onCreate(savedInstanceState);
42 setContentView(R.layout.main);
43
44 StationLocator.removeMockLocation(this);
45 //StationLocator.injectMockLocation(this);
46
47 adapter = new StationListAdapter(this);
48 setListAdapter(adapter);
49
50 locator = new StationLocator(this, stationsFetched);
51 if (savedInstanceState == null) {
52 startLookup();
53 } else {
54 stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations");
55 adapter.setStations(new ArrayList<StationBean>(stations));
56 }
57 }
58
59 @Override
60 public void onSaveInstanceState(Bundle outState)
61 {
62 if (dialog != null && dialog.isShowing())
63 dialog.dismiss();
64 outState.putSerializable("stations", (ArrayList<StationBean>) stations);
65 }
66
67
68
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 stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000);
101 }
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 locatorTask.execute();
111 break;
112 case GOTSTATIONLIST:
113 dialog.dismiss();
114 if (locator.getStations().size() == 0)
115 MessageBox.showMessage(TrainInfoList.this,"Error loading station list!");
116 stations = locator.getStations();
117 adapter.setStations( stations );
118 break;
119 case NOPROVIDER:
120 dialog.dismiss();
121 MessageBox.showMessage(TrainInfoList.this,"No location provider enabled. Plase enable gps.");
122 break;
123 case FIXTIMEOUT:
124 dialog.dismiss();
125 if (isRunning) {
126 locator.abortLocationListener();
127 MessageBox.showMessage(TrainInfoList.this,"GPS fix timed out");
128 }
129 break;
130 case LOOKUPSTATIONFAILED:
131 dialog.dismiss();
132 MessageBox.showMessage(TrainInfoList.this,"Error on finding nearby stations");
133 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 StationBean station = stations.get(position);
147
148
149 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 intent.putExtra("latitude", station.getLatitude());
154 intent.putExtra("longitude", station.getLongitude());
155 startActivity(intent);
156 }
157
158
159 class LocatorTask extends AsyncTask<Void,Void,Void> {
160 @Override
161 protected void onPreExecute() {
162
163 super.onPreExecute();
164 }
165
166 @Override
167 protected Void doInBackground(Void... params) {
168 locator.findNearestStations();
169
170 return null;
171 }
172
173 @Override
174 protected void onPostExecute(Void result) {
175 super.onPostExecute(result);
176
177 }
178 }
179 }

  ViewVC Help
Powered by ViewVC 1.1.20