diff --git a/android/src/com/google/zxing/client/android/DecodeThread.java b/android/src/com/google/zxing/client/android/DecodeThread.java index f2ad73bf0..d49f0523c 100755 --- a/android/src/com/google/zxing/client/android/DecodeThread.java +++ b/android/src/com/google/zxing/client/android/DecodeThread.java @@ -28,7 +28,6 @@ import com.google.zxing.MultiFormatReader; import com.google.zxing.ReaderException; import com.google.zxing.Result; -import java.util.Date; import java.util.Hashtable; import java.util.Vector; @@ -151,7 +150,7 @@ final class DecodeThread extends Thread { * @param height The height of the preview frame. */ private void decode(byte[] data, int width, int height) { - Date startDate = new Date(); + long start = System.currentTimeMillis(); boolean success; Result rawResult = null; YUVMonochromeBitmapSource source = new YUVMonochromeBitmapSource(data, width, height, @@ -162,18 +161,18 @@ final class DecodeThread extends Thread { } catch (ReaderException e) { success = false; } - Date endDate = new Date(); + long end = System.currentTimeMillis(); if (success) { Message message = Message.obtain(mActivity.mHandler, R.id.decode_succeeded, rawResult); - message.arg1 = (int) (endDate.getTime() - startDate.getTime()); + message.arg1 = (int) (end - start); Bundle bundle = new Bundle(); bundle.putParcelable(BARCODE_BITMAP, source.renderToBitmap()); message.setData(bundle); message.sendToTarget(); } else { Message message = Message.obtain(mActivity.mHandler, R.id.decode_failed); - message.arg1 = (int) (endDate.getTime() - startDate.getTime()); + message.arg1 = (int) (end - start); message.sendToTarget(); } } diff --git a/android/src/com/google/zxing/client/android/result/AddressBookResultHandler.java b/android/src/com/google/zxing/client/android/result/AddressBookResultHandler.java index 84afab564..20bef0eb3 100644 --- a/android/src/com/google/zxing/client/android/result/AddressBookResultHandler.java +++ b/android/src/com/google/zxing/client/android/result/AddressBookResultHandler.java @@ -32,6 +32,8 @@ import java.util.Date; public final class AddressBookResultHandler extends ResultHandler { + private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd"); + private final boolean[] mFields; private int mButtonCount; @@ -145,8 +147,10 @@ public final class AddressBookResultHandler extends ResultHandler { String birthday = result.getBirthday(); if (birthday != null && birthday.length() > 0) { - DateFormat 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); diff --git a/android/src/com/google/zxing/client/android/result/CalendarResultHandler.java b/android/src/com/google/zxing/client/android/result/CalendarResultHandler.java index 82d5fa947..462541747 100644 --- a/android/src/com/google/zxing/client/android/result/CalendarResultHandler.java +++ b/android/src/com/google/zxing/client/android/result/CalendarResultHandler.java @@ -30,6 +30,9 @@ import java.util.GregorianCalendar; public final class CalendarResultHandler extends ResultHandler { + private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd"); + private static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HHmmss"); + private static final int[] mButtons = { R.string.button_add_calendar }; @@ -77,13 +80,17 @@ public final class CalendarResultHandler extends ResultHandler { private void appendTime(String when, StringBuffer result) { if (when.length() == 8) { // Show only year/month/day - DateFormat format = new SimpleDateFormat("yyyyMMdd"); - Date date = format.parse(when, new ParsePosition(0)); + Date date; + synchronized (DATE_FORMAT) { + date = DATE_FORMAT.parse(when, new ParsePosition(0)); + } ParsedResult.maybeAppend(DateFormat.getDateInstance().format(date.getTime()), result); } else { // The when string can be local time, or UTC if it ends with a Z - DateFormat format = new SimpleDateFormat("yyyyMMdd'T'HHmmss"); - Date date = format.parse(when.substring(0, 15), new ParsePosition(0)); + Date date; + synchronized (DATE_TIME_FORMAT) { + date = DATE_TIME_FORMAT.parse(when.substring(0, 15), new ParsePosition(0)); + } long milliseconds = date.getTime(); if (when.length() == 16 && when.charAt(15) == 'Z') { Calendar calendar = new GregorianCalendar(); diff --git a/android/src/com/google/zxing/client/android/result/ResultHandler.java b/android/src/com/google/zxing/client/android/result/ResultHandler.java index 2e49aa985..ffbdf81f7 100644 --- a/android/src/com/google/zxing/client/android/result/ResultHandler.java +++ b/android/src/com/google/zxing/client/android/result/ResultHandler.java @@ -37,6 +37,9 @@ import java.util.GregorianCalendar; public abstract class ResultHandler { + private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd"); + private static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HHmmss"); + public static final int MAX_BUTTON_COUNT = 4; protected final ParsedResult mResult; @@ -119,13 +122,17 @@ public abstract class ResultHandler { private long calculateMilliseconds(String when) { if (when.length() == 8) { // Only contains year/month/day - DateFormat format = new SimpleDateFormat("yyyyMMdd"); - Date date = format.parse(when, new ParsePosition(0)); + Date date; + synchronized (DATE_FORMAT) { + date = DATE_FORMAT.parse(when, new ParsePosition(0)); + } return date.getTime(); } else { // The when string can be local time, or UTC if it ends with a Z - DateFormat format = new SimpleDateFormat("yyyyMMdd'T'HHmmss"); - Date date = format.parse(when.substring(0, 15), new ParsePosition(0)); + Date date; + synchronized (DATE_TIME_FORMAT) { + date = DATE_TIME_FORMAT.parse(when.substring(0, 15), new ParsePosition(0)); + } long milliseconds = date.getTime(); if (when.length() == 16 && when.charAt(15) == 'Z') { Calendar calendar = new GregorianCalendar();