package com.google.zxing.client.android.result;
+import com.google.zxing.client.android.R;
+import com.google.zxing.client.result.AddressBookParsedResult;
+import com.google.zxing.client.result.ParsedResult;
+
import android.app.Activity;
import android.telephony.PhoneNumberUtils;
+import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.StyleSpan;
-import com.google.zxing.client.android.R;
-import com.google.zxing.client.result.AddressBookParsedResult;
-import com.google.zxing.client.result.ParsedResult;
import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
-public class AddressBookResultHandler extends ResultHandler {
+/**
+ * Handles address book entries.
+ *
+ * @author dswitkin@google.com (Daniel Switkin)
+ */
+public final class AddressBookResultHandler extends ResultHandler {
+ private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
- private final boolean[] mFields;
- private int mButtonCount;
+ private final boolean[] fields;
+ private int buttonCount;
// This takes all the work out of figuring out which buttons/actions should be in which
// positions, based on which fields are present in this barcode.
private int mapIndexToAction(int index) {
- if (index < mButtonCount) {
+ if (index < buttonCount) {
int count = -1;
for (int x = 0; x < MAX_BUTTON_COUNT; x++) {
- if (mFields[x]) count++;
- if (count == index) return x;
+ if (fields[x]) {
+ count++;
+ }
+ if (count == index) {
+ return x;
+ }
}
}
return -1;
String[] emails = addressResult.getEmails();
boolean hasEmailAddress = emails != null && emails.length > 0;
- mFields = new boolean[MAX_BUTTON_COUNT];
- mFields[0] = true; // Add contact is always available
- mFields[1] = hasAddress;
- mFields[2] = hasPhoneNumber;
- mFields[3] = hasEmailAddress;
+ fields = new boolean[MAX_BUTTON_COUNT];
+ fields[0] = true; // Add contact is always available
+ fields[1] = hasAddress;
+ fields[2] = hasPhoneNumber;
+ fields[3] = hasEmailAddress;
- mButtonCount = 0;
+ buttonCount = 0;
for (int x = 0; x < MAX_BUTTON_COUNT; x++) {
- if (mFields[x]) mButtonCount++;
+ if (fields[x]) {
+ buttonCount++;
+ }
}
}
+ @Override
public int getButtonCount() {
- return mButtonCount;
+ return buttonCount;
}
+ @Override
public int getButtonText(int index) {
int action = mapIndexToAction(index);
switch (action) {
}
}
+ @Override
public void handleButtonPress(int index) {
- AddressBookParsedResult addressResult = (AddressBookParsedResult) mResult;
+ AddressBookParsedResult addressResult = (AddressBookParsedResult) getResult();
int action = mapIndexToAction(index);
switch (action) {
case 0:
// Overriden so we can hyphenate phone numbers, format birthdays, and bold the name.
@Override
public CharSequence getDisplayContents() {
- AddressBookParsedResult result = (AddressBookParsedResult) mResult;
+ AddressBookParsedResult result = (AddressBookParsedResult) getResult();
StringBuffer contents = new StringBuffer();
ParsedResult.maybeAppend(result.getNames(), contents);
int namesLength = contents.length();
if (pronunciation != null && pronunciation.length() > 0) {
contents.append("\n(");
contents.append(pronunciation);
- contents.append(")");
+ contents.append(')');
}
ParsedResult.maybeAppend(result.getTitle(), contents);
String birthday = result.getBirthday();
if (birthday != null && birthday.length() > 0) {
- SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
- Date date = format.parse(birthday, new ParsePosition(0));
+ Date date;
+ synchronized (DATE_FORMAT) {
+ date = DATE_FORMAT.parse(birthday, new ParsePosition(0));
+ }
ParsedResult.maybeAppend(DateFormat.getDateInstance().format(date.getTime()), contents);
}
ParsedResult.maybeAppend(result.getNote(), contents);
if (namesLength > 0) {
// Bold the full name to make it stand out a bit.
- SpannableString styled = new SpannableString(contents.toString());
+ Spannable styled = new SpannableString(contents.toString());
styled.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, namesLength, 0);
return styled;
} else {
}
}
+ @Override
public int getDisplayTitle() {
return R.string.result_address_book;
}
-
}