Support SMTP URLs
[zxing.git] / core / src / com / google / zxing / client / result / GeoParsedResult.java
index 57e3ac5..dc50e8a 100644 (file)
 
 package com.google.zxing.client.result;
 
-import com.google.zxing.Result;
-
 /**
- * Represents a "geo:" URI result, which specifices a location on the surface of
- * the Earth as well as an optional altitude above the surface. See
- * <a href="http://tools.ietf.org/html/draft-mayrhofer-geo-uri-00">
- * http://tools.ietf.org/html/draft-mayrhofer-geo-uri-00</a>.
- *
- * @author srowen@google.com (Sean Owen)
+ * @author Sean Owen
  */
-public final class GeoParsedResult extends ParsedReaderResult {
+public final class GeoParsedResult extends ParsedResult {
 
-  private final String geoURI;
-  private final float latitude;
-  private final float longitude;
-  private final float altitude;
+  private final double latitude;
+  private final double longitude;
+  private final double altitude;
+  private final String query;
 
-  private GeoParsedResult(String geoURI, float latitude, float longitude, float altitude) {
-    super(ParsedReaderResultType.GEO);
-    this.geoURI = geoURI;
+  GeoParsedResult(double latitude, double longitude, double altitude, String query) {
+    super(ParsedResultType.GEO);
     this.latitude = latitude;
     this.longitude = longitude;
     this.altitude = altitude;
+    this.query = query;
   }
 
-  public static GeoParsedResult parse(Result result) {
-    String rawText = result.getText();
-    if (rawText == null || !rawText.startsWith("geo:")) {
-      return null;
-    }
-    // Drop geo, query portion
-    int queryStart = rawText.indexOf('?', 4);
-    String geoURIWithoutQuery;
-    if (queryStart < 0) {
-      geoURIWithoutQuery = rawText.substring(4);
-    } else {
-      geoURIWithoutQuery = rawText.substring(4, queryStart);
-    }
-    int latitudeEnd = geoURIWithoutQuery.indexOf(',');
-    if (latitudeEnd < 0) {
-      return null;
+  public String getGeoURI() {
+    StringBuffer result = new StringBuffer();
+    result.append("geo:");
+    result.append(latitude);
+    result.append(',');
+    result.append(longitude);
+    if (altitude > 0) {
+      result.append(',');
+      result.append(altitude);
     }
-    float latitude = Float.parseFloat(geoURIWithoutQuery.substring(0, latitudeEnd));
-    int longitudeEnd = geoURIWithoutQuery.indexOf(',', latitudeEnd + 1);
-    float longitude;
-    float altitude; // in meters
-    if (longitudeEnd < 0) {
-      longitude = Float.parseFloat(geoURIWithoutQuery.substring(latitudeEnd + 1));
-      altitude = 0.0f;
-    } else {
-      longitude = Float.parseFloat(geoURIWithoutQuery.substring(latitudeEnd + 1, longitudeEnd));
-      altitude = Float.parseFloat(geoURIWithoutQuery.substring(longitudeEnd + 1));
+    if (query != null) {
+      result.append('?');
+      result.append(query);
     }
-    return new GeoParsedResult(rawText, latitude, longitude, altitude);
-  }
-
-  public String getGeoURI() {
-    return geoURI;
+    return result.toString();
   }
 
   /**
    * @return latitude in degrees
    */
-  public float getLatitude() {
+  public double getLatitude() {
     return latitude;
   }
 
   /**
    * @return longitude in degrees
    */
-  public float getLongitude() {
+  public double getLongitude() {
     return longitude;
   }
 
   /**
    * @return altitude in meters. If not specified, in the geo URI, returns 0.0
    */
-  public float getAltitude() {
+  public double getAltitude() {
     return altitude;
   }
 
+  /**
+   * @return query string associated with geo URI or null if none exists
+   */
+  public String getQuery() {
+    return query;
+  }
+
   public String getDisplayResult() {
-    StringBuffer result = new StringBuffer(50);
+    StringBuffer result = new StringBuffer(20);
     result.append(latitude);
-    result.append(" deg N, ");
+    result.append(", ");
     result.append(longitude);
-    result.append(" deg E");
-    if (altitude > 0.0f) {
+    if (altitude > 0.0) {
       result.append(", ");
       result.append(altitude);
       result.append('m');
     }
+    if (query != null) {
+      result.append(" (");
+      result.append(query);
+      result.append(')');
+    }
     return result.toString();
   }
 
@@ -126,8 +112,8 @@ public final class GeoParsedResult extends ParsedReaderResult {
     if (altitude > 0.0f) {
       // Map altitude to zoom level, cleverly. Roughly, zoom level 19 is like a
       // view from 1000ft, 18 is like 2000ft, 17 like 4000ft, and so on.
-      float altitudeInFeet = altitude * 3.28f;
-      int altitudeInKFeet = (int) (altitudeInFeet / 1000.0f);
+      double altitudeInFeet = altitude * 3.28;
+      int altitudeInKFeet = (int) (altitudeInFeet / 1000.0);
       // No Math.log() available here, so compute log base 2 the old fashioned way
       // Here logBaseTwo will take on a value between 0 and 18 actually
       int logBaseTwo = 0;