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; |
70 |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); |
71 |
|
|
72 |
GeoPair location = new GeoPair(); |
GeoPair location = new GeoPair(); |
73 |
|
|
74 |
|
boolean isLaunchedforShortcut; |
75 |
boolean isRunning = false; |
boolean isRunning = false; |
76 |
List<StationBean> stations = new ArrayList<StationBean>(); |
List<StationBean> stations = new ArrayList<StationBean>(); |
77 |
|
|
113 |
listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); |
listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); |
114 |
setTitle(); |
setTitle(); |
115 |
|
|
116 |
|
isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false); |
117 |
|
|
118 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
119 |
|
|
120 |
|
|
123 |
startLookup(); |
startLookup(); |
124 |
break; |
break; |
125 |
case ListSearch: |
case ListSearch: |
126 |
this.showDialog(DLG_STATIONNAME); |
this.showDialogSafe(DLG_STATIONNAME); |
127 |
break; |
break; |
128 |
case ListFavorites: |
case ListFavorites: |
129 |
startFavoriteLookup(); |
startFavoriteLookup(); |
139 |
} |
} |
140 |
|
|
141 |
} |
} |
142 |
|
|
143 |
|
|
144 |
|
@Override |
145 |
|
protected void onDestroy() { |
146 |
|
super.onDestroy(); |
147 |
|
|
148 |
|
if (findStationsTask != null) { |
149 |
|
findStationsTask.cancel(true); |
150 |
|
} |
151 |
|
if (locationLookup != null) { |
152 |
|
locationLookup.stopSearch(); |
153 |
|
} |
154 |
|
isRunning = false; |
155 |
|
} |
156 |
|
|
157 |
|
|
158 |
protected void setTitle() { |
protected void setTitle() { |
159 |
String dialogTitle = getResources().getString(app_name); |
String dialogTitle = getResources().getString(app_name); |
160 |
switch (listType) { |
switch (listType) { |
172 |
} |
} |
173 |
|
|
174 |
setTitle(dialogTitle); |
setTitle(dialogTitle); |
175 |
|
|
176 |
|
} |
177 |
|
|
178 |
|
|
179 |
|
/* these 3 dialogs helper functions are very rude and ugly hack |
180 |
|
* to remove these auto-reported exceptions |
181 |
|
* - android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@436aaef8 is not valid; is your activity running? |
182 |
|
* - java.lang.IllegalArgumentException: View not attached to window manager |
183 |
|
*/ |
184 |
|
|
185 |
|
public void showDialogSafe(int id) { |
186 |
|
try { |
187 |
|
showDialog(id); |
188 |
|
} catch (Exception e) { |
189 |
|
Log.e("StationList", "showDialog failed", e); |
190 |
|
} |
191 |
} |
} |
192 |
|
|
193 |
|
public void dismissDialogSafe(int id) { |
194 |
|
try { |
195 |
|
dismissDialog(id); |
196 |
|
} catch (Exception e) { |
197 |
|
Log.e("StationList", "dismissDialog failed", e); |
198 |
|
} |
199 |
|
} |
200 |
|
public void dismissDialogSafe(Dialog dlg) { |
201 |
|
try { |
202 |
|
dlg.dismiss(); |
203 |
|
} catch (Exception e) { |
204 |
|
Log.e("StationList", "dismissDialog failed", e); |
205 |
|
} |
206 |
|
} |
207 |
|
|
208 |
|
public void builderShowSafe(AlertDialog.Builder builder) { |
209 |
|
try { |
210 |
|
builder.show(); |
211 |
|
} catch (Exception e) { |
212 |
|
Log.e("StationList", "builder.show() failed", e); |
213 |
|
} |
214 |
|
|
215 |
|
} |
216 |
|
/* EOF rude and ugly dialog hack */ |
217 |
|
|
218 |
|
|
219 |
|
|
274 |
message.append( getString(stationlist_nolocation) ); |
message.append( getString(stationlist_nolocation) ); |
275 |
} |
} |
276 |
|
|
277 |
MessageBox.showMessage(this, message.toString()); |
MessageBox.showMessage(this, message.toString(), false); |
278 |
break; |
break; |
279 |
default: |
default: |
280 |
retval = super.onOptionsItemSelected(item); |
retval = super.onOptionsItemSelected(item); |
331 |
public void onClick(DialogInterface dialog, int which) { |
public void onClick(DialogInterface dialog, int which) { |
332 |
EditText et = (EditText) rootView.findViewById(R.id.EditText); |
EditText et = (EditText) rootView.findViewById(R.id.EditText); |
333 |
dialog.dismiss(); |
dialog.dismiss(); |
334 |
if (et.getText().toString().length() >= 2) { |
String search = et.getText().toString().trim(); |
335 |
startNameSearch(et.getText().toString()); |
if (search.length() >= 2) { |
336 |
|
startNameSearch(search); |
337 |
} else { |
} else { |
338 |
showMessageAndClose( getString(stationlist_twocharmin) ); |
showMessageAndClose( getString(stationlist_twocharmin) ); |
339 |
} |
} |
374 |
|
|
375 |
StationBean station = stations.get(position); |
StationBean station = stations.get(position); |
376 |
|
|
377 |
Intent intent = new Intent(this, DepartureList.class); |
if (isLaunchedforShortcut == true) { |
378 |
intent.putExtra("stationbean", station); |
Intent i = new Intent(); |
379 |
startActivity(intent); |
i.putExtra("station", station); |
380 |
|
setResult(Activity.RESULT_OK, i); |
381 |
|
finish(); |
382 |
|
} else { |
383 |
|
Intent intent = new Intent(this, DepartureList.class); |
384 |
|
intent.putExtra("stationbean", station); |
385 |
|
startActivity(intent); |
386 |
|
} |
387 |
} |
} |
388 |
|
|
389 |
///////////////////////////////////////////////////////////// |
///////////////////////////////////////////////////////////// |
392 |
public void startLookup() { |
public void startLookup() { |
393 |
isRunning = true; |
isRunning = true; |
394 |
dialogMessage = getString( stationlist_waitforlocation ); |
dialogMessage = getString( stationlist_waitforlocation ); |
395 |
showDialog(DLG_PROGRESS); |
showDialogSafe(DLG_PROGRESS); |
396 |
|
|
397 |
locationLookup.locateStations(); |
locationLookup.locateStations(); |
398 |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); |
stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); |
400 |
|
|
401 |
void startNameSearch(String name) { |
void startNameSearch(String name) { |
402 |
dialogMessage = getString( stationlist_findbyname ); |
dialogMessage = getString( stationlist_findbyname ); |
403 |
showDialog(DLG_PROGRESS); |
showDialogSafe(DLG_PROGRESS); |
404 |
|
|
405 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
406 |
findStationsTask.searchByName(name); |
findStationsTask.searchByName(name); |
412 |
|
|
413 |
if (favorites.size() > 0) { |
if (favorites.size() > 0) { |
414 |
dialogMessage = getString( stationlist_loadfavorites ); |
dialogMessage = getString( stationlist_loadfavorites ); |
415 |
showDialog(DLG_PROGRESS); |
showDialogSafe(DLG_PROGRESS); |
416 |
|
|
417 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
418 |
findStationsTask.searchByIds( favorites.toString() ); |
findStationsTask.searchByIds( favorites.toString() ); |
427 |
void startLocatorTask() |
void startLocatorTask() |
428 |
{ |
{ |
429 |
dialogMessage = getString( stationlist_findingnearby ); |
dialogMessage = getString( stationlist_findingnearby ); |
430 |
showDialog(DLG_PROGRESS); |
showDialogSafe(DLG_PROGRESS); |
431 |
|
|
432 |
findStationsTask = new FindStationsTask(); |
findStationsTask = new FindStationsTask(); |
433 |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
findStationsTask.searchByLocation( locationLookup.getLocation() ); |
444 |
|
|
445 |
switch (msg.what) { |
switch (msg.what) { |
446 |
case GOTLOCATION: |
case GOTLOCATION: |
447 |
dismissDialog(DLG_PROGRESS); |
dismissDialogSafe(DLG_PROGRESS); |
448 |
|
|
449 |
startLocatorTask(); |
startLocatorTask(); |
450 |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
location = GeoPair.fromLocation( locationLookup.getLocation() ); |
452 |
break; |
break; |
453 |
|
|
454 |
case NOPROVIDER: |
case NOPROVIDER: |
455 |
dismissDialog(DLG_PROGRESS); |
dismissDialogSafe(DLG_PROGRESS); |
456 |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) ); |
MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); |
457 |
|
//StationList.this.finish(); |
458 |
break; |
break; |
459 |
case LOCATIONFIXTIMEOUT: |
case LOCATIONFIXTIMEOUT: |
460 |
if (isRunning) { |
if (isRunning) { |
462 |
if (locationLookup.hasLocation()) { |
if (locationLookup.hasLocation()) { |
463 |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
stationsFetched.sendEmptyMessage( GOTLOCATION ); |
464 |
} else { |
} else { |
465 |
dismissDialog(DLG_PROGRESS); |
dismissDialogSafe(DLG_PROGRESS); |
466 |
|
|
467 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
468 |
builder.setMessage( getString( stationlist_gpstimeout) ); |
builder.setMessage( getString( stationlist_gpstimeout) ); |
478 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
479 |
dialog.dismiss(); |
dialog.dismiss(); |
480 |
} |
} |
481 |
}); |
}); |
482 |
builder.show(); |
builderShowSafe(builder); // builder.show() |
483 |
|
|
484 |
} |
} |
485 |
} |
} |
547 |
@Override |
@Override |
548 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
549 |
super.onPostExecute(result); |
super.onPostExecute(result); |
550 |
dialog.dismiss(); |
dismissDialogSafe(dialog); |
551 |
|
|
552 |
|
|
553 |
if (success) { |
if (success) { |
555 |
showMessageAndClose(getString(stationlist_nostations)); |
showMessageAndClose(getString(stationlist_nostations)); |
556 |
} |
} |
557 |
stations = stationProvider.getStations(); |
stations = stationProvider.getStations(); |
558 |
|
|
559 |
|
StationList.this.getListView().invalidateViews(); |
560 |
adapter.setStations( stations ); |
adapter.setStations( stations ); |
561 |
|
|
562 |
|
|
563 |
} else { //communication or parse errors |
} else { //communication or parse errors |
564 |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); |
565 |
builder.setMessage(getString(stationlist_nearbyerror)); |
builder.setMessage(getString(stationlist_fetcherror)); |
566 |
builder.setCancelable(true); |
builder.setCancelable(true); |
567 |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
568 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
602 |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
603 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
604 |
dialog.dismiss(); |
dialog.dismiss(); |
605 |
|
StationList.this.finish(); |
606 |
} |
} |
607 |
}); |
}); |
608 |
try { |
|
609 |
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 |
|
|
} |
|
610 |
} |
} |
611 |
} |
} |
612 |
} |
} |