blob: 278277b65cf3741b734af9f0b99ce284dcc24457 [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
86 public void addHistoryCall(HistoryCall historyCall) {
87 calls.put(historyCall.call_start, historyCall);
Alexandre Lisiona458ba22013-12-11 15:04:11 -050088 if (historyCall.isIncoming()) {
Alexandre Lisioncb2345c2013-12-09 15:39:13 -050089 ++incoming_sum;
Alexandre Lisiona458ba22013-12-11 15:04:11 -050090 } else {
91 ++outgoing_sum;
alision907bde72013-06-20 14:40:37 -040092 }
Alexandre Lisiona458ba22013-12-11 15:04:11 -050093 if (historyCall.isMissed())
94 missed_sum++;
alision2ec64f92013-06-17 17:28:58 -040095 }
96
97 public String getNumber() {
98 return calls.lastEntry().getValue().number;
99 }
100
101 public String getTotalDuration() {
102 int duration = 0;
103 ArrayList<HistoryCall> all_calls = new ArrayList<HistoryEntry.HistoryCall>(calls.values());
Alexandre Lision72e37322013-11-04 17:14:11 -0500104 for (int i = 0; i < all_calls.size(); ++i) {
alision2ec64f92013-06-17 17:28:58 -0400105 duration += all_calls.get(i).getDuration();
106 }
Alexandre Lision72e37322013-11-04 17:14:11 -0500107
alision2ec64f92013-06-17 17:28:58 -0400108 if (duration < 60)
109 return duration + "s";
110
111 return duration / 60 + "min";
112 }
alision907bde72013-06-20 14:40:37 -0400113
114 public int getMissed_sum() {
115 return missed_sum;
116 }
117
118 public int getOutgoing_sum() {
119 return outgoing_sum;
120 }
121
122 public int getIncoming_sum() {
123 return incoming_sum;
124 }
Alexandre Lision2e52d392013-11-06 15:14:31 -0500125
126 @Override
127 public int describeContents() {
128 return 0;
129 }
130
131 @Override
132 public void writeToParcel(Parcel dest, int flags) {
133
134 dest.writeParcelable(contact, 0);
135
136 dest.writeList(new ArrayList<HistoryCall>(calls.values()));
137 dest.writeList(new ArrayList<Long>(calls.keySet()));
138
Alexandre Lision2e52d392013-11-06 15:14:31 -0500139 dest.writeString(accountID);
140 dest.writeInt(missed_sum);
141 dest.writeInt(outgoing_sum);
142 dest.writeInt(incoming_sum);
143
144 }
145
146 public static final Parcelable.Creator<HistoryEntry> CREATOR = new Parcelable.Creator<HistoryEntry>() {
147 public HistoryEntry createFromParcel(Parcel in) {
148 return new HistoryEntry(in);
149 }
150
151 public HistoryEntry[] newArray(int size) {
152 return new HistoryEntry[size];
153 }
154 };
155
156 private HistoryEntry(Parcel in) {
157 contact = in.readParcelable(CallContact.class.getClassLoader());
158
159 ArrayList<HistoryCall> values = new ArrayList<HistoryEntry.HistoryCall>();
160 in.readList(values, HistoryCall.class.getClassLoader());
161
162 ArrayList<Long> keys = new ArrayList<Long>();
163 in.readList(keys, Long.class.getClassLoader());
164
165 calls = new TreeMap<Long, HistoryEntry.HistoryCall>();
166 for (int i = 0; i < keys.size(); ++i) {
167 calls.put(keys.get(i), values.get(i));
168 }
169
170 accountID = in.readString();
171 missed_sum = in.readInt();
172 outgoing_sum = in.readInt();
173 incoming_sum = in.readInt();
174 }
175
176 public static class HistoryCall implements Parcelable {
177 long call_start;
178 long call_end;
179 String number;
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500180
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500181 boolean missed;
182 String direction;
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500183
Alexandre Lision2e52d392013-11-06 15:14:31 -0500184 String recordPath;
185 String timeFormatted;
186 String displayName;
187
188 public HistoryCall(HashMap<String, String> entry) {
189 call_end = Long.parseLong(entry.get(ServiceConstants.history.TIMESTAMP_STOP_KEY));
190 call_start = Long.parseLong(entry.get(ServiceConstants.history.TIMESTAMP_START_KEY));
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500191
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500192 direction = entry.get(ServiceConstants.history.DIRECTION_KEY);
193 missed = entry.get(ServiceConstants.history.MISSED_KEY).contentEquals("true");
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500194
Alexandre Lision2e52d392013-11-06 15:14:31 -0500195 displayName = entry.get(ServiceConstants.history.DISPLAY_NAME_KEY);
196 recordPath = entry.get(ServiceConstants.history.RECORDING_PATH_KEY);
197 number = entry.get(ServiceConstants.history.PEER_NUMBER_KEY);
198 timeFormatted = HistoryManager.timeToHistoryConst(call_start);
199 }
200
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500201 public String getDirection() {
202 return direction;
203 }
204
Alexandre Lision2e52d392013-11-06 15:14:31 -0500205 public String getDate() {
206 return timeFormatted;
207 }
208
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500209 public String getStartString(String format) {
210 Timestamp stamp = new Timestamp(call_start * 1000); // in milliseconds
211 Date date = new Date(stamp.getTime());
212 SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
213 sdf.setTimeZone(TimeZone.getDefault());
214 String formattedDate = sdf.format(date);
215 return formattedDate;
216
217 }
218
Alexandre Lision2e52d392013-11-06 15:14:31 -0500219 public String getDurationString() {
220
221 long duration = call_end - call_start;
222 if (duration < 60)
223 return String.format(Locale.getDefault(), "%02d secs", duration);
224
225 if (duration < 3600)
226 return String.format(Locale.getDefault(), "%02d mins %02d secs", (duration % 3600) / 60, (duration % 60));
227
228 return String.format(Locale.getDefault(), "%d h %02d mins %02d secs", duration / 3600, (duration % 3600) / 60, (duration % 60));
229
230 }
231
232 public long getDuration() {
233 return call_end - call_start;
234
235 }
236
237 public String getRecordPath() {
238 return recordPath;
239 }
240
241 public String getNumber() {
242 return number;
243 }
244
245 public String getDisplayName() {
Alexandre Lisiona458ba22013-12-11 15:04:11 -0500246 return displayName.substring(0, displayName.indexOf('@'));
Alexandre Lision2e52d392013-11-06 15:14:31 -0500247 }
248
Alexandre Lision2e52d392013-11-06 15:14:31 -0500249 @Override
250 public int describeContents() {
251 return 0;
252 }
253
254 @Override
255 public void writeToParcel(Parcel dest, int flags) {
256 dest.writeLong(call_start);
257 dest.writeLong(call_end);
258 dest.writeString(number);
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500259 dest.writeByte((byte) (missed ? 1 : 0));
260 dest.writeString(direction);
Alexandre Lision2e52d392013-11-06 15:14:31 -0500261 dest.writeString(recordPath);
262 dest.writeString(timeFormatted);
263 dest.writeString(displayName);
264 }
265
266 public static final Parcelable.Creator<HistoryCall> CREATOR = new Parcelable.Creator<HistoryCall>() {
267 public HistoryCall createFromParcel(Parcel in) {
268 return new HistoryCall(in);
269 }
270
271 public HistoryCall[] newArray(int size) {
272 return new HistoryCall[size];
273 }
274 };
275
276 private HistoryCall(Parcel in) {
277 call_start = in.readLong();
278 call_end = in.readLong();
279 number = in.readString();
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500280 missed = in.readByte() == 1 ? true : false;
281 direction = in.readString();
Alexandre Lision2e52d392013-11-06 15:14:31 -0500282 recordPath = in.readString();
283 timeFormatted = in.readString();
284 displayName = in.readString();
285 }
286
287 public boolean hasRecord() {
288 return recordPath.length() > 0;
289 }
290
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500291 public boolean isIncoming() {
292 return direction.contentEquals(ServiceConstants.history.INCOMING_STRING);
293 }
294
295 public boolean isMissed() {
296 return missed;
297 }
298
Alexandre Lision2e52d392013-11-06 15:14:31 -0500299 }
300
alision2ec64f92013-06-17 17:28:58 -0400301}