1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
3 |
import android.app.AlertDialog; |
import java.util.ArrayList; |
4 |
|
import java.util.List; |
5 |
|
|
6 |
import android.app.Dialog; |
import android.app.Dialog; |
7 |
import android.app.ListActivity; |
import android.app.ListActivity; |
8 |
import android.app.ProgressDialog; |
import android.app.ProgressDialog; |
|
import android.content.DialogInterface; |
|
9 |
import android.content.Intent; |
import android.content.Intent; |
10 |
import android.os.AsyncTask; |
import android.os.AsyncTask; |
11 |
import android.os.Bundle; |
import android.os.Bundle; |
12 |
import android.os.Handler; |
import android.os.Handler; |
13 |
import android.os.Message; |
import android.os.Message; |
14 |
|
|
15 |
import android.view.View; |
import android.view.View; |
16 |
import android.widget.ListView; |
import android.widget.ListView; |
17 |
|
import dk.thoerup.traininfo.util.MessageBox; |
18 |
|
|
19 |
public class TrainInfoList extends ListActivity { |
public class TrainInfoList extends ListActivity { |
20 |
public static final int GOTLOCATION = 1; |
public static final int GOTLOCATION = 1; |
30 |
StationLocator locator = null; |
StationLocator locator = null; |
31 |
LocatorTask locatorTask = new LocatorTask(); |
LocatorTask locatorTask = new LocatorTask(); |
32 |
|
|
33 |
boolean isRunning; |
boolean isRunning = false; |
34 |
|
List<StationBean> stations = new ArrayList<StationBean>(); |
35 |
|
|
36 |
|
|
37 |
StationListAdapter adapter = null; |
StationListAdapter adapter = null; |
38 |
|
@SuppressWarnings("unchecked") |
39 |
@Override |
@Override |
40 |
public void onCreate(Bundle savedInstanceState) { |
public void onCreate(Bundle savedInstanceState) { |
41 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
42 |
setContentView(R.layout.main); |
setContentView(R.layout.main); |
43 |
|
|
44 |
|
StationLocator.removeMockLocation(this); |
45 |
//StationLocator.injectMockLocation(this); |
//StationLocator.injectMockLocation(this); |
46 |
|
|
47 |
adapter = new StationListAdapter(this); |
adapter = new StationListAdapter(this); |
48 |
setListAdapter(adapter); |
setListAdapter(adapter); |
49 |
|
|
50 |
locator = new StationLocator(this, stationsFetched); |
locator = new StationLocator(this, stationsFetched); |
51 |
|
if (savedInstanceState == null) { |
52 |
startLookup(); |
startLookup(); |
53 |
|
} else { |
54 |
|
stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations"); |
55 |
|
adapter.setStations(new ArrayList<StationBean>(stations)); |
56 |
|
} |
57 |
} |
} |
58 |
|
|
59 |
|
@Override |
60 |
|
public void onSaveInstanceState(Bundle outState) |
61 |
|
{ |
62 |
|
if (dialog != null && dialog.isShowing()) |
63 |
|
dialog.dismiss(); |
64 |
|
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
65 |
|
} |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
@Override |
@Override |
97 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
98 |
|
|
99 |
locator.locateStations(); |
locator.locateStations(); |
100 |
stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000); |
stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000); |
101 |
} |
} |
102 |
|
|
103 |
|
|
104 |
Handler stationsFetched = new Handler() { |
Handler stationsFetched = new Handler() { |
105 |
@Override |
@Override |
106 |
public void handleMessage(Message msg) { |
public void handleMessage(Message msg) { |
|
|
|
107 |
switch (msg.what) { |
switch (msg.what) { |
108 |
case GOTLOCATION: |
case GOTLOCATION: |
109 |
dialog.setMessage("Finding nearby stations"); |
dialog.setMessage("Finding nearby stations"); |
111 |
break; |
break; |
112 |
case GOTSTATIONLIST: |
case GOTSTATIONLIST: |
113 |
dialog.dismiss(); |
dialog.dismiss(); |
114 |
adapter.setStations( locator.getStations() ); |
if (locator.getStations().size() == 0) |
115 |
|
MessageBox.showMessage(TrainInfoList.this,"Error loading station list!"); |
116 |
|
stations = locator.getStations(); |
117 |
|
adapter.setStations( stations ); |
118 |
break; |
break; |
119 |
case NOPROVIDER: |
case NOPROVIDER: |
120 |
dialog.dismiss(); |
dialog.dismiss(); |
121 |
showMessageBox("No Location provider enabled. Plase enabled gps."); |
MessageBox.showMessage(TrainInfoList.this,"No location provider enabled. Plase enable gps."); |
122 |
break; |
break; |
123 |
case FIXTIMEOUT: |
case FIXTIMEOUT: |
124 |
dialog.dismiss(); |
dialog.dismiss(); |
125 |
if (isRunning) { |
if (isRunning) { |
126 |
locator.abortLocationListener(); |
locator.abortLocationListener(); |
127 |
showMessageBox("GPS fix timed out"); |
MessageBox.showMessage(TrainInfoList.this,"GPS fix timed out"); |
128 |
} |
} |
129 |
break; |
break; |
130 |
case LOOKUPSTATIONFAILED: |
case LOOKUPSTATIONFAILED: |
131 |
dialog.dismiss(); |
dialog.dismiss(); |
132 |
showMessageBox("Error on finding nearby stations"); |
MessageBox.showMessage(TrainInfoList.this,"Error on finding nearby stations"); |
133 |
break; |
break; |
134 |
} |
} |
|
|
|
135 |
isRunning = false; |
isRunning = false; |
136 |
} |
} |
137 |
}; |
}; |
142 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
143 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
144 |
|
|
145 |
StationBean station = adapter.getStation(position); |
|
146 |
|
StationBean station = stations.get(position); |
147 |
|
|
148 |
|
|
149 |
Intent intent = new Intent(this, DepartureList.class); |
Intent intent = new Intent(this, DepartureList.class); |
155 |
startActivity(intent); |
startActivity(intent); |
156 |
} |
} |
157 |
|
|
|
public void showMessageBox(String message) { |
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
|
|
builder.setMessage(message) |
|
|
.setCancelable(false) |
|
|
.setPositiveButton("OK", new DialogInterface.OnClickListener() { |
|
|
public void onClick(DialogInterface dialog, int id) { |
|
|
dialog.dismiss(); |
|
|
} |
|
|
}) |
|
|
.show(); |
|
|
|
|
|
} |
|
158 |
|
|
159 |
class LocatorTask extends AsyncTask<Void,Void,Void> { |
class LocatorTask extends AsyncTask<Void,Void,Void> { |
160 |
@Override |
@Override |
161 |
protected void onPreExecute() { |
protected void onPreExecute() { |
162 |
|
|
163 |
super.onPreExecute(); |
super.onPreExecute(); |
164 |
} |
} |
165 |
|
|
166 |
@Override |
@Override |
167 |
protected Void doInBackground(Void... params) { |
protected Void doInBackground(Void... params) { |
168 |
locator.findNearestStations(); |
locator.findNearestStations(); |
169 |
|
|
170 |
return null; |
return null; |
171 |
} |
} |
172 |
|
|
173 |
@Override |
@Override |
174 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
175 |
super.onPostExecute(result); |
super.onPostExecute(result); |
176 |
|
|
177 |
} |
} |
178 |
} |
} |
179 |
} |
} |