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