|
10 | 10 | import android.icu.text.CompactDecimalFormat; |
11 | 11 | import android.os.Build; |
12 | 12 | import android.text.TextUtils; |
| 13 | +import android.text.format.DateUtils; |
13 | 14 | import android.util.DisplayMetrics; |
14 | 15 |
|
15 | 16 | import androidx.annotation.NonNull; |
@@ -239,43 +240,27 @@ public static String likeCount(@NonNull final Context context, final int likeCou |
239 | 240 | } |
240 | 241 |
|
241 | 242 | /** |
242 | | - * Get a readable text for a duration in the format {@code days:hours:minutes:seconds}. |
243 | | - * Prepended zeros are removed. |
| 243 | + * Get a readable text for a duration in the format {@code hours:minutes:seconds}. |
| 244 | + * |
244 | 245 | * @param duration the duration in seconds |
245 | | - * @return a formatted duration String or {@code 0:00} if the duration is zero. |
| 246 | + * @return a formatted duration String or {@code 00:00} if the duration is zero. |
246 | 247 | */ |
247 | 248 | public static String getDurationString(final long duration) { |
248 | | - return getDurationString(duration, true, false); |
| 249 | + return DateUtils.formatElapsedTime(Math.max(duration, 0)); |
249 | 250 | } |
250 | 251 |
|
251 | 252 | /** |
252 | | - * Get a readable text for a duration in the format {@code days:hours:minutes:seconds+}. |
253 | | - * Prepended zeros are removed. If the given duration is incomplete, a plus is appended to the |
254 | | - * duration string. |
| 253 | + * Get a readable text for a duration in the format {@code hours:minutes:seconds+}. If the given |
| 254 | + * duration is incomplete, a plus is appended to the duration string. |
| 255 | + * |
255 | 256 | * @param duration the duration in seconds |
256 | 257 | * @param isDurationComplete whether the given duration is complete or whether info is missing |
257 | 258 | * @param showDurationPrefix whether the duration-prefix shall be shown |
258 | | - * @return a formatted duration String or {@code 0:00} if the duration is zero. |
| 259 | + * @return a formatted duration String or {@code 00:00} if the duration is zero. |
259 | 260 | */ |
260 | 261 | public static String getDurationString(final long duration, final boolean isDurationComplete, |
261 | 262 | final boolean showDurationPrefix) { |
262 | | - final String output; |
263 | | - |
264 | | - final long days = duration / (24 * 60 * 60L); /* greater than a day */ |
265 | | - final long hours = duration % (24 * 60 * 60L) / (60 * 60L); /* greater than an hour */ |
266 | | - final long minutes = duration % (24 * 60 * 60L) % (60 * 60L) / 60L; |
267 | | - final long seconds = duration % 60L; |
268 | | - |
269 | | - if (duration < 0) { |
270 | | - output = "0:00"; |
271 | | - } else if (days > 0) { |
272 | | - //handle days |
273 | | - output = String.format(Locale.US, "%d:%02d:%02d:%02d", days, hours, minutes, seconds); |
274 | | - } else if (hours > 0) { |
275 | | - output = String.format(Locale.US, "%d:%02d:%02d", hours, minutes, seconds); |
276 | | - } else { |
277 | | - output = String.format(Locale.US, "%d:%02d", minutes, seconds); |
278 | | - } |
| 263 | + final String output = getDurationString(duration); |
279 | 264 | final String durationPrefix = showDurationPrefix ? "⏱ " : ""; |
280 | 265 | final String durationPostfix = isDurationComplete ? "" : "+"; |
281 | 266 | return durationPrefix + output + durationPostfix; |
|
0 commit comments