Add support for tel: URIs
[zxing.git] / android / src / com / google / zxing / client / android / 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.google.zxing.client.android;
18
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.AddressBookAUParsedResult;
25 import com.google.zxing.client.result.AddressBookDoCoMoParsedResult;
26 import com.google.zxing.client.result.BookmarkDoCoMoParsedResult;
27 import com.google.zxing.client.result.EmailAddressParsedResult;
28 import com.google.zxing.client.result.EmailDoCoMoParsedResult;
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;
35
36 import java.net.URISyntaxException;
37
38 /**
39  * Handles the result of barcode decoding in the context of the Android platform,
40  * by dispatching the proper intents and so on.
41  *
42  * @author srowen@google.com (Sean Owen)
43  * @author dswitkin@google.com (Daniel Switkin)
44  */
45 final class ResultHandler extends Handler {
46
47   private final Intent intent;
48   private final BarcodeReaderCaptureActivity captureActivity;
49
50   ResultHandler(BarcodeReaderCaptureActivity captureActivity, ParsedReaderResult result) {
51     this.captureActivity = captureActivity;
52     this.intent = resultToIntent(result);
53   }
54
55   private static Intent resultToIntent(ParsedReaderResult result) {
56     Intent intent = null;
57     ParsedReaderResultType type = result.getType();
58     if (type.equals(ParsedReaderResultType.ADDRESSBOOK)) {
59       AddressBookDoCoMoParsedResult addressResult = (AddressBookDoCoMoParsedResult) result;
60       intent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
61       putExtra(intent, Contacts.Intents.Insert.NAME, addressResult.getName());
62       putExtra(intent, Contacts.Intents.Insert.PHONE, addressResult.getPhoneNumbers());
63       putExtra(intent, Contacts.Intents.Insert.EMAIL, addressResult.getEmail());
64       putExtra(intent, Contacts.Intents.Insert.NOTES, addressResult.getNote());
65       putExtra(intent, Contacts.Intents.Insert.POSTAL, addressResult.getAddress());
66     } else if (type.equals(ParsedReaderResultType.ADDRESSBOOK_AU)) {
67       AddressBookAUParsedResult addressResult = (AddressBookAUParsedResult) result;
68       intent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
69       putExtra(intent, Contacts.Intents.Insert.NAME, addressResult.getNames());
70       putExtra(intent, Contacts.Intents.Insert.PHONE, addressResult.getPhoneNumbers());
71       putExtra(intent, Contacts.Intents.Insert.EMAIL, addressResult.getEmails());
72       putExtra(intent, Contacts.Intents.Insert.NOTES, addressResult.getNote());
73       putExtra(intent, Contacts.Intents.Insert.POSTAL, addressResult.getAddress());
74     } else if (type.equals(ParsedReaderResultType.BOOKMARK)) {
75       // For now, we can only open the browser, and not actually add a bookmark
76       try {
77         intent = new Intent(Intent.VIEW_ACTION, new ContentURI(((BookmarkDoCoMoParsedResult) result).getURI()));
78       } catch (URISyntaxException e) {
79       }
80     } else if (type.equals(ParsedReaderResultType.URLTO)) {
81       try {
82         intent = new Intent(Intent.VIEW_ACTION, new ContentURI(((URLTOParsedResult) result).getURI()));
83       } catch (URISyntaxException e) {
84       }
85     } else if (type.equals(ParsedReaderResultType.EMAIL)) {
86       EmailDoCoMoParsedResult emailResult = (EmailDoCoMoParsedResult) result;
87       try {
88         intent = new Intent(Intent.SENDTO_ACTION, new ContentURI(emailResult.getTo()));
89       } catch (URISyntaxException e) {
90       }
91       putExtra(intent, "subject", emailResult.getSubject());
92       putExtra(intent, "body", emailResult.getBody());
93     } else if (type.equals(ParsedReaderResultType.EMAIL_ADDRESS)) {
94       EmailAddressParsedResult emailResult = (EmailAddressParsedResult) result;
95       try {
96         intent = new Intent(Intent.SENDTO_ACTION, new ContentURI(emailResult.getEmailAddress()));
97       } catch (URISyntaxException e) {
98       }
99     } else if (type.equals(ParsedReaderResultType.TEL)) {
100       TelParsedResult telResult = (TelParsedResult) result;
101       try {
102         intent = new Intent(Intent.DIAL_ACTION, new ContentURI("tel:" + telResult.getNumber()));
103       } catch (URISyntaxException e) {
104       }
105     //} else if (type.equals(ParsedReaderResultType.GEO)) {
106     //  GeoParsedResult geoResult = (GeoParsedResult) result;
107     //  try {
108     //    intent = new Intent(Intent.VIEW_ACTION, new ContentURI(geoResult.getGoogleMapsURI()));
109     //    // or can we send the raw geo: URI to Android? maybe it'll open Maps?
110     //    // or just open a MapView
111     //  } catch (URISyntaxException e) {
112     //    return;
113     //  }
114     } else if (type.equals(ParsedReaderResultType.UPC)) {
115       UPCParsedResult upcResult = (UPCParsedResult) result;
116       try {
117         ContentURI uri = new ContentURI("http://www.upcdatabase.com/item.asp?upc=" + upcResult.getUPC());
118         intent = new Intent(Intent.VIEW_ACTION, uri);
119       } catch (URISyntaxException e) {
120       }
121     } else if (type.equals(ParsedReaderResultType.URI)) {
122       URIParsedResult uriResult = (URIParsedResult) result;
123       try {
124         intent = new Intent(Intent.VIEW_ACTION, new ContentURI(uriResult.getURI()));
125       } catch (URISyntaxException e) {
126       }
127     } else if (type.equals(ParsedReaderResultType.ANDROID_INTENT)) {
128       intent = ((AndroidIntentParsedResult) result).getIntent();
129     }
130     return intent;
131   }
132
133   @Override
134   public void handleMessage(Message message) {
135     if (message.what == R.string.button_yes) {
136       if (intent != null) {
137         captureActivity.startActivity(intent);
138       }
139     } else {
140       captureActivity.restartPreview();
141     }
142   }
143
144   Intent getIntent() {
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 }