Merge pull request #193 from zxing/Issue192

Support multiple "to", and CC and BCC, throughout the email result API.
This commit is contained in:
Sean Owen 2014-07-25 22:54:23 +01:00
commit 8e86c7b7dc
11 changed files with 172 additions and 62 deletions

View file

@ -16,8 +16,8 @@
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.zxing.client.android"
android:versionName="4.7.0"
android:versionCode="100"
android:versionName="4.7.1"
android:versionCode="101"
android:installLocation="auto">
<uses-permission android:name="android.permission.CAMERA"/>

View file

@ -18,7 +18,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>android</artifactId>
<version>4.7.0</version>
<version>4.7.1</version>
<packaging>apk</packaging>
<dependencies>
@ -39,7 +39,7 @@
<parent>
<groupId>com.google.zxing</groupId>
<artifactId>zxing-parent</artifactId>
<version>3.1.0</version>
<version>3.1.1-SNAPSHOT</version>
</parent>
<build>

View file

@ -146,7 +146,7 @@ public final class AddressBookResultHandler extends ResultHandler {
dialPhone(addressResult.getPhoneNumbers()[0]);
break;
case 3:
sendEmail(addressResult.getEmails()[0], null, null);
sendEmail(addressResult.getEmails(), null, null, null, null);
break;
default:
break;

View file

@ -52,15 +52,14 @@ public final class EmailAddressResultHandler extends ResultHandler {
EmailAddressParsedResult emailResult = (EmailAddressParsedResult) getResult();
switch (index) {
case 0:
sendEmailFromUri(emailResult.getMailtoURI(),
emailResult.getEmailAddress(),
sendEmail(emailResult.getTos(),
emailResult.getCCs(),
emailResult.getBCCs(),
emailResult.getSubject(),
emailResult.getBody());
break;
case 1:
String[] addresses = new String[1];
addresses[0] = emailResult.getEmailAddress();
addEmailOnlyContact(addresses, null);
addEmailOnlyContact(emailResult.getTos(), null);
break;
}
}

View file

@ -317,18 +317,23 @@ public abstract class ResultHandler {
}
final void shareByEmail(String contents) {
sendEmailFromUri("mailto:", null, null, contents);
sendEmail(null, null, null, null, contents);
}
final void sendEmail(String address, String subject, String body) {
sendEmailFromUri("mailto:" + address, address, subject, body);
final void sendEmail(String[] to,
String[] cc,
String[] bcc,
String subject,
String body) {
Intent intent = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));
if (to != null && to.length != 0) {
intent.putExtra(Intent.EXTRA_EMAIL, to);
}
// Use public Intent fields rather than private GMail app fields to specify subject and body.
final void sendEmailFromUri(String uri, String email, String subject, String body) {
Intent intent = new Intent(Intent.ACTION_SEND, Uri.parse(uri));
if (email != null) {
intent.putExtra(Intent.EXTRA_EMAIL, new String[] {email});
if (cc != null && cc.length != 0) {
intent.putExtra(Intent.EXTRA_CC, cc);
}
if (bcc != null && bcc.length != 0) {
intent.putExtra(Intent.EXTRA_BCC, bcc);
}
putExtra(intent, Intent.EXTRA_SUBJECT, subject);
putExtra(intent, Intent.EXTRA_TEXT, body);

View file

@ -21,24 +21,48 @@ package com.google.zxing.client.result;
*/
public final class EmailAddressParsedResult extends ParsedResult {
private final String emailAddress;
private final String[] tos;
private final String[] ccs;
private final String[] bccs;
private final String subject;
private final String body;
private final String mailtoURI;
EmailAddressParsedResult(String emailAddress,
String subject,
String body,
String mailtoURI) {
super(ParsedResultType.EMAIL_ADDRESS);
this.emailAddress = emailAddress;
this.subject = subject;
this.body = body;
this.mailtoURI = mailtoURI;
EmailAddressParsedResult(String to) {
this(new String[] {to}, null, null, null, null);
}
EmailAddressParsedResult(String[] tos,
String[] ccs,
String[] bccs,
String subject,
String body) {
super(ParsedResultType.EMAIL_ADDRESS);
this.tos = tos;
this.ccs = ccs;
this.bccs = bccs;
this.subject = subject;
this.body = body;
}
/**
* @return first elements of {@link #getTos()} or {@code null} if none
* @deprecated use {@link #getTos()}
*/
@Deprecated
public String getEmailAddress() {
return emailAddress;
return tos == null || tos.length == 0 ? null : tos[0];
}
public String[] getTos() {
return tos;
}
public String[] getCCs() {
return ccs;
}
public String[] getBCCs() {
return bccs;
}
public String getSubject() {
@ -49,14 +73,20 @@ public final class EmailAddressParsedResult extends ParsedResult {
return body;
}
/**
* @deprecated without replacement
*/
@Deprecated
public String getMailtoURI() {
return mailtoURI;
return "mailto:";
}
@Override
public String getDisplayResult() {
StringBuilder result = new StringBuilder(30);
maybeAppend(emailAddress, result);
maybeAppend(tos, result);
maybeAppend(ccs, result);
maybeAppend(bccs, result);
maybeAppend(subject, result);
maybeAppend(body, result);
return result.toString();

View file

@ -19,6 +19,7 @@ package com.google.zxing.client.result;
import com.google.zxing.Result;
import java.util.Map;
import java.util.regex.Pattern;
/**
* Represents a result that encodes an e-mail address, either as a plain address
@ -28,35 +29,52 @@ import java.util.Map;
*/
public final class EmailAddressResultParser extends ResultParser {
private static final Pattern COMMA = Pattern.compile(",");
@Override
public EmailAddressParsedResult parse(Result result) {
String rawText = getMassagedText(result);
String emailAddress;
if (rawText.startsWith("mailto:") || rawText.startsWith("MAILTO:")) {
// If it starts with mailto:, assume it is definitely trying to be an email address
emailAddress = rawText.substring(7);
int queryStart = emailAddress.indexOf('?');
String hostEmail = rawText.substring(7);
int queryStart = hostEmail.indexOf('?');
if (queryStart >= 0) {
emailAddress = emailAddress.substring(0, queryStart);
hostEmail = hostEmail.substring(0, queryStart);
}
hostEmail = urlDecode(hostEmail);
String[] tos = null;
if (!hostEmail.isEmpty()) {
tos = COMMA.split(hostEmail);
}
emailAddress = urlDecode(emailAddress);
Map<String,String> nameValues = parseNameValuePairs(rawText);
String[] ccs = null;
String[] bccs = null;
String subject = null;
String body = null;
if (nameValues != null) {
if (emailAddress.isEmpty()) {
emailAddress = nameValues.get("to");
if (tos == null) {
String tosString = nameValues.get("to");
if (tosString != null) {
tos = COMMA.split(tosString);
}
}
String ccString = nameValues.get("cc");
if (ccString != null) {
ccs = COMMA.split(ccString);
}
String bccString = nameValues.get("bcc");
if (bccString != null) {
bccs = COMMA.split(bccString);
}
subject = nameValues.get("subject");
body = nameValues.get("body");
}
return new EmailAddressParsedResult(emailAddress, subject, body, rawText);
return new EmailAddressParsedResult(tos, ccs, bccs, subject, body);
} else {
if (!EmailDoCoMoResultParser.isBasicallyValidEmailAddress(rawText)) {
return null;
}
emailAddress = rawText;
return new EmailAddressParsedResult(emailAddress, null, null, "mailto:" + emailAddress);
return new EmailAddressParsedResult(rawText);
}
}

View file

@ -37,17 +37,18 @@ public final class EmailDoCoMoResultParser extends AbstractDoCoMoResultParser {
if (!rawText.startsWith("MATMSG:")) {
return null;
}
String[] rawTo = matchDoCoMoPrefixedField("TO:", rawText, true);
if (rawTo == null) {
String[] tos = matchDoCoMoPrefixedField("TO:", rawText, true);
if (tos == null) {
return null;
}
String to = rawTo[0];
for (String to : tos) {
if (!isBasicallyValidEmailAddress(to)) {
return null;
}
}
String subject = matchSingleDoCoMoPrefixedField("SUB:", rawText, false);
String body = matchSingleDoCoMoPrefixedField("BODY:", rawText, false);
return new EmailAddressParsedResult(to, subject, body, "mailto:" + to);
return new EmailAddressParsedResult(tos, null, null, subject, body);
}
/**

View file

@ -45,7 +45,10 @@ public final class SMTPResultParser extends ResultParser {
subject = subject.substring(0, colon);
}
}
String mailtoURI = "mailto:" + emailAddress;
return new EmailAddressParsedResult(emailAddress, subject, body, mailtoURI);
return new EmailAddressParsedResult(new String[] {emailAddress},
null,
null,
subject,
body);
}
}

View file

@ -34,6 +34,50 @@ public final class EmailAddressParsedResultTestCase extends Assert {
doTest("mailto:srowen@example.org", "srowen@example.org", null, null);
}
@Test
public void testTos() {
doTest("mailto:srowen@example.org,bob@example.org",
new String[] {"srowen@example.org", "bob@example.org"},
null, null, null, null);
doTest("mailto:?to=srowen@example.org,bob@example.org",
new String[] {"srowen@example.org", "bob@example.org"},
null, null, null, null);
}
@Test
public void testCCs() {
doTest("mailto:?cc=srowen@example.org",
null,
new String[] {"srowen@example.org"},
null, null, null);
doTest("mailto:?cc=srowen@example.org,bob@example.org",
null,
new String[] {"srowen@example.org", "bob@example.org"},
null, null, null);
}
@Test
public void testBCCs() {
doTest("mailto:?bcc=srowen@example.org",
null, null,
new String[] {"srowen@example.org"},
null, null);
doTest("mailto:?bcc=srowen@example.org,bob@example.org",
null, null,
new String[] {"srowen@example.org", "bob@example.org"},
null, null);
}
@Test
public void testAll() {
doTest("mailto:bob@example.org?cc=foo@example.org&bcc=srowen@example.org&subject=baz&body=buzz",
new String[] {"bob@example.org"},
new String[] {"foo@example.org"},
new String[] {"srowen@example.org"},
"baz",
"buzz");
}
@Test
public void testEmailDocomo() {
doTest("MATMSG:TO:srowen@example.org;;", "srowen@example.org", null, null);
@ -51,15 +95,25 @@ public final class EmailAddressParsedResultTestCase extends Assert {
}
private static void doTest(String contents,
String email,
String to,
String subject,
String body) {
doTest(contents, new String[] {to}, null, null, subject, body);
}
private static void doTest(String contents,
String[] tos,
String[] ccs,
String[] bccs,
String subject,
String body) {
Result fakeResult = new Result(contents, null, null, BarcodeFormat.QR_CODE);
ParsedResult result = ResultParser.parseResult(fakeResult);
assertSame(ParsedResultType.EMAIL_ADDRESS, result.getType());
EmailAddressParsedResult emailResult = (EmailAddressParsedResult) result;
assertEquals(email, emailResult.getEmailAddress());
assertEquals("mailto:" + email, emailResult.getMailtoURI());
assertArrayEquals(tos, emailResult.getTos());
assertArrayEquals(ccs, emailResult.getCCs());
assertArrayEquals(bccs, emailResult.getBCCs());
assertEquals(subject, emailResult.getSubject());
assertEquals(body, emailResult.getBody());
}

10
pom.xml
View file

@ -242,7 +242,7 @@
<dependency>
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm-provider-gitexe</artifactId>
<version>1.9</version>
<version>1.9.1</version>
</dependency>
</dependencies>
<configuration>
@ -255,7 +255,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<version>1.9</version>
<version>1.9.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
@ -309,7 +309,7 @@
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>3.9.0-rc.2</version>
<version>3.9.0-rc.3</version>
<extensions>true</extensions>
<executions>
<execution>
@ -362,7 +362,7 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>2.5.4</version>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
@ -451,7 +451,7 @@
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<!-- Work around bogus Maven 3.0.x warning by duplicating version above -->
<version>2.5.4</version>
<version>3.0.0</version>
<configuration>
<threshold>Normal</threshold>
<effort>Max</effort>