1 |
package dk.thoerup.traininfo; |
2 |
|
3 |
import android.app.AlertDialog; |
4 |
import android.app.Dialog; |
5 |
import android.app.ListActivity; |
6 |
import android.app.ProgressDialog; |
7 |
import android.content.DialogInterface; |
8 |
import android.content.Intent; |
9 |
import android.os.AsyncTask; |
10 |
import android.os.Bundle; |
11 |
import android.os.Handler; |
12 |
import android.os.Message; |
13 |
import android.view.View; |
14 |
import android.widget.ListView; |
15 |
|
16 |
public class TrainInfoList extends ListActivity { |
17 |
public static final int GOTLOCATION = 1; |
18 |
public static final int GOTSTATIONLIST = 2; |
19 |
public static final int NOPROVIDER = 3; |
20 |
public static final int FIXTIMEOUT = 4; |
21 |
public static final int LOOKUPSTATIONFAILED = 5; |
22 |
|
23 |
public static final int DLG_PROGRESS = 1; |
24 |
|
25 |
/** Called when the activity is first created. */ |
26 |
ProgressDialog dialog; |
27 |
StationLocator locator = null; |
28 |
LocatorTask locatorTask = new LocatorTask(); |
29 |
|
30 |
boolean isRunning; |
31 |
|
32 |
StationListAdapter adapter = null; |
33 |
@Override |
34 |
public void onCreate(Bundle savedInstanceState) { |
35 |
super.onCreate(savedInstanceState); |
36 |
setContentView(R.layout.main); |
37 |
|
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 |
adapter.setStations( locator.getStations() ); |
104 |
break; |
105 |
case NOPROVIDER: |
106 |
dialog.dismiss(); |
107 |
showMessageBox("No location provider enabled. Plase enable gps."); |
108 |
break; |
109 |
case FIXTIMEOUT: |
110 |
dialog.dismiss(); |
111 |
if (isRunning) { |
112 |
locator.abortLocationListener(); |
113 |
showMessageBox("GPS fix timed out"); |
114 |
} |
115 |
break; |
116 |
case LOOKUPSTATIONFAILED: |
117 |
dialog.dismiss(); |
118 |
showMessageBox("Error on finding nearby stations"); |
119 |
break; |
120 |
} |
121 |
|
122 |
isRunning = false; |
123 |
} |
124 |
}; |
125 |
|
126 |
|
127 |
|
128 |
@Override |
129 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
130 |
super.onListItemClick(l, v, position, id); |
131 |
|
132 |
StationBean station = adapter.getStation(position); |
133 |
|
134 |
|
135 |
Intent intent = new Intent(this, DepartureList.class); |
136 |
intent.putExtra("name", station.getName()); |
137 |
intent.putExtra("address", station.getAddress()); |
138 |
intent.putExtra("distance", station.getDistance()); |
139 |
intent.putExtra("latitude", station.getLatitude()); |
140 |
intent.putExtra("longitude", station.getLongitude()); |
141 |
startActivity(intent); |
142 |
} |
143 |
|
144 |
public void showMessageBox(String message) { |
145 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
146 |
builder.setMessage(message) |
147 |
.setCancelable(false) |
148 |
.setPositiveButton("OK", new DialogInterface.OnClickListener() { |
149 |
public void onClick(DialogInterface dialog, int id) { |
150 |
dialog.dismiss(); |
151 |
} |
152 |
}) |
153 |
.show(); |
154 |
|
155 |
} |
156 |
|
157 |
class LocatorTask extends AsyncTask<Void,Void,Void> { |
158 |
@Override |
159 |
protected void onPreExecute() { |
160 |
super.onPreExecute(); |
161 |
} |
162 |
|
163 |
@Override |
164 |
protected Void doInBackground(Void... params) { |
165 |
locator.findNearestStations(); |
166 |
return null; |
167 |
} |
168 |
|
169 |
@Override |
170 |
protected void onPostExecute(Void result) { |
171 |
super.onPostExecute(result); |
172 |
} |
173 |
} |
174 |
} |