[Android] Make seconds optionnal in millisToStrings helper

Geoffrey Métais git at videolan.org
Thu Jun 8 14:12:59 CEST 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Jun  8 14:11:58 2017 +0200| [53fb4a12479a2c02c1ad0cb30645543735865db8] | committer: Geoffrey Métais

Make seconds optionnal in millisToStrings helper

> https://code.videolan.org/videolan/vlc-android/commit/53fb4a12479a2c02c1ad0cb30645543735865db8
---

 medialibrary/src/org/videolan/medialibrary/Tools.java     | 12 ++++++------
 .../test/java/org/videolan/medialibrary/ToolsTest.java    | 15 +++++++++------
 vlc-android/src/org/videolan/vlc/gui/InfoActivity.java    |  2 +-
 3 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/medialibrary/src/org/videolan/medialibrary/Tools.java b/medialibrary/src/org/videolan/medialibrary/Tools.java
index cd8c2c76d..6077f8541 100644
--- a/medialibrary/src/org/videolan/medialibrary/Tools.java
+++ b/medialibrary/src/org/videolan/medialibrary/Tools.java
@@ -41,8 +41,8 @@ public class Tools {
         if (lastTime == 0L)
             return "";
         return String.format("%s / %s",
-                millisToString(lastTime, true),
-                millisToString(media.getLength(), true));
+                millisToString(lastTime, true, false),
+                millisToString(media.getLength(), true, false));
     }
 
     /**
@@ -51,7 +51,7 @@ public class Tools {
      * @return formated string (hh:)mm:ss
      */
     public static String millisToString(long millis) {
-        return millisToString(millis, false);
+        return millisToString(millis, false, true);
     }
 
     /**
@@ -60,7 +60,7 @@ public class Tools {
      * @return formated string "[hh]h[mm]min" / "[mm]min[s]s"
      */
     public static String millisToText(long millis) {
-        return millisToString(millis, true);
+        return millisToString(millis, true, true);
     }
 
     public static String getResolution(MediaWrapper media) {
@@ -95,7 +95,7 @@ public class Tools {
         }
     }
 
-    public static String millisToString(long millis, boolean text) {
+    public static String millisToString(long millis, boolean text, boolean seconds) {
         sb.setLength(0);
         if (millis < 0) {
             millis = -millis;
@@ -114,7 +114,7 @@ public class Tools {
                 sb.append(hours).append('h');
             if (min > 0)
                 sb.append(min).append("min");
-            if (sec > 0)
+            if ((seconds || sb.length() == 0) && sec > 0)
                 sb.append(sec).append("s");
         } else {
             if (hours > 0)
diff --git a/medialibrary/src/test/java/org/videolan/medialibrary/ToolsTest.java b/medialibrary/src/test/java/org/videolan/medialibrary/ToolsTest.java
index 2b0a8c5a0..7cb469921 100644
--- a/medialibrary/src/test/java/org/videolan/medialibrary/ToolsTest.java
+++ b/medialibrary/src/test/java/org/videolan/medialibrary/ToolsTest.java
@@ -8,12 +8,15 @@ public class ToolsTest extends TestCase {
     }
 
     public void testMillisToString() throws Exception {
-        assertEquals("3min30s", Tools.millisToString(210000, true));
-        assertEquals("3min", Tools.millisToString(180000, true));
-        assertEquals("1h30min30s", Tools.millisToString(5430000, true));
-        assertEquals("1h30min", Tools.millisToString(5400000, true));
-        assertEquals("1h30s", Tools.millisToString(3630000, true));
-        assertEquals("-32:40", Tools.millisToString(-1960000, false));
+        assertEquals("3min30s", Tools.millisToString(210000, true, true));
+        assertEquals("3min", Tools.millisToString(180000, true, true));
+        assertEquals("1h30min30s", Tools.millisToString(5430000, true, true));
+        assertEquals("1h30min", Tools.millisToString(5430000, true, false));
+        assertEquals("1h30min", Tools.millisToString(5400000, true, true));
+        assertEquals("17s", Tools.millisToString(17000, true, true));
+        assertEquals("17s", Tools.millisToString(17000, true, false));
+        assertEquals("1h30s", Tools.millisToString(3630000, true, true));
+        assertEquals("-32:40", Tools.millisToString(-1960000, false, true));
     }
 
 }
\ No newline at end of file
diff --git a/vlc-android/src/org/videolan/vlc/gui/InfoActivity.java b/vlc-android/src/org/videolan/vlc/gui/InfoActivity.java
index 7f6151fd0..3842e7609 100644
--- a/vlc-android/src/org/videolan/vlc/gui/InfoActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/InfoActivity.java
@@ -119,7 +119,7 @@ public class InfoActivity extends AudioPlayerContainerActivity implements View.O
             for (MediaWrapper media : tracks)
                 length += media.getLength();
         if (length > 0)
-            mBinding.setLength(Tools.millisToString(length, true));
+            mBinding.setLength(Tools.millisToText(length));
 
         if (mItem.getItemType() == MediaLibraryItem.TYPE_MEDIA) {
             MediaWrapper media = (MediaWrapper) mItem;



More information about the Android mailing list