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