Small style stuff
[zxing.git] / android / src / com / google / zxing / client / android / result / ResultHandler.java
1 /*
2  * Copyright (C) 2008 ZXing authors
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.google.zxing.client.android.result;
18
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;
30
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
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;
50
51 /**
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.
54  *
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.
58  *
59  * @author dswitkin@google.com (Daniel Switkin)
60  */
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");
64
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";
71
72   public static final int MAX_BUTTON_COUNT = 4;
73
74   private final ParsedResult result;
75   private final Activity activity;
76   private final Result rawResult;
77   private final String customProductSearch;
78
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)));
84     }
85   };
86
87   ResultHandler(Activity activity, ParsedResult result) {
88     this(activity, result, null);
89   }
90
91   ResultHandler(Activity activity, ParsedResult result, Result rawResult) {
92     this.result = result;
93     this.activity = activity;
94     this.rawResult = rawResult;
95     this.customProductSearch = parseCustomSearchURL();
96
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);
101   }
102
103   ParsedResult getResult() {
104     return result;
105   }
106
107   boolean hasCustomProductSearch() {
108     return customProductSearch != null;
109   }
110
111   /**
112    * Indicates how many buttons the derived class wants shown.
113    *
114    * @return The integer button count.
115    */
116   public abstract int getButtonCount();
117
118   /**
119    * The text of the nth action button.
120    *
121    * @param index From 0 to getButtonCount() - 1
122    * @return The button text as a resource ID
123    */
124   public abstract int getButtonText(int index);
125
126
127   /**
128    * Execute the action which corresponds to the nth button.
129    *
130    * @param index The button that was clicked.
131    */
132   public abstract void handleButtonPress(int index);
133
134   /**
135    * The Google Shopper button is special and is not handled by the abstract button methods above.
136    *
137    * @param listener The on click listener to install for this button.
138    */
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);
143   }
144
145   /**
146    * Create a possibly styled string for the contents of the current barcode.
147    *
148    * @return The text to be displayed.
149    */
150   public CharSequence getDisplayContents() {
151     String contents = result.getDisplayResult();
152     return contents.replace("\r", "");
153   }
154
155   /**
156    * A string describing the kind of barcode that was found, e.g. "Found contact info".
157    *
158    * @return The resource ID of the string.
159    */
160   public abstract int getDisplayTitle();
161
162   /**
163    * A convenience method to get the parsed type. Should not be overridden.
164    *
165    * @return The parsed type, e.g. URI or ISBN
166    */
167   public final ParsedResultType getType() {
168     return result.getType();
169   }
170
171   /**
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.
174    *
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
180    */
181   final void addCalendarEvent(String summary,
182                               String start,
183                               String end,
184                               String location,
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);
191     }
192     if (end == null) {
193       end = start;
194     }
195     intent.putExtra("endTime", calculateMilliseconds(end));
196     intent.putExtra("title", summary);
197     intent.putExtra("eventLocation", location);
198     intent.putExtra("description", description);
199     launchIntent(intent);
200   }
201
202   private static long calculateMilliseconds(String when) {
203     if (when.length() == 8) {
204       // Only contains year/month/day
205       Date date;
206       synchronized (DATE_FORMAT) {
207         date = DATE_FORMAT.parse(when, new ParsePosition(0));
208       }
209       return date.getTime();
210     } else {
211       // The when string can be local time, or UTC if it ends with a Z
212       Date date;
213       synchronized (DATE_TIME_FORMAT) {
214        date = DATE_TIME_FORMAT.parse(when.substring(0, 15), new ParsePosition(0));
215       }
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;
221       }
222       return milliseconds;
223     }
224   }
225
226   final void addContact(String[] names, String[] phoneNumbers, String[] emails, String note,
227                          String address, String org, String title) {
228
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);
232
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]);
237     }
238
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]);
242     }
243
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);
249   }
250
251   final void shareByEmail(String contents) {
252     sendEmailFromUri("mailto:", null, activity.getString(R.string.msg_share_subject_line), contents);
253   }
254
255   final void sendEmail(String address, String subject, String body) {
256     sendEmailFromUri("mailto:" + address, address, subject, body);
257   }
258
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));
262     if (email != null) {
263       intent.putExtra(Intent.EXTRA_EMAIL, new String[] {email});
264     }
265     putExtra(intent, Intent.EXTRA_SUBJECT, subject);
266     putExtra(intent, Intent.EXTRA_TEXT, body);
267     intent.setType("text/plain");
268     launchIntent(intent);
269   }
270
271   final void shareBySMS(String contents) {
272     sendSMSFromUri("smsto:", activity.getString(R.string.msg_share_subject_line) + ":\n" +
273         contents);
274   }
275
276   final void sendSMS(String phoneNumber, String body) {
277     sendSMSFromUri("smsto:" + phoneNumber, body);
278   }
279
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);
286   }
287
288   final void sendMMS(String phoneNumber, String subject, String body) {
289     sendMMSFromUri("mmsto:" + phoneNumber, subject, body);
290   }
291
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));
297     } else {
298       putExtra(intent, "subject", subject);
299     }
300     putExtra(intent, "sms_body", body);
301     intent.putExtra("compose_mode", true);
302     launchIntent(intent);
303   }
304
305   final void dialPhone(String phoneNumber) {
306     launchIntent(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber)));
307   }
308
309   final void dialPhoneFromUri(String uri) {
310     launchIntent(new Intent(Intent.ACTION_DIAL, Uri.parse(uri)));
311   }
312
313   final void openMap(String geoURI) {
314     launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(geoURI)));
315   }
316
317   /**
318    * Do a geo search using the address as the query.
319    *
320    * @param address The address to find
321    * @param title An optional title, e.g. the name of the business at this address
322    */
323   final void searchMap(String address, String title) {
324     String query = address;
325     if (title != null && title.length() > 0) {
326       query = query + " (" + title + ')';
327     }
328     launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + Uri.encode(query))));
329   }
330
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)));
334   }
335
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));
341   }
342
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));
347   }
348
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);
354   }
355
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);
363   }
364
365   final void openURL(String url) {
366     launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
367   }
368
369   final void webSearch(String query) {
370     Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
371     intent.putExtra("query", query);
372     launchIntent(intent);
373   }
374
375   final void openGoogleShopper(String query) {
376     try {
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);
391       builder.show();
392     }
393   }
394
395   void launchIntent(Intent intent) {
396     if (intent != null) {
397       intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
398       try {
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);
405         builder.show();
406       }
407     }
408   }
409
410   private static void putExtra(Intent intent, String key, String value) {
411     if (value != null && value.length() > 0) {
412       intent.putExtra(key, value);
413     }
414   }
415
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);
421     } else {
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);
427       builder.show();
428     }
429   }
430
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) {
435       return null;
436     }
437     return customProductSearch;
438   }
439
440   String fillInCustomSearchURL(String text) {
441     String url = customProductSearch.replace("%s", text);
442     if (rawResult != null) {
443       url = url.replace("%f", rawResult.getBarcodeFormat().toString());
444     }
445     return url;
446   }
447
448 }