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