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; |
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; |
import android.util.Log; |
44 |
import android.view.ContextMenu; |
import android.view.ContextMenu; |
45 |
import android.view.ContextMenu.ContextMenuInfo; |
import android.view.ContextMenu.ContextMenuInfo; |
|
import android.view.LayoutInflater; |
|
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; |
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; |
import dk.thoerup.android.traininfo.common.StationBean; |
55 |
import dk.thoerup.android.traininfo.common.StationEntry; |
import dk.thoerup.android.traininfo.common.StationEntry; |
56 |
|
import dk.thoerup.traininfo.WelcomeScreen.ListType; |
57 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
import dk.thoerup.traininfo.provider.ProviderFactory; |
58 |
import dk.thoerup.traininfo.provider.StationProvider; |
import dk.thoerup.traininfo.provider.StationProvider; |
59 |
import dk.thoerup.traininfo.stationmap.GeoPair; |
import dk.thoerup.traininfo.stationmap.GeoPair; |
69 |
public static final int OPTIONS_GPSINFO = 104; |
public static final int OPTIONS_GPSINFO = 104; |
70 |
|
|
71 |
public static final int DLG_PROGRESS = 3001; |
public static final int DLG_PROGRESS = 3001; |
|
public static final int DLG_STATIONNAME = 3002; |
|
72 |
|
|
73 |
|
|
74 |
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 |
85 |
String dialogMessage = ""; |
String dialogMessage = ""; |
86 |
ProgressDialog dialog; |
ProgressDialog dialog; |
87 |
LocationLookup locationLookup = null; |
LocationLookup locationLookup = null; |
|
FindStationsTask findStationsTask; |
|
88 |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
89 |
|
|
90 |
//GeoPair location = new GeoPair(); |
//GeoPair location = new GeoPair(); |
112 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
113 |
setContentView(R.layout.stationlist); |
setContentView(R.layout.stationlist); |
114 |
|
|
115 |
|
listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); |
116 |
|
|
117 |
adapter = new StationListAdapter(this); |
adapter = new StationListAdapter(this, (listType == ListType.ListNearest) ); |
118 |
setListAdapter(adapter); |
setListAdapter(adapter); |
119 |
|
|
120 |
ListView lv = getListView(); |
ListView lv = getListView(); |
129 |
favorites.fromString(favoriteString); |
favorites.fromString(favoriteString); |
130 |
} |
} |
131 |
|
|
|
listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); |
|
132 |
setTitle(); |
setTitle(); |
133 |
|
|
134 |
isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false); |
isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false); |
135 |
|
|
136 |
ProviderFactory.purgeOldEntries(); //cleanup before fetching more data |
ProviderFactory.purgeOldEntries(); //cleanup before fetching more data |
137 |
|
|
138 |
|
if (listType == ListType.ListSearch) { |
139 |
|
enableNamesearchField(); |
140 |
|
} |
141 |
|
|
142 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
143 |
|
|
|
|
|
144 |
switch (listType) { |
switch (listType) { |
145 |
case ListNearest: |
case ListNearest: |
146 |
startNearestLookup(); |
startNearestLookup(); |
147 |
break; |
break; |
148 |
case ListSearch: |
case ListSearch: |
149 |
showDialog(DLG_STATIONNAME); |
// do nothing here |
150 |
break; |
break; |
151 |
case ListFavorites: |
case ListFavorites: |
152 |
startFavoriteLookup(); |
startFavoriteLookup(); |
177 |
if (locationLookup != null) { |
if (locationLookup != null) { |
178 |
locationLookup.stopSearch(); |
locationLookup.stopSearch(); |
179 |
} |
} |
180 |
if (findStationsTask != null) { |
|
|
findStationsTask.cancel(true); |
|
|
} |
|
181 |
} |
} |
182 |
|
|
183 |
|
|
201 |
|
|
202 |
} |
} |
203 |
|
|
204 |
|
|
205 |
|
public void enableNamesearchField() { |
206 |
|
final TextView stationSearch = (TextView) findViewById(R.id.stationsearch); |
207 |
|
stationSearch.setVisibility( View.VISIBLE ); |
208 |
|
stationSearch.addTextChangedListener( new TextWatcher() { |
209 |
|
|
210 |
|
@Override |
211 |
|
public void afterTextChanged(Editable s) { |
212 |
|
if (s.length() > 0) { |
213 |
|
startNameLookup( s.toString() ); |
214 |
|
} else { |
215 |
|
stations = new StationBean(); |
216 |
|
getListView().invalidateViews(); |
217 |
|
adapter.setStations( stations ); |
218 |
|
} |
219 |
|
} |
220 |
|
|
221 |
|
@Override |
222 |
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
223 |
|
} |
224 |
|
|
225 |
|
@Override |
226 |
|
public void onTextChanged(CharSequence s, int start, int before, int count) { |
227 |
|
} |
228 |
|
|
229 |
|
}); |
230 |
|
|
231 |
|
|
232 |
|
} |
233 |
|
|
234 |
|
|
235 |
@Override |
@Override |
270 |
case OPTIONS_MAP: |
case OPTIONS_MAP: |
271 |
|
|
272 |
if ( stations == null || stations.entries == null || stations.entries.size() == 0 ) { |
if ( stations == null || stations.entries == null || stations.entries.size() == 0 ) { |
273 |
Toast.makeText(this, "No stations to show on map", Toast.LENGTH_SHORT); //TODO: Translate |
Toast.makeText(this, "No stations to show on map", Toast.LENGTH_SHORT).show(); //TODO: Translate |
274 |
return true; |
return true; |
275 |
} |
} |
276 |
|
|
339 |
dlg.setMessage( getString(stationlist_waitforlocation) ); |
dlg.setMessage( getString(stationlist_waitforlocation) ); |
340 |
dlg.setCancelable(false); |
dlg.setCancelable(false); |
341 |
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(); |
|
342 |
|
|
343 |
default: |
default: |
344 |
return super.onCreateDialog(id); |
return super.onCreateDialog(id); |
391 |
} |
} |
392 |
|
|
393 |
void startNameLookup(String name) { |
void startNameLookup(String name) { |
|
dialogMessage = getString( stationlist_findbyname ); |
|
|
showDialog(DLG_PROGRESS); |
|
|
|
|
|
findStationsTask = new FindStationsTask(); |
|
|
findStationsTask.searchByName(name); |
|
|
findStationsTask.execute(); |
|
394 |
|
|
395 |
|
stations = stationProvider.lookupStationsByName(name); |
396 |
|
getListView().invalidateViews(); |
397 |
|
adapter.setStations( stations ); |
398 |
} |
} |
399 |
|
|
400 |
public void startFavoriteLookup() { |
public void startFavoriteLookup() { |
401 |
|
|
402 |
if (favorites.size() > 0) { |
if (favorites.size() > 0) { |
403 |
dialogMessage = getString( stationlist_loadfavorites ); |
|
404 |
showDialog(DLG_PROGRESS); |
stations = stationProvider.lookupStationsByIds( favorites.toString() ); |
405 |
|
getListView().invalidateViews(); |
406 |
findStationsTask = new FindStationsTask(); |
adapter.setStations( stations ); |
407 |
findStationsTask.searchByIds( favorites.toString() ); |
|
408 |
findStationsTask.execute(); |
|
409 |
} else { |
} else { |
410 |
showMessageAndClose( getString( stationlist_nofavorites ) ); |
showMessageAndClose( getString( stationlist_nofavorites ) ); |
411 |
} |
} |
413 |
|
|
414 |
|
|
415 |
|
|
416 |
void startLocatorTask() |
void startNearestLookupPhase2() |
417 |
{ |
{ |
418 |
dialogMessage = getString( stationlist_findingnearby ); |
stations = stationProvider.lookupStationsByLocation( locationLookup.getLocation() ); |
419 |
showDialog(DLG_PROGRESS); |
getListView().invalidateViews(); |
420 |
|
adapter.setStations( stations ); |
|
findStationsTask = new FindStationsTask(); |
|
|
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
|
|
findStationsTask.execute(); |
|
421 |
} |
} |
422 |
|
|
423 |
|
|
435 |
case GOTLOCATION: |
case GOTLOCATION: |
436 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
437 |
|
|
438 |
startLocatorTask(); |
startNearestLookupPhase2(); |
439 |
|
|
440 |
return; |
return; |
441 |
|
|
460 |
locationLookup.stopSearch(); |
locationLookup.stopSearch(); |
461 |
|
|
462 |
if (locationLookup.hasLocation()) { |
if (locationLookup.hasLocation()) { |
463 |
startLocatorTask(); |
startNearestLookupPhase2(); |
464 |
} else { |
} else { |
465 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
466 |
builder.setMessage( getString( stationlist_gpstimeout) ); |
builder.setMessage( getString( stationlist_gpstimeout) ); |
496 |
|
|
497 |
|
|
498 |
|
|
|
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(); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
499 |
class FavoritesMenu implements OnCreateContextMenuListener { |
class FavoritesMenu implements OnCreateContextMenuListener { |
500 |
private final static int FAVORITES_ADD = 9001; |
private final static int FAVORITES_ADD = 9001; |
501 |
private final static int FAVORITES_REMOVE = 9002; |
private final static int FAVORITES_REMOVE = 9002; |