2 * Copyright 2008 ZXing authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing.client.android;
19 import android.content.Intent;
20 import android.net.Uri;
21 import android.provider.Contacts;
22 import android.view.View;
23 import android.widget.Button;
24 import com.google.zxing.client.result.AddressBookParsedResult;
25 import com.google.zxing.client.result.EmailAddressParsedResult;
26 import com.google.zxing.client.result.GeoParsedResult;
27 import com.google.zxing.client.result.ParsedResult;
28 import com.google.zxing.client.result.ParsedResultType;
29 import com.google.zxing.client.result.SMSParsedResult;
30 import com.google.zxing.client.result.TelParsedResult;
31 import com.google.zxing.client.result.UPCParsedResult;
32 import com.google.zxing.client.result.URIParsedResult;
35 * Handles the result of barcode decoding in the context of the Android platform,
36 * by dispatching the proper intents to open other activities like GMail, Maps, etc.
38 * @author srowen@google.com (Sean Owen)
39 * @author dswitkin@google.com (Daniel Switkin)
41 final class ResultHandler implements Button.OnClickListener {
43 private final Intent intent;
44 private final BarcodeReaderCaptureActivity captureActivity;
46 ResultHandler(BarcodeReaderCaptureActivity captureActivity, ParsedResult result) {
47 this.captureActivity = captureActivity;
48 this.intent = resultToIntent(result);
51 private static Intent resultToIntent(ParsedResult result) {
53 ParsedResultType type = result.getType();
54 if (type.equals(ParsedResultType.ADDRESSBOOK)) {
55 AddressBookParsedResult addressResult = (AddressBookParsedResult) result;
56 intent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
57 putExtra(intent, Contacts.Intents.Insert.NAME, addressResult.getNames());
58 putExtra(intent, Contacts.Intents.Insert.PHONE, addressResult.getPhoneNumbers());
59 putExtra(intent, Contacts.Intents.Insert.EMAIL, addressResult.getEmails());
60 putExtra(intent, Contacts.Intents.Insert.NOTES, addressResult.getNote());
61 putExtra(intent, Contacts.Intents.Insert.POSTAL, addressResult.getAddress());
62 } else if (type.equals(ParsedResultType.EMAIL_ADDRESS)) {
63 EmailAddressParsedResult emailResult = (EmailAddressParsedResult) result;
64 intent = new Intent(Intent.SENDTO_ACTION, Uri.parse(emailResult.getMailtoURI()));
65 putExtra(intent, "subject", emailResult.getSubject());
66 putExtra(intent, "body", emailResult.getBody());
67 } else if (type.equals(ParsedResultType.SMS)) {
68 SMSParsedResult smsResult = (SMSParsedResult) result;
69 intent = new Intent(Intent.SENDTO_ACTION, Uri.parse(smsResult.getSMSURI()));
70 } else if (type.equals(ParsedResultType.TEL)) {
71 TelParsedResult telResult = (TelParsedResult) result;
72 intent = new Intent(Intent.DIAL_ACTION, Uri.parse(telResult.getTelURI()));
73 } else if (type.equals(ParsedResultType.GEO)) {
74 GeoParsedResult geoResult = (GeoParsedResult) result;
75 intent = new Intent(Intent.VIEW_ACTION, Uri.parse(geoResult.getGeoURI()));
76 } else if (type.equals(ParsedResultType.UPC)) {
77 UPCParsedResult upcResult = (UPCParsedResult) result;
78 Uri uri = Uri.parse("http://www.upcdatabase.com/item.asp?upc=" + upcResult.getUPC());
79 intent = new Intent(Intent.VIEW_ACTION, uri);
80 } else if (type.equals(ParsedResultType.URI)) {
81 URIParsedResult uriResult = (URIParsedResult) result;
82 intent = new Intent(Intent.VIEW_ACTION, Uri.parse(uriResult.getURI()));
83 } else if (type.equals(ParsedResultType.ANDROID_INTENT)) {
84 intent = ((AndroidIntentParsedResult) result).getIntent();
89 public void onClick(View view) {
91 captureActivity.startActivity(intent);
99 private static void putExtra(Intent intent, String key, String value) {
100 if (value != null && value.length() > 0) {
101 intent.putExtra(key, value);
105 private static void putExtra(Intent intent, String key, String[] value) {
106 if (value != null && value.length > 0) {
107 putExtra(intent, key, value[0]);