Minor style tweaks
[zxing.git] / android / src / com / google / zxing / client / android / ResultHandler.java
1 /*
2  * Copyright 2008 ZXing authors
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.Uri;
21 import android.provider.Contacts;
22 import android.view.View;
23 import android.widget.Button;
24 import com.google.zxing.client.result.AddressBookParsedResult;
25 import com.google.zxing.client.result.EmailAddressParsedResult;
26 import com.google.zxing.client.result.GeoParsedResult;
27 import com.google.zxing.client.result.ParsedResult;
28 import com.google.zxing.client.result.ParsedResultType;
29 import com.google.zxing.client.result.SMSParsedResult;
30 import com.google.zxing.client.result.TelParsedResult;
31 import com.google.zxing.client.result.UPCParsedResult;
32 import com.google.zxing.client.result.URIParsedResult;
33
34 /**
35  * Handles the result of barcode decoding in the context of the Android platform,
36  * by dispatching the proper intents to open other activities like GMail, Maps, etc.
37  *
38  * @author srowen@google.com (Sean Owen)
39  * @author dswitkin@google.com (Daniel Switkin)
40  */
41 final class ResultHandler implements Button.OnClickListener {
42
43   private final Intent intent;
44   private final BarcodeReaderCaptureActivity captureActivity;
45
46   ResultHandler(BarcodeReaderCaptureActivity captureActivity, ParsedResult result) {
47     this.captureActivity = captureActivity;
48     this.intent = resultToIntent(result);
49   }
50
51   private static Intent resultToIntent(ParsedResult result) {
52     Intent intent = null;
53     ParsedResultType type = result.getType();
54     if (type.equals(ParsedResultType.ADDRESSBOOK)) {
55       AddressBookParsedResult addressResult = (AddressBookParsedResult) result;
56       intent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
57       putExtra(intent, Contacts.Intents.Insert.NAME, addressResult.getNames());
58       putExtra(intent, Contacts.Intents.Insert.PHONE, addressResult.getPhoneNumbers());
59       putExtra(intent, Contacts.Intents.Insert.EMAIL, addressResult.getEmails());
60       putExtra(intent, Contacts.Intents.Insert.NOTES, addressResult.getNote());
61       putExtra(intent, Contacts.Intents.Insert.POSTAL, addressResult.getAddress());
62       putExtra(intent, Contacts.Intents.Insert.COMPANY, addressResult.getOrg());
63       putExtra(intent, Contacts.Intents.Insert.JOB_TITLE, addressResult.getTitle());
64     } else if (type.equals(ParsedResultType.EMAIL_ADDRESS)) {
65       EmailAddressParsedResult emailResult = (EmailAddressParsedResult) result;
66       intent = new Intent(Intent.SENDTO_ACTION, Uri.parse(emailResult.getMailtoURI()));
67       putExtra(intent, "subject", emailResult.getSubject());
68       putExtra(intent, "body", emailResult.getBody());
69     } else if (type.equals(ParsedResultType.SMS)) {
70       SMSParsedResult smsResult = (SMSParsedResult) result;
71       intent = new Intent(Intent.SENDTO_ACTION, Uri.parse(smsResult.getSMSURI()));
72       putExtra(intent, "subject", smsResult.getSubject());
73       putExtra(intent, "body", smsResult.getBody());
74     } else if (type.equals(ParsedResultType.TEL)) {
75       TelParsedResult telResult = (TelParsedResult) result;
76       intent = new Intent(Intent.DIAL_ACTION, Uri.parse(telResult.getTelURI()));
77     } else if (type.equals(ParsedResultType.GEO)) {
78       GeoParsedResult geoResult = (GeoParsedResult) result;
79       intent = new Intent(Intent.VIEW_ACTION, Uri.parse(geoResult.getGeoURI()));
80     } else if (type.equals(ParsedResultType.UPC)) {
81       UPCParsedResult upcResult = (UPCParsedResult) result;
82       Uri uri = Uri.parse("http://www.upcdatabase.com/item.asp?upc=" + upcResult.getUPC());
83       intent = new Intent(Intent.VIEW_ACTION, uri);
84     } else if (type.equals(ParsedResultType.URI)) {
85       URIParsedResult uriResult = (URIParsedResult) result;
86       intent = new Intent(Intent.VIEW_ACTION, Uri.parse(uriResult.getURI()));
87     } else if (type.equals(ParsedResultType.ANDROID_INTENT)) {
88       intent = ((AndroidIntentParsedResult) result).getIntent();
89     }
90     return intent;
91   }
92
93   public void onClick(View view) {
94     if (intent != null) {
95       captureActivity.startActivity(intent);
96     }
97   }
98
99   Intent getIntent() {
100     return intent;
101   }
102
103   private static void putExtra(Intent intent, String key, String value) {
104     if (value != null && value.length() > 0) {
105       intent.putExtra(key, value);
106     }
107   }
108
109   private static void putExtra(Intent intent, String key, String[] value) {
110     if (value != null && value.length > 0) {
111       putExtra(intent, key, value[0]);
112     }
113   }
114
115 }