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.DialogInterface;
20 import android.content.Intent;
21 import android.net.Uri;
22 import android.provider.Contacts;
23 import com.google.zxing.client.result.AddressBookAUParsedResult;
24 import com.google.zxing.client.result.AddressBookDoCoMoParsedResult;
25 import com.google.zxing.client.result.BookmarkDoCoMoParsedResult;
26 import com.google.zxing.client.result.EmailAddressParsedResult;
27 import com.google.zxing.client.result.EmailDoCoMoParsedResult;
28 import com.google.zxing.client.result.GeoParsedResult;
29 import com.google.zxing.client.result.ParsedReaderResult;
30 import com.google.zxing.client.result.ParsedReaderResultType;
31 import com.google.zxing.client.result.TelParsedResult;
32 import com.google.zxing.client.result.UPCParsedResult;
33 import com.google.zxing.client.result.URIParsedResult;
34 import com.google.zxing.client.result.URLTOParsedResult;
37 * Handles the result of barcode decoding in the context of the Android platform,
38 * by dispatching the proper intents and so on.
40 * @author srowen@google.com (Sean Owen)
41 * @author dswitkin@google.com (Daniel Switkin)
43 final class ResultHandler implements DialogInterface.OnClickListener {
45 private final Intent intent;
46 private final BarcodeReaderCaptureActivity captureActivity;
48 ResultHandler(BarcodeReaderCaptureActivity captureActivity, ParsedReaderResult result) {
49 this.captureActivity = captureActivity;
50 this.intent = resultToIntent(result);
53 private static Intent resultToIntent(ParsedReaderResult result) {
55 ParsedReaderResultType type = result.getType();
56 if (type.equals(ParsedReaderResultType.ADDRESSBOOK)) {
57 AddressBookDoCoMoParsedResult addressResult = (AddressBookDoCoMoParsedResult) result;
58 intent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
59 putExtra(intent, Contacts.Intents.Insert.NAME, addressResult.getName());
60 putExtra(intent, Contacts.Intents.Insert.PHONE, addressResult.getPhoneNumbers());
61 putExtra(intent, Contacts.Intents.Insert.EMAIL, addressResult.getEmail());
62 putExtra(intent, Contacts.Intents.Insert.NOTES, addressResult.getNote());
63 putExtra(intent, Contacts.Intents.Insert.POSTAL, addressResult.getAddress());
64 } else if (type.equals(ParsedReaderResultType.ADDRESSBOOK_AU)) {
65 AddressBookAUParsedResult addressResult = (AddressBookAUParsedResult) result;
66 intent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
67 putExtra(intent, Contacts.Intents.Insert.NAME, addressResult.getNames());
68 putExtra(intent, Contacts.Intents.Insert.PHONE, addressResult.getPhoneNumbers());
69 putExtra(intent, Contacts.Intents.Insert.EMAIL, addressResult.getEmails());
70 putExtra(intent, Contacts.Intents.Insert.NOTES, addressResult.getNote());
71 putExtra(intent, Contacts.Intents.Insert.POSTAL, addressResult.getAddress());
72 } else if (type.equals(ParsedReaderResultType.BOOKMARK)) {
73 // For now, we can only open the browser, and not actually add a bookmark
74 intent = new Intent(Intent.VIEW_ACTION, Uri.parse(((BookmarkDoCoMoParsedResult) result).getURI()));
75 } else if (type.equals(ParsedReaderResultType.URLTO)) {
76 intent = new Intent(Intent.VIEW_ACTION, Uri.parse(((URLTOParsedResult) result).getURI()));
77 } else if (type.equals(ParsedReaderResultType.EMAIL)) {
78 EmailDoCoMoParsedResult emailResult = (EmailDoCoMoParsedResult) result;
79 intent = new Intent(Intent.SENDTO_ACTION, Uri.parse(emailResult.getTo()));
80 putExtra(intent, "subject", emailResult.getSubject());
81 putExtra(intent, "body", emailResult.getBody());
82 } else if (type.equals(ParsedReaderResultType.EMAIL_ADDRESS)) {
83 EmailAddressParsedResult emailResult = (EmailAddressParsedResult) result;
84 intent = new Intent(Intent.SENDTO_ACTION, Uri.parse(emailResult.getEmailAddress()));
85 } else if (type.equals(ParsedReaderResultType.TEL)) {
86 TelParsedResult telResult = (TelParsedResult) result;
87 intent = new Intent(Intent.DIAL_ACTION, Uri.parse("tel:" + telResult.getNumber()));
88 } else if (type.equals(ParsedReaderResultType.GEO)) {
89 GeoParsedResult geoResult = (GeoParsedResult) result;
90 intent = new Intent(Intent.VIEW_ACTION, Uri.parse(geoResult.getGeoURI()));
91 } else if (type.equals(ParsedReaderResultType.UPC)) {
92 UPCParsedResult upcResult = (UPCParsedResult) result;
93 Uri uri = Uri.parse("http://www.upcdatabase.com/item.asp?upc=" + upcResult.getUPC());
94 intent = new Intent(Intent.VIEW_ACTION, uri);
95 } else if (type.equals(ParsedReaderResultType.URI)) {
96 URIParsedResult uriResult = (URIParsedResult) result;
97 intent = new Intent(Intent.VIEW_ACTION, Uri.parse(uriResult.getURI()));
98 } else if (type.equals(ParsedReaderResultType.ANDROID_INTENT)) {
99 intent = ((AndroidIntentParsedResult) result).getIntent();
104 public void onClick(DialogInterface dialogInterface, int i) {
105 if (i == DialogInterface.BUTTON1) {
106 if (intent != null) {
107 captureActivity.startActivity(intent);
110 captureActivity.restartPreview();
118 private static void putExtra(Intent intent, String key, String value) {
119 if (value != null && value.length() > 0) {
120 intent.putExtra(key, value);
124 private static void putExtra(Intent intent, String key, String[] value) {
125 if (value != null && value.length > 0) {
126 putExtra(intent, key, value[0]);