Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -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 | 2e52d39 | 2013-11-06 15:14:31 -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 | aad014e | 2014-01-14 17:45:49 -0500 | [diff] [blame^] | 32 | package org.sflphone.history; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 33 | |
Alexandre Lision | 6da3bb9 | 2013-12-10 17:32:46 -0500 | [diff] [blame] | 34 | import java.sql.Timestamp; |
| 35 | import java.text.SimpleDateFormat; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 36 | import java.util.ArrayList; |
Alexandre Lision | 6da3bb9 | 2013-12-10 17:32:46 -0500 | [diff] [blame] | 37 | import java.util.Date; |
Alexandre Lision | 72e3732 | 2013-11-04 17:14:11 -0500 | [diff] [blame] | 38 | import java.util.HashMap; |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 39 | import java.util.Locale; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 40 | import java.util.NavigableMap; |
Alexandre Lision | 6da3bb9 | 2013-12-10 17:32:46 -0500 | [diff] [blame] | 41 | import java.util.TimeZone; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 42 | import java.util.TreeMap; |
| 43 | |
Alexandre Lision | aad014e | 2014-01-14 17:45:49 -0500 | [diff] [blame^] | 44 | import org.sflphone.model.CallContact; |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 45 | import org.sflphone.service.ServiceConstants; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 46 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 47 | import android.os.Parcel; |
| 48 | import android.os.Parcelable; |
| 49 | |
| 50 | public class HistoryEntry implements Parcelable { |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 51 | |
| 52 | private CallContact contact; |
| 53 | private NavigableMap<Long, HistoryCall> calls; |
| 54 | private String accountID; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 55 | int missed_sum; |
| 56 | int outgoing_sum; |
| 57 | int incoming_sum; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 58 | |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 59 | public HistoryEntry(String account, CallContact c) { |
| 60 | contact = c; |
| 61 | calls = new TreeMap<Long, HistoryEntry.HistoryCall>(); |
| 62 | accountID = account; |
| 63 | missed_sum = outgoing_sum = incoming_sum = 0; |
| 64 | } |
Alexandre Lision | 72e3732 | 2013-11-04 17:14:11 -0500 | [diff] [blame] | 65 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 66 | public String getAccountID() { |
| 67 | return accountID; |
| 68 | } |
| 69 | |
| 70 | public void setAccountID(String accountID) { |
| 71 | this.accountID = accountID; |
| 72 | } |
| 73 | |
| 74 | public NavigableMap<Long, HistoryCall> getCalls() { |
| 75 | return calls; |
| 76 | } |
| 77 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 78 | public CallContact getContact() { |
| 79 | return contact; |
| 80 | } |
| 81 | |
| 82 | public void setContact(CallContact contact) { |
| 83 | this.contact = contact; |
| 84 | } |
| 85 | |
Alexandre Lision | 2b4f94e | 2013-12-12 10:15:47 -0500 | [diff] [blame] | 86 | /** |
| 87 | * Each call is associated with a contact. |
| 88 | * When adding a call to an HIstoryEntry, this methods also verifies if we can update |
| 89 | * the contact (if contact is Unknown, replace it) |
| 90 | * @param historyCall The call to put in this HistoryEntry |
| 91 | * @param linkedTo The associated CallContact |
| 92 | */ |
| 93 | public void addHistoryCall(HistoryCall historyCall, CallContact linkedTo) { |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 94 | calls.put(historyCall.call_start, historyCall); |
Alexandre Lision | a458ba2 | 2013-12-11 15:04:11 -0500 | [diff] [blame] | 95 | if (historyCall.isIncoming()) { |
Alexandre Lision | cb2345c | 2013-12-09 15:39:13 -0500 | [diff] [blame] | 96 | ++incoming_sum; |
Alexandre Lision | a458ba2 | 2013-12-11 15:04:11 -0500 | [diff] [blame] | 97 | } else { |
| 98 | ++outgoing_sum; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 99 | } |
Alexandre Lision | a458ba2 | 2013-12-11 15:04:11 -0500 | [diff] [blame] | 100 | if (historyCall.isMissed()) |
| 101 | missed_sum++; |
Alexandre Lision | 2b4f94e | 2013-12-12 10:15:47 -0500 | [diff] [blame] | 102 | |
| 103 | if(contact.isUnknown() && !linkedTo.isUnknown()) |
| 104 | setContact(linkedTo); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | public String getNumber() { |
| 108 | return calls.lastEntry().getValue().number; |
| 109 | } |
| 110 | |
| 111 | public String getTotalDuration() { |
| 112 | int duration = 0; |
| 113 | ArrayList<HistoryCall> all_calls = new ArrayList<HistoryEntry.HistoryCall>(calls.values()); |
Alexandre Lision | 72e3732 | 2013-11-04 17:14:11 -0500 | [diff] [blame] | 114 | for (int i = 0; i < all_calls.size(); ++i) { |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 115 | duration += all_calls.get(i).getDuration(); |
| 116 | } |
Alexandre Lision | 72e3732 | 2013-11-04 17:14:11 -0500 | [diff] [blame] | 117 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 118 | if (duration < 60) |
| 119 | return duration + "s"; |
| 120 | |
| 121 | return duration / 60 + "min"; |
| 122 | } |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 123 | |
| 124 | public int getMissed_sum() { |
| 125 | return missed_sum; |
| 126 | } |
| 127 | |
| 128 | public int getOutgoing_sum() { |
| 129 | return outgoing_sum; |
| 130 | } |
| 131 | |
| 132 | public int getIncoming_sum() { |
| 133 | return incoming_sum; |
| 134 | } |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 135 | |
| 136 | @Override |
| 137 | public int describeContents() { |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void writeToParcel(Parcel dest, int flags) { |
| 143 | |
| 144 | dest.writeParcelable(contact, 0); |
| 145 | |
| 146 | dest.writeList(new ArrayList<HistoryCall>(calls.values())); |
| 147 | dest.writeList(new ArrayList<Long>(calls.keySet())); |
| 148 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 149 | dest.writeString(accountID); |
| 150 | dest.writeInt(missed_sum); |
| 151 | dest.writeInt(outgoing_sum); |
| 152 | dest.writeInt(incoming_sum); |
| 153 | |
| 154 | } |
| 155 | |
| 156 | public static final Parcelable.Creator<HistoryEntry> CREATOR = new Parcelable.Creator<HistoryEntry>() { |
| 157 | public HistoryEntry createFromParcel(Parcel in) { |
| 158 | return new HistoryEntry(in); |
| 159 | } |
| 160 | |
| 161 | public HistoryEntry[] newArray(int size) { |
| 162 | return new HistoryEntry[size]; |
| 163 | } |
| 164 | }; |
| 165 | |
| 166 | private HistoryEntry(Parcel in) { |
| 167 | contact = in.readParcelable(CallContact.class.getClassLoader()); |
| 168 | |
| 169 | ArrayList<HistoryCall> values = new ArrayList<HistoryEntry.HistoryCall>(); |
| 170 | in.readList(values, HistoryCall.class.getClassLoader()); |
| 171 | |
| 172 | ArrayList<Long> keys = new ArrayList<Long>(); |
| 173 | in.readList(keys, Long.class.getClassLoader()); |
| 174 | |
| 175 | calls = new TreeMap<Long, HistoryEntry.HistoryCall>(); |
| 176 | for (int i = 0; i < keys.size(); ++i) { |
| 177 | calls.put(keys.get(i), values.get(i)); |
| 178 | } |
| 179 | |
| 180 | accountID = in.readString(); |
| 181 | missed_sum = in.readInt(); |
| 182 | outgoing_sum = in.readInt(); |
| 183 | incoming_sum = in.readInt(); |
| 184 | } |
| 185 | |
| 186 | public static class HistoryCall implements Parcelable { |
| 187 | long call_start; |
| 188 | long call_end; |
| 189 | String number; |
Alexandre Lision | 6da3bb9 | 2013-12-10 17:32:46 -0500 | [diff] [blame] | 190 | |
Alexandre Lision | cb2345c | 2013-12-09 15:39:13 -0500 | [diff] [blame] | 191 | boolean missed; |
| 192 | String direction; |
Alexandre Lision | 6da3bb9 | 2013-12-10 17:32:46 -0500 | [diff] [blame] | 193 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 194 | String recordPath; |
| 195 | String timeFormatted; |
| 196 | String displayName; |
| 197 | |
Alexandre Lision | aad014e | 2014-01-14 17:45:49 -0500 | [diff] [blame^] | 198 | String accountID; |
| 199 | |
| 200 | long contactID; |
| 201 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 202 | public HistoryCall(HashMap<String, String> entry) { |
| 203 | call_end = Long.parseLong(entry.get(ServiceConstants.history.TIMESTAMP_STOP_KEY)); |
| 204 | call_start = Long.parseLong(entry.get(ServiceConstants.history.TIMESTAMP_START_KEY)); |
Alexandre Lision | aad014e | 2014-01-14 17:45:49 -0500 | [diff] [blame^] | 205 | accountID = entry.get(ServiceConstants.history.ACCOUNT_ID_KEY); |
Alexandre Lision | 6da3bb9 | 2013-12-10 17:32:46 -0500 | [diff] [blame] | 206 | |
Alexandre Lision | cb2345c | 2013-12-09 15:39:13 -0500 | [diff] [blame] | 207 | direction = entry.get(ServiceConstants.history.DIRECTION_KEY); |
| 208 | missed = entry.get(ServiceConstants.history.MISSED_KEY).contentEquals("true"); |
Alexandre Lision | 6da3bb9 | 2013-12-10 17:32:46 -0500 | [diff] [blame] | 209 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 210 | displayName = entry.get(ServiceConstants.history.DISPLAY_NAME_KEY); |
| 211 | recordPath = entry.get(ServiceConstants.history.RECORDING_PATH_KEY); |
| 212 | number = entry.get(ServiceConstants.history.PEER_NUMBER_KEY); |
Alexandre Lision | aad014e | 2014-01-14 17:45:49 -0500 | [diff] [blame^] | 213 | timeFormatted = HistoryTimeModel.timeToHistoryConst(call_start); |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 214 | } |
| 215 | |
Alexandre Lision | cb2345c | 2013-12-09 15:39:13 -0500 | [diff] [blame] | 216 | public String getDirection() { |
| 217 | return direction; |
| 218 | } |
| 219 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 220 | public String getDate() { |
| 221 | return timeFormatted; |
| 222 | } |
| 223 | |
Alexandre Lision | 6da3bb9 | 2013-12-10 17:32:46 -0500 | [diff] [blame] | 224 | public String getStartString(String format) { |
| 225 | Timestamp stamp = new Timestamp(call_start * 1000); // in milliseconds |
| 226 | Date date = new Date(stamp.getTime()); |
| 227 | SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault()); |
| 228 | sdf.setTimeZone(TimeZone.getDefault()); |
| 229 | String formattedDate = sdf.format(date); |
| 230 | return formattedDate; |
| 231 | |
| 232 | } |
| 233 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 234 | public String getDurationString() { |
| 235 | |
| 236 | long duration = call_end - call_start; |
| 237 | if (duration < 60) |
| 238 | return String.format(Locale.getDefault(), "%02d secs", duration); |
| 239 | |
| 240 | if (duration < 3600) |
| 241 | return String.format(Locale.getDefault(), "%02d mins %02d secs", (duration % 3600) / 60, (duration % 60)); |
| 242 | |
| 243 | return String.format(Locale.getDefault(), "%d h %02d mins %02d secs", duration / 3600, (duration % 3600) / 60, (duration % 60)); |
| 244 | |
| 245 | } |
| 246 | |
| 247 | public long getDuration() { |
| 248 | return call_end - call_start; |
| 249 | |
| 250 | } |
| 251 | |
| 252 | public String getRecordPath() { |
| 253 | return recordPath; |
| 254 | } |
| 255 | |
| 256 | public String getNumber() { |
| 257 | return number; |
| 258 | } |
| 259 | |
| 260 | public String getDisplayName() { |
Alexandre Lision | a458ba2 | 2013-12-11 15:04:11 -0500 | [diff] [blame] | 261 | return displayName.substring(0, displayName.indexOf('@')); |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 262 | } |
| 263 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 264 | @Override |
| 265 | public int describeContents() { |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public void writeToParcel(Parcel dest, int flags) { |
| 271 | dest.writeLong(call_start); |
| 272 | dest.writeLong(call_end); |
Alexandre Lision | aad014e | 2014-01-14 17:45:49 -0500 | [diff] [blame^] | 273 | dest.writeString(accountID); |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 274 | dest.writeString(number); |
Alexandre Lision | cb2345c | 2013-12-09 15:39:13 -0500 | [diff] [blame] | 275 | dest.writeByte((byte) (missed ? 1 : 0)); |
| 276 | dest.writeString(direction); |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 277 | dest.writeString(recordPath); |
| 278 | dest.writeString(timeFormatted); |
| 279 | dest.writeString(displayName); |
Alexandre Lision | aad014e | 2014-01-14 17:45:49 -0500 | [diff] [blame^] | 280 | dest.writeLong(contactID); |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | public static final Parcelable.Creator<HistoryCall> CREATOR = new Parcelable.Creator<HistoryCall>() { |
| 284 | public HistoryCall createFromParcel(Parcel in) { |
| 285 | return new HistoryCall(in); |
| 286 | } |
| 287 | |
| 288 | public HistoryCall[] newArray(int size) { |
| 289 | return new HistoryCall[size]; |
| 290 | } |
| 291 | }; |
| 292 | |
| 293 | private HistoryCall(Parcel in) { |
| 294 | call_start = in.readLong(); |
| 295 | call_end = in.readLong(); |
Alexandre Lision | aad014e | 2014-01-14 17:45:49 -0500 | [diff] [blame^] | 296 | accountID = in.readString(); |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 297 | number = in.readString(); |
Alexandre Lision | cb2345c | 2013-12-09 15:39:13 -0500 | [diff] [blame] | 298 | missed = in.readByte() == 1 ? true : false; |
| 299 | direction = in.readString(); |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 300 | recordPath = in.readString(); |
| 301 | timeFormatted = in.readString(); |
| 302 | displayName = in.readString(); |
Alexandre Lision | aad014e | 2014-01-14 17:45:49 -0500 | [diff] [blame^] | 303 | contactID = in.readLong(); |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | public boolean hasRecord() { |
| 307 | return recordPath.length() > 0; |
| 308 | } |
| 309 | |
Alexandre Lision | cb2345c | 2013-12-09 15:39:13 -0500 | [diff] [blame] | 310 | public boolean isIncoming() { |
| 311 | return direction.contentEquals(ServiceConstants.history.INCOMING_STRING); |
| 312 | } |
| 313 | |
| 314 | public boolean isMissed() { |
| 315 | return missed; |
| 316 | } |
| 317 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 318 | } |
| 319 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 320 | } |