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++) {
46 if (mFields[x]) count++;
47 if (count == index) return x;
53 public AddressBookResultHandler(Activity activity, ParsedResult result) {
54 super(activity, result);
55 AddressBookParsedResult addressResult = (AddressBookParsedResult) result;
56 String address = addressResult.getAddress();
57 boolean hasAddress = address != null && address.length() > 0;
58 String[] phoneNumbers = addressResult.getPhoneNumbers();
59 boolean hasPhoneNumber = phoneNumbers != null && phoneNumbers.length > 0;
60 String[] emails = addressResult.getEmails();
61 boolean hasEmailAddress = emails != null && emails.length > 0;
63 mFields = new boolean[MAX_BUTTON_COUNT];
64 mFields[0] = true; // Add contact is always available
65 mFields[1] = hasAddress;
66 mFields[2] = hasPhoneNumber;
67 mFields[3] = hasEmailAddress;
70 for (int x = 0; x < MAX_BUTTON_COUNT; x++) {
71 if (mFields[x]) mButtonCount++;
75 public int getButtonCount() {
79 public int getButtonText(int index) {
80 int action = mapIndexToAction(index);
83 return R.string.button_add_contact;
85 return R.string.button_show_map;
87 return R.string.button_dial;
89 return R.string.button_email;
91 throw new ArrayIndexOutOfBoundsException();
95 public void handleButtonPress(int index) {
96 AddressBookParsedResult addressResult = (AddressBookParsedResult) mResult;
97 int action = mapIndexToAction(index);
100 addContact(addressResult.getNames(), addressResult.getPhoneNumbers(),
101 addressResult.getEmails(), addressResult.getNote(),
102 addressResult.getAddress(), addressResult.getOrg(),
103 addressResult.getTitle());
106 String[] names = addressResult.getNames();
107 String title = names != null ? names[0] : null;
108 searchMap(addressResult.getAddress(), title);
111 dialPhone(addressResult.getPhoneNumbers()[0]);
114 sendEmail(addressResult.getEmails()[0], null, null);
121 // Overriden so we can hyphenate phone numbers, format birthdays, and bold the name.
123 public CharSequence getDisplayContents() {
124 AddressBookParsedResult result = (AddressBookParsedResult) mResult;
125 StringBuffer contents = new StringBuffer();
126 ParsedResult.maybeAppend(result.getNames(), contents);
127 int namesLength = contents.length();
129 String pronunciation = result.getPronunciation();
130 if (pronunciation != null && pronunciation.length() > 0) {
131 contents.append("\n(");
132 contents.append(pronunciation);
133 contents.append(")");
136 ParsedResult.maybeAppend(result.getTitle(), contents);
137 ParsedResult.maybeAppend(result.getOrg(), contents);
138 ParsedResult.maybeAppend(result.getAddress(), contents);
139 String[] numbers = result.getPhoneNumbers();
140 if (numbers != null) {
141 for (String number : numbers) {
142 ParsedResult.maybeAppend(PhoneNumberUtils.formatNumber(number), contents);
145 ParsedResult.maybeAppend(result.getEmails(), contents);
146 ParsedResult.maybeAppend(result.getURL(), contents);
148 String birthday = result.getBirthday();
149 if (birthday != null && birthday.length() > 0) {
151 synchronized (DATE_FORMAT) {
152 date = DATE_FORMAT.parse(birthday, new ParsePosition(0));
154 ParsedResult.maybeAppend(DateFormat.getDateInstance().format(date.getTime()), contents);
156 ParsedResult.maybeAppend(result.getNote(), contents);
158 if (namesLength > 0) {
159 // Bold the full name to make it stand out a bit.
160 Spannable styled = new SpannableString(contents.toString());
161 styled.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, namesLength, 0);
164 return contents.toString();
168 public int getDisplayTitle() {
169 return R.string.result_address_book;