1 package com.android.contacts.datepicker; 2 3 import android.widget.NumberPicker; 4 5 import java.text.DecimalFormatSymbols; 6 import java.util.Locale; 7 8 /** 9 * Copy of {@link android.widget.NumberPicker.TwoDigitFormatter}, modified 10 * so that it doesn't use libcore. 11 * 12 * Use a custom NumberPicker formatting callback to use two-digit minutes 13 * strings like "01". Keeping a static formatter etc. is the most efficient 14 * way to do this; it avoids creating temporary objects on every call to 15 * format(). 16 */ 17 public class TwoDigitFormatter implements NumberPicker.Formatter { 18 final StringBuilder mBuilder = new StringBuilder(); 19 20 char mZeroDigit; 21 java.util.Formatter mFmt; 22 23 final Object[] mArgs = new Object[1]; 24 25 public TwoDigitFormatter() { 26 final Locale locale = Locale.getDefault(); 27 init(locale); 28 } 29 30 private void init(Locale locale) { 31 mFmt = createFormatter(locale); 32 mZeroDigit = getZeroDigit(locale); 33 } 34 35 public String format(int value) { 36 final Locale currentLocale = Locale.getDefault(); 37 if (mZeroDigit != getZeroDigit(currentLocale)) { 38 init(currentLocale); 39 } 40 mArgs[0] = value; 41 mBuilder.delete(0, mBuilder.length()); 42 mFmt.format("%02d", mArgs); 43 return mFmt.toString(); 44 } 45 46 private static char getZeroDigit(Locale locale) { 47 // The original TwoDigitFormatter directly referenced LocaleData's value. Instead, 48 // we need to use the public DecimalFormatSymbols API. 49 return DecimalFormatSymbols.getInstance(locale).getZeroDigit(); 50 } 51 52 private java.util.Formatter createFormatter(Locale locale) { 53 return new java.util.Formatter(mBuilder, locale); 54 } 55 } 56