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;
44 import java.text.DateFormat;
45 import java.text.ParsePosition;
46 import java.text.SimpleDateFormat;
47 import java.util.Calendar;
48 import java.util.Date;
49 import java.util.GregorianCalendar;
52 * A base class for the Android-specific barcode handlers. These allow the app to polymorphically
53 * suggest the appropriate actions for each data type.
55 * This class also contains a bunch of utility methods to take common actions like opening a URL.
56 * They could easily be moved into a helper object, but it can't be static because the Activity
57 * instance is needed to launch an intent.
59 * @author dswitkin@google.com (Daniel Switkin)
61 public abstract class ResultHandler {
62 private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
63 private static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
65 private static final String GOOGLE_SHOPPER_PACKAGE = "com.google.android.apps.shopper";
66 private static final String GOOGLE_SHOPPER_ACTIVITY = GOOGLE_SHOPPER_PACKAGE +
67 ".results.SearchResultsActivity";
68 private static final String MARKET_URI_PREFIX = "market://search?q=pname:";
69 private static final String MARKET_REFERRER_SUFFIX =
70 "&referrer=utm_source%3Dbarcodescanner%26utm_medium%3Dapps%26utm_campaign%3Dscan";
72 public static final int MAX_BUTTON_COUNT = 4;
74 private final ParsedResult result;
75 private final Activity activity;
76 private final Result rawResult;
77 private final String customProductSearch;
79 private final DialogInterface.OnClickListener shopperMarketListener =
80 new DialogInterface.OnClickListener() {
81 public void onClick(DialogInterface dialogInterface, int which) {
82 launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(MARKET_URI_PREFIX +
83 GOOGLE_SHOPPER_PACKAGE + MARKET_REFERRER_SUFFIX)));
87 ResultHandler(Activity activity, ParsedResult result) {
88 this(activity, result, null);
91 ResultHandler(Activity activity, ParsedResult result, Result rawResult) {
93 this.activity = activity;
94 this.rawResult = rawResult;
95 this.customProductSearch = parseCustomSearchURL();
97 // Make sure the Shopper button is hidden by default. Without this, scanning a product followed
98 // by a QR Code would leave the button on screen among the QR Code actions.
99 View shopperButton = activity.findViewById(R.id.shopper_button);
100 shopperButton.setVisibility(View.GONE);
103 ParsedResult getResult() {
107 boolean hasCustomProductSearch() {
108 return customProductSearch != null;
112 * Indicates how many buttons the derived class wants shown.
114 * @return The integer button count.
116 public abstract int getButtonCount();
119 * The text of the nth action button.
121 * @param index From 0 to getButtonCount() - 1
122 * @return The button text as a resource ID
124 public abstract int getButtonText(int index);
128 * Execute the action which corresponds to the nth button.
130 * @param index The button that was clicked.
132 public abstract void handleButtonPress(int index);
135 * The Google Shopper button is special and is not handled by the abstract button methods above.
137 * @param listener The on click listener to install for this button.
139 protected void showGoogleShopperButton(View.OnClickListener listener) {
140 View shopperButton = activity.findViewById(R.id.shopper_button);
141 shopperButton.setVisibility(View.VISIBLE);
142 shopperButton.setOnClickListener(listener);
146 * Create a possibly styled string for the contents of the current barcode.
148 * @return The text to be displayed.
150 public CharSequence getDisplayContents() {
151 String contents = result.getDisplayResult();
152 return contents.replace("\r", "");
156 * A string describing the kind of barcode that was found, e.g. "Found contact info".
158 * @return The resource ID of the string.
160 public abstract int getDisplayTitle();
163 * A convenience method to get the parsed type. Should not be overridden.
165 * @return The parsed type, e.g. URI or ISBN
167 public final ParsedResultType getType() {
168 return result.getType();
172 * Sends an intent to create a new calendar event by prepopulating the Add Event UI. Older
173 * versions of the system have a bug where the event title will not be filled out.
175 * @param summary A description of the event
176 * @param start The start time as yyyyMMdd or yyyyMMdd'T'HHmmss or yyyyMMdd'T'HHmmss'Z'
177 * @param end The end time as yyyyMMdd or yyyyMMdd'T'HHmmss or yyyyMMdd'T'HHmmss'Z'
178 * @param location a text description of the event location
179 * @param description a text description of the event itself
181 final void addCalendarEvent(String summary,
185 String description) {
186 Intent intent = new Intent(Intent.ACTION_EDIT);
187 intent.setType("vnd.android.cursor.item/event");
188 intent.putExtra("beginTime", calculateMilliseconds(start));
189 if (start.length() == 8) {
190 intent.putExtra("allDay", true);
195 intent.putExtra("endTime", calculateMilliseconds(end));
196 intent.putExtra("title", summary);
197 intent.putExtra("eventLocation", location);
198 intent.putExtra("description", description);
199 launchIntent(intent);
202 private static long calculateMilliseconds(String when) {
203 if (when.length() == 8) {
204 // Only contains year/month/day
206 synchronized (DATE_FORMAT) {
207 date = DATE_FORMAT.parse(when, new ParsePosition(0));
209 return date.getTime();
211 // The when string can be local time, or UTC if it ends with a Z
213 synchronized (DATE_TIME_FORMAT) {
214 date = DATE_TIME_FORMAT.parse(when.substring(0, 15), new ParsePosition(0));
216 long milliseconds = date.getTime();
217 if (when.length() == 16 && when.charAt(15) == 'Z') {
218 Calendar calendar = new GregorianCalendar();
219 int offset = calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET);
220 milliseconds += offset;
226 final void addContact(String[] names, String[] phoneNumbers, String[] emails, String note,
227 String address, String org, String title) {
229 // Only use the first name in the array, if present.
230 Intent intent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
231 putExtra(intent, Contacts.Intents.Insert.NAME, names != null ? names[0] : null);
233 int phoneCount = Math.min((phoneNumbers != null) ? phoneNumbers.length : 0,
234 Contents.PHONE_KEYS.length);
235 for (int x = 0; x < phoneCount; x++) {
236 putExtra(intent, Contents.PHONE_KEYS[x], phoneNumbers[x]);
239 int emailCount = Math.min((emails != null) ? emails.length : 0, Contents.EMAIL_KEYS.length);
240 for (int x = 0; x < emailCount; x++) {
241 putExtra(intent, Contents.EMAIL_KEYS[x], emails[x]);
244 putExtra(intent, Contacts.Intents.Insert.NOTES, note);
245 putExtra(intent, Contacts.Intents.Insert.POSTAL, address);
246 putExtra(intent, Contacts.Intents.Insert.COMPANY, org);
247 putExtra(intent, Contacts.Intents.Insert.JOB_TITLE, title);
248 launchIntent(intent);
251 final void shareByEmail(String contents) {
252 sendEmailFromUri("mailto:", null, activity.getString(R.string.msg_share_subject_line), contents);
255 final void sendEmail(String address, String subject, String body) {
256 sendEmailFromUri("mailto:" + address, address, subject, body);
259 // Use public Intent fields rather than private GMail app fields to specify subject and body.
260 final void sendEmailFromUri(String uri, String email, String subject, String body) {
261 Intent intent = new Intent(Intent.ACTION_SEND, Uri.parse(uri));
263 intent.putExtra(Intent.EXTRA_EMAIL, new String[] {email});
265 putExtra(intent, Intent.EXTRA_SUBJECT, subject);
266 putExtra(intent, Intent.EXTRA_TEXT, body);
267 intent.setType("text/plain");
268 launchIntent(intent);
271 final void shareBySMS(String contents) {
272 sendSMSFromUri("smsto:", activity.getString(R.string.msg_share_subject_line) + ":\n" +
276 final void sendSMS(String phoneNumber, String body) {
277 sendSMSFromUri("smsto:" + phoneNumber, body);
280 final void sendSMSFromUri(String uri, String body) {
281 Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
282 putExtra(intent, "sms_body", body);
283 // Exit the app once the SMS is sent
284 intent.putExtra("compose_mode", true);
285 launchIntent(intent);
288 final void sendMMS(String phoneNumber, String subject, String body) {
289 sendMMSFromUri("mmsto:" + phoneNumber, subject, body);
292 final void sendMMSFromUri(String uri, String subject, String body) {
293 Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
294 // The Messaging app needs to see a valid subject or else it will treat this an an SMS.
295 if (subject == null || subject.length() == 0) {
296 putExtra(intent, "subject", activity.getString(R.string.msg_default_mms_subject));
298 putExtra(intent, "subject", subject);
300 putExtra(intent, "sms_body", body);
301 intent.putExtra("compose_mode", true);
302 launchIntent(intent);
305 final void dialPhone(String phoneNumber) {
306 launchIntent(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber)));
309 final void dialPhoneFromUri(String uri) {
310 launchIntent(new Intent(Intent.ACTION_DIAL, Uri.parse(uri)));
313 final void openMap(String geoURI) {
314 launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(geoURI)));
318 * Do a geo search using the address as the query.
320 * @param address The address to find
321 * @param title An optional title, e.g. the name of the business at this address
323 final void searchMap(String address, String title) {
324 String query = address;
325 if (title != null && title.length() > 0) {
326 query = query + " (" + title + ')';
328 launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + Uri.encode(query))));
331 final void getDirections(double latitude, double longitude) {
332 launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google." +
333 LocaleManager.getCountryTLD() + "/maps?f=d&daddr=" + latitude + ',' + longitude)));
336 // Uses the mobile-specific version of Product Search, which is formatted for small screens.
337 final void openProductSearch(String upc) {
338 Uri uri = Uri.parse("http://www.google." + LocaleManager.getProductSearchCountryTLD() +
339 "/m/products?q=" + upc + "&source=zxing");
340 launchIntent(new Intent(Intent.ACTION_VIEW, uri));
343 final void openBookSearch(String isbn) {
344 Uri uri = Uri.parse("http://books.google." + LocaleManager.getBookSearchCountryTLD() +
345 "/books?vid=isbn" + isbn);
346 launchIntent(new Intent(Intent.ACTION_VIEW, uri));
349 final void searchBookContents(String isbn) {
350 Intent intent = new Intent(Intents.SearchBookContents.ACTION);
351 intent.setClassName(activity, SearchBookContentsActivity.class.getName());
352 putExtra(intent, Intents.SearchBookContents.ISBN, isbn);
353 launchIntent(intent);
356 final void wifiConnect(WifiParsedResult wifiResult) {
357 Intent intent = new Intent(Intents.WifiConnect.ACTION);
358 intent.setClassName(activity, WifiActivity.class.getName());
359 putExtra(intent, Intents.WifiConnect.SSID, wifiResult.getSsid());
360 putExtra(intent, Intents.WifiConnect.TYPE, wifiResult.getNetworkEncryption());
361 putExtra(intent, Intents.WifiConnect.PASSWORD, wifiResult.getPassword());
362 launchIntent(intent);
365 final void openURL(String url) {
366 launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
369 final void webSearch(String query) {
370 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
371 intent.putExtra("query", query);
372 launchIntent(intent);
375 final void openGoogleShopper(String query) {
377 activity.getPackageManager().getPackageInfo(GOOGLE_SHOPPER_PACKAGE, 0);
378 // If we didn't throw, Shopper is installed, so launch it.
379 Intent intent = new Intent(Intent.ACTION_SEARCH);
380 intent.setClassName(GOOGLE_SHOPPER_PACKAGE, GOOGLE_SHOPPER_ACTIVITY);
381 intent.putExtra(SearchManager.QUERY, query);
382 activity.startActivity(intent);
383 } catch (PackageManager.NameNotFoundException e) {
384 // Otherwise offer to install it from Market.
385 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
386 builder.setTitle(R.string.msg_google_shopper_missing);
387 builder.setMessage(R.string.msg_install_google_shopper);
388 builder.setIcon(R.drawable.shopper_icon);
389 builder.setPositiveButton(R.string.button_ok, shopperMarketListener);
390 builder.setNegativeButton(R.string.button_cancel, null);
395 void launchIntent(Intent intent) {
396 if (intent != null) {
397 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
399 activity.startActivity(intent);
400 } catch (ActivityNotFoundException e) {
401 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
402 builder.setTitle(R.string.app_name);
403 builder.setMessage(R.string.msg_intent_failed);
404 builder.setPositiveButton(R.string.button_ok, null);
410 private static void putExtra(Intent intent, String key, String value) {
411 if (value != null && value.length() > 0) {
412 intent.putExtra(key, value);
416 protected void showNotOurResults(int index, AlertDialog.OnClickListener proceedListener) {
417 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
418 if (prefs.getBoolean(PreferencesActivity.KEY_NOT_OUR_RESULTS_SHOWN, false)) {
419 // already seen it, just proceed
420 proceedListener.onClick(null, index);
422 // note the user has seen it
423 prefs.edit().putBoolean(PreferencesActivity.KEY_NOT_OUR_RESULTS_SHOWN, true).commit();
424 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
425 builder.setMessage(R.string.msg_not_our_results);
426 builder.setPositiveButton(R.string.button_ok, proceedListener);
431 private String parseCustomSearchURL() {
432 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
433 String customProductSearch = prefs.getString(PreferencesActivity.KEY_CUSTOM_PRODUCT_SEARCH, null);
434 if (customProductSearch != null && customProductSearch.trim().length() == 0) {
437 return customProductSearch;
440 String fillInCustomSearchURL(String text) {
441 String url = customProductSearch.replace("%s", text);
442 if (rawResult != null) {
443 url = url.replace("%f", rawResult.getBarcodeFormat().toString());