2 * Copyright (C) 2008 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.client.android.result;
19 import android.app.Activity;
20 import android.content.Intent;
21 import android.net.Uri;
22 import android.provider.Contacts;
23 import com.google.zxing.client.android.Intents;
24 import com.google.zxing.client.android.R;
25 import com.google.zxing.client.android.SearchBookContentsActivity;
26 import com.google.zxing.client.result.ParsedResult;
27 import com.google.zxing.client.result.ParsedResultType;
29 import java.text.ParsePosition;
30 import java.text.SimpleDateFormat;
31 import java.util.Date;
32 import java.util.GregorianCalendar;
34 public abstract class ResultHandler {
36 public static final int MAX_BUTTON_COUNT = 4;
38 protected ParsedResult mResult;
39 private Activity mActivity;
41 public ResultHandler(Activity activity, ParsedResult result) {
47 * Indicates how many buttons the derived class wants shown.
49 * @return The integer button count.
51 public abstract int getButtonCount();
54 * The text of the nth action button.
56 * @param index From 0 to getButtonCount() - 1
57 * @return The button text as a resource ID
59 public abstract int getButtonText(int index);
63 * Execute the action which corresponds to the nth button.
65 * @param index The button that was clicked.
67 public abstract void handleButtonPress(int index);
70 * Create a possibly styled string for the contents of the current barcode.
72 * @return The text to be displayed.
74 public CharSequence getDisplayContents() {
75 String contents = mResult.getDisplayResult();
76 return contents.replace("\r", "");
80 * A string describing the kind of barcode that was found, e.g. "Found contact info".
82 * @return The resource ID of the string.
84 public abstract int getDisplayTitle();
87 * A convenience method to get the parsed type. Should not be overridden.
89 * @return The parsed type, e.g. URI or ISBN
91 public final ParsedResultType getType() {
92 return mResult.getType();
96 * Sends an intent to create a new calendar event by prepopulating the Add Event UI. Older
97 * versions of the system have a bug where the event title will not be filled out.
99 * @param summary A description of the event
100 * @param start The start time as yyyyMMdd or yyyyMMdd'T'HHmmss or yyyyMMdd'T'HHmmss'Z'
101 * @param end The end time as yyyyMMdd or yyyyMMdd'T'HHmmss or yyyyMMdd'T'HHmmss'Z'
103 public void addCalendarEvent(String summary, String start, String end) {
104 Intent intent = new Intent(Intent.ACTION_EDIT);
105 intent.setType("vnd.android.cursor.item/event");
106 intent.putExtra("beginTime", calculateMilliseconds(start));
107 if (start.length() == 8) {
108 intent.putExtra("allDay", true);
110 intent.putExtra("endTime", calculateMilliseconds(end));
111 intent.putExtra("title", summary);
112 launchIntent(intent);
115 private long calculateMilliseconds(String when) {
116 if (when.length() == 8) {
117 // Only contains year/month/day
118 SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
119 Date date = format.parse(when, new ParsePosition(0));
120 return date.getTime();
122 // The when string can be local time, or UTC if it ends with a Z
123 SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
124 Date date = format.parse(when.substring(0, 15), new ParsePosition(0));
125 long milliseconds = date.getTime();
126 if (when.length() == 16 && when.charAt(15) == 'Z') {
127 GregorianCalendar calendar = new GregorianCalendar();
128 int offset = (calendar.get(java.util.Calendar.ZONE_OFFSET) +
129 calendar.get(java.util.Calendar.DST_OFFSET));
130 milliseconds += offset;
136 public void addContact(String[] names, String[] phoneNumbers, String[] emails, String note,
137 String address, String org, String title) {
139 Intent intent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
140 putExtra(intent, Contacts.Intents.Insert.NAME, names);
141 putExtra(intent, Contacts.Intents.Insert.PHONE, phoneNumbers);
142 putExtra(intent, Contacts.Intents.Insert.EMAIL, emails);
143 putExtra(intent, Contacts.Intents.Insert.NOTES, note);
144 putExtra(intent, Contacts.Intents.Insert.POSTAL, address);
145 putExtra(intent, Contacts.Intents.Insert.COMPANY, org);
146 putExtra(intent, Contacts.Intents.Insert.JOB_TITLE, title);
147 launchIntent(intent);
150 public void shareByEmail(String contents) {
151 sendEmailFromUri("mailto:", mActivity.getString(R.string.msg_share_barcode), contents);
154 public void sendEmail(String address, String subject, String body) {
155 sendEmailFromUri("mailto:" + address, subject, body);
158 public void sendEmailFromUri(String uri, String subject, String body) {
159 Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
160 putExtra(intent, "subject", subject);
161 putExtra(intent, "body", body);
162 launchIntent(intent);
165 public void shareBySMS(String contents) {
166 sendSMSFromUri("smsto:", mActivity.getString(R.string.msg_share_barcode) + ":\n" + contents);
169 public void sendSMS(String phoneNumber, String body) {
170 sendSMSFromUri("smsto:" + phoneNumber, body);
173 public void sendSMSFromUri(String uri, String body) {
174 Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
175 putExtra(intent, "sms_body", body);
176 // Exit the app once the SMS is sent
177 intent.putExtra("compose_mode", true);
178 launchIntent(intent);
181 public void sendMMS(String phoneNumber, String subject, String body) {
182 sendMMSFromUri("mmsto:" + phoneNumber, subject, body);
185 public void sendMMSFromUri(String uri, String subject, String body) {
186 Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
187 // The Messaging app needs to see a valid subject or else it will treat this an an SMS.
188 if (subject == null || subject.length() == 0) {
189 putExtra(intent, "subject", mActivity.getString(R.string.msg_default_mms_subject));
191 putExtra(intent, "subject", subject);
193 putExtra(intent, "sms_body", body);
194 intent.putExtra("compose_mode", true);
195 launchIntent(intent);
198 public void dialPhone(String phoneNumber) {
199 launchIntent(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber)));
202 public void dialPhoneFromUri(String uri) {
203 launchIntent(new Intent(Intent.ACTION_DIAL, Uri.parse(uri)));
206 public void openMap(String geoURI) {
207 launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(geoURI)));
210 public void searchMap(String address) {
211 launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + Uri.encode(address))));
214 public void getDirections(float latitude, float longitude) {
215 launchIntent(new Intent(Intent.ACTION_VIEW,
216 Uri.parse("http://maps.google.com/maps?f=d&daddr=" + latitude + "," + longitude)));
219 public void openProductSearch(String upc) {
220 Uri uri = Uri.parse("http://www.google.com/products?q=" + upc);
221 launchIntent(new Intent(Intent.ACTION_VIEW, uri));
224 public void openBookSearch(String isbn) {
225 Uri uri = Uri.parse("http://books.google.com/books?vid=isbn" + isbn);
226 launchIntent(new Intent(Intent.ACTION_VIEW, uri));
229 public void searchBookContents(String isbn) {
230 Intent intent = new Intent(Intents.SearchBookContents.ACTION);
231 intent.setClassName(mActivity, SearchBookContentsActivity.class.getName());
232 putExtra(intent, Intents.SearchBookContents.ISBN, isbn);
233 launchIntent(intent);
236 public void openURL(String url) {
237 launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
240 public void webSearch(String query) {
241 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
242 intent.putExtra("query", query);
243 launchIntent(intent);
246 private void launchIntent(Intent intent) {
247 if (intent != null) {
248 mActivity.startActivity(intent);
252 private static void putExtra(Intent intent, String key, String value) {
253 if (value != null && value.length() > 0) {
254 intent.putExtra(key, value);
258 private static void putExtra(Intent intent, String key, String[] value) {
259 if (value != null && value.length > 0) {
260 putExtra(intent, key, value[0]);