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 com.google.zxing.Result;
20 import com.google.zxing.client.android.Contents;
21 import com.google.zxing.client.android.Intents;
22 import com.google.zxing.client.android.LocaleManager;
23 import com.google.zxing.client.android.PreferencesActivity;
24 import com.google.zxing.client.android.R;
25 import com.google.zxing.client.android.book.SearchBookContentsActivity;
26 import com.google.zxing.client.android.wifi.WifiActivity;
27 import com.google.zxing.client.result.ParsedResult;
28 import com.google.zxing.client.result.ParsedResultType;
29 import com.google.zxing.client.result.WifiParsedResult;
31 import android.app.Activity;
32 import android.app.AlertDialog;
33 import android.app.SearchManager;
34 import android.content.ActivityNotFoundException;
35 import android.content.DialogInterface;
36 import android.content.Intent;
37 import android.content.SharedPreferences;
38 import android.content.pm.PackageManager;
39 import android.net.Uri;
40 import android.preference.PreferenceManager;
41 import android.provider.Contacts;
42 import android.view.View;
43 import android.widget.Button;
45 import java.text.DateFormat;
46 import java.text.ParsePosition;
47 import java.text.SimpleDateFormat;
48 import java.util.Calendar;
49 import java.util.Date;
50 import java.util.GregorianCalendar;
53 * A base class for the Android-specific barcode handlers. These allow the app to polymorphically
54 * suggest the appropriate actions for each data type.
56 * This class also contains a bunch of utility methods to take common actions like opening a URL.
57 * They could easily be moved into a helper object, but it can't be static because the Activity
58 * instance is needed to launch an intent.
60 * @author dswitkin@google.com (Daniel Switkin)
62 public abstract class ResultHandler {
63 private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
64 private static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
66 private static final String GOOGLE_SHOPPER_PACKAGE = "com.google.android.apps.shopper";
67 private static final String GOOGLE_SHOPPER_ACTIVITY = GOOGLE_SHOPPER_PACKAGE +
68 ".results.SearchResultsActivity";
69 private static final String MARKET_URI_PREFIX = "market://search?q=pname:";
70 private static final String MARKET_REFERRER_SUFFIX =
71 "&referrer=utm_source%3Dbarcodescanner%26utm_medium%3Dapps%26utm_campaign%3Dscan";
73 public static final int MAX_BUTTON_COUNT = 4;
75 private final ParsedResult result;
76 private final Activity activity;
77 private final Result rawResult;
78 private final String customProductSearch;
80 private final DialogInterface.OnClickListener shopperMarketListener =
81 new DialogInterface.OnClickListener() {
82 public void onClick(DialogInterface dialogInterface, int which) {
83 launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(MARKET_URI_PREFIX +
84 GOOGLE_SHOPPER_PACKAGE + MARKET_REFERRER_SUFFIX)));
88 ResultHandler(Activity activity, ParsedResult result) {
89 this(activity, result, null);
92 ResultHandler(Activity activity, ParsedResult result, Result rawResult) {
94 this.activity = activity;
95 this.rawResult = rawResult;
96 this.customProductSearch = parseCustomSearchURL();
98 // Make sure the Shopper button is hidden by default. Without this, scanning a product followed
99 // by a QR Code would leave the button on screen among the QR Code actions.
100 Button shopperButton = (Button) activity.findViewById(R.id.shopper_button);
101 shopperButton.setVisibility(View.GONE);
104 ParsedResult getResult() {
108 boolean hasCustomProductSearch() {
109 return customProductSearch != null;
113 * Indicates how many buttons the derived class wants shown.
115 * @return The integer button count.
117 public abstract int getButtonCount();
120 * The text of the nth action button.
122 * @param index From 0 to getButtonCount() - 1
123 * @return The button text as a resource ID
125 public abstract int getButtonText(int index);
129 * Execute the action which corresponds to the nth button.
131 * @param index The button that was clicked.
133 public abstract void handleButtonPress(int index);
136 * The Google Shopper button is special and is not handled by the abstract button methods above.
138 * @param listener The on click listener to install for this button.
140 protected void showGoogleShopperButton(View.OnClickListener listener) {
141 Button shopperButton = (Button) activity.findViewById(R.id.shopper_button);
142 shopperButton.setVisibility(View.VISIBLE);
143 shopperButton.setOnClickListener(listener);
147 * Create a possibly styled string for the contents of the current barcode.
149 * @return The text to be displayed.
151 public CharSequence getDisplayContents() {
152 String contents = result.getDisplayResult();
153 return contents.replace("\r", "");
157 * A string describing the kind of barcode that was found, e.g. "Found contact info".
159 * @return The resource ID of the string.
161 public abstract int getDisplayTitle();
164 * A convenience method to get the parsed type. Should not be overridden.
166 * @return The parsed type, e.g. URI or ISBN
168 public final ParsedResultType getType() {
169 return result.getType();
173 * Sends an intent to create a new calendar event by prepopulating the Add Event UI. Older
174 * versions of the system have a bug where the event title will not be filled out.
176 * @param summary A description of the event
177 * @param start The start time as yyyyMMdd or yyyyMMdd'T'HHmmss or yyyyMMdd'T'HHmmss'Z'
178 * @param end The end time as yyyyMMdd or yyyyMMdd'T'HHmmss or yyyyMMdd'T'HHmmss'Z'
179 * @param location a text description of the event location
180 * @param description a text description of the event itself
182 final void addCalendarEvent(String summary,
186 String description) {
187 Intent intent = new Intent(Intent.ACTION_EDIT);
188 intent.setType("vnd.android.cursor.item/event");
189 intent.putExtra("beginTime", calculateMilliseconds(start));
190 if (start.length() == 8) {
191 intent.putExtra("allDay", true);
196 intent.putExtra("endTime", calculateMilliseconds(end));
197 intent.putExtra("title", summary);
198 intent.putExtra("eventLocation", location);
199 intent.putExtra("description", description);
200 launchIntent(intent);
203 private static long calculateMilliseconds(String when) {
204 if (when.length() == 8) {
205 // Only contains year/month/day
207 synchronized (DATE_FORMAT) {
208 date = DATE_FORMAT.parse(when, new ParsePosition(0));
210 return date.getTime();
212 // The when string can be local time, or UTC if it ends with a Z
214 synchronized (DATE_TIME_FORMAT) {
215 date = DATE_TIME_FORMAT.parse(when.substring(0, 15), new ParsePosition(0));
217 long milliseconds = date.getTime();
218 if (when.length() == 16 && when.charAt(15) == 'Z') {
219 Calendar calendar = new GregorianCalendar();
220 int offset = calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET);
221 milliseconds += offset;
227 final void addContact(String[] names, String[] phoneNumbers, String[] emails, String note,
228 String address, String org, String title) {
230 // Only use the first name in the array, if present.
231 Intent intent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
232 putExtra(intent, Contacts.Intents.Insert.NAME, names != null ? names[0] : null);
234 int phoneCount = Math.min((phoneNumbers != null) ? phoneNumbers.length : 0,
235 Contents.PHONE_KEYS.length);
236 for (int x = 0; x < phoneCount; x++) {
237 putExtra(intent, Contents.PHONE_KEYS[x], phoneNumbers[x]);
240 int emailCount = Math.min((emails != null) ? emails.length : 0, Contents.EMAIL_KEYS.length);
241 for (int x = 0; x < emailCount; x++) {
242 putExtra(intent, Contents.EMAIL_KEYS[x], emails[x]);
245 putExtra(intent, Contacts.Intents.Insert.NOTES, note);
246 putExtra(intent, Contacts.Intents.Insert.POSTAL, address);
247 putExtra(intent, Contacts.Intents.Insert.COMPANY, org);
248 putExtra(intent, Contacts.Intents.Insert.JOB_TITLE, title);
249 launchIntent(intent);
252 final void shareByEmail(String contents) {
253 sendEmailFromUri("mailto:", null, activity.getString(R.string.msg_share_subject_line), contents);
256 final void sendEmail(String address, String subject, String body) {
257 sendEmailFromUri("mailto:" + address, address, subject, body);
260 // Use public Intent fields rather than private GMail app fields to specify subject and body.
261 final void sendEmailFromUri(String uri, String email, String subject, String body) {
262 Intent intent = new Intent(Intent.ACTION_SEND, Uri.parse(uri));
264 intent.putExtra(Intent.EXTRA_EMAIL, new String[] {email});
266 putExtra(intent, Intent.EXTRA_SUBJECT, subject);
267 putExtra(intent, Intent.EXTRA_TEXT, body);
268 intent.setType("text/plain");
269 launchIntent(intent);
272 final void shareBySMS(String contents) {
273 sendSMSFromUri("smsto:", activity.getString(R.string.msg_share_subject_line) + ":\n" +
277 final void sendSMS(String phoneNumber, String body) {
278 sendSMSFromUri("smsto:" + phoneNumber, body);
281 final void sendSMSFromUri(String uri, String body) {
282 Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
283 putExtra(intent, "sms_body", body);
284 // Exit the app once the SMS is sent
285 intent.putExtra("compose_mode", true);
286 launchIntent(intent);
289 final void sendMMS(String phoneNumber, String subject, String body) {
290 sendMMSFromUri("mmsto:" + phoneNumber, subject, body);
293 final void sendMMSFromUri(String uri, String subject, String body) {
294 Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
295 // The Messaging app needs to see a valid subject or else it will treat this an an SMS.
296 if (subject == null || subject.length() == 0) {
297 putExtra(intent, "subject", activity.getString(R.string.msg_default_mms_subject));
299 putExtra(intent, "subject", subject);
301 putExtra(intent, "sms_body", body);
302 intent.putExtra("compose_mode", true);
303 launchIntent(intent);
306 final void dialPhone(String phoneNumber) {
307 launchIntent(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber)));
310 final void dialPhoneFromUri(String uri) {
311 launchIntent(new Intent(Intent.ACTION_DIAL, Uri.parse(uri)));
314 final void openMap(String geoURI) {
315 launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(geoURI)));
319 * Do a geo search using the address as the query.
321 * @param address The address to find
322 * @param title An optional title, e.g. the name of the business at this address
324 final void searchMap(String address, String title) {
325 String query = address;
326 if (title != null && title.length() > 0) {
327 query = query + " (" + title + ')';
329 launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + Uri.encode(query))));
332 final void getDirections(double latitude, double longitude) {
333 launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google." +
334 LocaleManager.getCountryTLD() + "/maps?f=d&daddr=" + latitude + ',' + longitude)));
337 // Uses the mobile-specific version of Product Search, which is formatted for small screens.
338 final void openProductSearch(String upc) {
339 Uri uri = Uri.parse("http://www.google." + LocaleManager.getProductSearchCountryTLD() +
340 "/m/products?q=" + upc + "&source=zxing");
341 launchIntent(new Intent(Intent.ACTION_VIEW, uri));
344 final void openBookSearch(String isbn) {
345 Uri uri = Uri.parse("http://books.google." + LocaleManager.getBookSearchCountryTLD() +
346 "/books?vid=isbn" + isbn);
347 launchIntent(new Intent(Intent.ACTION_VIEW, uri));
350 final void searchBookContents(String isbn) {
351 Intent intent = new Intent(Intents.SearchBookContents.ACTION);
352 intent.setClassName(activity, SearchBookContentsActivity.class.getName());
353 putExtra(intent, Intents.SearchBookContents.ISBN, isbn);
354 launchIntent(intent);
357 final void wifiConnect(WifiParsedResult wifiResult) {
358 Intent intent = new Intent(Intents.WifiConnect.ACTION);
359 intent.setClassName(activity, WifiActivity.class.getName());
360 putExtra(intent, Intents.WifiConnect.SSID, wifiResult.getSsid());
361 putExtra(intent, Intents.WifiConnect.TYPE, wifiResult.getNetworkEncryption());
362 putExtra(intent, Intents.WifiConnect.PASSWORD, wifiResult.getPassword());
363 launchIntent(intent);
366 final void openURL(String url) {
367 launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
370 final void webSearch(String query) {
371 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
372 intent.putExtra("query", query);
373 launchIntent(intent);
376 final void openGoogleShopper(String query) {
378 activity.getPackageManager().getPackageInfo(GOOGLE_SHOPPER_PACKAGE, 0);
379 // If we didn't throw, Shopper is installed, so launch it.
380 Intent intent = new Intent(Intent.ACTION_SEARCH);
381 intent.setClassName(GOOGLE_SHOPPER_PACKAGE, GOOGLE_SHOPPER_ACTIVITY);
382 intent.putExtra(SearchManager.QUERY, query);
383 activity.startActivity(intent);
384 } catch (PackageManager.NameNotFoundException e) {
385 // Otherwise offer to install it from Market.
386 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
387 builder.setTitle(R.string.msg_google_shopper_missing);
388 builder.setMessage(R.string.msg_install_google_shopper);
389 builder.setIcon(R.drawable.shopper_icon);
390 builder.setPositiveButton(R.string.button_ok, shopperMarketListener);
391 builder.setNegativeButton(R.string.button_cancel, null);
396 void launchIntent(Intent intent) {
397 if (intent != null) {
398 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
400 activity.startActivity(intent);
401 } catch (ActivityNotFoundException e) {
402 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
403 builder.setTitle(R.string.app_name);
404 builder.setMessage(R.string.msg_intent_failed);
405 builder.setPositiveButton(R.string.button_ok, null);
411 private static void putExtra(Intent intent, String key, String value) {
412 if (value != null && value.length() > 0) {
413 intent.putExtra(key, value);
417 protected void showNotOurResults(int index, AlertDialog.OnClickListener proceedListener) {
418 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
419 if (prefs.getBoolean(PreferencesActivity.KEY_NOT_OUR_RESULTS_SHOWN, false)) {
420 // already seen it, just proceed
421 proceedListener.onClick(null, index);
423 // note the user has seen it
424 prefs.edit().putBoolean(PreferencesActivity.KEY_NOT_OUR_RESULTS_SHOWN, true).commit();
425 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
426 builder.setMessage(R.string.msg_not_our_results);
427 builder.setPositiveButton(R.string.button_ok, proceedListener);
432 private String parseCustomSearchURL() {
433 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
434 String customProductSearch = prefs.getString(PreferencesActivity.KEY_CUSTOM_PRODUCT_SEARCH, null);
435 if (customProductSearch != null && customProductSearch.trim().length() == 0) {
438 return customProductSearch;
441 String fillInCustomSearchURL(String text) {
442 String url = customProductSearch.replace("%s", text);
443 if (rawResult != null) {
444 url = url.replace("%f", rawResult.getBarcodeFormat().toString());