Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | package com.android.contacts.interactions; |
| 17 | |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 18 | import android.content.Context; |
| 19 | import android.text.format.DateUtils; |
| 20 | |
yaolu | 0dd79bb | 2016-08-09 14:19:33 -0700 | [diff] [blame] | 21 | import com.android.contacts.R; |
| 22 | |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 23 | import com.google.common.base.Preconditions; |
| 24 | |
| 25 | import java.text.DateFormat; |
| 26 | import java.util.Calendar; |
| 27 | |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Utility methods for interactions and their loaders |
| 30 | */ |
| 31 | public class ContactInteractionUtil { |
| 32 | |
| 33 | /** |
| 34 | * @return a string like (?,?,?...) with {@param count} question marks. |
| 35 | */ |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 36 | public static String questionMarks(int count) { |
| 37 | Preconditions.checkArgument(count > 0); |
| 38 | StringBuilder sb = new StringBuilder("(?"); |
| 39 | for (int i = 1; i < count; i++) { |
| 40 | sb.append(",?"); |
| 41 | } |
| 42 | return sb.append(")").toString(); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Same as {@link formatDateStringFromTimestamp(long, Context, Calendar)} but uses the current |
| 47 | * time. |
| 48 | */ |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 49 | public static String formatDateStringFromTimestamp(long timestamp, Context context) { |
| 50 | return formatDateStringFromTimestamp(timestamp, context, Calendar.getInstance()); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Takes in a timestamp and outputs a human legible date. This checks the timestamp against |
| 55 | * compareCalendar. |
| 56 | * This formats the date based on a few conditions: |
| 57 | * 1. If the timestamp is today, the time is shown |
yaolu | bddb315 | 2016-08-09 11:01:06 -0700 | [diff] [blame] | 58 | * 2. Otherwise show full date and time |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 59 | */ |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 60 | public static String formatDateStringFromTimestamp(long timestamp, Context context, |
| 61 | Calendar compareCalendar) { |
| 62 | Calendar interactionCalendar = Calendar.getInstance(); |
| 63 | interactionCalendar.setTimeInMillis(timestamp); |
| 64 | |
| 65 | // compareCalendar is initialized to today |
| 66 | if (compareCalendarDayYear(interactionCalendar, compareCalendar)) { |
| 67 | return DateFormat.getTimeInstance(DateFormat.SHORT).format( |
| 68 | interactionCalendar.getTime()); |
| 69 | } |
| 70 | |
yaolu | bddb315 | 2016-08-09 11:01:06 -0700 | [diff] [blame] | 71 | return DateUtils.formatDateTime(context, timestamp, DateUtils.FORMAT_SHOW_TIME |
| 72 | | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY |
| 73 | | DateUtils.FORMAT_SHOW_YEAR); |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Compares the day and year of two calendars. |
| 78 | */ |
| 79 | private static boolean compareCalendarDayYear(Calendar c1, Calendar c2) { |
| 80 | return c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR) && |
| 81 | c1.get(Calendar.DAY_OF_YEAR) == c2.get(Calendar.DAY_OF_YEAR); |
| 82 | } |
yaolu | 0dd79bb | 2016-08-09 14:19:33 -0700 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * Takes duration of the call in seconds. |
| 86 | * Return the formatted duration in hr, min, sec order if they exist. |
| 87 | */ |
yaolu | 0dd79bb | 2016-08-09 14:19:33 -0700 | [diff] [blame] | 88 | public static String formatDuration(long callDuration, Context context) { |
| 89 | final int hours = (int) callDuration / 3600; |
| 90 | final int minutes = (int) (callDuration % 3600) / 60; |
| 91 | final int seconds = (int) (callDuration % 60); |
| 92 | |
| 93 | if (hours > 0) { |
| 94 | return context.getString(R.string.callDurationHourFormat, hours, minutes, seconds); |
| 95 | } else if (minutes > 0) { |
| 96 | return context.getString(R.string.callDurationMinuteFormat, minutes, seconds); |
| 97 | } else { |
| 98 | return context.getString(R.string.callDurationSecondFormat, seconds); |
| 99 | } |
| 100 | } |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 101 | } |