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.client.android.Contents;
20 import com.google.zxing.client.android.Intents;
21 import com.google.zxing.client.android.LocaleManager;
22 import com.google.zxing.client.android.PreferencesActivity;
23 import com.google.zxing.client.android.R;
24 import com.google.zxing.client.android.book.SearchBookContentsActivity;
25 import com.google.zxing.client.android.wifi.WifiActivity;
26 import com.google.zxing.client.result.ParsedResult;
27 import com.google.zxing.client.result.ParsedResultType;
28 import com.google.zxing.client.result.WifiParsedResult;
30 import android.app.Activity;
31 import android.app.AlertDialog;
32 import android.app.SearchManager;
33 import android.content.ActivityNotFoundException;
34 import android.content.DialogInterface;
35 import android.content.Intent;
36 import android.content.SharedPreferences;
37 import android.content.pm.PackageManager;
38 import android.net.Uri;
39 import android.preference.PreferenceManager;
40 import android.provider.Contacts;
42 import java.text.DateFormat;
43 import java.text.ParsePosition;
44 import java.text.SimpleDateFormat;
45 import java.util.Calendar;
46 import java.util.Date;
47 import java.util.GregorianCalendar;
50 * A base class for the Android-specific barcode handlers. These allow the app to polymorphically
51 * suggest the appropriate actions for each data type.
53 * This class also contains a bunch of utility methods to take common actions like opening a URL.
54 * They could easily be moved into a helper object, but it can't be static because the Activity
55 * instance is needed to launch an intent.
57 * @author dswitkin@google.com (Daniel Switkin)
59 public abstract class ResultHandler {
60 private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
61 private static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
63 private static final String GOOGLE_SHOPPER_PACKAGE = "com.google.android.apps.shopper";
64 private static final String GOOGLE_SHOPPER_ACTIVITY = GOOGLE_SHOPPER_PACKAGE +
65 ".results.SearchResultsActivity";
66 private static final String MARKET_URI_PREFIX = "market://search?q=pname:";
67 private static final String MARKET_REFERRER_SUFFIX =
68 "&referrer=utm_source%3Dbarcodescanner%26utm_medium%3Dapps%26utm_campaign%3Dscan";
70 public static final int MAX_BUTTON_COUNT = 4;
72 private final ParsedResult result;
73 private final Activity activity;
75 private final DialogInterface.OnClickListener shopperMarketListener =
76 new DialogInterface.OnClickListener() {
77 public void onClick(DialogInterface dialogInterface, int which) {
78 launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(MARKET_URI_PREFIX +
79 GOOGLE_SHOPPER_PACKAGE + MARKET_REFERRER_SUFFIX)));
83 ResultHandler(Activity activity, ParsedResult result) {
85 this.activity = activity;
88 ParsedResult getResult() {
93 * Indicates how many buttons the derived class wants shown.
95 * @return The integer button count.
97 public abstract int getButtonCount();
100 * The text of the nth action button.
102 * @param index From 0 to getButtonCount() - 1
103 * @return The button text as a resource ID
105 public abstract int getButtonText(int index);
109 * Execute the action which corresponds to the nth button.
111 * @param index The button that was clicked.
113 public abstract void handleButtonPress(int index);
116 * Create a possibly styled string for the contents of the current barcode.
118 * @return The text to be displayed.
120 public CharSequence getDisplayContents() {
121 String contents = result.getDisplayResult();
122 return contents.replace("\r", "");
126 * A string describing the kind of barcode that was found, e.g. "Found contact info".
128 * @return The resource ID of the string.
130 public abstract int getDisplayTitle();
133 * A convenience method to get the parsed type. Should not be overridden.
135 * @return The parsed type, e.g. URI or ISBN
137 public final ParsedResultType getType() {
138 return result.getType();
142 * Sends an intent to create a new calendar event by prepopulating the Add Event UI. Older
143 * versions of the system have a bug where the event title will not be filled out.
145 * @param summary A description of the event
146 * @param start The start time as yyyyMMdd or yyyyMMdd'T'HHmmss or yyyyMMdd'T'HHmmss'Z'
147 * @param end The end time as yyyyMMdd or yyyyMMdd'T'HHmmss or yyyyMMdd'T'HHmmss'Z'
148 * @param location a text description of the event location
149 * @param description a text description of the event itself
151 final void addCalendarEvent(String summary,
155 String description) {
156 Intent intent = new Intent(Intent.ACTION_EDIT);
157 intent.setType("vnd.android.cursor.item/event");
158 intent.putExtra("beginTime", calculateMilliseconds(start));
159 if (start.length() == 8) {
160 intent.putExtra("allDay", true);
165 intent.putExtra("endTime", calculateMilliseconds(end));
166 intent.putExtra("title", summary);
167 intent.putExtra("eventLocation", location);
168 intent.putExtra("description", description);
169 launchIntent(intent);
172 private static long calculateMilliseconds(String when) {
173 if (when.length() == 8) {
174 // Only contains year/month/day
176 synchronized (DATE_FORMAT) {
177 date = DATE_FORMAT.parse(when, new ParsePosition(0));
179 return date.getTime();
181 // The when string can be local time, or UTC if it ends with a Z
183 synchronized (DATE_TIME_FORMAT) {
184 date = DATE_TIME_FORMAT.parse(when.substring(0, 15), new ParsePosition(0));
186 long milliseconds = date.getTime();
187 if (when.length() == 16 && when.charAt(15) == 'Z') {
188 Calendar calendar = new GregorianCalendar();
189 int offset = calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET);
190 milliseconds += offset;
196 final void addContact(String[] names, String[] phoneNumbers, String[] emails, String note,
197 String address, String org, String title) {
199 // Only use the first name in the array, if present.
200 Intent intent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
201 putExtra(intent, Contacts.Intents.Insert.NAME, names != null ? names[0] : null);
203 int phoneCount = Math.min((phoneNumbers != null) ? phoneNumbers.length : 0,
204 Contents.PHONE_KEYS.length);
205 for (int x = 0; x < phoneCount; x++) {
206 putExtra(intent, Contents.PHONE_KEYS[x], phoneNumbers[x]);
209 int emailCount = Math.min((emails != null) ? emails.length : 0, Contents.EMAIL_KEYS.length);
210 for (int x = 0; x < emailCount; x++) {
211 putExtra(intent, Contents.EMAIL_KEYS[x], emails[x]);
214 putExtra(intent, Contacts.Intents.Insert.NOTES, note);
215 putExtra(intent, Contacts.Intents.Insert.POSTAL, address);
216 putExtra(intent, Contacts.Intents.Insert.COMPANY, org);
217 putExtra(intent, Contacts.Intents.Insert.JOB_TITLE, title);
218 launchIntent(intent);
221 final void shareByEmail(String contents) {
222 sendEmailFromUri("mailto:", null, activity.getString(R.string.msg_share_subject_line), contents);
225 final void sendEmail(String address, String subject, String body) {
226 sendEmailFromUri("mailto:" + address, address, subject, body);
229 // Use public Intent fields rather than private GMail app fields to specify subject and body.
230 final void sendEmailFromUri(String uri, String email, String subject, String body) {
231 Intent intent = new Intent(Intent.ACTION_SEND, Uri.parse(uri));
233 intent.putExtra(Intent.EXTRA_EMAIL, new String[] {email});
235 putExtra(intent, Intent.EXTRA_SUBJECT, subject);
236 putExtra(intent, Intent.EXTRA_TEXT, body);
237 intent.setType("text/plain");
238 launchIntent(intent);
241 final void shareBySMS(String contents) {
242 sendSMSFromUri("smsto:", activity.getString(R.string.msg_share_subject_line) + ":\n" +
246 final void sendSMS(String phoneNumber, String body) {
247 sendSMSFromUri("smsto:" + phoneNumber, body);
250 final void sendSMSFromUri(String uri, String body) {
251 Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
252 putExtra(intent, "sms_body", body);
253 // Exit the app once the SMS is sent
254 intent.putExtra("compose_mode", true);
255 launchIntent(intent);
258 final void sendMMS(String phoneNumber, String subject, String body) {
259 sendMMSFromUri("mmsto:" + phoneNumber, subject, body);
262 final void sendMMSFromUri(String uri, String subject, String body) {
263 Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
264 // The Messaging app needs to see a valid subject or else it will treat this an an SMS.
265 if (subject == null || subject.length() == 0) {
266 putExtra(intent, "subject", activity.getString(R.string.msg_default_mms_subject));
268 putExtra(intent, "subject", subject);
270 putExtra(intent, "sms_body", body);
271 intent.putExtra("compose_mode", true);
272 launchIntent(intent);
275 final void dialPhone(String phoneNumber) {
276 launchIntent(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber)));
279 final void dialPhoneFromUri(String uri) {
280 launchIntent(new Intent(Intent.ACTION_DIAL, Uri.parse(uri)));
283 final void openMap(String geoURI) {
284 launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(geoURI)));
288 * Do a geo search using the address as the query.
290 * @param address The address to find
291 * @param title An optional title, e.g. the name of the business at this address
293 final void searchMap(String address, String title) {
294 String query = address;
295 if (title != null && title.length() > 0) {
296 query = query + " (" + title + ')';
298 launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + Uri.encode(query))));
301 final void getDirections(double latitude, double longitude) {
302 launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google." +
303 LocaleManager.getCountryTLD() + "/maps?f=d&daddr=" + latitude + ',' + longitude)));
306 // Uses the mobile-specific version of Product Search, which is formatted for small screens.
307 final void openProductSearch(String upc) {
308 Uri uri = Uri.parse("http://www.google." + LocaleManager.getProductSearchCountryTLD() +
309 "/m/products?q=" + upc + "&source=zxing");
310 launchIntent(new Intent(Intent.ACTION_VIEW, uri));
313 final void openBookSearch(String isbn) {
314 Uri uri = Uri.parse("http://books.google." + LocaleManager.getBookSearchCountryTLD() +
315 "/books?vid=isbn" + isbn);
316 launchIntent(new Intent(Intent.ACTION_VIEW, uri));
319 final void searchBookContents(String isbn) {
320 Intent intent = new Intent(Intents.SearchBookContents.ACTION);
321 intent.setClassName(activity, SearchBookContentsActivity.class.getName());
322 putExtra(intent, Intents.SearchBookContents.ISBN, isbn);
323 launchIntent(intent);
326 final void wifiConnect(WifiParsedResult wifiResult) {
327 Intent intent = new Intent(Intents.WifiConnect.ACTION);
328 intent.setClassName(activity, WifiActivity.class.getName());
329 putExtra(intent, Intents.WifiConnect.SSID, wifiResult.getSsid());
330 putExtra(intent, Intents.WifiConnect.TYPE, wifiResult.getNetworkEncryption());
331 putExtra(intent, Intents.WifiConnect.PASSWORD, wifiResult.getPassword());
332 launchIntent(intent);
335 final void openURL(String url) {
336 launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
339 final void webSearch(String query) {
340 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
341 intent.putExtra("query", query);
342 launchIntent(intent);
345 final void openGoogleShopper(String query) {
347 activity.getPackageManager().getPackageInfo(GOOGLE_SHOPPER_PACKAGE, 0);
348 // If we didn't throw, Shopper is installed, so launch it.
349 Intent intent = new Intent(Intent.ACTION_SEARCH);
350 intent.setClassName(GOOGLE_SHOPPER_PACKAGE, GOOGLE_SHOPPER_ACTIVITY);
351 intent.putExtra(SearchManager.QUERY, query);
352 activity.startActivity(intent);
353 } catch (PackageManager.NameNotFoundException e) {
354 // Otherwise offer to install it from Market.
355 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
356 builder.setTitle(R.string.msg_google_shopper_missing);
357 builder.setMessage(R.string.msg_install_google_shopper);
358 builder.setIcon(R.drawable.shopper_icon);
359 builder.setPositiveButton(R.string.button_ok, shopperMarketListener);
360 builder.setNegativeButton(R.string.button_cancel, null);
365 void launchIntent(Intent intent) {
366 if (intent != null) {
367 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
369 activity.startActivity(intent);
370 } catch (ActivityNotFoundException e) {
371 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
372 builder.setTitle(R.string.app_name);
373 builder.setMessage(R.string.msg_intent_failed);
374 builder.setPositiveButton(R.string.button_ok, null);
380 private static void putExtra(Intent intent, String key, String value) {
381 if (value != null && value.length() > 0) {
382 intent.putExtra(key, value);
386 protected void showNotOurResults(int index, AlertDialog.OnClickListener proceedListener) {
387 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
388 if (prefs.getBoolean(PreferencesActivity.KEY_NOT_OUR_RESULTS_SHOWN, false)) {
389 // already seen it, just proceed
390 proceedListener.onClick(null, index);
392 // note the user has seen it
393 prefs.edit().putBoolean(PreferencesActivity.KEY_NOT_OUR_RESULTS_SHOWN, true).commit();
394 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
395 builder.setMessage(R.string.msg_not_our_results);
396 builder.setPositiveButton(R.string.button_ok, proceedListener);
401 protected String parseCustomSearchURL() {
402 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
403 String customProductSearch = prefs.getString(PreferencesActivity.KEY_CUSTOM_PRODUCT_SEARCH, null);
404 if (customProductSearch != null && customProductSearch.trim().length() == 0) {
407 return customProductSearch;