Now supports KDDI/AU / Softbank address book format
authorsrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Thu, 6 Mar 2008 15:43:31 +0000 (15:43 +0000)
committersrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Thu, 6 Mar 2008 15:43:31 +0000 (15:43 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@249 59b500cc-1b3d-0410-9834-0bbf25fbcc57

android/src/com/google/zxing/client/android/ResultHandler.java
core/src/com/google/zxing/client/result/AbstractDoCoMoResult.java
core/src/com/google/zxing/client/result/AddressBookAUResult.java [new file with mode: 0644]
core/src/com/google/zxing/client/result/AddressBookDoCoMoResult.java
core/src/com/google/zxing/client/result/ParsedReaderResult.java
core/src/com/google/zxing/client/result/ParsedReaderResultType.java
core/test/src/com/google/zxing/client/result/ParsedReaderResultTestCase.java

index 0f20b2d..9d8f1b7 100755 (executable)
@@ -21,6 +21,7 @@ import android.net.ContentURI;
 import android.os.Handler;
 import android.os.Message;
 import android.provider.Contacts;
+import com.google.zxing.client.result.AddressBookAUResult;
 import com.google.zxing.client.result.AddressBookDoCoMoResult;
 import com.google.zxing.client.result.BookmarkDoCoMoResult;
 import com.google.zxing.client.result.EmailAddressResult;
@@ -59,10 +60,18 @@ final class ResultHandler extends Handler {
         AddressBookDoCoMoResult addressResult = (AddressBookDoCoMoResult) result;
         intent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
         putExtra(intent, Contacts.Intents.Insert.NAME, addressResult.getName());
-        putExtra(intent, Contacts.Intents.Insert.PHONE, addressResult.getPhoneNumbers()[0]);
+        putExtra(intent, Contacts.Intents.Insert.PHONE, addressResult.getPhoneNumbers());
         putExtra(intent, Contacts.Intents.Insert.EMAIL, addressResult.getEmail());
         putExtra(intent, Contacts.Intents.Insert.NOTES, addressResult.getNote());
         putExtra(intent, Contacts.Intents.Insert.POSTAL, addressResult.getAddress());
+      } else if (type.equals(ParsedReaderResultType.ADDRESSBOOK_AU)) {
+        AddressBookAUResult addressResult = (AddressBookAUResult) result;
+        intent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
+        putExtra(intent, Contacts.Intents.Insert.NAME, addressResult.getNames());
+        putExtra(intent, Contacts.Intents.Insert.PHONE, addressResult.getPhoneNumbers());
+        putExtra(intent, Contacts.Intents.Insert.EMAIL, addressResult.getEmails());
+        putExtra(intent, Contacts.Intents.Insert.NOTES, addressResult.getNote());
+        putExtra(intent, Contacts.Intents.Insert.POSTAL, addressResult.getAddress());
       } else if (type.equals(ParsedReaderResultType.BOOKMARK)) {
         // For now, we can only open the browser, and not actually add a bookmark
         try {
@@ -120,9 +129,15 @@ final class ResultHandler extends Handler {
   }
 
   private static void putExtra(Intent intent, String key, String value) {
-    if (key != null && key.length() > 0) {
+    if (value != null && value.length() > 0) {
       intent.putExtra(key, value);
     }
   }
 
+  private static void putExtra(Intent intent, String key, String[] value) {
+    if (value != null && value.length > 0) {
+      putExtra(intent, key, value[0]);
+    }
+  }
+
 }
index f9436a0..88a4024 100644 (file)
@@ -38,6 +38,10 @@ abstract class AbstractDoCoMoResult extends ParsedReaderResult {
   // to run in a J2ME enviroment, where this unavailable.
 
   static String[] matchPrefixedField(String prefix, String rawText) {
+    return matchPrefixedField(prefix, rawText, ';');
+  }
+
+  static String[] matchPrefixedField(String prefix, String rawText, char endChar) {
     Vector matches = null;
     int i = 0;
     int max = rawText.length();
@@ -50,9 +54,9 @@ abstract class AbstractDoCoMoResult extends ParsedReaderResult {
       int start = i; // Found the start of a match here
       boolean done = false;
       while (!done) {
-        i = rawText.indexOf((int) ';', i);
+        i = rawText.indexOf((int) endChar, i);
         if (i < 0) {
-          // No terminating semicolon? uh, done. Set i such that loop terminates and break
+          // No terminating end character? uh, done. Set i such that loop terminates and break
           i = rawText.length();
           done = true;
         } else if (rawText.charAt(i - 1) == '\\') {
@@ -81,7 +85,11 @@ abstract class AbstractDoCoMoResult extends ParsedReaderResult {
   }
 
   static String matchSinglePrefixedField(String prefix, String rawText) {
-    String[] matches = matchPrefixedField(prefix, rawText);
+    return matchSinglePrefixedField(prefix, rawText, ';');
+  }
+
+  static String matchSinglePrefixedField(String prefix, String rawText, char endChar) {
+    String[] matches = matchPrefixedField(prefix, rawText, endChar);
     return matches == null ? null : matches[0];
   }
 
@@ -115,4 +123,13 @@ abstract class AbstractDoCoMoResult extends ParsedReaderResult {
     }
   }
 
+  static void maybeAppend(String[] value, StringBuffer result) {
+    if (value != null) {
+      for (int i = 0; i < value.length; i++) {
+        result.append('\n');
+        result.append(value[i]);
+      }
+    }
+  }
+
 }
\ No newline at end of file
diff --git a/core/src/com/google/zxing/client/result/AddressBookAUResult.java b/core/src/com/google/zxing/client/result/AddressBookAUResult.java
new file mode 100644 (file)
index 0000000..46409bb
--- /dev/null
@@ -0,0 +1,111 @@
+/*
+ * 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 java.util.Vector;
+
+/**
+ * Implements KDDI AU's address book format. See
+ * <a href="http://www.au.kddi.com/ezfactory/tec/two_dimensions/index.html">
+ * http://www.au.kddi.com/ezfactory/tec/two_dimensions/index.html</a>.
+ * (Thanks to Yuzo for translating!)
+ *
+ * @author srowen@google.com (Sean Owen)
+ */
+public final class AddressBookAUResult extends ParsedReaderResult {
+
+  private final String[] names;
+  private final String[] phoneNumbers;
+  private final String[] emails;
+  private final String note;
+  private final String address;
+
+  private AddressBookAUResult(String[] names, String[] phoneNumbers, String[] emails, String note, String address) {
+    super(ParsedReaderResultType.ADDRESSBOOK_AU);
+    this.names = names;
+    this.phoneNumbers = phoneNumbers;
+    this.emails = emails;
+    this.note = note;
+    this.address = address;
+  }
+
+  public static AddressBookAUResult parse(String rawText) {
+    // MEMORY is mandatory; seems like a decent indicator, as does end-of-record separator CR/LF
+    if (rawText.indexOf("MEMORY") < 0 || rawText.indexOf("\r\n") < 0) {
+      return null;
+    }
+    String[] names = matchMultipleValuePrefix("NAME", 2, rawText);
+    String[] phoneNumbers = matchMultipleValuePrefix("TEL", 3, rawText);
+    String[] emails = matchMultipleValuePrefix("MAIL", 3, rawText);
+    String note = AbstractDoCoMoResult.matchSinglePrefixedField("MEMORY", rawText, '\r');
+    String address = AbstractDoCoMoResult.matchSinglePrefixedField("ADD", rawText, '\r');
+    return new AddressBookAUResult(names, phoneNumbers, emails, note, address);
+  }
+
+  private static String[] matchMultipleValuePrefix(String prefix, int max, String rawText) {
+    Vector values = null;
+    for (int i = 1; i <= max; i++) {
+      String value = AbstractDoCoMoResult.matchSinglePrefixedField(prefix + i, rawText, '\r');
+      if (value == null) {
+        break;
+      }
+      if (values == null) {
+        values = new Vector(max); // lazy init
+      }
+      values.addElement(value);
+    }
+    if (values == null) {
+      return null;
+    }
+    String[] result = new String[values.size()];
+    for (int i = 0; i < result.length; i++) {
+      result[i] = (String) values.elementAt(i);
+    }
+    return result;
+  }
+
+  public String[] getNames() {
+    return names;
+  }
+
+  public String[] getPhoneNumbers() {
+    return phoneNumbers;
+  }
+
+  public String[] getEmails() {
+    return emails;
+  }
+
+  public String getNote() {
+    return note;
+  }
+
+  public String getAddress() {
+    return address;
+  }
+
+  public String getDisplayResult() {
+    StringBuffer result = new StringBuffer();
+    AbstractDoCoMoResult.maybeAppend(names, result);
+    AbstractDoCoMoResult.maybeAppend(emails, result);
+    AbstractDoCoMoResult.maybeAppend(address, result);
+    AbstractDoCoMoResult.maybeAppend(phoneNumbers, result);
+    AbstractDoCoMoResult.maybeAppend(note, result);
+    return result.toString();
+  }
+
+}
\ No newline at end of file
index f57a3c7..2150d33 100644 (file)
@@ -83,9 +83,7 @@ public final class AddressBookDoCoMoResult extends AbstractDoCoMoResult {
     StringBuffer result = new StringBuffer(name);
     maybeAppend(email, result);
     maybeAppend(address, result);
-    for (int i = 0; i < phoneNumbers.length; i++) {
-      maybeAppend(phoneNumbers[i], result);
-    }
+    maybeAppend(phoneNumbers, result);
     maybeAppend(note, result);
     return result.toString();
   }
index 18950c5..f1e8e5c 100644 (file)
@@ -54,6 +54,8 @@ public abstract class ParsedReaderResult {
       return result;
     } else if ((result = EmailAddressResult.parse(rawText)) != null) {
       return result;
+    } else if ((result = AddressBookAUResult.parse(rawText)) != null) {
+      return result;
     } else if ((result = URLTOResult.parse(rawText)) != null) {
       return result;
     } else if ((result = URIParsedResult.parse(rawText)) != null) {
index 48225ae..712e955 100644 (file)
@@ -27,6 +27,7 @@ public final class ParsedReaderResultType {
   public static final ParsedReaderResultType BOOKMARK = new ParsedReaderResultType("BOOKMARK");
   public static final ParsedReaderResultType URLTO = new ParsedReaderResultType("URLTO");
   public static final ParsedReaderResultType ADDRESSBOOK = new ParsedReaderResultType("ADDRESSBOOK");
+  public static final ParsedReaderResultType ADDRESSBOOK_AU = new ParsedReaderResultType("ADDRESSBOOK_AU");  
   public static final ParsedReaderResultType EMAIL = new ParsedReaderResultType("EMAIL");
   public static final ParsedReaderResultType EMAIL_ADDRESS = new ParsedReaderResultType("EMAIL_ADDRESS");
   public static final ParsedReaderResultType UPC = new ParsedReaderResultType("UPC");
index b8a6e4e..dc28f71 100644 (file)
@@ -68,6 +68,12 @@ public final class ParsedReaderResultTestCase extends TestCase {
     doTestResult("TEL:+12125551212;N:Sean Owen;;", ParsedReaderResultType.TEXT);
   }
 
+  public void testAddressBookAUType() {
+    doTestResult("MEMORY:\r\n", ParsedReaderResultType.ADDRESSBOOK_AU);
+    doTestResult("MEMORY:foo\r\nNAME1:Sean\r\n", ParsedReaderResultType.ADDRESSBOOK_AU);
+    doTestResult("TEL1:+12125551212\r\nMEMORY:\r\n", ParsedReaderResultType.ADDRESSBOOK_AU);
+  }
+
   public void testUPC() {
     doTestResult("123456789012", ParsedReaderResultType.UPC);
     doTestResult("1234567890123", ParsedReaderResultType.UPC);