Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2013 Savoir-Faire Linux Inc. |
| 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 | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 32 | package org.sflphone.model; |
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 | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 44 | import org.sflphone.service.ServiceConstants; |
Alexandre Lision | 72e3732 | 2013-11-04 17:14:11 -0500 | [diff] [blame] | 45 | import org.sflphone.utils.HistoryManager; |
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 | |
| 86 | public void addHistoryCall(HistoryCall historyCall) { |
| 87 | calls.put(historyCall.call_start, historyCall); |
Alexandre Lision | cb2345c | 2013-12-09 15:39:13 -0500 | [diff] [blame] | 88 | if (historyCall.getDirection().contentEquals(ServiceConstants.history.OUTGOING_STRING)) { |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 89 | ++outgoing_sum; |
Alexandre Lision | cb2345c | 2013-12-09 15:39:13 -0500 | [diff] [blame] | 90 | } else if (historyCall.isIncoming()) { |
| 91 | ++incoming_sum; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 92 | } |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | public String getNumber() { |
| 96 | return calls.lastEntry().getValue().number; |
| 97 | } |
| 98 | |
| 99 | public String getTotalDuration() { |
| 100 | int duration = 0; |
| 101 | ArrayList<HistoryCall> all_calls = new ArrayList<HistoryEntry.HistoryCall>(calls.values()); |
Alexandre Lision | 72e3732 | 2013-11-04 17:14:11 -0500 | [diff] [blame] | 102 | for (int i = 0; i < all_calls.size(); ++i) { |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 103 | duration += all_calls.get(i).getDuration(); |
| 104 | } |
Alexandre Lision | 72e3732 | 2013-11-04 17:14:11 -0500 | [diff] [blame] | 105 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 106 | if (duration < 60) |
| 107 | return duration + "s"; |
| 108 | |
| 109 | return duration / 60 + "min"; |
| 110 | } |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 111 | |
| 112 | public int getMissed_sum() { |
| 113 | return missed_sum; |
| 114 | } |
| 115 | |
| 116 | public int getOutgoing_sum() { |
| 117 | return outgoing_sum; |
| 118 | } |
| 119 | |
| 120 | public int getIncoming_sum() { |
| 121 | return incoming_sum; |
| 122 | } |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 123 | |
| 124 | @Override |
| 125 | public int describeContents() { |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void writeToParcel(Parcel dest, int flags) { |
| 131 | |
| 132 | dest.writeParcelable(contact, 0); |
| 133 | |
| 134 | dest.writeList(new ArrayList<HistoryCall>(calls.values())); |
| 135 | dest.writeList(new ArrayList<Long>(calls.keySet())); |
| 136 | |
| 137 | dest.writeMap(calls); |
| 138 | dest.writeString(accountID); |
| 139 | dest.writeInt(missed_sum); |
| 140 | dest.writeInt(outgoing_sum); |
| 141 | dest.writeInt(incoming_sum); |
| 142 | |
| 143 | } |
| 144 | |
| 145 | public static final Parcelable.Creator<HistoryEntry> CREATOR = new Parcelable.Creator<HistoryEntry>() { |
| 146 | public HistoryEntry createFromParcel(Parcel in) { |
| 147 | return new HistoryEntry(in); |
| 148 | } |
| 149 | |
| 150 | public HistoryEntry[] newArray(int size) { |
| 151 | return new HistoryEntry[size]; |
| 152 | } |
| 153 | }; |
| 154 | |
| 155 | private HistoryEntry(Parcel in) { |
| 156 | contact = in.readParcelable(CallContact.class.getClassLoader()); |
| 157 | |
| 158 | ArrayList<HistoryCall> values = new ArrayList<HistoryEntry.HistoryCall>(); |
| 159 | in.readList(values, HistoryCall.class.getClassLoader()); |
| 160 | |
| 161 | ArrayList<Long> keys = new ArrayList<Long>(); |
| 162 | in.readList(keys, Long.class.getClassLoader()); |
| 163 | |
| 164 | calls = new TreeMap<Long, HistoryEntry.HistoryCall>(); |
| 165 | for (int i = 0; i < keys.size(); ++i) { |
| 166 | calls.put(keys.get(i), values.get(i)); |
| 167 | } |
| 168 | |
| 169 | accountID = in.readString(); |
| 170 | missed_sum = in.readInt(); |
| 171 | outgoing_sum = in.readInt(); |
| 172 | incoming_sum = in.readInt(); |
| 173 | } |
| 174 | |
| 175 | public static class HistoryCall implements Parcelable { |
| 176 | long call_start; |
| 177 | long call_end; |
| 178 | String number; |
Alexandre Lision | 6da3bb9 | 2013-12-10 17:32:46 -0500 | [diff] [blame] | 179 | |
Alexandre Lision | cb2345c | 2013-12-09 15:39:13 -0500 | [diff] [blame] | 180 | boolean missed; |
| 181 | String direction; |
Alexandre Lision | 6da3bb9 | 2013-12-10 17:32:46 -0500 | [diff] [blame] | 182 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 183 | String recordPath; |
| 184 | String timeFormatted; |
| 185 | String displayName; |
| 186 | |
| 187 | public HistoryCall(HashMap<String, String> entry) { |
| 188 | call_end = Long.parseLong(entry.get(ServiceConstants.history.TIMESTAMP_STOP_KEY)); |
| 189 | call_start = Long.parseLong(entry.get(ServiceConstants.history.TIMESTAMP_START_KEY)); |
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 | direction = entry.get(ServiceConstants.history.DIRECTION_KEY); |
| 192 | missed = entry.get(ServiceConstants.history.MISSED_KEY).contentEquals("true"); |
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 | displayName = entry.get(ServiceConstants.history.DISPLAY_NAME_KEY); |
| 195 | recordPath = entry.get(ServiceConstants.history.RECORDING_PATH_KEY); |
| 196 | number = entry.get(ServiceConstants.history.PEER_NUMBER_KEY); |
| 197 | timeFormatted = HistoryManager.timeToHistoryConst(call_start); |
| 198 | } |
| 199 | |
Alexandre Lision | cb2345c | 2013-12-09 15:39:13 -0500 | [diff] [blame] | 200 | public String getDirection() { |
| 201 | return direction; |
| 202 | } |
| 203 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 204 | public String getDate() { |
| 205 | return timeFormatted; |
| 206 | } |
| 207 | |
Alexandre Lision | 6da3bb9 | 2013-12-10 17:32:46 -0500 | [diff] [blame] | 208 | public String getStartString(String format) { |
| 209 | Timestamp stamp = new Timestamp(call_start * 1000); // in milliseconds |
| 210 | Date date = new Date(stamp.getTime()); |
| 211 | SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault()); |
| 212 | sdf.setTimeZone(TimeZone.getDefault()); |
| 213 | String formattedDate = sdf.format(date); |
| 214 | return formattedDate; |
| 215 | |
| 216 | } |
| 217 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 218 | public String getDurationString() { |
| 219 | |
| 220 | long duration = call_end - call_start; |
| 221 | if (duration < 60) |
| 222 | return String.format(Locale.getDefault(), "%02d secs", duration); |
| 223 | |
| 224 | if (duration < 3600) |
| 225 | return String.format(Locale.getDefault(), "%02d mins %02d secs", (duration % 3600) / 60, (duration % 60)); |
| 226 | |
| 227 | return String.format(Locale.getDefault(), "%d h %02d mins %02d secs", duration / 3600, (duration % 3600) / 60, (duration % 60)); |
| 228 | |
| 229 | } |
| 230 | |
| 231 | public long getDuration() { |
| 232 | return call_end - call_start; |
| 233 | |
| 234 | } |
| 235 | |
| 236 | public String getRecordPath() { |
| 237 | return recordPath; |
| 238 | } |
| 239 | |
| 240 | public String getNumber() { |
| 241 | return number; |
| 242 | } |
| 243 | |
| 244 | public String getDisplayName() { |
| 245 | return displayName; |
| 246 | } |
| 247 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 248 | @Override |
| 249 | public int describeContents() { |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | @Override |
| 254 | public void writeToParcel(Parcel dest, int flags) { |
| 255 | dest.writeLong(call_start); |
| 256 | dest.writeLong(call_end); |
| 257 | dest.writeString(number); |
Alexandre Lision | cb2345c | 2013-12-09 15:39:13 -0500 | [diff] [blame] | 258 | dest.writeByte((byte) (missed ? 1 : 0)); |
| 259 | dest.writeString(direction); |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 260 | dest.writeString(recordPath); |
| 261 | dest.writeString(timeFormatted); |
| 262 | dest.writeString(displayName); |
| 263 | } |
| 264 | |
| 265 | public static final Parcelable.Creator<HistoryCall> CREATOR = new Parcelable.Creator<HistoryCall>() { |
| 266 | public HistoryCall createFromParcel(Parcel in) { |
| 267 | return new HistoryCall(in); |
| 268 | } |
| 269 | |
| 270 | public HistoryCall[] newArray(int size) { |
| 271 | return new HistoryCall[size]; |
| 272 | } |
| 273 | }; |
| 274 | |
| 275 | private HistoryCall(Parcel in) { |
| 276 | call_start = in.readLong(); |
| 277 | call_end = in.readLong(); |
| 278 | number = in.readString(); |
Alexandre Lision | cb2345c | 2013-12-09 15:39:13 -0500 | [diff] [blame] | 279 | missed = in.readByte() == 1 ? true : false; |
| 280 | direction = in.readString(); |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 281 | recordPath = in.readString(); |
| 282 | timeFormatted = in.readString(); |
| 283 | displayName = in.readString(); |
| 284 | } |
| 285 | |
| 286 | public boolean hasRecord() { |
| 287 | return recordPath.length() > 0; |
| 288 | } |
| 289 | |
Alexandre Lision | cb2345c | 2013-12-09 15:39:13 -0500 | [diff] [blame] | 290 | public boolean isIncoming() { |
| 291 | return direction.contentEquals(ServiceConstants.history.INCOMING_STRING); |
| 292 | } |
| 293 | |
| 294 | public boolean isMissed() { |
| 295 | return missed; |
| 296 | } |
| 297 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 298 | } |
| 299 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 300 | } |