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