From f7306489b688407b7317a7287d2f1e0930fc37e2 Mon Sep 17 00:00:00 2001 From: srowen Date: Thu, 6 Mar 2008 15:43:31 +0000 Subject: [PATCH] Now supports KDDI/AU / Softbank address book format git-svn-id: https://zxing.googlecode.com/svn/trunk@249 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- .../zxing/client/android/ResultHandler.java | 19 ++- .../client/result/AbstractDoCoMoResult.java | 23 +++- .../client/result/AddressBookAUResult.java | 111 ++++++++++++++++++ .../result/AddressBookDoCoMoResult.java | 4 +- .../client/result/ParsedReaderResult.java | 2 + .../client/result/ParsedReaderResultType.java | 1 + .../result/ParsedReaderResultTestCase.java | 6 + 7 files changed, 158 insertions(+), 8 deletions(-) create mode 100644 core/src/com/google/zxing/client/result/AddressBookAUResult.java diff --git a/android/src/com/google/zxing/client/android/ResultHandler.java b/android/src/com/google/zxing/client/android/ResultHandler.java index 0f20b2df7..9d8f1b795 100755 --- a/android/src/com/google/zxing/client/android/ResultHandler.java +++ b/android/src/com/google/zxing/client/android/ResultHandler.java @@ -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]); + } + } + } diff --git a/core/src/com/google/zxing/client/result/AbstractDoCoMoResult.java b/core/src/com/google/zxing/client/result/AbstractDoCoMoResult.java index f9436a044..88a402489 100644 --- a/core/src/com/google/zxing/client/result/AbstractDoCoMoResult.java +++ b/core/src/com/google/zxing/client/result/AbstractDoCoMoResult.java @@ -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 index 000000000..46409bbac --- /dev/null +++ b/core/src/com/google/zxing/client/result/AddressBookAUResult.java @@ -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 + * + * http://www.au.kddi.com/ezfactory/tec/two_dimensions/index.html. + * (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 diff --git a/core/src/com/google/zxing/client/result/AddressBookDoCoMoResult.java b/core/src/com/google/zxing/client/result/AddressBookDoCoMoResult.java index f57a3c7e1..2150d33b5 100644 --- a/core/src/com/google/zxing/client/result/AddressBookDoCoMoResult.java +++ b/core/src/com/google/zxing/client/result/AddressBookDoCoMoResult.java @@ -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(); } diff --git a/core/src/com/google/zxing/client/result/ParsedReaderResult.java b/core/src/com/google/zxing/client/result/ParsedReaderResult.java index 18950c517..f1e8e5ce4 100644 --- a/core/src/com/google/zxing/client/result/ParsedReaderResult.java +++ b/core/src/com/google/zxing/client/result/ParsedReaderResult.java @@ -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) { diff --git a/core/src/com/google/zxing/client/result/ParsedReaderResultType.java b/core/src/com/google/zxing/client/result/ParsedReaderResultType.java index 48225ae25..712e95540 100644 --- a/core/src/com/google/zxing/client/result/ParsedReaderResultType.java +++ b/core/src/com/google/zxing/client/result/ParsedReaderResultType.java @@ -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"); diff --git a/core/test/src/com/google/zxing/client/result/ParsedReaderResultTestCase.java b/core/test/src/com/google/zxing/client/result/ParsedReaderResultTestCase.java index b8a6e4e87..dc28f71f9 100644 --- a/core/test/src/com/google/zxing/client/result/ParsedReaderResultTestCase.java +++ b/core/test/src/com/google/zxing/client/result/ParsedReaderResultTestCase.java @@ -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);