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 cz.cvut.fel.dce.barcodescanner.result;
19 import android.app.Activity;
20 import android.telephony.PhoneNumberUtils;
21 import android.text.Spannable;
22 import android.text.SpannableString;
23 import android.text.style.StyleSpan;
25 import cz.cvut.fel.dce.barcodescanner.R;
26 import com.google.zxing.client.result.AddressBookParsedResult;
27 import com.google.zxing.client.result.ParsedResult;
29 import java.text.DateFormat;
30 import java.text.ParseException;
31 import java.text.SimpleDateFormat;
32 import java.util.Date;
33 import java.util.Locale;
36 * Handles address book entries.
38 * @author dswitkin@google.com (Daniel Switkin)
40 public final class AddressBookResultHandler extends ResultHandler {
42 private static final DateFormat[] DATE_FORMATS = {
43 new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH),
44 new SimpleDateFormat("yyyyMMdd'T'HHmmss", Locale.ENGLISH),
45 new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH),
46 new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH),
49 for (DateFormat format : DATE_FORMATS) {
50 format.setLenient(false);
54 private static final int[] BUTTON_TEXTS = {
55 R.string.button_add_contact,
56 R.string.button_show_map,
58 R.string.button_email,
61 private final boolean[] fields;
62 private int buttonCount;
64 // This takes all the work out of figuring out which buttons/actions should be in which
65 // positions, based on which fields are present in this barcode.
66 private int mapIndexToAction(int index) {
67 if (index < buttonCount) {
69 for (int x = 0; x < MAX_BUTTON_COUNT; x++) {
81 public AddressBookResultHandler(Activity activity, ParsedResult result) {
82 super(activity, result);
83 AddressBookParsedResult addressResult = (AddressBookParsedResult) result;
84 String[] addresses = addressResult.getAddresses();
85 boolean hasAddress = addresses != null && addresses.length > 0 && addresses[0] != null && !addresses[0].isEmpty();
86 String[] phoneNumbers = addressResult.getPhoneNumbers();
87 boolean hasPhoneNumber = phoneNumbers != null && phoneNumbers.length > 0;
88 String[] emails = addressResult.getEmails();
89 boolean hasEmailAddress = emails != null && emails.length > 0;
91 fields = new boolean[MAX_BUTTON_COUNT];
92 fields[0] = true; // Add contact is always available
93 fields[1] = hasAddress;
94 fields[2] = hasPhoneNumber;
95 fields[3] = hasEmailAddress;
98 for (int x = 0; x < MAX_BUTTON_COUNT; x++) {
106 public int getButtonCount() {
111 public int getButtonText(int index) {
112 return BUTTON_TEXTS[mapIndexToAction(index)];
116 public void handleButtonPress(int index) {
117 AddressBookParsedResult addressResult = (AddressBookParsedResult) getResult();
118 String[] addresses = addressResult.getAddresses();
119 String address1 = addresses == null || addresses.length < 1 ? null : addresses[0];
120 String[] addressTypes = addressResult.getAddressTypes();
121 String address1Type = addressTypes == null || addressTypes.length < 1 ? null : addressTypes[0];
122 int action = mapIndexToAction(index);
125 addContact(addressResult.getNames(),
126 addressResult.getNicknames(),
127 addressResult.getPronunciation(),
128 addressResult.getPhoneNumbers(),
129 addressResult.getPhoneTypes(),
130 addressResult.getEmails(),
131 addressResult.getEmailTypes(),
132 addressResult.getNote(),
133 addressResult.getInstantMessenger(),
136 addressResult.getOrg(),
137 addressResult.getTitle(),
138 addressResult.getURLs(),
139 addressResult.getBirthday(),
140 addressResult.getGeo());
146 dialPhone(addressResult.getPhoneNumbers()[0]);
149 sendEmail(addressResult.getEmails(), null, null, null, null);
156 private static Date parseDate(String s) {
157 for (DateFormat currentFormat : DATE_FORMATS) {
159 return currentFormat.parse(s);
160 } catch (ParseException e) {
167 // Overriden so we can hyphenate phone numbers, format birthdays, and bold the name.
169 public CharSequence getDisplayContents() {
170 AddressBookParsedResult result = (AddressBookParsedResult) getResult();
171 StringBuilder contents = new StringBuilder(100);
172 ParsedResult.maybeAppend(result.getNames(), contents);
173 int namesLength = contents.length();
175 String pronunciation = result.getPronunciation();
176 if (pronunciation != null && !pronunciation.isEmpty()) {
177 contents.append("\n(");
178 contents.append(pronunciation);
179 contents.append(')');
182 ParsedResult.maybeAppend(result.getTitle(), contents);
183 ParsedResult.maybeAppend(result.getOrg(), contents);
184 ParsedResult.maybeAppend(result.getAddresses(), contents);
185 String[] numbers = result.getPhoneNumbers();
186 if (numbers != null) {
187 for (String number : numbers) {
188 if (number != null) {
189 ParsedResult.maybeAppend(PhoneNumberUtils.formatNumber(number), contents);
193 ParsedResult.maybeAppend(result.getEmails(), contents);
194 ParsedResult.maybeAppend(result.getURLs(), contents);
196 String birthday = result.getBirthday();
197 if (birthday != null && !birthday.isEmpty()) {
198 Date date = parseDate(birthday);
200 ParsedResult.maybeAppend(DateFormat.getDateInstance(DateFormat.MEDIUM).format(date.getTime()), contents);
203 ParsedResult.maybeAppend(result.getNote(), contents);
205 if (namesLength > 0) {
206 // Bold the full name to make it stand out a bit.
207 Spannable styled = new SpannableString(contents.toString());
208 styled.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, namesLength, 0);
211 return contents.toString();
216 public int getDisplayTitle() {
217 return R.string.result_address_book;