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.Bundle; |
10 |
import android.os.Handler; |
11 |
import android.os.Message; |
12 |
import android.view.View; |
13 |
import android.widget.ListView; |
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 |
|
28 |
boolean isRunning; |
29 |
|
30 |
StationListAdapter adapter = null; |
31 |
@Override |
32 |
public void onCreate(Bundle savedInstanceState) { |
33 |
super.onCreate(savedInstanceState); |
34 |
setContentView(R.layout.main); |
35 |
|
36 |
adapter = new StationListAdapter(this); |
37 |
setListAdapter(adapter); |
38 |
|
39 |
locator = new StationLocator(this, stationsFetched); |
40 |
|
41 |
startLookup(); |
42 |
} |
43 |
|
44 |
|
45 |
|
46 |
@Override |
47 |
protected Dialog onCreateDialog(int id) { |
48 |
switch (id) { |
49 |
case DLG_PROGRESS: |
50 |
ProgressDialog dlg = new ProgressDialog(this); |
51 |
dlg.setMessage("Wait for location fix"); |
52 |
dlg.setCancelable(false); |
53 |
return dlg; |
54 |
default: |
55 |
return super.onCreateDialog(id); |
56 |
} |
57 |
|
58 |
} |
59 |
|
60 |
|
61 |
|
62 |
@Override |
63 |
protected void onPrepareDialog(int id, Dialog dialog) { |
64 |
super.onPrepareDialog(id, dialog); |
65 |
switch (id) { |
66 |
case DLG_PROGRESS: |
67 |
this.dialog = (ProgressDialog) dialog; |
68 |
break; |
69 |
} |
70 |
} |
71 |
|
72 |
public void startLookup() { |
73 |
isRunning = true; |
74 |
showDialog(DLG_PROGRESS); |
75 |
|
76 |
locator.locateStations(); |
77 |
stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000); |
78 |
} |
79 |
|
80 |
|
81 |
Handler stationsFetched = new Handler() { |
82 |
@Override |
83 |
public void handleMessage(Message msg) { |
84 |
|
85 |
switch (msg.what) { |
86 |
case GOTLOCATION: |
87 |
dialog.setMessage("Finding nearby stations"); |
88 |
break; |
89 |
case GOTSTATIONLIST: |
90 |
dialog.dismiss(); |
91 |
adapter.setStations( locator.getStations() ); |
92 |
break; |
93 |
case NOPROVIDER: |
94 |
dialog.dismiss(); |
95 |
showMessageBox("No Location provider enabled. Plase enabled gps."); |
96 |
break; |
97 |
case FIXTIMEOUT: |
98 |
dialog.dismiss(); |
99 |
if (isRunning) { |
100 |
locator.abortLocationListener(); |
101 |
showMessageBox("GPS fix timed out"); |
102 |
} |
103 |
break; |
104 |
case LOOKUPSTATIONFAILED: |
105 |
dialog.dismiss(); |
106 |
showMessageBox("Error on finding nearby stations"); |
107 |
break; |
108 |
} |
109 |
|
110 |
isRunning = false; |
111 |
} |
112 |
}; |
113 |
|
114 |
|
115 |
|
116 |
@Override |
117 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
118 |
super.onListItemClick(l, v, position, id); |
119 |
|
120 |
StationBean station = adapter.getStation(position); |
121 |
|
122 |
|
123 |
Intent intent = new Intent(this, DepartureList.class); |
124 |
intent.putExtra("name", station.getName()); |
125 |
intent.putExtra("address", station.getAddress()); |
126 |
intent.putExtra("distance", station.getDistance()); |
127 |
intent.putExtra("latitude", station.getLatitude()); |
128 |
intent.putExtra("longitude", station.getLongitude()); |
129 |
startActivity(intent); |
130 |
} |
131 |
|
132 |
public void showMessageBox(String message) { |
133 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
134 |
builder.setMessage(message) |
135 |
.setCancelable(false) |
136 |
.setPositiveButton("OK", new DialogInterface.OnClickListener() { |
137 |
public void onClick(DialogInterface dialog, int id) { |
138 |
dialog.dismiss(); |
139 |
} |
140 |
}) |
141 |
.show(); |
142 |
|
143 |
} |
144 |
} |