Add history feature; group some functionality into subpackages
[zxing.git] / android / src / com / google / zxing / client / android / result / AddressBookResultHandler.java
index 6e518cc..573adb4 100644 (file)
 
 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;
@@ -57,22 +69,26 @@ public class AddressBookResultHandler extends ResultHandler {
     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) {
@@ -89,8 +105,9 @@ public class AddressBookResultHandler extends ResultHandler {
     }
   }
 
+  @Override
   public void handleButtonPress(int index) {
-    AddressBookParsedResult addressResult = (AddressBookParsedResult) mResult;
+    AddressBookParsedResult addressResult = (AddressBookParsedResult) getResult();
     int action = mapIndexToAction(index);
     switch (action) {
       case 0:
@@ -118,7 +135,7 @@ public class AddressBookResultHandler extends ResultHandler {
   // 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();
@@ -127,7 +144,7 @@ public class AddressBookResultHandler extends ResultHandler {
     if (pronunciation != null && pronunciation.length() > 0) {
       contents.append("\n(");
       contents.append(pronunciation);
-      contents.append(")");
+      contents.append(')');
     }
 
     ParsedResult.maybeAppend(result.getTitle(), contents);
@@ -144,15 +161,17 @@ public class AddressBookResultHandler extends ResultHandler {
 
     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 {
@@ -160,8 +179,8 @@ public class AddressBookResultHandler extends ResultHandler {
     }
   }
 
+  @Override
   public int getDisplayTitle() {
     return R.string.result_address_book;
   }
-
 }