BartRunnerAndroid/src/com/dougkeen/bart/networktasks/GetRealTimeDeparturesTask.java

154 lines
4.3 KiB
Java
Raw Normal View History

package com.dougkeen.bart.networktasks;
2011-05-23 18:59:34 +00:00
import java.io.IOException;
2011-06-17 21:35:20 +00:00
import java.io.StringWriter;
2011-05-23 18:59:34 +00:00
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.List;
2011-06-17 21:35:20 +00:00
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
2011-05-23 18:59:34 +00:00
import android.os.AsyncTask;
2011-06-17 21:35:20 +00:00
import android.util.Log;
2011-05-23 18:59:34 +00:00
import android.util.Xml;
import com.dougkeen.bart.model.Constants;
import com.dougkeen.bart.model.RealTimeDepartures;
import com.dougkeen.bart.model.Route;
2012-07-18 02:20:18 +00:00
import com.dougkeen.bart.model.StationPair;
2011-06-17 21:35:20 +00:00
public abstract class GetRealTimeDeparturesTask extends
AsyncTask<StationPair, Integer, RealTimeDepartures> {
2011-05-23 18:59:34 +00:00
2012-01-02 23:08:43 +00:00
private final static String ETD_URL = "http://api.bart.gov/api/etd.aspx?cmd=etd&key="
+ Constants.API_KEY + "&orig=%1$s&dir=%2$s";
private final static String ETD_URL_NO_DIRECTION = "http://api.bart.gov/api/etd.aspx?cmd=etd&key="
+ Constants.API_KEY + "&orig=%1$s";
2011-06-20 22:34:09 +00:00
private final static int MAX_ATTEMPTS = 5;
2011-05-23 18:59:34 +00:00
2011-06-17 21:35:20 +00:00
private Exception mException;
2011-05-23 18:59:34 +00:00
private List<Route> mRoutes;
private final boolean ignoreDirection;
public GetRealTimeDeparturesTask(boolean ignoreDirection) {
super();
this.ignoreDirection = ignoreDirection;
}
2011-05-23 18:59:34 +00:00
@Override
protected RealTimeDepartures doInBackground(StationPair... paramsArray) {
2011-05-23 18:59:34 +00:00
// Always expect one param
StationPair params = paramsArray[0];
2011-05-23 18:59:34 +00:00
2012-04-29 16:42:54 +00:00
mRoutes = params.getOrigin().getDirectRoutesForDestination(
params.getDestination());
2011-05-23 18:59:34 +00:00
2012-07-18 02:20:18 +00:00
boolean hasDirectLine = false;
for (Route route : mRoutes) {
if (!route.hasTransfer()) {
hasDirectLine = true;
break;
}
}
if (mRoutes.isEmpty()
|| (params.getOrigin().transferFriendly && !hasDirectLine)) {
2012-04-29 16:42:54 +00:00
mRoutes.addAll(params.getOrigin().getTransferRoutes(
params.getDestination()));
}
if (!isCancelled()) {
return getDeparturesFromNetwork(params, 0);
} else {
return null;
}
2011-05-23 18:59:34 +00:00
}
private RealTimeDepartures getDeparturesFromNetwork(StationPair params,
2011-06-01 03:42:32 +00:00
int attemptNumber) {
2011-06-17 21:35:20 +00:00
String xml = null;
2011-05-23 18:59:34 +00:00
try {
String url;
if (ignoreDirection || params.getOrigin().endOfLine) {
url = String.format(ETD_URL_NO_DIRECTION,
params.getOrigin().abbreviation);
} else {
url = String.format(ETD_URL, params.getOrigin().abbreviation,
mRoutes.get(0).getDirection());
}
HttpUriRequest request = new HttpGet(url);
2011-06-01 03:42:32 +00:00
EtdContentHandler handler = new EtdContentHandler(
params.getOrigin(), params.getDestination(), mRoutes);
if (isCancelled()) {
return null;
}
2011-06-17 21:35:20 +00:00
2012-01-02 23:08:43 +00:00
HttpResponse response = NetworkUtils.executeWithRecovery(request);
2011-06-17 21:35:20 +00:00
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
throw new IOException("Server returned "
+ response.getStatusLine().toString());
}
StringWriter writer = new StringWriter();
IOUtils.copy(response.getEntity().getContent(), writer, "UTF-8");
xml = writer.toString();
if (xml.length() == 0) {
throw new IOException("Server returned blank xml document");
}
2012-07-16 15:06:57 +00:00
try {
Xml.parse(xml, handler);
} catch (Exception e) {
mException = new IOException("Server returned malformed xml: "
+ xml);
return null;
}
final RealTimeDepartures realTimeDepartures = handler
.getRealTimeDepartures();
return realTimeDepartures;
2011-05-23 18:59:34 +00:00
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
} catch (IOException e) {
if (attemptNumber < MAX_ATTEMPTS - 1) {
try {
2011-06-17 21:35:20 +00:00
Log.w(Constants.TAG,
2011-06-20 22:34:09 +00:00
"Attempt to contact server failed... retrying in 3s",
2011-06-17 21:35:20 +00:00
e);
2011-06-20 22:34:09 +00:00
Thread.sleep(3000);
2011-05-23 18:59:34 +00:00
} catch (InterruptedException interrupt) {
// Ignore... just go on to next attempt
}
return getDeparturesFromNetwork(params, attemptNumber + 1);
2011-05-23 18:59:34 +00:00
} else {
2011-06-17 21:35:20 +00:00
mException = new Exception("Could not contact BART system", e);
2011-05-23 18:59:34 +00:00
return null;
}
}
}
@Override
protected void onPostExecute(RealTimeDepartures result) {
2011-05-23 18:59:34 +00:00
if (result != null) {
onResult(result);
} else {
2011-06-17 21:35:20 +00:00
onError(mException);
2011-05-23 18:59:34 +00:00
}
}
public abstract void onResult(RealTimeDepartures result);
2011-05-23 18:59:34 +00:00
2011-06-17 21:35:20 +00:00
public abstract void onError(Exception exception);
2011-05-23 18:59:34 +00:00
}