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.android.barcodes;
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.Result;
25 import com.google.zxing.client.result.AddressBookParsedResult;
26 import com.google.zxing.client.result.EmailAddressParsedResult;
27 import com.google.zxing.client.result.GeoParsedResult;
28 import com.google.zxing.client.result.ParsedResult;
29 import com.google.zxing.client.result.ParsedResultType;
30 import com.google.zxing.client.result.ResultParser;
31 import com.google.zxing.client.result.SMSParsedResult;
32 import com.google.zxing.client.result.TelParsedResult;
33 import com.google.zxing.client.result.UPCParsedResult;
34 import com.google.zxing.client.result.URIParsedResult;
37 * Handles the result of barcode decoding in the context of the Android platform, by dispatching the
38 * proper intents to open other activities like GMail, Maps, etc.
40 final class ResultHandler implements Button.OnClickListener {
42 private static final String TAG = "ResultHandler";
44 private final Intent mIntent;
45 private final BarcodesCaptureActivity mCaptureActivity;
47 ResultHandler(BarcodesCaptureActivity captureActivity, ParsedResult result) {
48 mCaptureActivity = captureActivity;
49 mIntent = resultToIntent(result);
52 public void onClick(View view) {
53 if (mIntent != null) {
54 mCaptureActivity.startActivity(mIntent);
58 public Intent getIntent() {
62 public static ParsedResult parseResult(Result rawResult) {
63 ParsedResult result = ResultParser.parseResult(rawResult);
64 if (result.getType().equals(ParsedResultType.TEXT)) {
65 String rawText = rawResult.getText();
66 AndroidIntentParsedResult androidResult = AndroidIntentParsedResult.parse(rawText);
67 if (androidResult != null) {
68 Intent intent = androidResult.getIntent();
69 if (!Intent.ACTION_VIEW.equals(intent.getAction())) {
70 // For now, don't take anything that just parses as a View action. A lot
71 // of things are accepted as a View action by default.
72 result = androidResult;
79 public static int getActionButtonText(ParsedResultType type) {
81 if (type.equals(ParsedResultType.ADDRESSBOOK)) {
82 buttonText = R.string.button_add_contact;
83 } else if (type.equals(ParsedResultType.URI)) {
84 buttonText = R.string.button_open_browser;
85 } else if (type.equals(ParsedResultType.EMAIL_ADDRESS)) {
86 buttonText = R.string.button_email;
87 } else if (type.equals(ParsedResultType.UPC)) {
88 buttonText = R.string.button_lookup_product;
89 } else if (type.equals(ParsedResultType.TEL)) {
90 buttonText = R.string.button_dial;
91 } else if (type.equals(ParsedResultType.GEO)) {
92 buttonText = R.string.button_show_map;
99 private static Intent resultToIntent(ParsedResult result) {
100 Intent intent = null;
101 ParsedResultType type = result.getType();
102 if (type.equals(ParsedResultType.ADDRESSBOOK)) {
103 AddressBookParsedResult addressResult = (AddressBookParsedResult) result;
104 intent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
105 putExtra(intent, Contacts.Intents.Insert.NAME, addressResult.getNames());
106 putExtra(intent, Contacts.Intents.Insert.PHONE, addressResult.getPhoneNumbers());
107 putExtra(intent, Contacts.Intents.Insert.EMAIL, addressResult.getEmails());
108 putExtra(intent, Contacts.Intents.Insert.NOTES, addressResult.getNote());
109 putExtra(intent, Contacts.Intents.Insert.POSTAL, addressResult.getAddress());
110 putExtra(intent, Contacts.Intents.Insert.COMPANY, addressResult.getOrg());
111 putExtra(intent, Contacts.Intents.Insert.JOB_TITLE, addressResult.getTitle());
112 } else if (type.equals(ParsedResultType.EMAIL_ADDRESS)) {
113 EmailAddressParsedResult emailResult = (EmailAddressParsedResult) result;
114 intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(emailResult.getMailtoURI()));
115 putExtra(intent, "subject", emailResult.getSubject());
116 putExtra(intent, "body", emailResult.getBody());
117 } else if (type.equals(ParsedResultType.SMS)) {
118 SMSParsedResult smsResult = (SMSParsedResult) result;
119 intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(smsResult.getSMSURI()));
120 putExtra(intent, "subject", smsResult.getSubject());
121 putExtra(intent, "body", smsResult.getBody());
122 } else if (type.equals(ParsedResultType.TEL)) {
123 TelParsedResult telResult = (TelParsedResult) result;
124 intent = new Intent(Intent.ACTION_DIAL, Uri.parse(telResult.getTelURI()));
125 } else if (type.equals(ParsedResultType.GEO)) {
126 GeoParsedResult geoResult = (GeoParsedResult) result;
127 intent = new Intent(Intent.ACTION_VIEW, Uri.parse(geoResult.getGeoURI()));
128 } else if (type.equals(ParsedResultType.UPC)) {
129 UPCParsedResult upcResult = (UPCParsedResult) result;
130 // TODO: Add some UI to choose which product search to do
131 //Uri uri = Uri.parse("http://www.upcdatabase.com/item.asp?upc=" + upcResult.getUPC());
132 Uri uri = Uri.parse("http://www.google.com/products?q=" + upcResult.getUPC());
133 intent = new Intent(Intent.ACTION_VIEW, uri);
134 } else if (type.equals(ParsedResultType.URI)) {
135 URIParsedResult uriResult = (URIParsedResult) result;
136 intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uriResult.getURI()));
137 } else if (type.equals(ParsedResultType.ANDROID_INTENT)) {
138 intent = ((AndroidIntentParsedResult) result).getIntent();
143 private static void putExtra(Intent intent, String key, String value) {
144 if (value != null && value.length() > 0) {
145 intent.putExtra(key, value);
149 private static void putExtra(Intent intent, String key, String[] value) {
150 if (value != null && value.length > 0) {
151 putExtra(intent, key, value[0]);