4 |
import java.util.List; |
import java.util.List; |
5 |
|
|
6 |
|
|
7 |
|
import android.app.Activity; |
8 |
import android.app.AlertDialog; |
import android.app.AlertDialog; |
9 |
import android.app.Dialog; |
import android.app.Dialog; |
10 |
import android.app.ListActivity; |
import android.app.ListActivity; |
48 |
|
|
49 |
public static final int OPTIONS_MAP = 2003; |
public static final int OPTIONS_MAP = 2003; |
50 |
public static final int OPTIONS_GPSINFO = 2004; |
public static final int OPTIONS_GPSINFO = 2004; |
|
|
|
|
|
|
|
|
|
51 |
|
|
52 |
public static final int DLG_PROGRESS = 3001; |
public static final int DLG_PROGRESS = 3001; |
53 |
public static final int DLG_STATIONNAME = 3002; |
public static final int DLG_STATIONNAME = 3002; |
54 |
|
|
55 |
|
|
56 |
|
public static final int GPS_TIMEOUT_MS = 17500; //how long are we willing to wait for gps fix -in milliseconds |
57 |
|
|
58 |
|
|
59 |
static enum LookupMethod { |
static enum LookupMethod { |
60 |
ByLocation, |
ByLocation, |
61 |
ByName, |
ByName, |
71 |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
72 |
|
|
73 |
GeoPair location = new GeoPair(); |
GeoPair location = new GeoPair(); |
74 |
|
|
75 |
|
boolean isLaunchedforShortcut; |
76 |
boolean isRunning = false; |
boolean isRunning = false; |
77 |
List<StationBean> stations = new ArrayList<StationBean>(); |
List<StationBean> stations = new ArrayList<StationBean>(); |
78 |
|
|
114 |
listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); |
listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); |
115 |
setTitle(); |
setTitle(); |
116 |
|
|
117 |
|
isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false); |
118 |
|
|
119 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
120 |
|
|
121 |
|
|
124 |
startLookup(); |
startLookup(); |
125 |
break; |
break; |
126 |
case ListSearch: |
case ListSearch: |
127 |
this.showDialog(DLG_STATIONNAME); |
this.showDialogSafe(DLG_STATIONNAME); |
128 |
break; |
break; |
129 |
case ListFavorites: |
case ListFavorites: |
130 |
startFavoriteLookup(); |
startFavoriteLookup(); |
140 |
} |
} |
141 |
|
|
142 |
} |
} |
143 |
|
|
144 |
|
|
145 |
|
@Override |
146 |
|
protected void onDestroy() { |
147 |
|
super.onDestroy(); |
148 |
|
|
149 |
|
if (findStationsTask != null) { |
150 |
|
findStationsTask.cancel(true); |
151 |
|
} |
152 |
|
if (locationLookup != null) { |
153 |
|
locationLookup.stopSearch(); |
154 |
|
} |
155 |
|
isRunning = false; |
156 |
|
} |
157 |
|
|
158 |
|
|
159 |
protected void setTitle() { |
protected void setTitle() { |
160 |
String dialogTitle = getResources().getString(app_name); |
String dialogTitle = getResources().getString(app_name); |
161 |
switch (listType) { |
switch (listType) { |
173 |
} |
} |
174 |
|
|
175 |
setTitle(dialogTitle); |
setTitle(dialogTitle); |
176 |
|
|
177 |
} |
} |
178 |
|
|
179 |
|
|
180 |
|
/* these 3 dialogs helper functions are very rude and ugly hack |
181 |
|
* to remove these auto-reported exceptions |
182 |
|
* - android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@436aaef8 is not valid; is your activity running? |
183 |
|
* - java.lang.IllegalArgumentException: View not attached to window manager |
184 |
|
*/ |
185 |
|
|
186 |
|
public void showDialogSafe(int id) { |
187 |
|
try { |
188 |
|
showDialog(id); |
189 |
|
} catch (Exception e) { |
190 |
|
Log.e("StationList", "showDialog failed", e); |
191 |
|
} |
192 |
|
} |
193 |
|
|
194 |
|
public void dismissDialogSafe(int id) { |
195 |
|
try { |
196 |
|
dismissDialog(id); |
197 |
|
} catch (Exception e) { |
198 |
|
Log.e("StationList", "dismissDialog failed", e); |
199 |
|
} |
200 |
|
} |
201 |
|
public void dismissDialogSafe(Dialog dlg) { |
202 |
|
try { |
203 |
|
dlg.dismiss(); |
204 |
|
} catch (Exception e) { |
205 |
|
Log.e("StationList", "dismissDialog failed", e); |
206 |
|
} |
207 |
|
} |
208 |
|
|
209 |
|
public void builderShowSafe(AlertDialog.Builder builder) { |
210 |
|
try { |
211 |
|
builder.show(); |
212 |
|
} catch (Exception e) { |
213 |
|
Log.e("StationList", "builder.show() failed", e); |
214 |
|
} |
215 |
|
|
216 |
|
} |
217 |
|
/* EOF rude and ugly dialog hack */ |
218 |
|
|
219 |
|
|
220 |
|
|
221 |
@Override |
@Override |
275 |
message.append( getString(stationlist_nolocation) ); |
message.append( getString(stationlist_nolocation) ); |
276 |
} |
} |
277 |
|
|
278 |
MessageBox.showMessage(this, message.toString()); |
MessageBox.showMessage(this, message.toString(), false); |
279 |
break; |
break; |
280 |
default: |
default: |
281 |
retval = super.onOptionsItemSelected(item); |
retval = super.onOptionsItemSelected(item); |
332 |
public void onClick(DialogInterface dialog, int which) { |
public void onClick(DialogInterface dialog, int which) { |
333 |
EditText et = (EditText) rootView.findViewById(R.id.EditText); |
EditText et = (EditText) rootView.findViewById(R.id.EditText); |
334 |
dialog.dismiss(); |
dialog.dismiss(); |
335 |
if (et.getText().toString().length() >= 2) { |
String search = et.getText().toString().trim(); |
336 |
startNameSearch(et.getText().toString()); |
if (search.length() >= 2) { |
337 |
|
startNameSearch(search); |
338 |
} else { |
} else { |
339 |
showMessageAndClose( getString(stationlist_twocharmin) ); |
showMessageAndClose( getString(stationlist_twocharmin) ); |
340 |
} |
} |
375 |
|
|
376 |
StationBean station = stations.get(position); |
StationBean station = stations.get(position); |
377 |
|
|
378 |
Intent intent = new Intent(this, DepartureList.class); |
if (isLaunchedforShortcut == true) { |
379 |
intent.putExtra("stationbean", station); |
Intent i = new Intent(); |
380 |
startActivity(intent); |
i.putExtra("station", station); |
381 |
|
setResult(Activity.RESULT_OK, i); |
382 |
|
finish(); |
383 |
|
} else { |
384 |
|
Intent intent = new Intent(this, DepartureList.class); |
385 |
|
intent.putExtra("stationbean", station); |
386 |
|
startActivity(intent); |
387 |
|
} |
388 |
} |
} |
389 |
|
|
390 |
///////////////////////////////////////////////////////////// |
///////////////////////////////////////////////////////////// |
393 |
public void startLookup() { |
public void startLookup() { |
394 |
isRunning = true; |
isRunning = true; |
395 |
dialogMessage = getString( stationlist_waitforlocation ); |
dialogMessage = getString( stationlist_waitforlocation ); |
396 |
showDialog(DLG_PROGRESS); |
showDialogSafe(DLG_PROGRESS); |
397 |
|
|
398 |
locationLookup.locateStations(); |
locationLookup.locateStations(); |
399 |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS); |
400 |
} |
} |
401 |
|
|
402 |
void startNameSearch(String name) { |
void startNameSearch(String name) { |
403 |
dialogMessage = getString( stationlist_findbyname ); |
dialogMessage = getString( stationlist_findbyname ); |
404 |
showDialog(DLG_PROGRESS); |
showDialogSafe(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 |
showDialog(DLG_PROGRESS); |
showDialogSafe(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 |
showDialog(DLG_PROGRESS); |
showDialogSafe(DLG_PROGRESS); |
432 |
|
|
433 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
434 |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
445 |
|
|
446 |
switch (msg.what) { |
switch (msg.what) { |
447 |
case GOTLOCATION: |
case GOTLOCATION: |
448 |
dismissDialog(DLG_PROGRESS); |
dismissDialogSafe(DLG_PROGRESS); |
449 |
|
|
450 |
startLocatorTask(); |
startLocatorTask(); |
451 |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
453 |
break; |
break; |
454 |
|
|
455 |
case NOPROVIDER: |
case NOPROVIDER: |
456 |
dismissDialog(DLG_PROGRESS); |
dismissDialogSafe(DLG_PROGRESS); |
457 |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) ); |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); |
458 |
|
//StationList.this.finish(); |
459 |
break; |
break; |
460 |
case LOCATIONFIXTIMEOUT: |
case LOCATIONFIXTIMEOUT: |
461 |
if (isRunning) { |
if (isRunning) { |
463 |
if (locationLookup.hasLocation()) { |
if (locationLookup.hasLocation()) { |
464 |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
465 |
} else { |
} else { |
466 |
dismissDialog(DLG_PROGRESS); |
dismissDialogSafe(DLG_PROGRESS); |
467 |
|
|
468 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
469 |
builder.setMessage( getString( stationlist_gpstimeout) ); |
builder.setMessage( getString( stationlist_gpstimeout) ); |
479 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
480 |
dialog.dismiss(); |
dialog.dismiss(); |
481 |
} |
} |
482 |
}); |
}); |
483 |
builder.show(); |
builderShowSafe(builder); // builder.show() |
484 |
|
|
485 |
} |
} |
486 |
} |
} |
548 |
@Override |
@Override |
549 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
550 |
super.onPostExecute(result); |
super.onPostExecute(result); |
551 |
dialog.dismiss(); |
dismissDialogSafe(dialog); |
552 |
|
|
553 |
|
|
554 |
if (success) { |
if (success) { |
556 |
showMessageAndClose(getString(stationlist_nostations)); |
showMessageAndClose(getString(stationlist_nostations)); |
557 |
} |
} |
558 |
stations = stationProvider.getStations(); |
stations = stationProvider.getStations(); |
559 |
|
|
560 |
|
StationList.this.getListView().invalidateViews(); |
561 |
adapter.setStations( stations ); |
adapter.setStations( stations ); |
562 |
|
|
563 |
|
|
564 |
} else { //communication or parse errors |
} else { //communication or parse errors |
565 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
566 |
builder.setMessage(getString(stationlist_nearbyerror)); |
builder.setMessage(getString(stationlist_fetcherror)); |
567 |
builder.setCancelable(true); |
builder.setCancelable(true); |
568 |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
569 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
570 |
dialog.dismiss(); |
dialog.dismiss(); |
571 |
|
|
572 |
stationsFetched.post( new Runnable() { |
Runnable runner = null; |
573 |
@Override |
switch (method) { |
574 |
public void run() { |
case ByLocation: |
575 |
startLocatorTask(); |
runner = new Runnable() { |
576 |
} |
@Override |
577 |
}); |
public void run() { |
578 |
|
startLocatorTask(); |
579 |
|
} |
580 |
|
}; |
581 |
|
break; |
582 |
|
case ByName: |
583 |
|
runner = new Runnable() { |
584 |
|
@Override |
585 |
|
public void run() { |
586 |
|
startNameSearch( FindStationsTask.this.name ); |
587 |
|
} |
588 |
|
}; |
589 |
|
break; |
590 |
|
case ByList: |
591 |
|
runner = new Runnable() { |
592 |
|
@Override |
593 |
|
public void run() { |
594 |
|
startFavoriteLookup(); |
595 |
|
} |
596 |
|
}; |
597 |
|
break; |
598 |
|
} |
599 |
|
|
600 |
|
stationsFetched.post( runner ); |
601 |
} |
} |
602 |
}); |
}); |
603 |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
604 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
605 |
dialog.dismiss(); |
dialog.dismiss(); |
606 |
|
StationList.this.finish(); |
607 |
} |
} |
608 |
}); |
}); |
609 |
try { |
|
610 |
builder.show(); |
builderShowSafe(builder); // builder.show() |
|
} catch (android.view.WindowManager.BadTokenException e) { |
|
|
Log.i("StationList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |
|
|
} |
|
611 |
} |
} |
612 |
} |
} |
613 |
} |
} |