Add support for tel: URIs
authorsrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Mon, 17 Mar 2008 20:17:20 +0000 (20:17 +0000)
committersrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Mon, 17 Mar 2008 20:17:20 +0000 (20:17 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@292 59b500cc-1b3d-0410-9834-0bbf25fbcc57

android/res/values/strings.xml
android/src/com/google/zxing/client/android/BarcodeReaderCaptureActivity.java
android/src/com/google/zxing/client/android/ResultHandler.java
core/src/com/google/zxing/client/result/ParsedReaderResult.java
core/src/com/google/zxing/client/result/ParsedReaderResultType.java
core/src/com/google/zxing/client/result/TelParsedResult.java [new file with mode: 0644]
core/test/src/com/google/zxing/client/result/ParsedReaderResultTestCase.java
javame/src/com/google/zxing/client/j2me/ZXingMIDlet.java

index 75eb098..eb39149 100644 (file)
@@ -30,4 +30,5 @@
   <string name="title_add_contact">Add Contact?</string>
   <string name="title_compose_email">Compose E-mail?</string>
   <string name="title_lookup_barcode">Look Up Barcode Online?</string>
+  <string name="title_dial">Dial Number?</string>  
 </resources>
index e928af4..9599dfe 100644 (file)
@@ -205,6 +205,8 @@ public final class BarcodeReaderCaptureActivity extends Activity {
       return R.string.title_compose_email;
     } else if (type.equals(ParsedReaderResultType.UPC)) {
       return R.string.title_lookup_barcode;
+    } else if (type.equals(ParsedReaderResultType.TEL)) {
+      return R.string.title_dial;
     } else {
       return R.string.title_barcode_detected;
     }
index 13d5837..dc121d1 100755 (executable)
@@ -28,6 +28,7 @@ import com.google.zxing.client.result.EmailAddressParsedResult;
 import com.google.zxing.client.result.EmailDoCoMoParsedResult;
 import com.google.zxing.client.result.ParsedReaderResult;
 import com.google.zxing.client.result.ParsedReaderResultType;
+import com.google.zxing.client.result.TelParsedResult;
 import com.google.zxing.client.result.UPCParsedResult;
 import com.google.zxing.client.result.URIParsedResult;
 import com.google.zxing.client.result.URLTOParsedResult;
@@ -95,6 +96,12 @@ final class ResultHandler extends Handler {
         intent = new Intent(Intent.SENDTO_ACTION, new ContentURI(emailResult.getEmailAddress()));
       } catch (URISyntaxException e) {
       }
+    } else if (type.equals(ParsedReaderResultType.TEL)) {
+      TelParsedResult telResult = (TelParsedResult) result;
+      try {
+        intent = new Intent(Intent.DIAL_ACTION, new ContentURI("tel:" + telResult.getNumber()));
+      } catch (URISyntaxException e) {
+      }
     //} else if (type.equals(ParsedReaderResultType.GEO)) {
     //  GeoParsedResult geoResult = (GeoParsedResult) result;
     //  try {
index 39d548b..471f83d 100644 (file)
@@ -58,6 +58,8 @@ public abstract class ParsedReaderResult {
       return result;
     } else if ((result = AddressBookAUParsedResult.parse(theResult)) != null) {
       return result;
+    } else if ((result = TelParsedResult.parse(theResult)) != null) {
+      return result;
     } else if ((result = GeoParsedResult.parse(theResult)) != null) {
       return result;
     } else if ((result = URLTOParsedResult.parse(theResult)) != null) {
index 98e6879..e7345c3 100644 (file)
@@ -35,6 +35,7 @@ public final class ParsedReaderResultType {
   public static final ParsedReaderResultType TEXT = new ParsedReaderResultType("TEXT");
   public static final ParsedReaderResultType ANDROID_INTENT = new ParsedReaderResultType("ANDROID_INTENT"); 
   public static final ParsedReaderResultType GEO = new ParsedReaderResultType("GEO");
+  public static final ParsedReaderResultType TEL = new ParsedReaderResultType("TEL");
   // TODO later, add the NDEF types to those actually processed by the clients
   public static final ParsedReaderResultType NDEF_TEXT = new ParsedReaderResultType("NDEF_TEXT");
   public static final ParsedReaderResultType NDEF_URI = new ParsedReaderResultType("NDEF_URI");
diff --git a/core/src/com/google/zxing/client/result/TelParsedResult.java b/core/src/com/google/zxing/client/result/TelParsedResult.java
new file mode 100644 (file)
index 0000000..598861c
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2008 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.zxing.client.result;
+
+import com.google.zxing.Result;
+
+/**
+ * Represents a "tel:" URI result, which specifies a phone number.
+ *
+ * @author srowen@google.com (Sean Owen)
+ */
+public final class TelParsedResult extends ParsedReaderResult {
+
+  private final String number;
+
+  private TelParsedResult(String number) {
+    super(ParsedReaderResultType.TEL);
+    this.number = number;
+  }
+
+  public static TelParsedResult parse(Result result) {
+    String rawText = result.getText();
+    if (!rawText.startsWith("tel:")) {
+      return null;
+    }
+    // Drop tel, query portion
+    int queryStart = rawText.indexOf('?', 4);
+    if (queryStart < 0) {
+      rawText = rawText.substring(4);
+    } else {
+      rawText = rawText.substring(4, queryStart);
+    }
+    return new TelParsedResult(rawText);
+  }
+
+  public String getNumber() {
+    return number;
+  }
+
+  public String getDisplayResult() {
+    return number;
+  }
+
+}
\ No newline at end of file
index c796825..68e33fc 100644 (file)
@@ -16,6 +16,7 @@
 
 package com.google.zxing.client.result;
 
+import com.google.zxing.BarcodeFormat;
 import com.google.zxing.Result;
 import junit.framework.TestCase;
 
@@ -76,8 +77,8 @@ public final class ParsedReaderResultTestCase extends TestCase {
   }
 
   public void testUPC() {
-    doTestResult("123456789012", ParsedReaderResultType.UPC);
-    doTestResult("1234567890123", ParsedReaderResultType.UPC);
+    doTestResult("123456789012", ParsedReaderResultType.UPC, BarcodeFormat.UPC_A);
+    doTestResult("1234567890123", ParsedReaderResultType.UPC, BarcodeFormat.UPC_A);
     doTestResult("12345678901", ParsedReaderResultType.TEXT);
   }
 
@@ -94,11 +95,23 @@ public final class ParsedReaderResultTestCase extends TestCase {
   public void testGeo() {
     doTestResult("geo:1,2", ParsedReaderResultType.GEO);
     doTestResult("geo:1,2,3", ParsedReaderResultType.GEO);
-    doTestResult("geo:100.33,-32.3344,3.35", ParsedReaderResultType.GEO);    
+    doTestResult("geo:100.33,-32.3344,3.35", ParsedReaderResultType.GEO);
+    doTestResult("geography", ParsedReaderResultType.TEXT);        
+  }
+
+  public void testTel() {
+    doTestResult("tel:+15551212", ParsedReaderResultType.TEL);
+    doTestResult("tel:212 555 1212", ParsedReaderResultType.TEL);
+    doTestResult("tel:2125551212", ParsedReaderResultType.TEL);
+    doTestResult("telephone", ParsedReaderResultType.TEXT);
   }
 
   private static void doTestResult(String text, ParsedReaderResultType type) {
-    Result fakeResult = new Result(text, null, null, null);
+    doTestResult(text, type, null);
+  }
+
+  private static void doTestResult(String text, ParsedReaderResultType type, BarcodeFormat format) {
+    Result fakeResult = new Result(text, null, null, format);
     ParsedReaderResult result = ParsedReaderResult.parseReaderResult(fakeResult);
     assertNotNull(result);
     assertEquals(type, result.getType());
index 49bcf27..8f4372b 100644 (file)
@@ -22,6 +22,7 @@ import com.google.zxing.client.result.EmailAddressParsedResult;
 import com.google.zxing.client.result.EmailDoCoMoParsedResult;
 import com.google.zxing.client.result.ParsedReaderResult;
 import com.google.zxing.client.result.ParsedReaderResultType;
+import com.google.zxing.client.result.TelParsedResult;
 import com.google.zxing.client.result.UPCParsedResult;
 import com.google.zxing.client.result.URIParsedResult;
 import com.google.zxing.client.result.URLTOParsedResult;
@@ -214,6 +215,9 @@ public final class ZXingMIDlet extends MIDlet {
       String upc = ((UPCParsedResult) result).getUPC();
       String uri = "http://www.upcdatabase.com/item.asp?upc=" + upc;
       showOpenURL("Look Up Barcode Online?", upc, uri);
+    } else if (type.equals(ParsedReaderResultType.TEL)) {
+      String number = ((TelParsedResult) result).getNumber();
+      showOpenURL("Dial Number?", number, "tel:" + number);
     } else {
       showAlert("Barcode Detected", result.getDisplayResult());
     }