44 |
public static final int NOPROVIDER = 1003; |
public static final int NOPROVIDER = 1003; |
45 |
public static final int LOCATIONFIXTIMEOUT = 1004; |
public static final int LOCATIONFIXTIMEOUT = 1004; |
46 |
|
|
|
public static final int OPTIONS_RESCAN = 2001; |
|
|
public static final int OPTIONS_NAMESEARCH = 2002; |
|
47 |
public static final int OPTIONS_MAP = 2003; |
public static final int OPTIONS_MAP = 2003; |
48 |
public static final int OPTIONS_ABOUT = 2004; |
public static final int OPTIONS_GPSINFO = 2004; |
49 |
public static final int OPTIONS_FAVORITES = 2005; |
|
50 |
|
|
51 |
|
|
52 |
|
|
63 |
|
|
64 |
String dialogMessage = ""; |
String dialogMessage = ""; |
65 |
ProgressDialog dialog; |
ProgressDialog dialog; |
66 |
LocationLookup locator = null; |
LocationLookup locationLookup = null; |
67 |
FindStationsTask findStationsTask; |
FindStationsTask findStationsTask; |
68 |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
69 |
|
|
78 |
|
|
79 |
FavoritesMenu contextMenu = new FavoritesMenu(); |
FavoritesMenu contextMenu = new FavoritesMenu(); |
80 |
IntSet favorites = new IntSet(); |
IntSet favorites = new IntSet(); |
81 |
|
|
82 |
boolean showingFavorites = false; |
WelcomeScreen.ListType listType; |
|
String dialogTitle; |
|
83 |
SharedPreferences prefs; |
SharedPreferences prefs; |
84 |
|
|
85 |
/////////////////////////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////////////////////////// |
89 |
@Override |
@Override |
90 |
public void onCreate(Bundle savedInstanceState) { |
public void onCreate(Bundle savedInstanceState) { |
91 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
92 |
setContentView(R.layout.main); |
setContentView(R.layout.stationlist); |
93 |
|
|
94 |
|
|
95 |
adapter = new StationListAdapter(this); |
adapter = new StationListAdapter(this); |
98 |
ListView lv = getListView(); |
ListView lv = getListView(); |
99 |
lv.setOnCreateContextMenuListener(contextMenu); |
lv.setOnCreateContextMenuListener(contextMenu); |
100 |
|
|
101 |
locator = new LocationLookup(this, stationsFetched); |
locationLookup = new LocationLookup(this, stationsFetched); |
102 |
|
|
103 |
|
|
104 |
prefs = getSharedPreferences("TrainStation", 0); |
prefs = getSharedPreferences("TrainStation", 0); |
107 |
favorites.fromString(favoriteString); |
favorites.fromString(favoriteString); |
108 |
} |
} |
109 |
|
|
110 |
|
listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); |
111 |
|
setTitle(); |
112 |
|
|
113 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
114 |
startLookup(); |
|
115 |
|
|
116 |
|
switch (listType) { |
117 |
|
case ListNearest: |
118 |
|
startLookup(); |
119 |
|
break; |
120 |
|
case ListSearch: |
121 |
|
this.showDialog(DLG_STATIONNAME); |
122 |
|
break; |
123 |
|
case ListFavorites: |
124 |
|
startFavoriteLookup(); |
125 |
|
break; |
126 |
|
default: |
127 |
|
// Not possible !?! |
128 |
|
} |
129 |
|
|
130 |
} else { |
} else { |
131 |
stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations"); |
stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations"); |
132 |
adapter.setStations(stations); |
adapter.setStations(stations); |
133 |
location = (GeoPair) savedInstanceState.getSerializable("location"); |
location = (GeoPair) savedInstanceState.getSerializable("location"); |
|
dialogTitle = savedInstanceState.getString("title"); |
|
|
setTitle(dialogTitle); |
|
134 |
} |
} |
135 |
|
|
136 |
} |
} |
137 |
|
protected void setTitle() { |
138 |
|
String dialogTitle = getResources().getString(R.string.app_name); |
139 |
|
switch (listType) { |
140 |
|
case ListNearest: |
141 |
|
dialogTitle += " - Nearby stations"; |
142 |
|
break; |
143 |
|
case ListSearch: |
144 |
|
dialogTitle += " - Search"; |
145 |
|
break; |
146 |
|
case ListFavorites: |
147 |
|
dialogTitle += " - Favorites"; |
148 |
|
break; |
149 |
|
default: |
150 |
|
dialogTitle = "";//not possible |
151 |
|
} |
152 |
|
|
153 |
|
setTitle(dialogTitle); |
154 |
|
} |
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
|
159 |
@Override |
@Override |
163 |
dialog.dismiss(); |
dialog.dismiss(); |
164 |
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
165 |
outState.putSerializable("location", location); |
outState.putSerializable("location", location); |
166 |
outState.putString("title", dialogTitle); |
|
167 |
} |
} |
168 |
|
|
169 |
|
|
171 |
@Override |
@Override |
172 |
public boolean onCreateOptionsMenu(Menu menu) { |
public boolean onCreateOptionsMenu(Menu menu) { |
173 |
MenuItem item; |
MenuItem item; |
174 |
|
|
|
item = menu.add(0, OPTIONS_RESCAN, 0, "Nearest stations"); |
|
|
item.setIcon(android.R.drawable.ic_menu_mylocation); |
|
|
|
|
|
item = menu.add(0, OPTIONS_NAMESEARCH, 0, "Search for station"); |
|
|
item.setIcon(android.R.drawable.ic_menu_search); |
|
|
|
|
|
item = menu.add(0, OPTIONS_FAVORITES, 0, "Favorites"); |
|
|
item.setIcon(android.R.drawable.ic_menu_agenda); |
|
|
|
|
175 |
item = menu.add(0, OPTIONS_MAP, 0, "Station map"); |
item = menu.add(0, OPTIONS_MAP, 0, "Station map"); |
176 |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
177 |
|
|
178 |
item = menu.add(0, OPTIONS_ABOUT, 0, "About"); |
item = menu.add(0, OPTIONS_GPSINFO, 0, "GPS Info"); |
179 |
item.setIcon(android.R.drawable.ic_menu_info_details); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
180 |
|
|
181 |
return true; |
return true; |
182 |
} |
} |
183 |
|
|
185 |
public boolean onOptionsItemSelected(MenuItem item) { |
public boolean onOptionsItemSelected(MenuItem item) { |
186 |
boolean retval = true; |
boolean retval = true; |
187 |
|
|
188 |
|
//TODO: Cleanup |
189 |
switch (item.getItemId()) { |
switch (item.getItemId()) { |
|
case OPTIONS_RESCAN: |
|
|
startLookup(); |
|
|
break; |
|
|
case OPTIONS_NAMESEARCH: |
|
|
showDialog(DLG_STATIONNAME); |
|
|
break; |
|
|
case OPTIONS_FAVORITES: |
|
|
startFavoriteLookup(); |
|
|
break; |
|
190 |
case OPTIONS_MAP: |
case OPTIONS_MAP: |
191 |
|
|
192 |
Intent intent = new Intent(this,StationMapView.class); |
Intent intent = new Intent(this,StationMapView.class); |
201 |
|
|
202 |
startActivity(intent); |
startActivity(intent); |
203 |
break; |
break; |
204 |
case OPTIONS_ABOUT: |
case OPTIONS_GPSINFO: |
205 |
String ver = this.getResources().getString(R.string.app_version); |
Location loc = locationLookup.getLocation(); |
|
|
|
|
Location loc = locator.getLocation(); |
|
206 |
StringBuffer message = new StringBuffer(); |
StringBuffer message = new StringBuffer(); |
|
message.append("TrainInfo DK v").append(ver).append("\n"); |
|
|
message.append("By Torben Nielsen\n"); |
|
|
message.append("\n"); |
|
207 |
message.append("Location info:\n"); |
message.append("Location info:\n"); |
208 |
message.append("-Obtained by: ").append(loc != null ? loc.getProvider() : "-").append("\n"); |
message.append("-Obtained by: ").append(loc != null ? loc.getProvider() : "-").append("\n"); |
209 |
message.append("-Accuracy: ").append(loc != null ? (int)loc.getAccuracy() : "-").append("m\n"); |
message.append("-Accuracy: ").append(loc != null ? (int)loc.getAccuracy() : "-").append("m\n"); |
316 |
dialogMessage = "Wait for location fix"; |
dialogMessage = "Wait for location fix"; |
317 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
318 |
|
|
319 |
locator.locateStations(); |
locationLookup.locateStations(); |
320 |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); |
321 |
} |
} |
322 |
|
|
325 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
326 |
|
|
327 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
328 |
findStationsTask.searchByName(name, locator.getLocation()); |
findStationsTask.searchByName(name, locationLookup.getLocation()); |
329 |
findStationsTask.execute(); |
findStationsTask.execute(); |
330 |
|
|
331 |
} |
} |
337 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
338 |
|
|
339 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
340 |
findStationsTask.searchByIds(favorites.toString(), locator.getLocation()); |
findStationsTask.searchByIds(favorites.toString(), locationLookup.getLocation()); |
341 |
findStationsTask.execute(); |
findStationsTask.execute(); |
342 |
} else { |
} else { |
343 |
MessageBox.showMessage(this, "Favorite list is empty"); |
MessageBox.showMessage(this, "Favorite list is empty"); |
352 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
353 |
|
|
354 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
355 |
findStationsTask.searchByLocation( locator.getLocation() ); |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
356 |
findStationsTask.execute(); |
findStationsTask.execute(); |
357 |
} |
} |
358 |
|
|
396 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
397 |
|
|
398 |
startLocatorTask(); |
startLocatorTask(); |
399 |
location = GeoPair.fromLocation( locator.getLocation() ); |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
400 |
|
|
401 |
break; |
break; |
402 |
|
|
406 |
break; |
break; |
407 |
case LOCATIONFIXTIMEOUT: |
case LOCATIONFIXTIMEOUT: |
408 |
if (isRunning) { |
if (isRunning) { |
409 |
locator.stopSearch(); |
locationLookup.stopSearch(); |
410 |
if (locator.hasLocation()) { |
if (locationLookup.hasLocation()) { |
411 |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
412 |
} else { |
} else { |
413 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
519 |
super.onPostExecute(result); |
super.onPostExecute(result); |
520 |
dialog.dismiss(); |
dialog.dismiss(); |
521 |
|
|
|
//set title |
|
|
switch (method) { |
|
|
case ByLocation: |
|
|
dialogTitle = "Traininfo DK - Nearby stations"; |
|
|
break; |
|
|
case ByName: |
|
|
dialogTitle = "Traininfo DK - Search"; |
|
|
break; |
|
|
case ByList: |
|
|
dialogTitle = "Traininfo DK - Favorites"; |
|
|
break; |
|
|
default: |
|
|
dialogTitle = "";//not possible |
|
|
} |
|
|
|
|
|
StationList.this.setTitle(dialogTitle); |
|
|
//set title end |
|
522 |
|
|
523 |
if (success) { |
if (success) { |
524 |
if (stationProvider.getStations().size() == 0) |
if (stationProvider.getStations().size() == 0) |
525 |
MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! |
MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! |
526 |
stations = stationProvider.getStations(); |
stations = stationProvider.getStations(); |
527 |
adapter.setStations( stations ); |
adapter.setStations( stations ); |
|
|
|
|
showingFavorites = method.equals(LookupMethod.ByList); |
|
528 |
|
|
529 |
} else { //communication or parse errors |
} else { //communication or parse errors |
530 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
587 |
favorites.remove(stationID); |
favorites.remove(stationID); |
588 |
Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show(); |
Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show(); |
589 |
|
|
590 |
if (showingFavorites) { |
|
591 |
|
if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) { |
592 |
stations.remove(selectedPosition); |
stations.remove(selectedPosition); |
593 |
adapter.notifyDataSetChanged(); |
adapter.notifyDataSetChanged(); |
594 |
} |
} |