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.style.StyleSpan;
23 import com.google.zxing.client.android.R;
24 import com.google.zxing.client.result.AddressBookParsedResult;
25 import com.google.zxing.client.result.ParsedResult;
27 import java.text.DateFormat;
28 import java.text.ParsePosition;
29 import java.text.SimpleDateFormat;
30 import java.util.Date;
32 public class AddressBookResultHandler extends ResultHandler {
34 private boolean mFields[];
35 private int mButtonCount;
37 // This takes all the work out of figuring out which buttons/actions should be in which
38 // positions, based on which fields are present in this barcode.
39 private int mapIndexToAction(int index) {
40 if (index < mButtonCount) {
42 for (int x = 0; x < MAX_BUTTON_COUNT; x++) {
43 if (mFields[x]) count++;
44 if (count == index) return x;
50 public AddressBookResultHandler(Activity activity, ParsedResult result) {
51 super(activity, result);
52 AddressBookParsedResult addressResult = (AddressBookParsedResult) result;
53 String address = addressResult.getAddress();
54 boolean hasAddress = address != null && address.length() > 0;
55 String[] phoneNumbers = addressResult.getPhoneNumbers();
56 boolean hasPhoneNumber = phoneNumbers != null && phoneNumbers.length > 0;
57 String[] emails = addressResult.getEmails();
58 boolean hasEmailAddress = emails != null && emails.length > 0;
60 mFields = new boolean[MAX_BUTTON_COUNT];
61 mFields[0] = true; // Add contact is always available
62 mFields[1] = hasAddress;
63 mFields[2] = hasPhoneNumber;
64 mFields[3] = hasEmailAddress;
67 for (int x = 0; x < MAX_BUTTON_COUNT; x++) {
68 if (mFields[x]) mButtonCount++;
72 public int getButtonCount() {
76 public int getButtonText(int index) {
77 int action = mapIndexToAction(index);
80 return R.string.button_add_contact;
82 return R.string.button_show_map;
84 return R.string.button_dial;
86 return R.string.button_email;
88 throw new ArrayIndexOutOfBoundsException();
92 public void handleButtonPress(int index) {
93 AddressBookParsedResult addressResult = (AddressBookParsedResult) mResult;
94 int action = mapIndexToAction(index);
97 addContact(addressResult.getNames(), addressResult.getPhoneNumbers(),
98 addressResult.getEmails(), addressResult.getNote(),
99 addressResult.getAddress(), addressResult.getOrg(),
100 addressResult.getTitle());
103 searchMap(addressResult.getAddress());
106 dialPhone(addressResult.getPhoneNumbers()[0]);
109 sendEmail(addressResult.getEmails()[0], null, null);
116 // Overriden so we can hyphenate phone numbers, format birthdays, and bold the name.
118 public CharSequence getDisplayContents() {
119 AddressBookParsedResult result = (AddressBookParsedResult) mResult;
120 StringBuffer contents = new StringBuffer();
121 ParsedResult.maybeAppend(result.getNames(), contents);
122 int namesLength = contents.length();
124 ParsedResult.maybeAppend(result.getTitle(), contents);
125 ParsedResult.maybeAppend(result.getOrg(), contents);
126 ParsedResult.maybeAppend(result.getAddress(), contents);
127 String[] numbers = result.getPhoneNumbers();
128 if (numbers != null) {
129 for (String number : numbers) {
130 ParsedResult.maybeAppend(PhoneNumberUtils.formatNumber(number), contents);
133 ParsedResult.maybeAppend(result.getEmails(), contents);
134 ParsedResult.maybeAppend(result.getURL(), contents);
136 String birthday = result.getBirthday();
137 if (birthday != null && birthday.length() > 0) {
138 SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
139 Date date = format.parse(birthday, new ParsePosition(0));
140 ParsedResult.maybeAppend(DateFormat.getDateInstance().format(date.getTime()), contents);
142 ParsedResult.maybeAppend(result.getNote(), contents);
144 if (namesLength > 0) {
145 // Bold the full name to make it stand out a bit.
146 SpannableString styled = new SpannableString(contents.toString());
147 styled.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, namesLength, 0);
150 return contents.toString();
154 public int getDisplayTitle() {
155 return R.string.result_address_book;