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 com.google.common.base.Preconditions;
     19 
     20 import android.content.Context;
     21 import android.text.format.DateUtils;
     22 
     23 import com.android.contacts.common.testing.NeededForTesting;
     24 
     25 import java.text.DateFormat;
     26 
     27 import java.util.Calendar;
     28 
     29 import com.android.contacts.R;
     30 
     31 
     32 /**
     33  * Utility methods for interactions and their loaders
     34  */
     35 public class ContactInteractionUtil {
     36 
     37     /**
     38      * @return a string like (?,?,?...) with {@param count} question marks.
     39      */
     40     @NeededForTesting
     41     public static String questionMarks(int count) {
     42         Preconditions.checkArgument(count > 0);
     43         StringBuilder sb = new StringBuilder("(?");
     44         for (int i = 1; i < count; i++) {
     45             sb.append(",?");
     46         }
     47         return sb.append(")").toString();
     48     }
     49 
     50     /**
     51      * Same as {@link formatDateStringFromTimestamp(long, Context, Calendar)} but uses the current
     52      * time.
     53      */
     54     @NeededForTesting
     55     public static String formatDateStringFromTimestamp(long timestamp, Context context) {
     56         return formatDateStringFromTimestamp(timestamp, context, Calendar.getInstance());
     57     }
     58 
     59     /**
     60      * Takes in a timestamp and outputs a human legible date. This checks the timestamp against
     61      * compareCalendar.
     62      * This formats the date based on a few conditions:
     63      * 1. If the timestamp is today, the time is shown
     64      * 2. If the timestamp occurs tomorrow or yesterday, that is displayed
     65      * 3. Otherwise {Month Date} format is used
     66      */
     67     @NeededForTesting
     68     public static String formatDateStringFromTimestamp(long timestamp, Context context,
     69             Calendar compareCalendar) {
     70         Calendar interactionCalendar = Calendar.getInstance();
     71         interactionCalendar.setTimeInMillis(timestamp);
     72 
     73         // compareCalendar is initialized to today
     74         if (compareCalendarDayYear(interactionCalendar, compareCalendar)) {
     75             return DateFormat.getTimeInstance(DateFormat.SHORT).format(
     76                     interactionCalendar.getTime());
     77         }
     78 
     79         // Turn compareCalendar to yesterday
     80         compareCalendar.add(Calendar.DAY_OF_YEAR, -1);
     81         if (compareCalendarDayYear(interactionCalendar, compareCalendar)) {
     82             return context.getString(R.string.yesterday);
     83         }
     84 
     85         // Turn compareCalendar to tomorrow
     86         compareCalendar.add(Calendar.DAY_OF_YEAR, 2);
     87         if (compareCalendarDayYear(interactionCalendar, compareCalendar)) {
     88             return context.getString(R.string.tomorrow);
     89         }
     90         return DateUtils.formatDateTime(context, interactionCalendar.getTimeInMillis(),
     91                 DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_YEAR);
     92     }
     93 
     94     /**
     95      * Compares the day and year of two calendars.
     96      */
     97     private static boolean compareCalendarDayYear(Calendar c1, Calendar c2) {
     98         return c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR) &&
     99                 c1.get(Calendar.DAY_OF_YEAR) == c2.get(Calendar.DAY_OF_YEAR);
    100     }
    101 }
    102