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.content.SharedPreferences;
24 import android.net.Uri;
25 import android.preference.PreferenceManager;
26 import android.provider.Contacts;
27 import com.google.zxing.client.android.Contents;
28 import com.google.zxing.client.android.Intents;
29 import com.google.zxing.client.android.LocaleManager;
30 import com.google.zxing.client.android.PreferencesActivity;
31 import com.google.zxing.client.android.R;
32 import com.google.zxing.client.android.book.SearchBookContentsActivity;
33 import com.google.zxing.client.result.ParsedResult;
34 import com.google.zxing.client.result.ParsedResultType;
36 import java.text.DateFormat;
37 import java.text.ParsePosition;
38 import java.text.SimpleDateFormat;
39 import java.util.Calendar;
40 import java.util.Date;
41 import java.util.GregorianCalendar;
44 * A base class for the Android-specific barcode handlers. These allow the app to polymorphically
45 * suggest the appropriate actions for each data type.
47 * This class also contains a bunch of utility methods to take common actions like opening a URL.
48 * They could easily be moved into a helper object, but it can't be static because the Activity
49 * instance is needed to launch an intent.
51 * @author dswitkin@google.com (Daniel Switkin)
53 public abstract class ResultHandler {
54 private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
55 private static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
57 public static final int MAX_BUTTON_COUNT = 4;
59 private final ParsedResult result;
60 private final Activity activity;
62 ResultHandler(Activity activity, ParsedResult result) {
64 this.activity = activity;
67 ParsedResult getResult() {
72 * Indicates how many buttons the derived class wants shown.
74 * @return The integer button count.
76 public abstract int getButtonCount();
79 * The text of the nth action button.
81 * @param index From 0 to getButtonCount() - 1
82 * @return The button text as a resource ID
84 public abstract int getButtonText(int index);
88 * Execute the action which corresponds to the nth button.
90 * @param index The button that was clicked.
92 public abstract void handleButtonPress(int index);
95 * Create a possibly styled string for the contents of the current barcode.
97 * @return The text to be displayed.
99 public CharSequence getDisplayContents() {
100 String contents = result.getDisplayResult();
101 return contents.replace("\r", "");
105 * A string describing the kind of barcode that was found, e.g. "Found contact info".
107 * @return The resource ID of the string.
109 public abstract int getDisplayTitle();
112 * A convenience method to get the parsed type. Should not be overridden.
114 * @return The parsed type, e.g. URI or ISBN
116 public final ParsedResultType getType() {
117 return result.getType();
121 * Sends an intent to create a new calendar event by prepopulating the Add Event UI. Older
122 * versions of the system have a bug where the event title will not be filled out.
124 * @param summary A description of the event
125 * @param start The start time as yyyyMMdd or yyyyMMdd'T'HHmmss or yyyyMMdd'T'HHmmss'Z'
126 * @param end The end time as yyyyMMdd or yyyyMMdd'T'HHmmss or yyyyMMdd'T'HHmmss'Z'
128 final void addCalendarEvent(String summary, String start, String end) {
129 Intent intent = new Intent(Intent.ACTION_EDIT);
130 intent.setType("vnd.android.cursor.item/event");
131 intent.putExtra("beginTime", calculateMilliseconds(start));
132 if (start.length() == 8) {
133 intent.putExtra("allDay", true);
135 intent.putExtra("endTime", calculateMilliseconds(end));
136 intent.putExtra("title", summary);
137 launchIntent(intent);
140 private static long calculateMilliseconds(String when) {
141 if (when.length() == 8) {
142 // Only contains year/month/day
144 synchronized (DATE_FORMAT) {
145 date = DATE_FORMAT.parse(when, new ParsePosition(0));
147 return date.getTime();
149 // The when string can be local time, or UTC if it ends with a Z
151 synchronized (DATE_TIME_FORMAT) {
152 date = DATE_TIME_FORMAT.parse(when.substring(0, 15), new ParsePosition(0));
154 long milliseconds = date.getTime();
155 if (when.length() == 16 && when.charAt(15) == 'Z') {
156 Calendar calendar = new GregorianCalendar();
157 int offset = calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET);
158 milliseconds += offset;
164 final void addContact(String[] names, String[] phoneNumbers, String[] emails, String note,
165 String address, String org, String title) {
167 // Only use the first name in the array, if present.
168 Intent intent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
169 putExtra(intent, Contacts.Intents.Insert.NAME, names != null ? names[0] : null);
171 int phoneCount = Math.min((phoneNumbers != null) ? phoneNumbers.length : 0,
172 Contents.PHONE_KEYS.length);
173 for (int x = 0; x < phoneCount; x++) {
174 putExtra(intent, Contents.PHONE_KEYS[x], phoneNumbers[x]);
177 int emailCount = Math.min((emails != null) ? emails.length : 0, Contents.EMAIL_KEYS.length);
178 for (int x = 0; x < emailCount; x++) {
179 putExtra(intent, Contents.EMAIL_KEYS[x], emails[x]);
182 putExtra(intent, Contacts.Intents.Insert.NOTES, note);
183 putExtra(intent, Contacts.Intents.Insert.POSTAL, address);
184 putExtra(intent, Contacts.Intents.Insert.COMPANY, org);
185 putExtra(intent, Contacts.Intents.Insert.JOB_TITLE, title);
186 launchIntent(intent);
189 final void shareByEmail(String contents) {
190 sendEmailFromUri("mailto:", activity.getString(R.string.msg_share_subject_line), contents);
193 final void sendEmail(String address, String subject, String body) {
194 sendEmailFromUri("mailto:" + address, subject, body);
197 // Use public Intent fields rather than private GMail app fields to specify subject and body.
198 final void sendEmailFromUri(String uri, String subject, String body) {
199 Intent intent = new Intent(Intent.ACTION_SEND, Uri.parse(uri));
200 putExtra(intent, Intent.EXTRA_SUBJECT, subject);
201 putExtra(intent, Intent.EXTRA_TEXT, body);
202 intent.setType("text/plain");
203 launchIntent(intent);
206 final void shareBySMS(String contents) {
207 sendSMSFromUri("smsto:", activity.getString(R.string.msg_share_subject_line) + ":\n" +
211 final void sendSMS(String phoneNumber, String body) {
212 sendSMSFromUri("smsto:" + phoneNumber, body);
215 final void sendSMSFromUri(String uri, String body) {
216 Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
217 putExtra(intent, "sms_body", body);
218 // Exit the app once the SMS is sent
219 intent.putExtra("compose_mode", true);
220 launchIntent(intent);
223 final void sendMMS(String phoneNumber, String subject, String body) {
224 sendMMSFromUri("mmsto:" + phoneNumber, subject, body);
227 final void sendMMSFromUri(String uri, String subject, String body) {
228 Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
229 // The Messaging app needs to see a valid subject or else it will treat this an an SMS.
230 if (subject == null || subject.length() == 0) {
231 putExtra(intent, "subject", activity.getString(R.string.msg_default_mms_subject));
233 putExtra(intent, "subject", subject);
235 putExtra(intent, "sms_body", body);
236 intent.putExtra("compose_mode", true);
237 launchIntent(intent);
240 final void dialPhone(String phoneNumber) {
241 launchIntent(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber)));
244 final void dialPhoneFromUri(String uri) {
245 launchIntent(new Intent(Intent.ACTION_DIAL, Uri.parse(uri)));
248 final void openMap(String geoURI) {
249 launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(geoURI)));
253 * Do a geo search using the address as the query.
255 * @param address The address to find
256 * @param title An optional title, e.g. the name of the business at this address
258 final void searchMap(String address, String title) {
259 String query = address;
260 if (title != null && title.length() > 0) {
261 query = query + " (" + title + ')';
263 launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + Uri.encode(query))));
266 final void getDirections(double latitude, double longitude) {
267 launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google." +
268 LocaleManager.getCountryTLD() + "/maps?f=d&daddr=" + latitude + ',' + longitude)));
271 // Uses the mobile-specific version of Product Search, which is formatted for small screens.
272 final void openProductSearch(String upc) {
273 Uri uri = Uri.parse("http://www.google." + LocaleManager.getProductSearchCountryTLD() +
274 "/m/products?q=" + upc + "&source=zxing");
275 launchIntent(new Intent(Intent.ACTION_VIEW, uri));
278 final void openBookSearch(String isbn) {
279 Uri uri = Uri.parse("http://books.google." + LocaleManager.getBookSearchCountryTLD() +
280 "/books?vid=isbn" + isbn);
281 launchIntent(new Intent(Intent.ACTION_VIEW, uri));
284 final void searchBookContents(String isbn) {
285 Intent intent = new Intent(Intents.SearchBookContents.ACTION);
286 intent.setClassName(activity, SearchBookContentsActivity.class.getName());
287 putExtra(intent, Intents.SearchBookContents.ISBN, isbn);
288 launchIntent(intent);
291 final void openURL(String url) {
292 launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
295 final void webSearch(String query) {
296 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
297 intent.putExtra("query", query);
298 launchIntent(intent);
301 void launchIntent(Intent intent) {
302 if (intent != null) {
304 activity.startActivity(intent);
305 } catch (ActivityNotFoundException e) {
306 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
307 builder.setTitle(activity.getString(R.string.app_name));
308 builder.setMessage(activity.getString(R.string.msg_intent_failed));
309 builder.setPositiveButton(R.string.button_ok, null);
315 private static void putExtra(Intent intent, String key, String value) {
316 if (value != null && value.length() > 0) {
317 intent.putExtra(key, value);
321 protected void showNotOurResults(int index, AlertDialog.OnClickListener proceedListener) {
322 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
323 if (prefs.getBoolean(PreferencesActivity.KEY_NOT_OUR_RESULTS_SHOWN, false)) {
324 // already seen it, just proceed
325 proceedListener.onClick(null, index);
327 // note the user has seen it
328 prefs.edit().putBoolean(PreferencesActivity.KEY_NOT_OUR_RESULTS_SHOWN, true).commit();
329 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
330 builder.setMessage(R.string.msg_not_our_results);
331 builder.setPositiveButton(R.string.button_ok, proceedListener);