Small updates and improvements to the Android client.
[zxing.git] / android / src / com / android / barcodes / ResultHandler.java
1 /*
2  * Copyright (C) 2008 Google Inc.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.android.barcodes;
18
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;
36
37 /**
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.
40  */
41 final class ResultHandler implements Button.OnClickListener {
42
43     private static final String TAG = "ResultHandler";
44
45     private final Intent mIntent;
46     private final BarcodesCaptureActivity mCaptureActivity;
47
48     ResultHandler(BarcodesCaptureActivity captureActivity, ParsedResult result) {
49         mCaptureActivity = captureActivity;
50         mIntent = resultToIntent(result);
51     }
52
53     public void onClick(View view) {
54         if (mIntent != null) {
55             mCaptureActivity.startActivity(mIntent);
56         }
57     }
58
59     public Intent getIntent() {
60         return mIntent;
61     }
62
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;
74                 }
75             }
76         }
77         return result;
78     }
79
80     public static int getActionButtonText(ParsedResultType type) {
81         int buttonText;
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.UPC)) {
89             buttonText = R.string.button_lookup_product;
90         } else if (type.equals(ParsedResultType.TEL)) {
91             buttonText = R.string.button_dial;
92         } else if (type.equals(ParsedResultType.GEO)) {
93             buttonText = R.string.button_show_map;
94         } else if (type.equals(ParsedResultType.ISBN)) {
95             buttonText = R.string.button_lookup_book;
96         } else {
97             buttonText = 0;
98         }
99         return buttonText;
100     }
101
102     private static Intent resultToIntent(ParsedResult result) {
103         Intent intent = null;
104         ParsedResultType type = result.getType();
105         if (type.equals(ParsedResultType.ADDRESSBOOK)) {
106             AddressBookParsedResult addressResult = (AddressBookParsedResult) result;
107             intent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
108             putExtra(intent, Contacts.Intents.Insert.NAME, addressResult.getNames());
109             putExtra(intent, Contacts.Intents.Insert.PHONE, addressResult.getPhoneNumbers());
110             putExtra(intent, Contacts.Intents.Insert.EMAIL, addressResult.getEmails());
111             putExtra(intent, Contacts.Intents.Insert.NOTES, addressResult.getNote());
112             putExtra(intent, Contacts.Intents.Insert.POSTAL, addressResult.getAddress());
113             putExtra(intent, Contacts.Intents.Insert.COMPANY, addressResult.getOrg());
114             putExtra(intent, Contacts.Intents.Insert.JOB_TITLE, addressResult.getTitle());
115         } else if (type.equals(ParsedResultType.EMAIL_ADDRESS)) {
116             EmailAddressParsedResult emailResult = (EmailAddressParsedResult) result;
117             intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(emailResult.getMailtoURI()));
118             putExtra(intent, "subject", emailResult.getSubject());
119             putExtra(intent, "body", emailResult.getBody());
120         } else if (type.equals(ParsedResultType.SMS)) {
121             SMSParsedResult smsResult = (SMSParsedResult) result;
122             intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(smsResult.getSMSURI()));
123             putExtra(intent, "subject", smsResult.getSubject());
124             putExtra(intent, "body", smsResult.getBody());
125         } else if (type.equals(ParsedResultType.TEL)) {
126             TelParsedResult telResult = (TelParsedResult) result;
127             intent = new Intent(Intent.ACTION_DIAL, Uri.parse(telResult.getTelURI()));
128         } else if (type.equals(ParsedResultType.GEO)) {
129             GeoParsedResult geoResult = (GeoParsedResult) result;
130             intent = new Intent(Intent.ACTION_VIEW, Uri.parse(geoResult.getGeoURI()));
131         } else if (type.equals(ParsedResultType.UPC)) {
132             UPCParsedResult upcResult = (UPCParsedResult) result;
133             Uri uri = Uri.parse("http://www.google.com/products?q=" + upcResult.getUPC());
134             intent = new Intent(Intent.ACTION_VIEW, uri);
135         } else if (type.equals(ParsedResultType.ISBN)) {
136             ISBNParsedResult isbnResult = (ISBNParsedResult) result;
137             Uri uri = Uri.parse("http://www.google.com/products?q=" + isbnResult.getISBN());
138             intent = new Intent(Intent.ACTION_VIEW, uri);
139         } else if (type.equals(ParsedResultType.URI)) {
140             URIParsedResult uriResult = (URIParsedResult) result;
141             intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uriResult.getURI()));
142         } else if (type.equals(ParsedResultType.ANDROID_INTENT)) {
143             intent = ((AndroidIntentParsedResult) result).getIntent();
144         }
145         return intent;
146     }
147
148     private static void putExtra(Intent intent, String key, String value) {
149         if (value != null && value.length() > 0) {
150             intent.putExtra(key, value);
151         }
152     }
153
154     private static void putExtra(Intent intent, String key, String[] value) {
155         if (value != null && value.length > 0) {
156             putExtra(intent, key, value[0]);
157         }
158     }
159
160 }