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.injectMockLocation(this); |
38 |
|
39 |
adapter = new StationListAdapter(this); |
40 |
setListAdapter(adapter); |
41 |
|
42 |
locator = new StationLocator(this, stationsFetched); |
43 |
|
44 |
startLookup(); |
45 |
} |
46 |
|
47 |
@Override |
48 |
public void onSaveInstanceState(Bundle outState) |
49 |
{ |
50 |
if (dialog.isShowing()) |
51 |
dialog.dismiss(); |
52 |
} |
53 |
|
54 |
|
55 |
|
56 |
@Override |
57 |
protected Dialog onCreateDialog(int id) { |
58 |
switch (id) { |
59 |
case DLG_PROGRESS: |
60 |
ProgressDialog dlg = new ProgressDialog(this); |
61 |
dlg.setMessage("Wait for location fix"); |
62 |
dlg.setCancelable(false); |
63 |
return dlg; |
64 |
default: |
65 |
return super.onCreateDialog(id); |
66 |
} |
67 |
|
68 |
} |
69 |
|
70 |
|
71 |
|
72 |
@Override |
73 |
protected void onPrepareDialog(int id, Dialog dialog) { |
74 |
super.onPrepareDialog(id, dialog); |
75 |
switch (id) { |
76 |
case DLG_PROGRESS: |
77 |
this.dialog = (ProgressDialog) dialog; |
78 |
break; |
79 |
} |
80 |
} |
81 |
|
82 |
public void startLookup() { |
83 |
isRunning = true; |
84 |
showDialog(DLG_PROGRESS); |
85 |
|
86 |
locator.locateStations(); |
87 |
stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000); |
88 |
} |
89 |
|
90 |
|
91 |
Handler stationsFetched = new Handler() { |
92 |
@Override |
93 |
public void handleMessage(Message msg) { |
94 |
|
95 |
switch (msg.what) { |
96 |
case GOTLOCATION: |
97 |
dialog.setMessage("Finding nearby stations"); |
98 |
locatorTask.execute(); |
99 |
break; |
100 |
case GOTSTATIONLIST: |
101 |
dialog.dismiss(); |
102 |
if (locator.getStations().size() == 0) |
103 |
MessageBox.showMessage(TrainInfoList.this,"Error loading station list!"); |
104 |
adapter.setStations( locator.getStations() ); |
105 |
break; |
106 |
case NOPROVIDER: |
107 |
dialog.dismiss(); |
108 |
MessageBox.showMessage(TrainInfoList.this,"No location provider enabled. Plase enable gps."); |
109 |
break; |
110 |
case FIXTIMEOUT: |
111 |
dialog.dismiss(); |
112 |
if (isRunning) { |
113 |
locator.abortLocationListener(); |
114 |
MessageBox.showMessage(TrainInfoList.this,"GPS fix timed out"); |
115 |
} |
116 |
break; |
117 |
case LOOKUPSTATIONFAILED: |
118 |
dialog.dismiss(); |
119 |
MessageBox.showMessage(TrainInfoList.this,"Error on finding nearby stations"); |
120 |
break; |
121 |
} |
122 |
|
123 |
isRunning = false; |
124 |
} |
125 |
}; |
126 |
|
127 |
|
128 |
|
129 |
@Override |
130 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
131 |
super.onListItemClick(l, v, position, id); |
132 |
|
133 |
StationBean station = adapter.getStation(position); |
134 |
|
135 |
|
136 |
Intent intent = new Intent(this, DepartureList.class); |
137 |
intent.putExtra("name", station.getName()); |
138 |
intent.putExtra("address", station.getAddress()); |
139 |
intent.putExtra("distance", station.getDistance()); |
140 |
intent.putExtra("latitude", station.getLatitude()); |
141 |
intent.putExtra("longitude", station.getLongitude()); |
142 |
startActivity(intent); |
143 |
} |
144 |
|
145 |
|
146 |
class LocatorTask extends AsyncTask<Void,Void,Void> { |
147 |
@Override |
148 |
protected void onPreExecute() { |
149 |
super.onPreExecute(); |
150 |
} |
151 |
|
152 |
@Override |
153 |
protected Void doInBackground(Void... params) { |
154 |
locator.findNearestStations(); |
155 |
return null; |
156 |
} |
157 |
|
158 |
@Override |
159 |
protected void onPostExecute(Void result) { |
160 |
super.onPostExecute(result); |
161 |
} |
162 |
} |
163 |
} |