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" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.zxing.client.android" package="com.google.zxing.client.android"
android:versionName="4.7.0" android:versionName="4.7.1"
android:versionCode="100" android:versionCode="101"
android:installLocation="auto"> android:installLocation="auto">
<uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.CAMERA"/>

View file

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

View file

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

View file

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

View file

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

View file

@ -21,24 +21,48 @@ package com.google.zxing.client.result;
*/ */
public final class EmailAddressParsedResult extends ParsedResult { 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 subject;
private final String body; private final String body;
private final String mailtoURI;
EmailAddressParsedResult(String emailAddress, EmailAddressParsedResult(String to) {
String subject, this(new String[] {to}, null, null, null, null);
String body,
String mailtoURI) {
super(ParsedResultType.EMAIL_ADDRESS);
this.emailAddress = emailAddress;
this.subject = subject;
this.body = body;
this.mailtoURI = mailtoURI;
} }
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() { 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() { public String getSubject() {
@ -49,14 +73,20 @@ public final class EmailAddressParsedResult extends ParsedResult {
return body; return body;
} }
/**
* @deprecated without replacement
*/
@Deprecated
public String getMailtoURI() { public String getMailtoURI() {
return mailtoURI; return "mailto:";
} }
@Override @Override
public String getDisplayResult() { public String getDisplayResult() {
StringBuilder result = new StringBuilder(30); StringBuilder result = new StringBuilder(30);
maybeAppend(emailAddress, result); maybeAppend(tos, result);
maybeAppend(ccs, result);
maybeAppend(bccs, result);
maybeAppend(subject, result); maybeAppend(subject, result);
maybeAppend(body, result); maybeAppend(body, result);
return result.toString(); return result.toString();

View file

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

View file

@ -37,17 +37,18 @@ public final class EmailDoCoMoResultParser extends AbstractDoCoMoResultParser {
if (!rawText.startsWith("MATMSG:")) { if (!rawText.startsWith("MATMSG:")) {
return null; return null;
} }
String[] rawTo = matchDoCoMoPrefixedField("TO:", rawText, true); String[] tos = matchDoCoMoPrefixedField("TO:", rawText, true);
if (rawTo == null) { if (tos == null) {
return null; return null;
} }
String to = rawTo[0]; for (String to : tos) {
if (!isBasicallyValidEmailAddress(to)) { if (!isBasicallyValidEmailAddress(to)) {
return null; return null;
} }
}
String subject = matchSingleDoCoMoPrefixedField("SUB:", rawText, false); String subject = matchSingleDoCoMoPrefixedField("SUB:", rawText, false);
String body = matchSingleDoCoMoPrefixedField("BODY:", 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); subject = subject.substring(0, colon);
} }
} }
String mailtoURI = "mailto:" + emailAddress; return new EmailAddressParsedResult(new String[] {emailAddress},
return new EmailAddressParsedResult(emailAddress, subject, body, mailtoURI); 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); 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 @Test
public void testEmailDocomo() { public void testEmailDocomo() {
doTest("MATMSG:TO:srowen@example.org;;", "srowen@example.org", null, null); 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, 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 subject,
String body) { String body) {
Result fakeResult = new Result(contents, null, null, BarcodeFormat.QR_CODE); Result fakeResult = new Result(contents, null, null, BarcodeFormat.QR_CODE);
ParsedResult result = ResultParser.parseResult(fakeResult); ParsedResult result = ResultParser.parseResult(fakeResult);
assertSame(ParsedResultType.EMAIL_ADDRESS, result.getType()); assertSame(ParsedResultType.EMAIL_ADDRESS, result.getType());
EmailAddressParsedResult emailResult = (EmailAddressParsedResult) result; EmailAddressParsedResult emailResult = (EmailAddressParsedResult) result;
assertEquals(email, emailResult.getEmailAddress()); assertArrayEquals(tos, emailResult.getTos());
assertEquals("mailto:" + email, emailResult.getMailtoURI()); assertArrayEquals(ccs, emailResult.getCCs());
assertArrayEquals(bccs, emailResult.getBCCs());
assertEquals(subject, emailResult.getSubject()); assertEquals(subject, emailResult.getSubject());
assertEquals(body, emailResult.getBody()); assertEquals(body, emailResult.getBody());
} }

10
pom.xml
View file

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