Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 1 | /* |
Alexandre Lision | c1024c0 | 2014-01-06 11:12:53 -0500 | [diff] [blame] | 2 | * Copyright (C) 2004-2014 Savoir-Faire Linux Inc. |
Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 3 | * |
| 4 | * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | * |
| 20 | * Additional permission under GNU GPL version 3 section 7: |
| 21 | * |
| 22 | * If you modify this program, or any covered work, by linking or |
| 23 | * combining it with the OpenSSL project's OpenSSL library (or a |
| 24 | * modified version of that library), containing parts covered by the |
| 25 | * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc. |
| 26 | * grants you additional permission to convey the resulting work. |
| 27 | * Corresponding Source for a non-source form of such a combination |
| 28 | * shall include the source code for the parts of OpenSSL used as well |
| 29 | * as that of the covered work. |
| 30 | */ |
| 31 | |
Alexandre Lision | 72e3732 | 2013-11-04 17:14:11 -0500 | [diff] [blame] | 32 | package org.sflphone.utils; |
| 33 | |
| 34 | import java.text.SimpleDateFormat; |
| 35 | import java.util.ArrayList; |
| 36 | import java.util.Calendar; |
| 37 | import java.util.Locale; |
| 38 | |
| 39 | public class HistoryManager { |
| 40 | |
| 41 | static ArrayList<String> timeCategories; |
| 42 | |
| 43 | public interface HistoryTimeCategoryModel { |
| 44 | String TODAY = "Today"; // 0 |
| 45 | String YESTERDAY = "Yesterday"; // 1 |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 46 | String TWO_DAYS = HistoryManager.getDate(2, "MM/dd");// 2 |
| 47 | String THREE_DAYS = HistoryManager.getDate(3, "MM/dd");// 3 |
| 48 | String FOUR_DAYS = HistoryManager.getDate(4, "MM/dd");// 4 |
| 49 | String FIVE_DAYS = HistoryManager.getDate(5, "MM/dd");// 5 |
| 50 | String SIX_DAYS = HistoryManager.getDate(6, "MM/dd");// 6 |
Alexandre Lision | 72e3732 | 2013-11-04 17:14:11 -0500 | [diff] [blame] | 51 | String LAST_WEEK = "Last week"; // 7 |
| 52 | String TWO_WEEKS = "Two weeks ago"; // 8 |
| 53 | String THREE_WEEKS = "Three weeks ago"; // 9 |
| 54 | String LAST_MONTH = "Last month"; // 10 |
| 55 | String TWO_MONTH = "Two months ago"; // 11 |
| 56 | String THREE_MONTH = "Three months ago"; // 12 |
| 57 | String FOUR_MONTH = "Four months ago"; // 13 |
| 58 | String FIVE_MONTH = "Five months ago"; // 14 |
| 59 | String SIX_MONTH = "Six months ago"; // 15 |
| 60 | String SEVEN_MONTH = "Seven months ago"; // 16 |
| 61 | String EIGHT_MONTH = "Eight months ago"; // 17 |
| 62 | String NINE_MONTH = "Nine months ago"; // 18 |
| 63 | String TEN_MONTH = "Ten months ago"; // 19 |
| 64 | String ELEVEN_MONTH = "Eleven months ago"; // 20 |
| 65 | String TWELVE_MONTH = "Twelve months ago"; // 21 |
| 66 | String LAST_YEAR = "Last year"; // 22 |
| 67 | String LONG_TIME_AGO = "Very long time ago"; // 23 |
| 68 | String NEVER = "Never"; // 24 |
| 69 | } |
| 70 | |
| 71 | private static final String TAG = HistoryManager.class.getSimpleName(); |
| 72 | |
| 73 | static Calendar removeDays(int ago) { |
| 74 | Calendar cal = Calendar.getInstance(Locale.getDefault()); |
| 75 | int currentDay = cal.get(Calendar.DAY_OF_MONTH); |
| 76 | // Set the date to 2 days ago |
| 77 | cal.set(Calendar.DAY_OF_MONTH, currentDay - ago); |
| 78 | return cal; |
| 79 | } |
| 80 | |
| 81 | static String getDate(int ago, String format) { |
| 82 | Calendar cal = removeDays(ago); |
| 83 | SimpleDateFormat objFormatter = new SimpleDateFormat(format, Locale.CANADA); |
| 84 | objFormatter.setTimeZone(cal.getTimeZone()); |
| 85 | |
| 86 | String result = objFormatter.format(cal.getTime()); |
| 87 | cal.clear(); |
| 88 | return result; |
| 89 | } |
| 90 | |
| 91 | public static String timeToHistoryConst(long time) { |
| 92 | |
| 93 | if(timeCategories == null){ |
| 94 | initializeCategories(); |
| 95 | } |
| 96 | |
| 97 | long time2 = time; |
| 98 | long currentTime = Calendar.getInstance(Locale.getDefault()).getTime().getTime() / 1000; // in seconds |
| 99 | |
| 100 | if (time < 0) |
| 101 | return HistoryTimeCategoryModel.NEVER; |
| 102 | |
| 103 | // Check if part if the current Nychthemeron |
| 104 | if (currentTime - time <= 3600 * 24) // The future case would be a bug, but it have to be handled anyway or it will appear in |
| 105 | // "very long time ago" |
| 106 | return HistoryTimeCategoryModel.TODAY; |
| 107 | |
| 108 | time2 -= time % (3600 * 24); // Reset to midnight |
| 109 | currentTime -= currentTime % (3600 * 24); // Reset to midnight |
| 110 | // Check for last week |
| 111 | if (currentTime - (6) * 3600 * 24 < time2) { |
| 112 | for (int i = 1; i < 7; i++) { |
| 113 | if (currentTime - ((i) * 3600 * 24) == time2) |
| 114 | return timeCategories.get(i); // Yesterday to Six_days_ago |
| 115 | } |
| 116 | } |
| 117 | // Check for last month |
| 118 | else if (currentTime - ((4) * 7 * 24 * 3600) < time2) { |
| 119 | for (int i = 1; i < 4; i++) { |
| 120 | if (currentTime - ((i + 1) * 7 * 24 * 3600) < time2) |
| 121 | return timeCategories.get(i + timeCategories.indexOf(HistoryTimeCategoryModel.LAST_WEEK) - 1); // Last_week to Three_weeks_ago |
| 122 | } |
| 123 | } |
| 124 | // Check for last year |
| 125 | else if (currentTime - (12) * 30.4f * 24 * 3600 < time2) { |
| 126 | for (int i = 1; i < 12; i++) { |
| 127 | if (currentTime - (i + 1) * 30.4f * 24 * 3600 < time2) // Not exact, but faster |
| 128 | return timeCategories.get(i + timeCategories.indexOf(HistoryTimeCategoryModel.LAST_MONTH) - 1); |
| 129 | ; // Last_month to Twelve_months ago |
| 130 | } |
| 131 | } |
| 132 | // if (QDate::currentDate().addYears(-1) >= date && QDate::currentDate().addYears(-2) < date) |
| 133 | else if (currentTime - 365 * 24 * 3600 < time2) |
| 134 | return HistoryTimeCategoryModel.LAST_YEAR; |
| 135 | |
| 136 | // Every other senario |
| 137 | return HistoryTimeCategoryModel.LONG_TIME_AGO; |
| 138 | } |
| 139 | |
| 140 | private static void initializeCategories() { |
| 141 | timeCategories = new ArrayList<String>(); |
| 142 | timeCategories.add(HistoryTimeCategoryModel.TODAY); |
| 143 | timeCategories.add(HistoryTimeCategoryModel.YESTERDAY); |
| 144 | timeCategories.add(HistoryTimeCategoryModel.TWO_DAYS); |
| 145 | timeCategories.add(HistoryTimeCategoryModel.THREE_DAYS); |
| 146 | timeCategories.add(HistoryTimeCategoryModel.FOUR_DAYS); |
| 147 | timeCategories.add(HistoryTimeCategoryModel.FIVE_DAYS); |
| 148 | timeCategories.add(HistoryTimeCategoryModel.SIX_DAYS); |
| 149 | timeCategories.add(HistoryTimeCategoryModel.LAST_WEEK); |
| 150 | timeCategories.add(HistoryTimeCategoryModel.TWO_WEEKS); |
| 151 | timeCategories.add(HistoryTimeCategoryModel.THREE_WEEKS); |
| 152 | timeCategories.add(HistoryTimeCategoryModel.LAST_MONTH); |
| 153 | timeCategories.add(HistoryTimeCategoryModel.TWO_MONTH); |
| 154 | timeCategories.add(HistoryTimeCategoryModel.THREE_MONTH); |
| 155 | timeCategories.add(HistoryTimeCategoryModel.FOUR_MONTH); |
| 156 | timeCategories.add(HistoryTimeCategoryModel.FIVE_MONTH); |
| 157 | timeCategories.add(HistoryTimeCategoryModel.SIX_MONTH); |
| 158 | timeCategories.add(HistoryTimeCategoryModel.SEVEN_MONTH); |
| 159 | timeCategories.add(HistoryTimeCategoryModel.EIGHT_MONTH); |
| 160 | timeCategories.add(HistoryTimeCategoryModel.NINE_MONTH); |
| 161 | timeCategories.add(HistoryTimeCategoryModel.TEN_MONTH); |
| 162 | timeCategories.add(HistoryTimeCategoryModel.ELEVEN_MONTH); |
| 163 | timeCategories.add(HistoryTimeCategoryModel.TWELVE_MONTH); |
| 164 | timeCategories.add(HistoryTimeCategoryModel.LAST_YEAR); |
| 165 | timeCategories.add(HistoryTimeCategoryModel.LONG_TIME_AGO); |
| 166 | timeCategories.add(HistoryTimeCategoryModel.NEVER); |
| 167 | } |
| 168 | |
| 169 | } |