From 53f6e8a9b95a1fe5229295ade4989339d577de08 Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Tue, 13 Nov 2012 16:23:59 -0800 Subject: [PATCH] Utility to format human-friendly durations. Bug: 6777872 Change-Id: Ie7ebcb456306f4f88b4707030dd7beda3fc90368 --- core/java/android/text/format/DateUtils.java | 24 ++++++++++ core/res/res/values/strings.xml | 15 ++++++ core/res/res/values/symbols.xml | 3 ++ .../android/text/format/DateUtilsTest.java | 48 +++++++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 core/tests/coretests/src/android/text/format/DateUtilsTest.java diff --git a/core/java/android/text/format/DateUtils.java b/core/java/android/text/format/DateUtils.java index 1060bd84d1f6c..bcce61dc00aab 100644 --- a/core/java/android/text/format/DateUtils.java +++ b/core/java/android/text/format/DateUtils.java @@ -606,6 +606,30 @@ public class DateUtils } } + /** + * Return given duration in a human-friendly format. For example, "4 + * minutes" or "1 second". Returns only largest meaningful unit of time, + * from seconds up to hours. + * + * @hide + */ + public static CharSequence formatDuration(long millis) { + final Resources res = Resources.getSystem(); + if (millis >= HOUR_IN_MILLIS) { + final int hours = (int) ((millis + 1800000) / HOUR_IN_MILLIS); + return res.getQuantityString( + com.android.internal.R.plurals.duration_hours, hours, hours); + } else if (millis >= MINUTE_IN_MILLIS) { + final int minutes = (int) ((millis + 30000) / MINUTE_IN_MILLIS); + return res.getQuantityString( + com.android.internal.R.plurals.duration_minutes, minutes, minutes); + } else { + final int seconds = (int) ((millis + 500) / SECOND_IN_MILLIS); + return res.getQuantityString( + com.android.internal.R.plurals.duration_seconds, seconds, seconds); + } + } + /** * Formats an elapsed time in the form "MM:SS" or "H:MM:SS" * for display on the call-in-progress screen. diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml index 9932d1e169530..80c2a1318a6ab 100755 --- a/core/res/res/values/strings.xml +++ b/core/res/res/values/strings.xml @@ -2838,6 +2838,21 @@ years + + + 1 second + %d seconds + + + + 1 minute + %d minutes + + + + 1 hour + %d hours + Video problem diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml index 68a0289756816..f298988eccf75 100644 --- a/core/res/res/values/symbols.xml +++ b/core/res/res/values/symbols.xml @@ -870,6 +870,9 @@ + + + diff --git a/core/tests/coretests/src/android/text/format/DateUtilsTest.java b/core/tests/coretests/src/android/text/format/DateUtilsTest.java new file mode 100644 index 0000000000000..cf42bb1c003a0 --- /dev/null +++ b/core/tests/coretests/src/android/text/format/DateUtilsTest.java @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.text.format; + +import android.test.suitebuilder.annotation.SmallTest; + +import junit.framework.TestCase; + +public class DateUtilsTest extends TestCase { + @SmallTest + public void testFormatDurationSeconds() throws Exception { + assertEquals("0 seconds", DateUtils.formatDuration(0)); + assertEquals("0 seconds", DateUtils.formatDuration(1)); + assertEquals("0 seconds", DateUtils.formatDuration(499)); + assertEquals("1 second", DateUtils.formatDuration(500)); + assertEquals("1 second", DateUtils.formatDuration(1000)); + assertEquals("2 seconds", DateUtils.formatDuration(1500)); + } + + @SmallTest + public void testFormatDurationMinutes() throws Exception { + assertEquals("59 seconds", DateUtils.formatDuration(59000)); + assertEquals("60 seconds", DateUtils.formatDuration(59500)); + assertEquals("1 minute", DateUtils.formatDuration(60000)); + assertEquals("2 minutes", DateUtils.formatDuration(120000)); + } + + @SmallTest + public void testFormatDurationHours() throws Exception { + assertEquals("59 minutes", DateUtils.formatDuration(3540000)); + assertEquals("1 hour", DateUtils.formatDuration(3600000)); + assertEquals("48 hours", DateUtils.formatDuration(172800000)); + } +}