blob: a67067b5ce0d2bbebf628441cb500824d824f465 [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
alision2ec64f92013-06-17 17:28:58 -040034import java.util.ArrayList;
Alexandre Lision72e37322013-11-04 17:14:11 -050035import java.util.HashMap;
Alexandre Lision2e52d392013-11-06 15:14:31 -050036import java.util.Locale;
alision2ec64f92013-06-17 17:28:58 -040037import java.util.NavigableMap;
alision2ec64f92013-06-17 17:28:58 -040038import java.util.TreeMap;
39
Alexandre Lision064e1e02013-10-01 16:18:42 -040040import org.sflphone.service.ServiceConstants;
Alexandre Lision72e37322013-11-04 17:14:11 -050041import org.sflphone.utils.HistoryManager;
alision907bde72013-06-20 14:40:37 -040042
Alexandre Lision2e52d392013-11-06 15:14:31 -050043import android.os.Parcel;
44import android.os.Parcelable;
45
46public class HistoryEntry implements Parcelable {
alision2ec64f92013-06-17 17:28:58 -040047
48 private CallContact contact;
49 private NavigableMap<Long, HistoryCall> calls;
50 private String accountID;
alision907bde72013-06-20 14:40:37 -040051 int missed_sum;
52 int outgoing_sum;
53 int incoming_sum;
alision2ec64f92013-06-17 17:28:58 -040054
alision907bde72013-06-20 14:40:37 -040055 public HistoryEntry(String account, CallContact c) {
56 contact = c;
57 calls = new TreeMap<Long, HistoryEntry.HistoryCall>();
58 accountID = account;
59 missed_sum = outgoing_sum = incoming_sum = 0;
60 }
Alexandre Lision72e37322013-11-04 17:14:11 -050061
alision2ec64f92013-06-17 17:28:58 -040062 public String getAccountID() {
63 return accountID;
64 }
65
66 public void setAccountID(String accountID) {
67 this.accountID = accountID;
68 }
69
70 public NavigableMap<Long, HistoryCall> getCalls() {
71 return calls;
72 }
73
alision2ec64f92013-06-17 17:28:58 -040074 public CallContact getContact() {
75 return contact;
76 }
77
78 public void setContact(CallContact contact) {
79 this.contact = contact;
80 }
81
82 public void addHistoryCall(HistoryCall historyCall) {
83 calls.put(historyCall.call_start, historyCall);
Alexandre Lisioncb2345c2013-12-09 15:39:13 -050084 if (historyCall.getDirection().contentEquals(ServiceConstants.history.OUTGOING_STRING)) {
alision907bde72013-06-20 14:40:37 -040085 ++outgoing_sum;
Alexandre Lisioncb2345c2013-12-09 15:39:13 -050086 } else if (historyCall.isIncoming()) {
87 ++incoming_sum;
alision907bde72013-06-20 14:40:37 -040088 }
alision2ec64f92013-06-17 17:28:58 -040089 }
90
91 public String getNumber() {
92 return calls.lastEntry().getValue().number;
93 }
94
95 public String getTotalDuration() {
96 int duration = 0;
97 ArrayList<HistoryCall> all_calls = new ArrayList<HistoryEntry.HistoryCall>(calls.values());
Alexandre Lision72e37322013-11-04 17:14:11 -050098 for (int i = 0; i < all_calls.size(); ++i) {
alision2ec64f92013-06-17 17:28:58 -040099 duration += all_calls.get(i).getDuration();
100 }
Alexandre Lision72e37322013-11-04 17:14:11 -0500101
alision2ec64f92013-06-17 17:28:58 -0400102 if (duration < 60)
103 return duration + "s";
104
105 return duration / 60 + "min";
106 }
alision907bde72013-06-20 14:40:37 -0400107
108 public int getMissed_sum() {
109 return missed_sum;
110 }
111
112 public int getOutgoing_sum() {
113 return outgoing_sum;
114 }
115
116 public int getIncoming_sum() {
117 return incoming_sum;
118 }
Alexandre Lision2e52d392013-11-06 15:14:31 -0500119
120 @Override
121 public int describeContents() {
122 return 0;
123 }
124
125 @Override
126 public void writeToParcel(Parcel dest, int flags) {
127
128 dest.writeParcelable(contact, 0);
129
130 dest.writeList(new ArrayList<HistoryCall>(calls.values()));
131 dest.writeList(new ArrayList<Long>(calls.keySet()));
132
133 dest.writeMap(calls);
134 dest.writeString(accountID);
135 dest.writeInt(missed_sum);
136 dest.writeInt(outgoing_sum);
137 dest.writeInt(incoming_sum);
138
139 }
140
141 public static final Parcelable.Creator<HistoryEntry> CREATOR = new Parcelable.Creator<HistoryEntry>() {
142 public HistoryEntry createFromParcel(Parcel in) {
143 return new HistoryEntry(in);
144 }
145
146 public HistoryEntry[] newArray(int size) {
147 return new HistoryEntry[size];
148 }
149 };
150
151 private HistoryEntry(Parcel in) {
152 contact = in.readParcelable(CallContact.class.getClassLoader());
153
154 ArrayList<HistoryCall> values = new ArrayList<HistoryEntry.HistoryCall>();
155 in.readList(values, HistoryCall.class.getClassLoader());
156
157 ArrayList<Long> keys = new ArrayList<Long>();
158 in.readList(keys, Long.class.getClassLoader());
159
160 calls = new TreeMap<Long, HistoryEntry.HistoryCall>();
161 for (int i = 0; i < keys.size(); ++i) {
162 calls.put(keys.get(i), values.get(i));
163 }
164
165 accountID = in.readString();
166 missed_sum = in.readInt();
167 outgoing_sum = in.readInt();
168 incoming_sum = in.readInt();
169 }
170
171 public static class HistoryCall implements Parcelable {
172 long call_start;
173 long call_end;
174 String number;
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500175
176 boolean missed;
177 String direction;
178
Alexandre Lision2e52d392013-11-06 15:14:31 -0500179 String recordPath;
180 String timeFormatted;
181 String displayName;
182
183 public HistoryCall(HashMap<String, String> entry) {
184 call_end = Long.parseLong(entry.get(ServiceConstants.history.TIMESTAMP_STOP_KEY));
185 call_start = Long.parseLong(entry.get(ServiceConstants.history.TIMESTAMP_START_KEY));
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500186
187 direction = entry.get(ServiceConstants.history.DIRECTION_KEY);
188 missed = entry.get(ServiceConstants.history.MISSED_KEY).contentEquals("true");
189
Alexandre Lision2e52d392013-11-06 15:14:31 -0500190 displayName = entry.get(ServiceConstants.history.DISPLAY_NAME_KEY);
191 recordPath = entry.get(ServiceConstants.history.RECORDING_PATH_KEY);
192 number = entry.get(ServiceConstants.history.PEER_NUMBER_KEY);
193 timeFormatted = HistoryManager.timeToHistoryConst(call_start);
194 }
195
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500196 public String getDirection() {
197 return direction;
198 }
199
Alexandre Lision2e52d392013-11-06 15:14:31 -0500200 public String getDate() {
201 return timeFormatted;
202 }
203
204 public String getDurationString() {
205
206 long duration = call_end - call_start;
207 if (duration < 60)
208 return String.format(Locale.getDefault(), "%02d secs", duration);
209
210 if (duration < 3600)
211 return String.format(Locale.getDefault(), "%02d mins %02d secs", (duration % 3600) / 60, (duration % 60));
212
213 return String.format(Locale.getDefault(), "%d h %02d mins %02d secs", duration / 3600, (duration % 3600) / 60, (duration % 60));
214
215 }
216
217 public long getDuration() {
218 return call_end - call_start;
219
220 }
221
222 public String getRecordPath() {
223 return recordPath;
224 }
225
226 public String getNumber() {
227 return number;
228 }
229
230 public String getDisplayName() {
231 return displayName;
232 }
233
Alexandre Lision2e52d392013-11-06 15:14:31 -0500234 @Override
235 public int describeContents() {
236 return 0;
237 }
238
239 @Override
240 public void writeToParcel(Parcel dest, int flags) {
241 dest.writeLong(call_start);
242 dest.writeLong(call_end);
243 dest.writeString(number);
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500244 dest.writeByte((byte) (missed ? 1 : 0));
245 dest.writeString(direction);
Alexandre Lision2e52d392013-11-06 15:14:31 -0500246 dest.writeString(recordPath);
247 dest.writeString(timeFormatted);
248 dest.writeString(displayName);
249 }
250
251 public static final Parcelable.Creator<HistoryCall> CREATOR = new Parcelable.Creator<HistoryCall>() {
252 public HistoryCall createFromParcel(Parcel in) {
253 return new HistoryCall(in);
254 }
255
256 public HistoryCall[] newArray(int size) {
257 return new HistoryCall[size];
258 }
259 };
260
261 private HistoryCall(Parcel in) {
262 call_start = in.readLong();
263 call_end = in.readLong();
264 number = in.readString();
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500265 missed = in.readByte() == 1 ? true : false;
266 direction = in.readString();
Alexandre Lision2e52d392013-11-06 15:14:31 -0500267 recordPath = in.readString();
268 timeFormatted = in.readString();
269 displayName = in.readString();
270 }
271
272 public boolean hasRecord() {
273 return recordPath.length() > 0;
274 }
275
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500276 public boolean isIncoming() {
277 return direction.contentEquals(ServiceConstants.history.INCOMING_STRING);
278 }
279
280 public boolean isMissed() {
281 return missed;
282 }
283
Alexandre Lision2e52d392013-11-06 15:14:31 -0500284 }
285
alision2ec64f92013-06-17 17:28:58 -0400286}