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