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 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;
30 import java.text.ParsePosition;
31 import java.text.SimpleDateFormat;
32 import java.util.Date;
33 import java.util.GregorianCalendar;
35 public abstract class ResultHandler {
37 public static final int MAX_BUTTON_COUNT = 4;
39 protected final ParsedResult mResult;
40 private final Activity mActivity;
42 public ResultHandler(Activity activity, ParsedResult result) {
48 * Indicates how many buttons the derived class wants shown.
50 * @return The integer button count.
52 public abstract int getButtonCount();
55 * The text of the nth action button.
57 * @param index From 0 to getButtonCount() - 1
58 * @return The button text as a resource ID
60 public abstract int getButtonText(int index);
64 * Execute the action which corresponds to the nth button.
66 * @param index The button that was clicked.
68 public abstract void handleButtonPress(int index);
71 * Create a possibly styled string for the contents of the current barcode.
73 * @return The text to be displayed.
75 public CharSequence getDisplayContents() {
76 String contents = mResult.getDisplayResult();
77 return contents.replace("\r", "");
81 * A string describing the kind of barcode that was found, e.g. "Found contact info".
83 * @return The resource ID of the string.
85 public abstract int getDisplayTitle();
88 * A convenience method to get the parsed type. Should not be overridden.
90 * @return The parsed type, e.g. URI or ISBN
92 public final ParsedResultType getType() {
93 return mResult.getType();
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.
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'
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);
111 intent.putExtra("endTime", calculateMilliseconds(end));
112 intent.putExtra("title", summary);
113 launchIntent(intent);
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();
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;
137 public void addContact(String[] names, String[] phoneNumbers, String[] emails, String note,
138 String address, String org, String title) {
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);
151 public void shareByEmail(String contents) {
152 sendEmailFromUri("mailto:", mActivity.getString(R.string.msg_share_subject_line), contents);
155 public void sendEmail(String address, String subject, String body) {
156 sendEmailFromUri("mailto:" + address, subject, body);
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);
166 public void shareBySMS(String contents) {
167 sendSMSFromUri("smsto:", mActivity.getString(R.string.msg_share_subject_line) + ":\n" + contents);
170 public void sendSMS(String phoneNumber, String body) {
171 sendSMSFromUri("smsto:" + phoneNumber, body);
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);
182 public void sendMMS(String phoneNumber, String subject, String body) {
183 sendMMSFromUri("mmsto:" + phoneNumber, subject, body);
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));
192 putExtra(intent, "subject", subject);
194 putExtra(intent, "sms_body", body);
195 intent.putExtra("compose_mode", true);
196 launchIntent(intent);
199 public void dialPhone(String phoneNumber) {
200 launchIntent(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber)));
203 public void dialPhoneFromUri(String uri) {
204 launchIntent(new Intent(Intent.ACTION_DIAL, Uri.parse(uri)));
207 public void openMap(String geoURI) {
208 launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(geoURI)));
212 * Do a geo search using the address as the query.
214 * @param address The address to find
215 * @param title An optional title, e.g. the name of the business at this address
217 public void searchMap(String address, String title) {
218 String query = address;
219 if (title != null && title.length() > 0) {
220 query = query + " (" + title + ")";
222 launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + Uri.encode(query))));
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)));
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));
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));
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);
247 public void openURL(String url) {
248 launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
251 public void webSearch(String query) {
252 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
253 intent.putExtra("query", query);
254 launchIntent(intent);
257 private void launchIntent(Intent intent) {
258 if (intent != null) {
259 mActivity.startActivity(intent);
263 private static void putExtra(Intent intent, String key, String value) {
264 if (value != null && value.length() > 0) {
265 intent.putExtra(key, value);
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]);