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.app.AlertDialog;
21 import android.content.ActivityNotFoundException;
22 import android.content.Intent;
23 import android.net.Uri;
24 import android.provider.Contacts;
25 import com.google.zxing.client.android.Intents;
26 import com.google.zxing.client.android.R;
27 import com.google.zxing.client.android.SearchBookContentsActivity;
28 import com.google.zxing.client.android.LocaleManager;
29 import com.google.zxing.client.android.Contents;
30 import com.google.zxing.client.result.ParsedResult;
31 import com.google.zxing.client.result.ParsedResultType;
33 import java.text.ParsePosition;
34 import java.text.SimpleDateFormat;
35 import java.util.Calendar;
36 import java.text.DateFormat;
37 import java.util.Date;
38 import java.util.GregorianCalendar;
40 public abstract class ResultHandler {
42 private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
43 private static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
45 public static final int MAX_BUTTON_COUNT = 4;
47 protected final ParsedResult mResult;
48 private final Activity mActivity;
50 protected ResultHandler(Activity activity, ParsedResult result) {
56 * Indicates how many buttons the derived class wants shown.
58 * @return The integer button count.
60 public abstract int getButtonCount();
63 * The text of the nth action button.
65 * @param index From 0 to getButtonCount() - 1
66 * @return The button text as a resource ID
68 public abstract int getButtonText(int index);
72 * Execute the action which corresponds to the nth button.
74 * @param index The button that was clicked.
76 public abstract void handleButtonPress(int index);
79 * Create a possibly styled string for the contents of the current barcode.
81 * @return The text to be displayed.
83 public CharSequence getDisplayContents() {
84 String contents = mResult.getDisplayResult();
85 return contents.replace("\r", "");
89 * A string describing the kind of barcode that was found, e.g. "Found contact info".
91 * @return The resource ID of the string.
93 public abstract int getDisplayTitle();
96 * A convenience method to get the parsed type. Should not be overridden.
98 * @return The parsed type, e.g. URI or ISBN
100 public final ParsedResultType getType() {
101 return mResult.getType();
105 * Sends an intent to create a new calendar event by prepopulating the Add Event UI. Older
106 * versions of the system have a bug where the event title will not be filled out.
108 * @param summary A description of the event
109 * @param start The start time as yyyyMMdd or yyyyMMdd'T'HHmmss or yyyyMMdd'T'HHmmss'Z'
110 * @param end The end time as yyyyMMdd or yyyyMMdd'T'HHmmss or yyyyMMdd'T'HHmmss'Z'
112 public final void addCalendarEvent(String summary, String start, String end) {
113 Intent intent = new Intent(Intent.ACTION_EDIT);
114 intent.setType("vnd.android.cursor.item/event");
115 intent.putExtra("beginTime", calculateMilliseconds(start));
116 if (start.length() == 8) {
117 intent.putExtra("allDay", true);
119 intent.putExtra("endTime", calculateMilliseconds(end));
120 intent.putExtra("title", summary);
121 launchIntent(intent);
124 private static long calculateMilliseconds(String when) {
125 if (when.length() == 8) {
126 // Only contains year/month/day
128 synchronized (DATE_FORMAT) {
129 date = DATE_FORMAT.parse(when, new ParsePosition(0));
131 return date.getTime();
133 // The when string can be local time, or UTC if it ends with a Z
135 synchronized (DATE_TIME_FORMAT) {
136 date = DATE_TIME_FORMAT.parse(when.substring(0, 15), new ParsePosition(0));
138 long milliseconds = date.getTime();
139 if (when.length() == 16 && when.charAt(15) == 'Z') {
140 Calendar calendar = new GregorianCalendar();
141 int offset = calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET);
142 milliseconds += offset;
148 public final void addContact(String[] names, String[] phoneNumbers, String[] emails, String note,
149 String address, String org, String title) {
151 // Only use the first name in the array, if present.
152 Intent intent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
153 putExtra(intent, Contacts.Intents.Insert.NAME, names != null ? names[0] : null);
155 int phoneCount = Math.min((phoneNumbers != null) ? phoneNumbers.length : 0,
156 Contents.PHONE_KEYS.length);
157 for (int x = 0; x < phoneCount; x++) {
158 putExtra(intent, Contents.PHONE_KEYS[x], phoneNumbers[x]);
161 int emailCount = Math.min((emails != null) ? emails.length : 0, Contents.EMAIL_KEYS.length);
162 for (int x = 0; x < emailCount; x++) {
163 putExtra(intent, Contents.EMAIL_KEYS[x], emails[x]);
166 putExtra(intent, Contacts.Intents.Insert.NOTES, note);
167 putExtra(intent, Contacts.Intents.Insert.POSTAL, address);
168 putExtra(intent, Contacts.Intents.Insert.COMPANY, org);
169 putExtra(intent, Contacts.Intents.Insert.JOB_TITLE, title);
170 launchIntent(intent);
173 public final void shareByEmail(String contents) {
174 sendEmailFromUri("mailto:", mActivity.getString(R.string.msg_share_subject_line), contents);
177 public final void sendEmail(String address, String subject, String body) {
178 sendEmailFromUri("mailto:" + address, subject, body);
181 // Use public Intent fields rather than private GMail app fields to specify subject and body.
182 public final void sendEmailFromUri(String uri, String subject, String body) {
183 Intent intent = new Intent(Intent.ACTION_SEND, Uri.parse(uri));
184 putExtra(intent, Intent.EXTRA_SUBJECT, subject);
185 putExtra(intent, Intent.EXTRA_TEXT, body);
186 intent.setType("text/plain");
187 launchIntent(intent);
190 public final void shareBySMS(String contents) {
191 sendSMSFromUri("smsto:", mActivity.getString(R.string.msg_share_subject_line) + ":\n" +
195 public final void sendSMS(String phoneNumber, String body) {
196 sendSMSFromUri("smsto:" + phoneNumber, body);
199 public final void sendSMSFromUri(String uri, String body) {
200 Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
201 putExtra(intent, "sms_body", body);
202 // Exit the app once the SMS is sent
203 intent.putExtra("compose_mode", true);
204 launchIntent(intent);
207 public final void sendMMS(String phoneNumber, String subject, String body) {
208 sendMMSFromUri("mmsto:" + phoneNumber, subject, body);
211 public final void sendMMSFromUri(String uri, String subject, String body) {
212 Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
213 // The Messaging app needs to see a valid subject or else it will treat this an an SMS.
214 if (subject == null || subject.length() == 0) {
215 putExtra(intent, "subject", mActivity.getString(R.string.msg_default_mms_subject));
217 putExtra(intent, "subject", subject);
219 putExtra(intent, "sms_body", body);
220 intent.putExtra("compose_mode", true);
221 launchIntent(intent);
224 public final void dialPhone(String phoneNumber) {
225 launchIntent(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber)));
228 public final void dialPhoneFromUri(String uri) {
229 launchIntent(new Intent(Intent.ACTION_DIAL, Uri.parse(uri)));
232 public final void openMap(String geoURI) {
233 launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(geoURI)));
237 * Do a geo search using the address as the query.
239 * @param address The address to find
240 * @param title An optional title, e.g. the name of the business at this address
242 public final void searchMap(String address, String title) {
243 String query = address;
244 if (title != null && title.length() > 0) {
245 query = query + " (" + title + ')';
247 launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + Uri.encode(query))));
250 public final void getDirections(double latitude, double longitude) {
251 launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google." +
252 LocaleManager.getCountryTLD() + "/maps?f=d&daddr=" + latitude + ',' + longitude)));
255 // Uses the mobile-specific version of Product Search, which is formatted for small screens.
256 public final void openProductSearch(String upc) {
257 Uri uri = Uri.parse("http://www.google." + LocaleManager.getProductSearchCountryTLD() +
258 "/m/products?q=" + upc + "&source=zxing");
259 launchIntent(new Intent(Intent.ACTION_VIEW, uri));
262 public final void openBookSearch(String isbn) {
263 Uri uri = Uri.parse("http://books.google." + LocaleManager.getBookSearchCountryTLD() +
264 "/books?vid=isbn" + isbn);
265 launchIntent(new Intent(Intent.ACTION_VIEW, uri));
268 public final void searchBookContents(String isbn) {
269 Intent intent = new Intent(Intents.SearchBookContents.ACTION);
270 intent.setClassName(mActivity, SearchBookContentsActivity.class.getName());
271 putExtra(intent, Intents.SearchBookContents.ISBN, isbn);
272 launchIntent(intent);
275 public final void openURL(String url) {
276 launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
279 public final void webSearch(String query) {
280 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
281 intent.putExtra("query", query);
282 launchIntent(intent);
285 private void launchIntent(Intent intent) {
286 if (intent != null) {
288 mActivity.startActivity(intent);
289 } catch (ActivityNotFoundException e) {
290 AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
291 builder.setTitle(mActivity.getString(R.string.app_name));
292 builder.setMessage(mActivity.getString(R.string.msg_intent_failed));
293 builder.setPositiveButton(R.string.button_ok, null);
299 private static void putExtra(Intent intent, String key, String value) {
300 if (value != null && value.length() > 0) {
301 intent.putExtra(key, value);