blob: 578a0308ab043a41c644342538a093b968b1139c [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 Lisioncb2345c2013-12-09 15:39:13 -050088 if (historyCall.getDirection().contentEquals(ServiceConstants.history.OUTGOING_STRING)) {
alision907bde72013-06-20 14:40:37 -040089 ++outgoing_sum;
Alexandre Lisioncb2345c2013-12-09 15:39:13 -050090 } else if (historyCall.isIncoming()) {
91 ++incoming_sum;
alision907bde72013-06-20 14:40:37 -040092 }
alision2ec64f92013-06-17 17:28:58 -040093 }
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 Lision72e37322013-11-04 17:14:11 -0500102 for (int i = 0; i < all_calls.size(); ++i) {
alision2ec64f92013-06-17 17:28:58 -0400103 duration += all_calls.get(i).getDuration();
104 }
Alexandre Lision72e37322013-11-04 17:14:11 -0500105
alision2ec64f92013-06-17 17:28:58 -0400106 if (duration < 60)
107 return duration + "s";
108
109 return duration / 60 + "min";
110 }
alision907bde72013-06-20 14:40:37 -0400111
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 Lision2e52d392013-11-06 15:14:31 -0500123
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 Lision6da3bb92013-12-10 17:32:46 -0500179
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500180 boolean missed;
181 String direction;
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500182
Alexandre Lision2e52d392013-11-06 15:14:31 -0500183 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 Lision6da3bb92013-12-10 17:32:46 -0500190
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500191 direction = entry.get(ServiceConstants.history.DIRECTION_KEY);
192 missed = entry.get(ServiceConstants.history.MISSED_KEY).contentEquals("true");
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500193
Alexandre Lision2e52d392013-11-06 15:14:31 -0500194 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 Lisioncb2345c2013-12-09 15:39:13 -0500200 public String getDirection() {
201 return direction;
202 }
203
Alexandre Lision2e52d392013-11-06 15:14:31 -0500204 public String getDate() {
205 return timeFormatted;
206 }
207
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500208 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 Lision2e52d392013-11-06 15:14:31 -0500218 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 Lision2e52d392013-11-06 15:14:31 -0500248 @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 Lisioncb2345c2013-12-09 15:39:13 -0500258 dest.writeByte((byte) (missed ? 1 : 0));
259 dest.writeString(direction);
Alexandre Lision2e52d392013-11-06 15:14:31 -0500260 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 Lisioncb2345c2013-12-09 15:39:13 -0500279 missed = in.readByte() == 1 ? true : false;
280 direction = in.readString();
Alexandre Lision2e52d392013-11-06 15:14:31 -0500281 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 Lisioncb2345c2013-12-09 15:39:13 -0500290 public boolean isIncoming() {
291 return direction.contentEquals(ServiceConstants.history.INCOMING_STRING);
292 }
293
294 public boolean isMissed() {
295 return missed;
296 }
297
Alexandre Lision2e52d392013-11-06 15:14:31 -0500298 }
299
alision2ec64f92013-06-17 17:28:58 -0400300}