3 |
import static dk.thoerup.traininfo.R.string.app_name; |
import static dk.thoerup.traininfo.R.string.app_name; |
4 |
import static dk.thoerup.traininfo.R.string.generic_cancel; |
import static dk.thoerup.traininfo.R.string.generic_cancel; |
5 |
import static dk.thoerup.traininfo.R.string.generic_retry; |
import static dk.thoerup.traininfo.R.string.generic_retry; |
|
import static dk.thoerup.traininfo.R.string.generic_search; |
|
6 |
import static dk.thoerup.traininfo.R.string.stationlist_accuracy; |
import static dk.thoerup.traininfo.R.string.stationlist_accuracy; |
7 |
import static dk.thoerup.traininfo.R.string.stationlist_addfavorite; |
import static dk.thoerup.traininfo.R.string.stationlist_addfavorite; |
8 |
import static dk.thoerup.traininfo.R.string.stationlist_favorites; |
import static dk.thoerup.traininfo.R.string.stationlist_favorites; |
|
import static dk.thoerup.traininfo.R.string.stationlist_fetcherror; |
|
|
import static dk.thoerup.traininfo.R.string.stationlist_findbyname; |
|
|
import static dk.thoerup.traininfo.R.string.stationlist_findingnearby; |
|
9 |
import static dk.thoerup.traininfo.R.string.stationlist_gpsinfo; |
import static dk.thoerup.traininfo.R.string.stationlist_gpsinfo; |
10 |
import static dk.thoerup.traininfo.R.string.stationlist_gpstimeout; |
import static dk.thoerup.traininfo.R.string.stationlist_gpstimeout; |
11 |
import static dk.thoerup.traininfo.R.string.stationlist_latitude; |
import static dk.thoerup.traininfo.R.string.stationlist_latitude; |
|
import static dk.thoerup.traininfo.R.string.stationlist_loadfavorites; |
|
12 |
import static dk.thoerup.traininfo.R.string.stationlist_locationinfo; |
import static dk.thoerup.traininfo.R.string.stationlist_locationinfo; |
13 |
import static dk.thoerup.traininfo.R.string.stationlist_longitude; |
import static dk.thoerup.traininfo.R.string.stationlist_longitude; |
14 |
import static dk.thoerup.traininfo.R.string.stationlist_nearbystations; |
import static dk.thoerup.traininfo.R.string.stationlist_nearbystations; |
15 |
import static dk.thoerup.traininfo.R.string.stationlist_nofavorites; |
import static dk.thoerup.traininfo.R.string.stationlist_nofavorites; |
|
import static dk.thoerup.traininfo.R.string.stationlist_nolocation; |
|
16 |
import static dk.thoerup.traininfo.R.string.stationlist_nolocationprovider; |
import static dk.thoerup.traininfo.R.string.stationlist_nolocationprovider; |
|
import static dk.thoerup.traininfo.R.string.stationlist_nostations; |
|
17 |
import static dk.thoerup.traininfo.R.string.stationlist_obtainedby; |
import static dk.thoerup.traininfo.R.string.stationlist_obtainedby; |
18 |
import static dk.thoerup.traininfo.R.string.stationlist_removefavorite; |
import static dk.thoerup.traininfo.R.string.stationlist_removefavorite; |
19 |
import static dk.thoerup.traininfo.R.string.stationlist_satellitecount; |
import static dk.thoerup.traininfo.R.string.stationlist_satellitecount; |
21 |
import static dk.thoerup.traininfo.R.string.stationlist_stationadded; |
import static dk.thoerup.traininfo.R.string.stationlist_stationadded; |
22 |
import static dk.thoerup.traininfo.R.string.stationlist_stationmap; |
import static dk.thoerup.traininfo.R.string.stationlist_stationmap; |
23 |
import static dk.thoerup.traininfo.R.string.stationlist_stationremoved; |
import static dk.thoerup.traininfo.R.string.stationlist_stationremoved; |
|
import static dk.thoerup.traininfo.R.string.stationlist_stationsearch; |
|
|
import static dk.thoerup.traininfo.R.string.stationlist_twocharmin; |
|
24 |
import static dk.thoerup.traininfo.R.string.stationlist_waitforlocation; |
import static dk.thoerup.traininfo.R.string.stationlist_waitforlocation; |
25 |
|
|
26 |
import java.util.ArrayList; |
import java.util.ArrayList; |
32 |
import android.app.ProgressDialog; |
import android.app.ProgressDialog; |
33 |
import android.content.DialogInterface; |
import android.content.DialogInterface; |
34 |
import android.content.Intent; |
import android.content.Intent; |
|
import android.content.SharedPreferences; |
|
|
import android.content.SharedPreferences.Editor; |
|
35 |
import android.location.Location; |
import android.location.Location; |
|
import android.os.AsyncTask; |
|
36 |
import android.os.Bundle; |
import android.os.Bundle; |
37 |
import android.os.Handler; |
import android.os.Handler; |
38 |
import android.os.Message; |
import android.os.Message; |
39 |
|
import android.text.Editable; |
40 |
|
import android.text.TextWatcher; |
41 |
import android.util.Log; |
import android.util.Log; |
42 |
import android.view.ContextMenu; |
import android.view.ContextMenu; |
43 |
import android.view.LayoutInflater; |
import android.view.ContextMenu.ContextMenuInfo; |
44 |
import android.view.Menu; |
import android.view.Menu; |
45 |
import android.view.MenuItem; |
import android.view.MenuItem; |
46 |
import android.view.View; |
import android.view.View; |
|
import android.view.ContextMenu.ContextMenuInfo; |
|
47 |
import android.view.View.OnCreateContextMenuListener; |
import android.view.View.OnCreateContextMenuListener; |
48 |
import android.widget.AdapterView; |
import android.widget.AdapterView; |
|
import android.widget.EditText; |
|
49 |
import android.widget.ListView; |
import android.widget.ListView; |
50 |
|
import android.widget.TextView; |
51 |
import android.widget.Toast; |
import android.widget.Toast; |
52 |
import dk.thoerup.android.traininfo.common.StationBean; |
import dk.thoerup.android.traininfo.common.StationBean; |
53 |
import dk.thoerup.android.traininfo.common.StationBean.StationEntry; |
import dk.thoerup.android.traininfo.common.StationEntry; |
54 |
|
import dk.thoerup.traininfo.WelcomeScreen.ListType; |
55 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
import dk.thoerup.traininfo.provider.ProviderFactory; |
56 |
import dk.thoerup.traininfo.provider.StationProvider; |
import dk.thoerup.traininfo.provider.StationProvider; |
57 |
import dk.thoerup.traininfo.stationmap.GeoPair; |
import dk.thoerup.traininfo.stationmap.GeoPair; |
58 |
import dk.thoerup.traininfo.stationmap.StationMapView; |
import dk.thoerup.traininfo.stationmap.StationMapView; |
59 |
import dk.thoerup.traininfo.util.IntSet; |
import dk.thoerup.traininfo.util.FavoritesHelper; |
60 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
61 |
|
|
62 |
public class StationList extends ListActivity { |
public class StationList extends ListActivity { |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
public static final int OPTIONS_MAP = 2003; |
public static final int OPTIONS_MAP = 103; |
67 |
public static final int OPTIONS_GPSINFO = 2004; |
public static final int OPTIONS_GPSINFO = 104; |
68 |
|
|
69 |
public static final int DLG_PROGRESS = 3001; |
public static final int DLG_PROGRESS = 3001; |
|
public static final int DLG_STATIONNAME = 3002; |
|
70 |
|
|
71 |
|
|
72 |
public static final int GPS_TIMEOUT_MS = 15000; //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 |
83 |
String dialogMessage = ""; |
String dialogMessage = ""; |
84 |
ProgressDialog dialog; |
ProgressDialog dialog; |
85 |
LocationLookup locationLookup = null; |
LocationLookup locationLookup = null; |
|
FindStationsTask findStationsTask; |
|
86 |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
87 |
|
|
88 |
//GeoPair location = new GeoPair(); |
//GeoPair location = new GeoPair(); |
96 |
StationListAdapter adapter = null; |
StationListAdapter adapter = null; |
97 |
|
|
98 |
FavoritesMenu contextMenu = new FavoritesMenu(); |
FavoritesMenu contextMenu = new FavoritesMenu(); |
99 |
IntSet favorites = new IntSet(); |
FavoritesHelper favorites; |
100 |
|
|
101 |
WelcomeScreen.ListType listType; |
WelcomeScreen.ListType listType; |
|
SharedPreferences prefs; |
|
102 |
|
|
103 |
/////////////////////////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////////////////////////// |
104 |
//Activity call backs |
//Activity call backs |
108 |
public void onCreate(Bundle savedInstanceState) { |
public void onCreate(Bundle savedInstanceState) { |
109 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
110 |
setContentView(R.layout.stationlist); |
setContentView(R.layout.stationlist); |
111 |
|
|
112 |
|
favorites = new FavoritesHelper(this); |
113 |
|
|
114 |
|
listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); |
115 |
|
|
116 |
adapter = new StationListAdapter(this); |
adapter = new StationListAdapter(this, (listType == ListType.ListNearest) ); |
117 |
setListAdapter(adapter); |
setListAdapter(adapter); |
118 |
|
|
119 |
ListView lv = getListView(); |
ListView lv = getListView(); |
121 |
|
|
122 |
locationLookup = new LocationLookup(this); |
locationLookup = new LocationLookup(this); |
123 |
|
|
|
|
|
|
prefs = getSharedPreferences("TrainStation", 0); |
|
|
String favoriteString = prefs.getString("favorites", ""); |
|
|
if (! favoriteString.equals("") ) { |
|
|
favorites.fromString(favoriteString); |
|
|
} |
|
124 |
|
|
|
listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); |
|
125 |
setTitle(); |
setTitle(); |
126 |
|
|
127 |
isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false); |
isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false); |
128 |
|
|
129 |
ProviderFactory.purgeOldEntries(); //cleanup before fetching more data |
ProviderFactory.purgeOldEntries(); //cleanup before fetching more data |
130 |
|
|
131 |
|
if (listType == ListType.ListSearch) { |
132 |
|
enableNamesearchField(); |
133 |
|
} |
134 |
|
|
135 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
136 |
|
|
|
|
|
137 |
switch (listType) { |
switch (listType) { |
138 |
case ListNearest: |
case ListNearest: |
139 |
startNearestLookup(); |
startNearestLookup(); |
140 |
break; |
break; |
141 |
case ListSearch: |
case ListSearch: |
142 |
showDialog(DLG_STATIONNAME); |
// do nothing here |
143 |
break; |
break; |
144 |
case ListFavorites: |
case ListFavorites: |
145 |
startFavoriteLookup(); |
startFavoriteLookup(); |
149 |
} |
} |
150 |
|
|
151 |
} else { |
} else { |
152 |
|
((TextView)findViewById(R.id.stationsearch)).setText( savedInstanceState.getString( "search") ); |
153 |
|
|
154 |
stations = (StationBean) savedInstanceState.getSerializable("stations"); |
stations = (StationBean) savedInstanceState.getSerializable("stations"); |
155 |
adapter.setStations(stations); |
adapter.setStations(stations); |
156 |
} |
} |
172 |
if (locationLookup != null) { |
if (locationLookup != null) { |
173 |
locationLookup.stopSearch(); |
locationLookup.stopSearch(); |
174 |
} |
} |
175 |
if (findStationsTask != null) { |
|
|
findStationsTask.cancel(true); |
|
|
} |
|
176 |
} |
} |
177 |
|
|
178 |
|
|
196 |
|
|
197 |
} |
} |
198 |
|
|
199 |
|
|
200 |
|
public void enableNamesearchField() { |
201 |
|
final TextView stationSearch = (TextView) findViewById(R.id.stationsearch); |
202 |
|
stationSearch.setVisibility( View.VISIBLE ); |
203 |
|
stationSearch.addTextChangedListener( new TextWatcher() { |
204 |
|
|
205 |
|
@Override |
206 |
|
public void afterTextChanged(Editable s) { |
207 |
|
if (s.length() > 0) { |
208 |
|
startNameLookup( s.toString() ); |
209 |
|
} else { |
210 |
|
stations = new StationBean(); |
211 |
|
getListView().invalidateViews(); |
212 |
|
adapter.setStations( stations ); |
213 |
|
} |
214 |
|
} |
215 |
|
|
216 |
|
@Override |
217 |
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
218 |
|
} |
219 |
|
|
220 |
|
@Override |
221 |
|
public void onTextChanged(CharSequence s, int start, int before, int count) { |
222 |
|
} |
223 |
|
|
224 |
|
}); |
225 |
|
|
226 |
|
|
227 |
|
} |
228 |
|
|
229 |
|
|
230 |
@Override |
@Override |
233 |
if (dialog != null && dialog.isShowing()) |
if (dialog != null && dialog.isShowing()) |
234 |
dialog.dismiss(); |
dialog.dismiss(); |
235 |
outState.putSerializable("stations", (StationBean) stations); |
outState.putSerializable("stations", (StationBean) stations); |
236 |
|
outState.putString("search", ((TextView)findViewById(R.id.stationsearch)).getText().toString() ); |
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>(); |
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) { |
|
|
startNameLookup(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); |
387 |
} |
} |
388 |
|
|
389 |
void startNameLookup(String name) { |
void startNameLookup(String name) { |
|
dialogMessage = getString( stationlist_findbyname ); |
|
|
showDialog(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.getSize() > 0) { |
399 |
dialogMessage = getString( stationlist_loadfavorites ); |
|
400 |
showDialog(DLG_PROGRESS); |
stations = stationProvider.lookupStationsByIds( favorites.getString() ); |
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); |
getListView().invalidateViews(); |
416 |
|
adapter.setStations( stations ); |
|
findStationsTask = new FindStationsTask(); |
|
|
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
|
|
findStationsTask.execute(); |
|
417 |
} |
} |
418 |
|
|
419 |
|
|
431 |
case GOTLOCATION: |
case GOTLOCATION: |
432 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
433 |
|
|
434 |
startLocatorTask(); |
startNearestLookupPhase2(); |
435 |
|
|
436 |
return; |
return; |
437 |
|
|
450 |
|
|
451 |
|
|
452 |
if (locationLookup.elapsedTime() >= GPS_TIMEOUT_MS) { |
if (locationLookup.elapsedTime() >= GPS_TIMEOUT_MS) { |
453 |
try { |
dismissDialog(DLG_PROGRESS); |
454 |
dismissDialog(DLG_PROGRESS); |
|
|
} catch (IllegalArgumentException ex) { |
|
|
// I get stacktraces that reports an exception is thrown here - |
|
|
// but i can not recreate the situation that causes said exception |
|
|
Log.e("TrainInfo", "Why the f*** is this exception thrown here ?? " + ex.getMessage() ); |
|
|
} |
|
455 |
|
|
456 |
locationLookup.stopSearch(); |
locationLookup.stopSearch(); |
457 |
|
|
458 |
if (locationLookup.hasLocation()) { |
if (locationLookup.hasLocation()) { |
459 |
startLocatorTask(); |
startNearestLookupPhase2(); |
460 |
} else { |
} else { |
461 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
462 |
builder.setMessage( getString( stationlist_gpstimeout) ); |
builder.setMessage( getString( stationlist_gpstimeout) ); |
492 |
|
|
493 |
|
|
494 |
|
|
|
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
|
|
|
|
|
LookupMethod method = LookupMethod.MethodNone; |
|
|
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() { |
|
|
|
|
|
if (method.equals(LookupMethod.MethodNone)) |
|
|
throw new RuntimeException("Method not set"); |
|
|
super.onPreExecute(); |
|
|
} |
|
|
|
|
|
@Override |
|
|
protected Void doInBackground(Void... params) { |
|
|
|
|
|
switch (method) { |
|
|
case ByLocation: |
|
|
stations = stationProvider.lookupStationsByLocation(loc); |
|
|
break; |
|
|
case ByName: |
|
|
stations = stationProvider.lookupStationsByName(name); |
|
|
break; |
|
|
case ByList: |
|
|
stations = stationProvider.lookupStationsByIds(ids); |
|
|
break; |
|
|
default: |
|
|
stations = null; // not possible |
|
|
} |
|
|
|
|
|
|
|
|
return null; |
|
|
} |
|
|
|
|
|
@Override |
|
|
protected void onPostExecute(Void result) { |
|
|
super.onPostExecute(result); |
|
|
dialog.dismiss(); |
|
|
|
|
|
|
|
|
if (stations != null) { |
|
|
if (stations.entries.size() == 0) { |
|
|
showMessageAndClose(getString(stationlist_nostations)); |
|
|
} |
|
|
|
|
|
StationList.this.getListView().invalidateViews(); |
|
|
adapter.setStations( stations ); |
|
|
|
|
|
|
|
|
} else { //communication or parse errors |
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
|
|
builder.setMessage(getString(stationlist_fetcherror)); |
|
|
builder.setCancelable(true); |
|
|
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
|
|
public void onClick(DialogInterface dialog, int id) { |
|
|
dialog.dismiss(); |
|
|
|
|
|
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() { |
|
|
startNameLookup( FindStationsTask.this.name ); |
|
|
} |
|
|
}; |
|
|
break; |
|
|
case ByList: |
|
|
runner = new Runnable() { |
|
|
@Override |
|
|
public void run() { |
|
|
startFavoriteLookup(); |
|
|
} |
|
|
}; |
|
|
break; |
|
|
} |
|
|
|
|
|
stationsFetched.post( runner ); |
|
|
} |
|
|
}); |
|
|
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
|
|
public void onClick(DialogInterface dialog, int id) { |
|
|
dialog.dismiss(); |
|
|
StationList.this.finish(); |
|
|
} |
|
|
}); |
|
|
|
|
|
builder.show(); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
495 |
class FavoritesMenu implements OnCreateContextMenuListener { |
class FavoritesMenu implements OnCreateContextMenuListener { |
496 |
private final static int FAVORITES_ADD = 9001; |
private final static int FAVORITES_ADD = 9001; |
497 |
private final static int FAVORITES_REMOVE = 9002; |
private final static int FAVORITES_REMOVE = 9002; |
506 |
selectedPosition = info.position; |
selectedPosition = info.position; |
507 |
int stationID = stations.entries.get(selectedPosition).getId(); |
int stationID = stations.entries.get(selectedPosition).getId(); |
508 |
|
|
509 |
if (!favorites.contains(stationID)) { |
if (!favorites.hasFavorite(stationID)) { |
510 |
menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) ); |
menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) ); |
511 |
} else { |
} else { |
512 |
menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_removefavorite) ); |
menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_removefavorite) ); |
519 |
|
|
520 |
int stationID = sb.getId(); |
int stationID = sb.getId(); |
521 |
if (item.getItemId() == FAVORITES_ADD) { |
if (item.getItemId() == FAVORITES_ADD) { |
522 |
favorites.add(stationID); |
favorites.addFavorite(stationID); |
523 |
Toast.makeText(StationList.this, getString(stationlist_stationadded), Toast.LENGTH_SHORT).show(); |
Toast.makeText(StationList.this, getString(stationlist_stationadded), Toast.LENGTH_SHORT).show(); |
524 |
} else { |
} else { |
525 |
|
|
526 |
favorites.remove(stationID); |
favorites.removeFavorite(stationID); |
527 |
Toast.makeText(StationList.this, getString(stationlist_stationremoved), Toast.LENGTH_SHORT).show(); |
Toast.makeText(StationList.this, getString(stationlist_stationremoved), Toast.LENGTH_SHORT).show(); |
528 |
|
|
529 |
|
|
532 |
adapter.notifyDataSetChanged(); |
adapter.notifyDataSetChanged(); |
533 |
} |
} |
534 |
} |
} |
|
Editor ed = prefs.edit(); |
|
|
ed.putString("favorites", favorites.toString()); |
|
|
ed.commit(); |
|
535 |
} |
} |
536 |
} |
} |
|
} |
|
537 |
|
} |