blob: e7a7a6db87aa76a1bc98380f8472c3d34b293e9e [file] [log] [blame]
Paul Soulosb3054e52014-06-05 16:46:02 -07001/*
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 */
16package com.android.contacts.interactions;
17
Paul Soulosb3054e52014-06-05 16:46:02 -070018import android.content.Context;
19import android.text.format.DateUtils;
20
yaolu0dd79bb2016-08-09 14:19:33 -070021import com.android.contacts.R;
22
Gary Mai0a49afa2016-12-05 15:53:58 -080023import com.google.common.base.Preconditions;
24
25import java.text.DateFormat;
26import java.util.Calendar;
27
Paul Soulosb3054e52014-06-05 16:46:02 -070028/**
29 * Utility methods for interactions and their loaders
30 */
31public class ContactInteractionUtil {
32
33 /**
34 * @return a string like (?,?,?...) with {@param count} question marks.
35 */
Paul Soulosb3054e52014-06-05 16:46:02 -070036 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 Soulosb3054e52014-06-05 16:46:02 -070049 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
yaolubddb3152016-08-09 11:01:06 -070058 * 2. Otherwise show full date and time
Paul Soulosb3054e52014-06-05 16:46:02 -070059 */
Paul Soulosb3054e52014-06-05 16:46:02 -070060 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
yaolubddb3152016-08-09 11:01:06 -070071 return DateUtils.formatDateTime(context, timestamp, DateUtils.FORMAT_SHOW_TIME
72 | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY
73 | DateUtils.FORMAT_SHOW_YEAR);
Paul Soulosb3054e52014-06-05 16:46:02 -070074 }
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 }
yaolu0dd79bb2016-08-09 14:19:33 -070083
84 /**
85 * Takes duration of the call in seconds.
86 * Return the formatted duration in hr, min, sec order if they exist.
87 */
yaolu0dd79bb2016-08-09 14:19:33 -070088 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 Soulosb3054e52014-06-05 16:46:02 -0700101}