From 9fbd5f90c4eddc930a7e09d99d1be08a7408269c Mon Sep 17 00:00:00 2001 From: Doug Keen Date: Sat, 16 Apr 2016 09:47:41 -0700 Subject: [PATCH] Make things a little more resilient to weird station abbreviations from the BART API --- .../java/com/dougkeen/bart/model/Station.java | 17 +++++++++++++ .../bart/networktasks/EtdContentHandler.java | 24 +++++++++++++------ build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 4 ++-- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/dougkeen/bart/model/Station.java b/app/src/main/java/com/dougkeen/bart/model/Station.java index 9e0990b..b0ede9a 100644 --- a/app/src/main/java/com/dougkeen/bart/model/Station.java +++ b/app/src/main/java/com/dougkeen/bart/model/Station.java @@ -130,6 +130,23 @@ public enum Station { } } + public static Station getByApproximateName(String name) { + if (name == null) return null; + + final String lowercaseName = name.toLowerCase(); + for (Station station : Station.values()) { + if (lowercaseName.startsWith(station.name.toLowerCase())) { + return station; + } + } + for (Station station : Station.values()) { + if (lowercaseName.endsWith(station.name.toLowerCase())) { + return station; + } + } + return Station.SPCL; + } + public Station getInboundTransferStation() { return getByAbbreviation(inboundTransferStation); } diff --git a/app/src/main/java/com/dougkeen/bart/networktasks/EtdContentHandler.java b/app/src/main/java/com/dougkeen/bart/networktasks/EtdContentHandler.java index 6ed9671..b661d50 100644 --- a/app/src/main/java/com/dougkeen/bart/networktasks/EtdContentHandler.java +++ b/app/src/main/java/com/dougkeen/bart/networktasks/EtdContentHandler.java @@ -27,7 +27,7 @@ public class EtdContentHandler extends DefaultHandler { private final static List TAGS = Arrays.asList("date", "time", "abbreviation", "minutes", "platform", "direction", "length", - "color", "hexcolor", "bikeflag"); + "color", "hexcolor", "bikeflag", "destination"); private RealTimeDepartures realTimeDepartures; @@ -36,7 +36,8 @@ public class EtdContentHandler extends DefaultHandler { } private String date; - private String currentDestination; + private String currentDestinationAbbreviation; + private String currentDestinationName; private String currentValue; private Departure currentDeparture; private boolean isParsingTag; @@ -57,8 +58,11 @@ public class EtdContentHandler extends DefaultHandler { } if (localName.equals("estimate")) { currentDeparture = new Departure(); - currentDeparture.setTrainDestination(Station - .getByAbbreviation(currentDestination)); + Station destination = Station.getByAbbreviation(currentDestinationAbbreviation); + if (destination == null) { + destination = Station.getByApproximateName(currentDestinationName); + } + currentDeparture.setTrainDestination(destination); currentDeparture.setOrigin(realTimeDepartures.getOrigin()); } } @@ -71,7 +75,9 @@ public class EtdContentHandler extends DefaultHandler { } else if (localName.equals("time")) { realTimeDepartures.setTime(Date.parse(date + " " + currentValue)); } else if (localName.equals("abbreviation")) { - currentDestination = currentValue; + currentDestinationAbbreviation = currentValue; + } else if (localName.equals("destination")) { + currentDestinationName = currentValue; } else if (localName.equals("minutes")) { if (StringUtils.isNumeric(currentValue)) { currentDeparture.setMinutes(Integer.parseInt(currentValue)); @@ -109,10 +115,14 @@ public class EtdContentHandler extends DefaultHandler { } else if (localName.equals("bikeflag")) { currentDeparture.setBikeAllowed(currentValue.equalsIgnoreCase("1")); } else if (localName.equals("estimate")) { - realTimeDepartures.addDeparture(currentDeparture); + // If we can't infer a real destination because of weird API data, + // just skip it ¯\_(ツ)_/¯ + if (realTimeDepartures.getDestination() != null) { + realTimeDepartures.addDeparture(currentDeparture); + } currentDeparture = null; } else if (localName.equals("etd")) { - currentDestination = null; + currentDestinationAbbreviation = null; } else if (localName.equals("station")) { realTimeDepartures.finalizeDeparturesList(); } diff --git a/build.gradle b/build.gradle index cfc3897..b076d7b 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:1.3.0' + classpath 'com.android.tools.build:gradle:2.0.0' // Used for annotation processing needed by the AndroidAnnotations library classpath 'com.neenbedankt.gradle.plugins:android-apt:1.6' } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 1ac7004..2a2fd76 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Mon Jul 20 14:31:30 PDT 2015 +#Sat Apr 16 08:57:22 PDT 2016 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.5-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip