Now attempts to use locale-specific Google property in client. Also made some 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 android.app.Activity;
20 import android.content.Intent;
21 import android.net.Uri;
22 import android.provider.Contacts;
23 import com.google.zxing.client.android.Intents;
24 import com.google.zxing.client.android.R;
25 import com.google.zxing.client.android.SearchBookContentsActivity;
26 import com.google.zxing.client.android.LocaleManager;
27 import com.google.zxing.client.result.ParsedResult;
28 import com.google.zxing.client.result.ParsedResultType;
29
30 import java.text.ParsePosition;
31 import java.text.SimpleDateFormat;
32 import java.util.Date;
33 import java.util.GregorianCalendar;
34
35 public abstract class ResultHandler {
36
37   public static final int MAX_BUTTON_COUNT = 4;
38
39   protected final ParsedResult mResult;
40   private final Activity mActivity;
41
42   public ResultHandler(Activity activity, ParsedResult result) {
43     mResult = result;
44     mActivity = activity;
45   }
46
47   /**
48    * Indicates how many buttons the derived class wants shown.
49    *
50    * @return The integer button count.
51    */
52   public abstract int getButtonCount();
53
54   /**
55    * The text of the nth action button.
56    *
57    * @param index From 0 to getButtonCount() - 1
58    * @return The button text as a resource ID
59    */
60   public abstract int getButtonText(int index);
61
62
63   /**
64    * Execute the action which corresponds to the nth button.
65    *
66    * @param index The button that was clicked.
67    */
68   public abstract void handleButtonPress(int index);
69
70   /**
71    * Create a possibly styled string for the contents of the current barcode.
72    *
73    * @return The text to be displayed.
74    */
75   public CharSequence getDisplayContents() {
76     String contents = mResult.getDisplayResult();
77     return contents.replace("\r", "");
78   }
79
80   /**
81    * A string describing the kind of barcode that was found, e.g. "Found contact info".
82    *
83    * @return The resource ID of the string.
84    */
85   public abstract int getDisplayTitle();
86
87   /**
88    * A convenience method to get the parsed type. Should not be overridden.
89    *
90    * @return The parsed type, e.g. URI or ISBN
91    */
92   public final ParsedResultType getType() {
93     return mResult.getType();
94   }
95
96   /**
97    * Sends an intent to create a new calendar event by prepopulating the Add Event UI. Older
98    * versions of the system have a bug where the event title will not be filled out.
99    *
100    * @param summary A description of the event
101    * @param start   The start time as yyyyMMdd or yyyyMMdd'T'HHmmss or yyyyMMdd'T'HHmmss'Z'
102    * @param end     The end time as yyyyMMdd or yyyyMMdd'T'HHmmss or yyyyMMdd'T'HHmmss'Z'
103    */
104   public void addCalendarEvent(String summary, String start, String end) {
105     Intent intent = new Intent(Intent.ACTION_EDIT);
106     intent.setType("vnd.android.cursor.item/event");
107     intent.putExtra("beginTime", calculateMilliseconds(start));
108     if (start.length() == 8) {
109       intent.putExtra("allDay", true);
110     }
111     intent.putExtra("endTime", calculateMilliseconds(end));
112     intent.putExtra("title", summary);
113     launchIntent(intent);
114   }
115
116   private long calculateMilliseconds(String when) {
117     if (when.length() == 8) {
118       // Only contains year/month/day
119       SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
120       Date date = format.parse(when, new ParsePosition(0));
121       return date.getTime();
122     } else {
123       // The when string can be local time, or UTC if it ends with a Z
124       SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
125       Date date = format.parse(when.substring(0, 15), new ParsePosition(0));
126       long milliseconds = date.getTime();
127       if (when.length() == 16 && when.charAt(15) == 'Z') {
128         GregorianCalendar calendar = new GregorianCalendar();
129         int offset = (calendar.get(java.util.Calendar.ZONE_OFFSET) +
130             calendar.get(java.util.Calendar.DST_OFFSET));
131         milliseconds += offset;
132       }
133       return milliseconds;
134     }
135   }
136
137   public void addContact(String[] names, String[] phoneNumbers, String[] emails, String note,
138                          String address, String org, String title) {
139
140     Intent intent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
141     putExtra(intent, Contacts.Intents.Insert.NAME, names);
142     putExtra(intent, Contacts.Intents.Insert.PHONE, phoneNumbers);
143     putExtra(intent, Contacts.Intents.Insert.EMAIL, emails);
144     putExtra(intent, Contacts.Intents.Insert.NOTES, note);
145     putExtra(intent, Contacts.Intents.Insert.POSTAL, address);
146     putExtra(intent, Contacts.Intents.Insert.COMPANY, org);
147     putExtra(intent, Contacts.Intents.Insert.JOB_TITLE, title);
148     launchIntent(intent);
149   }
150
151   public void shareByEmail(String contents) {
152     sendEmailFromUri("mailto:", mActivity.getString(R.string.msg_share_subject_line), contents);
153   }
154
155   public void sendEmail(String address, String subject, String body) {
156     sendEmailFromUri("mailto:" + address, subject, body);
157   }
158
159   public void sendEmailFromUri(String uri, String subject, String body) {
160     Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
161     putExtra(intent, "subject", subject);
162     putExtra(intent, "body", body);
163     launchIntent(intent);
164   }
165
166   public void shareBySMS(String contents) {
167     sendSMSFromUri("smsto:", mActivity.getString(R.string.msg_share_subject_line) + ":\n" + contents);
168   }
169
170   public void sendSMS(String phoneNumber, String body) {
171     sendSMSFromUri("smsto:" + phoneNumber, body);
172   }
173
174   public void sendSMSFromUri(String uri, String body) {
175     Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
176     putExtra(intent, "sms_body", body);
177     // Exit the app once the SMS is sent
178     intent.putExtra("compose_mode", true);
179     launchIntent(intent);
180   }
181
182   public void sendMMS(String phoneNumber, String subject, String body) {
183     sendMMSFromUri("mmsto:" + phoneNumber, subject, body);
184   }
185
186   public void sendMMSFromUri(String uri, String subject, String body) {
187     Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
188     // The Messaging app needs to see a valid subject or else it will treat this an an SMS.
189     if (subject == null || subject.length() == 0) {
190       putExtra(intent, "subject", mActivity.getString(R.string.msg_default_mms_subject));
191     } else {
192       putExtra(intent, "subject", subject);
193     }
194     putExtra(intent, "sms_body", body);
195     intent.putExtra("compose_mode", true);
196     launchIntent(intent);
197   }
198
199   public void dialPhone(String phoneNumber) {
200     launchIntent(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber)));
201   }
202
203   public void dialPhoneFromUri(String uri) {
204     launchIntent(new Intent(Intent.ACTION_DIAL, Uri.parse(uri)));
205   }
206
207   public void openMap(String geoURI) {
208     launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(geoURI)));
209   }
210
211   /**
212    * Do a geo search using the address as the query.
213    *
214    * @param address The address to find
215    * @param title An optional title, e.g. the name of the business at this address
216    */
217   public void searchMap(String address, String title) {
218     String query = address;
219     if (title != null && title.length() > 0) {
220       query = query + " (" + title + ")";
221     }
222     launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + Uri.encode(query))));
223   }
224
225   public void getDirections(float latitude, float longitude) {
226     launchIntent(new Intent(Intent.ACTION_VIEW,
227         Uri.parse("http://maps.google." + LocaleManager.getCountryTLD() + "/maps?f=d&daddr=" + latitude + "," + longitude)));
228   }
229
230   public void openProductSearch(String upc) {
231     Uri uri = Uri.parse("http://www.google." + LocaleManager.getCountryTLD() + "/products?q=" + upc);
232     launchIntent(new Intent(Intent.ACTION_VIEW, uri));
233   }
234
235   public void openBookSearch(String isbn) {
236     Uri uri = Uri.parse("http://books.google." + LocaleManager.getCountryTLD() + "/books?vid=isbn" + isbn);
237     launchIntent(new Intent(Intent.ACTION_VIEW, uri));
238   }
239
240   public void searchBookContents(String isbn) {
241     Intent intent = new Intent(Intents.SearchBookContents.ACTION);
242     intent.setClassName(mActivity, SearchBookContentsActivity.class.getName());
243     putExtra(intent, Intents.SearchBookContents.ISBN, isbn);
244     launchIntent(intent);
245   }
246
247   public void openURL(String url) {
248     launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
249   }
250
251   public void webSearch(String query) {
252     Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
253     intent.putExtra("query", query);
254     launchIntent(intent);
255   }
256
257   private void launchIntent(Intent intent) {
258     if (intent != null) {
259       mActivity.startActivity(intent);
260     }
261   }
262
263   private static void putExtra(Intent intent, String key, String value) {
264     if (value != null && value.length() > 0) {
265       intent.putExtra(key, value);
266     }
267   }
268
269   // TODO: The current Contacts Intent API can only accept one value for each field, so we pick the
270   // first element in the array for names, phone numbers, and emails. It would be great to fix this.
271   private static void putExtra(Intent intent, String key, String[] value) {
272     if (value != null && value.length > 0) {
273       putExtra(intent, key, value[0]);
274     }
275   }
276
277 }