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