Search in sources :

Example 6 with Formatter

use of java.util.Formatter in project android_frameworks_base by ParanoidAndroid.

the class DateUtils method formatElapsedTime.

/**
     * Formats an elapsed time in a format like "MM:SS" or "H:MM:SS" (using a form
     * suited to the current locale), similar to that used on the call-in-progress
     * screen.
     *
     * @param recycle {@link StringBuilder} to recycle, or null to use a temporary one.
     * @param elapsedSeconds the elapsed time in seconds.
     */
public static String formatElapsedTime(StringBuilder recycle, long elapsedSeconds) {
    // Break the elapsed seconds into hours, minutes, and seconds.
    long hours = 0;
    long minutes = 0;
    long seconds = 0;
    if (elapsedSeconds >= 3600) {
        hours = elapsedSeconds / 3600;
        elapsedSeconds -= hours * 3600;
    }
    if (elapsedSeconds >= 60) {
        minutes = elapsedSeconds / 60;
        elapsedSeconds -= minutes * 60;
    }
    seconds = elapsedSeconds;
    // Create a StringBuilder if we weren't given one to recycle.
    // TODO: if we cared, we could have a thread-local temporary StringBuilder.
    StringBuilder sb = recycle;
    if (sb == null) {
        sb = new StringBuilder(8);
    } else {
        sb.setLength(0);
    }
    // Format the broken-down time in a locale-appropriate way.
    // TODO: use icu4c when http://unicode.org/cldr/trac/ticket/3407 is fixed.
    Formatter f = new Formatter(sb, Locale.getDefault());
    initFormatStrings();
    if (hours > 0) {
        return f.format(sElapsedFormatHMMSS, hours, minutes, seconds).toString();
    } else {
        return f.format(sElapsedFormatMMSS, minutes, seconds).toString();
    }
}
Also used : Formatter(java.util.Formatter)

Example 7 with Formatter

use of java.util.Formatter in project android_frameworks_base by ParanoidAndroid.

the class Chronometer method updateText.

private synchronized void updateText(long now) {
    long seconds = now - mBase;
    seconds /= 1000;
    String text = DateUtils.formatElapsedTime(mRecycle, seconds);
    if (mFormat != null) {
        Locale loc = Locale.getDefault();
        if (mFormatter == null || !loc.equals(mFormatterLocale)) {
            mFormatterLocale = loc;
            mFormatter = new Formatter(mFormatBuilder, loc);
        }
        mFormatBuilder.setLength(0);
        mFormatterArgs[0] = text;
        try {
            mFormatter.format(mFormat, mFormatterArgs);
            text = mFormatBuilder.toString();
        } catch (IllegalFormatException ex) {
            if (!mLogged) {
                Log.w(TAG, "Illegal format string: " + mFormat);
                mLogged = true;
            }
        }
    }
    setText(text);
}
Also used : Locale(java.util.Locale) Formatter(java.util.Formatter) IllegalFormatException(java.util.IllegalFormatException)

Example 8 with Formatter

use of java.util.Formatter in project android_frameworks_base by ParanoidAndroid.

the class StaticLayoutDirectionsTest method testDirections.

// @SmallTest
public void testDirections() {
    StringBuilder buf = new StringBuilder("\n");
    Formatter f = new Formatter(buf);
    LayoutBuilder b = StaticLayoutTest.builder();
    for (int i = 0; i < texts.length; ++i) {
        b.setText(pseudoBidiToReal(texts[i]));
        checkDirections(b.build(), i, b.text, expected, f);
    }
    if (buf.length() > 1) {
        fail(buf.toString());
    }
}
Also used : LayoutBuilder(android.text.StaticLayoutTest.LayoutBuilder) Formatter(java.util.Formatter)

Example 9 with Formatter

use of java.util.Formatter in project OpenMEAP by OpenMEAP.

the class BankingService method getSha1.

private static String getSha1(String value) {
    MessageDigest sha1;
    try {
        sha1 = MessageDigest.getInstance("SHA1");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
    Formatter formatter = new Formatter();
    for (byte b : sha1.digest(value.getBytes())) {
        formatter.format("%02x", b);
    }
    return formatter.toString();
}
Also used : Formatter(java.util.Formatter) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 10 with Formatter

use of java.util.Formatter in project atlas by alibaba.

the class MemberIdsSection method getTooManyMembersMessage.

private String getTooManyMembersMessage() {
    Map<String, AtomicInteger> membersByPackage = new TreeMap<String, AtomicInteger>();
    for (Object member : items()) {
        String packageName = ((MemberIdItem) member).getDefiningClass().getPackageName();
        AtomicInteger count = membersByPackage.get(packageName);
        if (count == null) {
            count = new AtomicInteger();
            membersByPackage.put(packageName, count);
        }
        count.incrementAndGet();
    }
    Formatter formatter = new Formatter();
    try {
        String memberType = this instanceof MethodIdsSection ? "method" : "field";
        formatter.format("Too many %s references: %d; max is %d.%n%n" + "References by package:", memberType, items().size(), DexFormat.MAX_MEMBER_IDX + 1);
        for (Map.Entry<String, AtomicInteger> entry : membersByPackage.entrySet()) {
            formatter.format("%n%6d %s", entry.getValue().get(), entry.getKey());
        }
        return formatter.toString();
    } finally {
        formatter.close();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Formatter(java.util.Formatter) TreeMap(java.util.TreeMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Aggregations

Formatter (java.util.Formatter)313 File (java.io.File)18 AlertDialog (android.app.AlertDialog)14 DialogInterface (android.content.DialogInterface)14 Justif (aQute.lib.justif.Justif)12 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)10 Map (java.util.Map)10 Test (org.junit.Test)10 BigInteger (java.math.BigInteger)9 Locale (java.util.Locale)9 IOException (java.io.IOException)8 UnknownFormatConversionException (java.util.UnknownFormatConversionException)8 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7 BigDecimal (java.math.BigDecimal)7 IllegalFormatException (java.util.IllegalFormatException)7 IllegalFormatFlagsException (java.util.IllegalFormatFlagsException)7 Resources (android.content.res.Resources)6 LayoutBuilder (android.text.StaticLayoutTest.LayoutBuilder)6 FormatFlagsConversionMismatchException (java.util.FormatFlagsConversionMismatchException)6