Add support for tel: URIs

git-svn-id: https://zxing.googlecode.com/svn/trunk@292 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2008-03-17 20:17:20 +00:00
parent 5248fb1a7e
commit 379c3a8cc0
8 changed files with 92 additions and 4 deletions

View 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>

View 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;
}

View file

@ -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 {

View 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) {

View 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");

View file

@ -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;
}
}

View 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());

View 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());
}