1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.dialer.calllogutils; 18 19 import android.content.Context; 20 import android.content.res.Resources; 21 import android.text.format.Formatter; 22 import com.android.dialer.util.DialerUtils; 23 import java.util.ArrayList; 24 import java.util.List; 25 import java.util.concurrent.TimeUnit; 26 27 /** Utility class for formatting duration and data usage in call log entries. */ 28 public class CallLogDurations { 29 formatDuration(Context context, long elapsedSeconds)30 private static CharSequence formatDuration(Context context, long elapsedSeconds) { 31 // Getting this method into a good state took a bunch of work between eng, i18n team and 32 // translators. If at all possible, the strings should not be changed or updated. 33 long minutes = TimeUnit.SECONDS.toMinutes(elapsedSeconds); 34 long seconds = elapsedSeconds - TimeUnit.MINUTES.toSeconds(minutes); 35 Resources res = context.getResources(); 36 String formatPattern; 37 if (elapsedSeconds >= 60) { 38 String minutesString = res.getString(R.string.call_details_minutes_abbreviation); 39 String secondsString = res.getString(R.string.call_details_seconds_abbreviation); 40 // example output: "1m 1s" 41 formatPattern = 42 context.getString( 43 R.string.call_duration_format_pattern, 44 Long.toString(minutes), 45 minutesString, 46 Long.toString(seconds), 47 secondsString); 48 } else { 49 String secondsString = res.getString(R.string.call_details_seconds_abbreviation); 50 // example output: "1s" 51 formatPattern = 52 context.getString( 53 R.string.call_duration_short_format_pattern, Long.toString(seconds), secondsString); 54 } 55 56 // Since we don't want to update the strings.xml, we need to remove the quotations from the 57 // previous implementation. 58 return formatPattern.replace("\'", ""); 59 } 60 formatDurationA11y(Context context, long elapsedSeconds)61 private static CharSequence formatDurationA11y(Context context, long elapsedSeconds) { 62 Resources res = context.getResources(); 63 if (elapsedSeconds >= 60) { 64 int minutes = (int) (elapsedSeconds / 60); 65 int seconds = (int) elapsedSeconds - minutes * 60; 66 String minutesString = res.getQuantityString(R.plurals.a11y_minutes, minutes); 67 String secondsString = res.getQuantityString(R.plurals.a11y_seconds, seconds); 68 // example output: "1 minute 1 second", "2 minutes 2 seconds", ect. 69 return context.getString( 70 R.string.a11y_call_duration_format, minutes, minutesString, seconds, secondsString); 71 } else { 72 String secondsString = res.getQuantityString(R.plurals.a11y_seconds, (int) elapsedSeconds); 73 // example output: "1 second", "2 seconds" 74 return context.getString( 75 R.string.a11y_call_duration_short_format, elapsedSeconds, secondsString); 76 } 77 } 78 79 /** 80 * Formats a string containing the call duration and the data usage (if specified). 81 * 82 * @param elapsedSeconds Total elapsed seconds. 83 * @param dataUsage Data usage in bytes, or null if not specified. 84 * @return String containing call duration and data usage. 85 */ formatDurationAndDataUsage( Context context, long elapsedSeconds, long dataUsage)86 public static CharSequence formatDurationAndDataUsage( 87 Context context, long elapsedSeconds, long dataUsage) { 88 return formatDurationAndDataUsageInternal( 89 context, formatDuration(context, elapsedSeconds), dataUsage); 90 } 91 92 /** 93 * Formats a string containing the call duration and the data usage (if specified) for TalkBack. 94 * 95 * @param elapsedSeconds Total elapsed seconds. 96 * @param dataUsage Data usage in bytes, or null if not specified. 97 * @return String containing call duration and data usage. 98 */ formatDurationAndDataUsageA11y( Context context, long elapsedSeconds, long dataUsage)99 public static CharSequence formatDurationAndDataUsageA11y( 100 Context context, long elapsedSeconds, long dataUsage) { 101 return formatDurationAndDataUsageInternal( 102 context, formatDurationA11y(context, elapsedSeconds), dataUsage); 103 } 104 formatDurationAndDataUsageInternal( Context context, CharSequence duration, long dataUsage)105 private static CharSequence formatDurationAndDataUsageInternal( 106 Context context, CharSequence duration, long dataUsage) { 107 List<CharSequence> durationItems = new ArrayList<>(); 108 if (dataUsage > 0) { 109 durationItems.add(duration); 110 durationItems.add(Formatter.formatShortFileSize(context, dataUsage)); 111 return DialerUtils.join(durationItems); 112 } else { 113 return duration; 114 } 115 } 116 } 117