Merge "Utility to format human-friendly durations." into jb-mr1.1-dev
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -2838,6 +2838,21 @@
|
||||
<!-- Appened to express the value is this unit of time. -->
|
||||
<string name="years">years</string>
|
||||
|
||||
<!-- Phrase describing a time duration using seconds [CHAR LIMIT=16] -->
|
||||
<plurals name="duration_seconds">
|
||||
<item quantity="one">1 second</item>
|
||||
<item quantity="other"><xliff:g id="count">%d</xliff:g> seconds</item>
|
||||
</plurals>
|
||||
<!-- Phrase describing a time duration using minutes [CHAR LIMIT=16] -->
|
||||
<plurals name="duration_minutes">
|
||||
<item quantity="one">1 minute</item>
|
||||
<item quantity="other"><xliff:g id="count">%d</xliff:g> minutes</item>
|
||||
</plurals>
|
||||
<!-- Phrase describing a time duration using hours [CHAR LIMIT=16] -->
|
||||
<plurals name="duration_hours">
|
||||
<item quantity="one">1 hour</item>
|
||||
<item quantity="other"><xliff:g id="count">%d</xliff:g> hours</item>
|
||||
</plurals>
|
||||
|
||||
<!-- Title for error alert when a video cannot be played. it can be used by any app. -->
|
||||
<string name="VideoView_error_title">Video problem</string>
|
||||
|
||||
@@ -870,6 +870,9 @@
|
||||
<java-symbol type="plurals" name="abbrev_num_hours_ago" />
|
||||
<java-symbol type="plurals" name="abbrev_num_minutes_ago" />
|
||||
<java-symbol type="plurals" name="abbrev_num_seconds_ago" />
|
||||
<java-symbol type="plurals" name="duration_hours" />
|
||||
<java-symbol type="plurals" name="duration_minutes" />
|
||||
<java-symbol type="plurals" name="duration_seconds" />
|
||||
<java-symbol type="plurals" name="in_num_days" />
|
||||
<java-symbol type="plurals" name="in_num_hours" />
|
||||
<java-symbol type="plurals" name="in_num_minutes" />
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user