2 * Copyright (C) 2008 Google Inc.
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.util.Log;
23 import android.view.View;
24 import android.widget.Button;
25 import com.google.zxing.client.result.*;
28 * Handles the result of barcode decoding in the context of the Android platform,
29 * by dispatching the proper intents to open other activities like GMail, Maps, etc.
31 * @author srowen@google.com (Sean Owen)
32 * @author dswitkin@google.com (Daniel Switkin)
34 final class ResultHandler implements Button.OnClickListener {
36 private static final String TAG = "ResultHandler";
38 private final Intent intent;
39 private final BarcodeReaderCaptureActivity captureActivity;
41 ResultHandler(BarcodeReaderCaptureActivity captureActivity, ParsedReaderResult result) {
42 this.captureActivity = captureActivity;
43 this.intent = resultToIntent(result);
46 private static Intent resultToIntent(ParsedReaderResult result) {
48 ParsedReaderResultType type = result.getType();
49 if (type.equals(ParsedReaderResultType.ADDRESSBOOK)) {
50 AddressBookDoCoMoParsedResult addressResult = (AddressBookDoCoMoParsedResult) result;
51 intent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
52 putExtra(intent, Contacts.Intents.Insert.NAME, addressResult.getName());
53 putExtra(intent, Contacts.Intents.Insert.PHONE, addressResult.getPhoneNumbers());
54 putExtra(intent, Contacts.Intents.Insert.EMAIL, addressResult.getEmail());
55 putExtra(intent, Contacts.Intents.Insert.NOTES, addressResult.getNote());
56 putExtra(intent, Contacts.Intents.Insert.POSTAL, addressResult.getAddress());
57 } else if (type.equals(ParsedReaderResultType.ADDRESSBOOK_AU)) {
58 AddressBookAUParsedResult addressResult = (AddressBookAUParsedResult) result;
59 intent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
60 putExtra(intent, Contacts.Intents.Insert.NAME, addressResult.getNames());
61 putExtra(intent, Contacts.Intents.Insert.PHONE, addressResult.getPhoneNumbers());
62 putExtra(intent, Contacts.Intents.Insert.EMAIL, addressResult.getEmails());
63 putExtra(intent, Contacts.Intents.Insert.NOTES, addressResult.getNote());
64 putExtra(intent, Contacts.Intents.Insert.POSTAL, addressResult.getAddress());
65 } else if (type.equals(ParsedReaderResultType.BOOKMARK)) {
66 // For now, we can only open the browser, and not actually add a bookmark
67 intent = new Intent(Intent.VIEW_ACTION, Uri.parse(((BookmarkDoCoMoParsedResult) result).getURI()));
68 } else if (type.equals(ParsedReaderResultType.URLTO)) {
69 intent = new Intent(Intent.VIEW_ACTION, Uri.parse(((URLTOParsedResult) result).getURI()));
70 } else if (type.equals(ParsedReaderResultType.EMAIL)) {
71 EmailDoCoMoParsedResult emailResult = (EmailDoCoMoParsedResult) result;
72 intent = new Intent(Intent.SENDTO_ACTION, Uri.parse(emailResult.getTo()));
73 putExtra(intent, "subject", emailResult.getSubject());
74 putExtra(intent, "body", emailResult.getBody());
75 } else if (type.equals(ParsedReaderResultType.EMAIL_ADDRESS)) {
76 EmailAddressParsedResult emailResult = (EmailAddressParsedResult) result;
77 intent = new Intent(Intent.SENDTO_ACTION, Uri.parse("mailto:" + emailResult.getEmailAddress()));
78 } else if (type.equals(ParsedReaderResultType.TEL)) {
79 TelParsedResult telResult = (TelParsedResult) result;
80 intent = new Intent(Intent.DIAL_ACTION, Uri.parse("tel:" + telResult.getNumber()));
81 } else if (type.equals(ParsedReaderResultType.GEO)) {
82 GeoParsedResult geoResult = (GeoParsedResult) result;
83 intent = new Intent(Intent.VIEW_ACTION, Uri.parse(geoResult.getGeoURI()));
84 } else if (type.equals(ParsedReaderResultType.UPC)) {
85 UPCParsedResult upcResult = (UPCParsedResult) result;
86 Uri uri = Uri.parse("http://www.upcdatabase.com/item.asp?upc=" + upcResult.getUPC());
87 intent = new Intent(Intent.VIEW_ACTION, uri);
88 } else if (type.equals(ParsedReaderResultType.URI)) {
89 URIParsedResult uriResult = (URIParsedResult) result;
90 intent = new Intent(Intent.VIEW_ACTION, Uri.parse(uriResult.getURI()));
91 } else if (type.equals(ParsedReaderResultType.ANDROID_INTENT)) {
92 intent = ((AndroidIntentParsedResult) result).getIntent();
97 public void onClick(View view) {
99 captureActivity.startActivity(intent);
107 private static void putExtra(Intent intent, String key, String value) {
108 if (value != null && value.length() > 0) {
109 intent.putExtra(key, value);
113 private static void putExtra(Intent intent, String key, String[] value) {
114 if (value != null && value.length > 0) {
115 putExtra(intent, key, value[0]);