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