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.generic_search; |
7 |
|
import static dk.thoerup.traininfo.R.string.stationlist_accuracy; |
8 |
|
import static dk.thoerup.traininfo.R.string.stationlist_addfavorite; |
9 |
|
import static dk.thoerup.traininfo.R.string.stationlist_favorites; |
10 |
|
import static dk.thoerup.traininfo.R.string.stationlist_fetcherror; |
11 |
|
import static dk.thoerup.traininfo.R.string.stationlist_findbyname; |
12 |
|
import static dk.thoerup.traininfo.R.string.stationlist_findingnearby; |
13 |
|
import static dk.thoerup.traininfo.R.string.stationlist_gpsinfo; |
14 |
|
import static dk.thoerup.traininfo.R.string.stationlist_gpstimeout; |
15 |
|
import static dk.thoerup.traininfo.R.string.stationlist_latitude; |
16 |
|
import static dk.thoerup.traininfo.R.string.stationlist_loadfavorites; |
17 |
|
import static dk.thoerup.traininfo.R.string.stationlist_locationinfo; |
18 |
|
import static dk.thoerup.traininfo.R.string.stationlist_longitude; |
19 |
|
import static dk.thoerup.traininfo.R.string.stationlist_nearbystations; |
20 |
|
import static dk.thoerup.traininfo.R.string.stationlist_nofavorites; |
21 |
|
import static dk.thoerup.traininfo.R.string.stationlist_nolocation; |
22 |
|
import static dk.thoerup.traininfo.R.string.stationlist_nolocationprovider; |
23 |
|
import static dk.thoerup.traininfo.R.string.stationlist_nostations; |
24 |
|
import static dk.thoerup.traininfo.R.string.stationlist_obtainedby; |
25 |
|
import static dk.thoerup.traininfo.R.string.stationlist_removefavorite; |
26 |
|
import static dk.thoerup.traininfo.R.string.stationlist_search; |
27 |
|
import static dk.thoerup.traininfo.R.string.stationlist_stationadded; |
28 |
|
import static dk.thoerup.traininfo.R.string.stationlist_stationmap; |
29 |
|
import static dk.thoerup.traininfo.R.string.stationlist_stationremoved; |
30 |
|
import static dk.thoerup.traininfo.R.string.stationlist_stationsearch; |
31 |
|
import static dk.thoerup.traininfo.R.string.stationlist_twocharmin; |
32 |
|
import static dk.thoerup.traininfo.R.string.stationlist_waitforlocation; |
33 |
|
import static dk.thoerup.traininfo.R.string.stationlist_satellitecount; |
34 |
|
|
35 |
|
import java.util.ArrayList; |
36 |
|
|
37 |
import android.app.Activity; |
import android.app.Activity; |
38 |
import android.app.AlertDialog; |
import android.app.AlertDialog; |
48 |
import android.os.Bundle; |
import android.os.Bundle; |
49 |
import android.os.Handler; |
import android.os.Handler; |
50 |
import android.os.Message; |
import android.os.Message; |
|
|
|
51 |
import android.util.Log; |
import android.util.Log; |
52 |
import android.view.ContextMenu; |
import android.view.ContextMenu; |
53 |
import android.view.LayoutInflater; |
import android.view.LayoutInflater; |
60 |
import android.widget.EditText; |
import android.widget.EditText; |
61 |
import android.widget.ListView; |
import android.widget.ListView; |
62 |
import android.widget.Toast; |
import android.widget.Toast; |
63 |
|
import dk.thoerup.android.traininfo.common.StationBean; |
64 |
|
import dk.thoerup.android.traininfo.common.StationBean.StationEntry; |
65 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
import dk.thoerup.traininfo.provider.ProviderFactory; |
66 |
import dk.thoerup.traininfo.provider.StationProvider; |
import dk.thoerup.traininfo.provider.StationProvider; |
67 |
import dk.thoerup.traininfo.stationmap.GeoPair; |
import dk.thoerup.traininfo.stationmap.GeoPair; |
69 |
import dk.thoerup.traininfo.util.IntSet; |
import dk.thoerup.traininfo.util.IntSet; |
70 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
71 |
|
|
|
import static dk.thoerup.traininfo.R.string.*; |
|
|
|
|
72 |
public class StationList extends ListActivity { |
public class StationList extends ListActivity { |
73 |
public static final int GOTLOCATION = 1001; |
public enum LookupStates { |
74 |
public static final int GOTSTATIONLIST = 1002; |
GOTLOCATION, |
75 |
public static final int NOPROVIDER = 1003; |
GOTSTATIONLIST, |
76 |
public static final int LOCATIONFIXTIMEOUT = 1004; |
NOPROVIDER, |
77 |
|
LOCATIONFIXTIMEOUT, |
78 |
|
GPS_SAT_COUNT |
79 |
|
} |
80 |
|
|
81 |
|
|
82 |
public static final int OPTIONS_MAP = 2003; |
public static final int OPTIONS_MAP = 2003; |
83 |
public static final int OPTIONS_GPSINFO = 2004; |
public static final int OPTIONS_GPSINFO = 2004; |
|
|
|
|
|
|
|
|
|
84 |
|
|
85 |
public static final int DLG_PROGRESS = 3001; |
public static final int DLG_PROGRESS = 3001; |
86 |
public static final int DLG_STATIONNAME = 3002; |
public static final int DLG_STATIONNAME = 3002; |
87 |
|
|
88 |
|
|
89 |
|
public static final int GPS_TIMEOUT_MS = 15000; //how long are we willing to wait for gps fix -in milliseconds |
90 |
|
|
91 |
|
|
92 |
static enum LookupMethod { |
static enum LookupMethod { |
93 |
ByLocation, |
ByLocation, |
94 |
ByName, |
ByName, |
107 |
|
|
108 |
boolean isLaunchedforShortcut; |
boolean isLaunchedforShortcut; |
109 |
boolean isRunning = false; |
boolean isRunning = false; |
110 |
List<StationBean> stations = new ArrayList<StationBean>(); |
StationBean stations = new StationBean(); |
111 |
|
|
112 |
StationProvider stationProvider = ProviderFactory.getStationProvider(); |
StationProvider stationProvider = ProviderFactory.getStationProvider(); |
113 |
|
|
122 |
/////////////////////////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////////////////////////// |
123 |
//Activity call backs |
//Activity call backs |
124 |
|
|
125 |
@SuppressWarnings("unchecked") |
|
126 |
@Override |
@Override |
127 |
public void onCreate(Bundle savedInstanceState) { |
public void onCreate(Bundle savedInstanceState) { |
128 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
149 |
|
|
150 |
isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false); |
isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false); |
151 |
|
|
152 |
|
ProviderFactory.purgeOldEntries(); //cleanup before fetching more data |
153 |
|
|
154 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
155 |
|
|
156 |
|
|
158 |
case ListNearest: |
case ListNearest: |
159 |
startLookup(); |
startLookup(); |
160 |
break; |
break; |
161 |
case ListSearch: |
case ListSearch: |
162 |
this.showDialogSafe(DLG_STATIONNAME); |
showDialog(DLG_STATIONNAME); |
163 |
break; |
break; |
164 |
case ListFavorites: |
case ListFavorites: |
165 |
startFavoriteLookup(); |
startFavoriteLookup(); |
169 |
} |
} |
170 |
|
|
171 |
} else { |
} else { |
172 |
stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations"); |
stations = (StationBean) savedInstanceState.getSerializable("stations"); |
173 |
adapter.setStations(stations); |
adapter.setStations(stations); |
174 |
location = (GeoPair) savedInstanceState.getSerializable("location"); |
location = (GeoPair) savedInstanceState.getSerializable("location"); |
175 |
} |
} |
176 |
|
|
177 |
} |
} |
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
|
183 |
|
|
184 |
@Override |
@Override |
185 |
protected void onDestroy() { |
protected void onDestroy() { |
186 |
super.onDestroy(); |
super.onDestroy(); |
187 |
|
|
188 |
if (findStationsTask != null) { |
isRunning = false; |
189 |
findStationsTask.cancel(true); |
|
|
} |
|
190 |
if (locationLookup != null) { |
if (locationLookup != null) { |
191 |
locationLookup.stopSearch(); |
locationLookup.stopSearch(); |
192 |
} |
} |
193 |
isRunning = false; |
if (findStationsTask != null) { |
194 |
|
findStationsTask.cancel(true); |
195 |
|
} |
196 |
} |
} |
197 |
|
|
198 |
|
|
216 |
|
|
217 |
} |
} |
218 |
|
|
|
|
|
|
/* these 3 dialogs helper functions are very rude and ugly hack |
|
|
* to remove these auto-reported exceptions |
|
|
* - android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@436aaef8 is not valid; is your activity running? |
|
|
* - java.lang.IllegalArgumentException: View not attached to window manager |
|
|
*/ |
|
|
|
|
|
public void showDialogSafe(int id) { |
|
|
try { |
|
|
showDialog(id); |
|
|
} catch (Exception e) { |
|
|
Log.e("StationList", "showDialog failed", e); |
|
|
} |
|
|
} |
|
|
|
|
|
public void dismissDialogSafe(int id) { |
|
|
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); |
|
|
} |
|
|
} |
|
|
|
|
|
public void builderShowSafe(AlertDialog.Builder builder) { |
|
|
try { |
|
|
builder.show(); |
|
|
} catch (Exception e) { |
|
|
Log.e("StationList", "builder.show() failed", e); |
|
|
} |
|
219 |
|
|
|
} |
|
|
/* EOF rude and ugly dialog hack */ |
|
|
|
|
|
|
|
220 |
|
|
221 |
@Override |
@Override |
222 |
public void onSaveInstanceState(Bundle outState) |
public void onSaveInstanceState(Bundle outState) |
223 |
{ |
{ |
224 |
if (dialog != null && dialog.isShowing()) |
if (dialog != null && dialog.isShowing()) |
225 |
dialog.dismiss(); |
dialog.dismiss(); |
226 |
outState.putSerializable("stations", (ArrayList<StationBean>) stations); |
outState.putSerializable("stations", (StationBean) stations); |
227 |
outState.putSerializable("location", location); |
outState.putSerializable("location", location); |
228 |
|
|
229 |
} |
} |
254 |
Intent intent = new Intent(this,StationMapView.class); |
Intent intent = new Intent(this,StationMapView.class); |
255 |
|
|
256 |
ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>(); |
ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>(); |
257 |
for (StationBean st : stations ) { |
for (StationEntry st : stations.entries ) { |
258 |
stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) ); |
stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) ); |
259 |
} |
} |
260 |
|
|
373 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
374 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
375 |
|
|
376 |
StationBean station = stations.get(position); |
StationEntry station = stations.entries.get(position); |
377 |
|
|
378 |
if (isLaunchedforShortcut == true) { |
if (isLaunchedforShortcut == true) { |
379 |
Intent i = new Intent(); |
Intent i = new Intent(); |
393 |
public void startLookup() { |
public void startLookup() { |
394 |
isRunning = true; |
isRunning = true; |
395 |
dialogMessage = getString( stationlist_waitforlocation ); |
dialogMessage = getString( stationlist_waitforlocation ); |
396 |
showDialogSafe(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
397 |
|
|
398 |
locationLookup.locateStations(); |
locationLookup.locateStations(); |
399 |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); |
stationsFetched.sendEmptyMessageDelayed(LookupStates.LOCATIONFIXTIMEOUT.ordinal(), GPS_TIMEOUT_MS); |
400 |
} |
} |
401 |
|
|
402 |
void startNameSearch(String name) { |
void startNameSearch(String name) { |
403 |
dialogMessage = getString( stationlist_findbyname ); |
dialogMessage = getString( stationlist_findbyname ); |
404 |
showDialogSafe(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
405 |
|
|
406 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
407 |
findStationsTask.searchByName(name); |
findStationsTask.searchByName(name); |
413 |
|
|
414 |
if (favorites.size() > 0) { |
if (favorites.size() > 0) { |
415 |
dialogMessage = getString( stationlist_loadfavorites ); |
dialogMessage = getString( stationlist_loadfavorites ); |
416 |
showDialogSafe(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
417 |
|
|
418 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
419 |
findStationsTask.searchByIds( favorites.toString() ); |
findStationsTask.searchByIds( favorites.toString() ); |
428 |
void startLocatorTask() |
void startLocatorTask() |
429 |
{ |
{ |
430 |
dialogMessage = getString( stationlist_findingnearby ); |
dialogMessage = getString( stationlist_findingnearby ); |
431 |
showDialogSafe(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
432 |
|
|
433 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
434 |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
442 |
class StationsFetchedHandler extends Handler { |
class StationsFetchedHandler extends Handler { |
443 |
@Override |
@Override |
444 |
public void handleMessage(Message msg) { |
public void handleMessage(Message msg) { |
445 |
|
Log.e("Handler", "WHAT:" + msg.what); |
446 |
|
|
447 |
|
LookupStates state = LookupStates.values()[msg.what]; |
448 |
|
|
449 |
switch (msg.what) { |
switch (state) { |
450 |
case GOTLOCATION: |
case GOTLOCATION: |
451 |
dismissDialogSafe(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
452 |
|
|
453 |
startLocatorTask(); |
startLocatorTask(); |
454 |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
456 |
break; |
break; |
457 |
|
|
458 |
case NOPROVIDER: |
case NOPROVIDER: |
459 |
dismissDialogSafe(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
460 |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); |
461 |
//StationList.this.finish(); |
//StationList.this.finish(); |
462 |
break; |
break; |
463 |
|
case GPS_SAT_COUNT: |
464 |
|
int count = msg.arg1; |
465 |
|
String dialogMessage = getString( stationlist_waitforlocation ) + "\n" + getString( stationlist_satellitecount ) + ": " + count; |
466 |
|
dialog.setMessage( dialogMessage ); |
467 |
|
return; //abort immediately |
468 |
case LOCATIONFIXTIMEOUT: |
case LOCATIONFIXTIMEOUT: |
469 |
if (isRunning) { |
if (isRunning) { |
470 |
locationLookup.stopSearch(); |
locationLookup.stopSearch(); |
471 |
if (locationLookup.hasLocation()) { |
if (locationLookup.hasLocation()) { |
472 |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
stationsFetched.sendEmptyMessage( LookupStates.GOTLOCATION.ordinal() ); |
473 |
} else { |
} else { |
474 |
dismissDialogSafe(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
475 |
|
|
476 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
477 |
builder.setMessage( getString( stationlist_gpstimeout) ); |
builder.setMessage( getString( stationlist_gpstimeout) ); |
488 |
dialog.dismiss(); |
dialog.dismiss(); |
489 |
} |
} |
490 |
}); |
}); |
491 |
builderShowSafe(builder); // builder.show() |
builder.show(); |
492 |
|
|
493 |
} |
} |
494 |
} |
} |
502 |
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
class FindStationsTask extends AsyncTask<Void,Void,Void> { |
503 |
|
|
504 |
LookupMethod method = LookupMethod.MethodNone; |
LookupMethod method = LookupMethod.MethodNone; |
|
boolean success; |
|
505 |
String name; |
String name; |
506 |
Location loc; |
Location loc; |
507 |
String ids; |
String ids; |
536 |
|
|
537 |
switch (method) { |
switch (method) { |
538 |
case ByLocation: |
case ByLocation: |
539 |
success = stationProvider.lookupStations(loc); |
stations = stationProvider.lookupStations(loc); |
540 |
break; |
break; |
541 |
case ByName: |
case ByName: |
542 |
success = stationProvider.lookupStationsByName(name); |
stations = stationProvider.lookupStationsByName(name); |
543 |
break; |
break; |
544 |
case ByList: |
case ByList: |
545 |
success = stationProvider.lookupStationsByIds(ids); |
stations = stationProvider.lookupStationsByIds(ids); |
546 |
break; |
break; |
547 |
default: |
default: |
548 |
success = false; // not possible |
stations = null; // not possible |
549 |
} |
} |
550 |
|
|
551 |
|
|
555 |
@Override |
@Override |
556 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
557 |
super.onPostExecute(result); |
super.onPostExecute(result); |
558 |
dismissDialogSafe(dialog); |
dialog.dismiss(); |
559 |
|
|
560 |
|
|
561 |
if (success) { |
if (stations != null) { |
562 |
if (stationProvider.getStations().size() == 0) { |
if (stations.entries.size() == 0) { |
563 |
showMessageAndClose(getString(stationlist_nostations)); |
showMessageAndClose(getString(stationlist_nostations)); |
564 |
} |
} |
|
stations = stationProvider.getStations(); |
|
565 |
|
|
566 |
StationList.this.getListView().invalidateViews(); |
StationList.this.getListView().invalidateViews(); |
567 |
adapter.setStations( stations ); |
adapter.setStations( stations ); |
613 |
} |
} |
614 |
}); |
}); |
615 |
|
|
616 |
builderShowSafe(builder); // builder.show() |
builder.show(); |
617 |
} |
} |
618 |
} |
} |
619 |
} |
} |
631 |
|
|
632 |
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; |
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; |
633 |
selectedPosition = info.position; |
selectedPosition = info.position; |
634 |
int stationID = stations.get(selectedPosition).getId(); |
int stationID = stations.entries.get(selectedPosition).getId(); |
635 |
|
|
636 |
if (!favorites.contains(stationID)) { |
if (!favorites.contains(stationID)) { |
637 |
menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) ); |
menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) ); |
642 |
} |
} |
643 |
|
|
644 |
public void onContextItemSelected(MenuItem item) { |
public void onContextItemSelected(MenuItem item) { |
645 |
StationBean sb = stations.get(selectedPosition); |
StationEntry sb = stations.entries.get(selectedPosition); |
646 |
|
|
647 |
int stationID = sb.getId(); |
int stationID = sb.getId(); |
648 |
if (item.getItemId() == FAVORITES_ADD) { |
if (item.getItemId() == FAVORITES_ADD) { |
655 |
|
|
656 |
|
|
657 |
if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) { |
if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) { |
658 |
stations.remove(selectedPosition); |
stations.entries.remove(selectedPosition); |
659 |
adapter.notifyDataSetChanged(); |
adapter.notifyDataSetChanged(); |
660 |
} |
} |
661 |
} |
} |