1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
3 |
|
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; |
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; |
import dk.thoerup.traininfo.util.MessageBox; |
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(stations); |
56 |
|
} |
57 |
} |
} |
58 |
|
|
59 |
@Override |
@Override |
60 |
public void onSaveInstanceState(Bundle outState) |
public void onSaveInstanceState(Bundle outState) |
61 |
{ |
{ |
62 |
if (dialog.isShowing()) |
if (dialog != null && dialog.isShowing()) |
63 |
dialog.dismiss(); |
dialog.dismiss(); |
64 |
|
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
65 |
} |
} |
66 |
|
|
67 |
|
|
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"); |
113 |
dialog.dismiss(); |
dialog.dismiss(); |
114 |
if (locator.getStations().size() == 0) |
if (locator.getStations().size() == 0) |
115 |
MessageBox.showMessage(TrainInfoList.this,"Error loading station list!"); |
MessageBox.showMessage(TrainInfoList.this,"Error loading station list!"); |
116 |
adapter.setStations( locator.getStations() ); |
stations = locator.getStations(); |
117 |
|
adapter.setStations( stations ); |
118 |
break; |
break; |
119 |
case NOPROVIDER: |
case NOPROVIDER: |
120 |
dialog.dismiss(); |
dialog.dismiss(); |
132 |
MessageBox.showMessage(TrainInfoList.this,"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); |
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 |
} |
} |