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.telephony.PhoneNumberUtils;
21 import android.text.SpannableString;
22 import android.text.Spannable;
23 import android.text.style.StyleSpan;
24 import com.google.zxing.client.android.R;
25 import com.google.zxing.client.result.AddressBookParsedResult;
26 import com.google.zxing.client.result.ParsedResult;
28 import java.text.DateFormat;
29 import java.text.ParsePosition;
30 import java.text.SimpleDateFormat;
31 import java.util.Date;
33 public final class AddressBookResultHandler extends ResultHandler {
35 private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
37 private final boolean[] mFields;
38 private int mButtonCount;
40 // This takes all the work out of figuring out which buttons/actions should be in which
41 // positions, based on which fields are present in this barcode.
42 private int mapIndexToAction(int index) {
43 if (index < mButtonCount) {
45 for (int x = 0; x < MAX_BUTTON_COUNT; x++) {
57 public AddressBookResultHandler(Activity activity, ParsedResult result) {
58 super(activity, result);
59 AddressBookParsedResult addressResult = (AddressBookParsedResult) result;
60 String address = addressResult.getAddress();
61 boolean hasAddress = address != null && address.length() > 0;
62 String[] phoneNumbers = addressResult.getPhoneNumbers();
63 boolean hasPhoneNumber = phoneNumbers != null && phoneNumbers.length > 0;
64 String[] emails = addressResult.getEmails();
65 boolean hasEmailAddress = emails != null && emails.length > 0;
67 mFields = new boolean[MAX_BUTTON_COUNT];
68 mFields[0] = true; // Add contact is always available
69 mFields[1] = hasAddress;
70 mFields[2] = hasPhoneNumber;
71 mFields[3] = hasEmailAddress;
74 for (int x = 0; x < MAX_BUTTON_COUNT; x++) {
82 public int getButtonCount() {
87 public int getButtonText(int index) {
88 int action = mapIndexToAction(index);
91 return R.string.button_add_contact;
93 return R.string.button_show_map;
95 return R.string.button_dial;
97 return R.string.button_email;
99 throw new ArrayIndexOutOfBoundsException();
104 public void handleButtonPress(int index) {
105 AddressBookParsedResult addressResult = (AddressBookParsedResult) mResult;
106 int action = mapIndexToAction(index);
109 addContact(addressResult.getNames(), addressResult.getPhoneNumbers(),
110 addressResult.getEmails(), addressResult.getNote(),
111 addressResult.getAddress(), addressResult.getOrg(),
112 addressResult.getTitle());
115 String[] names = addressResult.getNames();
116 String title = names != null ? names[0] : null;
117 searchMap(addressResult.getAddress(), title);
120 dialPhone(addressResult.getPhoneNumbers()[0]);
123 sendEmail(addressResult.getEmails()[0], null, null);
130 // Overriden so we can hyphenate phone numbers, format birthdays, and bold the name.
132 public CharSequence getDisplayContents() {
133 AddressBookParsedResult result = (AddressBookParsedResult) mResult;
134 StringBuffer contents = new StringBuffer();
135 ParsedResult.maybeAppend(result.getNames(), contents);
136 int namesLength = contents.length();
138 String pronunciation = result.getPronunciation();
139 if (pronunciation != null && pronunciation.length() > 0) {
140 contents.append("\n(");
141 contents.append(pronunciation);
142 contents.append(")");
145 ParsedResult.maybeAppend(result.getTitle(), contents);
146 ParsedResult.maybeAppend(result.getOrg(), contents);
147 ParsedResult.maybeAppend(result.getAddress(), contents);
148 String[] numbers = result.getPhoneNumbers();
149 if (numbers != null) {
150 for (String number : numbers) {
151 ParsedResult.maybeAppend(PhoneNumberUtils.formatNumber(number), contents);
154 ParsedResult.maybeAppend(result.getEmails(), contents);
155 ParsedResult.maybeAppend(result.getURL(), contents);
157 String birthday = result.getBirthday();
158 if (birthday != null && birthday.length() > 0) {
160 synchronized (DATE_FORMAT) {
161 date = DATE_FORMAT.parse(birthday, new ParsePosition(0));
163 ParsedResult.maybeAppend(DateFormat.getDateInstance().format(date.getTime()), contents);
165 ParsedResult.maybeAppend(result.getNote(), contents);
167 if (namesLength > 0) {
168 // Bold the full name to make it stand out a bit.
169 Spannable styled = new SpannableString(contents.toString());
170 styled.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, namesLength, 0);
173 return contents.toString();
178 public int getDisplayTitle() {
179 return R.string.result_address_book;