1b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos/* 2b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos * Copyright (C) 2014 The Android Open Source Project 3b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos * 4b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos * Licensed under the Apache License, Version 2.0 (the "License"); 5b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos * you may not use this file except in compliance with the License. 6b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos * You may obtain a copy of the License at 7b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos * 8b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos * http://www.apache.org/licenses/LICENSE-2.0 9b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos * 10b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos * Unless required by applicable law or agreed to in writing, software 11b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos * distributed under the License is distributed on an "AS IS" BASIS, 12b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos * See the License for the specific language governing permissions and 14b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos * limitations under the License. 15b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos */ 16b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulospackage com.android.contacts.interactions; 17b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos 18b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport com.google.common.base.Preconditions; 19b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos 20b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport android.content.Context; 21b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport android.text.format.DateUtils; 22b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos 23b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport com.android.contacts.common.testing.NeededForTesting; 24b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos 25b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport java.text.DateFormat; 26b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos 27b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport java.util.Calendar; 28b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos 29b4574d6f585f665cbb5cef31628e7d23d43b3f08Jeff Davidsonimport com.android.contacts.R; 30b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos 31b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos 32b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos/** 33b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos * Utility methods for interactions and their loaders 34b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos */ 35b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulospublic class ContactInteractionUtil { 36b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos 37b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos /** 38b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos * @return a string like (?,?,?...) with {@param count} question marks. 39b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos */ 40b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos @NeededForTesting 41b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos public static String questionMarks(int count) { 42b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos Preconditions.checkArgument(count > 0); 43b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos StringBuilder sb = new StringBuilder("(?"); 44b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos for (int i = 1; i < count; i++) { 45b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos sb.append(",?"); 46b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos } 47b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos return sb.append(")").toString(); 48b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos } 49b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos 50b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos /** 51b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos * Same as {@link formatDateStringFromTimestamp(long, Context, Calendar)} but uses the current 52b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos * time. 53b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos */ 54b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos @NeededForTesting 55b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos public static String formatDateStringFromTimestamp(long timestamp, Context context) { 56b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos return formatDateStringFromTimestamp(timestamp, context, Calendar.getInstance()); 57b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos } 58b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos 59b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos /** 60b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos * Takes in a timestamp and outputs a human legible date. This checks the timestamp against 61b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos * compareCalendar. 62b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos * This formats the date based on a few conditions: 63b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos * 1. If the timestamp is today, the time is shown 64b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos * 2. If the timestamp occurs tomorrow or yesterday, that is displayed 65b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos * 3. Otherwise {Month Date} format is used 66b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos */ 67b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos @NeededForTesting 68b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos public static String formatDateStringFromTimestamp(long timestamp, Context context, 69b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos Calendar compareCalendar) { 70b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos Calendar interactionCalendar = Calendar.getInstance(); 71b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos interactionCalendar.setTimeInMillis(timestamp); 72b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos 73b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos // compareCalendar is initialized to today 74b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos if (compareCalendarDayYear(interactionCalendar, compareCalendar)) { 75b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos return DateFormat.getTimeInstance(DateFormat.SHORT).format( 76b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos interactionCalendar.getTime()); 77b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos } 78b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos 79b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos // Turn compareCalendar to yesterday 80b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos compareCalendar.add(Calendar.DAY_OF_YEAR, -1); 81b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos if (compareCalendarDayYear(interactionCalendar, compareCalendar)) { 82899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos return context.getString(R.string.yesterday); 83b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos } 84b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos 85b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos // Turn compareCalendar to tomorrow 86b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos compareCalendar.add(Calendar.DAY_OF_YEAR, 2); 87b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos if (compareCalendarDayYear(interactionCalendar, compareCalendar)) { 88899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos return context.getString(R.string.tomorrow); 89b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos } 90b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos return DateUtils.formatDateTime(context, interactionCalendar.getTimeInMillis(), 91b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_YEAR); 92b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos } 93b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos 94b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos /** 95b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos * Compares the day and year of two calendars. 96b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos */ 97b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos private static boolean compareCalendarDayYear(Calendar c1, Calendar c2) { 98b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos return c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR) && 99b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos c1.get(Calendar.DAY_OF_YEAR) == c2.get(Calendar.DAY_OF_YEAR); 100b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos } 101b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos} 102