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