diff options
author | Treehugger Robot <treehugger-gerrit@google.com> | 2017-08-31 16:17:04 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2017-08-31 16:17:04 +0000 |
commit | c39ea3c55fac807c0b98aabdf56c70dc8a49036c (patch) | |
tree | e282668a9587cf6c1ec7b604dea860400c75c6c7 /java/com/android/dialer/calllogutils/CallLogDurations.java | |
parent | 68038172793ee0e2ab3e2e56ddfbeb82879d1f58 (diff) | |
parent | 2ca4318cc1ee57dda907ba2069bd61d162b1baef (diff) |
Merge "Update Dialer source to latest internal Google revision."
Diffstat (limited to 'java/com/android/dialer/calllogutils/CallLogDurations.java')
-rw-r--r-- | java/com/android/dialer/calllogutils/CallLogDurations.java | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/java/com/android/dialer/calllogutils/CallLogDurations.java b/java/com/android/dialer/calllogutils/CallLogDurations.java new file mode 100644 index 000000000..20998deb4 --- /dev/null +++ b/java/com/android/dialer/calllogutils/CallLogDurations.java @@ -0,0 +1,127 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.dialer.calllogutils; + +import android.content.Context; +import android.content.res.Resources; +import android.text.format.Formatter; +import com.android.dialer.util.DialerUtils; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.concurrent.TimeUnit; + +/** Utility class for formatting duration and data usage in call log entries. */ +public class CallLogDurations { + + private static CharSequence formatDuration(Context context, long elapsedSeconds) { + Resources res = context.getResources(); + String formatPattern; + if (elapsedSeconds >= 60) { + String minutesString = res.getString(R.string.call_details_minutes_abbreviation); + String secondsString = res.getString(R.string.call_details_seconds_abbreviation); + // example output: "1m 1s" + formatPattern = + context.getString( + R.string.call_duration_format_pattern, "m", minutesString, "s", secondsString); + } else { + String secondsString = res.getString(R.string.call_details_seconds_abbreviation); + // example output: "1s" + formatPattern = + context.getString(R.string.call_duration_short_format_pattern, "s", secondsString); + + // Temporary work around for a broken Hebrew(iw) translation. + if (formatPattern.endsWith("\'\'")) { + formatPattern = formatPattern.substring(0, formatPattern.length() - 1); + } + } + + // If new translation issues arise, we should catch them here to prevent crashes. + try { + Date date = new Date(TimeUnit.SECONDS.toMillis(elapsedSeconds)); + SimpleDateFormat format = new SimpleDateFormat(formatPattern); + String duration = format.format(date); + + // SimpleDateFormat cannot display more than 59 minutes, instead it displays MINUTES % 60. + // Here we check for that value and replace it with the correct value. + if (elapsedSeconds >= TimeUnit.MINUTES.toSeconds(60)) { + int minutes = (int) (elapsedSeconds / 60); + duration = duration.replaceFirst(Integer.toString(minutes % 60), Integer.toString(minutes)); + } + return duration; + } catch (Exception e) { + return ""; + } + } + + private static CharSequence formatDurationA11y(Context context, long elapsedSeconds) { + Resources res = context.getResources(); + if (elapsedSeconds >= 60) { + int minutes = (int) (elapsedSeconds / 60); + int seconds = (int) elapsedSeconds - minutes * 60; + String minutesString = res.getQuantityString(R.plurals.a11y_minutes, minutes); + String secondsString = res.getQuantityString(R.plurals.a11y_seconds, seconds); + // example output: "1 minute 1 second", "2 minutes 2 seconds", ect. + return context.getString( + R.string.a11y_call_duration_format, minutes, minutesString, seconds, secondsString); + } else { + String secondsString = res.getQuantityString(R.plurals.a11y_seconds, (int) elapsedSeconds); + // example output: "1 second", "2 seconds" + return context.getString( + R.string.a11y_call_duration_short_format, elapsedSeconds, secondsString); + } + } + + /** + * Formats a string containing the call duration and the data usage (if specified). + * + * @param elapsedSeconds Total elapsed seconds. + * @param dataUsage Data usage in bytes, or null if not specified. + * @return String containing call duration and data usage. + */ + public static CharSequence formatDurationAndDataUsage( + Context context, long elapsedSeconds, long dataUsage) { + return formatDurationAndDataUsageInternal( + context, formatDuration(context, elapsedSeconds), dataUsage); + } + + /** + * Formats a string containing the call duration and the data usage (if specified) for TalkBack. + * + * @param elapsedSeconds Total elapsed seconds. + * @param dataUsage Data usage in bytes, or null if not specified. + * @return String containing call duration and data usage. + */ + public static CharSequence formatDurationAndDataUsageA11y( + Context context, long elapsedSeconds, long dataUsage) { + return formatDurationAndDataUsageInternal( + context, formatDurationA11y(context, elapsedSeconds), dataUsage); + } + + private static CharSequence formatDurationAndDataUsageInternal( + Context context, CharSequence duration, long dataUsage) { + List<CharSequence> durationItems = new ArrayList<>(); + if (dataUsage > 0) { + durationItems.add(duration); + durationItems.add(Formatter.formatShortFileSize(context, dataUsage)); + return DialerUtils.join(durationItems); + } else { + return duration; + } + } +} |