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.ContentURI;
21 import android.os.Handler;
22 import android.os.Message;
23 import android.provider.Contacts;
24 import com.google.zxing.client.result.AddressBookAUResult;
25 import com.google.zxing.client.result.AddressBookDoCoMoResult;
26 import com.google.zxing.client.result.BookmarkDoCoMoResult;
27 import com.google.zxing.client.result.EmailAddressResult;
28 import com.google.zxing.client.result.EmailDoCoMoResult;
29 import com.google.zxing.client.result.ParsedReaderResult;
30 import com.google.zxing.client.result.ParsedReaderResultType;
31 import com.google.zxing.client.result.UPCParsedResult;
32 import com.google.zxing.client.result.URIParsedResult;
33 import com.google.zxing.client.result.URLTOResult;
35 import java.net.URISyntaxException;
38 * Handles the result of barcode decoding in the context of the Android platform,
39 * by dispatching the proper intents and so on.
41 * @author srowen@google.com (Sean Owen)
42 * @author dswitkin@google.com (Daniel Switkin)
44 final class ResultHandler extends Handler {
46 private final ParsedReaderResult result;
47 private final BarcodeReaderCaptureActivity captureActivity;
49 ResultHandler(BarcodeReaderCaptureActivity captureActivity, ParsedReaderResult result) {
50 this.captureActivity = captureActivity;
55 public void handleMessage(Message message) {
56 if (message.what == R.string.button_yes) {
58 ParsedReaderResultType type = result.getType();
59 if (type.equals(ParsedReaderResultType.ADDRESSBOOK)) {
60 AddressBookDoCoMoResult addressResult = (AddressBookDoCoMoResult) result;
61 intent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
62 putExtra(intent, Contacts.Intents.Insert.NAME, addressResult.getName());
63 putExtra(intent, Contacts.Intents.Insert.PHONE, addressResult.getPhoneNumbers());
64 putExtra(intent, Contacts.Intents.Insert.EMAIL, addressResult.getEmail());
65 putExtra(intent, Contacts.Intents.Insert.NOTES, addressResult.getNote());
66 putExtra(intent, Contacts.Intents.Insert.POSTAL, addressResult.getAddress());
67 } else if (type.equals(ParsedReaderResultType.ADDRESSBOOK_AU)) {
68 AddressBookAUResult addressResult = (AddressBookAUResult) result;
69 intent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
70 putExtra(intent, Contacts.Intents.Insert.NAME, addressResult.getNames());
71 putExtra(intent, Contacts.Intents.Insert.PHONE, addressResult.getPhoneNumbers());
72 putExtra(intent, Contacts.Intents.Insert.EMAIL, addressResult.getEmails());
73 putExtra(intent, Contacts.Intents.Insert.NOTES, addressResult.getNote());
74 putExtra(intent, Contacts.Intents.Insert.POSTAL, addressResult.getAddress());
75 } else if (type.equals(ParsedReaderResultType.BOOKMARK)) {
76 // For now, we can only open the browser, and not actually add a bookmark
78 intent = new Intent(Intent.VIEW_ACTION,
79 new ContentURI(((BookmarkDoCoMoResult) result).getURI()));
80 } catch (URISyntaxException e) {
83 } else if (type.equals(ParsedReaderResultType.URLTO)) {
85 intent = new Intent(Intent.VIEW_ACTION,
86 new ContentURI(((URLTOResult) result).getURI()));
87 } catch (URISyntaxException e) {
90 } else if (type.equals(ParsedReaderResultType.EMAIL)) {
91 EmailDoCoMoResult emailResult = (EmailDoCoMoResult) result;
93 intent = new Intent(Intent.SENDTO_ACTION, new ContentURI(emailResult.getTo()));
94 } catch (URISyntaxException e) {
97 putExtra(intent, "subject", emailResult.getSubject());
98 putExtra(intent, "body", emailResult.getBody());
99 } else if (type.equals(ParsedReaderResultType.EMAIL_ADDRESS)) {
100 EmailAddressResult emailResult = (EmailAddressResult) result;
102 intent = new Intent(Intent.SENDTO_ACTION, new ContentURI(emailResult.getEmailAddress()));
103 } catch (URISyntaxException e) {
106 } else if (type.equals(ParsedReaderResultType.UPC)) {
107 UPCParsedResult upcResult = (UPCParsedResult) result;
109 ContentURI uri = new ContentURI("http://www.upcdatabase.com/item.asp?upc=" +
111 intent = new Intent(Intent.VIEW_ACTION, uri);
112 } catch (URISyntaxException e) {
115 } else if (type.equals(ParsedReaderResultType.URI)) {
116 URIParsedResult uriResult = (URIParsedResult) result;
118 intent = new Intent(Intent.VIEW_ACTION, new ContentURI(uriResult.getURI()));
119 } catch (URISyntaxException e) {
122 } else if (type.equals(ParsedReaderResultType.ANDROID_INTENT)) {
123 intent = ((AndroidIntentParsedResult) result).getIntent();
125 if (intent != null) {
126 captureActivity.startActivity(intent);
129 captureActivity.restartPreview();
133 private static void putExtra(Intent intent, String key, String value) {
134 if (value != null && value.length() > 0) {
135 intent.putExtra(key, value);
139 private static void putExtra(Intent intent, String key, String[] value) {
140 if (value != null && value.length > 0) {
141 putExtra(intent, key, value[0]);