Home | History | Annotate | Download | only in interactions
      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 
     18 import android.content.Context;
     19 import android.text.format.DateUtils;
     20 
     21 import com.android.contacts.R;
     22 
     23 import com.google.common.base.Preconditions;
     24 
     25 import java.text.DateFormat;
     26 import java.util.Calendar;
     27 
     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      */
     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      */
     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
     58      * 2. Otherwise show full date and time
     59      */
     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 
     71         return DateUtils.formatDateTime(context, timestamp, DateUtils.FORMAT_SHOW_TIME
     72                 | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY
     73                 | DateUtils.FORMAT_SHOW_YEAR);
     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     }
     83 
     84     /**
     85      * Takes duration of the call in seconds.
     86      * Return the formatted duration in hr, min, sec order if they exist.
     87      */
     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     }
    101 }
    102