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(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 |
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 |
} |
134 |
break; |
135 |
case LOOKUPSTATIONFAILED: |
136 |
dialog.dismiss(); |
137 |
MessageBox.showMessage(TrainInfoList.this,"Error on finding nearby stations"); |
138 |
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 |
StationBean station = stations.get(position); |
152 |
|
153 |
|
154 |
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 |
intent.putExtra("latitude", station.getLatitude()); |
159 |
intent.putExtra("longitude", station.getLongitude()); |
160 |
startActivity(intent); |
161 |
} |
162 |
|
163 |
|
164 |
class LocatorTask extends AsyncTask<Void,Void,Void> { |
165 |
@Override |
166 |
protected void onPreExecute() { |
167 |
|
168 |
super.onPreExecute(); |
169 |
} |
170 |
|
171 |
@Override |
172 |
protected Void doInBackground(Void... params) { |
173 |
locator.findNearestStations(); |
174 |
|
175 |
return null; |
176 |
} |
177 |
|
178 |
@Override |
179 |
protected void onPostExecute(Void result) { |
180 |
super.onPostExecute(result); |
181 |
|
182 |
} |
183 |
} |
184 |
} |