1 package com.android.incallui; 2 3 import android.content.Context; 4 import android.content.res.Resources; 5 6 /** 7 * Methods to parse time and date information in the InCallUi 8 */ 9 public class InCallDateUtils { 10 11 /** 12 * Return given duration in a human-friendly format. For example, "4 minutes 3 seconds" or 13 * "3 hours 1 second". Returns the hours, minutes and seconds in that order if they exist. 14 */ formatDuration(Context context, long millis)15 public static String formatDuration(Context context, long millis) { 16 int hours = 0; 17 int minutes = 0; 18 int seconds = 0; 19 int elapsedSeconds = (int) (millis / 1000); 20 if (elapsedSeconds >= 3600) { 21 hours = elapsedSeconds / 3600; 22 elapsedSeconds -= hours * 3600; 23 } 24 if (elapsedSeconds >= 60) { 25 minutes = elapsedSeconds / 60; 26 elapsedSeconds -= minutes * 60; 27 } 28 seconds = elapsedSeconds; 29 30 final Resources res = context.getResources(); 31 StringBuilder duration = new StringBuilder(); 32 try { 33 if (hours > 0) { 34 duration.append(res.getQuantityString(R.plurals.duration_hours, hours, hours)); 35 } 36 if (minutes > 0) { 37 if (hours > 0) { 38 duration.append(' '); 39 } 40 duration.append(res.getQuantityString(R.plurals.duration_minutes, minutes, minutes)); 41 } 42 if (seconds > 0) { 43 if (hours > 0 || minutes > 0) { 44 duration.append(' '); 45 } 46 duration.append(res.getQuantityString(R.plurals.duration_seconds, seconds, seconds)); 47 } 48 } catch (Resources.NotFoundException e) { 49 // Ignore; plurals throws an exception for an untranslated quantity for a given locale. 50 return null; 51 } 52 return duration.toString(); 53 } 54 } 55