Don't like using == instead of equals() here, even though it's valid here. Fixed...
[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.AddressBookDoCoMoResult;
25 import com.google.zxing.client.result.BookmarkDoCoMoResult;
26 import com.google.zxing.client.result.EmailAddressResult;
27 import com.google.zxing.client.result.EmailDoCoMoResult;
28 import com.google.zxing.client.result.ParsedReaderResult;
29 import com.google.zxing.client.result.ParsedReaderResultType;
30 import com.google.zxing.client.result.UPCParsedResult;
31 import com.google.zxing.client.result.URIParsedResult;
32 import com.google.zxing.client.result.URLTOResult;
33
34 import java.net.URISyntaxException;
35
36 /**
37  * Handles the result of barcode decoding in the context of the Android platform,
38  * by dispatching the proper intents and so on.
39  *
40  * @author srowen@google.com (Sean Owen)
41  * @author dswitkin@google.com (Daniel Switkin)
42  */
43 final class ResultHandler extends Handler {
44
45   private final ParsedReaderResult result;
46   private final BarcodeReaderCaptureActivity captureActivity;
47
48   ResultHandler(BarcodeReaderCaptureActivity captureActivity, ParsedReaderResult result) {
49     this.captureActivity = captureActivity;
50     this.result = result;
51   }
52
53   @Override
54   public void handleMessage(Message message) {
55     if (message.what == R.string.button_yes) {
56       Intent intent = null;
57       ParsedReaderResultType type = result.getType();
58       if (type.equals(ParsedReaderResultType.ADDRESSBOOK)) {
59         AddressBookDoCoMoResult addressResult = (AddressBookDoCoMoResult) 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()[0]);
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.BOOKMARK)) {
67         // For now, we can only open the browser, and not actually add a bookmark
68         try {
69           intent = new Intent(Intent.VIEW_ACTION,
70               new ContentURI(((BookmarkDoCoMoResult) result).getURI()));
71         } catch (URISyntaxException e) {
72           return;
73         }
74       } else if (type.equals(ParsedReaderResultType.URLTO)) {
75         try {
76           intent = new Intent(Intent.VIEW_ACTION,
77               new ContentURI(((URLTOResult) result).getURI()));
78         } catch (URISyntaxException e) {
79           return;
80         }
81       } else if (type.equals(ParsedReaderResultType.EMAIL)) {
82         EmailDoCoMoResult emailResult = (EmailDoCoMoResult) result;
83         try {
84           intent = new Intent(Intent.SENDTO_ACTION, new ContentURI(emailResult.getTo()));
85         } catch (URISyntaxException e) {
86           return;
87         }
88         putExtra(intent, "subject", emailResult.getSubject());
89         putExtra(intent, "body", emailResult.getBody());
90       } else if (type.equals(ParsedReaderResultType.EMAIL_ADDRESS)) {
91         EmailAddressResult emailResult = (EmailAddressResult) result;
92         try {
93           intent = new Intent(Intent.SENDTO_ACTION, new ContentURI(emailResult.getEmailAddress()));
94         } catch (URISyntaxException e) {
95           return;
96         }
97       } else if (type.equals(ParsedReaderResultType.UPC)) {
98         UPCParsedResult upcResult = (UPCParsedResult) result;
99         try {
100           ContentURI uri = new ContentURI("http://www.upcdatabase.com/item.asp?upc=" +
101               upcResult.getUPC());
102           intent = new Intent(Intent.VIEW_ACTION, uri);
103         } catch (URISyntaxException e) {
104           return;
105         }
106       } else if (type.equals(ParsedReaderResultType.URI)) {
107         URIParsedResult uriResult = (URIParsedResult) result;
108         try {
109           intent = new Intent(Intent.VIEW_ACTION, new ContentURI(uriResult.getURI()));
110         } catch (URISyntaxException e) {
111           return;
112         }
113       }
114       if (intent != null) {
115         captureActivity.startActivity(intent);
116       }
117     } else {
118       captureActivity.restartPreview();
119     }
120   }
121
122   private static void putExtra(Intent intent, String key, String value) {
123     if (key != null && key.length() > 0) {
124       intent.putExtra(key, value);
125     }
126   }
127
128 }