From f2a54f8505d27a2ac43ea520d13daba3e764d9af Mon Sep 17 00:00:00 2001 From: daniel Date: Tue, 5 Jun 2012 22:25:07 +0200 Subject: [PATCH] Added functions to convert milliseconds into a more readable String --- .../activity/MediaplayerActivity.java | 6 ++--- src/de/podfetcher/util/Converter.java | 25 +++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/de/podfetcher/activity/MediaplayerActivity.java b/src/de/podfetcher/activity/MediaplayerActivity.java index 162615d97..55cc2b1e7 100644 --- a/src/de/podfetcher/activity/MediaplayerActivity.java +++ b/src/de/podfetcher/activity/MediaplayerActivity.java @@ -117,7 +117,7 @@ public class MediaplayerActivity extends SherlockActivity { protected void onProgressUpdate(Long... values) { super.onProgressUpdate(values); txtvPosition.setText( - Integer.toString(playbackService.getPlayer().getCurrentPosition())); + Converter.getDurationStringLong(playbackService.getPlayer().getCurrentPosition())); } }; @@ -136,8 +136,8 @@ public class MediaplayerActivity extends SherlockActivity { imgvCover.setImageBitmap( media.getItem().getFeed().getImage().getImageBitmap()); - txtvPosition.setText(Integer.toString(player.getCurrentPosition())); - txtvLength.setText(Integer.toString(player.getDuration())); + txtvPosition.setText(Converter.getDurationStringLong((player.getCurrentPosition()))); + txtvLength.setText(Converter.getDurationStringLong(player.getDuration())); } } diff --git a/src/de/podfetcher/util/Converter.java b/src/de/podfetcher/util/Converter.java index eb91be286..ab34c566c 100644 --- a/src/de/podfetcher/util/Converter.java +++ b/src/de/podfetcher/util/Converter.java @@ -22,6 +22,11 @@ public final class Converter { private static final int GB_RANGE = 3; /** Determines the length of the number for best readability.*/ private static final int NUM_LENGTH = 1000; + + + private static final int HOURS_MIL = 3600000; + private static final int MINUTES_MIL = 60000; + private static final int SECONDS_MIL = 1000; /** Takes a byte-value and converts it into a more readable * String. @@ -53,4 +58,24 @@ public final class Converter { return "ERROR"; } } + + /** Converts milliseconds to a string containing hours, minutes and seconds */ + public static String getDurationStringLong(int duration) { + int h = duration / HOURS_MIL; + int rest = duration - h * HOURS_MIL; + int m = rest / MINUTES_MIL; + rest -= m * MINUTES_MIL; + int s = rest / SECONDS_MIL; + + return String.format("%02d:%02d:%02d", h, m, s); + } + + /** Converts milliseconds to a string containing hours and minutes */ + public static String getDurationStringShort(int duration) { + int h = duration / HOURS_MIL; + int rest = duration - h * HOURS_MIL; + int m = rest / MINUTES_MIL; + + return String.format("%02d:%02d", h, m); + } }