Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c749261f authored by Elliott Hughes's avatar Elliott Hughes Committed by Gerrit Code Review
Browse files

Merge "Fix DateUtils.formatElapsedTime."

parents 01f42639 2eda1848
Loading
Loading
Loading
Loading
+18 −71
Original line number Diff line number Diff line
@@ -43,11 +43,6 @@ public class DateUtils
    private static String sElapsedFormatMMSS;
    private static String sElapsedFormatHMMSS;

    private static final String FAST_FORMAT_HMMSS = "%1$d:%2$02d:%3$02d";
    private static final String FAST_FORMAT_MMSS = "%1$02d:%2$02d";
    private static final char TIME_SEPARATOR = ':';


    public static final long SECOND_IN_MILLIS = 1000;
    public static final long MINUTE_IN_MILLIS = SECOND_IN_MILLIS * 60;
    public static final long HOUR_IN_MILLIS = MINUTE_IN_MILLIS * 60;
@@ -616,19 +611,18 @@ public class DateUtils
    }

    /**
     * Formats an elapsed time in the form "MM:SS" or "H:MM:SS"
     * for display on the call-in-progress screen.
     * 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, if possible
     * @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) {
        initFormatStrings();

        // 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;
@@ -639,70 +633,23 @@ public class DateUtils
        }
        seconds = elapsedSeconds;

        String result;
        if (hours > 0) {
            return formatElapsedTime(recycle, sElapsedFormatHMMSS, hours, minutes, seconds);
        } else {
            return formatElapsedTime(recycle, sElapsedFormatMMSS, minutes, seconds);
        }
    }

    private static void append(StringBuilder sb, long value, boolean pad, char zeroDigit) {
        if (value < 10) {
            if (pad) {
                sb.append(zeroDigit);
            }
        } else {
            sb.append((char) (zeroDigit + (value / 10)));
        }
        sb.append((char) (zeroDigit + (value % 10)));
    }

    /**
     * Fast formatting of h:mm:ss.
     */
    private static String formatElapsedTime(StringBuilder recycle, String format, long hours,
            long minutes, long seconds) {
        if (FAST_FORMAT_HMMSS.equals(format)) {
            char zeroDigit = LocaleData.get(Locale.getDefault()).zeroDigit;

        // 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);
        }
            append(sb, hours, false, zeroDigit);
            sb.append(TIME_SEPARATOR);
            append(sb, minutes, true, zeroDigit);
            sb.append(TIME_SEPARATOR);
            append(sb, seconds, true, zeroDigit);
            return sb.toString();
        } else {
            return String.format(format, hours, minutes, seconds);
        }
    }

    /**
     * Fast formatting of mm:ss.
     */
    private static String formatElapsedTime(StringBuilder recycle, String format, long minutes,
            long seconds) {
        if (FAST_FORMAT_MMSS.equals(format)) {
            char zeroDigit = LocaleData.get(Locale.getDefault()).zeroDigit;

            StringBuilder sb = recycle;
            if (sb == null) {
                sb = new StringBuilder(8);
            } else {
                sb.setLength(0);
            }
            append(sb, minutes, false, zeroDigit);
            sb.append(TIME_SEPARATOR);
            append(sb, seconds, true, zeroDigit);
            return sb.toString();
        // 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 String.format(format, minutes, seconds);
            return f.format(sElapsedFormatMMSS, minutes, seconds).toString();
        }
    }