3 |
import java.util.ArrayList; |
import java.util.ArrayList; |
4 |
import java.util.List; |
import java.util.List; |
5 |
import java.util.Locale; |
import java.util.Locale; |
6 |
|
import java.util.prefs.Preferences; |
7 |
|
|
8 |
import android.app.AlertDialog; |
import android.app.AlertDialog; |
9 |
import android.app.Dialog; |
import android.app.Dialog; |
11 |
import android.app.ProgressDialog; |
import android.app.ProgressDialog; |
12 |
import android.content.DialogInterface; |
import android.content.DialogInterface; |
13 |
import android.content.Intent; |
import android.content.Intent; |
14 |
|
import android.content.SharedPreferences; |
15 |
import android.location.Address; |
import android.location.Address; |
16 |
import android.location.Geocoder; |
import android.location.Geocoder; |
17 |
import android.location.Location; |
import android.location.Location; |
20 |
import android.os.Handler; |
import android.os.Handler; |
21 |
import android.os.Message; |
import android.os.Message; |
22 |
import android.util.Log; |
import android.util.Log; |
23 |
|
import android.view.ContextMenu; |
24 |
import android.view.LayoutInflater; |
import android.view.LayoutInflater; |
25 |
import android.view.Menu; |
import android.view.Menu; |
26 |
import android.view.MenuItem; |
import android.view.MenuItem; |
27 |
import android.view.View; |
import android.view.View; |
28 |
|
import android.view.ContextMenu.ContextMenuInfo; |
29 |
|
import android.view.View.OnCreateContextMenuListener; |
30 |
|
import android.widget.AdapterView; |
31 |
import android.widget.EditText; |
import android.widget.EditText; |
32 |
import android.widget.ListView; |
import android.widget.ListView; |
33 |
|
import android.widget.Toast; |
34 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
import dk.thoerup.traininfo.provider.ProviderFactory; |
35 |
import dk.thoerup.traininfo.provider.StationProvider; |
import dk.thoerup.traininfo.provider.StationProvider; |
36 |
import dk.thoerup.traininfo.stationmap.GeoPair; |
import dk.thoerup.traininfo.stationmap.GeoPair; |
37 |
import dk.thoerup.traininfo.stationmap.StationMapView; |
import dk.thoerup.traininfo.stationmap.StationMapView; |
38 |
|
import dk.thoerup.traininfo.util.IntSet; |
39 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
40 |
|
|
41 |
public class StationList extends ListActivity { |
public class StationList extends ListActivity { |
48 |
public static final int OPTIONS_NAMESEARCH = 2002; |
public static final int OPTIONS_NAMESEARCH = 2002; |
49 |
public static final int OPTIONS_MAP = 2003; |
public static final int OPTIONS_MAP = 2003; |
50 |
public static final int OPTIONS_ABOUT = 2004; |
public static final int OPTIONS_ABOUT = 2004; |
51 |
|
public static final int OPTIONS_FAVORITES = 2005; |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
public static final int DLG_PROGRESS = 3001; |
public static final int DLG_PROGRESS = 3001; |
71 |
|
|
72 |
StationListAdapter adapter = null; |
StationListAdapter adapter = null; |
73 |
|
|
74 |
|
FavoritesMenu contextMenu = new FavoritesMenu(); |
75 |
|
IntSet favorites = new IntSet(); |
76 |
|
|
77 |
static enum LookupMethod { |
static enum LookupMethod { |
78 |
ByLocation, |
ByLocation, |
79 |
ByName, |
ByName, |
80 |
|
ByList, |
81 |
MethodNone |
MethodNone |
82 |
} |
} |
83 |
|
|
84 |
|
SharedPreferences prefs; |
85 |
|
|
86 |
/////////////////////////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////////////////////////// |
87 |
//Activity call backs |
//Activity call backs |
88 |
|
|
96 |
adapter = new StationListAdapter(this); |
adapter = new StationListAdapter(this); |
97 |
setListAdapter(adapter); |
setListAdapter(adapter); |
98 |
|
|
99 |
|
ListView lv = getListView(); |
100 |
|
lv.setOnCreateContextMenuListener(contextMenu); |
101 |
|
|
102 |
locator = new LocationLookup(this, stationsFetched); |
locator = new LocationLookup(this, stationsFetched); |
103 |
|
|
104 |
|
|
105 |
|
prefs = getSharedPreferences("TrainStation", 0); |
106 |
|
String favoriteString = prefs.getString("favorites", ""); |
107 |
|
if (! favoriteString.equals("") ) { |
108 |
|
favorites.fromString(favoriteString); |
109 |
|
} |
110 |
|
|
111 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
112 |
startLookup(); |
startLookup(); |
113 |
} else { |
} else { |
139 |
item = menu.add(0, OPTIONS_NAMESEARCH, 0, "Search for station"); |
item = menu.add(0, OPTIONS_NAMESEARCH, 0, "Search for station"); |
140 |
item.setIcon(android.R.drawable.ic_menu_search); |
item.setIcon(android.R.drawable.ic_menu_search); |
141 |
|
|
142 |
|
item = menu.add(0, OPTIONS_FAVORITES, 0, "Favorites"); |
143 |
|
item.setIcon(android.R.drawable.ic_menu_agenda); |
144 |
|
|
145 |
item = menu.add(0, OPTIONS_MAP, 0, "Show station map"); |
item = menu.add(0, OPTIONS_MAP, 0, "Show station map"); |
146 |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
147 |
|
|
162 |
case OPTIONS_NAMESEARCH: |
case OPTIONS_NAMESEARCH: |
163 |
showDialog(DLG_STATIONNAME); |
showDialog(DLG_STATIONNAME); |
164 |
break; |
break; |
165 |
|
case OPTIONS_FAVORITES: |
166 |
|
startFavoriteLookup(); |
167 |
|
break; |
168 |
case OPTIONS_MAP: |
case OPTIONS_MAP: |
169 |
|
|
170 |
Intent intent = new Intent(this,StationMapView.class); |
Intent intent = new Intent(this,StationMapView.class); |
199 |
|
|
200 |
return retval; |
return retval; |
201 |
} |
} |
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
@Override |
206 |
|
public boolean onContextItemSelected(MenuItem item) { |
207 |
|
contextMenu.onContextItemSelected(item); |
208 |
|
return true; |
209 |
|
|
210 |
|
|
211 |
|
} |
212 |
|
|
213 |
|
|
214 |
|
|
215 |
|
|
216 |
@Override |
@Override |
217 |
protected Dialog onCreateDialog(int id) { |
protected Dialog onCreateDialog(int id) { |
312 |
locatorTask.execute(); |
locatorTask.execute(); |
313 |
|
|
314 |
} |
} |
315 |
|
|
316 |
|
public void startFavoriteLookup() { |
317 |
|
|
318 |
|
if (favorites.toString().length() > 0) { |
319 |
|
dialogMessage = "Loading favorites"; |
320 |
|
showDialog(DLG_PROGRESS); |
321 |
|
|
322 |
|
locatorTask = new LocatorTask(); |
323 |
|
locatorTask.searchByIds(favorites.toString(), locator.getLocation()); |
324 |
|
locatorTask.execute(); |
325 |
|
} else { |
326 |
|
MessageBox.showMessage(this, "Favorite list is empty"); |
327 |
|
} |
328 |
|
} |
329 |
|
|
330 |
|
|
331 |
|
|
427 |
boolean success; |
boolean success; |
428 |
String name; |
String name; |
429 |
Location loc; |
Location loc; |
430 |
|
String ids; |
431 |
|
|
432 |
public void searchByName(String n, Location l) { |
public void searchByName(String n, Location l) { |
433 |
|
|
441 |
loc = l; |
loc = l; |
442 |
} |
} |
443 |
|
|
444 |
|
public void searchByIds(String id, Location l) { |
445 |
|
|
446 |
|
method = LookupMethod.ByList; |
447 |
|
loc = l; |
448 |
|
ids = id; |
449 |
|
} |
450 |
|
|
451 |
@Override |
@Override |
452 |
protected void onPreExecute() { |
protected void onPreExecute() { |
453 |
|
|
463 |
success = stationProvider.lookupStations(loc); |
success = stationProvider.lookupStations(loc); |
464 |
|
|
465 |
if (method.equals(LookupMethod.ByName)) |
if (method.equals(LookupMethod.ByName)) |
466 |
success = stationProvider.lookupStations(name); |
success = stationProvider.lookupStationsByName(name); |
467 |
|
|
468 |
|
if (method.equals(LookupMethod.ByList)) |
469 |
|
success = stationProvider.lookupStationsByIds(ids); |
470 |
|
|
471 |
Location dummy = new Location("gps"); |
Location dummy = new Location("gps"); |
472 |
List<StationBean> stations = stationProvider.getStations(); |
List<StationBean> stations = stationProvider.getStations(); |
475 |
String addr = lookupAddress(station.getLatitude(), station.getLongitude()); |
String addr = lookupAddress(station.getLatitude(), station.getLongitude()); |
476 |
station.setAddress(addr); |
station.setAddress(addr); |
477 |
|
|
478 |
if (method.equals(LookupMethod.ByName) ) { |
if (method.equals(LookupMethod.ByName) || method.equals(LookupMethod.ByList)) { |
479 |
dummy.setLatitude(station.getLatitude()); |
dummy.setLatitude(station.getLatitude()); |
480 |
dummy.setLongitude(station.getLongitude()); |
dummy.setLongitude(station.getLongitude()); |
481 |
station.setDistance( (int)loc.distanceTo(dummy) ); |
station.setDistance( (int)loc.distanceTo(dummy) ); |
521 |
} |
} |
522 |
} |
} |
523 |
} |
} |
524 |
|
|
525 |
|
|
526 |
|
class FavoritesMenu implements OnCreateContextMenuListener { |
527 |
|
private final static int FAVORITES_ADD = 9001; |
528 |
|
private final static int FAVORITES_REMOVE = 9002; |
529 |
|
|
530 |
|
private int selectedPosition; |
531 |
|
|
532 |
|
|
533 |
|
@Override |
534 |
|
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { |
535 |
|
|
536 |
|
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; |
537 |
|
selectedPosition = info.position; |
538 |
|
int stationID = stations.get(selectedPosition).getId(); |
539 |
|
|
540 |
|
if (!favorites.hasInt(stationID)) { |
541 |
|
menu.add(0, FAVORITES_ADD, 0, "Add to favorites"); |
542 |
|
} else { |
543 |
|
menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites"); |
544 |
|
} |
545 |
|
|
546 |
|
} |
547 |
|
|
548 |
|
public void onContextItemSelected(MenuItem item) { |
549 |
|
StationBean sb = stations.get(selectedPosition); |
550 |
|
|
551 |
|
int stationID = sb.getId(); |
552 |
|
if (item.getItemId() == FAVORITES_ADD) { |
553 |
|
favorites.add(stationID); |
554 |
|
Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show(); |
555 |
|
} else { |
556 |
|
favorites.remove(stationID); |
557 |
|
Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show(); |
558 |
|
} |
559 |
|
|
560 |
|
prefs.edit().putString("favorites", favorites.toString()); |
561 |
|
} |
562 |
|
} |
563 |
} |
} |