blob: 141690e5e443ca476fd0e6c98987fd88e50346d5 [file] [log] [blame]
Alexandre Lision2e52d392013-11-06 15:14:31 -05001/*
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 Lision064e1e02013-10-01 16:18:42 -040032package org.sflphone.model;
alision2ec64f92013-06-17 17:28:58 -040033
Alexandre Lision6da3bb92013-12-10 17:32:46 -050034import java.sql.Timestamp;
35import java.text.SimpleDateFormat;
alision2ec64f92013-06-17 17:28:58 -040036import java.util.ArrayList;
Alexandre Lision6da3bb92013-12-10 17:32:46 -050037import java.util.Date;
Alexandre Lision72e37322013-11-04 17:14:11 -050038import java.util.HashMap;
Alexandre Lision2e52d392013-11-06 15:14:31 -050039import java.util.Locale;
alision2ec64f92013-06-17 17:28:58 -040040import java.util.NavigableMap;
Alexandre Lision6da3bb92013-12-10 17:32:46 -050041import java.util.TimeZone;
alision2ec64f92013-06-17 17:28:58 -040042import java.util.TreeMap;
43
Alexandre Lision064e1e02013-10-01 16:18:42 -040044import org.sflphone.service.ServiceConstants;
Alexandre Lision72e37322013-11-04 17:14:11 -050045import org.sflphone.utils.HistoryManager;
alision907bde72013-06-20 14:40:37 -040046
Alexandre Lision2e52d392013-11-06 15:14:31 -050047import android.os.Parcel;
48import android.os.Parcelable;
49
50public class HistoryEntry implements Parcelable {
alision2ec64f92013-06-17 17:28:58 -040051
52 private CallContact contact;
53 private NavigableMap<Long, HistoryCall> calls;
54 private String accountID;
alision907bde72013-06-20 14:40:37 -040055 int missed_sum;
56 int outgoing_sum;
57 int incoming_sum;
alision2ec64f92013-06-17 17:28:58 -040058
alision907bde72013-06-20 14:40:37 -040059 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 Lision72e37322013-11-04 17:14:11 -050065
alision2ec64f92013-06-17 17:28:58 -040066 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
alision2ec64f92013-06-17 17:28:58 -040078 public CallContact getContact() {
79 return contact;
80 }
81
82 public void setContact(CallContact contact) {
83 this.contact = contact;
84 }
85
Alexandre Lision2b4f94e2013-12-12 10:15:47 -050086 /**
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) {
alision2ec64f92013-06-17 17:28:58 -040094 calls.put(historyCall.call_start, historyCall);
Alexandre Lisiona458ba22013-12-11 15:04:11 -050095 if (historyCall.isIncoming()) {
Alexandre Lisioncb2345c2013-12-09 15:39:13 -050096 ++incoming_sum;
Alexandre Lisiona458ba22013-12-11 15:04:11 -050097 } else {
98 ++outgoing_sum;
alision907bde72013-06-20 14:40:37 -040099 }
Alexandre Lisiona458ba22013-12-11 15:04:11 -0500100 if (historyCall.isMissed())
101 missed_sum++;
Alexandre Lision2b4f94e2013-12-12 10:15:47 -0500102
103 if(contact.isUnknown() && !linkedTo.isUnknown())
104 setContact(linkedTo);
alision2ec64f92013-06-17 17:28:58 -0400105 }
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 Lision72e37322013-11-04 17:14:11 -0500114 for (int i = 0; i < all_calls.size(); ++i) {
alision2ec64f92013-06-17 17:28:58 -0400115 duration += all_calls.get(i).getDuration();
116 }
Alexandre Lision72e37322013-11-04 17:14:11 -0500117
alision2ec64f92013-06-17 17:28:58 -0400118 if (duration < 60)
119 return duration + "s";
120
121 return duration / 60 + "min";
122 }
alision907bde72013-06-20 14:40:37 -0400123
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 Lision2e52d392013-11-06 15:14:31 -0500135
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 Lision2e52d392013-11-06 15:14:31 -0500149 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 Lision6da3bb92013-12-10 17:32:46 -0500190
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500191 boolean missed;
192 String direction;
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500193
Alexandre Lision2e52d392013-11-06 15:14:31 -0500194 String recordPath;
195 String timeFormatted;
196 String displayName;
197
198 public HistoryCall(HashMap<String, String> entry) {
199 call_end = Long.parseLong(entry.get(ServiceConstants.history.TIMESTAMP_STOP_KEY));
200 call_start = Long.parseLong(entry.get(ServiceConstants.history.TIMESTAMP_START_KEY));
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500201
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500202 direction = entry.get(ServiceConstants.history.DIRECTION_KEY);
203 missed = entry.get(ServiceConstants.history.MISSED_KEY).contentEquals("true");
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500204
Alexandre Lision2e52d392013-11-06 15:14:31 -0500205 displayName = entry.get(ServiceConstants.history.DISPLAY_NAME_KEY);
206 recordPath = entry.get(ServiceConstants.history.RECORDING_PATH_KEY);
207 number = entry.get(ServiceConstants.history.PEER_NUMBER_KEY);
208 timeFormatted = HistoryManager.timeToHistoryConst(call_start);
209 }
210
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500211 public String getDirection() {
212 return direction;
213 }
214
Alexandre Lision2e52d392013-11-06 15:14:31 -0500215 public String getDate() {
216 return timeFormatted;
217 }
218
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500219 public String getStartString(String format) {
220 Timestamp stamp = new Timestamp(call_start * 1000); // in milliseconds
221 Date date = new Date(stamp.getTime());
222 SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
223 sdf.setTimeZone(TimeZone.getDefault());
224 String formattedDate = sdf.format(date);
225 return formattedDate;
226
227 }
228
Alexandre Lision2e52d392013-11-06 15:14:31 -0500229 public String getDurationString() {
230
231 long duration = call_end - call_start;
232 if (duration < 60)
233 return String.format(Locale.getDefault(), "%02d secs", duration);
234
235 if (duration < 3600)
236 return String.format(Locale.getDefault(), "%02d mins %02d secs", (duration % 3600) / 60, (duration % 60));
237
238 return String.format(Locale.getDefault(), "%d h %02d mins %02d secs", duration / 3600, (duration % 3600) / 60, (duration % 60));
239
240 }
241
242 public long getDuration() {
243 return call_end - call_start;
244
245 }
246
247 public String getRecordPath() {
248 return recordPath;
249 }
250
251 public String getNumber() {
252 return number;
253 }
254
255 public String getDisplayName() {
Alexandre Lisiona458ba22013-12-11 15:04:11 -0500256 return displayName.substring(0, displayName.indexOf('@'));
Alexandre Lision2e52d392013-11-06 15:14:31 -0500257 }
258
Alexandre Lision2e52d392013-11-06 15:14:31 -0500259 @Override
260 public int describeContents() {
261 return 0;
262 }
263
264 @Override
265 public void writeToParcel(Parcel dest, int flags) {
266 dest.writeLong(call_start);
267 dest.writeLong(call_end);
268 dest.writeString(number);
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500269 dest.writeByte((byte) (missed ? 1 : 0));
270 dest.writeString(direction);
Alexandre Lision2e52d392013-11-06 15:14:31 -0500271 dest.writeString(recordPath);
272 dest.writeString(timeFormatted);
273 dest.writeString(displayName);
274 }
275
276 public static final Parcelable.Creator<HistoryCall> CREATOR = new Parcelable.Creator<HistoryCall>() {
277 public HistoryCall createFromParcel(Parcel in) {
278 return new HistoryCall(in);
279 }
280
281 public HistoryCall[] newArray(int size) {
282 return new HistoryCall[size];
283 }
284 };
285
286 private HistoryCall(Parcel in) {
287 call_start = in.readLong();
288 call_end = in.readLong();
289 number = in.readString();
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500290 missed = in.readByte() == 1 ? true : false;
291 direction = in.readString();
Alexandre Lision2e52d392013-11-06 15:14:31 -0500292 recordPath = in.readString();
293 timeFormatted = in.readString();
294 displayName = in.readString();
295 }
296
297 public boolean hasRecord() {
298 return recordPath.length() > 0;
299 }
300
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500301 public boolean isIncoming() {
302 return direction.contentEquals(ServiceConstants.history.INCOMING_STRING);
303 }
304
305 public boolean isMissed() {
306 return missed;
307 }
308
Alexandre Lision2e52d392013-11-06 15:14:31 -0500309 }
310
alision2ec64f92013-06-17 17:28:58 -0400311}