1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
3 |
import java.util.ArrayList; |
import static dk.thoerup.traininfo.R.string.app_name; |
4 |
import java.util.List; |
import static dk.thoerup.traininfo.R.string.generic_cancel; |
5 |
|
import static dk.thoerup.traininfo.R.string.generic_retry; |
6 |
|
import static dk.thoerup.traininfo.R.string.stationlist_accuracy; |
7 |
|
import static dk.thoerup.traininfo.R.string.stationlist_addfavorite; |
8 |
|
import static dk.thoerup.traininfo.R.string.stationlist_favorites; |
9 |
|
import static dk.thoerup.traininfo.R.string.stationlist_gpsinfo; |
10 |
|
import static dk.thoerup.traininfo.R.string.stationlist_gpstimeout; |
11 |
|
import static dk.thoerup.traininfo.R.string.stationlist_latitude; |
12 |
|
import static dk.thoerup.traininfo.R.string.stationlist_locationinfo; |
13 |
|
import static dk.thoerup.traininfo.R.string.stationlist_longitude; |
14 |
|
import static dk.thoerup.traininfo.R.string.stationlist_nearbystations; |
15 |
|
import static dk.thoerup.traininfo.R.string.stationlist_nofavorites; |
16 |
|
import static dk.thoerup.traininfo.R.string.stationlist_nolocationprovider; |
17 |
|
import static dk.thoerup.traininfo.R.string.stationlist_obtainedby; |
18 |
|
import static dk.thoerup.traininfo.R.string.stationlist_removefavorite; |
19 |
|
import static dk.thoerup.traininfo.R.string.stationlist_satellitecount; |
20 |
|
import static dk.thoerup.traininfo.R.string.stationlist_search; |
21 |
|
import static dk.thoerup.traininfo.R.string.stationlist_stationadded; |
22 |
|
import static dk.thoerup.traininfo.R.string.stationlist_stationmap; |
23 |
|
import static dk.thoerup.traininfo.R.string.stationlist_stationremoved; |
24 |
|
import static dk.thoerup.traininfo.R.string.stationlist_waitforlocation; |
25 |
|
|
26 |
|
import java.util.ArrayList; |
27 |
|
|
28 |
import android.app.Activity; |
import android.app.Activity; |
29 |
import android.app.AlertDialog; |
import android.app.AlertDialog; |
35 |
import android.content.SharedPreferences; |
import android.content.SharedPreferences; |
36 |
import android.content.SharedPreferences.Editor; |
import android.content.SharedPreferences.Editor; |
37 |
import android.location.Location; |
import android.location.Location; |
|
import android.os.AsyncTask; |
|
38 |
import android.os.Bundle; |
import android.os.Bundle; |
39 |
import android.os.Handler; |
import android.os.Handler; |
40 |
import android.os.Message; |
import android.os.Message; |
41 |
|
import android.text.Editable; |
42 |
|
import android.text.TextWatcher; |
43 |
|
import android.util.Log; |
44 |
import android.view.ContextMenu; |
import android.view.ContextMenu; |
45 |
import android.view.LayoutInflater; |
import android.view.ContextMenu.ContextMenuInfo; |
46 |
import android.view.Menu; |
import android.view.Menu; |
47 |
import android.view.MenuItem; |
import android.view.MenuItem; |
48 |
import android.view.View; |
import android.view.View; |
|
import android.view.ContextMenu.ContextMenuInfo; |
|
49 |
import android.view.View.OnCreateContextMenuListener; |
import android.view.View.OnCreateContextMenuListener; |
50 |
import android.widget.AdapterView; |
import android.widget.AdapterView; |
|
import android.widget.EditText; |
|
51 |
import android.widget.ListView; |
import android.widget.ListView; |
52 |
|
import android.widget.TextView; |
53 |
import android.widget.Toast; |
import android.widget.Toast; |
54 |
|
import dk.thoerup.android.traininfo.common.StationBean; |
55 |
|
import dk.thoerup.android.traininfo.common.StationEntry; |
56 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
import dk.thoerup.traininfo.provider.ProviderFactory; |
57 |
import dk.thoerup.traininfo.provider.StationProvider; |
import dk.thoerup.traininfo.provider.StationProvider; |
58 |
import dk.thoerup.traininfo.stationmap.GeoPair; |
import dk.thoerup.traininfo.stationmap.GeoPair; |
60 |
import dk.thoerup.traininfo.util.IntSet; |
import dk.thoerup.traininfo.util.IntSet; |
61 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
62 |
|
|
|
import static dk.thoerup.traininfo.R.string.*; |
|
|
|
|
63 |
public class StationList extends ListActivity { |
public class StationList extends ListActivity { |
64 |
public static final int GOTLOCATION = 1001; |
|
|
public static final int GOTSTATIONLIST = 1002; |
|
|
public static final int NOPROVIDER = 1003; |
|
|
public static final int LOCATIONFIXTIMEOUT = 1004; |
|
65 |
|
|
66 |
public static final int OPTIONS_MAP = 2003; |
|
67 |
public static final int OPTIONS_GPSINFO = 2004; |
public static final int OPTIONS_MAP = 103; |
68 |
|
public static final int OPTIONS_GPSINFO = 104; |
69 |
|
|
70 |
public static final int DLG_PROGRESS = 3001; |
public static final int DLG_PROGRESS = 3001; |
|
public static final int DLG_STATIONNAME = 3002; |
|
71 |
|
|
72 |
|
|
73 |
public static final int GPS_TIMEOUT_MS = 17500; //how long are we willing to wait for gps fix -in milliseconds |
public static final int GPS_TIMEOUT_MS = 15000; //how long are we willing to wait for gps fix -in milliseconds |
74 |
|
|
75 |
|
|
76 |
static enum LookupMethod { |
static enum LookupMethod { |
84 |
String dialogMessage = ""; |
String dialogMessage = ""; |
85 |
ProgressDialog dialog; |
ProgressDialog dialog; |
86 |
LocationLookup locationLookup = null; |
LocationLookup locationLookup = null; |
|
FindStationsTask findStationsTask; |
|
87 |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
88 |
|
|
89 |
GeoPair location = new GeoPair(); |
//GeoPair location = new GeoPair(); |
90 |
|
|
91 |
boolean isLaunchedforShortcut; |
boolean isLaunchedforShortcut; |
92 |
boolean isRunning = false; |
|
93 |
List<StationBean> stations = new ArrayList<StationBean>(); |
StationBean stations = new StationBean(); |
94 |
|
|
95 |
StationProvider stationProvider = ProviderFactory.getStationProvider(); |
StationProvider stationProvider = ProviderFactory.getStationProvider(); |
96 |
|
|
101 |
|
|
102 |
WelcomeScreen.ListType listType; |
WelcomeScreen.ListType listType; |
103 |
SharedPreferences prefs; |
SharedPreferences prefs; |
104 |
|
|
105 |
/////////////////////////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////////////////////////// |
106 |
//Activity call backs |
//Activity call backs |
107 |
|
|
108 |
@SuppressWarnings("unchecked") |
|
109 |
@Override |
@Override |
110 |
public void onCreate(Bundle savedInstanceState) { |
public void onCreate(Bundle savedInstanceState) { |
111 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
118 |
ListView lv = getListView(); |
ListView lv = getListView(); |
119 |
lv.setOnCreateContextMenuListener(contextMenu); |
lv.setOnCreateContextMenuListener(contextMenu); |
120 |
|
|
121 |
locationLookup = new LocationLookup(this, stationsFetched); |
locationLookup = new LocationLookup(this); |
122 |
|
|
123 |
|
|
124 |
prefs = getSharedPreferences("TrainStation", 0); |
prefs = getSharedPreferences("TrainStation", 0); |
132 |
|
|
133 |
isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false); |
isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false); |
134 |
|
|
135 |
|
ProviderFactory.purgeOldEntries(); //cleanup before fetching more data |
136 |
|
|
137 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
138 |
|
|
139 |
|
|
140 |
switch (listType) { |
switch (listType) { |
141 |
case ListNearest: |
case ListNearest: |
142 |
startLookup(); |
startNearestLookup(); |
143 |
break; |
break; |
144 |
case ListSearch: |
case ListSearch: |
145 |
showDialog(DLG_STATIONNAME); //TODO: this.showDialogSafe(DLG_STATIONNAME); |
enableNamesearchField(); |
146 |
break; |
break; |
147 |
case ListFavorites: |
case ListFavorites: |
148 |
startFavoriteLookup(); |
startFavoriteLookup(); |
152 |
} |
} |
153 |
|
|
154 |
} else { |
} else { |
155 |
stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations"); |
stations = (StationBean) savedInstanceState.getSerializable("stations"); |
156 |
adapter.setStations(stations); |
adapter.setStations(stations); |
|
location = (GeoPair) savedInstanceState.getSerializable("location"); |
|
157 |
} |
} |
158 |
|
|
159 |
} |
} |
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
|
166 |
@Override |
@Override |
167 |
protected void onDestroy() { |
protected void onDestroy() { |
168 |
super.onDestroy(); |
super.onDestroy(); |
169 |
|
|
170 |
if (findStationsTask != null) { |
stationsFetched.removeMessages(0); |
171 |
findStationsTask.cancel(true); |
|
172 |
} |
|
173 |
if (locationLookup != null) { |
if (locationLookup != null) { |
174 |
locationLookup.stopSearch(); |
locationLookup.stopSearch(); |
175 |
} |
} |
176 |
isRunning = false; |
|
177 |
} |
} |
178 |
|
|
179 |
|
|
198 |
} |
} |
199 |
|
|
200 |
|
|
201 |
/* these 3 dialogs helper functions are very rude and ugly hack |
public void enableNamesearchField() { |
202 |
* to remove these auto-reported exceptions |
final TextView stationSearch = (TextView) findViewById(R.id.stationsearch); |
203 |
* - android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@436aaef8 is not valid; is your activity running? |
stationSearch.setVisibility( View.VISIBLE ); |
204 |
* - java.lang.IllegalArgumentException: View not attached to window manager |
stationSearch.addTextChangedListener( new TextWatcher() { |
205 |
*/ |
|
206 |
|
@Override |
207 |
|
public void afterTextChanged(Editable s) { |
208 |
|
if (s.length() > 0) { |
209 |
|
startNameLookup( s.toString() ); |
210 |
|
} else { |
211 |
|
stations = new StationBean(); |
212 |
|
getListView().invalidateViews(); |
213 |
|
adapter.setStations( stations ); |
214 |
|
} |
215 |
|
} |
216 |
|
|
217 |
/* |
@Override |
218 |
public void showDialogSafe(int id) { |
public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
219 |
try { |
} |
220 |
showDialog(id); |
|
221 |
} catch (Exception e) { |
@Override |
222 |
Log.e("StationList", "showDialog failed", e); |
public void onTextChanged(CharSequence s, int start, int before, int count) { |
223 |
} |
} |
224 |
} |
|
225 |
|
}); |
226 |
public void dismissDialogSafe(int id) { |
|
227 |
try { |
|
|
dismissDialog(id); |
|
|
} catch (Exception e) { |
|
|
Log.e("StationList", "dismissDialog failed", e); |
|
|
} |
|
|
} |
|
|
public void dismissDialogSafe(Dialog dlg) { |
|
|
try { |
|
|
dlg.dismiss(); |
|
|
} catch (Exception e) { |
|
|
Log.e("StationList", "dismissDialog failed", e); |
|
|
} |
|
228 |
} |
} |
|
|
|
|
public void builderShowSafe(AlertDialog.Builder builder) { |
|
|
try { |
|
|
builder.show(); |
|
|
} catch (Exception e) { |
|
|
Log.e("StationList", "builder.show() failed", e); |
|
|
} |
|
229 |
|
|
|
}*/ |
|
|
|
|
|
/* EOF rude and ugly dialog hack */ |
|
|
|
|
|
|
|
230 |
|
|
231 |
@Override |
@Override |
232 |
public void onSaveInstanceState(Bundle outState) |
public void onSaveInstanceState(Bundle outState) |
233 |
{ |
{ |
234 |
if (dialog != null && dialog.isShowing()) |
if (dialog != null && dialog.isShowing()) |
235 |
dialog.dismiss(); |
dialog.dismiss(); |
236 |
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
outState.putSerializable("stations", (StationBean) stations); |
|
outState.putSerializable("location", location); |
|
237 |
|
|
238 |
} |
} |
239 |
|
|
247 |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
248 |
|
|
249 |
item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo)); |
item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo)); |
250 |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
item.setIcon(android.R.drawable.ic_menu_info_details); |
251 |
|
boolean hasLoc = (locationLookup.getLocation() != null); |
252 |
|
item.setEnabled(hasLoc); |
253 |
|
|
254 |
return true; |
return true; |
255 |
} |
} |
256 |
|
|
257 |
|
|
258 |
|
|
259 |
|
|
260 |
@Override |
@Override |
261 |
public boolean onOptionsItemSelected(MenuItem item) { |
public boolean onOptionsItemSelected(MenuItem item) { |
262 |
boolean retval = true; |
boolean retval = true; |
263 |
|
|
264 |
//TODO: Cleanup |
|
265 |
switch (item.getItemId()) { |
switch (item.getItemId()) { |
266 |
case OPTIONS_MAP: |
case OPTIONS_MAP: |
267 |
|
|
268 |
|
if ( stations == null || stations.entries == null || stations.entries.size() == 0 ) { |
269 |
|
Toast.makeText(this, "No stations to show on map", Toast.LENGTH_SHORT).show(); //TODO: Translate |
270 |
|
return true; |
271 |
|
} |
272 |
|
|
273 |
Intent intent = new Intent(this,StationMapView.class); |
Intent intent = new Intent(this,StationMapView.class); |
274 |
|
|
275 |
ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>(); |
ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>(); |
276 |
for (StationBean st : stations ) { |
for (StationEntry st : stations.entries ) { |
277 |
stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) ); |
stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) ); |
278 |
} |
} |
279 |
|
|
285 |
Location loc = locationLookup.getLocation(); |
Location loc = locationLookup.getLocation(); |
286 |
StringBuffer message = new StringBuffer(); |
StringBuffer message = new StringBuffer(); |
287 |
message.append( getString(stationlist_locationinfo) ).append(":\n"); |
message.append( getString(stationlist_locationinfo) ).append(":\n"); |
288 |
if (loc != null) { |
|
289 |
message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n"); |
message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n"); |
290 |
message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n"); |
message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n"); |
291 |
message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n"); |
message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n"); |
292 |
message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n"); |
message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n"); |
293 |
} else { |
|
|
message.append( getString(stationlist_nolocation) ); |
|
|
} |
|
294 |
|
|
295 |
MessageBox.showMessage(this, message.toString(), false); |
MessageBox.showMessage(this, message.toString(), false); |
296 |
break; |
break; |
335 |
dlg.setMessage( getString(stationlist_waitforlocation) ); |
dlg.setMessage( getString(stationlist_waitforlocation) ); |
336 |
dlg.setCancelable(false); |
dlg.setCancelable(false); |
337 |
return dlg; |
return dlg; |
|
case DLG_STATIONNAME: |
|
|
LayoutInflater factory = LayoutInflater.from(this); |
|
|
final View rootView = factory.inflate(R.layout.textinput, null); |
|
|
|
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
|
|
|
|
|
builder.setTitle( getString(stationlist_stationsearch) ); |
|
|
builder.setView(rootView); |
|
|
builder.setCancelable(true); |
|
|
builder.setPositiveButton( getString(generic_search), new DialogInterface.OnClickListener() { |
|
|
public void onClick(DialogInterface dialog, int which) { |
|
|
EditText et = (EditText) rootView.findViewById(R.id.EditText); |
|
|
dialog.dismiss(); |
|
|
String search = et.getText().toString().trim(); |
|
|
if (search.length() >= 2) { |
|
|
startNameSearch(search); |
|
|
} else { |
|
|
showMessageAndClose( getString(stationlist_twocharmin) ); |
|
|
} |
|
|
} |
|
|
}); |
|
|
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
|
|
public void onClick(DialogInterface dialog, int which) { |
|
|
dialog.dismiss(); |
|
|
StationList.this.finish(); // Close this Activity |
|
|
} |
|
|
}); |
|
|
return builder.create(); |
|
338 |
|
|
339 |
default: |
default: |
340 |
return super.onCreateDialog(id); |
return super.onCreateDialog(id); |
361 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
362 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
363 |
|
|
364 |
StationBean station = stations.get(position); |
StationEntry station = stations.entries.get(position); |
365 |
|
|
366 |
if (isLaunchedforShortcut == true) { |
if (isLaunchedforShortcut == true) { |
367 |
Intent i = new Intent(); |
Intent i = new Intent(); |
378 |
///////////////////////////////////////////////////////////// |
///////////////////////////////////////////////////////////// |
379 |
// |
// |
380 |
|
|
381 |
public void startLookup() { |
public void startNearestLookup() { |
|
isRunning = true; |
|
382 |
dialogMessage = getString( stationlist_waitforlocation ); |
dialogMessage = getString( stationlist_waitforlocation ); |
383 |
showDialog(DLG_PROGRESS);//TODO:showDialogSafe(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
384 |
|
|
385 |
locationLookup.locateStations(); |
locationLookup.locateStations(); |
386 |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS); |
stationsFetched.sendEmptyMessageDelayed(0, 500); |
387 |
} |
} |
388 |
|
|
389 |
void startNameSearch(String name) { |
void startNameLookup(String name) { |
|
dialogMessage = getString( stationlist_findbyname ); |
|
|
showDialog(DLG_PROGRESS);//TODO:showDialogSafe(DLG_PROGRESS); |
|
|
|
|
|
findStationsTask = new FindStationsTask(); |
|
|
findStationsTask.searchByName(name); |
|
|
findStationsTask.execute(); |
|
390 |
|
|
391 |
|
stations = stationProvider.lookupStationsByName(name); |
392 |
|
getListView().invalidateViews(); |
393 |
|
adapter.setStations( stations ); |
394 |
} |
} |
395 |
|
|
396 |
public void startFavoriteLookup() { |
public void startFavoriteLookup() { |
397 |
|
|
398 |
if (favorites.size() > 0) { |
if (favorites.size() > 0) { |
399 |
dialogMessage = getString( stationlist_loadfavorites ); |
|
400 |
showDialog(DLG_PROGRESS);//TODO:showDialogSafe(DLG_PROGRESS); |
stations = stationProvider.lookupStationsByIds( favorites.toString() ); |
401 |
|
getListView().invalidateViews(); |
402 |
findStationsTask = new FindStationsTask(); |
adapter.setStations( stations ); |
403 |
findStationsTask.searchByIds( favorites.toString() ); |
|
404 |
findStationsTask.execute(); |
|
405 |
} else { |
} else { |
406 |
showMessageAndClose( getString( stationlist_nofavorites ) ); |
showMessageAndClose( getString( stationlist_nofavorites ) ); |
407 |
} |
} |
409 |
|
|
410 |
|
|
411 |
|
|
412 |
void startLocatorTask() |
void startNearestLookupPhase2() |
413 |
{ |
{ |
414 |
dialogMessage = getString( stationlist_findingnearby ); |
stations = stationProvider.lookupStationsByLocation( locationLookup.getLocation() ); |
415 |
showDialog(DLG_PROGRESS);//TODO:showDialogSafe(DLG_PROGRESS); |
getListView().invalidateViews(); |
416 |
|
adapter.setStations( stations ); |
|
findStationsTask = new FindStationsTask(); |
|
|
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
|
|
findStationsTask.execute(); |
|
417 |
} |
} |
418 |
|
|
419 |
|
|
424 |
@Override |
@Override |
425 |
public void handleMessage(Message msg) { |
public void handleMessage(Message msg) { |
426 |
|
|
427 |
switch (msg.what) { |
LocationLookup.LookupStates state = locationLookup.getState(); |
428 |
|
|
429 |
|
|
430 |
|
switch (state) { |
431 |
case GOTLOCATION: |
case GOTLOCATION: |
432 |
dismissDialog(DLG_PROGRESS);//TODO:dismissDialogSafe(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
433 |
|
|
434 |
startLocatorTask(); |
startNearestLookupPhase2(); |
435 |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
|
436 |
|
return; |
|
break; |
|
437 |
|
|
438 |
case NOPROVIDER: |
case NOPROVIDER: |
439 |
dismissDialog(DLG_PROGRESS);//TODO:dismissDialogSafe(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
440 |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); |
441 |
//StationList.this.finish(); |
//StationList.this.finish(); |
442 |
break; |
return; |
443 |
case LOCATIONFIXTIMEOUT: |
case IDLE: |
444 |
if (isRunning) { |
Log.e("TrainInfo", "How did this happen ???"); |
445 |
locationLookup.stopSearch(); |
dismissDialog(DLG_PROGRESS); // how did we get here ?? |
446 |
if (locationLookup.hasLocation()) { |
return; |
447 |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
|
|
} else { |
|
|
dismissDialog(DLG_PROGRESS);//TODO:dismissDialogSafe(DLG_PROGRESS); |
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
|
|
builder.setMessage( getString( stationlist_gpstimeout) ); |
|
|
builder.setCancelable(true); |
|
|
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
|
|
public void onClick(DialogInterface dialog, int id) { |
|
|
dialog.dismiss(); |
|
|
startLookup(); |
|
|
|
|
|
} |
|
|
}); |
|
|
builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() { |
|
|
public void onClick(DialogInterface dialog, int id) { |
|
|
dialog.dismiss(); |
|
|
} |
|
|
}); |
|
|
builder.show();//TODO:builderShowSafe(builder); |
|
|
|
|
|
} |
|
|
} |
|
|
break; |
|
448 |
} |
} |
|
isRunning = false; |
|
|
} |
|
|
}; |
|
449 |
|
|
|
|
|
|
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
|
|
|
|
|
LookupMethod method = LookupMethod.MethodNone; |
|
|
boolean success; |
|
|
String name; |
|
|
Location loc; |
|
|
String ids; |
|
|
|
|
|
public void searchByName(String n) { |
|
|
|
|
|
method = LookupMethod.ByName; |
|
|
name = n; |
|
|
} |
|
|
|
|
|
public void searchByLocation(Location l) { |
|
|
method = LookupMethod.ByLocation; |
|
|
loc = l; |
|
|
} |
|
|
|
|
|
public void searchByIds(String id) { |
|
|
|
|
|
method = LookupMethod.ByList; |
|
|
ids = id; |
|
|
} |
|
|
|
|
|
@Override |
|
|
protected void onPreExecute() { |
|
450 |
|
|
|
if (method.equals(LookupMethod.MethodNone)) |
|
|
throw new RuntimeException("Method not set"); |
|
|
super.onPreExecute(); |
|
|
} |
|
|
|
|
|
@Override |
|
|
protected Void doInBackground(Void... params) { |
|
451 |
|
|
452 |
switch (method) { |
if (locationLookup.elapsedTime() >= GPS_TIMEOUT_MS) { |
453 |
case ByLocation: |
dismissDialog(DLG_PROGRESS); |
|
success = stationProvider.lookupStations(loc); |
|
|
break; |
|
|
case ByName: |
|
|
success = stationProvider.lookupStationsByName(name); |
|
|
break; |
|
|
case ByList: |
|
|
success = stationProvider.lookupStationsByIds(ids); |
|
|
break; |
|
|
default: |
|
|
success = false; // not possible |
|
|
} |
|
|
|
|
|
|
|
|
return null; |
|
|
} |
|
454 |
|
|
|
@Override |
|
|
protected void onPostExecute(Void result) { |
|
|
super.onPostExecute(result); |
|
|
dialog.dismiss();//TODO:dismissDialogSafe(dialog); |
|
|
|
|
|
|
|
|
if (success) { |
|
|
if (stationProvider.getStations().size() == 0) { |
|
|
showMessageAndClose(getString(stationlist_nostations)); |
|
|
} |
|
|
stations = stationProvider.getStations(); |
|
455 |
|
|
456 |
StationList.this.getListView().invalidateViews(); |
locationLookup.stopSearch(); |
457 |
adapter.setStations( stations ); |
|
458 |
|
if (locationLookup.hasLocation()) { |
459 |
|
startNearestLookupPhase2(); |
460 |
} else { //communication or parse errors |
} else { |
461 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
462 |
builder.setMessage(getString(stationlist_fetcherror)); |
builder.setMessage( getString( stationlist_gpstimeout) ); |
463 |
builder.setCancelable(true); |
builder.setCancelable(true); |
464 |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
465 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
466 |
dialog.dismiss(); |
dialog.dismiss(); |
467 |
|
startNearestLookup(); |
468 |
Runnable runner = null; |
|
|
switch (method) { |
|
|
case ByLocation: |
|
|
runner = new Runnable() { |
|
|
@Override |
|
|
public void run() { |
|
|
startLocatorTask(); |
|
|
} |
|
|
}; |
|
|
break; |
|
|
case ByName: |
|
|
runner = new Runnable() { |
|
|
@Override |
|
|
public void run() { |
|
|
startNameSearch( FindStationsTask.this.name ); |
|
|
} |
|
|
}; |
|
|
break; |
|
|
case ByList: |
|
|
runner = new Runnable() { |
|
|
@Override |
|
|
public void run() { |
|
|
startFavoriteLookup(); |
|
|
} |
|
|
}; |
|
|
break; |
|
469 |
} |
} |
470 |
|
}); |
471 |
stationsFetched.post( runner ); |
builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() { |
472 |
} |
public void onClick(DialogInterface dialog, int id) { |
473 |
}); |
dialog.dismiss(); |
474 |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
StationList.this.finish(); // Close this Activity |
475 |
public void onClick(DialogInterface dialog, int id) { |
} |
476 |
dialog.dismiss(); |
}); |
477 |
StationList.this.finish(); |
builder.show(); |
478 |
} |
|
479 |
}); |
} |
480 |
|
} else { |
481 |
builder.show();//TODO:builderShowSafe(builder); |
if (locationLookup.hasGps()) { |
482 |
|
int count = locationLookup.getSatCount(); |
483 |
|
String dialogMessage = getString( stationlist_waitforlocation ) + "\n" + getString( stationlist_satellitecount ) + ": " + count; |
484 |
|
dialog.setMessage( dialogMessage ); |
485 |
|
} |
486 |
|
this.sendEmptyMessageDelayed(0, 500); |
487 |
} |
} |
488 |
|
|
489 |
} |
} |
490 |
} |
} |
491 |
|
|
492 |
|
|
493 |
|
|
494 |
|
|
495 |
class FavoritesMenu implements OnCreateContextMenuListener { |
class FavoritesMenu implements OnCreateContextMenuListener { |
496 |
private final static int FAVORITES_ADD = 9001; |
private final static int FAVORITES_ADD = 9001; |
504 |
|
|
505 |
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; |
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; |
506 |
selectedPosition = info.position; |
selectedPosition = info.position; |
507 |
int stationID = stations.get(selectedPosition).getId(); |
int stationID = stations.entries.get(selectedPosition).getId(); |
508 |
|
|
509 |
if (!favorites.contains(stationID)) { |
if (!favorites.contains(stationID)) { |
510 |
menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) ); |
menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) ); |
515 |
} |
} |
516 |
|
|
517 |
public void onContextItemSelected(MenuItem item) { |
public void onContextItemSelected(MenuItem item) { |
518 |
StationBean sb = stations.get(selectedPosition); |
StationEntry sb = stations.entries.get(selectedPosition); |
519 |
|
|
520 |
int stationID = sb.getId(); |
int stationID = sb.getId(); |
521 |
if (item.getItemId() == FAVORITES_ADD) { |
if (item.getItemId() == FAVORITES_ADD) { |
528 |
|
|
529 |
|
|
530 |
if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) { |
if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) { |
531 |
stations.remove(selectedPosition); |
stations.entries.remove(selectedPosition); |
532 |
adapter.notifyDataSetChanged(); |
adapter.notifyDataSetChanged(); |
533 |
} |
} |
534 |
} |
} |